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 CHECK_TRUE(it->name() != capability.name(), false,
37 "capability.name conflict with %zu", (it - pluginCapabilities_.begin()));
38 }
39 pluginCapabilities_.push_back(capability);
40 HILOG_INFO(LOG_CORE, "AddCapability({%s, %s}) SUCCESS!", capability.name().c_str(), capability.path().c_str());
41 return true;
42 }
43
GetCapability(const std::string & name) const44 const ProfilerPluginCapability* ProfilerCapabilityManager::GetCapability(const std::string& name) const
45 {
46 std::lock_guard<std::mutex> guard(mutex_);
47 for (size_t i = 0; i < pluginCapabilities_.size(); i++) {
48 if (pluginCapabilities_[i].name() == name) {
49 return &pluginCapabilities_[i];
50 }
51 }
52 return nullptr;
53 }
54
GetCapabilities() const55 std::vector<ProfilerPluginCapability> ProfilerCapabilityManager::GetCapabilities() const
56 {
57 std::lock_guard<std::mutex> guard(mutex_);
58 return pluginCapabilities_;
59 }
60
UpdateCapability(const std::string & name,const ProfilerPluginCapability & capability)61 bool ProfilerCapabilityManager::UpdateCapability(const std::string& name, const ProfilerPluginCapability& capability)
62 {
63 std::lock_guard<std::mutex> guard(mutex_);
64 for (auto it = pluginCapabilities_.begin(); it != pluginCapabilities_.end(); it++) {
65 if (it->name() == capability.name()) {
66 *it = capability;
67 HILOG_WARN(LOG_CORE, "UpdateCapability(%s, %s) SUCCESS!", it->name().c_str(), it->path().c_str());
68 return true;
69 }
70 }
71 return false;
72 }
73
RemoveCapability(const std::string & name)74 bool ProfilerCapabilityManager::RemoveCapability(const std::string& name)
75 {
76 std::lock_guard<std::mutex> guard(mutex_);
77 for (auto it = pluginCapabilities_.begin(); it != pluginCapabilities_.end(); it++) {
78 if (it->name() == name) {
79 HILOG_INFO(LOG_CORE, "RemoveCapability({%s, %s}) SUCCESS!", it->name().c_str(), it->path().c_str());
80 pluginCapabilities_.erase(it);
81 return true;
82 }
83 }
84 return false;
85 }
86