| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2013 The WebRTC Project Authors. All rights reserved. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license | |
| 5 * that can be found in the LICENSE file in the root of the source | |
| 6 * tree. An additional intellectual property rights grant can be found | |
| 7 * in the file PATENTS. All contributing project authors may | |
| 8 * be found in the AUTHORS file in the root of the source tree. | |
| 9 */ | |
| 10 | |
| 11 // A simple wall-clock profiler for instrumented code. | |
| 12 // Example: | |
| 13 // void MyLongFunction() { | |
| 14 // PROFILE_F(); // Time the execution of this function. | |
| 15 // // Do something | |
| 16 // { // Time just what is in this scope. | |
| 17 // PROFILE("My event"); | |
| 18 // // Do something else | |
| 19 // } | |
| 20 // } | |
| 21 // Another example: | |
| 22 // void StartAsyncProcess() { | |
| 23 // PROFILE_START("My async event"); | |
| 24 // DoSomethingAsyncAndThenCall(&Callback); | |
| 25 // } | |
| 26 // void Callback() { | |
| 27 // PROFILE_STOP("My async event"); | |
| 28 // // Handle callback. | |
| 29 // } | |
| 30 | |
| 31 #ifndef WEBRTC_BASE_PROFILER_H_ | |
| 32 #define WEBRTC_BASE_PROFILER_H_ | |
| 33 | |
| 34 #include <map> | |
| 35 #include <string> | |
| 36 | |
| 37 #include "webrtc/base/basictypes.h" | |
| 38 #include "webrtc/base/common.h" | |
| 39 #include "webrtc/base/constructormagic.h" | |
| 40 #include "webrtc/base/logging.h" | |
| 41 #include "webrtc/base/sharedexclusivelock.h" | |
| 42 | |
| 43 // Profiling could be switched via a build flag, but for now, it's always on. | |
| 44 #ifndef ENABLE_PROFILING | |
| 45 #define ENABLE_PROFILING | |
| 46 #endif | |
| 47 | |
| 48 #ifdef ENABLE_PROFILING | |
| 49 | |
| 50 #define UV_HELPER2(x) _uv_ ## x | |
| 51 #define UV_HELPER(x) UV_HELPER2(x) | |
| 52 #define UNIQUE_VAR UV_HELPER(__LINE__) | |
| 53 | |
| 54 // Profiles the current scope. | |
| 55 #define PROFILE(msg) rtc::ProfilerScope UNIQUE_VAR(msg) | |
| 56 // When placed at the start of a function, profiles the current function. | |
| 57 #define PROFILE_F() PROFILE(__FUNCTION__) | |
| 58 // Reports current timings to the log at severity |sev|. | |
| 59 #define PROFILE_DUMP_ALL(sev) \ | |
| 60 rtc::Profiler::Instance()->ReportAllToLog(__FILE__, __LINE__, sev) | |
| 61 // Reports current timings for all events whose names are prefixed by |prefix| | |
| 62 // to the log at severity |sev|. Using a unique event name as |prefix| will | |
| 63 // report only that event. | |
| 64 #define PROFILE_DUMP(sev, prefix) \ | |
| 65 rtc::Profiler::Instance()->ReportToLog(__FILE__, __LINE__, sev, prefix) | |
| 66 // Starts and stops a profile event. Useful when an event is not easily | |
| 67 // captured within a scope (eg, an async call with a callback when done). | |
| 68 #define PROFILE_START(msg) rtc::Profiler::Instance()->StartEvent(msg) | |
| 69 #define PROFILE_STOP(msg) rtc::Profiler::Instance()->StopEvent(msg) | |
| 70 // TODO(ryanpetrie): Consider adding PROFILE_DUMP_EVERY(sev, iterations) | |
| 71 | |
| 72 #undef UV_HELPER2 | |
| 73 #undef UV_HELPER | |
| 74 #undef UNIQUE_VAR | |
| 75 | |
| 76 #else // ENABLE_PROFILING | |
| 77 | |
| 78 #define PROFILE(msg) (void)0 | |
| 79 #define PROFILE_F() (void)0 | |
| 80 #define PROFILE_DUMP_ALL(sev) (void)0 | |
| 81 #define PROFILE_DUMP(sev, prefix) (void)0 | |
| 82 #define PROFILE_START(msg) (void)0 | |
| 83 #define PROFILE_STOP(msg) (void)0 | |
| 84 | |
| 85 #endif // ENABLE_PROFILING | |
| 86 | |
| 87 namespace rtc { | |
| 88 | |
| 89 // Tracks information for one profiler event. | |
| 90 class ProfilerEvent { | |
| 91 public: | |
| 92 ProfilerEvent(); | |
| 93 void Start(); | |
| 94 void Stop(); | |
| 95 void Stop(uint64_t stop_time); | |
| 96 double standard_deviation() const; | |
| 97 double total_time() const { return total_time_; } | |
| 98 double mean() const { return mean_; } | |
| 99 double minimum() const { return minimum_; } | |
| 100 double maximum() const { return maximum_; } | |
| 101 int event_count() const { return event_count_; } | |
| 102 bool is_started() const { return start_count_ > 0; } | |
| 103 | |
| 104 private: | |
| 105 uint64_t current_start_time_; | |
| 106 double total_time_; | |
| 107 double mean_; | |
| 108 double sum_of_squared_differences_; | |
| 109 double minimum_; | |
| 110 double maximum_; | |
| 111 int start_count_; | |
| 112 int event_count_; | |
| 113 }; | |
| 114 | |
| 115 // Singleton that owns ProfilerEvents and reports results. Prefer to use | |
| 116 // macros, defined above, rather than directly calling Profiler methods. | |
| 117 class Profiler { | |
| 118 public: | |
| 119 ~Profiler(); | |
| 120 void StartEvent(const std::string& event_name); | |
| 121 void StopEvent(const std::string& event_name); | |
| 122 void ReportToLog(const char* file, int line, LoggingSeverity severity_to_use, | |
| 123 const std::string& event_prefix); | |
| 124 void ReportAllToLog(const char* file, int line, | |
| 125 LoggingSeverity severity_to_use); | |
| 126 const ProfilerEvent* GetEvent(const std::string& event_name) const; | |
| 127 // Clears all _stopped_ events. Returns true if _all_ events were cleared. | |
| 128 bool Clear(); | |
| 129 | |
| 130 static Profiler* Instance(); | |
| 131 private: | |
| 132 Profiler(); | |
| 133 | |
| 134 typedef std::map<std::string, ProfilerEvent> EventMap; | |
| 135 EventMap events_; | |
| 136 mutable SharedExclusiveLock lock_; | |
| 137 | |
| 138 RTC_DISALLOW_COPY_AND_ASSIGN(Profiler); | |
| 139 }; | |
| 140 | |
| 141 // Starts an event on construction and stops it on destruction. | |
| 142 // Used by PROFILE macro. | |
| 143 class ProfilerScope { | |
| 144 public: | |
| 145 explicit ProfilerScope(const std::string& event_name) | |
| 146 : event_name_(event_name) { | |
| 147 Profiler::Instance()->StartEvent(event_name_); | |
| 148 } | |
| 149 ~ProfilerScope() { | |
| 150 Profiler::Instance()->StopEvent(event_name_); | |
| 151 } | |
| 152 private: | |
| 153 std::string event_name_; | |
| 154 | |
| 155 RTC_DISALLOW_COPY_AND_ASSIGN(ProfilerScope); | |
| 156 }; | |
| 157 | |
| 158 std::ostream& operator<<(std::ostream& stream, | |
| 159 const ProfilerEvent& profiler_event); | |
| 160 | |
| 161 } // namespace rtc | |
| 162 | |
| 163 #endif // WEBRTC_BASE_PROFILER_H_ | |
| OLD | NEW |