1 /*
2 * Copyright (C) 2021 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 <iostream>
16 #include "include/sp_utils.h"
17 #include <dirent.h>
18 #include "include/Temperature.h"
19 #include "include/sp_log.h"
20 namespace OHOS {
21 namespace SmartPerf {
ItemData()22 std::map<std::string, std::string> Temperature::ItemData()
23 {
24 DIR *dp = opendir(thermalBasePath.c_str());
25 struct dirent *dirp;
26 std::vector<std::string> dirs;
27 if (dp == nullptr) {
28 LOGE("Open directory failed!");
29 }
30 while ((dirp = readdir(dp)) != nullptr) {
31 if (strcmp(dirp->d_name, ".") != 0 && strcmp(dirp->d_name, "..") != 0) {
32 std::string filename(dirp->d_name);
33 if (filename.find("cooling") == std::string::npos) {
34 dirs.push_back(SPUtils::IncludePathDelimiter(thermalBasePath) + filename);
35 }
36 }
37 }
38 closedir(dp);
39 std::map<std::string, std::string> result;
40 for (auto dir : dirs) {
41 std::string dirType = dir + "/type";
42 LOGD("dirType = %s", dirType.c_str());
43 std::string dirTemp = dir + "/temp";
44 LOGD("dirTemp = %s", dirTemp.c_str());
45
46 if (!SPUtils::FileAccess(dirType)) {
47 continue;
48 }
49
50 std::string type;
51 std::string temp;
52 SPUtils::LoadFile(dirType, type);
53 SPUtils::LoadFile(dirTemp, temp);
54 GetTempInfos(result, type, temp);
55 }
56
57 LOGD("Temperature::ItemData map size(%u)", result.size());
58 return result;
59 }
60
GetTempInfos(std::map<std::string,std::string> & result,std::string type,std::string temp)61 void Temperature::GetTempInfos(std::map<std::string, std::string> &result, std::string type, std::string temp)
62 {
63 for (auto node : collectNodes) {
64 if (type.find(node) != std::string::npos) {
65 float t = SPUtilesTye::StringToSometype<float>(temp);
66 if (node == "gpu") {
67 result[type] = std::to_string(t);
68 } else {
69 result[type] = std::to_string(t / 1e3);
70 }
71 }
72 }
73 }
74 }
75 }
76