From 628a1517d3f082cc35aa40966d2e64a503d2a304 Mon Sep 17 00:00:00 2001 From: wanderer-dl122 Date: Fri, 20 May 2022 11:49:43 +0800 Subject: [PATCH] add dumper Signed-off-by: wanderer-dl122 --- .../src/distributed_hardware_service.cpp | 5 +- .../componentmanager/component_manager.h | 2 +- .../include/hidumphelper/enabled_comps_dump.h | 16 +++-- .../include/hidumphelper/hidump_helper.h | 2 +- .../resourcemanager/capability_info_manager.h | 2 +- .../include/task/task_board.h | 4 +- .../include/utils/impl_utils.h | 8 +++ .../componentmanager/component_manager.cpp | 10 ++-- .../src/hidumphelper/enabled_comps_dump.cpp | 20 +++---- .../src/hidumphelper/hidump_helper.cpp | 58 ++++++++++++------- .../capability_info_manager.cpp | 10 +++- .../src/task/task_board.cpp | 18 +++++- 12 files changed, 100 insertions(+), 55 deletions(-) diff --git a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp index d01506e..a5acfbd 100644 --- a/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp +++ b/services/distributedhardwarefwkservice/src/distributed_hardware_service.cpp @@ -86,12 +86,13 @@ int32_t DistributedHardwareService::QuerySinkVersion(std::unordered_map& args) { DHLOGI("DistributedHardwareService Dump."); - std::string result; - std::vector argsStr; + + std::vector argsStr {}; for (auto item : args) { argsStr.emplace_back(Str16ToStr8(item)); } + std::string result(""); int ret = AccessManager::GetInstance()->Dump(argsStr, result); if (ret != DH_FWK_SUCCESS) { DHLOGE("Dump error, ret = %d", ret); diff --git a/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h b/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h index ac9a1ad..8a592df 100644 --- a/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/componentmanager/component_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/services/distributedhardwarefwkserviceimpl/include/hidumphelper/enabled_comps_dump.h b/services/distributedhardwarefwkserviceimpl/include/hidumphelper/enabled_comps_dump.h index 699b059..dc9371d 100644 --- a/services/distributedhardwarefwkserviceimpl/include/hidumphelper/enabled_comps_dump.h +++ b/services/distributedhardwarefwkserviceimpl/include/hidumphelper/enabled_comps_dump.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,6 +16,7 @@ #ifndef OHOS_DISTRIBUTED_ENABLED_COMPS_DUMP_H #define OHOS_DISTRIBUTED_ENABLED_COMPS_DUMP_H #include +#include #include #include @@ -25,21 +26,25 @@ namespace OHOS { namespace DistributedHardware { struct HidumpCompInfo { + std::string deviceId_; std::string dhId_; DHType dhType_; + HidumpCompInfo(std::string deviceId, DHType dhType, std::string dhId) : + deviceId_(deviceId), dhId_(dhId), dhType_(dhType) {} + bool operator < (const HidumpCompInfo &other) const { - return (((this->dhType_ == other.dhType_) && (this->dhId_ < other.dhId_)) || - (this->dhType_ < other.dhType_)); + return (((this->deviceId_ == other.deviceId_) && (this->dhId_ < other.dhId_)) || + (this->deviceId_ < other.deviceId_)); } }; class EnabledCompsDump { DECLARE_SINGLE_INSTANCE_BASE(EnabledCompsDump); public: - void DumpEnabledComp(const DHType dhType, const std::string &dhId); - void DumpDisabledComp(const DHType dhType, const std::string &dhId); + void DumpEnabledComp(const std::string &uuid, const DHType dhType, const std::string &dhId); + void DumpDisabledComp(const std::string &uuid, const DHType dhType, const std::string &dhId); void Dump(std::set &compInfoSet); @@ -48,6 +53,7 @@ private: ~EnabledCompsDump() = default; private: + std::mutex compInfosMutex_; std::set compInfoSet_; }; } // namespace DistributedHardware diff --git a/services/distributedhardwarefwkserviceimpl/include/hidumphelper/hidump_helper.h b/services/distributedhardwarefwkserviceimpl/include/hidumphelper/hidump_helper.h index 7a16249..3104908 100644 --- a/services/distributedhardwarefwkserviceimpl/include/hidumphelper/hidump_helper.h +++ b/services/distributedhardwarefwkserviceimpl/include/hidumphelper/hidump_helper.h @@ -26,7 +26,7 @@ namespace OHOS { namespace DistributedHardware { enum class HidumpFlag { - UNKNOW = 0, + UNKNOWN = 0, GET_HELP, GET_LOADED_COMP_LIST, GET_ENABLED_COMP_LIST, diff --git a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/capability_info_manager.h b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/capability_info_manager.h index daa640d..c5e384e 100644 --- a/services/distributedhardwarefwkserviceimpl/include/resourcemanager/capability_info_manager.h +++ b/services/distributedhardwarefwkserviceimpl/include/resourcemanager/capability_info_manager.h @@ -93,7 +93,7 @@ public: /* EventBus async processing callback */ void OnEvent(CapabilityInfoEvent &e) override; - void DumpCapabilityInfos(CapabilityInfoMap &capInfoMap); + void DumpCapabilityInfos(std::vector &capInfos); private: CapabilityInfoManager(); diff --git a/services/distributedhardwarefwkserviceimpl/include/task/task_board.h b/services/distributedhardwarefwkserviceimpl/include/task/task_board.h index 1ab51de..4639551 100644 --- a/services/distributedhardwarefwkserviceimpl/include/task/task_board.h +++ b/services/distributedhardwarefwkserviceimpl/include/task/task_board.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -35,7 +35,7 @@ public: void RemoveTask(std::string taskId); int32_t WaitForALLTaskFinish(); - void DumpAllTasks(std::unordered_map> &tasks); + void DumpAllTasks(std::vector &taskInfos); private: void RemoveTaskInner(std::string taskId); diff --git a/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h b/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h index 4c7ab4c..3dfc2a3 100644 --- a/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h +++ b/services/distributedhardwarefwkserviceimpl/include/utils/impl_utils.h @@ -67,6 +67,14 @@ struct TaskParam { std::string dhId; DHType dhType; }; + +struct TaskDump { + std::string id; + TaskType taskType; + TaskParam taskParm; + TaskState taskState; + std::vector taskSteps; +}; } // namespace DistributedHardware } // namespace OHOS #endif diff --git a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp index 94af15a..e433516 100644 --- a/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/componentmanager/component_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -258,7 +258,7 @@ int32_t ComponentManager::Enable(const std::string &networkId, const std::string } if (compEnable->Enable(networkId, dhId, param, find->second) == DH_FWK_SUCCESS) { DHLOGE("enable success, retryCount = %d", retryCount); - EnabledCompsDump::GetInstance().DumpEnabledComp(dhType, dhId); + EnabledCompsDump::GetInstance().DumpEnabledComp(uuid, dhType, dhId); return DH_FWK_SUCCESS; } DHLOGE("enable failed, retryCount = %d", retryCount); @@ -267,7 +267,7 @@ int32_t ComponentManager::Enable(const std::string &networkId, const std::string } DHLOGI("enable result is %d, uuid = %s, dhId = %s", result, GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str()); - EnabledCompsDump::GetInstance().DumpEnabledComp(dhType, dhId); + EnabledCompsDump::GetInstance().DumpEnabledComp(uuid, dhType, dhId); return result; } @@ -289,7 +289,7 @@ int32_t ComponentManager::Disable(const std::string &networkId, const std::strin } if (compDisable->Disable(networkId, dhId, find->second) == DH_FWK_SUCCESS) { DHLOGE("disable success, retryCount = %d", retryCount); - EnabledCompsDump::GetInstance().DumpDisabledComp(dhType, dhId); + EnabledCompsDump::GetInstance().DumpDisabledComp(uuid, dhType, dhId); return DH_FWK_SUCCESS; } DHLOGE("disable failed, retryCount = %d", retryCount); @@ -298,7 +298,7 @@ int32_t ComponentManager::Disable(const std::string &networkId, const std::strin } DHLOGI("disable result is %d, uuid = %s, dhId = %s", result, GetAnonyString(uuid).c_str(), GetAnonyString(dhId).c_str()); - EnabledCompsDump::GetInstance().DumpDisabledComp(dhType, dhId); + EnabledCompsDump::GetInstance().DumpDisabledComp(uuid, dhType, dhId); return result; } diff --git a/services/distributedhardwarefwkserviceimpl/src/hidumphelper/enabled_comps_dump.cpp b/services/distributedhardwarefwkserviceimpl/src/hidumphelper/enabled_comps_dump.cpp index 0cf8180..931ff54 100644 --- a/services/distributedhardwarefwkserviceimpl/src/hidumphelper/enabled_comps_dump.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/hidumphelper/enabled_comps_dump.cpp @@ -14,26 +14,25 @@ */ #include "enabled_comps_dump.h" +#include "dh_utils_tool.h" namespace OHOS { namespace DistributedHardware { IMPLEMENT_SINGLE_INSTANCE(EnabledCompsDump); -void EnabledCompsDump::DumpEnabledComp(const DHType dhType, const std::string &dhId) +void EnabledCompsDump::DumpEnabledComp(const std::string &uuid, const DHType dhType, const std::string &dhId) { - HidumpCompInfo info = { - .dhId_ = dhId, - .dhType_ = dhType, - }; + HidumpCompInfo info (GetDeviceIdByUUID(uuid), dhType, dhId); + + std::lock_guard lock(compInfosMutex_); compInfoSet_.emplace(info); } -void EnabledCompsDump::DumpDisabledComp(const DHType dhType, const std::string &dhId) +void EnabledCompsDump::DumpDisabledComp(const std::string &uuid, const DHType dhType, const std::string &dhId) { - HidumpCompInfo info = { - .dhId_ = dhId, - .dhType_ = dhType, - }; + HidumpCompInfo info (GetDeviceIdByUUID(uuid), dhType, dhId); + + std::lock_guard lock(compInfosMutex_); auto it = compInfoSet_.find(info); if (it != compInfoSet_.end()) { compInfoSet_.erase(it); @@ -42,6 +41,7 @@ void EnabledCompsDump::DumpDisabledComp(const DHType dhType, const std::string & void EnabledCompsDump::Dump(std::set &compInfoSet) { + std::lock_guard lock(compInfosMutex_); compInfoSet = compInfoSet_; } } // namespace DistributedHardware diff --git a/services/distributedhardwarefwkserviceimpl/src/hidumphelper/hidump_helper.cpp b/services/distributedhardwarefwkserviceimpl/src/hidumphelper/hidump_helper.cpp index 523682f..e2daaaf 100644 --- a/services/distributedhardwarefwkserviceimpl/src/hidumphelper/hidump_helper.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/hidumphelper/hidump_helper.cpp @@ -93,7 +93,7 @@ int32_t HidumpHelper::Dump(const std::vector& args, std::string &re auto flag = MAP_ARGS.find(args[0]); if ((args.size() > 1) || (flag == MAP_ARGS.end())) { - errCode = ProcessDump(HidumpFlag::UNKNOW, result); + errCode = ProcessDump(HidumpFlag::UNKNOWN, result); } else { errCode = ProcessDump(flag->second, result); } @@ -138,8 +138,8 @@ int32_t HidumpHelper::ProcessDump(const HidumpFlag &flag, std::string &result) int32_t HidumpHelper::ShowAllLoadedComps(std::string &result) { DHLOGI("Dump all loaded compTypes."); - std::set loadedCompSource; - std::set loadedCompSink; + std::set loadedCompSource {}; + std::set loadedCompSink {}; ComponentManager::GetInstance().DumpLoadedComps(loadedCompSource, loadedCompSink); result.append("Local loaded components:\n{"); @@ -167,10 +167,14 @@ int32_t HidumpHelper::ShowAllLoadedComps(std::string &result) int32_t HidumpHelper::ShowAllEnabledComps(std::string &result) { DHLOGI("Dump all enabled comps."); - std::set compInfoSet; + std::set compInfoSet {}; EnabledCompsDump::GetInstance().Dump(compInfoSet); result.append("All enabled components:"); + if (compInfoSet.empty()) { + return DH_FWK_SUCCESS; + } + for (auto info : compInfoSet) { result.append("\n{"); result.append("\n DHType : "); @@ -186,23 +190,29 @@ int32_t HidumpHelper::ShowAllEnabledComps(std::string &result) int32_t HidumpHelper::ShowAllTaskInfos(std::string &result) { DHLOGI("Dump all task infos."); - std::unordered_map> tasks; - TaskBoard::GetInstance().DumpAllTasks(tasks); + std::vector taskInfos {}; + TaskBoard::GetInstance().DumpAllTasks(taskInfos); result.append("All task infos:"); - for (auto task : tasks) { + if (taskInfos.empty()) { + return DH_FWK_SUCCESS; + } + + for (auto taskInfo : taskInfos) { result.append("\n{"); + result.append("\n TaskId : "); + result.append(taskInfo.id); result.append("\n TaskType : "); - result.append(g_mapTaskType[task.second->GetTaskType()]); + result.append(g_mapTaskType[taskInfo.taskType]); result.append("\n DHType : "); - result.append(g_mapDhTypeName[task.second->GetDhType()]); + result.append(g_mapDhTypeName[taskInfo.taskParm.dhType]); result.append("\n DHId : "); - result.append(GetAnonyString(task.second->GetDhId())); + result.append(GetAnonyString(taskInfo.taskParm.dhId)); result.append("\n TaskState : "); - result.append(g_mapTaskState[task.second->GetTaskState()]); + result.append(g_mapTaskState[taskInfo.taskState]); result.append("\n TaskStep : [ "); - std::vector taskStep = task.second->GetTaskSteps(); - for (auto step : taskStep) { + std::vector taskSteps = taskInfo.taskSteps; + for (auto step : taskSteps) { result.append(g_mapTaskStep[step]); result.append(" "); } @@ -216,24 +226,28 @@ int32_t HidumpHelper::ShowAllTaskInfos(std::string &result) int32_t HidumpHelper::ShowAllCapabilityInfos(std::string &result) { DHLOGI("Dump all capability infos."); - CapabilityInfoMap capInfoMap; - CapabilityInfoManager::GetInstance()->DumpCapabilityInfos(capInfoMap); + std::vector capInfos; + CapabilityInfoManager::GetInstance()->DumpCapabilityInfos(capInfos); result.append("All capability infos:"); - for (auto info : capInfoMap) { + if (capInfos.empty()) { + return DH_FWK_SUCCESS; + } + + for (auto info : capInfos) { result.append("\n{"); result.append("\n DeviceName : "); - result.append(GetAnonyString(info.second->GetDeviceName())); + result.append(GetAnonyString(info.GetDeviceName())); result.append("\n DeviceId : "); - result.append(GetAnonyString(info.second->GetDeviceId())); + result.append(GetAnonyString(info.GetDeviceId())); result.append("\n DeviceType : "); - result.append(std::to_string(info.second->GetDeviceType())); + result.append(std::to_string(info.GetDeviceType())); result.append("\n DHType : "); - result.append(g_mapDhTypeName[info.second->GetDHType()]); + result.append(g_mapDhTypeName[info.GetDHType()]); result.append("\n DHId : "); - result.append(GetAnonyString(info.second->GetDHId())); + result.append(GetAnonyString(info.GetDHId())); result.append("\n DHAttrs :\n"); - result.append(info.second->GetDHAttrs()); + result.append(info.GetDHAttrs()); result.append("\n},"); } result.replace(result.size() - 1, 1, "\n"); diff --git a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/capability_info_manager.cpp b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/capability_info_manager.cpp index c530098..6507314 100644 --- a/services/distributedhardwarefwkserviceimpl/src/resourcemanager/capability_info_manager.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/resourcemanager/capability_info_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -536,9 +536,13 @@ int32_t CapabilityInfoManager::GetDataByKeyPrefix(const std::string &keyPrefix, return DH_FWK_SUCCESS; } -void CapabilityInfoManager::DumpCapabilityInfos(CapabilityInfoMap &capInfoMap) +void CapabilityInfoManager::DumpCapabilityInfos(std::vector &capInfos) { - capInfoMap = globalCapInfoMap_; + for (auto info : globalCapInfoMap_) { + CapabilityInfo capInfo; + capInfo = *(info.second); + capInfos.emplace_back(capInfo); + } } } // namespace DistributedHardware } // namespace OHOS diff --git a/services/distributedhardwarefwkserviceimpl/src/task/task_board.cpp b/services/distributedhardwarefwkserviceimpl/src/task/task_board.cpp index 230092c..59b635b 100644 --- a/services/distributedhardwarefwkserviceimpl/src/task/task_board.cpp +++ b/services/distributedhardwarefwkserviceimpl/src/task/task_board.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -86,9 +86,21 @@ void TaskBoard::RemoveTaskInner(std::string taskId) tasks_.erase(taskId); } -void TaskBoard::DumpAllTasks(std::unordered_map> &tasks) +void TaskBoard::DumpAllTasks(std::vector &taskInfos) { - tasks = tasks_; + std::lock_guard lock(tasksMtx_); + for (auto t : tasks_) { + TaskDump taskInfo = { + .id = t.second->GetId(), + .taskType = t.second->GetTaskType(), + .taskParm.networkId = t.second->GetNetworkId(), + .taskParm.uuid = t.second->GetUUID(), + .taskParm.dhId = t.second->GetDhId(), + .taskParm.dhType = t.second->GetDhType(), + .taskSteps = t.second->GetTaskSteps() + }; + taskInfos.emplace_back(taskInfo); + } } } // namespace DistributedHardware } // namespace OHOS