Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <atomic>
#include <fstream>
#include <initializer_list>
#include <iostream>
#include <tuple>
 
#include "core/common/profiler_common.h"
#include "core/common/logging/logging.h"
#include <mutex>
 
namespace onnxruntime {
 
namespace profiling {
 
// uncomment the macro below, or use -DENABLE_STATIC_PROFILER_INSTANCE for debugging
// note that static profiler instance only works with single session
// #define ENABLE_STATIC_PROFILER_INSTANCE
 
/**
 * Main class for profiling. It continues to accumulate events and produce
 * a corresponding "complete event (X)" in "chrome tracing" format.
 */
class Profiler {
 public:
  /// turned off by default.
  /// Even this function is marked as noexcept, the code inside it may throw exceptions
  Profiler() noexcept {};  // NOLINT
 
  ~Profiler();
 
  /*
  Initializes Profiler with the session logger to log framework specific messages
  */
  void Initialize(const logging::Logger* session_logger);
 
  /*
  Send profiling data to custom logger
  */
  void StartProfiling(const logging::Logger* custom_logger);
 
  /*
  Start profiler and record beginning time.
  */
  template <typename T>
  void StartProfiling(const std::basic_string<T>& file_name);
 
  /*
  Start profiling and return current time point.
  */
  TimePoint Start();
 
  /*
  Whether data collection and output from this profiler is enabled.
  */
  bool IsEnabled() const {
    return enabled_;
  }
  /*
  Return the stored start time of profiler.
  On some platforms, this timer may not be as precise as nanoseconds
  For instance, on Windows and MacOS, the precision (high_resolution_clock) will be ~100ns
  */
  uint64_t GetStartTimeNs() const {
    return std::chrono::duration_cast<std::chrono::nanoseconds>(
               profiling_start_time_.time_since_epoch())
        .count();
  }
  /*
  Record a single event. Time is measured till the call of this function from
  the start_time.
  */
  void EndTimeAndRecordEvent(EventCategory category,
                             const std::string& event_name,
                             const TimePoint& start_time,
                             InlinedHashMap<std::string, std::string> event_args = {},
                             bool sync_gpu = false);
 
#if !defined(ORT_MINIMAL_BUILD)
  void EndTimeAndRecordEvent(EventCategory category,
                             const std::string& event_name,
                             const TimePoint& start_time,
                             const TimePoint& end_time,
                             InlinedHashMap<std::string, std::string> event_args = {},
                             bool sync_gpu = false);
 
  // Records an event without changing execution-provider correlation state.
  void RecordEvent(EventCategory category,
                   const std::string& event_name,
                   const TimePoint& start_time,
                   const TimePoint& end_time,
                   InlinedHashMap<std::string, std::string> event_args = {});
#endif
 
  /*
  Write profile data to the given stream in chrome format defined below.
  https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview#
  */
  std::string EndProfiling();
 
  static Profiler& Instance() {
#ifdef ENABLE_STATIC_PROFILER_INSTANCE
    ORT_ENFORCE(instance_ != nullptr);
    return *instance_;
#else
    ORT_THROW("Static profiler instance is not enabled, please compile with -DENABLE_STATIC_PROFILER_INSTANCE");
#endif
  }
 
  /*
  Gets the maximum event count to set for new profiler instances.
  */
  static size_t GetGlobalMaxNumEvents() {
    return global_max_num_events_.load();
  }
 
  /*
  Sets the maximum event count to set for new profiler instances.
  Existing profiler instances will not be affected.
  */
  static void SetGlobalMaxNumEvents(size_t new_max_num_events) {
    global_max_num_events_.store(new_max_num_events);
  }
 
#if !defined(ORT_MINIMAL_BUILD)
  // Testing only. Must be called before profiling starts.
  void SetMaxNumEventsForTest(size_t max_num_events) {
    ORT_ENFORCE(!enabled_ && events_.empty());
    max_num_events_ = max_num_events;
  }
#endif
 
  void AddEpProfilers(std::unique_ptr<EpProfiler> ep_profiler) {
    if (ep_profiler) {
      ep_profilers_.push_back(std::move(ep_profiler));
      if (enabled_) {
        auto status = ep_profilers_.back()->StartProfiling(profiling_start_time_);
        if (!status.IsOK() && ep_start_profiling_status_.IsOK()) {
          ep_start_profiling_status_ = status;
        }
      }
    }
  }
 
  /// Returns the aggregate status from calling StartProfiling on EP profilers.
  /// OK if all EP profilers started successfully (or if none are registered).
  /// Returns the first error status encountered otherwise.
  const Status& GetEpProfilingStatus() const { return ep_start_profiling_status_; }
 
  /// Returns true if at least one EP profiler was registered.
  bool HasEpProfilers() const { return !ep_profilers_.empty(); }
 
 private:
  ORT_DISALLOW_COPY_ASSIGNMENT_AND_MOVE(Profiler);
 
  /**
   * The maximum number of profiler records to collect.
   * This value is used to initialize the per-profiler maximum.
   * It can be set, but won't affect existing profilers.
   */
  static std::atomic<size_t> global_max_num_events_;
 
#if !defined(ORT_MINIMAL_BUILD)
  void RecordEventImpl(EventCategory category,
                       const std::string& event_name,
                       const TimePoint& start_time,
                       const TimePoint& end_time,
                       InlinedHashMap<std::string, std::string> event_args,
                       bool stop_ep_profilers);
#endif
 
  // Mutex controlling access to profiler data
  std::mutex mutex_;
  bool enabled_{false};
#if defined(__wasm__)
  /*
   * The simplest way to emit profiling data in WebAssembly is to print out to console,
   * since browsers can't access to a file system directly.
   * TODO: Consider MEMFS or IndexedDB instead of console.
   */
  std::ostream& profile_stream_{std::cout};
#else
  std::ofstream profile_stream_;
#endif
  std::string profile_stream_file_;
  const logging::Logger* session_logger_{nullptr};
  const logging::Logger* custom_logger_{nullptr};
  TimePoint profiling_start_time_;
  Events events_;
  bool max_events_reached{false};
#if !defined(ORT_MINIMAL_BUILD)
  size_t dropped_event_count_{0};
#endif
  bool profile_with_logger_{false};
#if !defined(ORT_MINIMAL_BUILD)
  size_t max_num_events_{global_max_num_events_.load()};
#else
  const size_t max_num_events_{global_max_num_events_.load()};
#endif
 
#ifdef ENABLE_STATIC_PROFILER_INSTANCE
  static Profiler* instance_;
#endif
 
  std::vector<std::unique_ptr<EpProfiler>> ep_profilers_;
 
  // Aggregate status from EP profiler StartProfiling calls.
  // Stores the first error encountered.
  Status ep_start_profiling_status_;
};
 
}  // namespace profiling
}  // namespace onnxruntime