• 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 "memory_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 testserver;
23 
MemoryCollection(PerfMetric perfMetric)24     MemoryCollection::MemoryCollection(PerfMetric perfMetric) : DataCollection(perfMetric)
25     {
26         isCollecting_ = false;
27         startRss_ = INITIAL_VALUE;
28         endRss_ = INITIAL_VALUE;
29         startPss_ = INITIAL_VALUE;
30         endPss_ = INITIAL_VALUE;
31         memoryRss_ = INVALID_VALUE;
32         memoryPss_ = INVALID_VALUE;
33     }
34 
StartCollection(ApiCallErr & error)35     void MemoryCollection::StartCollection(ApiCallErr &error)
36     {
37         if (isCollecting_) {
38             LOG_I("Memory collection has started");
39             return;
40         }
41         pid_ = GetPidByBundleName(bundleName_);
42         if (pid_ == -1) {
43             error = ApiCallErr(ERR_DATA_COLLECTION_FAILED, "The process does not exist during memory collection");
44             return;
45         }
46         ProcessMemoryInfo processMemoryInfo;
47         if (TestServerClient::GetInstance().CollectProcessMemory(pid_, processMemoryInfo) != TEST_SERVER_OK) {
48             error = ApiCallErr(ERR_DATA_COLLECTION_FAILED, "Start memory collection failed");
49             return;
50         }
51         startRss_ = static_cast<double>(processMemoryInfo.rss);
52         startPss_ = static_cast<double>(processMemoryInfo.pss);
53         LOG_I("Start collect memory, startRss_ = %{public}.2f, startPss_ = %{public}.2f", startRss_, startPss_);
54         isCollecting_ = true;
55     }
56 
StopCollectionAndGetResult(ApiCallErr & error)57     double MemoryCollection::StopCollectionAndGetResult(ApiCallErr &error)
58     {
59         if (isCollecting_) {
60             LOG_I("Stop memory collection");
61             if (!IsProcessExist(pid_)) {
62                 error = ApiCallErr(ERR_DATA_COLLECTION_FAILED, "The process does not exist during memory collection");
63                 return INVALID_VALUE;
64             }
65             ProcessMemoryInfo processMemoryInfo;
66             if (TestServerClient::GetInstance().CollectProcessMemory(pid_, processMemoryInfo) != TEST_SERVER_OK) {
67                 error = ApiCallErr(ERR_DATA_COLLECTION_FAILED, "Stop memory collection failed");
68                 isCollecting_ = false;
69                 return INVALID_VALUE;
70             }
71             endRss_ = static_cast<double>(processMemoryInfo.rss);
72             endPss_ = static_cast<double>(processMemoryInfo.pss);
73             LOG_I("End collect memory, endRss_ = %{public}.2f, endPss_ = %{public}.2f", endRss_, endPss_);
74             memoryRss_ = endRss_;
75             memoryPss_ = endPss_;
76             isCollecting_ = false;
77         }
78         switch (perfMetric_) {
79             case MEMORY_RSS:
80                 return memoryRss_;
81             case MEMORY_PSS:
82                 return memoryPss_;
83             default:
84                 return INVALID_VALUE;
85         }
86         return INVALID_VALUE;
87     }
88 } // namespace OHOS::perftest
89