1 /*
2  * Copyright (c) 2023 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 "io_calculator.h"
16 
17 #include "file_util.h"
18 #include "hiview_logger.h"
19 #include "string_util.h"
20 
21 namespace OHOS {
22 namespace HiviewDFX {
23 namespace UCollectUtil {
24 DEFINE_LOG_TAG("UCollectUtil-IoCalculator");
25 namespace {
26 constexpr int EMMC_SIZE_INDEX = 10;
27 }
28 
GetEMMCSize(const std::string & path)29 int64_t IoCalculator::GetEMMCSize(const std::string &path)
30 {
31     int64_t eMMCSize = -1;
32     std::string sizeStr;
33     std::string filePath = path + "/size";
34     if (FileUtil::FileExists(filePath)) {
35         sizeStr = FileUtil::GetFirstLine(filePath);
36     } else {
37         for (int index = 0; index < EMMC_SIZE_INDEX; ++index) {
38             filePath = path + "/block/mmcblk" + std::to_string(index) + "/size";
39             if (FileUtil::FileExists(filePath)) {
40                 sizeStr = FileUtil::GetFirstLine(filePath);
41                 break;
42             }
43         }
44     }
45 
46     if (!sizeStr.empty()) {
47         StringUtil::ConvertStringTo<int64_t>(sizeStr, eMMCSize);
48     } else {
49         HIVIEW_LOGE("load file=%{public}s failed.", filePath.c_str());
50     }
51     return eMMCSize;
52 }
53 
GetEMMCManfid(const std::string & path)54 std::string IoCalculator::GetEMMCManfid(const std::string &path)
55 {
56     std::string filePath = path + "/manfid";
57     std::string eMMCManfid = FileUtil::GetFirstLine(filePath);
58     if (eMMCManfid.empty()) {
59         HIVIEW_LOGE("load file=%{public}s failed.", filePath.c_str());
60         return eMMCManfid;
61     }
62 
63     int manfid = StringUtil::StrToInt(eMMCManfid);
64     switch (manfid) {
65         case 0X000003:
66         case 0X000045:
67         case 0X000002:
68             eMMCManfid = "0x03";
69             break;
70         case 0X000011:
71         case 0X000098:
72         case 0X000198:
73             eMMCManfid = "0x02";
74             break;
75         case 0X000090:
76         case 0X0000AD:
77         case 0X0001AD:
78             eMMCManfid = "0x04";
79             break;
80         case 0X0000FE:
81         case 0X00002C:
82         case 0X000013:
83             eMMCManfid = "0x05";
84             break;
85         case 0X000015:
86         case 0X0000CE:
87         case 0X0001CE:
88             eMMCManfid = "0x01";
89             break;
90         default:
91             break;
92     }
93     return eMMCManfid;
94 }
95 
PercentValue(uint64_t preValue,uint64_t currValue,uint64_t period)96 double IoCalculator::PercentValue(uint64_t preValue, uint64_t currValue, uint64_t period)
97 {
98     if (period == 0) {
99         return 0;
100     }
101     uint64_t subValue = (currValue > preValue) ? (currValue - preValue) : 0;
102     return ((subValue * 1.0) / period);
103 }
104 } // UCollectUtil
105 } // HiViewDFX
106 } // OHOS
107