• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 TIME_PERF_H
17 #define TIME_PERF_H
18 
19 #include <string_view>
20 #include <sys/time.h>
21 #include <unordered_map>
22 #include <mutex>
23 #include "nocopyable.h"
24 
25 namespace OHOS {
26 namespace Media {
27 #define SPLICE_IMPL(a, b) a ## b
28 #define SPLICE(a, b) SPLICE_IMPL(a, b)
29 
30 #define AUTO_PERF(obj, tag) \
31     AutoPerf SPLICE(autoPerf, __LINE__)(reinterpret_cast<uintptr_t>(obj), tag)
32 #define ASYNC_PERF_START(obj, tag) \
33     TimePerf::Inst().StartPerfRecord(reinterpret_cast<uintptr_t>(obj), tag)
34 #define ASYNC_PERF_STOP(obj, tag) \
35     TimePerf::Inst().StopPerfRecord(reinterpret_cast<uintptr_t>(obj), tag)
36 #define CLEAN_PERF_RECORD(obj) \
37     TimePerf::Inst().DumpObjectRecord(reinterpret_cast<uintptr_t>(obj)); \
38     TimePerf::Inst().CleanObjectRecord(reinterpret_cast<uintptr_t>(obj))
39 
40 class __attribute__((visibility("default"))) TimePerf {
41 public:
Inst()42     static TimePerf &Inst()
43     {
44         static TimePerf inst;
45         return inst;
46     }
47 
48     void StartPerfRecord(uintptr_t obj, std::string_view tag);
49     void StopPerfRecord(uintptr_t obj, std::string_view tag);
50     void DumpObjectRecord(uintptr_t obj);
51     void CleanObjectRecord(uintptr_t obj);
52     void DumpAllRecord();
53     void CleanAllRecord();
54 
55 private:
56     TimePerf() = default;
57     ~TimePerf() = default;
58 
59     void TimeVal2USec(const struct timeval &time, int64_t &usec);
60 
61     struct PerfRecord {
62         int64_t currStart; // microsecond
63         int64_t currStop;  // microsecond
64         int64_t peakTime;
65         int64_t firstTime;
66         int64_t avgTime;
67         int64_t count;
68     };
69 
70     using TagRecords = std::unordered_map<std::string_view, PerfRecord>;
71     std::unordered_map<uintptr_t, TagRecords> objPerfRecords_;
72     std::mutex mutex_;
73 };
74 
75 struct __attribute__((visibility("default"))) AutoPerf : public NoCopyable {
AutoPerfAutoPerf76     AutoPerf(uintptr_t obj, std::string_view tag) : obj_(obj), tag_(tag)
77     {
78         TimePerf::Inst().StartPerfRecord(obj, tag);
79     }
80 
~AutoPerfAutoPerf81     ~AutoPerf()
82     {
83         TimePerf::Inst().StopPerfRecord(obj_, tag_);
84     }
85 
86     uintptr_t obj_;
87     std::string_view tag_;
88 };
89 } // namespace Media
90 } // namespace OHOS
91 
92 #endif
93