1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef MAPLE_UTIL_INCLUDE_MPL_TIMER_H 17 #define MAPLE_UTIL_INCLUDE_MPL_TIMER_H 18 19 #include <chrono> 20 #include <map> 21 #include <string> 22 #include <sstream> 23 #include <iomanip> 24 25 namespace maple { 26 class MPLTimer { 27 public: 28 MPLTimer() = default; 29 ~MPLTimer()= default; 30 Start()31 void Start() 32 { 33 startTime = std::chrono::system_clock::now(); 34 } 35 Stop()36 void Stop() 37 { 38 endTime = std::chrono::system_clock::now(); 39 } 40 Elapsed()41 long Elapsed() const 42 { 43 return std::chrono::duration_cast<std::chrono::seconds>(endTime - startTime).count(); 44 } 45 ElapsedMilliseconds()46 long ElapsedMilliseconds() const 47 { 48 return std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count(); 49 } 50 ElapsedMicroseconds()51 long ElapsedMicroseconds() const 52 { 53 return std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count(); 54 } 55 56 private: 57 std::chrono::system_clock::time_point startTime; 58 std::chrono::system_clock::time_point endTime; 59 }; 60 61 class MPLTimerManager { 62 public: 63 enum class Unit { 64 kMicroSeconds, // us 65 kMilliSeconds, // ms 66 kSeconds, // s 67 }; 68 69 // time data 70 struct Timer { StartTimer71 void Start() noexcept 72 { 73 startTime = std::chrono::system_clock::now(); 74 ++count; 75 } 76 StopTimer77 void Stop() noexcept 78 { 79 useTime += (std::chrono::system_clock::now() - startTime); 80 } 81 82 template <class T = std::chrono::milliseconds> ElapsedTimer83 long Elapsed() const noexcept 84 { 85 return std::chrono::duration_cast<T>(useTime).count(); 86 } 87 88 std::chrono::system_clock::time_point startTime; 89 std::chrono::nanoseconds useTime; 90 uint32_t count = 0; // run count 91 }; 92 93 MPLTimerManager() = default; 94 virtual ~MPLTimerManager() = default; 95 Clear()96 void Clear() 97 { 98 allTimer.clear(); 99 } 100 GetTimerFormKey(const std::string & key)101 Timer &GetTimerFormKey(const std::string &key) 102 { 103 return allTimer[key]; 104 } 105 106 template <Unit unit = Unit::kMilliSeconds> ConvertAllTimer2Str()107 std::string ConvertAllTimer2Str() const 108 { 109 std::ostringstream os; 110 for (auto &[key, timer] : allTimer) { 111 os << "\t" << key << ": "; 112 if constexpr (unit == Unit::kMicroSeconds) { 113 os << timer.Elapsed<std::chrono::microseconds>() << "us"; 114 } else if constexpr (unit == Unit::kMilliSeconds) { 115 os << timer.Elapsed<std::chrono::milliseconds>() << "ms"; 116 } else { 117 static_assert(unit == Unit::kSeconds, "unknown units"); 118 os << timer.Elapsed<std::chrono::seconds>() << "s"; 119 } 120 os << ", count: " << timer.count << std::endl; 121 } 122 return os.str(); 123 } 124 125 private: 126 std::map<std::string, Timer> allTimer; 127 }; 128 129 class MPLTimerRegister { 130 public: MPLTimerRegister(MPLTimerManager & timerM,const std::string & key)131 MPLTimerRegister(MPLTimerManager &timerM, const std::string &key) 132 { 133 timer = &timerM.GetTimerFormKey(key); 134 timer->Start(); 135 } 136 ~MPLTimerRegister()137 ~MPLTimerRegister() 138 { 139 Stop(); 140 } 141 Stop()142 void Stop() noexcept 143 { 144 if (timer != nullptr) { 145 timer->Stop(); 146 timer = nullptr; 147 } 148 } 149 150 private: 151 MPLTimerManager::Timer *timer = nullptr; 152 }; 153 } // namespace maple 154 #endif // MAPLE_UTIL_INCLUDE_MPL_TIMER_H 155