100600bfbSopenharmony_ci/* 200600bfbSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 300600bfbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 400600bfbSopenharmony_ci * you may not use this file except in compliance with the License. 500600bfbSopenharmony_ci * You may obtain a copy of the License at 600600bfbSopenharmony_ci * 700600bfbSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 800600bfbSopenharmony_ci * 900600bfbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1000600bfbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1100600bfbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1200600bfbSopenharmony_ci * See the License for the specific language governing permissions and 1300600bfbSopenharmony_ci * limitations under the License. 1400600bfbSopenharmony_ci */ 1500600bfbSopenharmony_ci#include "dump_cpu_data.h" 1600600bfbSopenharmony_ci#include "hilog_wrapper.h" 1700600bfbSopenharmony_cinamespace OHOS { 1800600bfbSopenharmony_cinamespace HiviewDFX { 1900600bfbSopenharmony_ciDumpCpuData::DumpCpuData() 2000600bfbSopenharmony_ci{ 2100600bfbSopenharmony_ci} 2200600bfbSopenharmony_ci 2300600bfbSopenharmony_ciDumpCpuData::DumpCpuData(std::string& startTime, std::string& endTime, int cpuUsagePid, StringCpuMatrix dumpCPUDatas) 2400600bfbSopenharmony_ci : startTime_(startTime), endTime_(endTime), cpuUsagePid_(cpuUsagePid), dumpCPUDatas_(dumpCPUDatas) 2500600bfbSopenharmony_ci{ 2600600bfbSopenharmony_ci} 2700600bfbSopenharmony_ci 2800600bfbSopenharmony_ciDumpCpuData::~DumpCpuData() 2900600bfbSopenharmony_ci{ 3000600bfbSopenharmony_ci} 3100600bfbSopenharmony_ci 3200600bfbSopenharmony_cibool DumpCpuData::Marshalling(Parcel& parcel) const 3300600bfbSopenharmony_ci{ 3400600bfbSopenharmony_ci RETURN_PARCEL_WRITE_HELPER_RET(parcel, String, startTime_, false); 3500600bfbSopenharmony_ci RETURN_PARCEL_WRITE_HELPER_RET(parcel, String, endTime_, false); 3600600bfbSopenharmony_ci RETURN_PARCEL_WRITE_HELPER_RET(parcel, Int32, cpuUsagePid_, false); 3700600bfbSopenharmony_ci if (!WriteStringMatrix(dumpCPUDatas_, parcel)) { 3800600bfbSopenharmony_ci DUMPER_HILOGE(MODULE_CPU_DATA, "failed to write dumpCPUDatas_"); 3900600bfbSopenharmony_ci return false; 4000600bfbSopenharmony_ci } 4100600bfbSopenharmony_ci return true; 4200600bfbSopenharmony_ci} 4300600bfbSopenharmony_ci 4400600bfbSopenharmony_ciDumpCpuData *DumpCpuData::Unmarshalling(Parcel& parcel) 4500600bfbSopenharmony_ci{ 4600600bfbSopenharmony_ci DumpCpuData *info = new (std::nothrow) DumpCpuData(); 4700600bfbSopenharmony_ci if (info == nullptr) { 4800600bfbSopenharmony_ci DUMPER_HILOGE(MODULE_CPU_DATA, "failed to create DumpCpuData"); 4900600bfbSopenharmony_ci return nullptr; 5000600bfbSopenharmony_ci } 5100600bfbSopenharmony_ci if (!info->ReadFromParcel(parcel)) { 5200600bfbSopenharmony_ci DUMPER_HILOGE(MODULE_CPU_DATA, "failed to read from parcel"); 5300600bfbSopenharmony_ci delete info; 5400600bfbSopenharmony_ci info = nullptr; 5500600bfbSopenharmony_ci } 5600600bfbSopenharmony_ci return info; 5700600bfbSopenharmony_ci} 5800600bfbSopenharmony_ci 5900600bfbSopenharmony_cibool DumpCpuData::ReadFromParcel(Parcel &parcel) 6000600bfbSopenharmony_ci{ 6100600bfbSopenharmony_ci RETURN_PARCEL_READ_HELPER_RET(parcel, String, startTime_, false); 6200600bfbSopenharmony_ci RETURN_PARCEL_READ_HELPER_RET(parcel, String, endTime_, false); 6300600bfbSopenharmony_ci RETURN_PARCEL_READ_HELPER_RET(parcel, Int32, cpuUsagePid_, false); 6400600bfbSopenharmony_ci ReadStringMatrix(dumpCPUDatas_, parcel); 6500600bfbSopenharmony_ci return true; 6600600bfbSopenharmony_ci} 6700600bfbSopenharmony_ci 6800600bfbSopenharmony_cibool DumpCpuData::WriteStringMatrix(const std::vector<std::vector<std::string>> &martrixVec, Parcel &data) const 6900600bfbSopenharmony_ci{ 7000600bfbSopenharmony_ci if (!data.WriteUint32(martrixVec.size())) { 7100600bfbSopenharmony_ci DUMPER_HILOGE(MODULE_CPU_DATA, "failed to WriteInt32 for martrixVec.size()"); 7200600bfbSopenharmony_ci return false; 7300600bfbSopenharmony_ci } 7400600bfbSopenharmony_ci 7500600bfbSopenharmony_ci for (auto &parcelable : martrixVec) { 7600600bfbSopenharmony_ci if (!data.WriteStringVector(parcelable)) { 7700600bfbSopenharmony_ci DUMPER_HILOGE(MODULE_CPU_DATA, "failed to WriteParcelable for parcelable"); 7800600bfbSopenharmony_ci return false; 7900600bfbSopenharmony_ci } 8000600bfbSopenharmony_ci } 8100600bfbSopenharmony_ci return true; 8200600bfbSopenharmony_ci} 8300600bfbSopenharmony_ci 8400600bfbSopenharmony_cibool DumpCpuData::ReadStringMatrix(std::vector<std::vector<std::string>> &martrixVec, Parcel &data) 8500600bfbSopenharmony_ci{ 8600600bfbSopenharmony_ci int size = 0; 8700600bfbSopenharmony_ci if (!data.ReadInt32(size)) { 8800600bfbSopenharmony_ci return false; 8900600bfbSopenharmony_ci } 9000600bfbSopenharmony_ci DUMPER_HILOGI(MODULE_CPU_DATA, "ReadStringMatrix size = %{public}d", size); 9100600bfbSopenharmony_ci if (size > INT_MAX) { 9200600bfbSopenharmony_ci return false; 9300600bfbSopenharmony_ci } 9400600bfbSopenharmony_ci martrixVec.clear(); 9500600bfbSopenharmony_ci for (int i = 0; i < size; i++) { 9600600bfbSopenharmony_ci std::vector<std::string> vec; 9700600bfbSopenharmony_ci if (!data.ReadStringVector(&vec)) { 9800600bfbSopenharmony_ci DUMPER_HILOGE(MODULE_CPU_DATA, "failed to ReadStringMatrix for martrixVec"); 9900600bfbSopenharmony_ci return false; 10000600bfbSopenharmony_ci } 10100600bfbSopenharmony_ci martrixVec.emplace_back(vec); 10200600bfbSopenharmony_ci } 10300600bfbSopenharmony_ci return true; 10400600bfbSopenharmony_ci} 10500600bfbSopenharmony_ci} // namespace HiviewDFX 10600600bfbSopenharmony_ci} // namespace OHOS 107