|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +#include "tracing.h" |
| 5 | + |
| 6 | +#include <chrono> |
| 7 | +#include <fstream> |
| 8 | +#include <mutex> |
| 9 | +#include <optional> |
| 10 | +#include <sstream> |
| 11 | +#include <thread> |
| 12 | + |
| 13 | +#include "models/env_utils.h" |
| 14 | + |
| 15 | +namespace Generators { |
| 16 | + |
| 17 | +#if defined(ORTGENAI_ENABLE_TRACING) |
| 18 | + |
| 19 | +namespace { |
| 20 | + |
| 21 | +// Writes trace events to a file in Chrome tracing format. |
| 22 | +// See more details about the format here: |
| 23 | +// https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU |
| 24 | +class FileTraceSink : public TraceSink { |
| 25 | + public: |
| 26 | + FileTraceSink(std::string_view file_path) |
| 27 | + : ostream_{std::ofstream{file_path.data()}}, |
| 28 | + start_{Clock::now()}, |
| 29 | + insert_event_delimiter_{false} { |
| 30 | + ostream_ << "["; |
| 31 | + } |
| 32 | + |
| 33 | + ~FileTraceSink() { |
| 34 | + ostream_ << "]\n"; |
| 35 | + } |
| 36 | + |
| 37 | + void BeginDuration(std::string_view label) { |
| 38 | + LogEvent("B", label); |
| 39 | + } |
| 40 | + |
| 41 | + void EndDuration() { |
| 42 | + LogEvent("E"); |
| 43 | + } |
| 44 | + |
| 45 | + private: |
| 46 | + using Clock = std::chrono::steady_clock; |
| 47 | + |
| 48 | + void LogEvent(std::string_view phase_type, std::optional<std::string_view> label = std::nullopt) { |
| 49 | + const auto thread_id = std::this_thread::get_id(); |
| 50 | + const auto ts = std::chrono::duration_cast<std::chrono::microseconds>(Clock::now() - start_); |
| 51 | + |
| 52 | + std::ostringstream event{}; |
| 53 | + |
| 54 | + event << "{"; |
| 55 | + |
| 56 | + if (label.has_value()) { |
| 57 | + event << "\"name\": \"" << *label << "\", "; |
| 58 | + } |
| 59 | + |
| 60 | + event << "\"cat\": \"perf\", " |
| 61 | + << "\"ph\": \"" << phase_type << "\", " |
| 62 | + << "\"pid\": 0, " |
| 63 | + << "\"tid\": " << thread_id << ", " |
| 64 | + << "\"ts\": " << ts.count() |
| 65 | + << "}"; |
| 66 | + |
| 67 | + { |
| 68 | + std::scoped_lock g{output_mutex_}; |
| 69 | + |
| 70 | + // add the delimiter only after writing the first event |
| 71 | + if (insert_event_delimiter_) { |
| 72 | + ostream_ << ",\n"; |
| 73 | + } else { |
| 74 | + insert_event_delimiter_ = true; |
| 75 | + } |
| 76 | + |
| 77 | + ostream_ << event.str(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + std::ofstream ostream_; |
| 82 | + const Clock::time_point start_; |
| 83 | + bool insert_event_delimiter_; |
| 84 | + |
| 85 | + std::mutex output_mutex_; |
| 86 | +}; |
| 87 | + |
| 88 | +std::string GetTraceFileName() { |
| 89 | + constexpr const char* kTraceFileEnvironmentVariableName = "ORTGENAI_TRACE_FILE_PATH"; |
| 90 | + auto trace_file_name = GetEnv(kTraceFileEnvironmentVariableName); |
| 91 | + if (trace_file_name.empty()) { |
| 92 | + trace_file_name = "ortgenai_trace.log"; |
| 93 | + } |
| 94 | + return trace_file_name; |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace |
| 98 | + |
| 99 | +#endif // defined(ORTGENAI_ENABLE_TRACING) |
| 100 | + |
| 101 | +Tracer::Tracer() { |
| 102 | +#if defined(ORTGENAI_ENABLE_TRACING) |
| 103 | + const auto trace_file_name = GetTraceFileName(); |
| 104 | + sink_ = std::make_unique<FileTraceSink>(trace_file_name); |
| 105 | +#endif |
| 106 | +} |
| 107 | + |
| 108 | +void Tracer::BeginDuration(std::string_view label) { |
| 109 | +#if defined(ORTGENAI_ENABLE_TRACING) |
| 110 | + sink_->BeginDuration(label); |
| 111 | +#else |
| 112 | + static_cast<void>(label); |
| 113 | +#endif |
| 114 | +} |
| 115 | + |
| 116 | +void Tracer::EndDuration() { |
| 117 | +#if defined(ORTGENAI_ENABLE_TRACING) |
| 118 | + sink_->EndDuration(); |
| 119 | +#endif |
| 120 | +} |
| 121 | + |
| 122 | +Tracer& DefaultTracerInstance() { |
| 123 | + static auto tracer = Tracer{}; |
| 124 | + return tracer; |
| 125 | +} |
| 126 | + |
| 127 | +} // namespace Generators |
0 commit comments