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 #include "profiler_capability_manager.h"
17
18 #include <algorithm>
19 #include "logging.h"
20
GetInstance()21 ProfilerCapabilityManager& ProfilerCapabilityManager::GetInstance()
22 {
23 static ProfilerCapabilityManager instance;
24 return instance;
25 }
26
~ProfilerCapabilityManager()27 ProfilerCapabilityManager::~ProfilerCapabilityManager()
28 {
29 pluginCapabilities_.clear();
30 }
31
AddCapability(const ProfilerPluginCapability & capability)32 bool ProfilerCapabilityManager::AddCapability(const ProfilerPluginCapability& capability)
33 {
34 std::lock_guard<std::mutex> guard(mutex_);
35 for (auto it = pluginCapabilities_.begin(); it != pluginCapabilities_.end(); it++) {
36 if (it->name() == capability.name()) {
37 HILOG_WARN(LOG_CORE, "capability.name conflict with %zu", (it - pluginCapabilities_.begin()));
38 return false;
39 }
40 }
41 pluginCapabilities_.push_back(capability);
42 HILOG_INFO(LOG_CORE, "AddCapability({%s, %s}) SUCCESS!", capability.name().c_str(), capability.path().c_str());
43 return true;
44 }
45
GetCapability(const std::string & name) const46 const ProfilerPluginCapability* ProfilerCapabilityManager::GetCapability(const std::string& name) const
47 {
48 std::lock_guard<std::mutex> guard(mutex_);
49 for (size_t i = 0; i < pluginCapabilities_.size(); i++) {
50 if (pluginCapabilities_[i].name() == name) {
51 return &pluginCapabilities_[i];
52 }
53 }
54 return nullptr;
55 }
56
GetCapabilities() const57 std::vector<ProfilerPluginCapability> ProfilerCapabilityManager::GetCapabilities() const
58 {
59 std::lock_guard<std::mutex> guard(mutex_);
60 return pluginCapabilities_;
61 }
62
UpdateCapability(const std::string & name,const ProfilerPluginCapability & capability)63 bool ProfilerCapabilityManager::UpdateCapability(const std::string& name, const ProfilerPluginCapability& capability)
64 {
65 std::lock_guard<std::mutex> guard(mutex_);
66 for (auto it = pluginCapabilities_.begin(); it != pluginCapabilities_.end(); it++) {
67 if (it->name() == capability.name()) {
68 *it = capability;
69 HILOG_WARN(LOG_CORE, "UpdateCapability(%s, %s) SUCCESS!", it->name().c_str(), it->path().c_str());
70 return true;
71 }
72 }
73 return false;
74 }
75
RemoveCapability(const std::string & name)76 bool ProfilerCapabilityManager::RemoveCapability(const std::string& name)
77 {
78 std::lock_guard<std::mutex> guard(mutex_);
79 for (auto it = pluginCapabilities_.begin(); it != pluginCapabilities_.end(); it++) {
80 if (it->name() == name) {
81 HILOG_INFO(LOG_CORE, "RemoveCapability({%s, %s}) SUCCESS!", it->name().c_str(), it->path().c_str());
82 pluginCapabilities_.erase(it);
83 return true;
84 }
85 }
86 return false;
87 }
88