]> granicus.if.org Git - sysstat/blob - cifsiostat.c
SVG: Add SVG output for IPv6 network errors statistics
[sysstat] / cifsiostat.c
1 /*
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>
5  *
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.                                              *
11  *                                                                         *
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 *
15  * for more details.                                                       *
16  *                                                                         *
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  ***************************************************************************
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <signal.h>
28 #include <sys/utsname.h>
29 #include <ctype.h>
30
31 #include "version.h"
32 #include "cifsiostat.h"
33 #include "count.h"
34 #include "common.h"
35
36 #ifdef USE_NLS
37 #include <locale.h>
38 #include <libintl.h>
39 #define _(string) gettext(string)
40 #else
41 #define _(string) (string)
42 #endif
43
44 #define SCCSID "@(#)sysstat-" VERSION ": " __FILE__ " compiled " __DATE__ " " __TIME__
45 char *sccsid(void) { return (SCCSID); }
46
47 unsigned long long uptime0[2] = {0, 0};
48 struct cifs_stats *st_cifs[2];
49 struct io_hdr_stats *st_hdr_cifs;
50
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 */
54
55 long interval = 0;
56 char timestamp[64];
57
58 struct sigaction alrm_act;
59
60 /*
61  ***************************************************************************
62  * Print usage and exit.
63  *
64  * IN:
65  * @progname    Name of sysstat command.
66  ***************************************************************************
67  */
68 void usage(char *progname)
69 {
70         fprintf(stderr, _("Usage: %s [ options ] [ <interval> [ <count> ] ]\n"),
71                 progname);
72
73 #ifdef DEBUG
74         fprintf(stderr, _("Options are:\n"
75                           "[ -h ] [ -k | -m ] [ -t ] [ -V ] [ --debuginfo ]\n"));
76 #else
77         fprintf(stderr, _("Options are:\n"
78                           "[ -h ] [ -k | -m ] [ -t ] [ -V ]\n"));
79 #endif
80         exit(1);
81 }
82
83 /*
84  ***************************************************************************
85  * SIGALRM signal handler.
86  *
87  * IN:
88  * @sig Signal number.
89  ***************************************************************************
90  */
91 void alarm_handler(int sig)
92 {
93         alarm(interval);
94 }
95
96 /*
97  ***************************************************************************
98  * Find number of CIFS-mounted points that are registered in
99  * /proc/fs/cifs/Stats.
100  *
101  * RETURNS:
102  * Number of CIFS-mounted points.
103  ***************************************************************************
104  */
105 int get_cifs_nr(void)
106 {
107         FILE *fp;
108         char line[128];
109         int cifs = 0;
110
111         if ((fp = fopen(CIFSSTATS, "r")) == NULL)
112                 /* File non-existent */
113                 return 0;
114
115         while (fgets(line, sizeof(line), fp) != NULL) {
116
117                 if (!strncmp(line, "Share (unique mount targets): ", 30)) {
118                         sscanf(line + 30, "%d", &cifs);
119                         break;
120                 }
121         }
122
123         /* Close file */
124         fclose(fp);
125
126         return cifs;
127 }
128
129 /*
130  ***************************************************************************
131  * Set every cifs_io entry to inactive state (unregistered).
132  ***************************************************************************
133  */
134 void set_entries_inactive(void)
135 {
136         int i;
137         struct io_hdr_stats *shi = st_hdr_cifs;
138
139         for (i = 0; i < cifs_nr; i++, shi++) {
140                 shi->active = FALSE;
141         }
142 }
143
144 /*
145  ***************************************************************************
146  * Free inactive entries (mark them as unused).
147  ***************************************************************************
148  */
149 void free_inactive_entries(void)
150 {
151         int i;
152         struct io_hdr_stats *shi = st_hdr_cifs;
153
154         for (i = 0; i < cifs_nr; i++, shi++) {
155                 if (!shi->active) {
156                         shi->used = FALSE;
157                 }
158         }
159 }
160
161 /*
162  ***************************************************************************
163  * Allocate and init structures, according to system state.
164  ***************************************************************************
165  */
166 void io_sys_init(void)
167 {
168         int i;
169
170         /* How many processors on this machine? */
171         cpu_nr = get_cpu_nr(~0, FALSE);
172
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;
176         }
177
178         if (cifs_nr > 0) {
179                 if ((st_hdr_cifs = (struct io_hdr_stats *) calloc(cifs_nr, IO_HDR_STATS_SIZE)) == NULL) {
180                         perror("malloc");
181                         exit(4);
182                 }
183
184                 /* Allocate structures for number of CIFS directories found */
185                 for (i = 0; i < 2; i++) {
186                         if ((st_cifs[i] =
187                         (struct cifs_stats *) calloc(cifs_nr, CIFS_STATS_SIZE)) == NULL) {
188                                 perror("malloc");
189                                 exit(4);
190                         }
191                 }
192         }
193         else {
194                 /*
195                  * cifs_nr value is probably zero, but it can also be negative
196                  * (possible overflow when adding NR_CIFS_PREALLOC above).
197                  */
198                 cifs_nr = 0;
199         }
200 }
201
202 /*
203  ***************************************************************************
204  * Free various structures.
205  ***************************************************************************
206 */
207 void io_sys_free(void)
208 {
209         int i;
210
211         /* Free CIFS directories structures */
212         for (i = 0; i < 2; i++) {
213                 free(st_cifs[i]);
214         }
215
216         free(st_hdr_cifs);
217 }
218
219 /*
220  ***************************************************************************
221  * Save stats for current CIFS filesystem.
222  *
223  * IN:
224  * @name                Name of CIFS filesystem.
225  * @curr                Index in array for current sample statistics.
226  * @st_io               Structure with CIFS statistics to save.
227  ***************************************************************************
228  */
229 void save_stats(char *name, int curr, struct cifs_stats *st_io)
230 {
231         int i, j;
232         struct io_hdr_stats *st_hdr_cifs_i;
233         struct cifs_stats *st_cifs_i;
234
235         /* Look for CIFS directory in data table */
236         for (i = 0; i < cifs_nr; i++) {
237                 st_hdr_cifs_i = st_hdr_cifs + i;
238                 if ((st_hdr_cifs_i->used == TRUE) &&
239                     (!strcmp(st_hdr_cifs_i->name, name))) {
240                         break;
241                 }
242         }
243
244         if (i == cifs_nr) {
245                 /*
246                  * This is a new filesystem: Look for an unused entry to store it.
247                  */
248                 for (i = 0; i < cifs_nr; i++) {
249                         st_hdr_cifs_i = st_hdr_cifs + i;
250                         if (!st_hdr_cifs_i->used) {
251                                 /* Unused entry found... */
252                                 st_hdr_cifs_i->used = TRUE; /* Indicate it is now used */
253                                 st_hdr_cifs_i->active = TRUE;
254                                 strncpy(st_hdr_cifs_i->name, name, MAX_NAME_LEN - 1);
255                                 st_hdr_cifs_i->name[MAX_NAME_LEN - 1] = '\0';
256                                 st_cifs_i = st_cifs[curr] + i;
257                                 *st_cifs_i = *((struct cifs_stats *) st_io);
258                                 break;
259                         }
260                 }
261                 if (i == cifs_nr) {
262                         /*
263                          * It is a new CIFS directory
264                          * but there is no free structure to store it.
265                          */
266
267                         /* All entries are used: The number has to be increased */
268                         cifs_nr = cifs_nr + 5;
269
270                         /* Increase the size of st_hdr_ionfs buffer */
271                         if ((st_hdr_cifs = (struct io_hdr_stats *)
272                                 realloc(st_hdr_cifs, cifs_nr * IO_HDR_STATS_SIZE)) == NULL) {
273                                 perror("malloc");
274                                 exit(4);
275                         }
276
277                         /* Set the new entries inactive */
278                         for (j = 0; j < 5; j++) {
279                                 st_hdr_cifs_i = st_hdr_cifs + i + j;
280                                 st_hdr_cifs_i->used = FALSE;
281                                 st_hdr_cifs_i->active = FALSE;
282                         }
283
284                         /* Increase the size of st_hdr_ionfs buffer */
285                         for (j = 0; j < 2; j++) {
286                                 if ((st_cifs[j] = (struct cifs_stats *)
287                                         realloc(st_cifs[j], cifs_nr * CIFS_STATS_SIZE)) == NULL) {
288                                         perror("malloc");
289                                         exit(4);
290                                 }
291                                 memset(st_cifs[j] + i, 0, 5 * CIFS_STATS_SIZE);
292                         }
293                         /* Now i shows the first unused entry of the new block */
294                         st_hdr_cifs_i = st_hdr_cifs + i;
295                         st_hdr_cifs_i->used = TRUE; /* Indicate it is now used */
296                         st_hdr_cifs_i->active = TRUE;
297                         strncpy(st_hdr_cifs_i->name, name, MAX_NAME_LEN - 1);
298                         st_hdr_cifs_i->name[MAX_NAME_LEN - 1] = '\0';
299                         st_cifs_i = st_cifs[curr] + i;
300                         *st_cifs_i = *st_io;
301                 }
302         } else {
303                 st_hdr_cifs_i = st_hdr_cifs + i;
304                 st_hdr_cifs_i->active = TRUE;
305                 st_hdr_cifs_i->used = TRUE;
306                 st_cifs_i = st_cifs[curr] + i;
307                 *st_cifs_i = *st_io;
308         }
309         /*
310          * else it was a new CIFS directory
311          * but there was no free structure to store it.
312          */
313 }
314
315 /*
316  ***************************************************************************
317  * Read CIFS-mount directories stats from /proc/fs/cifs/Stats.
318  *
319  * IN:
320  * @curr        Index in array for current sample statistics.
321  ***************************************************************************
322  */
323 void read_cifs_stat(int curr)
324 {
325         FILE *fp;
326         char line[256];
327         char aux[32];
328         int start = 0;
329         long long unsigned aux_open;
330         long long unsigned all_open = 0;
331         char cifs_name[MAX_NAME_LEN];
332         char name_tmp[MAX_NAME_LEN];
333         struct cifs_stats scifs = {0, 0, 0, 0, 0, 0, 0};
334
335         /* Every CIFS entry is potentially unregistered */
336         set_entries_inactive();
337
338         if ((fp = fopen(CIFSSTATS, "r")) == NULL)
339                 return;
340
341         sprintf(aux, "%%*d) %%%ds",
342                 MAX_NAME_LEN < 200 ? MAX_NAME_LEN - 1 : 200);
343
344         while (fgets(line, sizeof(line), fp) != NULL) {
345
346                 /* Read CIFS directory name */
347                 if (isdigit((unsigned char) line[0]) && sscanf(line, aux , name_tmp) == 1) {
348                         if (start) {
349                                 scifs.fopens = all_open;
350                                 save_stats(cifs_name, curr, &scifs);
351                                 all_open = 0;
352                         }
353                         else {
354                                 start = 1;
355                         }
356                         strcpy(cifs_name, name_tmp);
357                 }
358                 else {
359                         if (!strncmp(line, "Reads:", 6)) {
360                                 sscanf(line, "Reads: %llu Bytes: %llu", &scifs.rd_ops, &scifs.rd_bytes);
361                         }
362                         if (!strncmp(line, "Writes:", 7)) {
363                                 sscanf(line, "Writes: %llu Bytes: %llu", &scifs.wr_ops, &scifs.wr_bytes);
364                         }
365                         if (!strncmp(line, "Opens:", 6)) {
366                                 sscanf(line, "Opens: %llu Closes:%llu Deletes: %llu",
367                                        &aux_open, &scifs.fcloses, &scifs.fdeletes);
368                                 all_open += aux_open;
369                         }
370                         if (!strncmp(line, "Posix Opens:", 12)) {
371                                 sscanf(line, "Posix Opens: %llu", &aux_open);
372                                 all_open += aux_open;
373                         }
374                 }
375         }
376
377         if (start) {
378                 scifs.fopens = all_open;
379                 save_stats(cifs_name, curr, &scifs);
380         }
381
382         fclose(fp);
383
384         /* Free structures corresponding to unregistered filesystems */
385         free_inactive_entries();
386 }
387
388 /*
389  ***************************************************************************
390  * Display CIFS stats header.
391  *
392  * OUT:
393  * @fctr        Conversion factor.
394  ***************************************************************************
395  */
396 void write_cifs_stat_header(int *fctr)
397 {
398         printf("Filesystem:           ");
399         if (DISPLAY_KILOBYTES(flags)) {
400                 printf("        rkB/s        wkB/s");
401                 *fctr = 1024;
402         }
403         else if (DISPLAY_MEGABYTES(flags)) {
404                 printf("        rMB/s        wMB/s");
405                 *fctr = 1024 * 1024;
406         }
407         else {
408                 printf("         rB/s         wB/s");
409                 *fctr = 1;
410         }
411         printf("    rops/s    wops/s         fo/s         fc/s         fd/s\n");
412 }
413
414 /*
415  ***************************************************************************
416  * Write CIFS stats read from /proc/fs/cifs/Stats.
417  *
418  * IN:
419  * @curr        Index in array for current sample statistics.
420  * @itv         Interval of time.
421  * @fctr        Conversion factor.
422  * @shi         Structures describing the CIFS filesystems.
423  * @ioi         Current sample statistics.
424  * @ioj         Previous sample statistics.
425  ***************************************************************************
426  */
427 void write_cifs_stat(int curr, unsigned long long itv, int fctr,
428                      struct io_hdr_stats *shi, struct cifs_stats *ioni,
429                      struct cifs_stats *ionj)
430 {
431         if (DISPLAY_HUMAN_READ(flags)) {
432                 cprintf_in(IS_STR, "%-22s\n", shi->name, 0);
433                 printf("%22s", "");
434         }
435         else {
436                 cprintf_in(IS_STR, "%-22s", shi->name, 0);
437         }
438
439         /*       rB/s   wB/s   fo/s   fc/s   fd/s*/
440         cprintf_f(2, 12, 2,
441                   S_VALUE(ionj->rd_bytes, ioni->rd_bytes, itv) / fctr,
442                   S_VALUE(ionj->wr_bytes, ioni->wr_bytes, itv) / fctr);
443         cprintf_f(2, 9, 2,
444                   S_VALUE(ionj->rd_ops, ioni->rd_ops, itv),
445                   S_VALUE(ionj->wr_ops, ioni->wr_ops, itv));
446         cprintf_f(3, 12, 2,
447                   S_VALUE(ionj->fopens, ioni->fopens, itv),
448                   S_VALUE(ionj->fcloses, ioni->fcloses, itv),
449                   S_VALUE(ionj->fdeletes, ioni->fdeletes, itv));
450         printf("\n");
451 }
452
453 /*
454  ***************************************************************************
455  * Print everything now (stats and uptime).
456  *
457  * IN:
458  * @curr        Index in array for current sample statistics.
459  * @rectime     Current date and time.
460  ***************************************************************************
461  */
462 void write_stats(int curr, struct tm *rectime)
463 {
464         int i, fctr = 1;
465         unsigned long long itv;
466         struct io_hdr_stats *shi;
467         struct cifs_stats *ioni, *ionj;
468
469         /* Test stdout */
470         TEST_STDOUT(STDOUT_FILENO);
471
472         /* Print time stamp */
473         if (DISPLAY_TIMESTAMP(flags)) {
474                 if (DISPLAY_ISO(flags)) {
475                         strftime(timestamp, sizeof(timestamp), "%FT%T%z", rectime);
476                 }
477                 else {
478                         strftime(timestamp, sizeof(timestamp), "%x %X", rectime);
479                 }
480                 printf("%s\n", timestamp);
481 #ifdef DEBUG
482                 if (DISPLAY_DEBUG(flags)) {
483                         fprintf(stderr, "%s\n", timestamp);
484                 }
485 #endif
486         }
487
488         /* Interval of time, reduced to one processor */
489         itv = get_interval(uptime0[!curr], uptime0[curr]);
490
491         shi = st_hdr_cifs;
492
493         /* Display CIFS stats header */
494         write_cifs_stat_header(&fctr);
495
496         for (i = 0; i < cifs_nr; i++, shi++) {
497                 if (shi->used) {
498                         ioni = st_cifs[curr]  + i;
499                         ionj = st_cifs[!curr] + i;
500 #ifdef DEBUG
501                         if (DISPLAY_DEBUG(flags)) {
502                                 /* Debug output */
503                                 fprintf(stderr, "name=%s itv=%llu fctr=%d ioni{ rd_bytes=%llu "
504                                                 "wr_bytes=%llu rd_ops=%llu wr_ops=%llu fopens=%llu "
505                                                 "fcloses=%llu fdeletes=%llu}\n",
506                                         shi->name, itv, fctr,
507                                         ioni->rd_bytes, ioni->wr_bytes,
508                                         ioni->rd_ops,   ioni->wr_ops,
509                                         ioni->fopens,   ioni->fcloses,
510                                         ioni->fdeletes);
511                         }
512 #endif
513                         write_cifs_stat(curr, itv, fctr, shi, ioni, ionj);
514                 }
515         }
516         printf("\n");
517 }
518
519 /*
520  ***************************************************************************
521  * Main loop: Read stats from the relevant sources and display them.
522  *
523  * IN:
524  * @count       Number of lines of stats to print.
525  * @rectime     Current date and time.
526  ***************************************************************************
527  */
528 void rw_io_stat_loop(long int count, struct tm *rectime)
529 {
530         int curr = 1;
531
532         /* Don't buffer data if redirected to a pipe */
533         setbuf(stdout, NULL);
534
535         do {
536                 /* Read system uptime (reduced to one processor) */
537                 uptime0[curr] = 0;
538                 read_uptime(&(uptime0[curr]));
539                 if (!uptime0[curr])
540                         /* Cannot read system uptime (/proc/uptime doesn't exist) */
541                         exit(2);
542
543                 /* Read CIFS stats */
544                 read_cifs_stat(curr);
545
546                 /* Get time */
547                 get_localtime(rectime, 0);
548
549                 /* Print results */
550                 write_stats(curr, rectime);
551
552                 if (count > 0) {
553                         count--;
554                 }
555
556                 if (count) {
557                         curr ^= 1;
558                         pause();
559                 }
560         }
561         while (count);
562 }
563
564 /*
565  ***************************************************************************
566  * Main entry to the cifsiostat program.
567  ***************************************************************************
568  */
569 int main(int argc, char **argv)
570 {
571         int it = 0;
572         int opt = 1;
573         int i;
574         long count = 1;
575         struct utsname header;
576         struct tm rectime;
577
578 #ifdef USE_NLS
579         /* Init National Language Support */
580         init_nls();
581 #endif
582
583         /* Init color strings */
584         init_colors();
585
586         /* Get HZ */
587         get_HZ();
588
589         /* Process args... */
590         while (opt < argc) {
591
592 #ifdef DEBUG
593                 if (!strcmp(argv[opt], "--debuginfo")) {
594                         flags |= I_D_DEBUG;
595                         opt++;
596                 } else
597 #endif
598                 if (!strncmp(argv[opt], "-", 1)) {
599                         for (i = 1; *(argv[opt] + i); i++) {
600
601                                 switch (*(argv[opt] + i)) {
602
603                                 case 'h':
604                                         /* Display an easy-to-read CIFS report */
605                                         flags |= I_D_HUMAN_READ;
606                                         break;
607
608                                 case 'k':
609                                         if (DISPLAY_MEGABYTES(flags)) {
610                                                 usage(argv[0]);
611                                         }
612                                         /* Display stats in kB/s */
613                                         flags |= I_D_KILOBYTES;
614                                         break;
615
616                                 case 'm':
617                                         if (DISPLAY_KILOBYTES(flags)) {
618                                                 usage(argv[0]);
619                                         }
620                                         /* Display stats in MB/s */
621                                         flags |= I_D_MEGABYTES;
622                                         break;
623
624                                 case 't':
625                                         /* Display timestamp */
626                                         flags |= I_D_TIMESTAMP;
627                                         break;
628
629                                 case 'V':
630                                         /* Print version number and exit */
631                                         print_version();
632                                         break;
633
634                                 default:
635                                         usage(argv[0]);
636                                 }
637                         }
638                         opt++;
639                 }
640
641                 else if (!it) {
642                         interval = atol(argv[opt++]);
643                         if (interval < 0) {
644                                 usage(argv[0]);
645                         }
646                         count = -1;
647                         it = 1;
648                 }
649
650                 else if (it > 0) {
651                         count = atol(argv[opt++]);
652                         if ((count < 1) || !interval) {
653                                 usage(argv[0]);
654                         }
655                         it = -1;
656                 }
657                 else {
658                         usage(argv[0]);
659                 }
660         }
661
662         if (!interval) {
663                 count = 1;
664         }
665
666         /* Init structures according to machine architecture */
667         io_sys_init();
668
669         get_localtime(&rectime, 0);
670
671         /* Get system name, release number and hostname */
672         uname(&header);
673         if (print_gal_header(&rectime, header.sysname, header.release,
674                              header.nodename, header.machine, cpu_nr)) {
675                 flags |= I_D_ISO;
676         }
677         printf("\n");
678
679         /* Set a handler for SIGALRM */
680         memset(&alrm_act, 0, sizeof(alrm_act));
681         alrm_act.sa_handler = alarm_handler;
682         sigaction(SIGALRM, &alrm_act, NULL);
683         alarm(interval);
684
685         /* Main loop */
686         rw_io_stat_loop(count, &rectime);
687
688         /* Free structures */
689         io_sys_free();
690
691         return 0;
692 }