From owner-freebsd-current@FreeBSD.ORG Mon Feb 2 13:16:56 2004 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id BD5A816A4CE for ; Mon, 2 Feb 2004 13:16:56 -0800 (PST) Received: from guldan.demon.nl (cust.13.38.adsl.cistron.nl [62.216.13.38]) by mx1.FreeBSD.org (Postfix) with ESMTP id 8622943D1F for ; Mon, 2 Feb 2004 13:16:54 -0800 (PST) (envelope-from robert@guldan.demon.nl) Received: from bombur.guldan.demon.nl ([192.168.201.3] helo=localhost) by guldan.demon.nl with esmtp (Exim 4.24; FreeBSD) id 1AnlT1-0007sr-Qm; Mon, 02 Feb 2004 22:18:51 +0100 Date: Mon, 2 Feb 2004 22:16:23 +0100 From: Robert Blacquiere To: freebsd-current@freebsd.org Message-ID: <20040202211623.GA28219@bombur.guldan.demon.nl> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="sdtB3X0nJg68CQEu" Content-Disposition: inline User-Agent: Mutt/1.4.1i X-Disclaimer: running FreeBSD X-Spam-Score: 0.0 (/) Subject: small sample program for speedstep and temperatue X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 02 Feb 2004 21:16:56 -0000 --sdtB3X0nJg68CQEu Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi, I've been working on a small programm for controlling cpu speed and temperature. I noticed my laptop runs warm/hot too hot accourding to me. So made first a perl script but did not like it so tried a c programm to do the same. Oh don't make funny remarks on the code just starting programming c. Please feel free to make comments and maybe help build a better one for controlling temperature and speedstep. Robert -- Microsoft: Where do you want to go today? Linux: Where do you want to go tomorrow? FreeBSD: Are you guys coming or what? OpenBSD: Hey guys you left some holes out there! --sdtB3X0nJg68CQEu Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="cpumon.c" /************************************************************** * * * cpumon a proram to monitor cpu temperature and speedstep * * features. It works as a daemon in de background reading * * with sysctl calls tz0 and cpu parameters * * * * This program is as is and follows the bsd license * * initial programming done by: robert@blacquiere.nl * * * ***************************************************************/ #define KELVIN_TO_CELSIUS(t) ((t-2732+5)/10) #include #include void usage (char **argv) { fprintf (stdout, "\n" "usage:\t%s [-h] [-a stepping] [-b stepping] [-c temp]\n" "\t\t[-w temp] [-s slowing]\n" "-a: speedstep setting ac online\n" "-b: speedstep setting battery powered\n" "-c: low temperature setting Celius\n" "-w: hot temperature setting Celius\n" "-s: slow increase decrease stepping\n" "-h: this help message\n", argv[0]); exit(0); } int main(int argc, char **argv) { int ret; int temp; size_t len_temp = sizeof(temp); int throttle; size_t len_throttle = sizeof(throttle); int mthrottle; size_t len_mthrottle = sizeof(mthrottle); int acline; size_t len_acline = sizeof(acline); int speedstep ; extern char *optarg; extern int optind; int ch; int battery_powered = 2; int ac_powered = 4; int low_temp = 60; int hot_temp = 70; int slow = 5; int slowing = 0; while ((ch = getopt(argc, argv, "hb:a:w:c:s:")) != -1 ) switch (ch) { case 'b': battery_powered = atoi(optarg); break; case 'a': ac_powered = atoi(optarg); break; case 'c': low_temp = atoi(optarg); break; case 'w': hot_temp = atoi(optarg); break; case 's': slow = atoi(optarg); break; case 'h': usage(argv); break; } argc -= optind; argv += optind; printf("Using values: -b %d\t-a %d\t-w %d\t-c %d\t-s %d\n", battery_powered, ac_powered, hot_temp, low_temp, slow); while (1) { ret=sysctlbyname("hw.acpi.thermal.tz0.temperature",&temp,&len_temp,NULL,0); if (ret != 0 ) printf("failed temperature\n"); ret=sysctlbyname("hw.acpi.acline",&acline,&len_acline,NULL,0); if (ret != 0 ) printf("failed acline\n"); ret=sysctlbyname("hw.acpi.cpu.throttle_state",&throttle,&len_throttle,NULL,0); if (ret != 0 ) printf("failed throttle state\n"); ret=sysctlbyname("hw.acpi.cpu.throttle_max",&mthrottle,&len_mthrottle,NULL,0); if (ret != 0 ) printf("failed throttle max\n"); printf("cpu: %d C\t",KELVIN_TO_CELSIUS(temp)); speedstep = ( throttle * 100 ) / mthrottle ; printf("CPU speed: %d %%\n", speedstep); if ( acline == 0 && throttle > battery_powered ) { // printf("acline = %d\tthrottle = %d\tbattery_powered = %d\n", acline, throttle, battery_powered); // printf("AC offline and cpu runs on higher stepping\n"); throttle = battery_powered; size_t len_throttle = sizeof(throttle); ret=sysctlbyname("hw.acpi.cpu.throttle_state",NULL,NULL,&throttle,&len_throttle); if (ret != 0 ) printf("set throttle_state failed\n"); } if (acline == 1 && throttle < ac_powered ) { // printf("acline = %d\tthrottle = %d\tac_powered = %d\n", acline, throttle, ac_powered); // printf("AC online and cpu runs on lower stepping\n"); throttle = ac_powered; size_t len_throttle = sizeof(throttle); ret=sysctlbyname("hw.acpi.cpu.throttle_state",NULL,NULL,&throttle,&len_throttle); if (ret != 0 ) printf("set throttle_state failed\n"); } if ( KELVIN_TO_CELSIUS(temp) > hot_temp ) { // printf("temp = %d\thot_temp = %d\n", temp, hot_temp); if ( throttle > 1 && slowing == 0 ) { // printf("It seems i'me hotter as usual trying to cool my self\n"); --throttle; slowing = slow; size_t len_throttle = sizeof(throttle); ret=sysctlbyname("hw.acpi.cpu.throttle_state",NULL,NULL,&throttle,&len_throttle); if (ret != 0 ) printf("set throttle_state failed\n"); } if ( --slowing <=0 ) { slowing = 0; } } if ( KELVIN_TO_CELSIUS(temp) < low_temp ) { if ( acline == 1 && throttle < ac_powered && slowing == 0 ) { // printf("It seems i've cooled my self down enough speeding up\n"); ++throttle; slowing = slow; size_t len_throttle = sizeof(throttle); ret=sysctlbyname("hw.acpi.cpu.throttle_state",NULL,0,&throttle,&len_throttle); if (ret != 0 ) printf("set throttle_state failed\n"); } if ( acline == 0 && throttle < battery_powered && slowing == 0 ) { // printf("It seems i've cooled my self down enough speeding up\n"); ++throttle; slowing = slow; size_t len_throttle = sizeof(throttle); ret=sysctlbyname("hw.acpi.cpu.throttle_state",NULL,0,&throttle,&len_throttle); if (ret != 0 ) printf("set throttle_state failed\n"); } if (--slowing <= 0) { slowing = 0; } } sleep(5); } return(0); } --sdtB3X0nJg68CQEu--