/***************************************************************************** * * cpu_freq_mon.c 2014.05.25 Smythies * Monitor the max and min CPU Frequnecies. * This change might be reverted later. * * cpu_freq_mon.c 2014.05.25 Smythies * Eat the keyboard character. Typically I depress the space * bar, and then end up with a space after the program, which * then prevetns the next command from being entered in * .bash_history, and I want it entered. * * cpu_freq_mon.c 2013.11.07 Smythies * I keep making the mistake of doing ^C instead of any other * key to save the data and exit. * Add a signal handler for ^C so that the data is not lost. * * cpu_freq_mon.c 2013.06.18 Smythies * Monitor /sys/devices/system/cpu/cpu7/cpufreq/cpuinfo_cur_freq * Either change the permissions for that file or run this as * root. * Check that running this program doesn't influence the * frequencies (i.e. this program adds negligible load). * Rewind doesn't seem to work, close / open the file * *****************************************************************************/ #include #include #include //#include //#include //#include //#include #include //#include //#include #include #define MAX_LENGTH 2000 /* maximum line length */ #define MAX_SAMPLES 4000000 /* maximum number of frequency samples */ #define SAMPLE_DELAY 100000 /* time to sleep between samples in microseconds */ long frequency[MAX_SAMPLES]; long max_freq, min_freq; int ctrl_c_flag; void sig_handler(int signo){ /* do not abort on ^C */ if (signo == SIGINT){ printf(" --- SIGINT (^C) detected. Terminate gracefully, saving the sample data...\n"); ctrl_c_flag = 1; } /* endif */ } /* endprocedure */ void main(int argc, char **argv){ char *outfile; FILE *inf, *outf; long sample_number, i; int err, print_flag; char buffer[1]; fd_set set; struct timeval tv; struct termios t; memset(&tv, 0, sizeof(tv)); tcgetattr(0, &t); t.c_lflag &= ~ICANON; /*cfmakeraw(&t);*/ tcsetattr(0, TCSANOW, &t); switch(argc){ case 2: outfile = argv[1]; break; default: printf("cpu_freq_mon outfile\n"); printf(" Monitors CPU 7 frequency and stores data in outfile.\n"); exit(-1); } /* endcase */ if((outf = fopen(outfile, "wt")) == NULL){ printf("Unable to open output file '%s'\n", outfile); exit(-1); } /* endif */ if (signal(SIGINT, sig_handler) == SIG_ERR){ /* register the signal handler */ printf("\ncan't catch SIGINT\n"); } /* endif */ ctrl_c_flag = 0; /* there has not been a ^C */ for (sample_number =0; sample_number < MAX_SAMPLES; sample_number++){ /* make sure memory allocated and available */ frequency[sample_number] = sample_number; } /* endfor */ sample_number = 0; max_freq = 0; min_freq = 9223372036854775807L; while(1){ /* */ FD_ZERO(&set); FD_SET(0, &set); select(1, &set, 0, 0, &tv); if (FD_ISSET(0, &set)){ read(0, buffer, 1); /* Eat the keyboard stroke */ break; /* exit on keyboard hit */ } /* endif */ if (ctrl_c_flag) break; /* exit on ^C */ if((inf = fopen("/sys/devices/system/cpu/cpu7/cpufreq/cpuinfo_cur_freq", "rt")) == NULL){ printf("Unable to open input file '/sys/devices/system/cpu/cpu7/cpufreq/cpuinfo_cur_freq'\n"); exit(-1); } /* endif */ if((err = fscanf(inf,"%ld", &frequency[sample_number])) == -1){ printf("Something is wrong. Error return code reading frequency sample.\n"); break; } /* endif */ print_flag = 0; if(max_freq < frequency[sample_number]){ max_freq = frequency[sample_number]; print_flag = 1; } /* endif */ if(min_freq > frequency[sample_number]){ min_freq = frequency[sample_number]; print_flag = 1; } /* endif */ if(print_flag != 0){ printf("Max: %ld ; Min: %ld\n", max_freq, min_freq); } /* endif */ sample_number++; if(sample_number >= MAX_SAMPLES){ printf("Maximum samples taken...\n"); break; } /* endif */ fclose(inf); // if(err = fseek(inf, 0L, SEEK_SET) == -1){ // printf("Something is wrong. Error return code rewinding file.\n"); // break; // } /* endif */ usleep(SAMPLE_DELAY); } /* endwhile */ if(sample_number > 0){ for( i = 0; i < sample_number; i++){ fprintf(outf, "%ld %ld\n", i, frequency[i]); } /* endfor */ } /* endif */ fclose(outf); printf("End... Total Samples processed: %ld\n", sample_number); } /* endprogram */