2 * cifsiostat: Report I/O statistics for CIFS filesystems.
3 * Copyright (C) 2010 Red Hat, Inc. All Rights Reserved
4 * Written by Ivana Varekova <varekova@redhat.com>
6 ***************************************************************************
7 * This program is free software; you can redistribute it and/or modify it *
8 * under the terms of the GNU General Public License as published by the *
9 * Free Software Foundation; either version 2 of the License, or (at your *
10 * option) any later version. *
12 * This program is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without the implied warranty of MERCHANTABILITY *
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * You should have received a copy of the GNU General Public License along *
18 * with this program; if not, write to the Free Software Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************
28 #include <sys/utsname.h>
32 #include "cifsiostat.h"
39 #define _(string) gettext(string)
41 #define _(string) (string)
45 #define SCCSID "@(#)sysstat-" VERSION ": " __FILE__ " compiled " __DATE__ " " __TIME__
46 char *sccsid(void) { return (SCCSID); }
49 unsigned long long uptime_cs[2] = {0, 0};
50 struct cifs_stats *st_cifs[2];
51 struct io_hdr_stats *st_hdr_cifs;
53 int cifs_nr = 0; /* Nb of CIFS mounted directories found */
54 int cpu_nr = 0; /* Nb of processors on the machine */
55 int flags = 0; /* Flag for common options and system state */
56 int dplaces_nr = -1; /* Number of decimal places */
59 char timestamp[TIMESTAMP_LEN];
61 struct sigaction alrm_act;
64 ***************************************************************************
65 * Print usage and exit.
68 * @progname Name of sysstat command.
69 ***************************************************************************
71 void usage(char *progname)
73 fprintf(stderr, _("Usage: %s [ options ] [ <interval> [ <count> ] ]\n"),
77 fprintf(stderr, _("Options are:\n"
78 "[ --dec={ 0 | 1 | 2 } ] [ --human ] [ -h ] [ -k | -m ] [ -t ] [ -V ] [ --debuginfo ]\n"));
80 fprintf(stderr, _("Options are:\n"
81 "[ --dec={ 0 | 1 | 2 } ] [ --human ] [ -h ] [ -k | -m ] [ -t ] [ -V ]\n"));
87 ***************************************************************************
88 * SIGALRM signal handler.
92 ***************************************************************************
94 void alarm_handler(int sig)
100 ***************************************************************************
101 * Find number of CIFS-mounted points that are registered in
102 * /proc/fs/cifs/Stats.
105 * Number of CIFS-mounted points.
106 ***************************************************************************
108 int get_cifs_nr(void)
114 if ((fp = fopen(CIFSSTATS, "r")) == NULL)
115 /* File non-existent */
118 while (fgets(line, sizeof(line), fp) != NULL) {
120 if (!strncmp(line, "Share (unique mount targets): ", 30)) {
121 sscanf(line + 30, "%d", &cifs);
133 ***************************************************************************
134 * Set every cifs_io entry to inactive state (unregistered).
135 ***************************************************************************
137 void set_entries_inactive(void)
140 struct io_hdr_stats *shi = st_hdr_cifs;
142 for (i = 0; i < cifs_nr; i++, shi++) {
148 ***************************************************************************
149 * Free inactive entries (mark them as unused).
150 ***************************************************************************
152 void free_inactive_entries(void)
155 struct io_hdr_stats *shi = st_hdr_cifs;
157 for (i = 0; i < cifs_nr; i++, shi++) {
165 ***************************************************************************
166 * Allocate and init structures, according to system state.
167 ***************************************************************************
169 void io_sys_init(void)
173 /* How many processors on this machine? */
174 cpu_nr = get_cpu_nr(~0, FALSE);
176 /* Get number of CIFS directories in /proc/fs/cifs/Stats */
177 if ((cifs_nr = get_cifs_nr()) > 0) {
178 cifs_nr += NR_CIFS_PREALLOC;
182 if ((st_hdr_cifs = (struct io_hdr_stats *) calloc(cifs_nr, IO_HDR_STATS_SIZE)) == NULL) {
187 /* Allocate structures for number of CIFS directories found */
188 for (i = 0; i < 2; i++) {
190 (struct cifs_stats *) calloc(cifs_nr, CIFS_STATS_SIZE)) == NULL) {
198 * cifs_nr value is probably zero, but it can also be negative
199 * (possible overflow when adding NR_CIFS_PREALLOC above).
206 ***************************************************************************
207 * Free various structures.
208 ***************************************************************************
210 void io_sys_free(void)
214 /* Free CIFS directories structures */
215 for (i = 0; i < 2; i++) {
223 ***************************************************************************
224 * Save stats for current CIFS filesystem.
227 * @name Name of CIFS filesystem.
228 * @curr Index in array for current sample statistics.
229 * @st_io Structure with CIFS statistics to save.
230 ***************************************************************************
232 void save_stats(char *name, int curr, struct cifs_stats *st_io)
235 struct io_hdr_stats *st_hdr_cifs_i;
236 struct cifs_stats *st_cifs_i;
238 /* Look for CIFS directory in data table */
239 for (i = 0; i < cifs_nr; i++) {
240 st_hdr_cifs_i = st_hdr_cifs + i;
241 if ((st_hdr_cifs_i->used == TRUE) &&
242 (!strcmp(st_hdr_cifs_i->name, name))) {
249 * This is a new filesystem: Look for an unused entry to store it.
251 for (i = 0; i < cifs_nr; i++) {
252 st_hdr_cifs_i = st_hdr_cifs + i;
253 if (!st_hdr_cifs_i->used) {
254 /* Unused entry found... */
255 st_hdr_cifs_i->used = TRUE; /* Indicate it is now used */
256 st_hdr_cifs_i->active = TRUE;
257 strncpy(st_hdr_cifs_i->name, name, MAX_NAME_LEN - 1);
258 st_hdr_cifs_i->name[MAX_NAME_LEN - 1] = '\0';
259 st_cifs_i = st_cifs[curr] + i;
260 *st_cifs_i = *((struct cifs_stats *) st_io);
266 * It is a new CIFS directory
267 * but there is no free structure to store it.
270 /* All entries are used: The number has to be increased */
271 cifs_nr = cifs_nr + 5;
273 /* Increase the size of st_hdr_ionfs buffer */
274 if ((st_hdr_cifs = (struct io_hdr_stats *)
275 realloc(st_hdr_cifs, cifs_nr * IO_HDR_STATS_SIZE)) == NULL) {
280 /* Set the new entries inactive */
281 for (j = 0; j < 5; j++) {
282 st_hdr_cifs_i = st_hdr_cifs + i + j;
283 st_hdr_cifs_i->used = FALSE;
284 st_hdr_cifs_i->active = FALSE;
287 /* Increase the size of st_hdr_ionfs buffer */
288 for (j = 0; j < 2; j++) {
289 if ((st_cifs[j] = (struct cifs_stats *)
290 realloc(st_cifs[j], cifs_nr * CIFS_STATS_SIZE)) == NULL) {
294 memset(st_cifs[j] + i, 0, 5 * CIFS_STATS_SIZE);
296 /* Now i shows the first unused entry of the new block */
297 st_hdr_cifs_i = st_hdr_cifs + i;
298 st_hdr_cifs_i->used = TRUE; /* Indicate it is now used */
299 st_hdr_cifs_i->active = TRUE;
300 strncpy(st_hdr_cifs_i->name, name, MAX_NAME_LEN - 1);
301 st_hdr_cifs_i->name[MAX_NAME_LEN - 1] = '\0';
302 st_cifs_i = st_cifs[curr] + i;
306 st_hdr_cifs_i = st_hdr_cifs + i;
307 st_hdr_cifs_i->active = TRUE;
308 st_hdr_cifs_i->used = TRUE;
309 st_cifs_i = st_cifs[curr] + i;
313 * else it was a new CIFS directory
314 * but there was no free structure to store it.
319 ***************************************************************************
320 * Read CIFS-mount directories stats from /proc/fs/cifs/Stats.
323 * @curr Index in array for current sample statistics.
324 ***************************************************************************
326 void read_cifs_stat(int curr)
332 long long unsigned aux_open;
333 long long unsigned all_open = 0;
334 char cifs_name[MAX_NAME_LEN];
335 char name_tmp[MAX_NAME_LEN];
336 struct cifs_stats scifs = {0, 0, 0, 0, 0, 0, 0};
338 /* Every CIFS entry is potentially unregistered */
339 set_entries_inactive();
341 if ((fp = fopen(CIFSSTATS, "r")) == NULL)
344 sprintf(aux, "%%*d) %%%ds",
345 MAX_NAME_LEN < 200 ? MAX_NAME_LEN - 1 : 200);
347 while (fgets(line, sizeof(line), fp) != NULL) {
349 /* Read CIFS directory name */
350 if (isdigit((unsigned char) line[0]) && sscanf(line, aux , name_tmp) == 1) {
352 scifs.fopens = all_open;
353 save_stats(cifs_name, curr, &scifs);
359 strncpy(cifs_name, name_tmp, MAX_NAME_LEN);
360 cifs_name[MAX_NAME_LEN - 1] = '\0';
363 if (!strncmp(line, "Reads:", 6)) {
364 sscanf(line, "Reads: %llu Bytes: %llu", &scifs.rd_ops, &scifs.rd_bytes);
366 if (!strncmp(line, "Writes:", 7)) {
367 sscanf(line, "Writes: %llu Bytes: %llu", &scifs.wr_ops, &scifs.wr_bytes);
369 if (!strncmp(line, "Opens:", 6)) {
370 sscanf(line, "Opens: %llu Closes:%llu Deletes: %llu",
371 &aux_open, &scifs.fcloses, &scifs.fdeletes);
372 all_open += aux_open;
374 if (!strncmp(line, "Posix Opens:", 12)) {
375 sscanf(line, "Posix Opens: %llu", &aux_open);
376 all_open += aux_open;
382 scifs.fopens = all_open;
383 save_stats(cifs_name, curr, &scifs);
388 /* Free structures corresponding to unregistered filesystems */
389 free_inactive_entries();
393 ***************************************************************************
394 * Display CIFS stats header.
397 * @fctr Conversion factor.
398 ***************************************************************************
400 void write_cifs_stat_header(int *fctr)
402 if (!DISPLAY_HUMAN_READ(flags)) {
403 printf("Filesystem ");
405 if (DISPLAY_KILOBYTES(flags)) {
406 printf(" rkB/s wkB/s");
409 else if (DISPLAY_MEGABYTES(flags)) {
410 printf(" rMB/s wMB/s");
414 printf(" rB/s wB/s");
417 printf(" rops/s wops/s fo/s fc/s fd/s");
418 if (DISPLAY_HUMAN_READ(flags)) {
419 printf(" Filesystem");
425 ***************************************************************************
426 * Write CIFS stats read from /proc/fs/cifs/Stats.
429 * @curr Index in array for current sample statistics.
430 * @itv Interval of time (in 1/100th of a second).
431 * @fctr Conversion factor.
432 * @shi Structures describing the CIFS filesystems.
433 * @ioi Current sample statistics.
434 * @ioj Previous sample statistics.
435 ***************************************************************************
437 void write_cifs_stat(int curr, unsigned long long itv, int fctr,
438 struct io_hdr_stats *shi, struct cifs_stats *ioni,
439 struct cifs_stats *ionj)
441 double rbytes, wbytes;
443 if (!DISPLAY_HUMAN_READ(flags)) {
444 cprintf_in(IS_STR, "%-22s", shi->name, 0);
447 /* rB/s wB/s fo/s fc/s fd/s*/
448 rbytes = S_VALUE(ionj->rd_bytes, ioni->rd_bytes, itv);
449 wbytes = S_VALUE(ionj->wr_bytes, ioni->wr_bytes, itv);
450 if (!DISPLAY_UNIT(flags)) {
454 cprintf_f(DISPLAY_UNIT(flags) ? UNIT_BYTE : NO_UNIT, 2, 12, 2,
456 cprintf_f(NO_UNIT, 2, 9, 2,
457 S_VALUE(ionj->rd_ops, ioni->rd_ops, itv),
458 S_VALUE(ionj->wr_ops, ioni->wr_ops, itv));
459 cprintf_f(NO_UNIT, 3, 12, 2,
460 S_VALUE(ionj->fopens, ioni->fopens, itv),
461 S_VALUE(ionj->fcloses, ioni->fcloses, itv),
462 S_VALUE(ionj->fdeletes, ioni->fdeletes, itv));
463 if (DISPLAY_HUMAN_READ(flags)) {
464 cprintf_in(IS_STR, " %s", shi->name, 0);
470 ***************************************************************************
471 * Print everything now (stats and uptime).
474 * @curr Index in array for current sample statistics.
475 * @rectime Current date and time.
476 ***************************************************************************
478 void write_stats(int curr, struct tm *rectime)
481 unsigned long long itv;
482 struct io_hdr_stats *shi;
483 struct cifs_stats *ioni, *ionj;
486 TEST_STDOUT(STDOUT_FILENO);
488 /* Print time stamp */
489 if (DISPLAY_TIMESTAMP(flags)) {
490 if (DISPLAY_ISO(flags)) {
491 strftime(timestamp, sizeof(timestamp), "%FT%T%z", rectime);
494 strftime(timestamp, sizeof(timestamp), "%x %X", rectime);
496 printf("%s\n", timestamp);
498 if (DISPLAY_DEBUG(flags)) {
499 fprintf(stderr, "%s\n", timestamp);
504 /* Interval of time, reduced to one processor */
505 itv = get_interval(uptime_cs[!curr], uptime_cs[curr]);
509 /* Display CIFS stats header */
510 write_cifs_stat_header(&fctr);
512 for (i = 0; i < cifs_nr; i++, shi++) {
514 ioni = st_cifs[curr] + i;
515 ionj = st_cifs[!curr] + i;
517 if (DISPLAY_DEBUG(flags)) {
519 fprintf(stderr, "name=%s itv=%llu fctr=%d ioni{ rd_bytes=%llu "
520 "wr_bytes=%llu rd_ops=%llu wr_ops=%llu fopens=%llu "
521 "fcloses=%llu fdeletes=%llu}\n",
522 shi->name, itv, fctr,
523 ioni->rd_bytes, ioni->wr_bytes,
524 ioni->rd_ops, ioni->wr_ops,
525 ioni->fopens, ioni->fcloses,
529 write_cifs_stat(curr, itv, fctr, shi, ioni, ionj);
536 ***************************************************************************
537 * Main loop: Read stats from the relevant sources and display them.
540 * @count Number of lines of stats to print.
541 * @rectime Current date and time.
542 ***************************************************************************
544 void rw_io_stat_loop(long int count, struct tm *rectime)
548 /* Don't buffer data if redirected to a pipe */
549 setbuf(stdout, NULL);
552 /* Read system uptime in 1/100th of a second */
553 read_uptime(&(uptime_cs[curr]));
555 /* Read CIFS stats */
556 read_cifs_stat(curr);
559 get_localtime(rectime, 0);
562 write_stats(curr, rectime);
577 ***************************************************************************
578 * Main entry to the cifsiostat program.
579 ***************************************************************************
581 int main(int argc, char **argv)
587 struct utsname header;
591 /* Init National Language Support */
595 /* Init color strings */
598 /* Process args... */
602 if (!strcmp(argv[opt], "--debuginfo")) {
608 if (!strcmp(argv[opt], "--human")) {
613 else if (!strncmp(argv[opt], "--dec=", 6) && (strlen(argv[opt]) == 7)) {
614 /* Get number of decimal places */
615 dplaces_nr = atoi(argv[opt] + 6);
616 if ((dplaces_nr < 0) || (dplaces_nr > 2)) {
622 else if (!strncmp(argv[opt], "-", 1)) {
623 for (i = 1; *(argv[opt] + i); i++) {
625 switch (*(argv[opt] + i)) {
628 /* Display an easy-to-read CIFS report. Also imply --human */
629 flags |= I_D_HUMAN_READ + I_D_UNIT;
633 if (DISPLAY_MEGABYTES(flags)) {
636 /* Display stats in kB/s */
637 flags |= I_D_KILOBYTES;
641 if (DISPLAY_KILOBYTES(flags)) {
644 /* Display stats in MB/s */
645 flags |= I_D_MEGABYTES;
649 /* Display timestamp */
650 flags |= I_D_TIMESTAMP;
654 /* Print version number and exit */
666 interval = atol(argv[opt++]);
675 count = atol(argv[opt++]);
676 if ((count < 1) || !interval) {
690 /* Init structures according to machine architecture */
693 get_localtime(&rectime, 0);
695 /* Get system name, release number and hostname */
697 if (print_gal_header(&rectime, header.sysname, header.release,
698 header.nodename, header.machine, cpu_nr,
704 /* Set a handler for SIGALRM */
705 memset(&alrm_act, 0, sizeof(alrm_act));
706 alrm_act.sa_handler = alarm_handler;
707 sigaction(SIGALRM, &alrm_act, NULL);
711 rw_io_stat_loop(count, &rectime);
713 /* Free structures */