/************************************************************** * * * 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); }