-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpufreqstats.c
194 lines (162 loc) · 4.71 KB
/
cpufreqstats.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
/*
* Copyright 2007, Intel Corporation
*
* This file is part of PowerTOP
*
* This program file 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; version 2 of the License.
*
* 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 in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authors:
* Arjan van de Ven <[email protected]>
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <dirent.h>
#include "powertop.h"
#define MAX_PSTATES 32
#define MAX_CPUS 8
struct cpufreqdata {
uint64_t frequency;
uint64_t count;
};
struct cpufreqdata freqs[MAX_CPUS][MAX_PSTATES];
struct cpufreqdata oldfreqs[MAX_CPUS][MAX_PSTATES];
struct cpufreqdata delta[MAX_CPUS][MAX_PSTATES];
char cpufreqstrings[25][256];
int topfreq = -1;
static void zap(void)
{
memset(freqs, 0, sizeof(freqs));
}
int sort_by_count (const void *av, const void *bv)
{
const struct cpufreqdata *a = av, *b = bv;
return b->count - a->count;
}
int sort_by_freq (const void *av, const void *bv)
{
const struct cpufreqdata *a = av, *b = bv;
return b->frequency - a->frequency;
}
static char *HzToHuman(unsigned long hz)
{
static char buffer[1024];
memset(buffer, 0, 1024);
unsigned long long Hz;
Hz = hz;
/* default: just put the Number in */
sprintf(buffer,_("%9lli"), Hz);
if (Hz>1000)
sprintf(buffer, _("%6lli Mhz"), (Hz+500)/1000);
if (Hz>1500000)
sprintf(buffer, _("%6.2f Ghz"), (Hz+5000.0)/1000000);
return buffer;
}
void do_cpufreq_stats(void)
{
DIR *dir;
struct dirent *dirent;
FILE *file;
char filename[PATH_MAX];
char line[1024];
int cpu = -1;
int ret, cpucount;
int maxfreq = 0;
uint64_t total_time[MAX_CPUS] = {0};
memcpy(&oldfreqs, &freqs, sizeof(freqs));
memset(&cpufreqstrings, 0, sizeof(cpufreqstrings));
sprintf(cpufreqstrings[0], _("P-states (frequencies)\n"));
for (ret = 0; ret<MAX_PSTATES; ret++) {
for (cpucount = 0; cpucount < MAX_CPUS; cpucount++)
freqs[cpucount][ret].count = 0;
}
dir = opendir("/sys/devices/system/cpu");
if (!dir)
return;
while ((dirent = readdir(dir))) {
int i;
if (dirent->d_name[0]=='.')
continue;
sprintf(filename, "/sys/devices/system/cpu/%s/cpufreq/stats/time_in_state", dirent->d_name);
file = fopen(filename, "r");
if (!file)
continue;
memset(line, 0, 1024);
cpu++;
if ( cpu >= MAX_CPUS)
cpu = MAX_CPUS -1;
i = 0;
while (!feof(file)) {
uint64_t f,count;
char *c;
if (fgets(line, 1023,file)==NULL)
break;
f = strtoull(line, &c, 10);
if (!c)
break;
count = strtoull(c, NULL, 10);
if (freqs[cpu][i].frequency && freqs[cpu][i].frequency != f) {
zap();
break;
}
freqs[cpu][i].frequency = f;
freqs[cpu][i].count += count;
if (f && maxfreq < i)
maxfreq = i;
i++;
if (i>(MAX_PSTATES - 1))
break;
}
fclose(file);
}
closedir(dir);
for (ret = 0; ret < MAX_PSTATES; ret++) {
for (cpucount = 0; cpucount < MAX_CPUS; cpucount++) {
delta[cpucount][ret].count = freqs[cpucount][ret].count - oldfreqs[cpucount][ret].count;
total_time[cpucount] += delta[cpucount][ret].count;
delta[cpucount][ret].frequency = freqs[cpucount][ret].frequency;
if (freqs[cpucount][ret].frequency != oldfreqs[cpucount][ret].frequency)
return; /* duff data */
}
}
for (cpucount = 0 ; cpucount < MAX_CPUS; cpucount++) {
if (total_time[cpucount])
break;
}
if (cpucount == MAX_CPUS)
return;
topfreq = -1;
for (ret = 0 ; ret<=maxfreq && ret<=254; ret++) {
uint64_t last_freq = -1;
strlcpy(cpufreqstrings[ret+1], " ",sizeof(cpufreqstrings[ret+1]));
for ( cpucount = 0; cpucount <= cpu; cpucount++) {
char temp_freq[32];
if (delta[cpucount][ret].frequency != last_freq && delta[cpucount][ret].frequency != 0) {
snprintf(temp_freq, sizeof(temp_freq), "%6s", HzToHuman(delta[cpucount][ret].frequency));
strncat(cpufreqstrings[ret+1], temp_freq, 32);
last_freq = delta[cpucount][ret].frequency;
}
snprintf(temp_freq, sizeof(temp_freq), "\t%5.1f%% ", delta[cpucount][ret].count * 100.0 / total_time[cpucount]);
strncat(cpufreqstrings[ret+1], temp_freq, 32);
if (delta[cpucount][ret].count > total_time[cpucount]/2)
topfreq = ret;
}
strncat(cpufreqstrings[ret+1], "\n", 1);
}
}