1 /*
2 * Copyright (c) 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 #include "hiview_event_cacher.h"
16
17 #include "hiview_event_common.h"
18 #include "plugin_stats_event_factory.h"
19
20 namespace OHOS {
21 namespace HiviewDFX {
22 using namespace PluginStatsEventSpace;
23
AddPluginStatsEvent(const std::vector<std::shared_ptr<LoggerEvent>> & events)24 void HiviewEventCacher::AddPluginStatsEvent(const std::vector<std::shared_ptr<LoggerEvent>>& events)
25 {
26 std::lock_guard<std::mutex> lock(mutex_);
27 for (auto event : events) {
28 std::string name = event->GetValue(KEY_OF_PLUGIN_NAME).GetString();
29 if (!name.empty()) {
30 pluginStatsEvents_[name] = event;
31 }
32 }
33 }
34
GetPluginStatsEvents(std::vector<std::shared_ptr<LoggerEvent>> & events)35 void HiviewEventCacher::GetPluginStatsEvents(std::vector<std::shared_ptr<LoggerEvent>>& events)
36 {
37 std::lock_guard<std::mutex> lock(mutex_);
38 for (auto entry : pluginStatsEvents_) {
39 events.push_back(entry.second);
40 }
41 }
42
UpdatePluginStatsEvent(const std::string & name,const std::string & procName,uint32_t procTime)43 void HiviewEventCacher::UpdatePluginStatsEvent(const std::string &name, const std::string &procName, uint32_t procTime)
44 {
45 if (name.empty()) {
46 return;
47 }
48 std::lock_guard<std::mutex> lock(mutex_);
49 if (pluginStatsEvents_.find(name) != pluginStatsEvents_.end()) {
50 auto event = pluginStatsEvents_.find(name)->second;
51 event->Update(KEY_OF_PROC_NAME, procName);
52 event->Update(KEY_OF_PROC_TIME, procTime);
53 } else {
54 auto event = std::make_unique<PluginStatsEventFactory>()->Create();
55 event->Update(KEY_OF_PLUGIN_NAME, name);
56 event->Update(KEY_OF_PROC_NAME, procName);
57 event->Update(KEY_OF_PROC_TIME, procTime);
58 pluginStatsEvents_[name] = std::move(event);
59 }
60 }
61
ClearPluginStatsEvents()62 void HiviewEventCacher::ClearPluginStatsEvents()
63 {
64 std::lock_guard<std::mutex> lock(mutex_);
65 pluginStatsEvents_.clear();
66 }
67 } // namespace HiviewDFX
68 } // namespace OHOS
69