• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 HIAPPEVENT_UTIL_H_
17 #define HIAPPEVENT_UTIL_H_
18 #include <memory>
19 #include <mutex>
20 #include <string>
21 #include <vector>
22 
23 #include "hidebug_util.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 
28 class ApiRecordReporter {
29 public:
30     virtual ~ApiRecordReporter() = default;
31     virtual void ReportRecord(const std::string& apiName, int errorCode, int64_t beginTime, int64_t endTime) = 0;
32     static void InitProcessor();
33 protected:
34     static int64_t processId_;
35 };
36 
37 class SingleRecordReporter final : public ApiRecordReporter {
38 public:
39     static SingleRecordReporter& GetInstance();
40     SingleRecordReporter(const SingleRecordReporter&) = delete;
41     SingleRecordReporter& operator=(const SingleRecordReporter&) = delete;
42     SingleRecordReporter(SingleRecordReporter&&) = delete;
43     SingleRecordReporter& operator=(SingleRecordReporter&&) = delete;
44     void ReportRecord(const std::string& apiName, int errorCode, int64_t beginTime, int64_t endTime) override;
45 private:
46     SingleRecordReporter() = default;
47     ~SingleRecordReporter() override = default;
48 };
49 
50 class MultipleRecordReporter final : public ApiRecordReporter {
51 public:
52     MultipleRecordReporter(uint32_t timeout, uint32_t limitValue);
53     void ReportRecord(const std::string& apiName, int errorCode, int64_t beginTime, int64_t endTime) override;
54 private:
55     void UploadRecordData(const std::string& apiName) const;
56     std::mutex mutex_;
57     const uint32_t timeout_;
58     const uint32_t limitValue_;
59     int64_t lastReportTime_{GetElapsedNanoSecondsSinceBoot()};
60     int64_t totalCostTime_{0};
61     int64_t maxCostTime_{std::numeric_limits<int64_t>::min()};
62     int64_t minCostTime_{std::numeric_limits<int64_t>::max()};;
63     std::vector<int32_t> records_{};
64 };
65 
66 class ApiInvokeRecorder {
67 public:
68     explicit ApiInvokeRecorder(std::string apiName, ApiRecordReporter& reporter = SingleRecordReporter::GetInstance());
69     ~ApiInvokeRecorder();
70     void SetErrorCode(int errorCode);
71 
72 private:
73     std::string apiName_;
74     int errorCode_{0};
75     int64_t beginTime_;
76     ApiRecordReporter& reporter_;
77 };
78 }
79 }
80 #endif // HIAPPEVENT_UTIL_H_
81