-
Notifications
You must be signed in to change notification settings - Fork 276
Add basic support for tracing #1524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a8fd37b
add tracing utils
edgchen1 5639d8c
add basic tracing
edgchen1 0039ac4
add tracing to API calls
edgchen1 add54d0
rename GetEnvironmentVariable to GetEnv
edgchen1 79f7288
add cmake option, document stuff
edgchen1 6f66c92
Apply suggestions from code review
edgchen1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #include "tracing.h" | ||
|
|
||
| #include <chrono> | ||
| #include <fstream> | ||
| #include <mutex> | ||
| #include <optional> | ||
| #include <sstream> | ||
| #include <thread> | ||
|
|
||
| #include "models/env_utils.h" | ||
|
|
||
| namespace Generators { | ||
|
|
||
| #if defined(ORTGENAI_ENABLE_TRACING) | ||
|
|
||
| namespace { | ||
|
|
||
| // Writes trace events to a file in Chrome tracing format. | ||
| // See more details about the format here: | ||
| // https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU | ||
| class FileTraceSink : public TraceSink { | ||
| public: | ||
| FileTraceSink(std::string_view file_path) | ||
| : ostream_{std::ofstream{file_path.data()}}, | ||
| start_{Clock::now()}, | ||
| insert_event_delimiter_{false} { | ||
| ostream_ << "["; | ||
| } | ||
|
|
||
| ~FileTraceSink() { | ||
| ostream_ << "]\n"; | ||
| } | ||
|
|
||
| void BeginDuration(std::string_view label) { | ||
| LogEvent("B", label); | ||
| } | ||
|
|
||
| void EndDuration() { | ||
| LogEvent("E"); | ||
| } | ||
|
|
||
| private: | ||
| using Clock = std::chrono::steady_clock; | ||
|
|
||
| void LogEvent(std::string_view phase_type, std::optional<std::string_view> label = std::nullopt) { | ||
| const auto thread_id = std::this_thread::get_id(); | ||
| const auto ts = std::chrono::duration_cast<std::chrono::microseconds>(Clock::now() - start_); | ||
|
|
||
| std::ostringstream event{}; | ||
|
|
||
| event << "{"; | ||
|
|
||
| if (label.has_value()) { | ||
| event << "\"name\": \"" << *label << "\", "; | ||
| } | ||
|
|
||
| event << "\"cat\": \"perf\", " | ||
| << "\"ph\": \"" << phase_type << "\", " | ||
| << "\"pid\": 0, " | ||
| << "\"tid\": " << thread_id << ", " | ||
| << "\"ts\": " << ts.count() | ||
| << "}"; | ||
|
|
||
| { | ||
| std::scoped_lock g{output_mutex_}; | ||
|
|
||
| // add the delimiter only after writing the first event | ||
| if (insert_event_delimiter_) { | ||
| ostream_ << ",\n"; | ||
| } else { | ||
| insert_event_delimiter_ = true; | ||
| } | ||
|
|
||
| ostream_ << event.str(); | ||
| } | ||
| } | ||
|
|
||
| std::ofstream ostream_; | ||
| const Clock::time_point start_; | ||
| bool insert_event_delimiter_; | ||
|
|
||
| std::mutex output_mutex_; | ||
| }; | ||
|
|
||
| std::string GetTraceFileName() { | ||
| constexpr const char* kTraceFileEnvironmentVariableName = "ORTGENAI_TRACE_FILE_PATH"; | ||
| auto trace_file_name = GetEnv(kTraceFileEnvironmentVariableName); | ||
| if (trace_file_name.empty()) { | ||
| trace_file_name = "ortgenai_trace.log"; | ||
| } | ||
| return trace_file_name; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| #endif // defined(ORTGENAI_ENABLE_TRACING) | ||
|
|
||
| Tracer::Tracer() { | ||
| #if defined(ORTGENAI_ENABLE_TRACING) | ||
| const auto trace_file_name = GetTraceFileName(); | ||
| sink_ = std::make_unique<FileTraceSink>(trace_file_name); | ||
| #endif | ||
| } | ||
|
|
||
| void Tracer::BeginDuration(std::string_view label) { | ||
| #if defined(ORTGENAI_ENABLE_TRACING) | ||
| sink_->BeginDuration(label); | ||
| #else | ||
| static_cast<void>(label); | ||
| #endif | ||
| } | ||
|
|
||
| void Tracer::EndDuration() { | ||
| #if defined(ORTGENAI_ENABLE_TRACING) | ||
| sink_->EndDuration(); | ||
| #endif | ||
| } | ||
|
|
||
| Tracer& DefaultTracerInstance() { | ||
| static auto tracer = Tracer{}; | ||
| return tracer; | ||
| } | ||
|
|
||
| } // namespace Generators |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| // Build with CMake option ENABLE_TRACING=ON to enable tracing. | ||
| // To avoid performance overhead, tracing is not enabled by default. | ||
|
|
||
| // When tracing is enabled, the trace data will be recorded to a file. | ||
| // The trace file path can be specified with the environment variable ORTGENAI_TRACE_FILE_PATH. | ||
| // The trace file can be viewed with Perfetto UI (https://ui.perfetto.dev/). | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <string_view> | ||
|
|
||
| namespace Generators { | ||
|
|
||
| // Trace consumer interface. | ||
| class TraceSink { | ||
| public: | ||
| virtual void BeginDuration(std::string_view label) = 0; | ||
| virtual void EndDuration() = 0; | ||
| virtual ~TraceSink() = default; | ||
| }; | ||
|
|
||
| // Main tracing class. | ||
| class Tracer { | ||
| public: | ||
| Tracer(); | ||
|
|
||
| // Begins a traced duration with the given label. | ||
| void BeginDuration(std::string_view label); | ||
|
|
||
| // Ends the traced duration from the most recent call to BeginDuration() in the same thread. | ||
| void EndDuration(); | ||
|
|
||
| private: | ||
| Tracer(const Tracer&) = delete; | ||
| Tracer& operator=(const Tracer&) = delete; | ||
| Tracer(Tracer&&) = delete; | ||
| Tracer& operator=(Tracer&&) = delete; | ||
|
|
||
| #if defined(ORTGENAI_ENABLE_TRACING) | ||
| std::unique_ptr<TraceSink> sink_; | ||
| #endif | ||
| }; | ||
|
|
||
| // Gets the default tracer instance. | ||
| Tracer& DefaultTracerInstance(); | ||
|
|
||
| // Records a traced duration while in scope. | ||
| class DurationTrace { | ||
| public: | ||
| [[nodiscard]] DurationTrace(std::string_view label) | ||
| : DurationTrace{DefaultTracerInstance(), label} { | ||
| } | ||
|
|
||
| [[nodiscard]] DurationTrace(Tracer& tracer, std::string_view label) | ||
| : tracer_{tracer} { | ||
| tracer_.BeginDuration(label); | ||
| } | ||
|
|
||
| ~DurationTrace() { | ||
| tracer_.EndDuration(); | ||
| } | ||
|
|
||
| private: | ||
| DurationTrace(const DurationTrace&) = delete; | ||
| DurationTrace& operator=(const DurationTrace&) = delete; | ||
| DurationTrace(DurationTrace&&) = delete; | ||
| DurationTrace& operator=(DurationTrace&&) = delete; | ||
|
|
||
| Tracer& tracer_; | ||
| }; | ||
|
|
||
| } // namespace Generators |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.