1 /* 2 * Copyright (C) 2021-2022 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 #include <unistd.h> 16 #include "executor/cpu_dumper.h" 17 #include "datetime_ex.h" 18 #include "dump_utils.h" 19 #include "securec.h" 20 #include "util/string_utils.h" 21 #include "dump_manager_cpu_client.h" 22 23 namespace OHOS { 24 namespace HiviewDFX { CPUDumper()25CPUDumper::CPUDumper() 26 { 27 } 28 ~CPUDumper()29CPUDumper::~CPUDumper() 30 { 31 } 32 PreExecute(const std::shared_ptr<DumperParameter> & parameter,StringMatrix dumpDatas)33DumpStatus CPUDumper::PreExecute(const std::shared_ptr<DumperParameter> ¶meter, StringMatrix dumpDatas) 34 { 35 DUMPER_HILOGD(MODULE_COMMON, "debug|CPUDumper PreExecute"); 36 dumpCPUDatas_ = dumpDatas; 37 isDumpCpuUsage_ = (parameter->GetOpts()).isDumpCpuUsage_; 38 cpuUsagePid_ = (parameter->GetOpts()).cpuUsagePid_; 39 if (cpuUsagePid_ != -1) { 40 curSpecProc_ = std::make_shared<ProcInfo>(); 41 oldSpecProc_ = std::make_shared<ProcInfo>(); 42 } 43 curCPUInfo_ = std::make_shared<CPUInfo>(); 44 oldCPUInfo_ = std::make_shared<CPUInfo>(); 45 return DumpStatus::DUMP_OK; 46 } 47 Execute()48DumpStatus CPUDumper::Execute() 49 { 50 if (isDumpCpuUsage_) { 51 auto& dumpManagerCpuClient = DumpManagerCpuClient::GetInstance(); 52 DumpCpuData dumpData = GetDumpCpuData(); 53 dumpManagerCpuClient.Request(dumpData); 54 dumpCPUDatas_->clear(); 55 for (auto it = dumpData.dumpCPUDatas_.begin(); it != dumpData.dumpCPUDatas_.end(); it++) { 56 std::vector<std::string> strVec; 57 for (auto str = it->begin(); str != it->end(); str++) { 58 strVec.push_back(*str); 59 } 60 dumpCPUDatas_->push_back(strVec); 61 } 62 return DumpStatus::DUMP_OK; 63 } else { 64 return DumpStatus::DUMP_FAIL; 65 } 66 } 67 AfterExecute()68DumpStatus CPUDumper::AfterExecute() 69 { 70 curCPUInfo_.reset(); 71 oldCPUInfo_.reset(); 72 curProcs_.clear(); 73 oldProcs_.clear(); 74 if (cpuUsagePid_ != -1) { 75 curSpecProc_.reset(); 76 oldSpecProc_.reset(); 77 } 78 return DumpStatus::DUMP_OK; 79 } 80 GetDumpCpuData()81DumpCpuData CPUDumper::GetDumpCpuData() 82 { 83 DumpCpuData dumpCpu; 84 dumpCpu.startTime_ = startTime_; 85 dumpCpu.endTime_ = endTime_; 86 dumpCpu.dumpCPUDatas_ = *dumpCPUDatas_; 87 dumpCpu.cpuUsagePid_ = cpuUsagePid_; 88 return dumpCpu; 89 } 90 } // namespace HiviewDFX 91 } // namespace OHOS 92