Line data Source code
1 : /** 2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd. 3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of 4 : * CANN Open Software License Agreement Version 2.0 (the "License"). 5 : * Please refer to the License for details. You may not use this file except in compliance with the License. 6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, 7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. 8 : * See LICENSE in the root of the software repository for the full text of the License. 9 : */ 10 : 11 : #include "dump.h" 12 : 13 : #include <mutex> 14 : #include <sstream> 15 : #include <set> 16 : 17 : #include "mmpa/mmpa_api.h" 18 : #include "adx_datadump_server.h" 19 : #include "adump_pub.h" 20 : 21 : #include "common/json_parser.h" 22 : #include "common/error_codes_inner.h" 23 : 24 : #include "common/log_inner.h" 25 : #include "utils/string_utils.h" 26 : #include "aclrt_impl/acl_rt_impl_base.h" 27 : #include "acl_rt_impl.h" 28 : 29 : namespace { 30 : bool aclmdlInitDumpFlag = false; 31 : std::mutex aclDumpMutex; 32 : constexpr int32_t ADX_ERROR_NONE = 0; 33 : } 34 : 35 : namespace acl { 36 88 : AclDump &AclDump::GetInstance() 37 : { 38 : static AclDump aclDumpProc; 39 88 : return aclDumpProc; 40 : } 41 : 42 29 : aclError AclDump::HandleDumpCommand(const char *configStr, size_t size, const char *configPath) 43 : { 44 29 : ACL_LOG_INFO("start to execute HandleDumpCommand."); 45 : 46 29 : int32_t adxRet = AdxDataDumpServerInit(); 47 29 : if (adxRet != ADX_ERROR_NONE) { 48 0 : ACL_LOG_INNER_ERROR("[AdxDataDumpServer][Init]dump server run failed, adx errorCode = %d", adxRet); 49 0 : return ACL_ERROR_INTERNAL_ERROR; 50 : } 51 29 : acl::AclDump::GetInstance().SetAdxInitFromAclInitFlag(true); 52 : 53 : // base dump 54 29 : Adx::DumpConfigInfo configInfo; 55 29 : configInfo.dumpConfigPath = configPath; 56 29 : configInfo.dumpConfigData = configStr; 57 29 : configInfo.dumpConfigSize = size; 58 29 : adxRet = Adx::AdumpSetDumpConfig(configInfo); 59 29 : if (adxRet != ADX_ERROR_NONE) { 60 1 : auto ret = 61 1 : (adxRet == Adx::ADUMP_INPUT_FAILED) ? ACL_ERROR_INVALID_DUMP_CONFIG : ACL_ERROR_INTERNAL_ERROR; 62 1 : ACL_LOG_INNER_ERROR("[Set][Dump]set dump config failed, adx errorCode = %d", adxRet); 63 1 : return ret; 64 : } 65 : 66 28 : return ACL_SUCCESS; 67 : } 68 : 69 30 : aclError AclDump::HandleDumpConfig(const char_t *const configPath) 70 : { 71 30 : ACL_LOG_INFO("start to execute HandleDumpConfig."); 72 60 : std::string configStr; 73 30 : aclError ret = acl::JsonParser::GetConfigStrFromFile(configPath, configStr); 74 30 : if (ret != ACL_SUCCESS) { 75 0 : ACL_LOG_INNER_ERROR("Get config string from file[%s] failed, errorCode = %d", 76 : configPath, ret); 77 0 : return ret; 78 : } 79 : try { 80 30 : if (!configStr.empty()) { 81 29 : return HandleDumpCommand(configStr.c_str(), configStr.size(), configPath); 82 : } 83 0 : } catch (const nlohmann::json::exception &e) { 84 0 : ACL_LOG_INNER_ERROR("[Convert][DumpConfig]parse json for config failed, exception:%s.", 85 : e.what()); 86 0 : return ACL_ERROR_INVALID_DUMP_CONFIG; 87 : } 88 1 : ACL_LOG_INFO("HandleDumpConfig end in HandleDumpConfig."); 89 1 : return ACL_SUCCESS; 90 : } 91 : } // namespace acl 92 : 93 8 : aclError aclmdlInitDumpImpl() 94 : { 95 8 : ACL_LOG_INFO("start to execute aclmdlInitDump."); 96 8 : if (!acl::GetAclInitFlag()) { 97 0 : ACL_LOG_INNER_ERROR("[Check][AclInitFlag]aclmdlInitDump is not supported because it does not execute aclInit"); 98 0 : return ACL_ERROR_UNINITIALIZE; 99 : } 100 : 101 16 : const std::unique_lock<std::mutex> lk(aclDumpMutex); 102 8 : if (aclmdlInitDumpFlag) { 103 1 : ACL_LOG_INNER_ERROR("[Check][InitDumpFlag]repeatedly initialized dump in aclmdlInitDump, " 104 : "only initialized once"); 105 1 : return ACL_ERROR_REPEAT_INITIALIZE; 106 : } 107 : 108 7 : const int32_t adxRet = AdxDataDumpServerInit(); 109 7 : if (adxRet != ADX_ERROR_NONE) { 110 1 : ACL_LOG_CALL_ERROR("[AdxDataDumpServer][Init]dump server run failed, adx errorCode = %d", adxRet); 111 1 : return ACL_ERROR_INTERNAL_ERROR; 112 : } 113 : 114 6 : aclmdlInitDumpFlag = true; 115 6 : ACL_LOG_INFO("successfully initialized dump in aclmdlInitDump."); 116 6 : return ACL_SUCCESS; 117 : } 118 : 119 5 : aclError aclmdlSetDumpImpl(const char *dumpCfgPath) 120 : { 121 5 : ACL_LOG_INFO("start to execute aclmdlSetDump."); 122 5 : if (!acl::GetAclInitFlag()) { 123 0 : ACL_LOG_INNER_ERROR("[Check][AclInitFlag]aclmdlSetDump is not supported because it does not execute aclInit"); 124 0 : return ACL_ERROR_UNINITIALIZE; 125 : } 126 : 127 10 : const std::unique_lock<std::mutex> lk(aclDumpMutex); 128 5 : if (!aclmdlInitDumpFlag) { 129 1 : ACL_LOG_INNER_ERROR("[Check][aclmdlInitDumpFlag]dump is not initialized in aclmdlInitDump"); 130 1 : return ACL_ERROR_DUMP_NOT_RUN; 131 : } 132 : 133 4 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(dumpCfgPath); 134 8 : std::string configStr; 135 4 : aclError ret = acl::JsonParser::GetConfigStrFromFile(dumpCfgPath, configStr); 136 4 : if (ret != ACL_SUCCESS) { 137 0 : ACL_LOG_INNER_ERROR("Get config string from file[%s] failed, errorCode = %d", 138 : dumpCfgPath, ret); 139 0 : return ret; 140 : } 141 : 142 : // base dump 143 4 : if (!configStr.empty()) { 144 4 : ACL_LOG_INFO("Start to set dump."); 145 4 : Adx::DumpConfigInfo configInfo; 146 4 : configInfo.dumpConfigPath = dumpCfgPath; 147 4 : configInfo.dumpConfigData = configStr.c_str(); 148 4 : configInfo.dumpConfigSize = configStr.size(); 149 4 : const auto adxRet = Adx::AdumpSetDumpConfig(configInfo); 150 4 : if (adxRet != ADX_ERROR_NONE) { 151 1 : ret = 152 1 : (adxRet == Adx::ADUMP_INPUT_FAILED) ? ACL_ERROR_INVALID_DUMP_CONFIG : ACL_ERROR_INTERNAL_ERROR; 153 1 : ACL_LOG_INNER_ERROR("[Set][Dump]set dump config failed, adx errorCode = %d", adxRet); 154 1 : return ret; 155 : } 156 : } 157 3 : ACL_LOG_INFO("set dump config successfully."); 158 3 : return ACL_SUCCESS; 159 : } 160 : 161 10 : aclError aclmdlFinalizeDumpImpl() 162 : { 163 10 : ACL_LOG_INFO("start to execute aclmdlFinalizeDump."); 164 10 : if (!acl::GetAclInitFlag()) { 165 0 : ACL_LOG_INNER_ERROR("[Check][AclInitFlag]aclmdlFinalizeDump is not supported because it does not " 166 : "execute aclInit"); 167 0 : return ACL_ERROR_UNINITIALIZE; 168 : } 169 : 170 20 : const std::unique_lock<std::mutex> lk(aclDumpMutex); 171 10 : if (!aclmdlInitDumpFlag) { 172 1 : ACL_LOG_INNER_ERROR("[Check][aclmdlInitDumpFlag]dump is not initialized in aclmdlInitDump"); 173 1 : return ACL_ERROR_DUMP_NOT_RUN; 174 : } 175 : 176 : // close adump opened 177 9 : int32_t adxRet = Adx::AdumpUnSetDump(); 178 9 : if (adxRet != ADX_ERROR_NONE) { 179 2 : ACL_LOG_INNER_ERROR("[Set][Dump]set dump off failed, adx errorCode = %d", adxRet); 180 2 : return ACL_ERROR_INTERNAL_ERROR; 181 : } 182 : 183 : // close dump server 184 7 : adxRet = AdxDataDumpServerUnInit(); 185 7 : if (adxRet != ADX_ERROR_NONE) { 186 1 : ACL_LOG_CALL_ERROR("[AdxDataDumpServer][UnInit]generate dump file failed in disk, adx errorCode = %d", adxRet); 187 1 : return ACL_ERROR_INTERNAL_ERROR; 188 : } 189 : 190 6 : aclmdlInitDumpFlag = false; 191 6 : ACL_LOG_INFO("successfully execute aclmdlFinalizeDump, the dump task completed!"); 192 6 : return ACL_SUCCESS; 193 : }