From abb16da39abae0123dec825f2b46a5637c2660cb Mon Sep 17 00:00:00 2001 From: taoxiangdong Date: Mon, 23 Nov 2020 12:51:10 +0800 Subject: [PATCH] profiling ar --- ge/common/profiling/ge_profiling.cc | 156 ++++++++++++++++++++++++++ inc/framework/common/profiling/ge_profiling.h | 47 ++++++++ 2 files changed, 203 insertions(+) create mode 100644 ge/common/profiling/ge_profiling.cc create mode 100644 inc/framework/common/profiling/ge_profiling.h diff --git a/ge/common/profiling/ge_profiling.cc b/ge/common/profiling/ge_profiling.cc new file mode 100644 index 00000000..6a2b4d41 --- /dev/null +++ b/ge/common/profiling/ge_profiling.cc @@ -0,0 +1,156 @@ +/** + * Copyright 2020 Huawei Technologies 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "common/profiling/ge_profiling.h" +#include "common/profiling/profiling_manager.h" +namespace { + const std::string kDeviceNums = "devNums"; + const std::string kDeviceIdList = "devIdList"; + const std::string kProfilingInit = "prof_init"; + const std::string kProfilingFinalize = "prof_finalize"; + const std::string kProfilingStart = "prof_start"; + const std::string kProfilingStop = "prof_stop"; + const std::string kProfModelSubscribe = "prof_model_subscribe"; + const std::string kProfModelUnsubscribe = "prof_model_cancel_subscribe"; + + const std::map kProfCommandTypeMap = { + {ge::kProfCommandhandleInit, kProfilingInit}, + {ge::kProfCommandhandleStart, kProfilingStart}, + {ge::kProfCommandhandleStop, prof_stop}, + {ge::kProfCommandhandleFinalize,kProfilingFinalize}, + {ge::kProfCommandhandleModelSubscribe, kProfModelSubscribe}, + {ge::kProfCommandhandleModelUnsubscribe, kProfModelUnsubscribe}}; +} // namespace + +namespace ge { +bool TransProfConfigToParam(const ProfCommand &profCommand, vector &prof_config_params) { + prof_config_params.clear(); + prof_config_params.emplace_back(kDeviceNums); + prof_config_params.emplace_back(std::to_string(profCommand.dev_nums)); + prof_config_params.emplace_back(kDeviceIdList); + std::string devID = ""; + if (profCommand.dev_nums == 0) { + GELOGW("The device num is invalid."); + return false; + } + for (uint32_t i = 0; i < profCommand.dev_nums; i++) { + devID.append(std::to_string(profCommand.device_list[i])); + if (i != profCommand.dev_nums - 1) { + devID.append(","); + } + } + + prof_config_params.push_back(devID); + return true; +} + +bool isProfConfigValid(const uint32_t *deviceid_list, uint32_t device_nums) { + if (deviceid_list == nullptr) { + GELOGE(PARAM_INVALID, "deviceIdList is nullptr"); + return false; + } + if (device_nums == 0 || device_nums > kMaxDeviceNum) { + GELOGE(PARAM_INVALID, "The device nums is invalid."); + return false; + } + + // real device num + int32_t dev_count = 0; + rtError_t rt_err = rtGetDeviceCount(&dev_count); + if (rt_err != RT_ERROR_NONE) { + GELOGE(INTERNAL_ERROR, "Get the Device count fail."); + return false; + } + + if (device_nums > static_cast(dev_count)) { + GELOGE(PARAM_INVALID, "Device num(%u) is not in range 1 ~ %d.", device_nums, dev_count); + return false; + } + + std::unordered_set record; + for (size_t i = 0; i < device_nums; ++i) { + uint32_t dev_id = deviceid_list[i]; + if (dev_id >= static_cast(dev_count)) { + GELOGE(PARAM_INVALID, "Device id %u is not in range 0 ~ %d(exclude %d)", dev_id, dev_count, dev_count); + return false; + } + if (record.count(dev_id) > 0) { + GELOGE(PARAM_INVALID, "Device id %u is duplicatedly set", dev_id); + return false; + } + record.insert(dev_id); + } + return true; +} + +Status GeRegProfCtrlCallback(MsprofCtrlCallback func) { + ProfilingManager::Instance().msprofCtrlCallback = func; +} + +Status GeRegProfSetDeviceCallback(MsprofSetDeviceCallback func) { + // pass MsprofSetDeviceCallback to runtime + Status rt_ret = rtRegDeviceStateCallback(func); +} + +Status GeRegProfReporterCallback(MsprofReporterCallback func) { + ProfilingManager::Instance().msprofReporterCallback = func; + // pass MsprofReporterCallback to runtime + Status rt_ret = rtSetMsprofReporterCallback(func); + // pass MsprofReporterCallback to hccl +} + +Status GeProfCommandHandle(MsprofCommandHandleType type, void *data, uint32_t len) { + GE_CHK_NOT_NULL(data); + ProfCommand *prof_config_param = (ProfCommand *)data; + if (!isProfConfigValid(deviceid_list, device_nums)) { + return FAILED; + } + std::vector prof_params; + if (!TransProfConfigToParam(*prof_config_param, prof_params)) { + GELOGE(PARAM_INVALID, "Transfer profilerConfig to string vector failed"); + return PARAM_INVALID; + } + auto iter = kProfCommandTypeMap.find(type); + if (iter == kProfCommandTypeMap.end()) { + GELOGW("The prof comand type is invalid."); + return PARAM_INVALID; + } + GraphLoader graph_loader; + Command command; + command.cmd_params.clear(); + command.cmd_type = iter->second; + command.cmd_params = prof_params; + command.module_index = prof_config_param->prof_switch; + GELOGI("GE commandhandle execute, device nums:%s , deviceID:[%s], data type config: 0x%llx", prof_params[0].c_str(), + prof_params[kDeviceListIndex].c_str(), command.module_index); + ret = graph_loader.CommandHandle(command); + if (ret != SUCCESS) { + GELOGE(ret, "Handle profiling command failed"); + return FAILED; + } + + GELOGI("Successfully execute profiling command 0x%llx.", command.module_index); +} + +bool IsGeInitialize() { + std::shared_ptr instance_ptr = ge::GELib::GetInstance(); + if (instance_ptr == nullptr || instance_ptr->InitFlag() == false) { + return false; + } + return true; +} + +} // namespace diff --git a/inc/framework/common/profiling/ge_profiling.h b/inc/framework/common/profiling/ge_profiling.h new file mode 100644 index 00000000..5245b54e --- /dev/null +++ b/inc/framework/common/profiling/ge_profiling.h @@ -0,0 +1,47 @@ +/** + * Copyright 2020 Huawei Technologies 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INC_FRAMEWORK_COMMON_GE_PROFILING_H_ +#define INC_FRAMEWORK_COMMON_GE_PROFILING_H_ + +#include "" +namespace ge { +#define MAX_DEV_NUM (64) +enum MsprofCommandHandleType { + kProfCommandhandleInit = 0, + kProfCommandhandleStart, + kProfCommandhandleStop, + kProfCommandhandleFinalize, + kProfCommandhandleModelSubscribe, + kProfCommandhandleModelUnsubscribe +}; + +struct ProfCommand { + uint64_t prof_switch; + uint32_t dev_nums; + uint32_t device_list[MAX_DEV_NUM]; + uint32_t model_id; // for subscribe +} + +Status GeRegProfCtrlCallback(MsprofCtrlCallback func); +Status GeRegProfSetDeviceCallback(MsprofSetDeviceCallback func); +Status GeRegProfReporterCallback(MsprofReporterCallback func); +Status GeProfCommandHandle(MsprofCommandHandleType type, void *data, uint32_t len); +bool IsGeInitialize(); + +} // namespace + +#endif // INC_FRAMEWORK_COMMON_GE_PROFILING_H_ \ No newline at end of file