• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         std::string dirTemp = dir + "/temp";
44 
45         if (!SPUtils::FileAccess(dirType)) {
46             continue;
47         }
48 
49         std::string type;
50         std::string temp;
51         SPUtils::LoadFile(dirType, type);
52         SPUtils::LoadFile(dirTemp, temp);
53         GetTempInfos(result, type, temp);
54     }
55 
56     LOGI("Temperature:ItemData map size(%u)", result.size());
57     return result;
58 }
59 
GetTempInfos(std::map<std::string,std::string> & result,const std::string & type,const std::string & temp)60 void Temperature::GetTempInfos(std::map<std::string, std::string> &result, const std::string& type,
61     const std::string& temp)
62 {
63     for (auto node : collectNodes) {
64         if (!strcmp(type.c_str(), node.c_str())) {
65             float t = SPUtilesTye::StringToSometype<float>(temp);
66             if (node == "gpu" || node.find("cluster") != std::string::npos) {
67                 result[type] = std::to_string(t);
68             } else if (node == "drmos_gpu_npu" || node == "npu_thermal") {
69                 result["npu_thermal"] = std::to_string(t / 1e3);
70             } else {
71                 result[type] = std::to_string(t / 1e3);
72             }
73         }
74     }
75 }
76 }
77 }
78