1/*
2 * Copyright (c) 2023 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 "dump_cpu_data.h"
16#include "hilog_wrapper.h"
17namespace OHOS {
18namespace HiviewDFX {
19DumpCpuData::DumpCpuData()
20{
21}
22
23DumpCpuData::DumpCpuData(std::string& startTime, std::string& endTime, int cpuUsagePid, StringCpuMatrix dumpCPUDatas)
24    : startTime_(startTime), endTime_(endTime), cpuUsagePid_(cpuUsagePid), dumpCPUDatas_(dumpCPUDatas)
25{
26}
27
28DumpCpuData::~DumpCpuData()
29{
30}
31
32bool DumpCpuData::Marshalling(Parcel& parcel) const
33{
34    RETURN_PARCEL_WRITE_HELPER_RET(parcel, String, startTime_, false);
35    RETURN_PARCEL_WRITE_HELPER_RET(parcel, String, endTime_, false);
36    RETURN_PARCEL_WRITE_HELPER_RET(parcel, Int32, cpuUsagePid_, false);
37    if (!WriteStringMatrix(dumpCPUDatas_, parcel)) {
38        DUMPER_HILOGE(MODULE_CPU_DATA, "failed to write dumpCPUDatas_");
39        return false;
40    }
41    return true;
42}
43
44DumpCpuData *DumpCpuData::Unmarshalling(Parcel& parcel)
45{
46    DumpCpuData *info = new (std::nothrow) DumpCpuData();
47    if (info == nullptr) {
48        DUMPER_HILOGE(MODULE_CPU_DATA, "failed to create DumpCpuData");
49        return nullptr;
50    }
51    if (!info->ReadFromParcel(parcel)) {
52        DUMPER_HILOGE(MODULE_CPU_DATA, "failed to read from parcel");
53        delete info;
54        info = nullptr;
55    }
56    return info;
57}
58
59bool DumpCpuData::ReadFromParcel(Parcel &parcel)
60{
61    RETURN_PARCEL_READ_HELPER_RET(parcel, String, startTime_, false);
62    RETURN_PARCEL_READ_HELPER_RET(parcel, String, endTime_, false);
63    RETURN_PARCEL_READ_HELPER_RET(parcel, Int32, cpuUsagePid_, false);
64    ReadStringMatrix(dumpCPUDatas_, parcel);
65    return true;
66}
67
68bool DumpCpuData::WriteStringMatrix(const std::vector<std::vector<std::string>> &martrixVec, Parcel &data) const
69{
70    if (!data.WriteUint32(martrixVec.size())) {
71        DUMPER_HILOGE(MODULE_CPU_DATA, "failed to WriteInt32 for martrixVec.size()");
72        return false;
73    }
74
75    for (auto &parcelable : martrixVec) {
76        if (!data.WriteStringVector(parcelable)) {
77            DUMPER_HILOGE(MODULE_CPU_DATA, "failed to WriteParcelable for parcelable");
78            return false;
79        }
80    }
81    return true;
82}
83
84bool DumpCpuData::ReadStringMatrix(std::vector<std::vector<std::string>> &martrixVec, Parcel &data)
85{
86    int size = 0;
87    if (!data.ReadInt32(size)) {
88        return false;
89    }
90    DUMPER_HILOGI(MODULE_CPU_DATA, "ReadStringMatrix size = %{public}d", size);
91    if (size > INT_MAX) {
92        return false;
93    }
94    martrixVec.clear();
95    for (int i = 0; i < size; i++) {
96        std::vector<std::string> vec;
97        if (!data.ReadStringVector(&vec)) {
98            DUMPER_HILOGE(MODULE_CPU_DATA, "failed to ReadStringMatrix for martrixVec");
99            return false;
100        }
101        martrixVec.emplace_back(vec);
102    }
103    return true;
104}
105} // namespace HiviewDFX
106} // namespace OHOS
107