• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 
16 #include "cpu_collection.h"
17 #include "test_server_client.h"
18 #include "test_server_error_code.h"
19 
20 namespace OHOS::perftest {
21     using namespace std;
22     using namespace OHOS::testserver;
23 
CpuCollection(PerfMetric perfMetric)24     CpuCollection::CpuCollection(PerfMetric perfMetric) : DataCollection(perfMetric)
25     {
26         isCollecting_ = false;
27         cpuLoad_ = INVALID_VALUE;
28         cpuUsage_ = INVALID_VALUE;
29     }
30 
StartCollection(ApiCallErr & error)31     void CpuCollection::StartCollection(ApiCallErr &error)
32     {
33         if (isCollecting_) {
34             LOG_I("Cpu collection has started");
35             return;
36         }
37         pid_ = GetPidByBundleName(bundleName_);
38         if (pid_ == -1) {
39             error = ApiCallErr(ERR_DATA_COLLECTION_FAILED, "The process does not exist during cpu collection");
40             return;
41         }
42         ProcessCpuInfo processCpuInfo;
43         if (TestServerClient::GetInstance().CollectProcessCpu(pid_, true, processCpuInfo) != TEST_SERVER_OK) {
44             error = ApiCallErr(ERR_DATA_COLLECTION_FAILED, "Start cpu collection failed");
45             return;
46         }
47         isCollecting_ = true;
48     }
49 
StopCollectionAndGetResult(ApiCallErr & error)50     double CpuCollection::StopCollectionAndGetResult(ApiCallErr &error)
51     {
52         if (isCollecting_) {
53             LOG_I("Stop cpu collection");
54             if (!IsProcessExist(pid_)) {
55                 error = ApiCallErr(ERR_DATA_COLLECTION_FAILED, "The process does not exist during cpu collection");
56                 isCollecting_ = false;
57                 return INVALID_VALUE;
58             }
59             ProcessCpuInfo processCpuInfo;
60             if (TestServerClient::GetInstance().CollectProcessCpu(pid_, true, processCpuInfo) != TEST_SERVER_OK) {
61                 error = ApiCallErr(ERR_DATA_COLLECTION_FAILED, "Stop cpu collection failed");
62                 isCollecting_ = false;
63                 return INVALID_VALUE;
64             }
65             cpuLoad_ = processCpuInfo.cpuLoad * ONE_HUNDRED;
66             cpuUsage_ = processCpuInfo.cpuUsage * ONE_HUNDRED;
67             LOG_I("End collect cpu, cpuLoad_ = %{public}.2f, cpuUsage_ = %{public}.2f", cpuLoad_, cpuUsage_);
68             isCollecting_ = false;
69         }
70         switch (perfMetric_) {
71             case CPU_LOAD:
72                 return cpuLoad_;
73             case CPU_USAGE:
74                 return cpuUsage_;
75             default:
76                 return INVALID_VALUE;
77         }
78         return INVALID_VALUE;
79     }
80 } // namespace OHOS::perftest
81