forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core-cpuidle.c
159 lines (138 loc) · 3.7 KB
/
core-cpuidle.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
/*
* Copyright (C) 2023 Colin Ian King.
*
* 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.
*
*/
#include "stress-ng.h"
#include "core-cpuidle.h"
#include "core-sort.h"
static cpu_cstate_t *cpu_cstate_list;
static size_t cpu_cstate_list_len;
cpu_cstate_t *stress_cpuidle_cstate_list_head(void)
{
return cpu_cstate_list;
}
#if defined(__linux__)
static void stress_cpuidle_cstate_add_unique(
const char *cstate,
const uint32_t residency)
{
cpu_cstate_t **cc;
cpu_cstate_t *new_cc;
for (cc = &cpu_cstate_list; *cc; cc = &(*cc)->next) {
const int cmp = strcmp(cstate, (*cc)->cstate);
if (cmp == 0)
return;
if (cmp < 0)
break;
}
new_cc = malloc(sizeof(*new_cc));
if (!new_cc)
return;
new_cc->cstate = strdup(cstate);
if (!new_cc->cstate) {
free(new_cc);
return;
}
new_cc->residency = residency;
new_cc->next = *cc;
*cc = new_cc;
cpu_cstate_list_len++;
}
#endif
void stress_cpuidle_init(void)
{
#if defined(__linux__)
DIR *cpu_dir;
struct dirent *cpu_d;
cpu_cstate_list = NULL;
cpu_cstate_list_len = 0;
cpu_dir = opendir("/sys/devices/system/cpu");
if (!cpu_dir)
return;
/*
* gather all known cpu states for all cpus
*/
while ((cpu_d = readdir(cpu_dir)) != NULL) {
char cpuidle_path[1024];
DIR *cpuidle_dir;
struct dirent *cpuidle_d;
if (strncmp(cpu_d->d_name, "cpu", 3))
continue;
(void)snprintf(cpuidle_path, sizeof(cpuidle_path),
"/sys/devices/system/cpu/%s/cpuidle", cpu_d->d_name);
cpuidle_dir = opendir(cpuidle_path);
if (!cpuidle_dir)
continue;
while ((cpuidle_d = readdir(cpuidle_dir)) != NULL) {
char path[PATH_MAX], data[64], *ptr;
uint32_t residency = 0;
if (strncmp(cpuidle_d->d_name, "state", 5))
continue;
(void)snprintf(path, sizeof(path), "%s/%s/residency", cpuidle_path, cpuidle_d->d_name);
if (stress_system_read(path, data, sizeof(data)) > 0)
residency = (uint32_t)atoi(data);
(void)snprintf(path, sizeof(path), "%s/%s/name", cpuidle_path, cpuidle_d->d_name);
if (stress_system_read(path, data, sizeof(data)) < 1)
continue;
ptr = strchr(data, '\n');
if (ptr)
*ptr = '\0';
stress_cpuidle_cstate_add_unique(data, residency);
}
(void)closedir(cpuidle_dir);
}
(void)closedir(cpu_dir);
#else
cpu_cstate_list = NULL;
cpu_cstate_list_len = 0;
#endif
}
void stress_cpuidle_free(void)
{
cpu_cstate_t *cc = cpu_cstate_list;
while (cc) {
cpu_cstate_t *next = cc->next;
free(cc->cstate);
free(cc);
cc = next;
}
cpu_cstate_list = NULL;
cpu_cstate_list_len = 0;
}
void stress_cpuidle_log_info(void)
{
char *buf;
cpu_cstate_t *cc;
size_t len = 1;
if (cpu_cstate_list_len < 1)
return;
for (cc = cpu_cstate_list; cc; cc = cc->next) {
len += strlen(cc->cstate) + 2;
}
buf = calloc(len, sizeof(*buf));
if (!buf)
return;
for (cc = cpu_cstate_list; cc; cc = cc->next) {
(void)shim_strlcat(buf, cc->cstate, len);
if (cc->next)
(void)shim_strlcat(buf, ", ", len);
}
pr_dbg("CPU%s %zu idle state%s: %s\n",
(cpu_cstate_list_len == 1) ? " has" : "s have",
cpu_cstate_list_len, (cpu_cstate_list_len == 1) ? "" : "s", buf);
free(buf);
}