xref: /illumos-gate/usr/src/cmd/powertop/common/cpufreq.c (revision c7402f0767d7a0360fabd0bd449c6baf9b282074)
1 /*
2  * Copyright 2009, Intel Corporation
3  * Copyright 2009, Sun Microsystems, Inc
4  *
5  * This file is part of PowerTOP
6  *
7  * This program file is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program in a file named COPYING; if not, write to the
18  * Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301 USA
21  *
22  * Authors:
23  *	Arjan van de Ven <arjan@linux.intel.com>
24  *	Eric C Saxe <eric.saxe@sun.com>
25  *	Aubrey Li <aubrey.li@intel.com>
26  */
27 
28 /*
29  * GPL Disclaimer
30  *
31  * For the avoidance of doubt, except that if any license choice other
32  * than GPL or LGPL is available it will apply instead, Sun elects to
33  * use only the General Public License version 2 (GPLv2) at this time
34  * for any software where a choice of GPL license versions is made
35  * available with the language indicating that GPLv2 or any later
36  * version may be used, or where a choice of which version of the GPL
37  * is applied is otherwise unspecified.
38  */
39 
40 #include <stdlib.h>
41 #include <string.h>
42 #include <dtrace.h>
43 #include <kstat.h>
44 #include <errno.h>
45 #include "powertop.h"
46 
47 #define	HZ2MHZ(speed)	((speed) / MICROSEC)
48 #define	DTP_ARG_COUNT	2
49 #define	DTP_ARG_LENGTH	5
50 
51 static uint64_t		max_cpufreq = 0;
52 static dtrace_hdl_t	*dtp;
53 static char		**dtp_argv;
54 
55 /*
56  * Enabling PM through /etc/power.conf
57  * See suggest_p_state()
58  */
59 static char default_conf[]	= "/etc/power.conf";
60 static char default_pmconf[]	= "/usr/sbin/pmconfig";
61 static char cpupm_enable[]	= " echo cpupm enable >> /etc/power.conf";
62 static char cpupm_treshold[]	= " echo cpu-threshold 1s >> /etc/power.conf";
63 
64 /*
65  * Buffer containing DTrace program to track CPU frequency transitions
66  */
67 static const char *dtp_cpufreq =
68 "hrtime_t last[$0];"
69 ""
70 "BEGIN"
71 "{"
72 "	begin = timestamp;"
73 "}"
74 ""
75 ":::cpu-change-speed"
76 "/last[(processorid_t)arg0] != 0/"
77 "{"
78 "	this->cpu = (processorid_t)arg0;"
79 "	this->oldspeed = (uint64_t)arg1;"
80 "	@times[this->cpu, this->oldspeed] = sum(timestamp - last[this->cpu]);"
81 "	last[this->cpu] = timestamp;"
82 "}"
83 ":::cpu-change-speed"
84 "/last[(processorid_t)arg0] == 0/"
85 "{"
86 "	this->cpu = (processorid_t)arg0;"
87 "	this->oldspeed = (uint64_t)arg1;"
88 "	@times[this->cpu, this->oldspeed] = sum(timestamp - begin);"
89 "	last[this->cpu] = timestamp;"
90 "}";
91 
92 /*
93  * Same as above, but only for a specific CPU
94  */
95 static const char *dtp_cpufreq_c =
96 "hrtime_t last;"
97 ""
98 "BEGIN"
99 "{"
100 "	begin = timestamp;"
101 "}"
102 ""
103 ":::cpu-change-speed"
104 "/(processorid_t)arg0 == $1 &&"
105 " last != 0/"
106 "{"
107 "	this->cpu = (processorid_t)arg0;"
108 "	this->oldspeed = (uint64_t)arg1;"
109 "	@times[this->cpu, this->oldspeed] = sum(timestamp - last);"
110 "	last = timestamp;"
111 "}"
112 ":::cpu-change-speed"
113 "/(processorid_t)arg0 == $1 &&"
114 " last == 0/"
115 "{"
116 "	this->cpu = (processorid_t)arg0;"
117 "	this->oldspeed = (uint64_t)arg1;"
118 "	@times[this->cpu, this->oldspeed] = sum(timestamp - begin);"
119 "	last = timestamp;"
120 "}";
121 
122 static int	pt_cpufreq_setup(void);
123 static int	pt_cpufreq_snapshot(void);
124 static int	pt_cpufreq_dtrace_walk(const dtrace_aggdata_t *, void *);
125 static void	pt_cpufreq_stat_account(double, uint_t);
126 static int	pt_cpufreq_snapshot_cpu(kstat_ctl_t *,
127     uint_t);
128 
129 static int
130 pt_cpufreq_setup(void)
131 {
132 	if ((dtp_argv = malloc(sizeof (char *) * DTP_ARG_COUNT)) == NULL)
133 		return (EXIT_FAILURE);
134 
135 	if ((dtp_argv[0] = malloc(sizeof (char) * DTP_ARG_LENGTH)) == NULL) {
136 		free(dtp_argv);
137 		return (EXIT_FAILURE);
138 	}
139 
140 	(void) snprintf(dtp_argv[0], 5, "%d\0", g_ncpus_observed);
141 
142 	if (PT_ON_CPU) {
143 		if ((dtp_argv[1] = malloc(sizeof (char) * DTP_ARG_LENGTH))
144 		    == NULL) {
145 			free(dtp_argv[0]);
146 			free(dtp_argv);
147 			return (EXIT_FAILURE);
148 		}
149 		(void) snprintf(dtp_argv[1], 5, "%d\0", g_observed_cpu);
150 	}
151 
152 	return (0);
153 }
154 
155 /*
156  * Perform setup necessary to enumerate and track CPU speed changes
157  */
158 int
159 pt_cpufreq_stat_prepare(void)
160 {
161 	dtrace_prog_t 		*prog;
162 	dtrace_proginfo_t 	info;
163 	dtrace_optval_t 	statustime;
164 	kstat_ctl_t 		*kc;
165 	kstat_t 		*ksp;
166 	kstat_named_t 		*knp;
167 	freq_state_info_t 	*state;
168 	char 			*s, *token, *prog_ptr;
169 	int 			err;
170 
171 	if ((err = pt_cpufreq_setup()) != 0) {
172 		pt_error("%s : failed to setup", __FILE__);
173 		return (errno);
174 	}
175 
176 	state = g_pstate_info;
177 	if ((g_cpu_power_states = calloc((size_t)g_ncpus,
178 	    sizeof (cpu_power_info_t))) == NULL)
179 		return (-1);
180 
181 	/*
182 	 * Enumerate the CPU frequencies
183 	 */
184 	if ((kc = kstat_open()) == NULL)
185 		return (errno);
186 
187 	ksp = kstat_lookup(kc, "cpu_info", g_cpu_table[g_observed_cpu], NULL);
188 
189 	if (ksp == NULL) {
190 		err = errno;
191 		(void) kstat_close(kc);
192 		return (err);
193 	}
194 
195 	(void) kstat_read(kc, ksp, NULL);
196 
197 	knp = kstat_data_lookup(ksp, "supported_frequencies_Hz");
198 	s = knp->value.str.addr.ptr;
199 
200 	g_npstates = 0;
201 
202 	for (token = strtok(s, ":"), s = NULL;
203 	    NULL != token && g_npstates < NSTATES;
204 	    token = strtok(NULL, ":")) {
205 
206 		state->speed = HZ2MHZ(atoll(token));
207 
208 		if (state->speed > max_cpufreq)
209 			max_cpufreq = state->speed;
210 
211 		state->total_time = (uint64_t)0;
212 
213 		g_npstates++;
214 		state++;
215 	}
216 
217 	if (token != NULL)
218 		pt_error("%s : exceeded NSTATES\n", __FILE__);
219 
220 	(void) kstat_close(kc);
221 
222 	/*
223 	 * Return if speed transition is not supported
224 	 */
225 	if (g_npstates < 2)
226 		return (-1);
227 
228 	/*
229 	 * Setup DTrace to look for CPU frequency changes
230 	 */
231 	if ((dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
232 		pt_error("%s : cannot open dtrace library: %s\n", __FILE__,
233 		    dtrace_errmsg(NULL, err));
234 		return (-2);
235 	}
236 
237 	/*
238 	 * Execute different scripts (defined above) depending on
239 	 * user specified options. Default mode uses dtp_cpufreq.
240 	 */
241 	if (PT_ON_CPU)
242 		prog_ptr = (char *)dtp_cpufreq_c;
243 	else
244 		prog_ptr = (char *)dtp_cpufreq;
245 
246 	if ((prog = dtrace_program_strcompile(dtp, prog_ptr,
247 	    DTRACE_PROBESPEC_NAME, 0, (1 + g_argc), dtp_argv)) == NULL) {
248 		pt_error("%s : cpu-change-speed probe unavailable\n", __FILE__);
249 		return (dtrace_errno(dtp));
250 	}
251 
252 	if (dtrace_program_exec(dtp, prog, &info) == -1) {
253 		pt_error("%s : failed to enable speed probe\n", __FILE__);
254 		return (dtrace_errno(dtp));
255 	}
256 
257 	if (dtrace_setopt(dtp, "aggsize", "128k") == -1) {
258 		pt_error("%s : failed to set speed 'aggsize'\n", __FILE__);
259 	}
260 
261 	if (dtrace_setopt(dtp, "aggrate", "0") == -1) {
262 		pt_error("%s : failed to set speed 'aggrate'\n", __FILE__);
263 	}
264 
265 	if (dtrace_setopt(dtp, "aggpercpu", 0) == -1) {
266 		pt_error("%s : failed to set speed 'aggpercpu'\n", __FILE__);
267 	}
268 
269 	if (dtrace_go(dtp) != 0) {
270 		pt_error("%s : failed to start speed observation", __FILE__);
271 		return (dtrace_errno(dtp));
272 	}
273 
274 	if (dtrace_getopt(dtp, "statusrate", &statustime) == -1) {
275 		pt_error("%s : failed to get speed 'statusrate'\n", __FILE__);
276 		return (dtrace_errno(dtp));
277 	}
278 
279 	return (0);
280 }
281 
282 /*
283  * The DTrace probes have already been enabled, and are tracking
284  * CPU speed transitions. Take a snapshot of the aggregations, and
285  * look for any CPUs that have made a speed transition over the last
286  * sampling interval. Note that the aggregations may be empty if no
287  * speed transitions took place over the last interval. In that case,
288  * notate that we have already accounted for the time, so that when
289  * we do encounter a speed transition in a future sampling interval
290  * we can subtract that time back out.
291  */
292 int
293 pt_cpufreq_stat_collect(double interval)
294 {
295 	int i, ret;
296 
297 	/*
298 	 * Zero out the interval time reported by DTrace for
299 	 * this interval
300 	 */
301 	for (i = 0; i < g_npstates; i++)
302 		g_pstate_info[i].total_time = 0;
303 
304 	for (i = 0; i < g_ncpus; i++)
305 		g_cpu_power_states[i].dtrace_time = 0;
306 
307 	if (dtrace_status(dtp) == -1)
308 		return (-1);
309 
310 	if (dtrace_aggregate_snap(dtp) != 0)
311 		pt_error("%s : failed to add to stats aggregation", __FILE__);
312 
313 	if (dtrace_aggregate_walk_keyvarsorted(dtp, pt_cpufreq_dtrace_walk,
314 	    NULL) != 0)
315 		pt_error("%s : failed to sort stats aggregation", __FILE__);
316 
317 	dtrace_aggregate_clear(dtp);
318 
319 	if ((ret = pt_cpufreq_snapshot()) != 0) {
320 		pt_error("%s : failed to add to stats aggregation", __FILE__);
321 		return (ret);
322 	}
323 
324 	switch (g_op_mode) {
325 	case PT_MODE_CPU:
326 		pt_cpufreq_stat_account(interval, g_observed_cpu);
327 		break;
328 	case PT_MODE_DEFAULT:
329 	default:
330 		for (i = 0; i < g_ncpus_observed; i++)
331 			pt_cpufreq_stat_account(interval, i);
332 		break;
333 	}
334 
335 	return (0);
336 }
337 
338 static void
339 pt_cpufreq_stat_account(double interval, uint_t cpu)
340 {
341 	cpu_power_info_t 	*cpu_pow;
342 	uint64_t 		speed;
343 	hrtime_t 		duration;
344 	int			i;
345 
346 	cpu_pow = &g_cpu_power_states[cpu];
347 	speed = cpu_pow->current_pstate;
348 
349 	duration = (hrtime_t)(interval * NANOSEC) - cpu_pow->dtrace_time;
350 
351 	/*
352 	 * 'duration' may be a negative value when we're using or forcing a
353 	 * small interval, and the amount of time already accounted ends up
354 	 * being larger than the the former.
355 	 */
356 	if (duration < 0)
357 		return;
358 
359 	for (i = 0; i < g_npstates; i++) {
360 		if (g_pstate_info[i].speed == speed) {
361 			g_pstate_info[i].total_time += duration;
362 			cpu_pow->time_accounted += duration;
363 			cpu_pow->speed_accounted = speed;
364 		}
365 	}
366 }
367 
368 /*
369  * Take a snapshot of each CPU's speed by looking through the cpu_info kstats.
370  */
371 static int
372 pt_cpufreq_snapshot(void)
373 {
374 	kstat_ctl_t 	*kc;
375 	int 		ret;
376 	uint_t		i;
377 
378 	if ((kc = kstat_open()) == NULL)
379 		return (errno);
380 
381 	switch (g_op_mode) {
382 	case PT_MODE_CPU:
383 		ret = pt_cpufreq_snapshot_cpu(kc, g_observed_cpu);
384 		break;
385 	case PT_MODE_DEFAULT:
386 	default:
387 		for (i = 0; i < g_ncpus_observed; i++)
388 			if ((ret = pt_cpufreq_snapshot_cpu(kc, i)) != 0)
389 				break;
390 		break;
391 	}
392 
393 	if (kstat_close(kc) != 0)
394 		pt_error("%s : couldn't close kstat\n", __FILE__);
395 
396 	return (ret);
397 }
398 
399 static int
400 pt_cpufreq_snapshot_cpu(kstat_ctl_t *kc, uint_t cpu)
401 {
402 	kstat_t 		*ksp;
403 	kstat_named_t 		*knp;
404 
405 	ksp = kstat_lookup(kc, "cpu_info", g_cpu_table[cpu], NULL);
406 	if (ksp == NULL) {
407 		pt_error("%s : couldn't find cpu_info kstat for CPU "
408 		"%d\n", __FILE__, cpu);
409 		return (1);
410 	}
411 
412 	if (kstat_read(kc, ksp, NULL) == -1) {
413 		pt_error("%s : couldn't read cpu_info kstat for "
414 		    "CPU %d\n", __FILE__, cpu);
415 		return (2);
416 	}
417 
418 	knp = kstat_data_lookup(ksp, "current_clock_Hz");
419 	if (knp == NULL) {
420 		pt_error("%s : couldn't find current_clock_Hz "
421 		    "kstat for CPU %d\n", __FILE__, cpu);
422 		return (3);
423 	}
424 
425 	g_cpu_power_states[cpu].current_pstate = HZ2MHZ(knp->value.ui64);
426 
427 	return (0);
428 }
429 
430 /*
431  * DTrace aggregation walker that sorts through a snapshot of the
432  * aggregation data collected during firings of the cpu-change-speed
433  * probe.
434  */
435 /*ARGSUSED*/
436 static int
437 pt_cpufreq_dtrace_walk(const dtrace_aggdata_t *data, void *arg)
438 {
439 	dtrace_aggdesc_t 	*aggdesc = data->dtada_desc;
440 	dtrace_recdesc_t 	*cpu_rec, *speed_rec;
441 	cpu_power_info_t 	*cp;
442 	int32_t 		cpu;
443 	uint64_t 		speed;
444 	hrtime_t 		res;
445 	int 			i;
446 
447 	if (strcmp(aggdesc->dtagd_name, "times") == 0) {
448 		cpu_rec = &aggdesc->dtagd_rec[1];
449 		speed_rec = &aggdesc->dtagd_rec[2];
450 
451 		/* LINTED - alignment */
452 		cpu = *(int32_t *)(data->dtada_data + cpu_rec->dtrd_offset);
453 
454 		/* LINTED - alignment */
455 		res = *((hrtime_t *)(data->dtada_percpu[cpu]));
456 
457 		/* LINTED - alignment */
458 		speed = *(uint64_t *)(data->dtada_data +
459 		    speed_rec->dtrd_offset);
460 
461 		if (speed == 0)
462 			speed = max_cpufreq;
463 		else
464 			speed = HZ2MHZ(speed);
465 
466 		/*
467 		 * We have an aggregation record for "cpu" being at "speed"
468 		 * for an interval of "n" nanoseconds. The reported interval
469 		 * may exceed the powertop sampling interval, since we only
470 		 * notice during potentially infrequent firings of the
471 		 * "speed change" DTrace probe. In this case powertop would
472 		 * have already accounted for the portions of the interval
473 		 * that happened during prior powertop samplings, so subtract
474 		 * out time already accounted.
475 		 */
476 		cp = &g_cpu_power_states[cpu];
477 
478 		for (i = 0; i < g_npstates; i++) {
479 			if (g_pstate_info[i].speed == speed) {
480 
481 				if (cp->time_accounted > 0 &&
482 				    cp->speed_accounted == speed) {
483 					if (res > cp->time_accounted) {
484 						res -= cp->time_accounted;
485 						cp->time_accounted = 0;
486 						cp->speed_accounted = 0;
487 					} else {
488 						return (DTRACE_AGGWALK_NEXT);
489 					}
490 				}
491 
492 				g_pstate_info[i].total_time += res;
493 				cp->dtrace_time += res;
494 			}
495 		}
496 	}
497 
498 	return (DTRACE_AGGWALK_NEXT);
499 }
500 
501 /*
502  * Used as a suggestion, sets PM in /etc/power.conf and
503  * a 1sec threshold, then calls /usr/sbin/pmconfig
504  */
505 void
506 enable_p_state(void)
507 {
508 	(void) system(cpupm_enable);
509 	(void) system(cpupm_treshold);
510 	(void) system(default_pmconf);
511 }
512 
513 /*
514  * Checks if PM is enabled in /etc/power.conf, enabling if not
515  */
516 void
517 suggest_p_state(void)
518 {
519 	char 	line[1024];
520 	FILE 	*file;
521 
522 	/*
523 	 * Return if speed transition is not supported
524 	 */
525 	if (g_npstates < 2)
526 		return;
527 
528 	file = fopen(default_conf, "r");
529 
530 	if (!file)
531 		return;
532 
533 	(void) memset(line, 0, 1024);
534 
535 	while (fgets(line, 1023, file)) {
536 		if (strstr(line, "cpupm")) {
537 			if (strstr(line, "enable")) {
538 				(void) fclose(file);
539 				return;
540 			}
541 		}
542 	}
543 
544 	add_suggestion("Suggestion: enable CPU power management by "
545 	    "pressing the P key",  40, 'P', "P - Enable p-state",
546 	    enable_p_state);
547 
548 	(void) fclose(file);
549 }
550