• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 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 PANDA_RUNTIME_TIMING_H
17 #define PANDA_RUNTIME_TIMING_H
18 
19 #include <string>
20 #include <iostream>
21 #include <string_view>
22 
23 #include "libpandabase/os/time.h"
24 #include "libpandabase/utils/time.h"
25 #include "runtime/include/mem/panda_containers.h"
26 #include "runtime/include/mem/panda_string.h"
27 
28 namespace panda {
29 class Timing {
30 public:
31     Timing() = default;
32 
33     ~Timing() = default;
34 
35     enum class TimeLabelType {
36         BEGIN,
37         END,
38     };
39 
40     class TimeLabel {
41     public:
42         TimeLabel(std::string_view name, uint64_t time, uint64_t cpu_time, TimeLabelType type = TimeLabelType::BEGIN)
name_(name)43             : name_(name), time_(time), cpu_time_(cpu_time), type_(type)
44         {
45         }
46 
47         ~TimeLabel() = default;
48 
GetType()49         TimeLabelType GetType() const
50         {
51             return type_;
52         }
53 
GetName()54         std::string_view GetName() const
55         {
56             return name_;
57         }
58 
GetTime()59         uint64_t GetTime() const
60         {
61             return time_;
62         }
63 
SetTime(uint64_t duration)64         void SetTime(uint64_t duration)
65         {
66             time_ = duration;
67         }
68 
GetCPUTime()69         uint64_t GetCPUTime() const
70         {
71             return cpu_time_;
72         }
73 
SetCPUTime(uint64_t duration)74         void SetCPUTime(uint64_t duration)
75         {
76             cpu_time_ = duration;
77         }
78 
79         DEFAULT_COPY_SEMANTIC(TimeLabel);
80         DEFAULT_MOVE_SEMANTIC(TimeLabel);
81 
82     private:
83         std::string_view name_;
84         uint64_t time_;      //  After processed, time_ is used to save duration.
85         uint64_t cpu_time_;  //  After processed, cpu_time_ is used to save duration.
86         TimeLabelType type_;
87     };
88 
NewSection(std::string_view tag)89     void NewSection(std::string_view tag)
90     {
91         labels_.push_back(TimeLabel(tag, time::GetCurrentTimeInNanos(), panda::os::time::GetClockTimeInThreadCpuTime(),
92                                     TimeLabelType::BEGIN));
93     }
94 
EndSection()95     void EndSection()
96     {
97         labels_.push_back(TimeLabel("", time::GetCurrentTimeInNanos(), panda::os::time::GetClockTimeInThreadCpuTime(),
98                                     TimeLabelType::END));
99     }
100 
101     PandaString Dump();
102 
Reset()103     void Reset()
104     {
105         labels_.clear();
106     }
107 
108     static PandaString PrettyTimeNs(uint64_t duration);
109 
110 private:
111     void Process();
112 
113     PandaVector<TimeLabel> labels_;
114 
115     NO_MOVE_SEMANTIC(Timing);
116     NO_COPY_SEMANTIC(Timing);
117 };
118 
119 class ScopedTiming {
120 public:
121     // NOLINTNEXTLINE(google-runtime-references)
ScopedTiming(std::string_view tag,Timing & timing)122     ScopedTiming(std::string_view tag, Timing &timing) : timing_(timing)
123     {
124         timing_.NewSection(tag);
125     }
~ScopedTiming()126     ~ScopedTiming()
127     {
128         timing_.EndSection();
129     }
130 
131     NO_MOVE_SEMANTIC(ScopedTiming);
132     NO_COPY_SEMANTIC(ScopedTiming);
133 
134 private:
135     Timing &timing_;
136 };
137 
138 }  // namespace panda
139 #endif  // PANDA_RUNTIME_TIMING_H
140