1 /*
2  * Copyright (c) 2022-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 WRITE_CONTROLLER_H
17 #define WRITE_CONTROLLER_H
18 
19 #include <list>
20 #include <mutex>
21 #include <sys/time.h>
22 #include <unordered_map>
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 static constexpr uint64_t INVALID_TIME_STAMP = 0;
27 static constexpr size_t HISYSEVENT_DEFAULT_PERIOD = 5;
28 static constexpr size_t HISYSEVENT_DEFAULT_THRESHOLD = 100;
29 
30 using ControlParam = struct {
31     size_t period;
32     size_t threshold;
33 };
34 
35 using CallerInfo = struct {
36     const char* func;
37     int64_t line;
38     uint64_t timeStamp;
39 };
40 
41 class EventWroteLruCache;
42 
43 class WriteController {
44 public:
45     static uint64_t GetCurrentTimeMills();
46     static uint64_t CheckLimitWritingEvent(const ControlParam& param, const char* domain, const char* eventName,
47         const CallerInfo& callerInfo);
48     static uint64_t CheckLimitWritingEvent(const ControlParam& param, const char* domain, const char* eventName,
49         const char* func, int64_t line);
50 
51 private:
52     static std::shared_ptr<EventWroteLruCache> eventWroteLruCache_;
53 };
54 } // HiviewDFX
55 } // OHOS
56 
57 #endif // WRITE_CONTROLLER_H