From owner-freebsd-bugs@FreeBSD.ORG Fri Mar 26 17:00:10 2010 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 810F21065678 for ; Fri, 26 Mar 2010 17:00:10 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id 386608FC26 for ; Fri, 26 Mar 2010 17:00:10 +0000 (UTC) Received: from freefall.freebsd.org (localhost [127.0.0.1]) by freefall.freebsd.org (8.14.4/8.14.4) with ESMTP id o2QH09fu086447 for ; Fri, 26 Mar 2010 17:00:09 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.4/8.14.4/Submit) id o2QH09d2086446; Fri, 26 Mar 2010 17:00:09 GMT (envelope-from gnats) Resent-Date: Fri, 26 Mar 2010 17:00:09 GMT Resent-Message-Id: <201003261700.o2QH09d2086446@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Boris Kochergin Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 965261065674 for ; Fri, 26 Mar 2010 16:55:06 +0000 (UTC) (envelope-from nobody@FreeBSD.org) Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21]) by mx1.freebsd.org (Postfix) with ESMTP id 84D9D8FC18 for ; Fri, 26 Mar 2010 16:55:06 +0000 (UTC) Received: from www.freebsd.org (localhost [127.0.0.1]) by www.freebsd.org (8.14.3/8.14.3) with ESMTP id o2QGt6kL004586 for ; Fri, 26 Mar 2010 16:55:06 GMT (envelope-from nobody@www.freebsd.org) Received: (from nobody@localhost) by www.freebsd.org (8.14.3/8.14.3/Submit) id o2QGt6Ud004585; Fri, 26 Mar 2010 16:55:06 GMT (envelope-from nobody) Message-Id: <201003261655.o2QGt6Ud004585@www.freebsd.org> Date: Fri, 26 Mar 2010 16:55:06 GMT From: Boris Kochergin To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.1 Cc: Subject: bin/145063: [powerd] [patch] Add -m and -M (minimum and maximum frequency) options to powerd X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 26 Mar 2010 17:00:10 -0000 >Number: 145063 >Category: bin >Synopsis: [powerd] [patch] Add -m and -M (minimum and maximum frequency) options to powerd >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Fri Mar 26 17:00:09 UTC 2010 >Closed-Date: >Last-Modified: >Originator: Boris Kochergin >Release: 8.0-RELEASE-p2 >Organization: Polytechnic Institute of NYU >Environment: FreeBSD peer 8.0-RELEASE-p2 FreeBSD 8.0-RELEASE-p2 #2: Mon Feb 22 23:18:53 EST 2010 root@peer:/usr/obj/usr/src/sys/PEER i386 >Description: This patch implements the -m and -M (minimum and maximum frequency, respectively) options for powerd. It has been tested on 7.2, 8.x, and 9.x. >How-To-Repeat: >Fix: Patch attached with submission follows: --- powerd.c.orig 2010-03-25 15:48:09.000000000 -0400 +++ powerd.c 2010-03-26 12:31:17.000000000 -0400 @@ -84,7 +84,8 @@ #define DEVCTL_MAXBUF 1024 static int read_usage_times(int *load); -static int read_freqs(int *numfreqs, int **freqs, int **power); +static int read_freqs(int *numfreqs, int **freqs, int **power, + int minfreq, int maxfreq); static int set_freq(int freq); static void acline_init(void); static void acline_read(void); @@ -174,10 +175,10 @@ } static int -read_freqs(int *numfreqs, int **freqs, int **power) +read_freqs(int *numfreqs, int **freqs, int **power, int minfreq, int maxfreq) { char *freqstr, *p, *q; - int i; + int i, j; size_t len = 0; if (sysctl(levels_mib, 4, NULL, &len, NULL, 0)) @@ -201,19 +202,30 @@ free(*freqs); return (-1); } - for (i = 0, p = freqstr; i < *numfreqs; i++) { + for (i = 0, j = 0, p = freqstr; i < *numfreqs; i++) { q = strchr(p, ' '); if (q != NULL) *q = '\0'; - if (sscanf(p, "%d/%d", &(*freqs)[i], &(*power)[i]) != 2) { + if (sscanf(p, "%d/%d", &(*freqs)[j], &(*power)[i]) != 2) { free(freqstr); free(*freqs); free(*power); return (-1); } + if (((*freqs)[j] >= minfreq || minfreq == -1) && + ((*freqs)[j] <= maxfreq || maxfreq == -1)) + j++; p = q + 1; } + *numfreqs = j; + if ((*freqs = realloc(*freqs, *numfreqs * sizeof(int))) == NULL) { + free(freqstr); + free(*freqs); + free(*power); + return (-1); + } + free(freqstr); return (0); } @@ -422,7 +434,7 @@ { fprintf(stderr, -"usage: powerd [-v] [-a mode] [-b mode] [-i %%] [-n mode] [-p ival] [-r %%] [-P pidfile]\n"); +"usage: powerd [-v] [-a mode] [-b mode] [-i %%] [-m freq] [-M freq] [-n mode] [-p ival] [-r %%] [-P pidfile]\n"); exit(1); } @@ -435,6 +447,7 @@ struct pidfh *pfh = NULL; const char *pidfile = NULL; int freq, curfreq, initfreq, *freqs, i, j, *mwatts, numfreqs, load; + int minfreq = -1, maxfreq = -1; int ch, mode, mode_ac, mode_battery, mode_none; uint64_t mjoules_used; size_t len; @@ -452,7 +465,7 @@ if (geteuid() != 0) errx(1, "must be root to run"); - while ((ch = getopt(argc, argv, "a:b:i:n:p:P:r:v")) != -1) + while ((ch = getopt(argc, argv, "a:b:i:m:M:n:p:P:r:v")) != -1) switch (ch) { case 'a': parse_mode(optarg, &mode_ac, ch); @@ -468,6 +481,22 @@ usage(); } break; + case 'm': + minfreq = atoi(optarg); + if (minfreq < 0) { + warnx("%d is not a valid CPU frequency", + minfreq); + usage(); + } + break; + case 'M': + maxfreq = atoi(optarg); + if (maxfreq < 0) { + warnx("%d is not a valid CPU frequency", + maxfreq); + usage(); + } + break; case 'n': parse_mode(optarg, &mode_none, ch); break; @@ -515,8 +544,10 @@ /* Check if we can read the load and supported freqs. */ if (read_usage_times(NULL)) err(1, "read_usage_times"); - if (read_freqs(&numfreqs, &freqs, &mwatts)) + if (read_freqs(&numfreqs, &freqs, &mwatts, minfreq, maxfreq)) err(1, "error reading supported CPU frequencies"); + if (numfreqs == 0) + errx(1, "no CPU frequencies in user-specified range"); /* Run in the background unless in verbose mode. */ if (!vflag) { @@ -551,6 +582,49 @@ freq = initfreq = get_freq(); if (freq < 1) freq = 1; + + /* + * If we are in adaptive mode and the current frequency is outside the + * user-defined range, adjust it to be within the user-defined range. + */ + acline_read(); + if (acline_status > SRC_UNKNOWN) + errx(1, "invalid AC line status %d", acline_status); + if ((acline_status == SRC_AC && + (mode_ac == MODE_ADAPTIVE || mode_ac == MODE_HIADAPTIVE)) || + (acline_status == SRC_BATTERY && + (mode_battery == MODE_ADAPTIVE || mode_battery == MODE_HIADAPTIVE)) || + (acline_status == SRC_UNKNOWN && + (mode_none == MODE_ADAPTIVE || mode_none == MODE_HIADAPTIVE))) { + /* Read the current frequency. */ + len = sizeof(curfreq); + if (sysctl(freq_mib, 4, &curfreq, &len, NULL, 0) != 0) { + if (vflag) + warn("error reading current CPU frequency"); + } + if (curfreq < freqs[numfreqs - 1]) { + if (vflag) { + printf("CPU frequency is below user-defined " + "minimum; changing frequency to %d " + "MHz\n", freqs[numfreqs - 1]); + } + if (set_freq(freqs[numfreqs - 1]) != 0) { + warn("error setting CPU freq %d", + freqs[numfreqs - 1]); + } + } else if (curfreq > freqs[0]) { + if (vflag) { + printf("CPU frequency is above user-defined " + "maximum; changing frequency to %d " + "MHz\n", freqs[0]); + } + if (set_freq(freqs[0]) != 0) { + warn("error setting CPU freq %d", + freqs[0]); + } + } + } + /* Main loop. */ for (;;) { FD_ZERO(&fdset); --- powerd.8.orig 2010-03-26 12:37:50.000000000 -0400 +++ powerd.8 2010-03-26 12:41:06.000000000 -0400 @@ -35,6 +35,8 @@ .Op Fl a Ar mode .Op Fl b Ar mode .Op Fl i Ar percent +.Op Fl m Ar freq +.Op Fl M Ar freq .Op Fl n Ar mode .Op Fl p Ar ival .Op Fl P Ar pidfile @@ -79,6 +81,10 @@ Specifies the CPU load percent level when adaptive mode should begin to degrade performance to save power. The default is 50% or lower. +.It Fl m Ar freq +Specifies the minimum frequency to throttle down to. +.It Fl M Ar freq +Specifies the maximum frequency to throttle up to. .It Fl n Ar mode Selects the .Ar mode >Release-Note: >Audit-Trail: >Unformatted: