• 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 <cmath>
16 #include <iostream>
17 #include "include/Temperature.h"
18 namespace OHOS {
19     namespace SmartPerf {
20         pthread_mutex_t Temperature::mutex;
21         Temperature *Temperature::instance = nullptr;
getInstance()22         Temperature *Temperature::getInstance()
23         {
24             if (instance == nullptr) {
25                 pthread_mutex_lock(&mutex);
26                 if (instance == nullptr) {
27                     instance = new Temperature();
28                 }
29                 pthread_mutex_unlock(&mutex);
30             }
31             return instance;
32         }
33 
Temperature()34         Temperature::Temperature()
35         {
36             pthread_mutex_init(&mutex, nullptr);
37             int cnt = sizeof(thermal_path) / sizeof(const char *);
38             for (int i = 0; i < cnt; ++i) {
39                 if (GPUtils::canOpen(std::string(thermal_path[i]) + "/thermal_zone1/temp")) {
40                     thermal_base_path = std::string(thermal_path[i]);
41                     initThermalNode();
42                 }
43             }
44         }
45 
initThermalNode()46         void Temperature::initThermalNode()
47         {
48             char typeNode[256];
49             char tempNode[256];
50             char buffer[256];
51             FILE *fp = nullptr;
52             const int zoneTravelNum = 100;
53             for (int zone = 0; zone < zoneTravelNum; ++zone) {
54                 if (snprintf(typeNode, sizeof(typeNode),
55                 "%s/thermal_zone%d/type", thermal_base_path.c_str(), zone) < 0) {
56                     std::cout << "snprintf fail";
57                 }
58 
59                 fp = fopen(typeNode, "r");
60                 if (fp == nullptr) {
61                     continue;
62                 }
63                 buffer[0] = '\0';
64                 while (fgets(buffer, sizeof(buffer), fp) == nullptr) {
65                     std::cout << "fgets fail";
66                 }
67                 if (fclose(fp) == EOF) {
68                     std::cout << "fclose fail";
69                 }
70 
71                 if (strlen(buffer) == 0) {
72                     continue;
73                 }
74                 if (buffer[strlen(buffer) - 1] == '\n')
75                     buffer[strlen(buffer) - 1] = '\0';
76                 std::string type = std::string(buffer);
77                 if (collect_nodes.count(type) == 0) {
78                     continue;
79                 }
80                 if (snprintf(tempNode, sizeof(tempNode),
81                 "%s/thermal_zone%d/temp", thermal_base_path.c_str(), zone) < 0) {
82                     std::cout << "snprintf fail";
83                 }
84                 thermal_node_path_map[type] = std::string(tempNode);
85             }
86         }
87 
getThermalMap()88         std::map<std::string, float> Temperature::getThermalMap()
89         {
90             std::map<std::string, float> thermal_map;
91 
92             FILE *fp = nullptr;
93             char buffer[256];
94             std::map<std::string, std::string>::iterator iter;
95             for (iter = thermal_node_path_map.begin(); iter != thermal_node_path_map.end(); ++iter) {
96                 std::string type = iter->first;
97                 std::string tempNode = thermal_node_path_map[type];
98                 fp = fopen(tempNode.c_str(), "r");
99                 if (fp == nullptr) {
100                     thermal_map[type] = -1.0f;
101                     continue;
102                 }
103                 buffer[0] = '\0';
104                 while (fgets(buffer, sizeof(buffer), fp) == nullptr) {
105                     std::cout << "fgets fail";
106                 }
107                 float temp = std::fabs(atof(buffer));
108                 if (fclose(fp) == EOF) {
109                     std::cout << "fclose fail";
110                 }
111                 if (strlen(buffer) == 0) {
112                     thermal_map[type] = -1.0f;
113                     continue;
114                 }
115                 thermal_map[type] = temp;
116             }
117             return thermal_map;
118         }
119     }
120 }
121