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 "[ -h ] [ -k | -m ] [ -t ] [ -V ] [ --debuginfo ]\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 long long unsigned aux_open;
323 long long unsigned all_open = 0;
324 char cifs_name[MAX_NAME_LEN];
325 char name_tmp[MAX_NAME_LEN];
326 struct cifs_stats scifs;
328 /* Every CIFS entry is potentially unregistered */
329 set_entries_inactive();
331 if ((fp = fopen(CIFSSTATS, "r")) == NULL)
334 sprintf(aux, "%%*d) %%%ds",
335 MAX_NAME_LEN < 200 ? MAX_NAME_LEN - 1 : 200);
337 while (fgets(line, 256, fp) != NULL) {
339 /* Read CIFS directory name */
340 if (isdigit((unsigned char) line[0]) && sscanf(line, aux , name_tmp) == 1) {
342 scifs.fopens = all_open;
343 save_stats(cifs_name, curr, &scifs);
349 strcpy(cifs_name, name_tmp);
352 if (!strncmp(line, "Reads:", 6)) {
353 sscanf(line, "Reads: %llu Bytes: %llu", &scifs.rd_ops, &scifs.rd_bytes);
355 if (!strncmp(line, "Writes:", 7)) {
356 sscanf(line, "Writes: %llu Bytes: %llu", &scifs.wr_ops, &scifs.wr_bytes);
358 if (!strncmp(line, "Opens:", 6)) {
359 sscanf(line, "Opens: %llu Closes:%llu Deletes: %llu",
360 &aux_open, &scifs.fcloses, &scifs.fdeletes);
361 all_open += aux_open;
363 if (!strncmp(line, "Posix Opens:", 12)) {
364 sscanf(line, "Posix Opens: %llu", &aux_open);
365 all_open += aux_open;
371 scifs.fopens = all_open;
372 save_stats(cifs_name, curr, &scifs);
377 /* Free structures corresponding to unregistered filesystems */
378 free_inactive_entries();
382 ***************************************************************************
383 * Display CIFS stats header.
386 * @fctr Conversion factor.
387 ***************************************************************************
389 void write_cifs_stat_header(int *fctr)
391 printf("Filesystem: ");
392 if (DISPLAY_KILOBYTES(flags)) {
393 printf(" rkB/s wkB/s");
396 else if (DISPLAY_MEGABYTES(flags)) {
397 printf(" rMB/s wMB/s");
401 printf(" rB/s wB/s");
404 printf(" rops/s wops/s fo/s fc/s fd/s\n");
408 ***************************************************************************
409 * Write CIFS stats read from /proc/fs/cifs/Stats.
412 * @curr Index in array for current sample statistics.
413 * @itv Interval of time.
414 * @fctr Conversion factor.
415 * @shi Structures describing the CIFS filesystems.
416 * @ioi Current sample statistics.
417 * @ioj Previous sample statistics.
418 ***************************************************************************
420 void write_cifs_stat(int curr, unsigned long long itv, int fctr,
421 struct io_hdr_stats *shi, struct cifs_stats *ioni,
422 struct cifs_stats *ionj)
424 if (DISPLAY_HUMAN_READ(flags)) {
425 printf("%-22s\n%23s", shi->name, "");
428 printf("%-22s ", shi->name);
431 /* rB/s wB/s fo/s fc/s fd/s*/
432 printf("%12.2f %12.2f %9.2f %9.2f %12.2f %12.2f %12.2f \n",
433 S_VALUE(ionj->rd_bytes, ioni->rd_bytes, itv) / fctr,
434 S_VALUE(ionj->wr_bytes, ioni->wr_bytes, itv) / fctr,
435 S_VALUE(ionj->rd_ops, ioni->rd_ops, itv),
436 S_VALUE(ionj->wr_ops, ioni->wr_ops, itv),
437 S_VALUE(ionj->fopens, ioni->fopens, itv),
438 S_VALUE(ionj->fcloses, ioni->fcloses, itv),
439 S_VALUE(ionj->fdeletes, ioni->fdeletes, itv));
443 ***************************************************************************
444 * Print everything now (stats and uptime).
447 * @curr Index in array for current sample statistics.
448 * @rectime Current date and time.
449 ***************************************************************************
451 void write_stats(int curr, struct tm *rectime)
454 unsigned long long itv;
455 struct io_hdr_stats *shi;
456 struct cifs_stats *ioni, *ionj;
459 TEST_STDOUT(STDOUT_FILENO);
461 /* Print time stamp */
462 if (DISPLAY_TIMESTAMP(flags)) {
463 if (DISPLAY_ISO(flags)) {
464 strftime(timestamp, sizeof(timestamp), "%FT%T%z", rectime);
467 strftime(timestamp, sizeof(timestamp), "%x %X", rectime);
469 printf("%s\n", timestamp);
471 if (DISPLAY_DEBUG(flags)) {
472 fprintf(stderr, "%s\n", timestamp);
477 /* Interval is multiplied by the number of processors */
478 itv = get_interval(uptime[!curr], uptime[curr]);
481 /* On SMP machines, reduce itv to one processor (see note above) */
482 itv = get_interval(uptime0[!curr], uptime0[curr]);
487 /* Display CIFS stats header */
488 write_cifs_stat_header(&fctr);
490 for (i = 0; i < cifs_nr; i++, shi++) {
492 ioni = st_cifs[curr] + i;
493 ionj = st_cifs[!curr] + i;
495 if (DISPLAY_DEBUG(flags)) {
497 fprintf(stderr, "name=%s itv=%llu fctr=%d ioni{ rd_bytes=%llu "
498 "wr_bytes=%llu rd_ops=%llu wr_ops=%llu fopens=%llu "
499 "fcloses=%llu fdeletes=%llu}\n",
500 shi->name, itv, fctr,
501 ioni->rd_bytes, ioni->wr_bytes,
502 ioni->rd_ops, ioni->wr_ops,
503 ioni->fopens, ioni->fcloses,
507 write_cifs_stat(curr, itv, fctr, shi, ioni, ionj);
514 ***************************************************************************
515 * Main loop: Read stats from the relevant sources and display them.
518 * @count Number of lines of stats to print.
519 * @rectime Current date and time.
520 ***************************************************************************
522 void rw_io_stat_loop(long int count, struct tm *rectime)
526 /* Don't buffer data if redirected to a pipe */
527 setbuf(stdout, NULL);
532 * Read system uptime (only for SMP machines).
533 * Init uptime0. So if /proc/uptime cannot fill it,
534 * this will be done by /proc/stat.
537 read_uptime(&(uptime0[curr]));
540 /* Read CIFS stats */
541 read_cifs_stat(curr);
544 get_localtime(rectime, 0);
547 write_stats(curr, rectime);
562 ***************************************************************************
563 * Main entry to the cifsiostat program.
564 ***************************************************************************
566 int main(int argc, char **argv)
572 struct utsname header;
576 /* Init National Language Support */
583 /* Process args... */
587 if (!strcmp(argv[opt], "--debuginfo")) {
592 if (!strncmp(argv[opt], "-", 1)) {
593 for (i = 1; *(argv[opt] + i); i++) {
595 switch (*(argv[opt] + i)) {
598 /* Display an easy-to-read CIFS report */
599 flags |= I_D_HUMAN_READ;
603 if (DISPLAY_MEGABYTES(flags)) {
606 /* Display stats in kB/s */
607 flags |= I_D_KILOBYTES;
611 if (DISPLAY_KILOBYTES(flags)) {
614 /* Display stats in MB/s */
615 flags |= I_D_MEGABYTES;
619 /* Display timestamp */
620 flags |= I_D_TIMESTAMP;
624 /* Print version number and exit */
636 interval = atol(argv[opt++]);
645 count = atol(argv[opt++]);
646 if ((count < 1) || !interval) {
660 /* Init structures according to machine architecture */
663 get_localtime(&rectime, 0);
665 /* Get system name, release number and hostname */
667 if (print_gal_header(&rectime, header.sysname, header.release,
668 header.nodename, header.machine, cpu_nr)) {
673 /* Set a handler for SIGALRM */
677 rw_io_stat_loop(count, &rectime);
679 /* Free structures */