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