]> granicus.if.org Git - sysstat/blob - cifsiostat.c
e03e839563a78a187976a42c96cd9d21b8a7fa6c
[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 "common.h"
34
35 #ifdef USE_NLS
36 #include <locale.h>
37 #include <libintl.h>
38 #define _(string) gettext(string)
39 #else
40 #define _(string) (string)
41 #endif
42
43 #define SCCSID "@(#)sysstat-" VERSION ": " __FILE__ " compiled " __DATE__ " " __TIME__
44 char *sccsid(void) { return (SCCSID); }
45
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;
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
59 /*
60  ***************************************************************************
61  * Print usage and exit.
62  *
63  * IN:
64  * @progname    Name of sysstat command.
65  ***************************************************************************
66  */
67 void usage(char *progname)
68 {
69         fprintf(stderr, _("Usage: %s [ options ] [ <interval> [ <count> ] ]\n"),
70                 progname);
71
72 #ifdef DEBUG
73         fprintf(stderr, _("Options are:\n"
74                           "[ --debuginfo ] [ -h ] [ -k | -m ] [ -t ] [ -V ]\n"));
75 #else
76         fprintf(stderr, _("Options are:\n"
77                           "[ -h ] [ -k | -m ] [ -t ] [ -V ]\n"));
78 #endif
79         exit(1);
80 }
81
82 /*
83  ***************************************************************************
84  * SIGALRM signal handler.
85  *
86  * IN:
87  * @sig Signal number. Set to 0 for the first time, then to SIGALRM.
88  ***************************************************************************
89  */
90 void alarm_handler(int sig)
91 {
92         signal(SIGALRM, alarm_handler);
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, 128, 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);
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         if ((st_hdr_cifs = (struct io_hdr_stats *) calloc(cifs_nr, IO_HDR_STATS_SIZE)) == NULL) {
178                 perror("malloc");
179                 exit(4);
180         }
181         
182         /* Allocate structures for number of CIFS directories found */
183         for (i = 0; i < 2; i++) {
184                 if ((st_cifs[i] =
185                     (struct cifs_stats *) calloc(cifs_nr, CIFS_STATS_SIZE)) == NULL) {
186                         perror("malloc");
187                         exit(4);
188                 }
189         }
190 }
191
192 /*
193  ***************************************************************************
194  * Free various structures.
195  ***************************************************************************
196 */
197 void io_sys_free(void)
198 {
199         int i;
200
201         /* Free CIFS directories structures */
202         for (i = 0; i < 2; i++) {
203
204                 if (st_cifs[i]) {
205                         free(st_cifs[i]);
206                 }
207         }
208         
209         if (st_hdr_cifs) {
210                 free(st_hdr_cifs);
211         }
212 }
213
214 /*
215  ***************************************************************************
216  * Save stats for current CIFS filesystem.
217  *
218  * IN:
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  ***************************************************************************
223  */
224 void save_stats(char *name, int curr, struct cifs_stats *st_io)
225 {
226         int i, j;
227         struct io_hdr_stats *st_hdr_cifs_i;
228         struct cifs_stats *st_cifs_i;
229
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))) {
235                         break;
236                 }
237         }
238
239         if (i == cifs_nr) {
240                 /*
241                  * This is a new filesystem: Look for an unused entry to store it.
242                  */
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);
252                                 break;
253                         }
254                 }
255                 if (i == cifs_nr) {
256                         /*
257                          * It is a new CIFS directory
258                          * but there is no free structure to store it.
259                          */
260
261                         /* All entries are used: The number has to be increased */
262                         cifs_nr = cifs_nr + 5;
263
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) {
267                                 perror("malloc");
268                                 exit(4);
269                         }
270
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;
276                         }
277
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) {
282                                         perror("malloc");
283                                         exit(4);
284                                 }
285                                 memset(st_cifs[j] + i, 0, 5 * CIFS_STATS_SIZE);
286                         }
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;
293                         *st_cifs_i = *st_io;
294                 }
295         } else {
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;
300                 *st_cifs_i = *st_io;
301         }
302         /*
303          * else it was a new CIFS directory
304          * but there was no free structure to store it.
305          */
306 }
307
308 /*
309  ***************************************************************************
310  * Read CIFS-mount directories stats from /proc/fs/cifs/Stats.
311  *
312  * IN:
313  * @curr        Index in array for current sample statistics.
314  ***************************************************************************
315  */
316 void read_cifs_stat(int curr)
317 {
318         FILE *fp;
319         char line[256];
320         char aux[32];
321         int start = 0;
322         char cifs_name[MAX_NAME_LEN];
323         char name_tmp[MAX_NAME_LEN];
324         struct cifs_stats scifs;
325
326         /* Every CIFS entry is potentially unregistered */
327         set_entries_inactive();
328
329         if ((fp = fopen(CIFSSTATS, "r")) == NULL)
330                 return;
331
332         sprintf(aux, "%%*d) %%%ds",
333                 MAX_NAME_LEN < 200 ? MAX_NAME_LEN - 1 : 200);
334
335         while (fgets(line, 256, fp) != NULL) {
336
337                 /* Read CIFS directory name */
338                 if (isdigit((unsigned char) line[0]) && sscanf(line, aux , name_tmp) == 1) {
339                         if (start) {
340                                 save_stats(cifs_name, curr, &scifs);
341                         }
342                         else {
343                                 start = 1;
344                         }
345                         strcpy(cifs_name, name_tmp);
346                 }
347                 else {
348                         if (!strncmp(line, "Reads:", 6)) {
349                                 sscanf(line, "Reads: %llu Bytes: %llu", &scifs.rd_ops, &scifs.rd_bytes);
350                         }
351                         if (!strncmp(line, "Writes:", 7)) {
352                                 sscanf(line, "Writes: %llu Bytes: %llu", &scifs.wr_ops, &scifs.wr_bytes);
353                         }
354                         if (!strncmp(line, "Opens:", 6)) {
355                                 sscanf(line, "Opens: %llu Closes:%llu Deletes: %llu",
356                                        &scifs.fopens, &scifs.fcloses, &scifs.fdeletes);
357                         }
358                 }
359         }
360         
361         if (start) {
362                 save_stats(cifs_name, curr, &scifs);
363         }
364
365         fclose(fp);
366
367         /* Free structures corresponding to unregistered filesystems */
368         free_inactive_entries();
369 }
370
371 /*
372  ***************************************************************************
373  * Display CIFS stats header.
374  *
375  * OUT:
376  * @fctr        Conversion factor.
377  ***************************************************************************
378  */
379 void write_cifs_stat_header(int *fctr)
380 {
381         printf("Filesystem:           ");
382         if (DISPLAY_KILOBYTES(flags)) {
383                 printf("        rkB/s        wkB/s");
384                 *fctr = 1024;
385         }
386         else if (DISPLAY_MEGABYTES(flags)) {
387                 printf("        rMB/s        wMB/s");
388                 *fctr = 1024 * 1024;
389         }
390         else {
391                 printf("         rB/s         wB/s");
392                 *fctr = 1;
393         }
394         printf("    rops/s    wops/s         fo/s         fc/s         fd/s\n");
395 }
396
397 /*
398  ***************************************************************************
399  * Write CIFS stats read from /proc/fs/cifs/Stats.
400  *
401  * IN:
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  ***************************************************************************
409  */
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)
413 {
414         if (DISPLAY_HUMAN_READ(flags)) {
415                 printf("%-22s\n%23s", shi->name, "");
416         }
417         else {
418                 printf("%-22s ", shi->name);
419         }
420
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));
430 }
431
432 /*
433  ***************************************************************************
434  * Print everything now (stats and uptime).
435  *
436  * IN:
437  * @curr        Index in array for current sample statistics.
438  * @rectime     Current date and time.
439  ***************************************************************************
440  */
441 void write_stats(int curr, struct tm *rectime)
442 {
443         int i, fctr = 1;
444         unsigned long long itv;
445         struct io_hdr_stats *shi;
446         struct cifs_stats *ioni, *ionj;
447
448         /* Test stdout */
449         TEST_STDOUT(STDOUT_FILENO);
450
451         /* Print time stamp */
452         if (DISPLAY_TIMESTAMP(flags)) {
453                 if (DISPLAY_ISO(flags)) {
454                         strftime(timestamp, sizeof(timestamp), "%FT%T%z", rectime);
455                 }
456                 else {
457                         strftime(timestamp, sizeof(timestamp), "%x %X", rectime);
458                 }
459                 printf("%s\n", timestamp);
460 #ifdef DEBUG
461                 if (DISPLAY_DEBUG(flags)) {
462                         fprintf(stderr, "%s\n", timestamp);
463                 }
464 #endif
465         }
466
467         /* Interval is multiplied by the number of processors */
468         itv = get_interval(uptime[!curr], uptime[curr]);
469
470         if (cpu_nr > 1) {
471                 /* On SMP machines, reduce itv to one processor (see note above) */
472                 itv = get_interval(uptime0[!curr], uptime0[curr]);
473         }
474
475         shi = st_hdr_cifs;
476
477         /* Display CIFS stats header */
478         write_cifs_stat_header(&fctr);
479
480         for (i = 0; i < cifs_nr; i++, shi++) {
481                 if (shi->used) {
482                         ioni = st_cifs[curr]  + i;
483                         ionj = st_cifs[!curr] + i;
484 #ifdef DEBUG
485                         if (DISPLAY_DEBUG(flags)) {
486                                 /* Debug output */
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,
494                                         ioni->fdeletes);
495                         }
496 #endif
497                         write_cifs_stat(curr, itv, fctr, shi, ioni, ionj);
498                 }
499         }
500         printf("\n");
501 }
502
503 /*
504  ***************************************************************************
505  * Main loop: Read stats from the relevant sources and display them.
506  *
507  * IN:
508  * @count       Number of lines of stats to print.
509  * @rectime     Current date and time.
510  ***************************************************************************
511  */
512 void rw_io_stat_loop(long int count, struct tm *rectime)
513 {
514         int curr = 1;
515
516         /* Don't buffer data if redirected to a pipe */
517         setbuf(stdout, NULL);
518         
519         do {
520                 if (cpu_nr > 1) {
521                         /*
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.
525                          */
526                         uptime0[curr] = 0;
527                         read_uptime(&(uptime0[curr]));
528                 }
529
530                 /* Read CIFS stats */
531                 read_cifs_stat(curr);
532
533                 /* Get time */
534                 get_localtime(rectime);
535
536                 /* Print results */
537                 write_stats(curr, rectime);
538
539                 if (count > 0) {
540                         count--;
541                 }
542
543                 if (count) {
544                         curr ^= 1;
545                         pause();
546                 }
547         }
548         while (count);
549 }
550
551 /*
552  ***************************************************************************
553  * Main entry to the cifsiostat program.
554  ***************************************************************************
555  */
556 int main(int argc, char **argv)
557 {
558         int it = 0;
559         int opt = 1;
560         int i;
561         long count = 1;
562         struct utsname header;
563         struct tm rectime;
564
565 #ifdef USE_NLS
566         /* Init National Language Support */
567         init_nls();
568 #endif
569
570         /* Get HZ */
571         get_HZ();
572
573         /* Process args... */
574         while (opt < argc) {
575
576 #ifdef DEBUG
577                 if (!strcmp(argv[opt], "--debuginfo")) {
578                         flags |= I_D_DEBUG;
579                         opt++;
580                 } else
581 #endif
582                 if (!strncmp(argv[opt], "-", 1)) {
583                         for (i = 1; *(argv[opt] + i); i++) {
584
585                                 switch (*(argv[opt] + i)) {
586
587                                 case 'h':
588                                         /* Display an easy-to-read CIFS report */
589                                         flags |= I_D_HUMAN_READ;
590                                         break;
591         
592                                 case 'k':
593                                         if (DISPLAY_MEGABYTES(flags)) {
594                                                 usage(argv[0]);
595                                         }
596                                         /* Display stats in kB/s */
597                                         flags |= I_D_KILOBYTES;
598                                         break;
599
600                                 case 'm':
601                                         if (DISPLAY_KILOBYTES(flags)) {
602                                                 usage(argv[0]);
603                                         }
604                                         /* Display stats in MB/s */
605                                         flags |= I_D_MEGABYTES;
606                                         break;
607
608                                 case 't':
609                                         /* Display timestamp */
610                                         flags |= I_D_TIMESTAMP;
611                                         break;
612
613                                 case 'V':
614                                         /* Print version number and exit */
615                                         print_version();
616                                         break;
617         
618                                 default:
619                                         usage(argv[0]);
620                                 }
621                         }
622                         opt++;
623                 }
624
625                 else if (!it) {
626                         interval = atol(argv[opt++]);
627                         if (interval < 0) {
628                                 usage(argv[0]);
629                         }
630                         count = -1;
631                         it = 1;
632                 }
633
634                 else if (it > 0) {
635                         count = atol(argv[opt++]);
636                         if ((count < 1) || !interval) {
637                                 usage(argv[0]);
638                         }
639                         it = -1;
640                 }
641                 else {
642                         usage(argv[0]);
643                 }
644         }
645
646         if (!interval) {
647                 count = 1;
648         }
649
650         /* Init structures according to machine architecture */
651         io_sys_init();
652
653         get_localtime(&rectime);
654
655         /* Get system name, release number and hostname */
656         uname(&header);
657         if (print_gal_header(&rectime, header.sysname, header.release,
658                              header.nodename, header.machine, cpu_nr)) {
659                 flags |= I_D_ISO;
660         }
661         printf("\n");
662
663         /* Set a handler for SIGALRM */
664         alarm_handler(0);
665
666         /* Main loop */
667         rw_io_stat_loop(count, &rectime);
668
669         /* Free structures */
670         io_sys_free();
671
672         return 0;
673 }