1 /* 2 * Copyright (c) 2023-2024 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 #ifndef FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_FILE_UTILS_H 16 #define FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_FILE_UTILS_H 17 18 #include <map> 19 #include <string> 20 21 #include "hisysevent.h" 22 #include "trace_collector.h" 23 #include "trace_state_machine.h" 24 #include "trace_flow_controller.h" 25 26 using OHOS::HiviewDFX::UCollectUtil::TraceCollector; 27 using OHOS::HiviewDFX::Hitrace::TraceErrorCode; 28 using OHOS::HiviewDFX::UCollect::UcError; 29 30 namespace OHOS { 31 namespace HiviewDFX { 32 namespace { 33 const std::map<TraceErrorCode, UcError> CODE_MAP = { 34 {TraceErrorCode::SUCCESS, UcError::SUCCESS}, 35 {TraceErrorCode::TRACE_NOT_SUPPORTED, UcError::UNSUPPORT}, 36 {TraceErrorCode::TRACE_IS_OCCUPIED, UcError::TRACE_IS_OCCUPIED}, 37 {TraceErrorCode::TAG_ERROR, UcError::TRACE_TAG_ERROR}, 38 {TraceErrorCode::FILE_ERROR, UcError::TRACE_FILE_ERROR}, 39 {TraceErrorCode::WRITE_TRACE_INFO_ERROR, UcError::TRACE_WRITE_ERROR}, 40 {TraceErrorCode::WRONG_TRACE_MODE, UcError::TRACE_WRONG_MODE}, 41 {TraceErrorCode::OUT_OF_TIME, UcError::TRACE_OUT_OF_TIME}, 42 {TraceErrorCode::FORK_ERROR, UcError::TRACE_FORK_ERROR}, 43 {TraceErrorCode::EPOLL_WAIT_ERROR, UcError::TRACE_EPOLL_WAIT_ERROR}, 44 {TraceErrorCode::PIPE_CREATE_ERROR, UcError::TRACE_PIPE_CREATE_ERROR}, 45 {TraceErrorCode::SYSINFO_READ_FAILURE, UcError::TRACE_SYSINFO_READ_FAILURE}, 46 }; 47 48 const std::map<TraceStateCode, UcError> TRACE_STATE_MAP = { 49 {TraceStateCode::SUCCESS, UcError::SUCCESS}, 50 {TraceStateCode::FAIL, UcError::TRACE_STATE_ERROR}, 51 {TraceStateCode::DENY, UcError::TRACE_OPEN_ERROR}, 52 }; 53 54 const std::map<TraceFlowCode, UcError> TRACE_FLOW_MAP = { 55 {TraceFlowCode::TRACE_ALLOW, UcError::SUCCESS}, 56 {TraceFlowCode::TRACE_DUMP_DENY, UcError::TRACE_DUMP_OVER_FLOW}, 57 {TraceFlowCode::TRACE_UPLOAD_DENY, UcError::TRACE_OVER_FLOW}, 58 {TraceFlowCode::TRACE_HAS_CAPTURED_TRACE, UcError::HAD_CAPTURED_TRACE} 59 }; 60 } 61 62 struct DumpEvent { 63 std::string caller; 64 int32_t errorCode = 0; 65 uint64_t ipcTime = 0; 66 uint64_t reqTime = 0; 67 int32_t reqDuration = 0; 68 uint64_t execTime = 0; 69 int32_t execDuration = 0; 70 int32_t coverDuration = 0; 71 int32_t coverRatio = 0; 72 std::vector<std::string> tags; 73 int32_t fileSize = 0; 74 int32_t sysMemTotal = 0; 75 int32_t sysMemFree = 0; 76 int32_t sysMemAvail = 0; 77 int32_t sysCpu = 0; 78 }; 79 80 namespace TelemetryEvent { 81 const std::string TELEMETRY_START = "telemetryStart"; 82 const std::string TELEMETRY_STOP = "telemetryStop"; 83 const std::string TELEMETRY_TIMEOUT = "telemetryTimeout"; 84 } 85 86 UcError TransCodeToUcError(TraceErrorCode ret); 87 UcError TransStateToUcError(TraceStateCode ret); 88 UcError TransFlowToUcError(TraceFlowCode ret); 89 const std::string EnumToString(UCollect::TraceCaller &caller); 90 const std::string ClientToString(UCollect::TraceClient &client); 91 const std::string ModuleToString(UCollect::TeleModule &module); 92 void CopyFile(const std::string &src, const std::string &dst); 93 std::vector<std::string> GetUnifiedZipFiles(const std::vector<std::string> outputFiles, const std::string &destDir); 94 std::vector<std::string> GetUnifiedSpecialFiles(const std::vector<std::string>& outputFiles, const std::string& prefix); 95 void ZipTraceFile(const std::string &srcSysPath, const std::string &destZipPath); 96 std::string AddVersionInfoToZipName(const std::string &srcZipPath); 97 void CheckCurrentCpuLoad(); 98 void WriteDumpTraceHisysevent(DumpEvent &dumpEvent, int32_t retCode); 99 void LoadMemoryInfo(DumpEvent &dumpEvent); 100 void CheckAndCreateDirectory(const std::string &tmpDirPath); 101 bool CreateMultiDirectory(const std::string &dirPath); 102 void RecoverTmpTrace(); 103 int64_t GetTraceSize(TraceRetInfo &ret); 104 UcError GetUcError(TraceRet ret); 105 } // HiViewDFX 106 } // OHOS 107 #endif // FRAMEWORK_NATIVE_UNIFIED_COLLECTION_COLLECTOR_FILE_UTILS_H 108