• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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();
29         ~MPLTimer();
30         void Start();
31         void Stop();
32         long Elapsed() const;
33         long ElapsedMilliseconds() const;
34         long ElapsedMicroseconds() const;
35 
36     private:
37         std::chrono::system_clock::time_point startTime;
38         std::chrono::system_clock::time_point endTime;
39     };
40 
41 class MPLTimerManager {
42 public:
43     enum class Unit {
44         kMicroSeconds, // us
45         kMilliSeconds, // ms
46         kSeconds,      // s
47     };
48 
49     // time data
50     struct Timer {
StartTimer51         void Start() noexcept
52         {
53             startTime = std::chrono::system_clock::now();
54             ++count;
55         }
56 
StopTimer57         void Stop() noexcept
58         {
59             useTime += (std::chrono::system_clock::now() - startTime);
60         }
61 
62         template <class T = std::chrono::milliseconds>
ElapsedTimer63         long Elapsed() const noexcept
64         {
65             return std::chrono::duration_cast<T>(useTime).count();
66         }
67 
68         std::chrono::system_clock::time_point startTime;
69         std::chrono::nanoseconds useTime;
70         uint32_t count = 0; // run count
71     };
72 
73     MPLTimerManager() = default;
74     virtual ~MPLTimerManager() = default;
75 
Clear()76     void Clear()
77     {
78         allTimer.clear();
79     }
80 
GetTimerFormKey(const std::string & key)81     Timer &GetTimerFormKey(const std::string &key)
82     {
83         return allTimer[key];
84     }
85 
86     template <Unit unit = Unit::kMilliSeconds>
ConvertAllTimer2Str()87     std::string ConvertAllTimer2Str() const
88     {
89         std::ostringstream os;
90         for (auto &[key, timer] : allTimer) {
91             os << "\t" << key << ": ";
92             if constexpr (unit == Unit::kMicroSeconds) {
93                 os << timer.Elapsed<std::chrono::microseconds>() << "us";
94             } else if constexpr (unit == Unit::kMilliSeconds) {
95                 os << timer.Elapsed<std::chrono::milliseconds>() << "ms";
96             } else {
97                 static_assert(unit == Unit::kSeconds, "unknown units");
98                 os << timer.Elapsed<std::chrono::seconds>() << "s";
99             }
100             os << ", count: " << timer.count << std::endl;
101         }
102         return os.str();
103     }
104 
105 private:
106     std::map<std::string, Timer> allTimer;
107 };
108 
109 class MPLTimerRegister {
110 public:
MPLTimerRegister(MPLTimerManager & timerM,const std::string & key)111     MPLTimerRegister(MPLTimerManager &timerM, const std::string &key)
112     {
113         timer = &timerM.GetTimerFormKey(key);
114         timer->Start();
115     }
116 
~MPLTimerRegister()117     ~MPLTimerRegister()
118     {
119         Stop();
120     }
121 
Stop()122     void Stop() noexcept
123     {
124         if (timer != nullptr) {
125             timer->Stop();
126             timer = nullptr;
127         }
128     }
129 
130 private:
131     MPLTimerManager::Timer *timer = nullptr;
132 };
133 } // namespace maple
134 #endif // MAPLE_UTIL_INCLUDE_MPL_TIMER_H
135