diff -Nru tp-thermstat-0.01.01~o/debian/changelog tp-thermstat-0.01.02~o/debian/changelog --- tp-thermstat-0.01.01~o/debian/changelog 2012-03-12 12:15:56.000000000 +0000 +++ tp-thermstat-0.01.02~o/debian/changelog 2012-03-14 11:11:22.000000000 +0000 @@ -1,3 +1,10 @@ +tp-thermstat (0.01.02~o) oneiric; urgency=low + + * Add CPU freq, re-work output + * Fix minor typos in app name + + -- Colin King Wed, 14 Mar 2012 11:08:00 +0000 + tp-thermstat (0.01.01~o) oneiric; urgency=low * Add debian related files diff -Nru tp-thermstat-0.01.01~o/tp-thermstat.c tp-thermstat-0.01.02~o/tp-thermstat.c --- tp-thermstat-0.01.01~o/tp-thermstat.c 2012-03-12 12:15:56.000000000 +0000 +++ tp-thermstat-0.01.02~o/tp-thermstat.c 2012-03-14 11:11:22.000000000 +0000 @@ -29,10 +29,12 @@ #include #include -#define APP_NAME "cpustat" -#define TABLE_SIZE (32999) /* Should be a prime */ -#define OPT_QUIET (0x00000001) -#define OPT_IGNORE_SELF (0x00000002) +#define APP_NAME "tp-thermstat" +#define TABLE_SIZE (32999) /* Should be a prime */ + +#define OPT_QUIET (0x00000001) +#define OPT_IGNORE_SELF (0x00000002) +#define OPT_PER_CPU_TICKS (0x00000004) typedef struct link { void *data; @@ -41,7 +43,7 @@ typedef struct { link_t *head; - link_t *tail; + link_t *tail ; size_t length; } list_t; @@ -62,13 +64,15 @@ cpu_info_t *info; /* CPU info */ struct cpu_stat *next; /* Next cpu stat in hash table */ struct cpu_stat *sorted_usage_next;/* Next CPU stat in CPU usage sorted list */ + double percent; /* Percentage of total CPU ticks */ } cpu_stat_t; static list_t cpu_info_list; /* cache list of cpu_info */ -static volatile bool stop_cpustat = false; /* set by sighandler */ +static volatile bool stop_tp_thermstat = false; /* set by sighandler */ static unsigned long opt_threshold; /* ignore samples with CPU usage deltas less than this */ static unsigned int opt_flags; /* option flags */ static unsigned long clock_ticks; +static unsigned long num_cpus; static inline void list_init(list_t *list) { @@ -120,7 +124,7 @@ */ static void handle_sigint(int dummy) { - stop_cpustat = true; + stop_tp_thermstat = true; } @@ -328,8 +332,14 @@ int j = 0; cpu_stat_t *sorted = NULL; + cpu_stat_t *cpu_stat = NULL; + + unsigned long nr_ticks; - unsigned long nr_ticks = sysconf(_SC_NPROCESSORS_CONF) * clock_ticks; + if (opt_flags & OPT_PER_CPU_TICKS) + nr_ticks = clock_ticks; + else + nr_ticks = num_cpus * clock_ticks; for (i=0; isorted_usage_next) { + double cpu_usage = + 100.0 * (double)cpu_stat->delta / + (double)(duration * nr_ticks); + cpu_stat->percent = cpu_usage; + if (cpu_usage > 0.0) + total += cpu_usage; + } + printf("Total %%CPU:\t%.2f\n\n", total); - while (sorted) { + printf(" %%CPU PID Task\n"); + for (cpu_stat = sorted; cpu_stat; cpu_stat = cpu_stat->sorted_usage_next) { if ((n_lines == -1) || (j < n_lines)) { j++; - double cpu_usage = - 100.0 * (double)sorted->delta / - (double)(duration * nr_ticks); - if (cpu_usage > 0.0) { + if (cpu_stat->percent > 0.0) { printf("%5.2f %5d %-15s\n", - cpu_usage, sorted->info->pid, sorted->info->comm); + cpu_stat->percent, + cpu_stat->info->pid, + cpu_stat->info->comm); } } - sorted = sorted->sorted_usage_next; } printf("\n"); } @@ -440,6 +459,25 @@ return temp; } +static void get_cpu_scaling_freq(int *cpu_freqs, unsigned long cpus) +{ + unsigned long i; + + for (i=0; i < cpus; i++) { + FILE *fp; + char path[PATH_MAX]; + + snprintf(path, sizeof(path), + "/sys/devices/system/cpu/cpu%lu/cpufreq/scaling_cur_freq", i); + if ((fp = fopen(path, "r")) == NULL) { + cpu_freqs[i] = -1; + } else { + fscanf(fp, "%d", &cpu_freqs[i]); + fclose(fp); + } + } +} + static int thinkpad_get_fan_speed(void) { FILE *fp; @@ -472,12 +510,13 @@ */ void show_usage(void) { - printf("Usage: %s [-q] [-r csv_file] [-n task_count] [duration] [count]\n", APP_NAME); + printf("Usage: %s [-p] [-q] [-r csv_file] [-n task_count] [duration] [count]\n", APP_NAME); printf("\t-h help\n"); printf("\t-i ignore %s in the statistics\n", APP_NAME); printf("\t-n specifies number of tasks to display\n"); printf("\t-q run quietly, useful with option -r\n"); printf("\t-t specifies an task tick count threshold where samples less than this are ignored.\n"); + printf("\t-p calculate from per CPU ticks rather than total CPU ticks\n"); } int main(int argc, char **argv) @@ -489,16 +528,26 @@ unsigned long whence = 0; bool forever = true; struct timeval tv1, tv2; + int *cpu_freqs; list_init(&cpu_info_list); clock_ticks = sysconf(_SC_CLK_TCK); + num_cpus = sysconf(_SC_NPROCESSORS_CONF); + + if ((cpu_freqs = calloc(num_cpus, sizeof(int))) == NULL) { + fprintf(stderr, "Out of memory allocating cpu_freqs\n"); + exit(EXIT_FAILURE); + } for (;;) { - int c = getopt(argc, argv, "hin:qt:"); + int c = getopt(argc, argv, "hin:qt:p"); if (c == -1) break; switch (c) { + case 'p': + opt_flags |= OPT_PER_CPU_TICKS; + break; case 'h': show_usage(); exit(EXIT_SUCCESS); @@ -558,24 +607,14 @@ gettimeofday(&tv1, NULL); get_cpustats(cpu_stats_old); - while (!stop_cpustat && (forever || count--)) { + while (!stop_tp_thermstat && (forever || count--)) { suseconds_t usec; int temp, speed; + int i; temp = thinkpad_get_temp(); speed = thinkpad_get_fan_speed(); - - printf("Temperature: "); - if (temp == -1) - printf("Uknown, "); - else - printf("%.2f Degrees C, ", (float)temp/1000.0); - - printf("Fan Speed: "); - if (speed == -1) - printf("Uknown\n"); - else - printf("%d RPM\n", speed); + get_cpu_scaling_freq(cpu_freqs, num_cpus); gettimeofday(&tv2, NULL); usec = ((tv1.tv_sec + whence + duration - tv2.tv_sec) * 1000000) + @@ -586,6 +625,26 @@ select(0, NULL, NULL, NULL, &tv2); get_cpustats(cpu_stats_new); + printf("Temperature:\t"); + if (temp == -1) + printf("Uknown\n"); + else + printf("%.2f Degrees C\n", (float)temp/1000.0); + + printf("Fan Speed:\t"); + if (speed == -1) + printf("Uknown\n"); + else + printf("%d RPM\n", speed); + + printf("CPU freq (GHz):\t"); + for (i = 0; i < num_cpus; i++) { + if (cpu_freqs[i] > -1) + printf("%s%f", i == 0 ? "" : " ", + (double)cpu_freqs[i] / 1000000.0); + } + printf("\n"); + cpu_stat_diff(duration, n_lines, whence, cpu_stats_old, cpu_stats_new); cpu_stat_free_contents(cpu_stats_old); @@ -602,6 +661,7 @@ free(cpu_stats_old); free(cpu_stats_new); cpu_info_list_free(); + free(cpu_freqs); exit(EXIT_SUCCESS); }