• 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 #include "appfreeze_util.h"
16 
17 #include <chrono>
18 #include <fcntl.h>
19 #include <iostream>
20 #include <iomanip>
21 #include <sstream>
22 
23 #include "directory_ex.h"
24 #include "file_ex.h"
25 #include "string_ex.h"
26 #include "hilog_tag_wrapper.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 namespace {
31     constexpr int64_t MAX_TIME_BUFF = 64;
32     constexpr mode_t DEFAULT_LOG_DIR_MODE = 0770;
33     constexpr mode_t DEFAULT_LOG_FILE_MODE = 0644;
34     constexpr uint32_t TWO_DECIMALS = 2;
35     constexpr size_t CPU_INFO_SIZE = 11;
36 }
37 
AppfreezeUtil()38 AppfreezeUtil::AppfreezeUtil()
39 {
40 }
41 
~AppfreezeUtil()42 AppfreezeUtil::~AppfreezeUtil()
43 {
44 }
45 
CreateFile(const std::string & dirPath,const std::string & fileName)46 std::string AppfreezeUtil::CreateFile(const std::string &dirPath, const std::string &fileName)
47 {
48     if (!OHOS::FileExists(dirPath)) {
49         OHOS::ForceCreateDirectory(dirPath);
50         OHOS::ChangeModeDirectory(dirPath, DEFAULT_LOG_DIR_MODE);
51     }
52     std::string filePath = dirPath + "/" + fileName;
53     FILE* fp = fopen(filePath.c_str(), "w+");
54     chmod(filePath.c_str(), DEFAULT_LOG_FILE_MODE);
55     if (fp == nullptr) {
56         TAG_LOGW(AAFwkTag::APPDFR, "filePath create failed, errno: %{public}d", errno);
57         return "";
58     } else {
59         TAG_LOGI(AAFwkTag::APPDFR, "filePath: %{public}s", filePath.c_str());
60     }
61     (void)fclose(fp);
62     return filePath;
63 }
64 
TimestampFormatToDate(time_t timeStamp,const std::string & format)65 std::string AppfreezeUtil::TimestampFormatToDate(time_t timeStamp, const std::string &format)
66 {
67     char date[MAX_TIME_BUFF] = {0};
68     struct tm result {};
69     if (localtime_r(&timeStamp, &result) != nullptr) {
70         strftime(date, MAX_TIME_BUFF, format.c_str(), &result);
71     }
72     return std::string(date);
73 }
74 
GetMilliseconds()75 uint64_t AppfreezeUtil::GetMilliseconds()
76 {
77     auto now = std::chrono::system_clock::now();
78     auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
79     return millisecs.count();
80 }
81 
RoundToTwoDecimals(float value)82 std::string AppfreezeUtil::RoundToTwoDecimals(float value)
83 {
84     std::stringstream ss;
85     ss<< std::fixed << std::setprecision(TWO_DECIMALS) << value;
86     return ss.str();
87 }
88 
GetCpuCount()89 int AppfreezeUtil::GetCpuCount()
90 {
91     std::string procStatPath = PROC_STAT_PATH;
92     std::string content;
93     if (!LoadStringFromFile(procStatPath, content) || content.empty()) {
94         TAG_LOGW(AAFwkTag::APPDFR, "failed to read path:%{public}s, errno:%{public}d",
95             procStatPath.c_str(), errno);
96         return 0;
97     }
98     int cpuCount = 0;
99     std::istringstream iss(content);
100     std::string line;
101     while (std::getline(iss, line)) {
102         if (line.empty()) {
103             continue;
104         }
105         std::vector<std::string> splitStrs;
106         SplitStr(line, " ", splitStrs);
107         if (splitStrs.size() != CPU_INFO_SIZE) {
108             break;
109         }
110         if (splitStrs[0].find("cpu") != 0) {
111             TAG_LOGW(AAFwkTag::APPDFR, "not find cpu prefix, head: %{public}s.", splitStrs[0].c_str());
112             break;
113         }
114         cpuCount++;
115     }
116     cpuCount -= CPU_COUNT_SUBTRACT;
117     TAG_LOGD(AAFwkTag::APPDFR, "read: %{public}s to get cpu count:%{public}d.", procStatPath.c_str(), cpuCount);
118     return cpuCount;
119 }
120 }  // namespace AppExecFwk
121 }  // namespace OHOS
122