Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 1 | /* 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 Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 10 | //config:config IOSTAT |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 11 | //config: bool "iostat (7.4 kb)" |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 12 | //config: default y |
| 13 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 14 | //config: Report CPU and I/O statistics |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 15 | |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 16 | //applet:IF_IOSTAT(APPLET(iostat, BB_DIR_BIN, BB_SUID_DROP)) |
| 17 | |
| 18 | //kbuild:lib-$(CONFIG_IOSTAT) += iostat.o |
| 19 | |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 20 | #include "libbb.h" |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 21 | #include <sys/utsname.h> /* struct utsname */ |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 22 | |
Denys Vlasenko | dcaed97 | 2010-08-12 15:36:34 +0200 | [diff] [blame] | 23 | //#define debug(fmt, ...) fprintf(stderr, fmt, ## __VA_ARGS__) |
| 24 | #define debug(fmt, ...) ((void)0) |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 25 | |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 26 | #define MAX_DEVICE_NAME 12 |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 27 | #define MAX_DEVICE_NAME_STR "12" |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 28 | |
| 29 | #if 1 |
| 30 | typedef unsigned long long cputime_t; |
| 31 | typedef long long icputime_t; |
| 32 | # define FMT_DATA "ll" |
| 33 | # define CPUTIME_MAX (~0ULL) |
| 34 | #else |
| 35 | typedef unsigned long cputime_t; |
| 36 | typedef long icputime_t; |
| 37 | # define FMT_DATA "l" |
| 38 | # define CPUTIME_MAX (~0UL) |
| 39 | #endif |
| 40 | |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 41 | enum { |
| 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 Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 56 | }; |
| 57 | |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 58 | typedef struct { |
| 59 | cputime_t vector[N_STATS_CPU]; |
| 60 | } stats_cpu_t; |
| 61 | |
| 62 | typedef struct { |
| 63 | stats_cpu_t *prev; |
| 64 | stats_cpu_t *curr; |
| 65 | cputime_t itv; |
| 66 | } stats_cpu_pair_t; |
| 67 | |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 68 | typedef struct { |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 69 | unsigned long long rd_sectors; |
| 70 | unsigned long long wr_sectors; |
| 71 | unsigned long rd_ops; |
| 72 | unsigned long wr_ops; |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 73 | } stats_dev_data_t; |
| 74 | |
| 75 | typedef 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 Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 81 | |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 82 | /* Globals. Sort by size and access frequency. */ |
| 83 | struct globals { |
| 84 | smallint show_all; |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 85 | unsigned total_cpus; /* Number of CPUs */ |
| 86 | unsigned clk_tck; /* Number of clock ticks per second */ |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 87 | llist_t *dev_name_list; /* List of devices entered on the command line */ |
| 88 | stats_dev_t *stats_dev_list; |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 89 | struct tm tmtime; |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 90 | struct { |
| 91 | const char *str; |
| 92 | unsigned div; |
| 93 | } unit; |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 94 | }; |
| 95 | #define G (*ptr_to_globals) |
| 96 | #define INIT_G() do { \ |
| 97 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 98 | G.unit.str = "Blk"; \ |
| 99 | G.unit.div = 1; \ |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 100 | } while (0) |
| 101 | |
| 102 | /* Must match option string! */ |
| 103 | enum { |
| 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 | |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 112 | static ALWAYS_INLINE int this_is_smp(void) |
| 113 | { |
| 114 | return (G.total_cpus > 1); |
| 115 | } |
| 116 | |
| 117 | static void print_header(void) |
| 118 | { |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 119 | char buf[32]; |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 120 | struct utsname uts; |
| 121 | |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 122 | uname(&uts); /* never fails */ |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 123 | |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 124 | /* Date representation for the current locale */ |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 125 | strftime(buf, sizeof(buf), "%x", &G.tmtime); |
| 126 | |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 127 | printf("%s %s (%s) \t%s \t_%s_\t(%u CPU)\n\n", |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 128 | uts.sysname, uts.release, uts.nodename, |
| 129 | buf, uts.machine, G.total_cpus); |
| 130 | } |
| 131 | |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 132 | static void get_localtime(struct tm *ptm) |
| 133 | { |
| 134 | time_t timer; |
| 135 | time(&timer); |
| 136 | localtime_r(&timer, ptm); |
| 137 | } |
| 138 | |
| 139 | static void print_timestamp(void) |
| 140 | { |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 141 | char buf[64]; |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 142 | /* %x: date representation for the current locale */ |
| 143 | /* %X: time representation for the current locale */ |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 144 | strftime(buf, sizeof(buf), "%x %X", &G.tmtime); |
Denys Vlasenko | d60752f | 2015-10-07 22:42:45 +0200 | [diff] [blame] | 145 | puts(buf); |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Denys Vlasenko | c3f8db9 | 2011-05-07 04:36:46 +0200 | [diff] [blame] | 148 | static cputime_t get_smp_uptime(void) |
| 149 | { |
| 150 | FILE *fp; |
Denys Vlasenko | c3f8db9 | 2011-05-07 04:36:46 +0200 | [diff] [blame] | 151 | unsigned long sec, dec; |
| 152 | |
| 153 | fp = xfopen_for_read("/proc/uptime"); |
| 154 | |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 155 | if (fscanf(fp, "%lu.%lu", &sec, &dec) != 2) |
| 156 | bb_error_msg_and_die("can't read '%s'", "/proc/uptime"); |
Denys Vlasenko | c3f8db9 | 2011-05-07 04:36:46 +0200 | [diff] [blame] | 157 | |
| 158 | fclose(fp); |
| 159 | |
| 160 | return (cputime_t)sec * G.clk_tck + dec * G.clk_tck / 100; |
| 161 | } |
| 162 | |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 163 | /* Fetch CPU statistics from /proc/stat */ |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 164 | static void get_cpu_statistics(stats_cpu_t *sc) |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 165 | { |
| 166 | FILE *fp; |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 167 | char buf[1024]; |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 168 | |
| 169 | fp = xfopen_for_read("/proc/stat"); |
| 170 | |
| 171 | memset(sc, 0, sizeof(*sc)); |
| 172 | |
| 173 | while (fgets(buf, sizeof(buf), fp)) { |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 174 | int i; |
| 175 | char *ibuf; |
| 176 | |
| 177 | /* Does the line start with "cpu "? */ |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 178 | if (!starts_with_cpu(buf) || buf[3] != ' ') { |
| 179 | continue; |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 180 | } |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 181 | ibuf = buf + 4; |
| 182 | for (i = STATS_CPU_USER; i <= STATS_CPU_GUEST; i++) { |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 183 | ibuf = skip_whitespace(ibuf); |
| 184 | sscanf(ibuf, "%"FMT_DATA"u", &sc->vector[i]); |
| 185 | if (i != STATS_CPU_GUEST) { |
| 186 | sc->vector[GLOBAL_UPTIME] += sc->vector[i]; |
| 187 | } |
| 188 | ibuf = skip_non_whitespace(ibuf); |
| 189 | } |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | if (this_is_smp()) { |
| 194 | sc->vector[SMP_UPTIME] = get_smp_uptime(); |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | fclose(fp); |
| 198 | } |
| 199 | |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 200 | static ALWAYS_INLINE cputime_t get_interval(cputime_t old, cputime_t new) |
| 201 | { |
| 202 | cputime_t itv = new - old; |
| 203 | |
| 204 | return (itv == 0) ? 1 : itv; |
| 205 | } |
| 206 | |
| 207 | #if CPUTIME_MAX > 0xffffffff |
| 208 | /* |
| 209 | * Handle overflow conditions properly for counters which can have |
| 210 | * less bits than cputime_t, depending on the kernel version. |
| 211 | */ |
| 212 | /* Surprisingly, on 32bit inlining is a size win */ |
| 213 | static ALWAYS_INLINE cputime_t overflow_safe_sub(cputime_t prev, cputime_t curr) |
| 214 | { |
| 215 | cputime_t v = curr - prev; |
| 216 | |
| 217 | if ((icputime_t)v < 0 /* curr < prev - counter overflow? */ |
| 218 | && prev <= 0xffffffff /* kernel uses 32bit value for the counter? */ |
| 219 | ) { |
| 220 | /* Add 33th bit set to 1 to curr, compensating for the overflow */ |
| 221 | /* double shift defeats "warning: left shift count >= width of type" */ |
| 222 | v += ((cputime_t)1 << 16) << 16; |
| 223 | } |
| 224 | return v; |
| 225 | } |
| 226 | #else |
| 227 | static ALWAYS_INLINE cputime_t overflow_safe_sub(cputime_t prev, cputime_t curr) |
| 228 | { |
| 229 | return curr - prev; |
| 230 | } |
| 231 | #endif |
| 232 | |
| 233 | static double percent_value(cputime_t prev, cputime_t curr, cputime_t itv) |
| 234 | { |
| 235 | return ((double)overflow_safe_sub(prev, curr)) / itv * 100; |
| 236 | } |
| 237 | |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 238 | static void print_stats_cpu_struct(stats_cpu_pair_t *stats) |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 239 | { |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 240 | cputime_t *p = stats->prev->vector; |
| 241 | cputime_t *c = stats->curr->vector; |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 242 | printf(" %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n", |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 243 | percent_value(p[STATS_CPU_USER] , c[STATS_CPU_USER] , stats->itv), |
| 244 | percent_value(p[STATS_CPU_NICE] , c[STATS_CPU_NICE] , stats->itv), |
| 245 | percent_value(p[STATS_CPU_SYSTEM] + p[STATS_CPU_SOFTIRQ] + p[STATS_CPU_IRQ], |
| 246 | c[STATS_CPU_SYSTEM] + c[STATS_CPU_SOFTIRQ] + c[STATS_CPU_IRQ], stats->itv), |
| 247 | percent_value(p[STATS_CPU_IOWAIT], c[STATS_CPU_IOWAIT], stats->itv), |
| 248 | percent_value(p[STATS_CPU_STEAL] , c[STATS_CPU_STEAL] , stats->itv), |
| 249 | percent_value(p[STATS_CPU_IDLE] , c[STATS_CPU_IDLE] , stats->itv) |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 250 | ); |
| 251 | } |
| 252 | |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 253 | static void cpu_report(stats_cpu_pair_t *stats) |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 254 | { |
| 255 | /* Always print a header */ |
| 256 | puts("avg-cpu: %user %nice %system %iowait %steal %idle"); |
| 257 | |
| 258 | /* Print current statistics */ |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 259 | print_stats_cpu_struct(stats); |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 260 | } |
| 261 | |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 262 | static void print_stats_dev_struct(stats_dev_t *stats_dev, cputime_t itv) |
| 263 | { |
| 264 | stats_dev_data_t *p = &stats_dev->prev_data; |
| 265 | stats_dev_data_t *c = &stats_dev->curr_data; |
| 266 | if (option_mask32 & OPT_z) |
| 267 | if (p->rd_ops == c->rd_ops && p->wr_ops == c->wr_ops) |
| 268 | return; |
| 269 | |
| 270 | printf("%-13s %8.2f %12.2f %12.2f %10llu %10llu\n", |
| 271 | stats_dev->dname, |
| 272 | percent_value(p->rd_ops + p->wr_ops, c->rd_ops + c->wr_ops, itv), |
| 273 | percent_value(p->rd_sectors, c->rd_sectors, itv) / G.unit.div, |
| 274 | percent_value(p->wr_sectors, c->wr_sectors, itv) / G.unit.div, |
| 275 | (c->rd_sectors - p->rd_sectors) / G.unit.div, |
| 276 | (c->wr_sectors - p->wr_sectors) / G.unit.div |
| 277 | ); |
| 278 | } |
| 279 | |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 280 | static void print_devstat_header(void) |
| 281 | { |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 282 | printf("Device:%15s%6s%s/s%6s%s/s%6s%s%6s%s\n", |
| 283 | "tps", |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 284 | G.unit.str, "_read", G.unit.str, "_wrtn", |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 285 | G.unit.str, "_read", G.unit.str, "_wrtn" |
| 286 | ); |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Is input partition of format [sdaN]? |
| 291 | */ |
| 292 | static int is_partition(const char *dev) |
| 293 | { |
| 294 | /* Ok, this is naive... */ |
| 295 | return ((dev[0] - 's') | (dev[1] - 'd') | (dev[2] - 'a')) == 0 && isdigit(dev[3]); |
| 296 | } |
| 297 | |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 298 | static stats_dev_t *stats_dev_find_or_new(const char *dev_name) |
| 299 | { |
| 300 | stats_dev_t **curr = &G.stats_dev_list; |
| 301 | |
| 302 | while (*curr != NULL) { |
| 303 | if (strcmp((*curr)->dname, dev_name) == 0) |
| 304 | return *curr; |
| 305 | curr = &(*curr)->next; |
| 306 | } |
| 307 | |
| 308 | *curr = xzalloc(sizeof(stats_dev_t)); |
| 309 | strncpy((*curr)->dname, dev_name, MAX_DEVICE_NAME); |
| 310 | return *curr; |
| 311 | } |
| 312 | |
| 313 | static void stats_dev_free(stats_dev_t *stats_dev) |
| 314 | { |
| 315 | if (stats_dev) { |
| 316 | stats_dev_free(stats_dev->next); |
| 317 | free(stats_dev); |
| 318 | } |
| 319 | } |
| 320 | |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 321 | static void do_disk_statistics(cputime_t itv) |
| 322 | { |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 323 | char buf[128]; |
| 324 | char dev_name[MAX_DEVICE_NAME + 1]; |
| 325 | unsigned long long rd_sec_or_dummy; |
| 326 | unsigned long long wr_sec_or_dummy; |
| 327 | stats_dev_data_t *curr_data; |
| 328 | stats_dev_t *stats_dev; |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 329 | FILE *fp; |
| 330 | int rc; |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 331 | |
| 332 | fp = xfopen_for_read("/proc/diskstats"); |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 333 | /* Read and possibly print stats from /proc/diskstats */ |
| 334 | while (fgets(buf, sizeof(buf), fp)) { |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 335 | sscanf(buf, "%*s %*s %"MAX_DEVICE_NAME_STR"s", dev_name); |
| 336 | if (G.dev_name_list) { |
| 337 | /* Is device name in list? */ |
| 338 | if (!llist_find_str(G.dev_name_list, dev_name)) |
| 339 | continue; |
| 340 | } else if (is_partition(dev_name)) { |
| 341 | continue; |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 344 | stats_dev = stats_dev_find_or_new(dev_name); |
| 345 | curr_data = &stats_dev->curr_data; |
| 346 | |
| 347 | rc = sscanf(buf, "%*s %*s %*s %lu %llu %llu %llu %lu %*s %llu", |
| 348 | &curr_data->rd_ops, |
| 349 | &rd_sec_or_dummy, |
| 350 | &curr_data->rd_sectors, |
| 351 | &wr_sec_or_dummy, |
| 352 | &curr_data->wr_ops, |
| 353 | &curr_data->wr_sectors); |
| 354 | if (rc != 6) { |
| 355 | curr_data->rd_sectors = rd_sec_or_dummy; |
| 356 | curr_data->wr_sectors = wr_sec_or_dummy; |
| 357 | //curr_data->rd_ops = ; |
| 358 | curr_data->wr_ops = (unsigned long)curr_data->rd_sectors; |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 359 | } |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 360 | |
| 361 | if (!G.dev_name_list /* User didn't specify device */ |
| 362 | && !G.show_all |
| 363 | && curr_data->rd_ops == 0 |
| 364 | && curr_data->wr_ops == 0 |
| 365 | ) { |
| 366 | /* Don't print unused device */ |
| 367 | continue; |
| 368 | } |
| 369 | |
| 370 | /* Print current statistics */ |
| 371 | print_stats_dev_struct(stats_dev, itv); |
| 372 | stats_dev->prev_data = *curr_data; |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 373 | } |
Denys Vlasenko | dcaed97 | 2010-08-12 15:36:34 +0200 | [diff] [blame] | 374 | |
| 375 | fclose(fp); |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | static void dev_report(cputime_t itv) |
| 379 | { |
| 380 | /* Always print a header */ |
| 381 | print_devstat_header(); |
| 382 | |
| 383 | /* Fetch current disk statistics */ |
| 384 | do_disk_statistics(itv); |
| 385 | } |
| 386 | |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 387 | //usage:#define iostat_trivial_usage |
| 388 | //usage: "[-c] [-d] [-t] [-z] [-k|-m] [ALL|BLOCKDEV...] [INTERVAL [COUNT]]" |
| 389 | //usage:#define iostat_full_usage "\n\n" |
| 390 | //usage: "Report CPU and I/O statistics\n" |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 391 | //usage: "\n -c Show CPU utilization" |
| 392 | //usage: "\n -d Show device utilization" |
| 393 | //usage: "\n -t Print current time" |
| 394 | //usage: "\n -z Omit devices with no activity" |
| 395 | //usage: "\n -k Use kb/s" |
| 396 | //usage: "\n -m Use Mb/s" |
| 397 | |
| 398 | int iostat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 399 | int iostat_main(int argc UNUSED_PARAM, char **argv) |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 400 | { |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 401 | int opt; |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 402 | unsigned interval; |
Denys Vlasenko | dcaed97 | 2010-08-12 15:36:34 +0200 | [diff] [blame] | 403 | int count; |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 404 | stats_cpu_t stats_data[2]; |
| 405 | smallint current_stats; |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 406 | |
| 407 | INIT_G(); |
| 408 | |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 409 | memset(&stats_data, 0, sizeof(stats_data)); |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 410 | |
| 411 | /* Get number of clock ticks per sec */ |
Bartosz Golaszewski | 5d2e409 | 2014-06-22 14:01:13 +0200 | [diff] [blame] | 412 | G.clk_tck = bb_clk_tck(); |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 413 | |
| 414 | /* Determine number of CPUs */ |
Denys Vlasenko | c9b9750 | 2010-08-16 02:49:21 +0200 | [diff] [blame] | 415 | G.total_cpus = get_cpu_count(); |
| 416 | if (G.total_cpus == 0) |
| 417 | G.total_cpus = 1; |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 418 | |
| 419 | /* Parse and process arguments */ |
| 420 | /* -k and -m are mutually exclusive */ |
Denys Vlasenko | 22542ec | 2017-08-08 21:55:02 +0200 | [diff] [blame] | 421 | opt = getopt32(argv, "^" "cdtzkm" "\0" "k--m:m--k"); |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 422 | if (!(opt & (OPT_c + OPT_d))) |
| 423 | /* Default is -cd */ |
| 424 | opt |= OPT_c + OPT_d; |
| 425 | |
| 426 | argv += optind; |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 427 | |
| 428 | /* Store device names into device list */ |
| 429 | while (*argv && !isdigit(*argv[0])) { |
Denys Vlasenko | c3f8db9 | 2011-05-07 04:36:46 +0200 | [diff] [blame] | 430 | if (strcmp(*argv, "ALL") != 0) { |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 431 | /* If not ALL, save device name */ |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 432 | char *dev_name = skip_dev_pfx(*argv); |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 433 | if (!llist_find_str(G.dev_name_list, dev_name)) { |
| 434 | llist_add_to(&G.dev_name_list, dev_name); |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 435 | } |
Denys Vlasenko | c3f8db9 | 2011-05-07 04:36:46 +0200 | [diff] [blame] | 436 | } else { |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 437 | G.show_all = 1; |
Denys Vlasenko | c3f8db9 | 2011-05-07 04:36:46 +0200 | [diff] [blame] | 438 | } |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 439 | argv++; |
| 440 | } |
| 441 | |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 442 | interval = 0; |
Denys Vlasenko | dcaed97 | 2010-08-12 15:36:34 +0200 | [diff] [blame] | 443 | count = 1; |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 444 | if (*argv) { |
| 445 | /* Get interval */ |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 446 | interval = xatoi_positive(*argv); |
Denys Vlasenko | dcaed97 | 2010-08-12 15:36:34 +0200 | [diff] [blame] | 447 | count = (interval != 0 ? -1 : 1); |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 448 | argv++; |
| 449 | if (*argv) |
| 450 | /* Get count value */ |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 451 | count = xatoi_positive(*argv); |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 452 | } |
| 453 | |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 454 | if (opt & OPT_m) { |
| 455 | G.unit.str = " MB"; |
| 456 | G.unit.div = 2048; |
| 457 | } |
| 458 | |
| 459 | if (opt & OPT_k) { |
| 460 | G.unit.str = " kB"; |
| 461 | G.unit.div = 2; |
| 462 | } |
| 463 | |
| 464 | get_localtime(&G.tmtime); |
| 465 | |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 466 | /* Display header */ |
| 467 | print_header(); |
| 468 | |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 469 | current_stats = 0; |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 470 | /* Main loop */ |
| 471 | for (;;) { |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 472 | stats_cpu_pair_t stats; |
| 473 | |
| 474 | stats.prev = &stats_data[current_stats ^ 1]; |
| 475 | stats.curr = &stats_data[current_stats]; |
| 476 | |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 477 | /* Fill the time structure */ |
| 478 | get_localtime(&G.tmtime); |
| 479 | |
| 480 | /* Fetch current CPU statistics */ |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 481 | get_cpu_statistics(stats.curr); |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 482 | |
| 483 | /* Get interval */ |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 484 | stats.itv = get_interval( |
| 485 | stats.prev->vector[GLOBAL_UPTIME], |
| 486 | stats.curr->vector[GLOBAL_UPTIME] |
| 487 | ); |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 488 | |
| 489 | if (opt & OPT_t) |
| 490 | print_timestamp(); |
| 491 | |
| 492 | if (opt & OPT_c) { |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 493 | cpu_report(&stats); |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 494 | if (opt & OPT_d) |
| 495 | /* Separate outputs by a newline */ |
| 496 | bb_putchar('\n'); |
| 497 | } |
| 498 | |
| 499 | if (opt & OPT_d) { |
| 500 | if (this_is_smp()) { |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 501 | stats.itv = get_interval( |
| 502 | stats.prev->vector[SMP_UPTIME], |
| 503 | stats.curr->vector[SMP_UPTIME] |
| 504 | ); |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 505 | } |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 506 | dev_report(stats.itv); |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 507 | } |
| 508 | |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 509 | bb_putchar('\n'); |
| 510 | |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 511 | if (count > 0) { |
| 512 | if (--count == 0) |
| 513 | break; |
| 514 | } |
| 515 | |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 516 | /* Swap stats */ |
| 517 | current_stats ^= 1; |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 518 | |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 519 | sleep(interval); |
| 520 | } |
| 521 | |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 522 | if (ENABLE_FEATURE_CLEAN_UP) { |
Maksym Kryzhanovskyy | 9f77802 | 2011-06-05 03:37:14 +0200 | [diff] [blame] | 523 | llist_free(G.dev_name_list, NULL); |
| 524 | stats_dev_free(G.stats_dev_list); |
Maksym Kryzhanovskyy | 29c54aa | 2011-05-07 04:37:22 +0200 | [diff] [blame] | 525 | free(&G); |
Marek Polacek | 733f26f | 2010-08-11 17:00:59 +0200 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | return EXIT_SUCCESS; |
| 529 | } |