1 /* vi: set sw=4 ts=4: */
3 * Report CPU and I/O stats, based on sysstat version 9.1.2 by Sebastien Godard
5 * Copyright (C) 2010 Marek Polacek <mmpolacek@gmail.com>
7 * Licensed under GPLv2, see file LICENSE in this source tree.
10 //config: bool "iostat (7.6 kb)"
13 //config: Report CPU and I/O statistics
15 //applet:IF_IOSTAT(APPLET(iostat, BB_DIR_BIN, BB_SUID_DROP))
17 //kbuild:lib-$(CONFIG_IOSTAT) += iostat.o
20 #include <sys/utsname.h> /* struct utsname */
22 //#define debug(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
23 #define debug(fmt, ...) ((void)0)
25 #define MAX_DEVICE_NAME 12
26 #define MAX_DEVICE_NAME_STR "12"
29 typedef unsigned long long cputime_t;
30 typedef long long icputime_t;
31 # define FMT_DATA "ll"
32 # define CPUTIME_MAX (~0ULL)
34 typedef unsigned long cputime_t;
35 typedef long icputime_t;
37 # define CPUTIME_MAX (~0UL)
58 cputime_t vector[N_STATS_CPU];
68 unsigned long long rd_sectors;
69 unsigned long long wr_sectors;
74 typedef struct stats_dev {
75 struct stats_dev *next;
76 char dname[MAX_DEVICE_NAME + 1];
77 stats_dev_data_t prev_data;
78 stats_dev_data_t curr_data;
81 /* Globals. Sort by size and access frequency. */
84 unsigned total_cpus; /* Number of CPUs */
85 unsigned clk_tck; /* Number of clock ticks per second */
86 llist_t *dev_name_list; /* List of devices entered on the command line */
87 stats_dev_t *stats_dev_list;
94 #define G (*ptr_to_globals)
95 #define INIT_G() do { \
96 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
101 /* Must match option string! */
111 static ALWAYS_INLINE int this_is_smp(void)
113 return (G.total_cpus > 1);
116 static void print_header(void)
121 uname(&uts); /* never fails */
123 /* Date representation for the current locale */
124 strftime(buf, sizeof(buf), "%x", &G.tmtime);
126 printf("%s %s (%s) \t%s \t_%s_\t(%u CPU)\n\n",
127 uts.sysname, uts.release, uts.nodename,
128 buf, uts.machine, G.total_cpus);
131 static void get_localtime(struct tm *ptm)
135 localtime_r(&timer, ptm);
138 static void print_timestamp(void)
141 /* %x: date representation for the current locale */
142 /* %X: time representation for the current locale */
143 strftime(buf, sizeof(buf), "%x %X", &G.tmtime);
147 static cputime_t get_smp_uptime(void)
150 unsigned long sec, dec;
152 fp = xfopen_for_read("/proc/uptime");
154 if (fscanf(fp, "%lu.%lu", &sec, &dec) != 2)
155 bb_error_msg_and_die("can't read '%s'", "/proc/uptime");
159 return (cputime_t)sec * G.clk_tck + dec * G.clk_tck / 100;
162 /* Fetch CPU statistics from /proc/stat */
163 static void get_cpu_statistics(stats_cpu_t *sc)
168 fp = xfopen_for_read("/proc/stat");
170 memset(sc, 0, sizeof(*sc));
172 while (fgets(buf, sizeof(buf), fp)) {
176 /* Does the line start with "cpu "? */
177 if (!starts_with_cpu(buf) || buf[3] != ' ') {
181 for (i = STATS_CPU_USER; i <= STATS_CPU_GUEST; i++) {
182 ibuf = skip_whitespace(ibuf);
183 sscanf(ibuf, "%"FMT_DATA"u", &sc->vector[i]);
184 if (i != STATS_CPU_GUEST) {
185 sc->vector[GLOBAL_UPTIME] += sc->vector[i];
187 ibuf = skip_non_whitespace(ibuf);
193 sc->vector[SMP_UPTIME] = get_smp_uptime();
199 static ALWAYS_INLINE cputime_t get_interval(cputime_t old, cputime_t new)
201 cputime_t itv = new - old;
203 return (itv == 0) ? 1 : itv;
206 #if CPUTIME_MAX > 0xffffffff
208 * Handle overflow conditions properly for counters which can have
209 * less bits than cputime_t, depending on the kernel version.
211 /* Surprisingly, on 32bit inlining is a size win */
212 static ALWAYS_INLINE cputime_t overflow_safe_sub(cputime_t prev, cputime_t curr)
214 cputime_t v = curr - prev;
216 if ((icputime_t)v < 0 /* curr < prev - counter overflow? */
217 && prev <= 0xffffffff /* kernel uses 32bit value for the counter? */
219 /* Add 33th bit set to 1 to curr, compensating for the overflow */
220 /* double shift defeats "warning: left shift count >= width of type" */
221 v += ((cputime_t)1 << 16) << 16;
226 static ALWAYS_INLINE cputime_t overflow_safe_sub(cputime_t prev, cputime_t curr)
232 static double percent_value(cputime_t prev, cputime_t curr, cputime_t itv)
234 return ((double)overflow_safe_sub(prev, curr)) / itv * 100;
237 static void print_stats_cpu_struct(stats_cpu_pair_t *stats)
239 cputime_t *p = stats->prev->vector;
240 cputime_t *c = stats->curr->vector;
241 printf(" %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n",
242 percent_value(p[STATS_CPU_USER] , c[STATS_CPU_USER] , stats->itv),
243 percent_value(p[STATS_CPU_NICE] , c[STATS_CPU_NICE] , stats->itv),
244 percent_value(p[STATS_CPU_SYSTEM] + p[STATS_CPU_SOFTIRQ] + p[STATS_CPU_IRQ],
245 c[STATS_CPU_SYSTEM] + c[STATS_CPU_SOFTIRQ] + c[STATS_CPU_IRQ], stats->itv),
246 percent_value(p[STATS_CPU_IOWAIT], c[STATS_CPU_IOWAIT], stats->itv),
247 percent_value(p[STATS_CPU_STEAL] , c[STATS_CPU_STEAL] , stats->itv),
248 percent_value(p[STATS_CPU_IDLE] , c[STATS_CPU_IDLE] , stats->itv)
252 static void cpu_report(stats_cpu_pair_t *stats)
254 /* Always print a header */
255 puts("avg-cpu: %user %nice %system %iowait %steal %idle");
257 /* Print current statistics */
258 print_stats_cpu_struct(stats);
261 static void print_stats_dev_struct(stats_dev_t *stats_dev, cputime_t itv)
263 stats_dev_data_t *p = &stats_dev->prev_data;
264 stats_dev_data_t *c = &stats_dev->curr_data;
265 if (option_mask32 & OPT_z)
266 if (p->rd_ops == c->rd_ops && p->wr_ops == c->wr_ops)
269 printf("%-13s %8.2f %12.2f %12.2f %10llu %10llu\n",
271 percent_value(p->rd_ops + p->wr_ops, c->rd_ops + c->wr_ops, itv),
272 percent_value(p->rd_sectors, c->rd_sectors, itv) / G.unit.div,
273 percent_value(p->wr_sectors, c->wr_sectors, itv) / G.unit.div,
274 (c->rd_sectors - p->rd_sectors) / G.unit.div,
275 (c->wr_sectors - p->wr_sectors) / G.unit.div
279 static void print_devstat_header(void)
281 printf("Device:%15s%6s%s/s%6s%s/s%6s%s%6s%s\n",
283 G.unit.str, "_read", G.unit.str, "_wrtn",
284 G.unit.str, "_read", G.unit.str, "_wrtn"
289 * Is input partition of format [sdaN]?
291 static int is_partition(const char *dev)
293 /* Ok, this is naive... */
294 return ((dev[0] - 's') | (dev[1] - 'd') | (dev[2] - 'a')) == 0 && isdigit(dev[3]);
297 static stats_dev_t *stats_dev_find_or_new(const char *dev_name)
299 stats_dev_t **curr = &G.stats_dev_list;
301 while (*curr != NULL) {
302 if (strcmp((*curr)->dname, dev_name) == 0)
304 curr = &(*curr)->next;
307 *curr = xzalloc(sizeof(stats_dev_t));
308 strncpy((*curr)->dname, dev_name, MAX_DEVICE_NAME);
312 static void stats_dev_free(stats_dev_t *stats_dev)
315 stats_dev_free(stats_dev->next);
320 static void do_disk_statistics(cputime_t itv)
323 char dev_name[MAX_DEVICE_NAME + 1];
324 unsigned long long rd_sec_or_dummy;
325 unsigned long long wr_sec_or_dummy;
326 stats_dev_data_t *curr_data;
327 stats_dev_t *stats_dev;
331 fp = xfopen_for_read("/proc/diskstats");
332 /* Read and possibly print stats from /proc/diskstats */
333 while (fgets(buf, sizeof(buf), fp)) {
334 sscanf(buf, "%*s %*s %"MAX_DEVICE_NAME_STR"s", dev_name);
335 if (G.dev_name_list) {
336 /* Is device name in list? */
337 if (!llist_find_str(G.dev_name_list, dev_name))
339 } else if (is_partition(dev_name)) {
343 stats_dev = stats_dev_find_or_new(dev_name);
344 curr_data = &stats_dev->curr_data;
346 rc = sscanf(buf, "%*s %*s %*s %lu %llu %llu %llu %lu %*s %llu",
349 &curr_data->rd_sectors,
352 &curr_data->wr_sectors);
354 curr_data->rd_sectors = rd_sec_or_dummy;
355 curr_data->wr_sectors = wr_sec_or_dummy;
356 //curr_data->rd_ops = ;
357 curr_data->wr_ops = (unsigned long)curr_data->rd_sectors;
360 if (!G.dev_name_list /* User didn't specify device */
362 && curr_data->rd_ops == 0
363 && curr_data->wr_ops == 0
365 /* Don't print unused device */
369 /* Print current statistics */
370 print_stats_dev_struct(stats_dev, itv);
371 stats_dev->prev_data = *curr_data;
377 static void dev_report(cputime_t itv)
379 /* Always print a header */
380 print_devstat_header();
382 /* Fetch current disk statistics */
383 do_disk_statistics(itv);
386 //usage:#define iostat_trivial_usage
387 //usage: "[-c] [-d] [-t] [-z] [-k|-m] [ALL|BLOCKDEV...] [INTERVAL [COUNT]]"
388 //usage:#define iostat_full_usage "\n\n"
389 //usage: "Report CPU and I/O statistics\n"
390 //usage: "\n -c Show CPU utilization"
391 //usage: "\n -d Show device utilization"
392 //usage: "\n -t Print current time"
393 //usage: "\n -z Omit devices with no activity"
394 //usage: "\n -k Use kb/s"
395 //usage: "\n -m Use Mb/s"
397 int iostat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
398 int iostat_main(int argc UNUSED_PARAM, char **argv)
403 stats_cpu_t stats_data[2];
404 smallint current_stats;
408 memset(&stats_data, 0, sizeof(stats_data));
410 /* Get number of clock ticks per sec */
411 G.clk_tck = bb_clk_tck();
413 /* Determine number of CPUs */
414 G.total_cpus = get_cpu_count();
415 if (G.total_cpus == 0)
418 /* Parse and process arguments */
419 /* -k and -m are mutually exclusive */
420 opt = getopt32(argv, "^" "cdtzkm" "\0" "k--m:m--k");
421 if (!(opt & (OPT_c + OPT_d)))
423 opt |= OPT_c + OPT_d;
427 /* Store device names into device list */
428 while (*argv && !isdigit(*argv[0])) {
429 if (strcmp(*argv, "ALL") != 0) {
430 /* If not ALL, save device name */
431 char *dev_name = skip_dev_pfx(*argv);
432 if (!llist_find_str(G.dev_name_list, dev_name)) {
433 llist_add_to(&G.dev_name_list, dev_name);
445 interval = xatoi_positive(*argv);
446 count = (interval != 0 ? -1 : 1);
449 /* Get count value */
450 count = xatoi_positive(*argv);
463 get_localtime(&G.tmtime);
471 stats_cpu_pair_t stats;
473 stats.prev = &stats_data[current_stats ^ 1];
474 stats.curr = &stats_data[current_stats];
476 /* Fill the time structure */
477 get_localtime(&G.tmtime);
479 /* Fetch current CPU statistics */
480 get_cpu_statistics(stats.curr);
483 stats.itv = get_interval(
484 stats.prev->vector[GLOBAL_UPTIME],
485 stats.curr->vector[GLOBAL_UPTIME]
494 /* Separate outputs by a newline */
500 stats.itv = get_interval(
501 stats.prev->vector[SMP_UPTIME],
502 stats.curr->vector[SMP_UPTIME]
505 dev_report(stats.itv);
521 if (ENABLE_FEATURE_CLEAN_UP) {
522 llist_free(G.dev_name_list, NULL);
523 stats_dev_free(G.stats_dev_list);