Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 2 Feb 2004 22:16:23 +0100
From:      Robert Blacquiere <freebsd-current@guldan.demon.nl>
To:        freebsd-current@freebsd.org
Subject:   small sample program for speedstep and temperatue
Message-ID:  <20040202211623.GA28219@bombur.guldan.demon.nl>

next in thread | raw e-mail | index | archive | help

--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 <stdio.h>
#include <unistd.h>


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--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20040202211623.GA28219>