1 /*
2  * Copyright (c) 2024 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 "process_decorator.h"
17 
18 namespace OHOS {
19 namespace HiviewDFX {
20 namespace UCollectUtil {
21 const std::string PROCESS_COLLECTOR_NAME = "ProcessCollector";
22 StatInfoWrapper ProcessDecorator::statInfoWrapper_;
23 
GetMemCgProcesses()24 CollectResult<std::unordered_set<int32_t>> ProcessDecorator::GetMemCgProcesses()
25 {
26     auto task = [this] { return processCollector_->GetMemCgProcesses(); };
27     return Invoke(task, statInfoWrapper_, PROCESS_COLLECTOR_NAME + UC_SEPARATOR + __func__);
28 }
29 
IsMemCgProcess(int32_t pid)30 CollectResult<bool> ProcessDecorator::IsMemCgProcess(int32_t pid)
31 {
32     auto task = [this, pid] { return processCollector_->IsMemCgProcess(pid); };
33     return Invoke(task, statInfoWrapper_, PROCESS_COLLECTOR_NAME + UC_SEPARATOR + __func__);
34 }
35 
ExportMemCgProcesses()36 CollectResult<std::string> ProcessDecorator::ExportMemCgProcesses()
37 {
38     auto task = [this] { return processCollector_->ExportMemCgProcesses(); };
39     return Invoke(task, statInfoWrapper_, PROCESS_COLLECTOR_NAME + UC_SEPARATOR + __func__);
40 }
41 
SaveStatCommonInfo()42 void ProcessDecorator::SaveStatCommonInfo()
43 {
44     std::map<std::string, StatInfo> statInfo = statInfoWrapper_.GetStatInfo();
45     std::vector<std::string> formattedStatInfo;
46     for (const auto& record : statInfo) {
47         formattedStatInfo.push_back(record.second.ToString());
48     }
49     WriteLinesToFile(formattedStatInfo, false);
50 }
51 
ResetStatInfo()52 void ProcessDecorator::ResetStatInfo()
53 {
54     statInfoWrapper_.ResetStatInfo();
55 }
56 } // namespace UCollectUtil
57 } // namespace HiviewDFX
58 } // namespace OHOS
59