xref: /freebsd/usr.sbin/powerd/powerd.c (revision 7aa383846770374466b1dcb2cefd71bde9acf463)
1 /*-
2  * Copyright (c) 2004 Colin Percival
3  * Copyright (c) 2005 Nate Lawson
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted providing that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/ioctl.h>
33 #include <sys/sysctl.h>
34 #include <sys/resource.h>
35 #include <sys/socket.h>
36 #include <sys/time.h>
37 #include <sys/un.h>
38 
39 #include <err.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <libutil.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 #ifdef __i386__
50 #define USE_APM
51 #endif
52 
53 #ifdef USE_APM
54 #include <machine/apm_bios.h>
55 #endif
56 
57 #define DEFAULT_ACTIVE_PERCENT	75
58 #define DEFAULT_IDLE_PERCENT	50
59 #define DEFAULT_POLL_INTERVAL	250	/* Poll interval in milliseconds */
60 
61 typedef enum {
62 	MODE_MIN,
63 	MODE_ADAPTIVE,
64 	MODE_HIADAPTIVE,
65 	MODE_MAX,
66 } modes_t;
67 
68 typedef enum {
69 	SRC_AC,
70 	SRC_BATTERY,
71 	SRC_UNKNOWN,
72 } power_src_t;
73 
74 const char *modes[] = {
75 	"AC",
76 	"battery",
77 	"unknown"
78 };
79 
80 #define ACPIAC		"hw.acpi.acline"
81 #define PMUAC		"dev.pmu.0.acline"
82 #define APMDEV		"/dev/apm"
83 #define DEVDPIPE	"/var/run/devd.pipe"
84 #define DEVCTL_MAXBUF	1024
85 
86 static int	read_usage_times(int *load);
87 static int	read_freqs(int *numfreqs, int **freqs, int **power);
88 static int	set_freq(int freq);
89 static void	acline_init(void);
90 static void	acline_read(void);
91 static int	devd_init(void);
92 static void	devd_close(void);
93 static void	handle_sigs(int sig);
94 static void	parse_mode(char *arg, int *mode, int ch);
95 static void	usage(void);
96 
97 /* Sysctl data structures. */
98 static int	cp_times_mib[2];
99 static int	freq_mib[4];
100 static int	levels_mib[4];
101 static int	acline_mib[4];
102 static size_t	acline_mib_len;
103 
104 /* Configuration */
105 static int	cpu_running_mark;
106 static int	cpu_idle_mark;
107 static int	poll_ival;
108 static int	vflag;
109 
110 static volatile sig_atomic_t exit_requested;
111 static power_src_t acline_status;
112 static enum {
113 	ac_none,
114 	ac_sysctl,
115 	ac_acpi_devd,
116 #ifdef USE_APM
117 	ac_apm,
118 #endif
119 } acline_mode;
120 #ifdef USE_APM
121 static int	apm_fd = -1;
122 #endif
123 static int	devd_pipe = -1;
124 
125 #define DEVD_RETRY_INTERVAL 60 /* seconds */
126 static struct timeval tried_devd;
127 
128 static int
129 read_usage_times(int *load)
130 {
131 	static long *cp_times = NULL, *cp_times_old = NULL;
132 	static int ncpus = 0;
133 	size_t cp_times_len;
134 	int error, cpu, i, total;
135 
136 	if (cp_times == NULL) {
137 		cp_times_len = 0;
138 		error = sysctl(cp_times_mib, 2, NULL, &cp_times_len, NULL, 0);
139 		if (error)
140 			return (error);
141 		if ((cp_times = malloc(cp_times_len)) == NULL)
142 			return (errno);
143 		if ((cp_times_old = malloc(cp_times_len)) == NULL) {
144 			free(cp_times);
145 			cp_times = NULL;
146 			return (errno);
147 		}
148 		ncpus = cp_times_len / (sizeof(long) * CPUSTATES);
149 	}
150 
151 	cp_times_len = sizeof(long) * CPUSTATES * ncpus;
152 	error = sysctl(cp_times_mib, 2, cp_times, &cp_times_len, NULL, 0);
153 	if (error)
154 		return (error);
155 
156 	if (load) {
157 		*load = 0;
158 		for (cpu = 0; cpu < ncpus; cpu++) {
159 			total = 0;
160 			for (i = 0; i < CPUSTATES; i++) {
161 			    total += cp_times[cpu * CPUSTATES + i] -
162 				cp_times_old[cpu * CPUSTATES + i];
163 			}
164 			if (total == 0)
165 				continue;
166 			*load += 100 - (cp_times[cpu * CPUSTATES + CP_IDLE] -
167 			    cp_times_old[cpu * CPUSTATES + CP_IDLE]) * 100 / total;
168 		}
169 	}
170 
171 	memcpy(cp_times_old, cp_times, cp_times_len);
172 
173 	return (0);
174 }
175 
176 static int
177 read_freqs(int *numfreqs, int **freqs, int **power)
178 {
179 	char *freqstr, *p, *q;
180 	int i;
181 	size_t len = 0;
182 
183 	if (sysctl(levels_mib, 4, NULL, &len, NULL, 0))
184 		return (-1);
185 	if ((freqstr = malloc(len)) == NULL)
186 		return (-1);
187 	if (sysctl(levels_mib, 4, freqstr, &len, NULL, 0))
188 		return (-1);
189 
190 	*numfreqs = 1;
191 	for (p = freqstr; *p != '\0'; p++)
192 		if (*p == ' ')
193 			(*numfreqs)++;
194 
195 	if ((*freqs = malloc(*numfreqs * sizeof(int))) == NULL) {
196 		free(freqstr);
197 		return (-1);
198 	}
199 	if ((*power = malloc(*numfreqs * sizeof(int))) == NULL) {
200 		free(freqstr);
201 		free(*freqs);
202 		return (-1);
203 	}
204 	for (i = 0, p = freqstr; i < *numfreqs; i++) {
205 		q = strchr(p, ' ');
206 		if (q != NULL)
207 			*q = '\0';
208 		if (sscanf(p, "%d/%d", &(*freqs)[i], &(*power)[i]) != 2) {
209 			free(freqstr);
210 			free(*freqs);
211 			free(*power);
212 			return (-1);
213 		}
214 		p = q + 1;
215 	}
216 
217 	free(freqstr);
218 	return (0);
219 }
220 
221 static int
222 get_freq(void)
223 {
224 	size_t len;
225 	int curfreq;
226 
227 	len = sizeof(curfreq);
228 	if (sysctl(freq_mib, 4, &curfreq, &len, NULL, 0) != 0) {
229 		if (vflag)
230 			warn("error reading current CPU frequency");
231 		curfreq = 0;
232 	}
233 	return (curfreq);
234 }
235 
236 static int
237 set_freq(int freq)
238 {
239 
240 	if (sysctl(freq_mib, 4, NULL, NULL, &freq, sizeof(freq))) {
241 		if (errno != EPERM)
242 			return (-1);
243 	}
244 
245 	return (0);
246 }
247 
248 static int
249 get_freq_id(int freq, int *freqs, int numfreqs)
250 {
251 	int i = 1;
252 
253 	while (i < numfreqs) {
254 		if (freqs[i] < freq)
255 			break;
256 		i++;
257 	}
258 	return (i - 1);
259 }
260 
261 /*
262  * Try to use ACPI to find the AC line status.  If this fails, fall back
263  * to APM.  If nothing succeeds, we'll just run in default mode.
264  */
265 static void
266 acline_init(void)
267 {
268 	acline_mib_len = 4;
269 
270 	if (sysctlnametomib(ACPIAC, acline_mib, &acline_mib_len) == 0) {
271 		acline_mode = ac_sysctl;
272 		if (vflag)
273 			warnx("using sysctl for AC line status");
274 #if __powerpc__
275 	} else if (sysctlnametomib(PMUAC, acline_mib, &acline_mib_len) == 0) {
276 		acline_mode = ac_sysctl;
277 		if (vflag)
278 			warnx("using sysctl for AC line status");
279 #endif
280 #ifdef USE_APM
281 	} else if ((apm_fd = open(APMDEV, O_RDONLY)) >= 0) {
282 		if (vflag)
283 			warnx("using APM for AC line status");
284 		acline_mode = ac_apm;
285 #endif
286 	} else {
287 		warnx("unable to determine AC line status");
288 		acline_mode = ac_none;
289 	}
290 }
291 
292 static void
293 acline_read(void)
294 {
295 	if (acline_mode == ac_acpi_devd) {
296 		char buf[DEVCTL_MAXBUF], *ptr;
297 		ssize_t rlen;
298 		int notify;
299 
300 		rlen = read(devd_pipe, buf, sizeof(buf));
301 		if (rlen == 0 || (rlen < 0 && errno != EWOULDBLOCK)) {
302 			if (vflag)
303 				warnx("lost devd connection, switching to sysctl");
304 			devd_close();
305 			acline_mode = ac_sysctl;
306 			/* FALLTHROUGH */
307 		}
308 		if (rlen > 0 &&
309 		    (ptr = strstr(buf, "system=ACPI")) != NULL &&
310 		    (ptr = strstr(ptr, "subsystem=ACAD")) != NULL &&
311 		    (ptr = strstr(ptr, "notify=")) != NULL &&
312 		    sscanf(ptr, "notify=%x", &notify) == 1)
313 			acline_status = (notify ? SRC_AC : SRC_BATTERY);
314 	}
315 	if (acline_mode == ac_sysctl) {
316 		int acline;
317 		size_t len;
318 
319 		len = sizeof(acline);
320 		if (sysctl(acline_mib, acline_mib_len, &acline, &len,
321 		    NULL, 0) == 0)
322 			acline_status = (acline ? SRC_AC : SRC_BATTERY);
323 		else
324 			acline_status = SRC_UNKNOWN;
325 	}
326 #ifdef USE_APM
327 	if (acline_mode == ac_apm) {
328 		struct apm_info info;
329 
330 		if (ioctl(apm_fd, APMIO_GETINFO, &info) == 0) {
331 			acline_status = (info.ai_acline ? SRC_AC : SRC_BATTERY);
332 		} else {
333 			close(apm_fd);
334 			apm_fd = -1;
335 			acline_mode = ac_none;
336 			acline_status = SRC_UNKNOWN;
337 		}
338 	}
339 #endif
340 	/* try to (re)connect to devd */
341 	if (acline_mode == ac_sysctl) {
342 		struct timeval now;
343 
344 		gettimeofday(&now, NULL);
345 		if (now.tv_sec > tried_devd.tv_sec + DEVD_RETRY_INTERVAL) {
346 			if (devd_init() >= 0) {
347 				if (vflag)
348 					warnx("using devd for AC line status");
349 				acline_mode = ac_acpi_devd;
350 			}
351 			tried_devd = now;
352 		}
353 	}
354 }
355 
356 static int
357 devd_init(void)
358 {
359 	struct sockaddr_un devd_addr;
360 
361 	bzero(&devd_addr, sizeof(devd_addr));
362 	if ((devd_pipe = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
363 		if (vflag)
364 			warn("%s(): socket()", __func__);
365 		return (-1);
366 	}
367 
368 	devd_addr.sun_family = PF_LOCAL;
369 	strlcpy(devd_addr.sun_path, DEVDPIPE, sizeof(devd_addr.sun_path));
370 	if (connect(devd_pipe, (struct sockaddr *)&devd_addr,
371 	    sizeof(devd_addr)) == -1) {
372 		if (vflag)
373 			warn("%s(): connect()", __func__);
374 		close(devd_pipe);
375 		devd_pipe = -1;
376 		return (-1);
377 	}
378 
379 	if (fcntl(devd_pipe, F_SETFL, O_NONBLOCK) == -1) {
380 		if (vflag)
381 			warn("%s(): fcntl()", __func__);
382 		close(devd_pipe);
383 		return (-1);
384 	}
385 
386 	return (devd_pipe);
387 }
388 
389 static void
390 devd_close(void)
391 {
392 
393 	close(devd_pipe);
394 	devd_pipe = -1;
395 }
396 
397 static void
398 parse_mode(char *arg, int *mode, int ch)
399 {
400 
401 	if (strcmp(arg, "minimum") == 0 || strcmp(arg, "min") == 0)
402 		*mode = MODE_MIN;
403 	else if (strcmp(arg, "maximum") == 0 || strcmp(arg, "max") == 0)
404 		*mode = MODE_MAX;
405 	else if (strcmp(arg, "adaptive") == 0 || strcmp(arg, "adp") == 0)
406 		*mode = MODE_ADAPTIVE;
407 	else if (strcmp(arg, "hiadaptive") == 0 || strcmp(arg, "hadp") == 0)
408 		*mode = MODE_HIADAPTIVE;
409 	else
410 		errx(1, "bad option: -%c %s", (char)ch, optarg);
411 }
412 
413 static void
414 handle_sigs(int __unused sig)
415 {
416 
417 	exit_requested = 1;
418 }
419 
420 static void
421 usage(void)
422 {
423 
424 	fprintf(stderr,
425 "usage: powerd [-v] [-a mode] [-b mode] [-i %%] [-n mode] [-p ival] [-r %%] [-P pidfile]\n");
426 	exit(1);
427 }
428 
429 int
430 main(int argc, char * argv[])
431 {
432 	struct timeval timeout;
433 	fd_set fdset;
434 	int nfds;
435 	struct pidfh *pfh = NULL;
436 	const char *pidfile = NULL;
437 	int freq, curfreq, initfreq, *freqs, i, j, *mwatts, numfreqs, load;
438 	int ch, mode, mode_ac, mode_battery, mode_none, idle, to;
439 	uint64_t mjoules_used;
440 	size_t len;
441 
442 	/* Default mode for all AC states is adaptive. */
443 	mode_ac = mode_none = MODE_HIADAPTIVE;
444 	mode_battery = MODE_ADAPTIVE;
445 	cpu_running_mark = DEFAULT_ACTIVE_PERCENT;
446 	cpu_idle_mark = DEFAULT_IDLE_PERCENT;
447 	poll_ival = DEFAULT_POLL_INTERVAL;
448 	mjoules_used = 0;
449 	vflag = 0;
450 
451 	/* User must be root to control frequencies. */
452 	if (geteuid() != 0)
453 		errx(1, "must be root to run");
454 
455 	while ((ch = getopt(argc, argv, "a:b:i:n:p:P:r:v")) != -1)
456 		switch (ch) {
457 		case 'a':
458 			parse_mode(optarg, &mode_ac, ch);
459 			break;
460 		case 'b':
461 			parse_mode(optarg, &mode_battery, ch);
462 			break;
463 		case 'i':
464 			cpu_idle_mark = atoi(optarg);
465 			if (cpu_idle_mark < 0 || cpu_idle_mark > 100) {
466 				warnx("%d is not a valid percent",
467 				    cpu_idle_mark);
468 				usage();
469 			}
470 			break;
471 		case 'n':
472 			parse_mode(optarg, &mode_none, ch);
473 			break;
474 		case 'p':
475 			poll_ival = atoi(optarg);
476 			if (poll_ival < 5) {
477 				warnx("poll interval is in units of ms");
478 				usage();
479 			}
480 			break;
481 		case 'P':
482 			pidfile = optarg;
483 			break;
484 		case 'r':
485 			cpu_running_mark = atoi(optarg);
486 			if (cpu_running_mark <= 0 || cpu_running_mark > 100) {
487 				warnx("%d is not a valid percent",
488 				    cpu_running_mark);
489 				usage();
490 			}
491 			break;
492 		case 'v':
493 			vflag = 1;
494 			break;
495 		default:
496 			usage();
497 		}
498 
499 	mode = mode_none;
500 
501 	/* Poll interval is in units of ms. */
502 	poll_ival *= 1000;
503 
504 	/* Look up various sysctl MIBs. */
505 	len = 2;
506 	if (sysctlnametomib("kern.cp_times", cp_times_mib, &len))
507 		err(1, "lookup kern.cp_times");
508 	len = 4;
509 	if (sysctlnametomib("dev.cpu.0.freq", freq_mib, &len))
510 		err(1, "lookup freq");
511 	len = 4;
512 	if (sysctlnametomib("dev.cpu.0.freq_levels", levels_mib, &len))
513 		err(1, "lookup freq_levels");
514 
515 	/* Check if we can read the load and supported freqs. */
516 	if (read_usage_times(NULL))
517 		err(1, "read_usage_times");
518 	if (read_freqs(&numfreqs, &freqs, &mwatts))
519 		err(1, "error reading supported CPU frequencies");
520 
521 	/* Run in the background unless in verbose mode. */
522 	if (!vflag) {
523 		pid_t otherpid;
524 
525 		pfh = pidfile_open(pidfile, 0600, &otherpid);
526 		if (pfh == NULL) {
527 			if (errno == EEXIST) {
528 				errx(1, "powerd already running, pid: %d",
529 				    otherpid);
530 			}
531 			warn("cannot open pid file");
532 		}
533 		if (daemon(0, 0) != 0) {
534 			warn("cannot enter daemon mode, exiting");
535 			pidfile_remove(pfh);
536 			exit(EXIT_FAILURE);
537 
538 		}
539 		pidfile_write(pfh);
540 	}
541 
542 	/* Decide whether to use ACPI or APM to read the AC line status. */
543 	acline_init();
544 
545 	/*
546 	 * Exit cleanly on signals.
547 	 */
548 	signal(SIGINT, handle_sigs);
549 	signal(SIGTERM, handle_sigs);
550 
551 	freq = initfreq = curfreq = get_freq();
552 	i = get_freq_id(curfreq, freqs, numfreqs);
553 	if (freq < 1)
554 		freq = 1;
555 	idle = 0;
556 	/* Main loop. */
557 	for (;;) {
558 		FD_ZERO(&fdset);
559 		if (devd_pipe >= 0) {
560 			FD_SET(devd_pipe, &fdset);
561 			nfds = devd_pipe + 1;
562 		} else {
563 			nfds = 0;
564 		}
565 		if (mode == MODE_HIADAPTIVE || idle < 120)
566 			to = poll_ival;
567 		else if (idle < 360)
568 			to = poll_ival * 2;
569 		else
570 			to = poll_ival * 4;
571 		timeout.tv_sec = to / 1000000;
572 		timeout.tv_usec = to % 1000000;
573 		select(nfds, &fdset, NULL, &fdset, &timeout);
574 
575 		/* If the user requested we quit, print some statistics. */
576 		if (exit_requested) {
577 			if (vflag && mjoules_used != 0)
578 				printf("total joules used: %u.%03u\n",
579 				    (u_int)(mjoules_used / 1000),
580 				    (int)mjoules_used % 1000);
581 			break;
582 		}
583 
584 		/* Read the current AC status and record the mode. */
585 		acline_read();
586 		switch (acline_status) {
587 		case SRC_AC:
588 			mode = mode_ac;
589 			break;
590 		case SRC_BATTERY:
591 			mode = mode_battery;
592 			break;
593 		case SRC_UNKNOWN:
594 			mode = mode_none;
595 			break;
596 		default:
597 			errx(1, "invalid AC line status %d", acline_status);
598 		}
599 
600 		/* Read the current frequency. */
601 		if (idle % 32 == 0) {
602 			if ((curfreq = get_freq()) == 0)
603 				continue;
604 			i = get_freq_id(curfreq, freqs, numfreqs);
605 		}
606 		idle++;
607 		if (vflag) {
608 			/* Keep a sum of all power actually used. */
609 			if (mwatts[i] != -1)
610 				mjoules_used +=
611 				    (mwatts[i] * (poll_ival / 1000)) / 1000;
612 		}
613 
614 		/* Always switch to the lowest frequency in min mode. */
615 		if (mode == MODE_MIN) {
616 			freq = freqs[numfreqs - 1];
617 			if (curfreq != freq) {
618 				if (vflag) {
619 					printf("now operating on %s power; "
620 					    "changing frequency to %d MHz\n",
621 					    modes[acline_status], freq);
622 				}
623 				idle = 0;
624 				if (set_freq(freq) != 0) {
625 					warn("error setting CPU freq %d",
626 					    freq);
627 					continue;
628 				}
629 			}
630 			continue;
631 		}
632 
633 		/* Always switch to the highest frequency in max mode. */
634 		if (mode == MODE_MAX) {
635 			freq = freqs[0];
636 			if (curfreq != freq) {
637 				if (vflag) {
638 					printf("now operating on %s power; "
639 					    "changing frequency to %d MHz\n",
640 					    modes[acline_status], freq);
641 				}
642 				idle = 0;
643 				if (set_freq(freq) != 0) {
644 					warn("error setting CPU freq %d",
645 				    	    freq);
646 					continue;
647 				}
648 			}
649 			continue;
650 		}
651 
652 		/* Adaptive mode; get the current CPU usage times. */
653 		if (read_usage_times(&load)) {
654 			if (vflag)
655 				warn("read_usage_times() failed");
656 			continue;
657 		}
658 
659 		if (mode == MODE_ADAPTIVE) {
660 			if (load > cpu_running_mark) {
661 				if (load > 95 || load > cpu_running_mark * 2)
662 					freq *= 2;
663 				else
664 					freq = freq * load / cpu_running_mark;
665 				if (freq > freqs[0])
666 					freq = freqs[0];
667 			} else if (load < cpu_idle_mark &&
668 			    curfreq * load < freqs[get_freq_id(
669 			    freq * 7 / 8, freqs, numfreqs)] *
670 			    cpu_running_mark) {
671 				freq = freq * 7 / 8;
672 				if (freq < freqs[numfreqs - 1])
673 					freq = freqs[numfreqs - 1];
674 			}
675 		} else { /* MODE_HIADAPTIVE */
676 			if (load > cpu_running_mark / 2) {
677 				if (load > 95 || load > cpu_running_mark)
678 					freq *= 4;
679 				else
680 					freq = freq * load * 2 / cpu_running_mark;
681 				if (freq > freqs[0] * 2)
682 					freq = freqs[0] * 2;
683 			} else if (load < cpu_idle_mark / 2 &&
684 			    curfreq * load < freqs[get_freq_id(
685 			    freq * 31 / 32, freqs, numfreqs)] *
686 			    cpu_running_mark / 2) {
687 				freq = freq * 31 / 32;
688 				if (freq < freqs[numfreqs - 1])
689 					freq = freqs[numfreqs - 1];
690 			}
691 		}
692 		if (vflag) {
693 		    printf("load %3d%%, current freq %4d MHz (%2d), wanted freq %4d MHz\n",
694 			load, curfreq, i, freq);
695 		}
696 		j = get_freq_id(freq, freqs, numfreqs);
697 		if (i != j) {
698 			if (vflag) {
699 				printf("changing clock"
700 				    " speed from %d MHz to %d MHz\n",
701 				    freqs[i], freqs[j]);
702 			}
703 			idle = 0;
704 			if (set_freq(freqs[j]))
705 				warn("error setting CPU frequency %d",
706 				    freqs[j]);
707 		}
708 	}
709 	if (set_freq(initfreq))
710 		warn("error setting CPU frequency %d", initfreq);
711 	free(freqs);
712 	free(mwatts);
713 	devd_close();
714 	if (!vflag)
715 		pidfile_remove(pfh);
716 
717 	exit(0);
718 }
719