blob: 978d23430539b6286413b4bd081b1192e1fd53c1 [file] [log] [blame]
Marek Polacek733f26f2010-08-11 17:00:59 +02001/* vi: set sw=4 ts=4: */
2/*
3 * Report CPU and I/O stats, based on sysstat version 9.1.2 by Sebastien Godard
4 *
5 * Copyright (C) 2010 Marek Polacek <mmpolacek@gmail.com>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2, see file LICENSE in this source tree.
Marek Polacek733f26f2010-08-11 17:00:59 +02008 */
9
Marek Polacek733f26f2010-08-11 17:00:59 +020010//config:config IOSTAT
11//config: bool "iostat"
12//config: default y
13//config: help
14//config: Report CPU and I/O statistics
15
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +020016//applet:IF_IOSTAT(APPLET(iostat, BB_DIR_BIN, BB_SUID_DROP))
17
18//kbuild:lib-$(CONFIG_IOSTAT) += iostat.o
19
Marek Polacek733f26f2010-08-11 17:00:59 +020020#include "libbb.h"
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +020021#include <sys/utsname.h> /* struct utsname */
Marek Polacek733f26f2010-08-11 17:00:59 +020022
Denys Vlasenkodcaed972010-08-12 15:36:34 +020023//#define debug(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__)
24#define debug(fmt, ...) ((void)0)
Marek Polacek733f26f2010-08-11 17:00:59 +020025
Denys Vlasenkofb132e42010-10-29 11:46:52 +020026#define MAX_DEVICE_NAME 12
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +020027#define MAX_DEVICE_NAME_STR "12"
Marek Polacek733f26f2010-08-11 17:00:59 +020028
29#if 1
30typedef unsigned long long cputime_t;
31typedef long long icputime_t;
32# define FMT_DATA "ll"
33# define CPUTIME_MAX (~0ULL)
34#else
35typedef unsigned long cputime_t;
36typedef long icputime_t;
37# define FMT_DATA "l"
38# define CPUTIME_MAX (~0UL)
39#endif
40
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +020041enum {
42 STATS_CPU_USER,
43 STATS_CPU_NICE,
44 STATS_CPU_SYSTEM,
45 STATS_CPU_IDLE,
46 STATS_CPU_IOWAIT,
47 STATS_CPU_IRQ,
48 STATS_CPU_SOFTIRQ,
49 STATS_CPU_STEAL,
50 STATS_CPU_GUEST,
51
52 GLOBAL_UPTIME,
53 SMP_UPTIME,
54
55 N_STATS_CPU,
Marek Polacek733f26f2010-08-11 17:00:59 +020056};
57
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +020058typedef struct {
59 cputime_t vector[N_STATS_CPU];
60} stats_cpu_t;
61
62typedef struct {
63 stats_cpu_t *prev;
64 stats_cpu_t *curr;
65 cputime_t itv;
66} stats_cpu_pair_t;
67
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +020068typedef struct {
Marek Polacek733f26f2010-08-11 17:00:59 +020069 unsigned long long rd_sectors;
70 unsigned long long wr_sectors;
71 unsigned long rd_ops;
72 unsigned long wr_ops;
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +020073} stats_dev_data_t;
74
75typedef struct stats_dev {
76 struct stats_dev *next;
77 char dname[MAX_DEVICE_NAME + 1];
78 stats_dev_data_t prev_data;
79 stats_dev_data_t curr_data;
80} stats_dev_t;
Marek Polacek733f26f2010-08-11 17:00:59 +020081
Marek Polacek733f26f2010-08-11 17:00:59 +020082/* Globals. Sort by size and access frequency. */
83struct globals {
84 smallint show_all;
Marek Polacek733f26f2010-08-11 17:00:59 +020085 unsigned total_cpus; /* Number of CPUs */
86 unsigned clk_tck; /* Number of clock ticks per second */
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +020087 llist_t *dev_name_list; /* List of devices entered on the command line */
88 stats_dev_t *stats_dev_list;
Marek Polacek733f26f2010-08-11 17:00:59 +020089 struct tm tmtime;
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +020090 struct {
91 const char *str;
92 unsigned div;
93 } unit;
Marek Polacek733f26f2010-08-11 17:00:59 +020094};
95#define G (*ptr_to_globals)
96#define INIT_G() do { \
97 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +020098 G.unit.str = "Blk"; \
99 G.unit.div = 1; \
Marek Polacek733f26f2010-08-11 17:00:59 +0200100} while (0)
101
102/* Must match option string! */
103enum {
104 OPT_c = 1 << 0,
105 OPT_d = 1 << 1,
106 OPT_t = 1 << 2,
107 OPT_z = 1 << 3,
108 OPT_k = 1 << 4,
109 OPT_m = 1 << 5,
110};
111
112static ALWAYS_INLINE unsigned get_user_hz(void)
113{
114 return sysconf(_SC_CLK_TCK);
115}
116
117static ALWAYS_INLINE int this_is_smp(void)
118{
119 return (G.total_cpus > 1);
120}
121
122static void print_header(void)
123{
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +0200124 char buf[32];
Marek Polacek733f26f2010-08-11 17:00:59 +0200125 struct utsname uts;
126
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200127 uname(&uts); /* never fails */
Marek Polacek733f26f2010-08-11 17:00:59 +0200128
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200129 /* Date representation for the current locale */
Marek Polacek733f26f2010-08-11 17:00:59 +0200130 strftime(buf, sizeof(buf), "%x", &G.tmtime);
131
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200132 printf("%s %s (%s) \t%s \t_%s_\t(%u CPU)\n\n",
Marek Polacek733f26f2010-08-11 17:00:59 +0200133 uts.sysname, uts.release, uts.nodename,
134 buf, uts.machine, G.total_cpus);
135}
136
Marek Polacek733f26f2010-08-11 17:00:59 +0200137static void get_localtime(struct tm *ptm)
138{
139 time_t timer;
140 time(&timer);
141 localtime_r(&timer, ptm);
142}
143
144static void print_timestamp(void)
145{
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +0200146 char buf[64];
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200147 /* %x: date representation for the current locale */
148 /* %X: time representation for the current locale */
Marek Polacek733f26f2010-08-11 17:00:59 +0200149 strftime(buf, sizeof(buf), "%x %X", &G.tmtime);
150 printf("%s\n", buf);
151}
152
Denys Vlasenkoc3f8db92011-05-07 04:36:46 +0200153static cputime_t get_smp_uptime(void)
154{
155 FILE *fp;
Denys Vlasenkoc3f8db92011-05-07 04:36:46 +0200156 unsigned long sec, dec;
157
158 fp = xfopen_for_read("/proc/uptime");
159
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200160 if (fscanf(fp, "%lu.%lu", &sec, &dec) != 2)
161 bb_error_msg_and_die("can't read '%s'", "/proc/uptime");
Denys Vlasenkoc3f8db92011-05-07 04:36:46 +0200162
163 fclose(fp);
164
165 return (cputime_t)sec * G.clk_tck + dec * G.clk_tck / 100;
166}
167
Marek Polacek733f26f2010-08-11 17:00:59 +0200168/* Fetch CPU statistics from /proc/stat */
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200169static void get_cpu_statistics(stats_cpu_t *sc)
Marek Polacek733f26f2010-08-11 17:00:59 +0200170{
171 FILE *fp;
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +0200172 char buf[1024];
Marek Polacek733f26f2010-08-11 17:00:59 +0200173
174 fp = xfopen_for_read("/proc/stat");
175
176 memset(sc, 0, sizeof(*sc));
177
178 while (fgets(buf, sizeof(buf), fp)) {
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +0200179 int i;
180 char *ibuf;
181
182 /* Does the line start with "cpu "? */
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200183 if (!starts_with_cpu(buf) || buf[3] != ' ') {
184 continue;
Marek Polacek733f26f2010-08-11 17:00:59 +0200185 }
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +0200186 ibuf = buf + 4;
187 for (i = STATS_CPU_USER; i <= STATS_CPU_GUEST; i++) {
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200188 ibuf = skip_whitespace(ibuf);
189 sscanf(ibuf, "%"FMT_DATA"u", &sc->vector[i]);
190 if (i != STATS_CPU_GUEST) {
191 sc->vector[GLOBAL_UPTIME] += sc->vector[i];
192 }
193 ibuf = skip_non_whitespace(ibuf);
194 }
195 break;
196 }
197
198 if (this_is_smp()) {
199 sc->vector[SMP_UPTIME] = get_smp_uptime();
Marek Polacek733f26f2010-08-11 17:00:59 +0200200 }
201
202 fclose(fp);
203}
204
Marek Polacek733f26f2010-08-11 17:00:59 +0200205static ALWAYS_INLINE cputime_t get_interval(cputime_t old, cputime_t new)
206{
207 cputime_t itv = new - old;
208
209 return (itv == 0) ? 1 : itv;
210}
211
212#if CPUTIME_MAX > 0xffffffff
213/*
214 * Handle overflow conditions properly for counters which can have
215 * less bits than cputime_t, depending on the kernel version.
216 */
217/* Surprisingly, on 32bit inlining is a size win */
218static ALWAYS_INLINE cputime_t overflow_safe_sub(cputime_t prev, cputime_t curr)
219{
220 cputime_t v = curr - prev;
221
222 if ((icputime_t)v < 0 /* curr < prev - counter overflow? */
223 && prev <= 0xffffffff /* kernel uses 32bit value for the counter? */
224 ) {
225 /* Add 33th bit set to 1 to curr, compensating for the overflow */
226 /* double shift defeats "warning: left shift count >= width of type" */
227 v += ((cputime_t)1 << 16) << 16;
228 }
229 return v;
230}
231#else
232static ALWAYS_INLINE cputime_t overflow_safe_sub(cputime_t prev, cputime_t curr)
233{
234 return curr - prev;
235}
236#endif
237
238static double percent_value(cputime_t prev, cputime_t curr, cputime_t itv)
239{
240 return ((double)overflow_safe_sub(prev, curr)) / itv * 100;
241}
242
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200243static void print_stats_cpu_struct(stats_cpu_pair_t *stats)
Marek Polacek733f26f2010-08-11 17:00:59 +0200244{
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200245 cputime_t *p = stats->prev->vector;
246 cputime_t *c = stats->curr->vector;
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +0200247 printf(" %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n",
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200248 percent_value(p[STATS_CPU_USER] , c[STATS_CPU_USER] , stats->itv),
249 percent_value(p[STATS_CPU_NICE] , c[STATS_CPU_NICE] , stats->itv),
250 percent_value(p[STATS_CPU_SYSTEM] + p[STATS_CPU_SOFTIRQ] + p[STATS_CPU_IRQ],
251 c[STATS_CPU_SYSTEM] + c[STATS_CPU_SOFTIRQ] + c[STATS_CPU_IRQ], stats->itv),
252 percent_value(p[STATS_CPU_IOWAIT], c[STATS_CPU_IOWAIT], stats->itv),
253 percent_value(p[STATS_CPU_STEAL] , c[STATS_CPU_STEAL] , stats->itv),
254 percent_value(p[STATS_CPU_IDLE] , c[STATS_CPU_IDLE] , stats->itv)
Marek Polacek733f26f2010-08-11 17:00:59 +0200255 );
256}
257
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200258static void cpu_report(stats_cpu_pair_t *stats)
Marek Polacek733f26f2010-08-11 17:00:59 +0200259{
260 /* Always print a header */
261 puts("avg-cpu: %user %nice %system %iowait %steal %idle");
262
263 /* Print current statistics */
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200264 print_stats_cpu_struct(stats);
Marek Polacek733f26f2010-08-11 17:00:59 +0200265}
266
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +0200267static void print_stats_dev_struct(stats_dev_t *stats_dev, cputime_t itv)
268{
269 stats_dev_data_t *p = &stats_dev->prev_data;
270 stats_dev_data_t *c = &stats_dev->curr_data;
271 if (option_mask32 & OPT_z)
272 if (p->rd_ops == c->rd_ops && p->wr_ops == c->wr_ops)
273 return;
274
275 printf("%-13s %8.2f %12.2f %12.2f %10llu %10llu\n",
276 stats_dev->dname,
277 percent_value(p->rd_ops + p->wr_ops, c->rd_ops + c->wr_ops, itv),
278 percent_value(p->rd_sectors, c->rd_sectors, itv) / G.unit.div,
279 percent_value(p->wr_sectors, c->wr_sectors, itv) / G.unit.div,
280 (c->rd_sectors - p->rd_sectors) / G.unit.div,
281 (c->wr_sectors - p->wr_sectors) / G.unit.div
282 );
283}
284
Marek Polacek733f26f2010-08-11 17:00:59 +0200285static void print_devstat_header(void)
286{
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +0200287 printf("Device:%15s%6s%s/s%6s%s/s%6s%s%6s%s\n",
288 "tps",
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200289 G.unit.str, "_read", G.unit.str, "_wrtn",
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +0200290 G.unit.str, "_read", G.unit.str, "_wrtn"
291 );
Marek Polacek733f26f2010-08-11 17:00:59 +0200292}
293
294/*
295 * Is input partition of format [sdaN]?
296 */
297static int is_partition(const char *dev)
298{
299 /* Ok, this is naive... */
300 return ((dev[0] - 's') | (dev[1] - 'd') | (dev[2] - 'a')) == 0 && isdigit(dev[3]);
301}
302
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +0200303static stats_dev_t *stats_dev_find_or_new(const char *dev_name)
304{
305 stats_dev_t **curr = &G.stats_dev_list;
306
307 while (*curr != NULL) {
308 if (strcmp((*curr)->dname, dev_name) == 0)
309 return *curr;
310 curr = &(*curr)->next;
311 }
312
313 *curr = xzalloc(sizeof(stats_dev_t));
314 strncpy((*curr)->dname, dev_name, MAX_DEVICE_NAME);
315 return *curr;
316}
317
318static void stats_dev_free(stats_dev_t *stats_dev)
319{
320 if (stats_dev) {
321 stats_dev_free(stats_dev->next);
322 free(stats_dev);
323 }
324}
325
Marek Polacek733f26f2010-08-11 17:00:59 +0200326static void do_disk_statistics(cputime_t itv)
327{
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +0200328 char buf[128];
329 char dev_name[MAX_DEVICE_NAME + 1];
330 unsigned long long rd_sec_or_dummy;
331 unsigned long long wr_sec_or_dummy;
332 stats_dev_data_t *curr_data;
333 stats_dev_t *stats_dev;
Marek Polacek733f26f2010-08-11 17:00:59 +0200334 FILE *fp;
335 int rc;
Marek Polacek733f26f2010-08-11 17:00:59 +0200336
337 fp = xfopen_for_read("/proc/diskstats");
Marek Polacek733f26f2010-08-11 17:00:59 +0200338 /* Read and possibly print stats from /proc/diskstats */
339 while (fgets(buf, sizeof(buf), fp)) {
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +0200340 sscanf(buf, "%*s %*s %"MAX_DEVICE_NAME_STR"s", dev_name);
341 if (G.dev_name_list) {
342 /* Is device name in list? */
343 if (!llist_find_str(G.dev_name_list, dev_name))
344 continue;
345 } else if (is_partition(dev_name)) {
346 continue;
Marek Polacek733f26f2010-08-11 17:00:59 +0200347 }
348
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +0200349 stats_dev = stats_dev_find_or_new(dev_name);
350 curr_data = &stats_dev->curr_data;
351
352 rc = sscanf(buf, "%*s %*s %*s %lu %llu %llu %llu %lu %*s %llu",
353 &curr_data->rd_ops,
354 &rd_sec_or_dummy,
355 &curr_data->rd_sectors,
356 &wr_sec_or_dummy,
357 &curr_data->wr_ops,
358 &curr_data->wr_sectors);
359 if (rc != 6) {
360 curr_data->rd_sectors = rd_sec_or_dummy;
361 curr_data->wr_sectors = wr_sec_or_dummy;
362 //curr_data->rd_ops = ;
363 curr_data->wr_ops = (unsigned long)curr_data->rd_sectors;
Marek Polacek733f26f2010-08-11 17:00:59 +0200364 }
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +0200365
366 if (!G.dev_name_list /* User didn't specify device */
367 && !G.show_all
368 && curr_data->rd_ops == 0
369 && curr_data->wr_ops == 0
370 ) {
371 /* Don't print unused device */
372 continue;
373 }
374
375 /* Print current statistics */
376 print_stats_dev_struct(stats_dev, itv);
377 stats_dev->prev_data = *curr_data;
Marek Polacek733f26f2010-08-11 17:00:59 +0200378 }
Denys Vlasenkodcaed972010-08-12 15:36:34 +0200379
380 fclose(fp);
Marek Polacek733f26f2010-08-11 17:00:59 +0200381}
382
383static void dev_report(cputime_t itv)
384{
385 /* Always print a header */
386 print_devstat_header();
387
388 /* Fetch current disk statistics */
389 do_disk_statistics(itv);
390}
391
Marek Polacek733f26f2010-08-11 17:00:59 +0200392//usage:#define iostat_trivial_usage
393//usage: "[-c] [-d] [-t] [-z] [-k|-m] [ALL|BLOCKDEV...] [INTERVAL [COUNT]]"
394//usage:#define iostat_full_usage "\n\n"
395//usage: "Report CPU and I/O statistics\n"
Marek Polacek733f26f2010-08-11 17:00:59 +0200396//usage: "\n -c Show CPU utilization"
397//usage: "\n -d Show device utilization"
398//usage: "\n -t Print current time"
399//usage: "\n -z Omit devices with no activity"
400//usage: "\n -k Use kb/s"
401//usage: "\n -m Use Mb/s"
402
403int iostat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200404int iostat_main(int argc UNUSED_PARAM, char **argv)
Marek Polacek733f26f2010-08-11 17:00:59 +0200405{
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +0200406 int opt;
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200407 unsigned interval;
Denys Vlasenkodcaed972010-08-12 15:36:34 +0200408 int count;
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200409 stats_cpu_t stats_data[2];
410 smallint current_stats;
Marek Polacek733f26f2010-08-11 17:00:59 +0200411
412 INIT_G();
413
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200414 memset(&stats_data, 0, sizeof(stats_data));
Marek Polacek733f26f2010-08-11 17:00:59 +0200415
416 /* Get number of clock ticks per sec */
417 G.clk_tck = get_user_hz();
418
419 /* Determine number of CPUs */
Denys Vlasenkoc9b97502010-08-16 02:49:21 +0200420 G.total_cpus = get_cpu_count();
421 if (G.total_cpus == 0)
422 G.total_cpus = 1;
Marek Polacek733f26f2010-08-11 17:00:59 +0200423
424 /* Parse and process arguments */
425 /* -k and -m are mutually exclusive */
426 opt_complementary = "k--m:m--k";
427 opt = getopt32(argv, "cdtzkm");
428 if (!(opt & (OPT_c + OPT_d)))
429 /* Default is -cd */
430 opt |= OPT_c + OPT_d;
431
432 argv += optind;
Marek Polacek733f26f2010-08-11 17:00:59 +0200433
434 /* Store device names into device list */
435 while (*argv && !isdigit(*argv[0])) {
Denys Vlasenkoc3f8db92011-05-07 04:36:46 +0200436 if (strcmp(*argv, "ALL") != 0) {
Marek Polacek733f26f2010-08-11 17:00:59 +0200437 /* If not ALL, save device name */
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200438 char *dev_name = skip_dev_pfx(*argv);
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +0200439 if (!llist_find_str(G.dev_name_list, dev_name)) {
440 llist_add_to(&G.dev_name_list, dev_name);
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200441 }
Denys Vlasenkoc3f8db92011-05-07 04:36:46 +0200442 } else {
Marek Polacek733f26f2010-08-11 17:00:59 +0200443 G.show_all = 1;
Denys Vlasenkoc3f8db92011-05-07 04:36:46 +0200444 }
Marek Polacek733f26f2010-08-11 17:00:59 +0200445 argv++;
446 }
447
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200448 interval = 0;
Denys Vlasenkodcaed972010-08-12 15:36:34 +0200449 count = 1;
Marek Polacek733f26f2010-08-11 17:00:59 +0200450 if (*argv) {
451 /* Get interval */
Denys Vlasenko77832482010-08-12 14:14:45 +0200452 interval = xatoi_positive(*argv);
Denys Vlasenkodcaed972010-08-12 15:36:34 +0200453 count = (interval != 0 ? -1 : 1);
Marek Polacek733f26f2010-08-11 17:00:59 +0200454 argv++;
455 if (*argv)
456 /* Get count value */
Denys Vlasenko77832482010-08-12 14:14:45 +0200457 count = xatoi_positive(*argv);
Marek Polacek733f26f2010-08-11 17:00:59 +0200458 }
459
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200460 if (opt & OPT_m) {
461 G.unit.str = " MB";
462 G.unit.div = 2048;
463 }
464
465 if (opt & OPT_k) {
466 G.unit.str = " kB";
467 G.unit.div = 2;
468 }
469
470 get_localtime(&G.tmtime);
471
Marek Polacek733f26f2010-08-11 17:00:59 +0200472 /* Display header */
473 print_header();
474
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200475 current_stats = 0;
Marek Polacek733f26f2010-08-11 17:00:59 +0200476 /* Main loop */
477 for (;;) {
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200478 stats_cpu_pair_t stats;
479
480 stats.prev = &stats_data[current_stats ^ 1];
481 stats.curr = &stats_data[current_stats];
482
Marek Polacek733f26f2010-08-11 17:00:59 +0200483 /* Fill the time structure */
484 get_localtime(&G.tmtime);
485
486 /* Fetch current CPU statistics */
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200487 get_cpu_statistics(stats.curr);
Marek Polacek733f26f2010-08-11 17:00:59 +0200488
489 /* Get interval */
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200490 stats.itv = get_interval(
491 stats.prev->vector[GLOBAL_UPTIME],
492 stats.curr->vector[GLOBAL_UPTIME]
493 );
Marek Polacek733f26f2010-08-11 17:00:59 +0200494
495 if (opt & OPT_t)
496 print_timestamp();
497
498 if (opt & OPT_c) {
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200499 cpu_report(&stats);
Marek Polacek733f26f2010-08-11 17:00:59 +0200500 if (opt & OPT_d)
501 /* Separate outputs by a newline */
502 bb_putchar('\n');
503 }
504
505 if (opt & OPT_d) {
506 if (this_is_smp()) {
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200507 stats.itv = get_interval(
508 stats.prev->vector[SMP_UPTIME],
509 stats.curr->vector[SMP_UPTIME]
510 );
Marek Polacek733f26f2010-08-11 17:00:59 +0200511 }
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200512 dev_report(stats.itv);
Marek Polacek733f26f2010-08-11 17:00:59 +0200513 }
514
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200515 bb_putchar('\n');
516
Marek Polacek733f26f2010-08-11 17:00:59 +0200517 if (count > 0) {
518 if (--count == 0)
519 break;
520 }
521
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200522 /* Swap stats */
523 current_stats ^= 1;
Marek Polacek733f26f2010-08-11 17:00:59 +0200524
Marek Polacek733f26f2010-08-11 17:00:59 +0200525 sleep(interval);
526 }
527
Marek Polacek733f26f2010-08-11 17:00:59 +0200528 if (ENABLE_FEATURE_CLEAN_UP) {
Maksym Kryzhanovskyy9f778022011-06-05 03:37:14 +0200529 llist_free(G.dev_name_list, NULL);
530 stats_dev_free(G.stats_dev_list);
Maksym Kryzhanovskyy29c54aa2011-05-07 04:37:22 +0200531 free(&G);
Marek Polacek733f26f2010-08-11 17:00:59 +0200532 }
533
534 return EXIT_SUCCESS;
535}