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()25 CPUDumper::CPUDumper()
26 {
27 }
28 
~CPUDumper()29 CPUDumper::~CPUDumper()
30 {
31 }
32 
PreExecute(const std::shared_ptr<DumperParameter> & parameter,StringMatrix dumpDatas)33 DumpStatus CPUDumper::PreExecute(const std::shared_ptr<DumperParameter> &parameter, StringMatrix dumpDatas)
34 {
35     DUMPER_HILOGI(MODULE_COMMON, "info|CPUDumper PreExecute");
36     dumpCPUDatas_ = dumpDatas;
37     isDumpCpuUsage_ = (parameter->GetOpts()).isDumpCpuUsage_;
38     cpuUsagePid_ = (parameter->GetOpts()).cpuUsagePid_;
39     return DumpStatus::DUMP_OK;
40 }
41 
Execute()42 DumpStatus CPUDumper::Execute()
43 {
44     DUMPER_HILOGI(MODULE_COMMON, "info|CPUDumper Execute");
45     if (isDumpCpuUsage_) {
46         auto& dumpManagerCpuClient = DumpManagerCpuClient::GetInstance();
47         DumpCpuData dumpData = GetDumpCpuData();
48         dumpManagerCpuClient.Request(dumpData);
49         dumpCPUDatas_->clear();
50         for (auto it = dumpData.dumpCPUDatas_.begin(); it != dumpData.dumpCPUDatas_.end(); it++) {
51             std::vector<std::string> strVec;
52             for (auto str = it->begin(); str != it->end(); str++) {
53                 strVec.push_back(*str);
54             }
55             dumpCPUDatas_->push_back(strVec);
56         }
57         return DumpStatus::DUMP_OK;
58     } else {
59         return DumpStatus::DUMP_FAIL;
60     }
61     DUMPER_HILOGI(MODULE_COMMON, "info|CPUDumper Execute end");
62 }
63 
AfterExecute()64 DumpStatus CPUDumper::AfterExecute()
65 {
66     return DumpStatus::DUMP_OK;
67 }
68 
GetDumpCpuData()69 DumpCpuData CPUDumper::GetDumpCpuData()
70 {
71     DumpCpuData dumpCpu;
72     dumpCpu.startTime_ = startTime_;
73     dumpCpu.endTime_ = endTime_;
74     dumpCpu.dumpCPUDatas_ = *dumpCPUDatas_;
75     dumpCpu.cpuUsagePid_ = cpuUsagePid_;
76     return dumpCpu;
77 }
78 } // namespace HiviewDFX
79 } // namespace OHOS
80