forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core-thermal-zone.c
196 lines (172 loc) · 4.5 KB
/
core-thermal-zone.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright (C) 2013-2019 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <colin.king@canonical.com> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <apw@rossby.metr.ou.edu> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
#if defined(STRESS_THERMAL_ZONES)
/*
* tz_init()
* gather all thermal zones
*/
int tz_init(tz_info_t **tz_info_list)
{
DIR *dir;
struct dirent *entry;
size_t i = 0;
dir = opendir("/sys/class/thermal");
if (!dir)
return 0;
while ((entry = readdir(dir)) != NULL) {
char path[PATH_MAX];
FILE *fp;
tz_info_t *tz_info;
/* Ignore non TZ interfaces */
if (strncmp(entry->d_name, "thermal_zone", 12))
continue;
/* Ensure we don't overstep the max limit of TZs */
if (i >= STRESS_THERMAL_ZONES_MAX)
break;
if ((tz_info = calloc(1, sizeof(*tz_info))) == NULL) {
pr_err("Cannot allocate thermal information\n");
(void)closedir(dir);
return -1;
}
(void)snprintf(path, sizeof(path),
"/sys/class/thermal/%s/type",
entry->d_name);
tz_info->path = strdup(entry->d_name);
if (!tz_info->path) {
free(tz_info);
(void)closedir(dir);
return -1;
}
tz_info->type = NULL;
if ((fp = fopen(path, "r")) != NULL) {
char type[128];
if (fgets(type, sizeof(type), fp) != NULL) {
type[strcspn(type, "\n")] = '\0';
tz_info->type = strdup(type);
}
(void)fclose(fp);
}
if (!tz_info->type) {
free(tz_info->path);
free(tz_info);
(void)closedir(dir);
return -1;
}
tz_info->index = i++;
tz_info->next = *tz_info_list;
*tz_info_list = tz_info;
}
(void)closedir(dir);
return 0;
}
/*
* tz_free()
* free thermal zones
*/
void tz_free(tz_info_t **tz_info_list)
{
tz_info_t *tz_info = *tz_info_list;
while (tz_info) {
tz_info_t *next = tz_info->next;
free(tz_info->path);
free(tz_info->type);
free(tz_info);
tz_info = next;
}
}
/*
* tz_get_temperatures()
* collect valid thermal_zones details
*/
int tz_get_temperatures(tz_info_t **tz_info_list, stress_tz_t *tz)
{
tz_info_t *tz_info;
for (tz_info = *tz_info_list; tz_info; tz_info = tz_info->next) {
char path[PATH_MAX];
FILE *fp;
const size_t i = tz_info->index;
(void)snprintf(path, sizeof(path),
"/sys/class/thermal/%s/temp",
tz_info->path);
tz->tz_stat[i].temperature = 0;
if ((fp = fopen(path, "r")) != NULL) {
if (fscanf(fp, "%" SCNu64,
&tz->tz_stat[i].temperature) != 1) {
tz->tz_stat[i].temperature = 0;
}
(void)fclose(fp);
}
}
return 0;
}
/*
* tz_dump()
* dump thermal zone temperatures
*/
void tz_dump(FILE *yaml, proc_info_t *procs_head)
{
bool no_tz_stats = true;
proc_info_t *pi;
pr_yaml(yaml, "thermal-zones:\n");
for (pi = procs_head; pi; pi = pi->next) {
tz_info_t *tz_info;
int32_t j;
uint64_t total = 0;
uint32_t count = 0;
bool dumped_heading = false;
for (tz_info = g_shared->tz_info; tz_info; tz_info = tz_info->next) {
for (j = 0; j < pi->started_procs; j++) {
const uint64_t temp =
pi->stats[j]->tz.tz_stat[tz_info->index].temperature;
/* Avoid crazy temperatures. e.g. > 250 C */
if (temp <= 250000) {
total += temp;
count++;
}
}
if (total) {
const double temp = ((double)total / count) / 1000.0;
char *munged = stress_munge_underscore(pi->stressor->name);
if (!dumped_heading) {
dumped_heading = true;
pr_inf("%s:\n", munged);
pr_yaml(yaml, " - stressor: %s\n",
munged);
}
pr_inf("%20s %7.2f C (%.2f K)\n",
tz_info->type, temp, temp + 273.15);
pr_yaml(yaml, " %s: %7.2f\n",
tz_info->type, temp);
no_tz_stats = false;
}
}
if (total)
pr_yaml(yaml, "\n");
}
if (no_tz_stats)
pr_inf("thermal zone temperatures not available\n");
}
#endif