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