cpustat-0.01.25/0000775000175000017500000000000012607512420012005 5ustar kingkingcpustat-0.01.25/cpustat.c0000664000175000017500000012752712607511740013656 0ustar kingking/* * Copyright (C) 2011-2015 Canonical * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: Colin Ian King */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define APP_NAME "cpustat" #define TABLE_SIZE (2411) /* Should be a prime */ #define PID_HASH_SIZE (11113) /* Ideally a large prime */ #define OPT_QUIET (0x00000001) #define OPT_IGNORE_SELF (0x00000002) #define OPT_CMD_SHORT (0x00000004) #define OPT_CMD_LONG (0x00000008) #define OPT_CMD_COMM (0x00000010) #define OPT_CMD_ALL (OPT_CMD_SHORT | OPT_CMD_LONG | OPT_CMD_COMM) #define OPT_DIRNAME_STRIP (0x00000020) #define OPT_TICKS_ALL (0x00000040) #define OPT_TOTAL (0x00000080) #define OPT_MATCH_PID (0x00000100) #define OPT_TIMESTAMP (0x00000200) #define OPT_GRAND_TOTAL (0x00000400) #define OPT_SAMPLES (0x00000800) #define OPT_DISTRIBUTION (0x00001000) #define OPT_EXTRA_STATS (0x00002000) /* Histogram specific constants */ #define MAX_DIVISIONS (20) #define DISTRIBUTION_WIDTH (40) #define SIZEOF_ARRAY(a) (sizeof(a) / sizeof(a[0])) #define _VER_(major, minor, patchlevel) \ ((major * 10000) + (minor * 100) + patchlevel) #if defined(__GNUC__) && defined(__GNUC_MINOR__) #if defined(__GNUC_PATCHLEVEL__) #define NEED_GNUC(major, minor, patchlevel) \ _VER_(major, minor, patchlevel) <= _VER_(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__) #else #define NEED_GNUC(major, minor, patchlevel) \ _VER_(major, minor, patchlevel) <= _VER_(__GNUC__, __GNUC_MINOR__, 0) #endif #else #define NEED_GNUC(major, minor, patchlevel) (0) #endif #if defined(__GNUC__) && NEED_GNUC(4,6,0) #define HOT __attribute__ ((hot)) #else #define HOT #endif #if defined(__GNUC__) && !defined(__clang__) && NEED_GNUC(4,6,0) #define OPTIMIZE3 __attribute__((optimize("-O3"))) #else #define OPTIMIZE3 #endif #if defined(__GNUC__) #define LIKELY(x) __builtin_expect((x),1) #define UNLIKELY(x) __builtin_expect((x),0) #else #define LIKELY(x) (x) #define UNLIKELY(x) (x) #endif /* per process cpu information */ typedef struct cpu_info_t { struct cpu_info_t *hash_next; /* Next cpu info in hash */ struct cpu_info_t *list_next; /* Next cpu info in list */ pid_t pid; /* Process ID */ char comm[17]; /* Name of process/kernel task */ char state; /* Run state */ bool kernel_thread; /* true if a kernel thread */ uint64_t utotal; /* Usr Space total CPU ticks */ uint64_t stotal; /* Sys Space total CPU ticks */ uint64_t total; /* Total number of CPU ticks */ uint64_t ticks; /* Total life time in CPU ticks */ char *cmdline; /* Full name of process cmdline */ int processor; /* Last CPU run on */ } cpu_info_t; /* system wide CPU stats */ typedef struct { uint64_t ctxt; /* Context switches */ uint64_t irq; /* IRQ count */ uint64_t softirq; /* Soft IRQ count */ uint64_t processes; /* Number of processes since boot */ uint64_t running; /* Number of processes running */ uint64_t blocked; /* Number of processes blocked */ } proc_stat_t; /* CPU utilisation stats */ typedef struct cpu_stat { struct cpu_stat *next; /* Next cpu stat in hash table */ struct cpu_stat *sorted_usage_next; /* Next CPU stat in CPU usage list */ cpu_info_t *info; /* CPU info */ uint64_t utime; /* User time */ uint64_t stime; /* System time */ int64_t delta; /* Total Change in CPU ticks */ int64_t udelta; /* Change in user time */ int64_t sdelta; /* Change in system time */ double time; /* Wall clock time */ double time_delta; /* Wall clock time delta */ bool old; /* Existing task, not a new one */ } cpu_stat_t; /* sample delta item as an element of the sample_delta_list_t */ typedef struct sample_delta_item { struct sample_delta_item *next; /* Next in the list */ cpu_info_t *info; /* CPU info this refers to */ int64_t delta; /* difference in CPU ticks */ double time_delta; /* difference in time */ } sample_delta_item_t; /* list of sample_delta_items */ typedef struct sample_delta_list { struct sample_delta_item *sample_delta_item_list; struct sample_delta_list *next; /* next item in sample delta list */ struct sample_delta_list *prev; /* Previous in the list */ double whence; /* when the sample was taken */ } sample_delta_list_t; /* hash of cmdline comm info */ typedef struct pid_info { struct pid_info *next; /* next pid_info in list */ struct timespec st_ctim; /* time of process creation */ pid_t pid; /* process ID */ char *cmdline; /* process command line */ } pid_info_t; typedef struct { double threshold; /* scaling threashold */ double scale; /* scaling value */ char *suffix; /* Human Hz scale factor */ } cpu_freq_scale_t; /* scaling factor */ typedef struct { const char ch; /* Scaling suffix */ const uint64_t scale; /* Amount to scale by */ } scale_t; typedef struct { uint32_t hash; uint16_t offset; } proc_stat_fields_t; static cpu_freq_scale_t cpu_freq_scale[] = { { 1e1, 1e0, "Hz" }, { 1e4, 1e3, "KHz" }, { 1e7, 1e6, "MHz" }, { 1e10, 1e9, "GHz" }, { 1e13, 1e12, "THz" }, { 1e16, 1e15, "PHz" }, { -1.0, -1.0, NULL } }; static const scale_t second_scales[] = { { 's', 1 }, { 'm', 60 }, { 'h', 3600 }, { 'd', 24 * 3600 }, { 'w', 7 * 24 * 3600 }, { 'y', 365 * 24 * 3600 }, { ' ', INT64_MAX }, }; static const proc_stat_fields_t fields[] = { { 0x0000ca52, offsetof(proc_stat_t, irq) }, /* intr */ { 0x01fd11a1, offsetof(proc_stat_t, softirq) }, /* softirq */ { 0x0000d8b4, offsetof(proc_stat_t, ctxt) }, /* ctxt */ { 0xa114a557, offsetof(proc_stat_t, running) }, /* procs_running */ { 0xa1582f8c, offsetof(proc_stat_t, blocked) }, /* procs_blocked */ { 0x7fcb299b, offsetof(proc_stat_t, processes) },/* processes */ }; static cpu_stat_t *cpu_stat_free_list; /* List of free'd cpu stats */ static cpu_info_t *cpu_info_hash[TABLE_SIZE]; /* hash of cpu_info */ static cpu_info_t *cpu_info_list; /* cache list of cpu_info */ static pid_info_t *pid_info_hash[PID_HASH_SIZE]; /* Hash of cmdline info */ static size_t cpu_info_list_length; /* cpu_info_list length */ static sample_delta_list_t *sample_delta_list_head; static sample_delta_list_t *sample_delta_list_tail; /* samples, sorted by sample time */ static char *csv_results; /* results in comma separated values */ static volatile bool stop_cpustat = false; /* set by sighandler */ static double opt_threshold; /* ignore samples with CPU usage deltas less than this */ static unsigned int opt_flags; /* option flags */ static uint64_t clock_ticks; /* number of clock ticks per second */ static pid_t opt_pid = -1; /* PID to match against, -p option */ /* * Attempt to catch a range of signals so * we can clean */ static const int signals[] = { /* POSIX.1-1990 */ #ifdef SIGHUP SIGHUP, #endif #ifdef SIGINT SIGINT, #endif #ifdef SIGQUIT SIGQUIT, #endif #ifdef SIGFPE SIGFPE, #endif #ifdef SIGTERM SIGTERM, #endif #ifdef SIGUSR1 SIGUSR1, #endif #ifdef SIGUSR2 SIGUSR2, /* POSIX.1-2001 */ #endif #ifdef SIGXCPU SIGXCPU, #endif #ifdef SIGXFSZ SIGXFSZ, #endif /* Linux various */ #ifdef SIGIOT SIGIOT, #endif #ifdef SIGSTKFLT SIGSTKFLT, #endif #ifdef SIGPWR SIGPWR, #endif #ifdef SIGINFO SIGINFO, #endif #ifdef SIGVTALRM SIGVTALRM, #endif -1, }; /* * strtouint64() * fast string to uint64, is ~33% faster than GNU libc * no white space pre-skip or -ve handling */ static uint64_t OPTIMIZE3 HOT strtouint64(char *str, char **endptr) { register uint64_t v = 0; for (;;) { register unsigned int digit = *str - '0'; if (digit > 9) break; if (UNLIKELY(v >= 1844674407370955161)) goto do_overflow; v *= 10; v += digit; str++; } *endptr = str; return v; do_overflow: errno = ERANGE; return ~0; } /* * strtouint32() * fast string to uint32, is ~33% faster than GNU libc * no white space pre-skip or -ve handling */ static uint32_t OPTIMIZE3 HOT strtouint32(char *str, char **endptr) { register uint64_t v = 0; for (;;) { register unsigned int digit = *str - '0'; if (digit > 9) break; if (UNLIKELY(v >= 429496729)) goto do_overflow; v *= 10; v += digit; str++; } *endptr = str; return v; do_overflow: errno = ERANGE; return ~0; } /* * get_ticks() * get ticks */ static inline uint64_t get_ticks(void) { return (opt_flags & OPT_TICKS_ALL) ? clock_ticks * (uint64_t)sysconf(_SC_NPROCESSORS_ONLN) : clock_ticks; } /* * secs_to_str() * report seconds in different units. */ static const char *secs_to_str(const double secs) { static char buf[16]; int i; for (i = 0; i < 5; i++) { if (secs <= second_scales[i + 1].scale) break; } snprintf(buf, sizeof(buf), "%5.2f%c", secs / second_scales[i].scale, second_scales[i].ch); return buf; } /* * get_tm() * fetch tm, will set fields to zero if can't get */ static void get_tm(const double time_now, struct tm *tm) { time_t now = (time_t)time_now; if (UNLIKELY((now == ((time_t) -1)))) { memset(tm, 0, sizeof(struct tm)); } else { (void)localtime_r(&now, tm); } } /* * timeval_to_double * timeval to a double (in seconds) */ static inline double timeval_to_double(const struct timeval *const tv) { return (double)tv->tv_sec + ((double)tv->tv_usec / 1000000.0); } /* * double_to_timeval * seconds in double to timeval */ static inline void double_to_timeval( const double val, struct timeval *tv) { tv->tv_sec = val; tv->tv_usec = (val - (time_t)val) * 1000000.0; } /* * gettime_to_double() * get time as a double */ static double gettime_to_double(void) { struct timeval tv; if (UNLIKELY(gettimeofday(&tv, NULL) < 0)) { fprintf(stderr, "gettimeofday failed: errno=%d (%s)\n", errno, strerror(errno)); exit(EXIT_FAILURE); } return timeval_to_double(&tv); } /* * handle_sig() * catch signal and flag a stop */ static void handle_sig(int dummy) { (void)dummy; stop_cpustat = true; } /* * count_bits() * count bits set, from C Programming Language 2nd Ed */ static unsigned int count_bits(const unsigned int val) { register unsigned int c, n = val; for (c = 0; n; c++) n &= n - 1; return c; } /* * get_pid_cmdline * get process's /proc/pid/cmdline */ static char *get_pid_cmdline(const pid_t pid) { static char buffer[4096]; char *ptr = NULL; int fd; ssize_t ret; pid_info_t *info; int h = pid % PID_HASH_SIZE; char path[PATH_MAX]; struct stat statbuf; bool statok = false; snprintf(path, sizeof(path), "/proc/%i/cmdline", pid); if (UNLIKELY((fd = open(path, O_RDONLY)) < 0)) return NULL; if (LIKELY(fstat(fd, &statbuf) == 0)) { statok = true; for (info = pid_info_hash[h]; info; info = info->next) { if (info->pid == pid) { if (statbuf.st_ctim.tv_sec > info->st_ctim.tv_sec) break; (void)close(fd); return info->cmdline; } } } ret = read(fd, buffer, sizeof(buffer)); (void)close(fd); if (ret <= 0) goto no_cmd; if (UNLIKELY(ret >= (ssize_t)sizeof(buffer))) ret = sizeof(buffer) - 1; buffer[ret] = '\0'; /* * OPT_CMD_LONG option we get the full cmdline args */ if (opt_flags & OPT_CMD_LONG) { for (ptr = buffer; ptr < buffer + ret - 1; ptr++) { if (*ptr == '\0') *ptr = ' '; } *ptr = '\0'; } /* * OPT_CMD_SHORT option we discard anything after a space */ if (opt_flags & OPT_CMD_SHORT) { for (ptr = buffer; *ptr && (ptr < buffer + ret); ptr++) { if (*ptr == ' ') *ptr = '\0'; } } if (opt_flags & OPT_DIRNAME_STRIP) ptr = basename(buffer); else ptr = buffer; no_cmd: if (statok) { /* * We may be re-using a stale old PID, or we may need * a new info struct */ if (!info) info = malloc(sizeof(pid_info_t)); if (LIKELY((info != NULL))) { info->pid = pid; info->cmdline = ptr; info->next = pid_info_hash[h]; info->st_ctim = statbuf.st_ctim; pid_info_hash[h] = info; } } return ptr; } /* * pid_info_hash_free() * free pid_info hash table * */ static void pid_info_hash_free(void) { size_t i; for (i = 0; i < PID_HASH_SIZE; i++) { pid_info_t *info = pid_info_hash[i]; while (info) { pid_info_t *next = info->next; free(info); info = next; } } } /* * samples_free() * free collected samples */ static void samples_free(void) { sample_delta_list_t *sdl = sample_delta_list_head; while (sdl) { sample_delta_list_t *sdl_next = sdl->next; sample_delta_item_t *sdi = sdl->sample_delta_item_list; while (sdi) { sample_delta_item_t *sdi_next = sdi->next; free(sdi); sdi = sdi_next; } free(sdl); sdl = sdl_next; } } /* * sample_add() * add a cpu_stat's delta and info field to a list at time position whence */ static void OPTIMIZE3 HOT sample_add( const cpu_stat_t *const cpu_stat, const double whence) { bool found = false; sample_delta_list_t *sdl; sample_delta_item_t *sdi; for (sdl = sample_delta_list_tail; sdl; sdl = sdl->prev) { if (sdl->whence == whence) { found = true; break; } if (sdl->whence < whence) break; } /* * New time period, need new sdl, we assume it goes at the end of the * list since time is assumed to be increasing */ if (!found) { if (UNLIKELY((sdl = malloc(sizeof(sample_delta_list_t))) == NULL)) { fprintf(stderr, "Cannot allocate sample delta list\n"); exit(EXIT_FAILURE); } sdl->next = NULL; sdl->whence = whence; sdl->sample_delta_item_list = NULL; if (sample_delta_list_tail) sample_delta_list_tail->next = sdl; else sample_delta_list_head = sdl; sdl->prev = sample_delta_list_tail; sample_delta_list_tail = sdl; } /* Now append the sdi onto the list */ if (UNLIKELY((sdi = malloc(sizeof(sample_delta_item_t))) == NULL)) { fprintf(stderr, "Cannot allocate sample delta item\n"); exit(EXIT_FAILURE); } sdi->next = sdl->sample_delta_item_list; sdi->info = cpu_stat->info; sdi->delta = cpu_stat->delta; sdi->time_delta = cpu_stat->time_delta; sdl->sample_delta_item_list = sdi; } /* * sample_find() * scan through a sample_delta_list for cpu info, return NULL if not found */ static inline OPTIMIZE3 HOT sample_delta_item_t *sample_find( const sample_delta_list_t *const sdl, const cpu_info_t *const info) { sample_delta_item_t *sdi; for (sdi = sdl->sample_delta_item_list; sdi; sdi = sdi->next) { if (sdi->info == info) return sdi; } return NULL; } /* * info_compare_total() * used by qsort to sort array in CPU consumed ticks total order */ static int info_compare_total(const void *const item1, const void *const item2) { cpu_info_t **info1 = (cpu_info_t **)item1; cpu_info_t **info2 = (cpu_info_t **)item2; if ((*info2)->total == (*info1)->total) return 0; return ((*info2)->total > (*info1)->total) ? 1 : -1; } /* * duration_round() * round duration to nearest 1/100th second */ static inline double duration_round(const double duration) { return floor((duration * 100.0) + 0.5) / 100.0; } static inline void putdec(const int v) { register int d; d = (v / 10); fputc(d > 9 ? '?' : '0' + d, stdout); d = (v % 10); fputc('0' + d, stdout); } /* * info_banner_dump() * dump banner for per_info stats */ static void info_banner_dump(const double time_now) { fputs(" %CPU %USR %SYS PID S CPU Time Task", stdout); if (opt_flags & OPT_TIMESTAMP) { struct tm tm; get_tm(time_now, &tm); fputs(" (", stdout); putdec(tm.tm_hour); fputc(':', stdout); putdec(tm.tm_min); fputc(':', stdout); putdec(tm.tm_sec); fputc(')', stdout); } fputc('\n', stdout); } /* * info_dump() * dump per cpu_info stats */ static void info_dump( const uint64_t uticks, const uint64_t sticks, const uint64_t total_ticks, const cpu_info_t *info, double *u_total, double *s_total) { const double cpu_u_usage = total_ticks == 0 ? 0.0 : 100.0 * (double)uticks / total_ticks; const double cpu_s_usage = total_ticks == 0 ? 0.0 : 100.0 * (double)sticks / total_ticks; double cpu_time = ((double)(info->ticks)) / (double)clock_ticks; *u_total += cpu_u_usage; *s_total += cpu_s_usage; printf("%6.2f %6.2f %6.2f %5d %c %4d %s %s%s%s\n", cpu_u_usage + cpu_s_usage, cpu_u_usage, cpu_s_usage, info->pid, info->state, info->processor, secs_to_str(cpu_time), info->kernel_thread ? "[" : "", info->cmdline, info->kernel_thread ? "]" : ""); } /* * info_total_dump() * dump out totals of total, system and user times */ static void info_total_dump( const double u_total, const double s_total) { if (opt_flags & OPT_TOTAL) printf("%6.2f %6.2f %6.2f Total\n", u_total + s_total, u_total, s_total); } /* * samples_dump() * dump out samples to file */ static void samples_dump( const char *const filename, /* file to dump samples */ const double duration, /* duration in seconds */ const double time_now, /* time right now */ const uint64_t nr_ticks, /* number of ticks per sec */ const uint64_t total_ticks, /* total clock ticks */ const uint32_t samples) /* number of samples */ { sample_delta_list_t *sdl; cpu_info_t **sorted_cpu_infos; cpu_info_t *cpu_info; size_t i = 0, n; FILE *fp; double first_time = -1.0; if (UNLIKELY((sorted_cpu_infos = calloc(cpu_info_list_length, sizeof(cpu_info_t*))) == NULL)) { fprintf(stderr, "Cannot allocate buffer for sorting cpu_infos\n"); exit(EXIT_FAILURE); } /* Just want the CPUs with some non-zero total */ for (n = 0, cpu_info = cpu_info_list; cpu_info; cpu_info = cpu_info->list_next) { if (LIKELY((cpu_info->total > 0))) sorted_cpu_infos[n++] = cpu_info; } qsort(sorted_cpu_infos, n, sizeof(cpu_info_t *), info_compare_total); if (opt_flags & OPT_GRAND_TOTAL) { double cpu_u_total = 0.0, cpu_s_total = 0.0; printf("Grand Total (from %" PRIu32 " samples, %.1f seconds):\n", samples, duration); info_banner_dump(time_now); for (i = 0; i < n; i++) { info_dump(sorted_cpu_infos[i]->utotal, sorted_cpu_infos[i]->stotal, total_ticks, sorted_cpu_infos[i], &cpu_u_total, &cpu_s_total); } info_total_dump(cpu_u_total, cpu_s_total); putchar('\n'); } if (!filename) { free(sorted_cpu_infos); return; } if ((fp = fopen(filename, "w")) == NULL) { fprintf(stderr, "Cannot write to file %s\n", filename); free(sorted_cpu_infos); return; } fprintf(fp, "Task:%s", (opt_flags & OPT_TIMESTAMP) ? "," : ""); for (i = 0; i < n; i++) fprintf(fp, ",%s (%d)", sorted_cpu_infos[i]->comm, sorted_cpu_infos[i]->pid); fprintf(fp, "\n"); fprintf(fp, "Ticks:%s", (opt_flags & OPT_TIMESTAMP) ? "," : ""); for (i = 0; i < n; i++) fprintf(fp, ",%" PRIu64, sorted_cpu_infos[i]->total); fprintf(fp, "\n"); for (sdl = sample_delta_list_head; sdl; sdl = sdl->next) { if (first_time < 0) first_time = sdl->whence; fprintf(fp, "%f", duration_round(sdl->whence - first_time)); if (opt_flags & OPT_TIMESTAMP) { struct tm tm; get_tm(sdl->whence, &tm); fprintf(fp, ",%2.2d:%2.2d:%2.2d", tm.tm_hour, tm.tm_min, tm.tm_sec); } /* Scan in CPU info order to be consistent for all sdl rows */ for (i = 0; i < n; i++) { sample_delta_item_t *sdi = sample_find(sdl, sorted_cpu_infos[i]); if (sdi) { double duration = duration_round(sdi->time_delta); fprintf(fp,",%f", (duration < 0.01) ? 0.0 : 100.0 * (double)sdi->delta / (duration * (double)nr_ticks)); } else fprintf(fp,", "); } fprintf(fp, "\n"); } free(sorted_cpu_infos); (void)fclose(fp); } /* * max_processors() * Determine number of CPUs used */ static inline int max_processors(void) { int cpu_max = 0; cpu_info_t *cpu_info; for (cpu_info = cpu_info_list; cpu_info; cpu_info = cpu_info->list_next) if (cpu_info->processor > cpu_max) cpu_max = cpu_info->processor; return cpu_max + 1; } /* * cpu_distribution() * CPU distribution() */ static void cpu_distribution( const uint64_t total_ticks) { cpu_info_t *cpu_info; int i, cpu_max = max_processors(); uint64_t utotal[cpu_max], stotal[cpu_max]; if (!total_ticks) { printf("Cannot calculate distribution of CPU utilisation, " "(zero clock tick)\n"); return; } memset(utotal, 0, sizeof(utotal)); memset(stotal, 0, sizeof(stotal)); for (cpu_info = cpu_info_list; cpu_info; cpu_info = cpu_info->list_next) { int cpu = cpu_info->processor; utotal[cpu] += cpu_info->utotal; stotal[cpu] += cpu_info->stotal; } printf("Distribution of CPU utilisation (per CPU):\n"); printf(" CPU# USR%% SYS%%\n"); for (i = 0; i < cpu_max; i++) printf("%5d %6.2f %6.2f\n", i, 100.0 * (double)utotal[i] / (double)total_ticks, 100.0 * (double)stotal[i] / (double)total_ticks); } /* * samples_distribution() * show distribution of CPU utilisation */ static void samples_distribution(const uint64_t nr_ticks) { sample_delta_list_t *sdl; unsigned int bucket[MAX_DIVISIONS], max_bucket = 0, valid = 0, i, total = 0; double min = DBL_MAX, max = -DBL_MAX, division, prev; double scale = 100.0 / (double)nr_ticks; memset(bucket, 0, sizeof(bucket)); for (sdl = sample_delta_list_head; sdl; sdl = sdl->next) { sample_delta_item_t *sdi; for (sdi = sdl->sample_delta_item_list; sdi; sdi = sdi->next) { double val = scale * (double)sdi->delta; if (val > max) max = val; if (val < min) min = val; valid++; } } if (valid <= 1) { printf("Too few samples, cannot compute distribution\n"); return; } if (max - min < 0.01) { printf("Range is too small, cannot compute distribution\n"); return; } division = ((max * 1.000001) - min) / (MAX_DIVISIONS); for (sdl = sample_delta_list_head; sdl; sdl = sdl->next) { sample_delta_item_t *sdi; for (sdi = sdl->sample_delta_item_list; sdi; sdi = sdi->next) { double val = 100.0 * (double)sdi->delta / (double)nr_ticks; int v = floor(val - min) / division; v = v > MAX_DIVISIONS - 1 ? MAX_DIVISIONS -1 : v; bucket[v]++; total++; if (max_bucket < bucket[v]) max_bucket = bucket[v]; } } printf("Distribution of CPU utilisation (per Task):\n"); printf("%% CPU Utilisation Count (%%)\n"); for (prev = min, i = 0; i < MAX_DIVISIONS; i++, prev += division) { printf("%6.2f - %6.2f %8u %6.2f\n", prev, prev + division - 0.001, bucket[i], 100.0 * (double)bucket[i] / (double)total); } putchar('\n'); } /* * cpu_info_find() * try to find existing cpu info in cache, and to the cache * if it is new. */ static cpu_info_t OPTIMIZE3 HOT *cpu_info_find( const cpu_info_t *const new_info, const uint32_t hash) { cpu_info_t *info; const char *comm = new_info->comm; const pid_t pid = new_info->pid; for (info = cpu_info_hash[hash]; info; info = info->hash_next) { if ((pid == info->pid) && (strcmp(comm, info->comm) == 0)) return info; } if (UNLIKELY((info = malloc(sizeof(cpu_info_t))) == NULL)) { fprintf(stderr, "Cannot allocate CPU info\n"); exit(EXIT_FAILURE); } memcpy(info, new_info, sizeof(cpu_info_t)); if ((new_info->cmdline == NULL) || (opt_flags & OPT_CMD_COMM)) { info->cmdline = info->comm; } else { if (UNLIKELY((info->cmdline = strdup(new_info->cmdline)) == NULL)) { fprintf(stderr, "Cannot allocate CPU cmdline field info\n"); exit(EXIT_FAILURE); } } /* Does not exist in list, append it */ info->list_next = cpu_info_list; cpu_info_list = info; cpu_info_list_length++; info->hash_next = cpu_info_hash[hash]; cpu_info_hash[hash] = info; return info; } /* * cpu_info_free() * free cpu_info and it's elements */ static void cpu_info_free(void *const data) { cpu_info_t *info = (cpu_info_t*)data; if (info->cmdline != info->comm) free(info->cmdline); free(info); } /* * cpu_info_list_free * free up all unique cpu infos */ static void cpu_info_list_free(void) { cpu_info_t *cpu_info = cpu_info_list; while (cpu_info) { cpu_info_t *next = cpu_info->list_next; cpu_info_free(cpu_info); cpu_info = next; } } /* * cpu_stat_list_free * free up cpu stat info from the free list */ static void cpu_stat_list_free(void) { cpu_stat_t *cs = cpu_stat_free_list; while (cs) { cpu_stat_t *next = cs->next; free(cs); cs = next; } } /* * hash_djb2a() * Hash a string, from Dan Bernstein comp.lang.c (xor version) */ static uint32_t OPTIMIZE3 HOT hash_djb2a(const pid_t pid, const char *str) { register uint32_t hash = 5381 + pid; register int c; while ((c = *str++)) { /* (hash * 33) ^ c */ hash = ((hash << 5) + hash) ^ c; } return hash % TABLE_SIZE; } /* * cpu_stat_free_contents() * Free CPU info from a hash table */ static void cpu_stat_free_contents( cpu_stat_t *cpu_stats[]) /* CPU stat hash table */ { int i; for (i = 0; i < TABLE_SIZE; i++) { cpu_stat_t *cs = cpu_stats[i]; while (cs) { cpu_stat_t *next = cs->next; /* Shove it onto the free list */ cs->next = cpu_stat_free_list; cpu_stat_free_list = cs; cs = next; } cpu_stats[i] = NULL; } } /* * cpu_stat_add() * add pid stats to a hash table if it is new, otherwise just * accumulate the tick count. */ static void OPTIMIZE3 HOT cpu_stat_add( cpu_stat_t *cpu_stats[], /* CPU stat hash table */ cpu_info_t *info, /* paritially complete cpu info */ const double time_now, /* time sample was taken */ const uint64_t utime, /* user time in ticks */ const uint64_t stime) /* system time in ticks */ { cpu_stat_t *cs, *cs_new; uint32_t h; const char *comm = info->comm; const pid_t pid = info->pid; h = hash_djb2a(pid, comm); for (cs = cpu_stats[h]; cs; cs = cs->next) { if ((pid == cs->info->pid) && (strcmp(cs->info->comm, comm) == 0)) { cs->utime += utime; cs->stime += stime; cs->info->state = info->state; cs->info->processor = info->processor; return; } } /* Not found, it is new! */ if (cpu_stat_free_list) { /* Re-use one from the free list */ cs_new = cpu_stat_free_list; cpu_stat_free_list = cs_new->next; } else { if (UNLIKELY((cs_new = malloc(sizeof(cpu_stat_t))) == NULL)) { fprintf(stderr, "Out of memory allocating a cpu stat\n"); exit(1); } } info->cmdline = get_pid_cmdline(pid); info->kernel_thread = (info->cmdline == NULL); cs_new->utime = utime; cs_new->stime = stime; cs_new->info = cpu_info_find(info, h); cs_new->time = time_now; cs_new->time_delta = 0.0; cs_new->old = false; cs_new->next = cpu_stats[h]; cs_new->sorted_usage_next = NULL; cpu_stats[h] = cs_new; } /* * cpu_stat_find() * find a CPU stat (needle) in a CPU stat hash table (haystack) */ static cpu_stat_t OPTIMIZE3 HOT *cpu_stat_find( cpu_stat_t *const haystack[], /* CPU stat hash table */ const cpu_stat_t *const needle) /* CPU stat to find */ { cpu_stat_t *ts; const char *comm = needle->info->comm; const pid_t pid = needle->info->pid; for (ts = haystack[hash_djb2a(needle->info->pid, needle->info->comm)]; ts; ts = ts->next) if ((pid == ts->info->pid) && (strcmp(comm, ts->info->comm) == 0)) return ts; return NULL; /* no success */ } /* * cpu_stat_sort_freq_add() * add a CPU stat to a sorted list of CPU stats */ static void OPTIMIZE3 HOT cpu_stat_sort_freq_add( cpu_stat_t **sorted, /* CPU stat sorted list */ cpu_stat_t *const new) /* CPU stat to add */ { while (*sorted) { if ((*sorted)->delta < new->delta) { new->sorted_usage_next = *(sorted); break; } sorted = &(*sorted)->sorted_usage_next; } *sorted = new; } /* * cpu_stat_diff() * find difference in tick count between to hash table samples of CPU * stats. We are interested in just current and new CPU stats, * not ones that silently die */ static void cpu_stat_diff( const double duration, /* time between each sample */ const uint64_t nr_ticks, /* ticks per second */ const int32_t n_lines, /* number of lines to output */ const double time_now, /* time right now */ cpu_stat_t *const cpu_stats_old[], /* old CPU stats samples */ cpu_stat_t *const cpu_stats_new[]) /* new CPU stats samples */ { int i; cpu_stat_t *sorted = NULL; const bool do_sample_add = (opt_flags & OPT_SAMPLES); for (i = 0; i < TABLE_SIZE; i++) { cpu_stat_t *cs; for (cs = cpu_stats_new[i]; cs; cs = cs->next) { cpu_stat_t *found = cpu_stat_find(cpu_stats_old, cs); if (found) { cs->udelta = cs->utime - found->utime; cs->sdelta = cs->stime - found->stime; cs->delta = cs->udelta + cs->sdelta; cs->time_delta = cs->time - found->time; if (cs->delta >= (int64_t)opt_threshold) { cs->old = true; if (cs->udelta + cs->sdelta > 0) cpu_stat_sort_freq_add(&sorted, cs); if (do_sample_add) sample_add(cs, time_now); found->info->total += cs->delta; found->info->utotal += cs->udelta; found->info->stotal += cs->sdelta; found->info->ticks = cs->utime + cs->stime; } } else { cs->delta = cs->udelta = cs->sdelta = 0; cs->time_delta = duration; if (cs->delta >= (int64_t)opt_threshold) { cs->old = false; if (do_sample_add) sample_add(cs, time_now); } } } } if (!(opt_flags & OPT_QUIET)) { int32_t j = 0; double cpu_u_total = 0.0, cpu_s_total = 0.0; info_banner_dump(time_now); while (sorted) { double cpu_u_usage = 100.0 * (double)sorted->udelta / (double)(nr_ticks); double cpu_s_usage = 100.0 * (double)sorted->sdelta / (double)(nr_ticks); double cpu_t_usage = cpu_u_usage + cpu_s_usage; if ((n_lines == -1) || (j < n_lines)) { j++; if (cpu_t_usage > 0.0) info_dump(sorted->udelta, sorted->sdelta, nr_ticks, sorted->info, &cpu_u_total, &cpu_s_total); } sorted = sorted->sorted_usage_next; } info_total_dump(cpu_u_total, cpu_s_total); } } /* * get_proc_stat() * read /proc/stat */ static int get_proc_stat(proc_stat_t *proc_stat) { FILE *fp; char buffer[4096]; memset(proc_stat, 0, sizeof(proc_stat_t)); fp = fopen("/proc/stat", "r"); if (UNLIKELY(!fp)) return -1; while (fgets(buffer, sizeof(buffer), fp) != NULL) { register char *ptr = buffer; register uint32_t hash = 0; size_t i; while (*ptr != ' ') { if (*ptr == '\0') goto next; hash <<= 3; hash ^= *ptr; ptr++; } for (i = 0; i < SIZEOF_ARRAY(fields); i++) { char *dummy; if (hash == fields[i].hash) { *((uint64_t *)(((uint8_t *)proc_stat) + fields[i].offset)) = strtouint64(ptr + 1, &dummy); break; } } next: ; } fclose(fp); return 0; } /* * proc_stat_diff() * compute delta between last proc_stat sample */ static inline void proc_stat_diff( const proc_stat_t *old, const proc_stat_t *new, proc_stat_t *delta) { delta->ctxt = new->ctxt - old->ctxt; delta->irq = new->irq - old->irq; delta->softirq = new->softirq - old->softirq; delta->processes = new->processes - old->processes; delta->running = new->running; delta->blocked = new->blocked; } /* * proc_stat_dump() * dump out proc_stat stats */ static inline void proc_stat_dump( const proc_stat_t *delta, const double duration) { double scale = 1.0 / duration; printf("%.1f Ctxt/s, %.1f IRQ/s, %.1f softIRQ/s, " "%.1f new tasks/s, %" PRIu64 " running, %" PRIu64 " blocked\n", scale * delta->ctxt, scale * delta->irq, scale * delta->softirq, scale * delta->processes, delta->running, delta->blocked); } /* * get_cpustats() * scan /proc/cpu_stats and populate a cpu stat hash table with * unique tasks */ static void get_cpustats( cpu_stat_t *cpu_stats[], const double time_now) { DIR *dir; struct dirent *entry; static pid_t my_pid; if (UNLIKELY(((opt_flags & OPT_IGNORE_SELF) && (my_pid == 0)))) my_pid = getpid(); if (UNLIKELY((dir = opendir("/proc")) == NULL)) { fprintf(stderr, "Cannot read directory /proc\n"); return; } while ((entry = readdir(dir)) != NULL) { cpu_info_t info; uint64_t utime; uint64_t stime; char filename[PATH_MAX]; char buffer[4096]; char *ptr = buffer, *endptr, *tmp; ssize_t len; int fd, skip; if (!isdigit(entry->d_name[0])) continue; snprintf(filename, sizeof(filename), "/proc/%s/stat", entry->d_name); if ((fd = open(filename, O_RDONLY)) < 0) continue; len = read(fd, buffer, sizeof(buffer) - 1); (void)close(fd); if (UNLIKELY(len <= 1)) continue; buffer[len] = '\0'; /* 3173 (a.out) R 3093 3173 3093 34818 3173 4202496 165 0 0 0 3194 0 */ /* * We used to use scanf but this is really expensive and it * is far faster to parse the data via a more tedious means * of scanning down the buffer manually.. */ info.pid = (pid_t)strtouint32(ptr, &endptr); if (endptr == ptr) continue; ptr = endptr; if (UNLIKELY(*ptr != ' ')) continue; ptr++; if (UNLIKELY((*ptr != '('))) continue; ptr++; tmp = info.comm; while ((*ptr != '\0') && (*ptr !=')') && ((size_t)(tmp - info.comm) < sizeof(info.comm))) *tmp++ = *ptr++; if (UNLIKELY(*ptr != ')')) continue; *tmp = '\0'; ptr++; if (UNLIKELY(*ptr != ' ')) continue; ptr++; info.state = *ptr; ptr++; /* Skip over fields to the 14th field (utime) */ skip = 11; while (skip > 0 && *ptr) { if (*ptr == ' ') skip--; ptr++; } if (UNLIKELY(*ptr == '\0')) continue; /* Field 14, utime */ utime = strtouint64(ptr, &endptr); if (UNLIKELY(endptr == ptr)) continue; ptr = endptr; if (UNLIKELY(*ptr != ' ')) continue; ptr++; /* Field 15, stime */ stime = strtouint64(ptr, &endptr); if (UNLIKELY(endptr == ptr)) continue; ptr = endptr; skip = 24; while (skip > 0 && *ptr) { if (*ptr == ' ') skip--; ptr++; } if (UNLIKELY((*ptr == '\0'))) continue; /* Field 39, processor */ info.processor = (int)strtouint32(ptr, &endptr); if (UNLIKELY(endptr == ptr)) continue; if (UNLIKELY(((opt_flags & OPT_IGNORE_SELF) && (my_pid == info.pid)))) continue; if (UNLIKELY(((opt_flags & OPT_MATCH_PID) && (opt_pid != info.pid)))) continue; info.total = 0; info.ticks = 0; cpu_stat_add(cpu_stats, &info, time_now, utime, stime); } (void)closedir(dir); } /* * cpu_freq_average() * get average CPU frequency */ static double cpu_freq_average(uint32_t max_cpus) { uint32_t i, n = 0; double total_freq = 0; for (i = 0; i < max_cpus; i++) { char path[PATH_MAX]; int fd; snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%" PRIu32 "/cpufreq/scaling_cur_freq", i); if (LIKELY((fd = open(path, O_RDONLY)) > -1)) { char buffer[64]; ssize_t ret; ret = read(fd, buffer, sizeof(buffer) - 1); (void)close(fd); if (LIKELY(ret > 0)) { double freq; char *dummy; buffer[ret] = '\0'; freq = 1000.0 * (double)strtouint64(buffer, &dummy); total_freq += freq; n++; } } } return n > 0 ? total_freq / n : 0.0; } /* * cpu_freq_format() * scale cpu freq into a human readable form */ static const char *cpu_freq_format(double freq) { static char buffer[40]; char *suffix = "EHz"; double scale = 1e18; size_t i; for (i = 0; cpu_freq_scale[i].suffix; i++) { if (freq < cpu_freq_scale[i].threshold) { suffix = cpu_freq_scale[i].suffix; scale = cpu_freq_scale[i].scale; break; } } snprintf(buffer, sizeof(buffer), "%.2f %s", freq / scale, suffix); return buffer; } /* * cpus_online() * determine number of CPUs online */ static char *cpus_online(void) { int fd; static char buffer[4096]; uint32_t cpus = 0; char *ptr = buffer; ssize_t ret; if (UNLIKELY((fd = open("/sys/devices/system/cpu/online", O_RDONLY)) < 0)) goto unknown; ret = read(fd, buffer, sizeof(buffer) - 1); close(fd); if (UNLIKELY(ret < 0)) goto unknown; for (;;) { char ch; int32_t n1; n1 = strtouint32(ptr, &ptr); ch = *ptr; if (ch == '-') { int32_t n2, range; ptr++; n2 = strtouint32(ptr, &ptr); range = 1 + n2 - n1; if (range > 0) cpus += range; n1 = -1; ch = *ptr; /* next char must be EOS or , */ } if (ch == '\0' || ch == '\n') { if (n1 > -1) cpus++; break; } else if (ch == ',') { ptr++; if (n1 > -1) cpus++; continue; } else goto unknown; } snprintf(buffer, sizeof(buffer), "%" PRId32, cpus); return buffer; unknown: return "unknown"; } /* * load_average() * get current load average stats */ static char *load_average(void) { static char buffer[4096]; char *ptr = buffer; ssize_t len; int fd, skip = 3; if (UNLIKELY((fd = open("/proc/loadavg", O_RDONLY)) < 0)) goto unknown; len = read(fd, buffer, sizeof(buffer) - 1); (void)close(fd); if (UNLIKELY(len < 1)) goto unknown; buffer[len] = '\0'; for (;;) { if (*ptr == '\0') { skip--; break; } if (*ptr == ' ') { skip--; if (skip == 0) { *ptr = '\0'; break; } } ptr++; } if (skip != 0) goto unknown; return buffer; unknown: return "unknown"; } /* * show_usage() * show how to use */ static void show_usage(void) { printf(APP_NAME ", version " VERSION "\n\n" "Usage: " APP_NAME " [options] [duration] [count]\n" " -h help\n" " -a calculate CPU utilisation based on all the CPU ticks\n" " rather than per CPU tick\n" " -c get command name from processes comm field\n" " -d strip directory basename off command information\n" " -D show distribution of CPU utilisation stats at end\n" " -g show grand total of CPU utilisation stats at end\n" " -i ignore " APP_NAME " in the statistics\n" " -l show long (full) command information\n" " -n specifies number of tasks to display\n" " -p just show utilisation for a specified PID\n" " -q run quietly, useful with option -r\n" " -r specifies a comma separated values output file to dump\n" " samples into\n" " -s show short command information\n" " -S timestamp output\n" " -t specifies a task tick count threshold where samples less\n" " than this are ignored\n" " -T show total CPU utilisation statistics\n" " -x show extra stats (load average, avg cpu freq, etc)\n"); } int main(int argc, char **argv) { cpu_stat_t **cpu_stats_old, **cpu_stats_new, **cpu_stats_tmp; struct sigaction new_action; proc_stat_t proc_stats[2]; proc_stat_t *proc_stat_old, *proc_stat_new, *proc_stat_tmp, proc_stat_delta; uint32_t max_cpus = sysconf(_SC_NPROCESSORS_CONF); double duration_secs = 1.0, time_start, time_now; int64_t count = 1, t = 1; uint64_t nr_ticks, total_ticks = 0; int32_t n_lines = -1; uint32_t samples = 0; bool forever = true; int i; clock_ticks = (uint64_t)sysconf(_SC_CLK_TCK); for (;;) { int c = getopt(argc, argv, "acdDghiln:qr:sSt:Tp:x"); if (c == -1) break; switch (c) { case 'a': opt_flags |= OPT_TICKS_ALL; break; case 'c': opt_flags |= OPT_CMD_COMM; break; case 'd': opt_flags |= OPT_DIRNAME_STRIP; break; case 'D': opt_flags |= (OPT_SAMPLES | OPT_DISTRIBUTION); break; case 'g': opt_flags |= OPT_GRAND_TOTAL; break; case 'h': show_usage(); exit(EXIT_SUCCESS); case 'i': opt_flags |= OPT_IGNORE_SELF; break; case 'l': opt_flags |= OPT_CMD_LONG; break; case 'n': errno = 0; n_lines = (int32_t)strtol(optarg, NULL, 10); if (errno) { fprintf(stderr, "Invalid value for -n option\n"); exit(EXIT_FAILURE); } if (n_lines < 1) { fprintf(stderr, "-n option must be greater than 0\n"); exit(EXIT_FAILURE); } break; case 'p': errno = 0; opt_pid = strtol(optarg, NULL, 10); if (errno) { fprintf(stderr, "Invalid value for -o option\n"); exit(EXIT_FAILURE); } opt_flags |= OPT_MATCH_PID; break; case 's': opt_flags |= OPT_CMD_SHORT; break; case 'S': opt_flags |= OPT_TIMESTAMP; break; case 't': opt_threshold = atof(optarg); if (opt_threshold < 0.0) { fprintf(stderr, "-t threshold must be 0 or more.\n"); exit(EXIT_FAILURE); } break; case 'T': opt_flags |= OPT_TOTAL; break; case 'q': opt_flags |= OPT_QUIET; break; case 'r': csv_results = optarg; opt_flags |= OPT_SAMPLES; break; case 'x': opt_flags |= OPT_EXTRA_STATS; break; default: show_usage(); exit(EXIT_FAILURE); } } if (count_bits(opt_flags & OPT_CMD_ALL) > 1) { fprintf(stderr, "Cannot have -c, -l, -s at same time.\n"); exit(EXIT_FAILURE); } if (optind < argc) { duration_secs = atof(argv[optind++]); if (duration_secs < 0.333) { fprintf(stderr, "Duration must 0.333 or more\n"); exit(EXIT_FAILURE); } } if (optind < argc) { forever = false; errno = 0; count = (int64_t)strtoll(argv[optind++], NULL, 10); if (errno) { fprintf(stderr, "Invalid value for count\n"); exit(EXIT_FAILURE); } if (count < 1) { fprintf(stderr, "Count must be greater than 0\n"); exit(EXIT_FAILURE); } } opt_threshold *= duration_secs; memset(&new_action, 0, sizeof(new_action)); for (i = 0; signals[i] != -1; i++) { new_action.sa_handler = handle_sig; sigemptyset(&new_action.sa_mask); new_action.sa_flags = 0; if (sigaction(signals[i], &new_action, NULL) < 0) { fprintf(stderr, "sigaction failed: errno=%d (%s)\n", errno, strerror(errno)); exit(EXIT_FAILURE); } } cpu_stats_old = calloc(TABLE_SIZE, sizeof(cpu_stat_t*)); cpu_stats_new = calloc(TABLE_SIZE, sizeof(cpu_stat_t*)); if (UNLIKELY(cpu_stats_old == NULL || cpu_stats_new == NULL)) { fprintf(stderr, "Cannot allocate CPU statistics tables\n"); exit(EXIT_FAILURE); } proc_stat_old = &proc_stats[0]; proc_stat_new = &proc_stats[1]; time_now = time_start = gettime_to_double(); get_cpustats(cpu_stats_old, time_now); if (opt_flags & OPT_EXTRA_STATS) get_proc_stat(proc_stat_old); nr_ticks = get_ticks(); while (!stop_cpustat && (forever || count--)) { struct timeval tv; double secs, duration, right_now; /* Timeout to wait for in the future for this sample */ secs = time_start + ((double)t * duration_secs) - time_now; /* Play catch-up, probably been asleep */ if (secs < 0.0) { t = ceil((time_now - time_start) / duration_secs); secs = time_start + ((double)t * duration_secs) - time_now; /* We don't get sane stats if duration is too small */ if (secs < 0.5) secs += duration_secs; } else { t++; } double_to_timeval(secs, &tv); if (UNLIKELY(select(0, NULL, NULL, NULL, &tv) < 0)) { if (errno == EINTR) { stop_cpustat = true; } else { fprintf(stderr, "select failed: errno=%d (%s)\n", errno, strerror(errno)); break; } } /* * total ticks can change depending on number of CPUs online * so we need to account for these changing. */ right_now = gettime_to_double(); duration = duration_round(right_now - time_now); nr_ticks = get_ticks() * duration; total_ticks += nr_ticks; time_now = right_now; get_cpustats(cpu_stats_new, time_now); if (opt_flags & OPT_EXTRA_STATS) { double avg_cpu_freq = cpu_freq_average(max_cpus); get_proc_stat(proc_stat_new); proc_stat_diff(proc_stat_old, proc_stat_new, &proc_stat_delta); printf("Load Avg %s, Freq Avg. %s, %s CPUs online\n", load_average(), cpu_freq_format(avg_cpu_freq), cpus_online()); proc_stat_dump(&proc_stat_delta, duration); } cpu_stat_diff(duration, nr_ticks, n_lines, time_now, cpu_stats_old, cpu_stats_new); cpu_stat_free_contents(cpu_stats_old); cpu_stats_tmp = cpu_stats_old; cpu_stats_old = cpu_stats_new; cpu_stats_new = cpu_stats_tmp; proc_stat_tmp = proc_stat_old; proc_stat_old = proc_stat_new; proc_stat_new = proc_stat_tmp; samples++; putchar('\n'); } time_now = gettime_to_double(); samples_dump(csv_results, time_now - time_start, time_now, nr_ticks, total_ticks, samples); if (opt_flags & OPT_DISTRIBUTION) { samples_distribution(nr_ticks); cpu_distribution(total_ticks); } cpu_stat_free_contents(cpu_stats_old); cpu_stat_free_contents(cpu_stats_new); free(cpu_stats_old); free(cpu_stats_new); samples_free(); cpu_info_list_free(); cpu_stat_list_free(); pid_info_hash_free(); exit(EXIT_SUCCESS); } cpustat-0.01.25/Makefile0000664000175000017500000000327512607511740013460 0ustar kingking# # Copyright (C) 2011-2015 Canonical # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # # Author: Colin Ian King # VERSION=0.01.25 CFLAGS += -Wall -Wextra -DVERSION='"$(VERSION)"' -O2 # # Some useful optimisation settings for GCC # # -Winline \ # -fwhole-program -freciprocal-math -ffast-math \ # --param max-reload-search-insns=32768 \ # --param max-cselib-memory-locations=32768 # BINDIR=/usr/sbin MANDIR=/usr/share/man/man8 cpustat: cpustat.o Makefile $(CC) $(CPPFLAGS) $(CFLAGS) $< -lm -o $@ $(LDFLAGS) cpustat.o: cpustat.c Makefile cpustat.8.gz: cpustat.8 gzip -c $< > $@ dist: rm -rf cpustat-$(VERSION) mkdir cpustat-$(VERSION) cp -rp README Makefile cpustat.c cpustat.8 COPYING cpustat-$(VERSION) tar -zcf cpustat-$(VERSION).tar.gz cpustat-$(VERSION) rm -rf cpustat-$(VERSION) clean: rm -f cpustat cpustat.o cpustat.8.gz rm -f cpustat-$(VERSION).tar.gz install: cpustat cpustat.8.gz mkdir -p ${DESTDIR}${BINDIR} cp cpustat ${DESTDIR}${BINDIR} mkdir -p ${DESTDIR}${MANDIR} cp cpustat.8.gz ${DESTDIR}${MANDIR} cpustat-0.01.25/cpustat.80000664000175000017500000001307412607511740013572 0ustar kingking.\" Hey, EMACS: -*- nroff -*- .\" First parameter, NAME, should be all caps .\" Second parameter, SECTION, should be 1-8, maybe w/ subsection .\" other parameters are allowed: see man(7), man(1) .TH CPUSTAT 8 "August 14, 2015" .\" Please adjust this date whenever revising the manpage. .\" .\" Some roff macros, for reference: .\" .nh disable hyphenation .\" .hy enable hyphenation .\" .ad l left justify .\" .ad b justify to both left and right margins .\" .nf disable filling .\" .fi enable filling .\" .br insert line break .\" .sp insert n+1 empty lines .\" for manpage-specific macros, see man(7) .SH NAME cpustat \- a tool to measure CPU utilization. .br .SH SYNOPSIS .B cpustat [ options ] .RI [ delay " [" count ]] .br .SH DESCRIPTION cpustat is a program that dumps the CPU utilization of current running tasks (that is, processes or kernel threads). cpustat is useful to monitor the activity of long lived processes in a system, such as daemons, kernel threads as well as typical user processes. .P cpustat shows only the tasks that have measured any change in their CPU activity between each sample interval (as indicated by an increment in the CPU tick count stats of utime and stime in /proc/$pid/stat). cpustat thus only reports activity of busy tasks that are still alive at the time of each sample snapshot and hence will not account for very short lived processes that exist between each sample period. .P For each running task that has consumed some CPU during the sample time, the following information is displayed: .TS center; lBw(10) lBw(50) l l. Heading T{ Description T} %CPU T{ Total CPU used (in percent) T} %USR T{ CPU used in user space (in percent) T} %SYS T{ CPU used in system (kernel) space (in percent) T} PID T{ Process ID T} S T{ Process State, R (Running), S (Sleeping), D (Waiting, Disk Sleep), T (Stopped), t (Tracing stopped), W (Paging), X (Dead), x (Dead), K (Wakekill), W (Waking), P (Parked). T} CPU T{ CPU used by the process at time of sampling. T} Time T{ Total CPU time used by the process since it started. T} Task T{ Process command line information (from process cmdline or comm fields) T} .TE .P cpustat was designed to try and minimize the CPU overhead of process statistics gathering and reporting. It is therefore ideal for small embedded devices where CPU is limited where measuring CPU utilisation may affect the overall CPU statistics. For this reason, it is not as complex as tools such as top(1) that have a more feature rich user interface. .SH OPTIONS cpustat options are as follow: .TP .B \-a calculate CPU utilisation based on all CPUs. For example, if a process is using 100% of 1 CPU and the system has 4 CPUs, then the utilisation will show as 25%. The default is to show utilisation on a per CPU basis. .TP .B \-c get command information from process comm field. .TP .B \-d strip directory basename off command information. .TP .B \-D compute and show the distribution of CPU utilisation by task and by CPU. .br By task, this breaks the CPU utilisation of each task into 20 ranges from minimum to the maximum and shows the count of tasks found at in that paricular utilisation range. Useful to see any outliers and to characterize the typical per task usage of the CPU. .br By CPU, this shows the user and system CPU utilisation by per CPU. .TP .B \-g show grand total of CPU utilisation statistics at the end of the run. This is the total cumulatave CPU used by each process, averaged over the entire run duration. .TP .B \-h show help. .TP .B \-i ignore cpustat in the statistics. .TP .B \-l show long (full) command information. .TP .B \-n task_count only display the first task_count number of top tasks. .TP .B \-p PID onlt display process that matches the given PID. .TP .B \-q run quietly, only really makes sense with -r option. .TP .B \-r csv_file output gathered data in a comma separated values file. This can be then imported and graphed using your favourite open source spread sheet. The %CPU utilisation (system and user) for each process at each sample time is output into a table. .TP .B \-s show short command information. .TP .B \-S show time stamp. If the \-r option is used then an extra column appears in the CSV output file with the time of day for each sample. .TP .B \-t threshold ignore samples where the CPU usage delta per second less than the given threshold. .TP .B \-T calculate total CPU utilisation. .TP .B \-x show extra CPU related statistics, namely: CPU load average over 1, 5 and 10 minutes, CPU frequency (average of all online CPU frequencies), number of CPUs online. .SH EXAMPLES .LP cpustat .IP Dump CPU stats every second until stopped. .LP cpustat \-n 20 60 .IP Dump the top 20 CPU consuming tasks every 60 seconds until stopped. .LP cpustat 10 5 .IP Dump CPU stats every 10 seconds just 5 times. .LP cpustat \-x \-D \-a 1 300 .IP Gather stats every second for 5 minutes with extra CPU stats and show CPU utilisation distributions per task and per CPU at the end of the run. Also, scale CPU utilisation by the number of CPUs so that 100% utilisation means 100% of all CPUs rather than 100% of 1 CPU. .SH SEE ALSO .BR forkstat (8), .BR eventstat (8), .BR vmstat (8), .BR top (1) .SH AUTHOR cpustat was written by Colin King .PP This manual page was written by Colin King , for the Ubuntu project (but may be used by others). .SH COPYRIGHT Copyright \(co 2011-2015 Canonical Ltd. .br This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. cpustat-0.01.25/COPYING0000664000175000017500000004325412607511740013054 0ustar kingking GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. cpustat-0.01.25/README0000664000175000017500000002337112607511740012677 0ustar kingkingcpustat cpustat periodically dumps out the current CPU utilisation statistics of running processes. cpustat has been optimised to have a minimal CPU overhead and typically uses about 35% of the CPU compared to top. cpustat also includes some simple statistical analysis options that can help characterise the way CPUs are being loaded. cpustat command line options: -h help -a calculate CPU utilisation based on all the CPU ticks rather than one CPU -c get command name from processes comm field (less expensive on CPU) -d strip directory basename off command information -D show distribution of CPU utilisation stats at end of run -g show grand total of CPU utilisation stats at end of run -i ignore cpustat in the statistics -l show long (full) command information -n specifies number of tasks to display -q run quietly, useful with option -r -r specifies a comma separated values output file to dump samples into. -s show short command information -S timestamped output -t specifies an task tick count threshold where samples less than this are ignored. -T show total CPU utilisation statistics -x show extra stats (load average, avg cpu freq, etc) Example Output: cpustat 5 5 -gxDST Load Avg 1.71 1.11 0.88, Freq Avg. 2.92 GHz, 4 CPUs online 7248.5 Ctxt/s, 2444.1 IRQ/s, 1658.1 softIRQ/s, 1.2 new tasks/s, 9 running, 0 blocked %CPU %USR %SYS PID S CPU Time Task (14:09:46) 73.25 73.25 0.00 31078 S 2 1.94m stress-ng 72.06 70.86 1.20 31079 R 1 1.94m stress-ng 20.36 16.97 3.39 31037 S 0 33.28s /usr/lib/firefox/plugin-container 8.78 7.98 0.80 7027 S 3 42.70s /usr/lib/thunderbird/thunderbird 4.99 3.19 1.80 7134 S 3 1.94m /usr/lib/firefox/firefox 4.79 3.19 1.60 901 S 3 8.06m /usr/bin/X 3.39 2.99 0.40 2250 S 2 3.99m compiz 1.60 0.80 0.80 2375 S 2 1.58m /usr/bin/pulseaudio 1.60 0.00 1.60 31036 S 0 1.12s [kworker/0:2] 0.40 0.40 0.00 5719 S 2 22.10s /usr/lib/gnome-terminal/gnome-terminal-server 0.40 0.40 0.00 2177 S 3 21.25s /usr/bin/ibus-daemon 0.20 0.20 0.00 30774 S 0 1.03s /usr/lib/firefox/plugin-container 0.20 0.00 0.20 493 S 2 0.25s [jbd2/sda3-8] 0.20 0.20 0.00 2206 S 3 3.44s /usr/lib/ibus/ibus-ui-gtk3 0.20 0.00 0.20 6852 S 3 0.24s [kworker/3:1] 0.20 0.20 0.00 2170 S 1 0.25s upstart-dbus-bridge 0.20 0.00 0.20 6806 S 2 12.18s [kworker/u16:3] 192.81 180.64 12.18 Total Load Avg 1.82 1.14 0.90, Freq Avg. 2.92 GHz, 4 CPUs online 6781.6 Ctxt/s, 2210.8 IRQ/s, 1338.8 softIRQ/s, 0.6 new tasks/s, 2 running, 0 blocked %CPU %USR %SYS PID S CPU Time Task (14:09:51) 73.40 73.40 0.00 31079 R 1 2.00m stress-ng 72.80 71.40 1.40 31078 S 2 2.00m stress-ng 19.40 17.40 2.00 31037 S 0 34.25s /usr/lib/firefox/plugin-container 15.00 13.80 1.20 7027 S 3 43.45s /usr/lib/thunderbird/thunderbird 5.60 3.40 2.20 901 S 3 8.06m /usr/bin/X 3.60 2.80 0.80 2250 S 2 4.00m compiz 3.20 2.40 0.80 7134 S 3 1.94m /usr/lib/firefox/firefox 1.60 1.20 0.40 2375 S 2 1.58m /usr/bin/pulseaudio 1.00 0.00 1.00 31036 S 0 1.17s [kworker/0:2] 0.20 0.00 0.20 30774 S 0 1.04s /usr/lib/firefox/plugin-container 0.20 0.20 0.00 2244 S 2 1.68s /usr/lib/unity-settings-daemon/unity-settings-daemon 0.20 0.20 0.00 2263 S 2 2.17s /usr/lib/unity/unity-panel-service 0.20 0.20 0.00 770 S 3 1.85s /usr/bin/dbus-daemon 0.20 0.20 0.00 722 S 1 1.14s /usr/lib/accountsservice/accounts-daemon 0.20 0.00 0.20 30780 S 0 2.05s /opt/google/talkplugin/GoogleTalkPlugin 0.20 0.20 0.00 2292 S 0 0.72s /usr/lib/x86_64-linux-gnu/indicator-messages/indicator-messages-service 0.20 0.20 0.00 2300 S 0 0.90s /usr/lib/x86_64-linux-gnu/indicator-sound/indicator-sound-service 197.20 187.00 10.20 Total Load Avg 1.75 1.14 0.90, Freq Avg. 2.90 GHz, 4 CPUs online 3776.4 Ctxt/s, 1477.4 IRQ/s, 789.0 softIRQ/s, 0.2 new tasks/s, 3 running, 0 blocked %CPU %USR %SYS PID S CPU Time Task (14:09:56) 74.80 74.80 0.00 31078 S 2 2.06m stress-ng 74.20 74.20 0.00 31079 R 1 2.06m stress-ng 18.80 16.60 2.20 31037 S 0 35.19s /usr/lib/firefox/plugin-container 5.00 4.40 0.60 7027 S 3 43.70s /usr/lib/thunderbird/thunderbird 3.00 1.40 1.60 901 S 3 8.07m /usr/bin/X 2.00 1.60 0.40 2250 S 2 4.00m compiz 1.40 0.60 0.80 2375 S 2 1.59m /usr/bin/pulseaudio 0.80 0.80 0.00 7134 S 3 1.94m /usr/lib/firefox/firefox 0.20 0.20 0.00 30774 S 0 1.05s /usr/lib/firefox/plugin-container 0.20 0.20 0.00 30780 S 0 2.06s /opt/google/talkplugin/GoogleTalkPlugin 0.20 0.00 0.20 31036 S 0 1.18s [kworker/0:2] 0.20 0.00 0.20 30763 S 1 3.39s [kworker/u16:1] 0.20 0.00 0.20 31116 R 2 0.01s ./cpustat 181.00 174.80 6.20 Total Load Avg 2.01 1.21 0.92, Freq Avg. 2.93 GHz, 4 CPUs online 3227.0 Ctxt/s, 1315.4 IRQ/s, 723.0 softIRQ/s, 0.2 new tasks/s, 2 running, 0 blocked %CPU %USR %SYS PID S CPU Time Task (14:10:01) 75.80 75.80 0.00 31078 S 2 2.13m stress-ng 74.20 74.00 0.20 31079 R 1 2.12m stress-ng 19.60 18.80 0.80 31037 S 0 36.17s /usr/lib/firefox/plugin-container 2.40 1.60 0.80 901 S 3 8.07m /usr/bin/X 2.20 2.00 0.20 2250 S 2 4.00m compiz 1.60 1.60 0.00 7027 S 3 43.78s /usr/lib/thunderbird/thunderbird 1.40 1.00 0.40 2375 S 2 1.59m /usr/bin/pulseaudio 0.60 0.60 0.00 7134 S 3 1.94m /usr/lib/firefox/firefox 0.20 0.00 0.20 2263 S 2 2.18s /usr/lib/unity/unity-panel-service 0.20 0.00 0.20 3 S 0 0.59s [ksoftirqd/0] 178.20 175.40 2.80 Total Load Avg 1.93 1.20 0.92, Freq Avg. 2.90 GHz, 4 CPUs online 4781.8 Ctxt/s, 1809.4 IRQ/s, 871.8 softIRQ/s, 0.2 new tasks/s, 3 running, 0 blocked %CPU %USR %SYS PID S CPU Time Task (14:10:06) 74.00 73.80 0.20 31078 S 2 2.19m stress-ng 72.80 72.60 0.20 31079 R 1 2.18m stress-ng 18.40 17.00 1.40 31037 S 0 37.09s /usr/lib/firefox/plugin-container 6.00 5.60 0.40 7027 S 3 44.08s /usr/lib/thunderbird/thunderbird 5.60 2.80 2.80 901 S 3 8.07m /usr/bin/X 5.00 4.00 1.00 2250 S 2 4.00m compiz 1.60 1.40 0.20 7134 S 3 1.94m /usr/lib/firefox/firefox 1.40 0.60 0.80 2375 S 2 1.59m /usr/bin/pulseaudio 0.60 0.60 0.00 5719 S 2 22.13s /usr/lib/gnome-terminal/gnome-terminal-server 0.40 0.40 0.00 2430 S 1 2.96s nautilus 0.20 0.20 0.00 2263 S 2 2.19s /usr/lib/unity/unity-panel-service 0.20 0.00 0.20 7 S 0 2.33s [rcu_sched] 0.20 0.00 0.20 30780 S 0 2.07s /opt/google/talkplugin/GoogleTalkPlugin 0.20 0.00 0.20 31036 S 0 1.19s [kworker/0:2] 186.60 179.00 7.60 Total Grand Total (from 5 samples, 25.0 seconds): %CPU %USR %SYS PID S CPU Time Task (14:10:06) 74.13 73.81 0.32 31078 S 2 2.19m stress-ng 73.33 73.01 0.32 31079 R 1 2.18m stress-ng 19.31 17.35 1.96 31037 S 0 37.09s /usr/lib/firefox/plugin-container 7.28 6.68 0.60 7027 S 3 44.08s /usr/lib/thunderbird/thunderbird 4.28 2.48 1.80 901 S 3 8.07m /usr/bin/X 3.24 2.68 0.56 2250 S 2 4.00m compiz 2.24 1.68 0.56 7134 S 3 1.94m /usr/lib/firefox/firefox 1.48 0.84 0.64 2375 S 2 1.59m /usr/bin/pulseaudio 0.60 0.00 0.60 31036 S 0 1.19s [kworker/0:2] 0.20 0.20 0.00 5719 S 2 22.13s /usr/lib/gnome-terminal/gnome-terminal-server 0.12 0.04 0.08 30780 S 0 2.07s /opt/google/talkplugin/GoogleTalkPlugin 0.12 0.08 0.04 30774 S 0 1.05s /usr/lib/firefox/plugin-container 0.12 0.08 0.04 2263 S 2 2.19s /usr/lib/unity/unity-panel-service 0.08 0.08 0.00 2430 S 1 2.96s nautilus 0.08 0.08 0.00 2177 S 3 21.25s /usr/bin/ibus-daemon 0.04 0.00 0.04 31116 R 2 0.01s ./cpustat 0.04 0.00 0.04 30763 S 1 3.39s [kworker/u16:1] 0.04 0.00 0.04 6852 S 3 0.24s [kworker/3:1] 0.04 0.00 0.04 6806 S 2 12.18s [kworker/u16:3] 0.04 0.04 0.00 2300 S 0 0.90s /usr/lib/x86_64-linux-gnu/indicator-sound/indicator-sound-service 0.04 0.04 0.00 2292 S 0 0.72s /usr/lib/x86_64-linux-gnu/indicator-messages/indicator-messages-service 0.04 0.04 0.00 2244 S 2 1.68s /usr/lib/unity-settings-daemon/unity-settings-daemon 0.04 0.04 0.00 2206 S 3 3.44s /usr/lib/ibus/ibus-ui-gtk3 0.04 0.04 0.00 2170 S 1 0.25s upstart-dbus-bridge 0.04 0.04 0.00 770 S 3 1.85s /usr/bin/dbus-daemon 0.04 0.04 0.00 722 S 1 1.14s /usr/lib/accountsservice/accounts-daemon 0.04 0.00 0.04 493 S 2 0.25s [jbd2/sda3-8] 0.04 0.00 0.04 7 S 0 2.33s [rcu_sched] 0.04 0.00 0.04 3 S 0 0.59s [ksoftirqd/0] 187.17 179.37 7.80 Total Distribution of CPU utilisation (per Task): % CPU Utilisation Count (%) 0.00 - 3.79 1086 97.84 3.79 - 7.58 7 0.63 7.58 - 11.37 1 0.09 11.37 - 15.16 1 0.09 15.16 - 18.95 2 0.18 18.95 - 22.74 3 0.27 22.74 - 26.53 0 0.00 26.53 - 30.32 0 0.00 30.32 - 34.11 0 0.00 34.11 - 37.90 0 0.00 37.90 - 41.69 0 0.00 41.69 - 45.48 0 0.00 45.48 - 49.27 0 0.00 49.27 - 53.06 0 0.00 53.06 - 56.85 0 0.00 56.85 - 60.64 0 0.00 60.64 - 64.43 0 0.00 64.43 - 68.22 0 0.00 68.22 - 72.01 3 0.27 72.01 - 75.80 7 0.63 Distribution of CPU utilisation (per CPU): CPU# USR% SYS% 0 17.55 2.76 1 73.17 0.36 2 77.65 1.68 3 11.00 3.00 (C) Colin King, colin.king@canonical.com Fri Aug 14 2015