• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025-2025 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 HKS_HA_PLUGIN_H
17 #define HKS_HA_PLUGIN_H
18 
19 #include "hks_ha_event_queue.h"
20 #include "hks_event_info.h"
21 #include "hks_type.h"
22 #include "hks_log.h"
23 #include "hks_mem.h"
24 #include <memory>
25 #include <vector>
26 #include <mutex>
27 #include <condition_variable>
28 #include <thread>
29 #include <list>
30 #include <string>
31 #include <cstdint>
32 #include <ctime>
33 #include <singleton.h>
34 
35 constexpr uint32_t MAX_CACHE_SIZE = 50;
36 constexpr time_t MAX_CACHE_DURATION = 3600; // Unit: seconds
37 
38 typedef int32_t (*HksParamSetToEventInfo)(const struct HksParamSet *paramSet, struct HksEventInfo *keyInfo);
39 
40 typedef bool (*HksEventInfoNeedReport)(const struct HksEventInfo *eventInfo);
41 
42 typedef bool (*HksEventInfoIsEqual)(const struct HksEventInfo *info1, const struct HksEventInfo *info2);
43 
44 typedef void (*HksEventInfoAdd)(struct HksEventInfo *info, const struct HksEventInfo *entry);
45 
46 typedef int32_t (*HksEventInfoToMap)(const struct HksEventInfo *info, std::unordered_map<std::string,
47     std::string>& map);
48 
49 typedef struct {
50     uint32_t eventId;
51     HksParamSetToEventInfo eventInfoCreate;
52     HksEventInfoNeedReport needReport;
53     HksEventInfoIsEqual eventInfoEqual;
54     HksEventInfoAdd eventInfoAdd;
55     HksEventInfoToMap eventInfoToMap;
56 } HksEventProcMap;
57 
58 typedef struct {
59     struct HksEventCommonInfo common;
60     std::unordered_map<std::string, std::string> eventMap;
61 } HksEventWithMap;
62 
63 typedef struct {
64     uint32_t eventId;
65     time_t timestamp;
66     struct HksEventInfo *data;
67 } HksEventCacheNode;
68 
69 class HksEventCacheList {
70 public:
HksEventCacheList()71     HksEventCacheList() {}
72 
Add(const HksEventCacheNode & node)73     void Add(const HksEventCacheNode& node)
74     {
75         std::lock_guard<std::mutex> lock(queueMutex_);
76         cacheList.emplace_back(node);
77     }
78 
FindAndUpdate(struct HksEventInfo * eventInfo,HksEventProcMap * procMap)79     bool FindAndUpdate(struct HksEventInfo *eventInfo, HksEventProcMap *procMap)
80     {
81         std::lock_guard<std::mutex> lock(queueMutex_);
82         for (auto& node : cacheList) {
83             if (procMap->eventInfoEqual(node.data, eventInfo)) {
84                 procMap->eventInfoAdd(node.data, eventInfo);
85                 return true;
86             }
87         }
88         return false;
89     }
90 
GetSize()91     uint32_t GetSize() const
92     {
93         return cacheList.size();
94     }
95 
RemoveFront(uint32_t count)96     void RemoveFront(uint32_t count)
97     {
98         std::lock_guard<std::mutex> lock(queueMutex_);
99         auto it = cacheList.begin();
100         for (uint32_t i = 0; i < count && it != cacheList.end(); ++i, ++it) {
101             if (it->data != nullptr) {
102                 HKS_FREE(it->data->common.function);
103                 HKS_FREE(it->data->common.callerInfo.name);
104                 HKS_FREE(it->data->common.result.errMsg);
105                 HKS_FREE(it->data);
106             }
107         }
108         cacheList.erase(cacheList.begin(), it);
109     }
110 
111     std::list<HksEventCacheNode> cacheList;
112 private:
113     mutable std::mutex queueMutex_;
114 };
115 
116 class HksHaPlugin : public OHOS::Singleton<HksHaPlugin> {
117 public:
118     HksHaPlugin();
119 
120     ~HksHaPlugin();
121 
122     void Destroy();
123 
124     void StartWorkerThread();
125 
126     void StopWorkerThread();
127 
128     void HandleEvent(uint32_t eventId, struct HksParamSet *reportParamSet);
129 
130     bool Enqueue(uint32_t eventId, struct HksParamSet *paramSet);
131 
132 private:
133     HksEventQueue queue;
134     std::thread workerThread;
135     std::atomic<bool> stopFlag;
136     HksEventCacheList eventCacheList;
137     std::vector<HksEventProcMap *> eventProcMap;
138 
139     void WorkerThread();
140 
141     void AddEventCache(uint32_t eventId, struct HksEventInfo *eventInfo);
142 
143     int32_t FillEventInfos(uint32_t reportCount, HksEventWithMap *eventsWithMap);
144 
145     int32_t CallBatchReport(uint32_t reportCount, HksEventWithMap *eventsWithMap);
146 
147     void RemoveReportedEvents(uint32_t reportCount);
148 
149     int32_t BatchReportEvents(uint32_t reportCount);
150 
151     void HandleFaultEvent(struct HksEventCommonInfo *eventInfo, std::unordered_map<std::string, std::string> &eventMap);
152 
153     void HandleStatisticEvent(struct HksEventInfo *eventInfo, uint32_t eventId, HksEventProcMap *procMap);
154 
155     HksEventProcMap* HksEventProcFind(uint32_t eventId);
156 
157     void HandlerReport(HksEventQueueItem &item);
158 };
159 
160 #ifdef __cplusplus
161 extern "C" {
162 #endif
163 
164 int32_t HksHaPluginInit(void);
165 
166 void HksHaPluginDestroy(HksHaPlugin *plugin);
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #endif