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