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"
38 #define _(string) gettext(string)
40 #define _(string) (string)
43 #define SCCSID "@(#)sysstat-" VERSION ": " __FILE__ " compiled " __DATE__ " " __TIME__
44 char *sccsid(void) { return (SCCSID); }
46 unsigned long long uptime[2] = {0, 0};
47 unsigned long long uptime0[2] = {0, 0};
48 struct cifs_stats *st_cifs[2];
49 struct io_hdr_stats *st_hdr_cifs;
51 int cifs_nr = 0; /* Nb of CIFS mounted directories found */
52 int cpu_nr = 0; /* Nb of processors on the machine */
53 int flags = 0; /* Flag for common options and system state */
60 ***************************************************************************
61 * Print usage and exit.
64 * @progname Name of sysstat command.
65 ***************************************************************************
67 void usage(char *progname)
69 fprintf(stderr, _("Usage: %s [ options ] [ <interval> [ <count> ] ]\n"),
73 fprintf(stderr, _("Options are:\n"
74 "[ --debuginfo ] [ -h ] [ -k | -m ] [ -t ] [ -V ]\n"));
76 fprintf(stderr, _("Options are:\n"
77 "[ -h ] [ -k | -m ] [ -t ] [ -V ]\n"));
83 ***************************************************************************
84 * SIGALRM signal handler.
87 * @sig Signal number. Set to 0 for the first time, then to SIGALRM.
88 ***************************************************************************
90 void alarm_handler(int sig)
92 signal(SIGALRM, alarm_handler);
97 ***************************************************************************
98 * Find number of CIFS-mounted points that are registered in
99 * /proc/fs/cifs/Stats.
102 * Number of CIFS-mounted points.
103 ***************************************************************************
105 int get_cifs_nr(void)
111 if ((fp = fopen(CIFSSTATS, "r")) == NULL)
112 /* File non-existent */
115 while (fgets(line, 128, fp) != NULL) {
117 if (!strncmp(line, "Share (unique mount targets): ", 30)) {
118 sscanf(line + 30, "%d", &cifs);
130 ***************************************************************************
131 * Set every cifs_io entry to inactive state (unregistered).
132 ***************************************************************************
134 void set_entries_inactive(void)
137 struct io_hdr_stats *shi = st_hdr_cifs;
139 for (i = 0; i < cifs_nr; i++, shi++) {
145 ***************************************************************************
146 * Free inactive entries (mark them as unused).
147 ***************************************************************************
149 void free_inactive_entries(void)
152 struct io_hdr_stats *shi = st_hdr_cifs;
154 for (i = 0; i < cifs_nr; i++, shi++) {
162 ***************************************************************************
163 * Allocate and init structures, according to system state.
164 ***************************************************************************
166 void io_sys_init(void)
170 /* How many processors on this machine? */
171 cpu_nr = get_cpu_nr(~0);
173 /* Get number of CIFS directories in /proc/fs/cifs/Stats */
174 if ((cifs_nr = get_cifs_nr()) > 0) {
175 cifs_nr += NR_CIFS_PREALLOC;
177 if ((st_hdr_cifs = (struct io_hdr_stats *) calloc(cifs_nr, IO_HDR_STATS_SIZE)) == NULL) {
182 /* Allocate structures for number of CIFS directories found */
183 for (i = 0; i < 2; i++) {
185 (struct cifs_stats *) calloc(cifs_nr, CIFS_STATS_SIZE)) == NULL) {
193 ***************************************************************************
194 * Free various structures.
195 ***************************************************************************
197 void io_sys_free(void)
201 /* Free CIFS directories structures */
202 for (i = 0; i < 2; i++) {
215 ***************************************************************************
216 * Save stats for current CIFS filesystem.
219 * @name Name of CIFS filesystem.
220 * @curr Index in array for current sample statistics.
221 * @st_io Structure with CIFS statistics to save.
222 ***************************************************************************
224 void save_stats(char *name, int curr, struct cifs_stats *st_io)
227 struct io_hdr_stats *st_hdr_cifs_i;
228 struct cifs_stats *st_cifs_i;
230 /* Look for CIFS directory in data table */
231 for (i = 0; i < cifs_nr; i++) {
232 st_hdr_cifs_i = st_hdr_cifs + i;
233 if ((st_hdr_cifs_i->used == TRUE) &&
234 (!strcmp(st_hdr_cifs_i->name, name))) {
241 * This is a new filesystem: Look for an unused entry to store it.
243 for (i = 0; i < cifs_nr; i++) {
244 st_hdr_cifs_i = st_hdr_cifs + i;
245 if (!st_hdr_cifs_i->used) {
246 /* Unused entry found... */
247 st_hdr_cifs_i->used = TRUE; /* Indicate it is now used */
248 st_hdr_cifs_i->active = TRUE;
249 strcpy(st_hdr_cifs_i->name, name);
250 st_cifs_i = st_cifs[curr] + i;
251 *st_cifs_i = *((struct cifs_stats *) st_io);
257 * It is a new CIFS directory
258 * but there is no free structure to store it.
261 /* All entries are used: The number has to be increased */
262 cifs_nr = cifs_nr + 5;
264 /* Increase the size of st_hdr_ionfs buffer */
265 if ((st_hdr_cifs = (struct io_hdr_stats *)
266 realloc(st_hdr_cifs, cifs_nr * IO_HDR_STATS_SIZE)) == NULL) {
271 /* Set the new entries inactive */
272 for (j = 0; j < 5; j++) {
273 st_hdr_cifs_i = st_hdr_cifs + i + j;
274 st_hdr_cifs_i->used = FALSE;
275 st_hdr_cifs_i->active = FALSE;
278 /* Increase the size of st_hdr_ionfs buffer */
279 for (j = 0; j < 2; j++) {
280 if ((st_cifs[j] = (struct cifs_stats *)
281 realloc(st_cifs[j], cifs_nr * CIFS_STATS_SIZE)) == NULL) {
285 memset(st_cifs[j] + i, 0, 5 * CIFS_STATS_SIZE);
287 /* Now i shows the first unused entry of the new block */
288 st_hdr_cifs_i = st_hdr_cifs + i;
289 st_hdr_cifs_i->used = TRUE; /* Indicate it is now used */
290 st_hdr_cifs_i->active = TRUE;
291 strcpy(st_hdr_cifs_i->name, name);
292 st_cifs_i = st_cifs[curr] + i;
296 st_hdr_cifs_i = st_hdr_cifs + i;
297 st_hdr_cifs_i->active = TRUE;
298 st_hdr_cifs_i->used = TRUE;
299 st_cifs_i = st_cifs[curr] + i;
303 * else it was a new CIFS directory
304 * but there was no free structure to store it.
309 ***************************************************************************
310 * Read CIFS-mount directories stats from /proc/fs/cifs/Stats.
313 * @curr Index in array for current sample statistics.
314 ***************************************************************************
316 void read_cifs_stat(int curr)
322 char cifs_name[MAX_NAME_LEN];
323 char name_tmp[MAX_NAME_LEN];
324 struct cifs_stats scifs;
326 /* Every CIFS entry is potentially unregistered */
327 set_entries_inactive();
329 if ((fp = fopen(CIFSSTATS, "r")) == NULL)
332 sprintf(aux, "%%*d) %%%ds",
333 MAX_NAME_LEN < 200 ? MAX_NAME_LEN - 1 : 200);
335 while (fgets(line, 256, fp) != NULL) {
337 /* Read CIFS directory name */
338 if (isdigit((unsigned char) line[0]) && sscanf(line, aux , name_tmp) == 1) {
340 save_stats(cifs_name, curr, &scifs);
345 strcpy(cifs_name, name_tmp);
348 if (!strncmp(line, "Reads:", 6)) {
349 sscanf(line, "Reads: %llu Bytes: %llu", &scifs.rd_ops, &scifs.rd_bytes);
351 if (!strncmp(line, "Writes:", 7)) {
352 sscanf(line, "Writes: %llu Bytes: %llu", &scifs.wr_ops, &scifs.wr_bytes);
354 if (!strncmp(line, "Opens:", 6)) {
355 sscanf(line, "Opens: %llu Closes:%llu Deletes: %llu",
356 &scifs.fopens, &scifs.fcloses, &scifs.fdeletes);
362 save_stats(cifs_name, curr, &scifs);
367 /* Free structures corresponding to unregistered filesystems */
368 free_inactive_entries();
372 ***************************************************************************
373 * Display CIFS stats header.
376 * @fctr Conversion factor.
377 ***************************************************************************
379 void write_cifs_stat_header(int *fctr)
381 printf("Filesystem: ");
382 if (DISPLAY_KILOBYTES(flags)) {
383 printf(" rkB/s wkB/s");
386 else if (DISPLAY_MEGABYTES(flags)) {
387 printf(" rMB/s wMB/s");
391 printf(" rB/s wB/s");
394 printf(" rops/s wops/s fo/s fc/s fd/s\n");
398 ***************************************************************************
399 * Write CIFS stats read from /proc/fs/cifs/Stats.
402 * @curr Index in array for current sample statistics.
403 * @itv Interval of time.
404 * @fctr Conversion factor.
405 * @shi Structures describing the CIFS filesystems.
406 * @ioi Current sample statistics.
407 * @ioj Previous sample statistics.
408 ***************************************************************************
410 void write_cifs_stat(int curr, unsigned long long itv, int fctr,
411 struct io_hdr_stats *shi, struct cifs_stats *ioni,
412 struct cifs_stats *ionj)
414 if (DISPLAY_HUMAN_READ(flags)) {
415 printf("%-22s\n%23s", shi->name, "");
418 printf("%-22s ", shi->name);
421 /* rB/s wB/s fo/s fc/s fd/s*/
422 printf("%12.2f %12.2f %9.2f %9.2f %12.2f %12.2f %12.2f \n",
423 S_VALUE(ionj->rd_bytes, ioni->rd_bytes, itv) / fctr,
424 S_VALUE(ionj->wr_bytes, ioni->wr_bytes, itv) / fctr,
425 S_VALUE(ionj->rd_ops, ioni->rd_ops, itv),
426 S_VALUE(ionj->wr_ops, ioni->wr_ops, itv),
427 S_VALUE(ionj->fopens, ioni->fopens, itv),
428 S_VALUE(ionj->fcloses, ioni->fcloses, itv),
429 S_VALUE(ionj->fdeletes, ioni->fdeletes, itv));
433 ***************************************************************************
434 * Print everything now (stats and uptime).
437 * @curr Index in array for current sample statistics.
438 * @rectime Current date and time.
439 ***************************************************************************
441 void write_stats(int curr, struct tm *rectime)
444 unsigned long long itv;
445 struct io_hdr_stats *shi;
446 struct cifs_stats *ioni, *ionj;
449 TEST_STDOUT(STDOUT_FILENO);
451 /* Print time stamp */
452 if (DISPLAY_TIMESTAMP(flags)) {
453 if (DISPLAY_ISO(flags)) {
454 strftime(timestamp, sizeof(timestamp), "%FT%T%z", rectime);
457 strftime(timestamp, sizeof(timestamp), "%x %X", rectime);
459 printf("%s\n", timestamp);
461 if (DISPLAY_DEBUG(flags)) {
462 fprintf(stderr, "%s\n", timestamp);
467 /* Interval is multiplied by the number of processors */
468 itv = get_interval(uptime[!curr], uptime[curr]);
471 /* On SMP machines, reduce itv to one processor (see note above) */
472 itv = get_interval(uptime0[!curr], uptime0[curr]);
477 /* Display CIFS stats header */
478 write_cifs_stat_header(&fctr);
480 for (i = 0; i < cifs_nr; i++, shi++) {
482 ioni = st_cifs[curr] + i;
483 ionj = st_cifs[!curr] + i;
485 if (DISPLAY_DEBUG(flags)) {
487 fprintf(stderr, "name=%s itv=%llu fctr=%d ioni{ rd_bytes=%llu "
488 "wr_bytes=%llu rd_ops=%llu wr_ops=%llu fopens=%llu "
489 "fcloses=%llu fdeletes=%llu}\n",
490 shi->name, itv, fctr,
491 ioni->rd_bytes, ioni->wr_bytes,
492 ioni->rd_ops, ioni->wr_ops,
493 ioni->fopens, ioni->fcloses,
497 write_cifs_stat(curr, itv, fctr, shi, ioni, ionj);
504 ***************************************************************************
505 * Main loop: Read stats from the relevant sources and display them.
508 * @count Number of lines of stats to print.
509 * @rectime Current date and time.
510 ***************************************************************************
512 void rw_io_stat_loop(long int count, struct tm *rectime)
516 /* Don't buffer data if redirected to a pipe */
517 setbuf(stdout, NULL);
522 * Read system uptime (only for SMP machines).
523 * Init uptime0. So if /proc/uptime cannot fill it,
524 * this will be done by /proc/stat.
527 read_uptime(&(uptime0[curr]));
530 /* Read CIFS stats */
531 read_cifs_stat(curr);
534 get_localtime(rectime);
537 write_stats(curr, rectime);
552 ***************************************************************************
553 * Main entry to the cifsiostat program.
554 ***************************************************************************
556 int main(int argc, char **argv)
562 struct utsname header;
566 /* Init National Language Support */
573 /* Process args... */
577 if (!strcmp(argv[opt], "--debuginfo")) {
582 if (!strncmp(argv[opt], "-", 1)) {
583 for (i = 1; *(argv[opt] + i); i++) {
585 switch (*(argv[opt] + i)) {
588 /* Display an easy-to-read CIFS report */
589 flags |= I_D_HUMAN_READ;
593 if (DISPLAY_MEGABYTES(flags)) {
596 /* Display stats in kB/s */
597 flags |= I_D_KILOBYTES;
601 if (DISPLAY_KILOBYTES(flags)) {
604 /* Display stats in MB/s */
605 flags |= I_D_MEGABYTES;
609 /* Display timestamp */
610 flags |= I_D_TIMESTAMP;
614 /* Print version number and exit */
626 interval = atol(argv[opt++]);
635 count = atol(argv[opt++]);
636 if ((count < 1) || !interval) {
650 /* Init structures according to machine architecture */
653 get_localtime(&rectime);
655 /* Get system name, release number and hostname */
657 if (print_gal_header(&rectime, header.sysname, header.release,
658 header.nodename, header.machine, cpu_nr)) {
663 /* Set a handler for SIGALRM */
667 rw_io_stat_loop(count, &rectime);
669 /* Free structures */