1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 PROFILER_LOG_INFO(LOG_CORE, "AddCapability({%s, %s}) SUCCESS!",
41 capability.name().c_str(), capability.path().c_str());
42 return true;
43 }
44
GetCapability(const std::string & name) const45 const ProfilerPluginCapability* ProfilerCapabilityManager::GetCapability(const std::string& name) const
46 {
47 std::lock_guard<std::mutex> guard(mutex_);
48 for (size_t i = 0; i < pluginCapabilities_.size(); i++) {
49 if (pluginCapabilities_[i].name() == name) {
50 return &pluginCapabilities_[i];
51 }
52 }
53 return nullptr;
54 }
55
GetCapabilities() const56 std::vector<ProfilerPluginCapability> ProfilerCapabilityManager::GetCapabilities() const
57 {
58 std::lock_guard<std::mutex> guard(mutex_);
59 return pluginCapabilities_;
60 }
61
UpdateCapability(const std::string & name,const ProfilerPluginCapability & capability)62 bool ProfilerCapabilityManager::UpdateCapability(const std::string& name, const ProfilerPluginCapability& capability)
63 {
64 std::lock_guard<std::mutex> guard(mutex_);
65 for (auto it = pluginCapabilities_.begin(); it != pluginCapabilities_.end(); it++) {
66 if (it->name() == capability.name()) {
67 *it = capability;
68 PROFILER_LOG_WARN(LOG_CORE, "UpdateCapability(%s, %s) SUCCESS!", it->name().c_str(), it->path().c_str());
69 return true;
70 }
71 }
72 return false;
73 }
74
RemoveCapability(const std::string & name)75 bool ProfilerCapabilityManager::RemoveCapability(const std::string& name)
76 {
77 std::lock_guard<std::mutex> guard(mutex_);
78 for (auto it = pluginCapabilities_.begin(); it != pluginCapabilities_.end(); it++) {
79 if (it->name() == name) {
80 PROFILER_LOG_INFO(LOG_CORE, "RemoveCapability({%s, %s}) SUCCESS!", it->name().c_str(), it->path().c_str());
81 pluginCapabilities_.erase(it);
82 return true;
83 }
84 }
85 return false;
86 }
87