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); }
50 void int_handler(int n) { return; }
53 unsigned long long uptime_cs[2] = {0, 0};
54 struct io_cifs *cifs_list = NULL;
56 int cpu_nr = 0; /* Nb of processors on the machine */
57 int flags = 0; /* Flag for common options and system state */
58 int dplaces_nr = -1; /* Number of decimal places */
61 char timestamp[TIMESTAMP_LEN];
63 struct sigaction alrm_act;
66 ***************************************************************************
67 * Print usage and exit.
70 * @progname Name of sysstat command.
71 ***************************************************************************
73 void usage(char *progname)
75 fprintf(stderr, _("Usage: %s [ options ] [ <interval> [ <count> ] ]\n"),
79 fprintf(stderr, _("Options are:\n"
80 "[ --dec={ 0 | 1 | 2 } ] [ --human ] [ --pretty ]\n"
81 "[ -h ] [ -k | -m ] [ -t ] [ -V ] [ --debuginfo ]\n"));
83 fprintf(stderr, _("Options are:\n"
84 "[ --dec={ 0 | 1 | 2 } ] [ --human ] [ --pretty ]\n"
85 "[ -h ] [ -k | -m ] [ -t ] [ -V ]\n"));
91 ***************************************************************************
92 * SIGALRM signal handler.
96 ***************************************************************************
98 void alarm_handler(int sig)
104 ***************************************************************************
105 * Set every cifs entry to nonexistent status.
108 * @clist Pointer on the start of the linked list.
109 ***************************************************************************
111 void set_cifs_nonexistent(struct io_cifs *clist)
113 while (clist != NULL) {
114 clist->exist = FALSE;
120 ***************************************************************************
121 * Check if a cifs filesystem is present in the list, and add it if requested.
124 * @clist Address of pointer on the start of the linked list.
128 * Pointer on the io_cifs structure in the list where the cifs is located
129 * (whether it was already in the list or if it has been added).
130 * NULL if the cifs name is too long or if the cifs doesn't exist and we
131 * don't want to add it.
132 ***************************************************************************
134 struct io_cifs *add_list_cifs(struct io_cifs **clist, char *name)
136 struct io_cifs *c, *cs;
139 if (strnlen(name, MAX_NAME_LEN) == MAX_NAME_LEN)
140 /* cifs name is too long */
143 while (*clist != NULL) {
145 if ((i = strcmp(c->name, name)) == 0) {
146 /* cifs found in list */
152 * If no group defined and we don't use /proc/diskstats,
153 * insert current device in alphabetical order.
154 * NB: Using /proc/diskstats ("iostat -p ALL") is a bit better than
155 * using alphabetical order because sda10 comes after sda9...
165 /* Add cifs to the list */
166 if ((*clist = (struct io_cifs *) malloc(IO_CIFS_SIZE)) == NULL) {
170 memset(*clist, 0, IO_CIFS_SIZE);
173 for (i = 0; i < 2; i++) {
174 if ((c->cifs_stats[i] = (struct cifs_st *) malloc(CIFS_ST_SIZE)) == NULL) {
178 memset(c->cifs_stats[i], 0, CIFS_ST_SIZE);
180 strncpy(c->name, name, sizeof(c->name));
181 c->name[sizeof(c->name) - 1] = '\0';
189 ***************************************************************************
190 * Read CIFS-mount directories stats from /proc/fs/cifs/Stats.
193 * @curr Index in array for current sample statistics.
194 ***************************************************************************
196 void read_cifs_stat(int curr)
202 long long unsigned aux_open;
203 long long unsigned all_open = 0;
204 char cifs_name[MAX_NAME_LEN];
205 char name_tmp[MAX_NAME_LEN];
206 struct cifs_st scifs;
209 if ((fp = fopen(CIFSSTATS, "r")) == NULL)
212 sprintf(aux, "%%*d) %%%ds",
213 MAX_NAME_LEN < 200 ? MAX_NAME_LEN - 1 : 200);
215 memset(&scifs, 0, CIFS_ST_SIZE);
217 while (fgets(line, sizeof(line), fp) != NULL) {
219 /* Read CIFS directory name */
220 if (isdigit((unsigned char) line[0]) && sscanf(line, aux , name_tmp) == 1) {
222 scifs.fopens = all_open;
223 ci = add_list_cifs(&cifs_list, cifs_name);
225 *ci->cifs_stats[curr] = scifs;
232 strncpy(cifs_name, name_tmp, sizeof(cifs_name));
233 cifs_name[sizeof(cifs_name) - 1] = '\0';
234 memset(&scifs, 0, CIFS_ST_SIZE);
237 if (!strncmp(line, "Reads:", 6)) {
239 * SMB1 format: Reads: %llu Bytes: %llu
240 * SMB2 format: Reads: %llu sent %llu failed
241 * If this is SMB2 format then only the first variable (rd_ops) will be set.
243 sscanf(line, "Reads: %llu Bytes: %llu", &scifs.rd_ops, &scifs.rd_bytes);
245 else if (!strncmp(line, "Bytes read:", 11)) {
246 sscanf(line, "Bytes read: %llu Bytes written: %llu",
247 &scifs.rd_bytes, &scifs.wr_bytes);
249 else if (!strncmp(line, "Writes:", 7)) {
251 * SMB1 format: Writes: %llu Bytes: %llu
252 * SMB2 format: Writes: %llu sent %llu failed
253 * If this is SMB2 format then only the first variable (wr_ops) will be set.
255 sscanf(line, "Writes: %llu Bytes: %llu", &scifs.wr_ops, &scifs.wr_bytes);
257 else if (!strncmp(line, "Opens:", 6)) {
258 sscanf(line, "Opens: %llu Closes:%llu Deletes: %llu",
259 &aux_open, &scifs.fcloses, &scifs.fdeletes);
260 all_open += aux_open;
262 else if (!strncmp(line, "Posix Opens:", 12)) {
263 sscanf(line, "Posix Opens: %llu", &aux_open);
264 all_open += aux_open;
266 else if (!strncmp(line, "Open files:", 11)) {
267 sscanf(line, "Open files: %llu total (local), %llu",
268 &all_open, &aux_open);
269 all_open += aux_open;
271 else if (!strncmp(line, "Closes:", 7)) {
272 sscanf(line, "Closes: %llu", &scifs.fcloses);
278 scifs.fopens = all_open;
279 ci = add_list_cifs(&cifs_list, cifs_name);
281 *ci->cifs_stats[curr] = scifs;
289 ***************************************************************************
290 * Display CIFS stats header.
293 * @fctr Conversion factor.
294 ***************************************************************************
296 void write_cifs_stat_header(int *fctr)
298 if (!DISPLAY_PRETTY(flags)) {
299 printf("Filesystem ");
301 if (DISPLAY_KILOBYTES(flags)) {
302 printf(" rkB/s wkB/s");
305 else if (DISPLAY_MEGABYTES(flags)) {
306 printf(" rMB/s wMB/s");
310 printf(" rB/s wB/s");
313 printf(" rops/s wops/s fo/s fc/s fd/s");
314 if (DISPLAY_PRETTY(flags)) {
315 printf(" Filesystem");
321 ***************************************************************************
322 * Write CIFS stats read from /proc/fs/cifs/Stats.
325 * @curr Index in array for current sample statistics.
326 * @itv Interval of time (in 1/100th of a second).
327 * @fctr Conversion factor.
328 * @clist Pointer on the linked list where the cifs is saved.
329 * @ioi Current sample statistics.
330 * @ioj Previous sample statistics.
331 ***************************************************************************
333 void write_cifs_stat(int curr, unsigned long long itv, int fctr,
334 struct io_cifs *clist, struct cifs_st *ioni,
335 struct cifs_st *ionj)
337 double rbytes, wbytes;
339 if (!DISPLAY_PRETTY(flags)) {
340 cprintf_in(IS_STR, "%-22s", clist->name, 0);
343 /* rB/s wB/s fo/s fc/s fd/s*/
344 rbytes = S_VALUE(ionj->rd_bytes, ioni->rd_bytes, itv);
345 wbytes = S_VALUE(ionj->wr_bytes, ioni->wr_bytes, itv);
346 if (!DISPLAY_UNIT(flags)) {
350 cprintf_f(DISPLAY_UNIT(flags) ? UNIT_BYTE : NO_UNIT, 2, 12, 2,
352 cprintf_f(NO_UNIT, 2, 9, 2,
353 S_VALUE(ionj->rd_ops, ioni->rd_ops, itv),
354 S_VALUE(ionj->wr_ops, ioni->wr_ops, itv));
355 cprintf_f(NO_UNIT, 3, 12, 2,
356 S_VALUE(ionj->fopens, ioni->fopens, itv),
357 S_VALUE(ionj->fcloses, ioni->fcloses, itv),
358 S_VALUE(ionj->fdeletes, ioni->fdeletes, itv));
359 if (DISPLAY_PRETTY(flags)) {
360 cprintf_in(IS_STR, " %s", clist->name, 0);
366 ***************************************************************************
367 * Print everything now (stats and uptime).
370 * @curr Index in array for current sample statistics.
371 * @rectime Current date and time.
372 ***************************************************************************
374 void write_stats(int curr, struct tm *rectime)
377 unsigned long long itv;
378 struct io_cifs *clist;
379 struct cifs_st *ioni, *ionj;
382 TEST_STDOUT(STDOUT_FILENO);
384 /* Print time stamp */
385 if (DISPLAY_TIMESTAMP(flags)) {
386 if (DISPLAY_ISO(flags)) {
387 strftime(timestamp, sizeof(timestamp), "%FT%T%z", rectime);
390 strftime(timestamp, sizeof(timestamp), "%x %X", rectime);
392 printf("%s\n", timestamp);
394 if (DISPLAY_DEBUG(flags)) {
395 fprintf(stderr, "%s\n", timestamp);
400 /* Interval of time, reduced to one processor */
401 itv = get_interval(uptime_cs[!curr], uptime_cs[curr]);
403 /* Display CIFS stats header */
404 write_cifs_stat_header(&fctr);
406 for (clist = cifs_list; clist != NULL; clist = clist->next) {
409 /* Current cifs non existent */
412 ioni = clist->cifs_stats[curr];
413 ionj = clist->cifs_stats[!curr];
416 if (DISPLAY_DEBUG(flags)) {
418 fprintf(stderr, "name=%s itv=%llu fctr=%d ioni{ rd_bytes=%llu "
419 "wr_bytes=%llu rd_ops=%llu wr_ops=%llu fopens=%llu "
420 "fcloses=%llu fdeletes=%llu}\n",
421 clist->name, itv, fctr,
422 ioni->rd_bytes, ioni->wr_bytes,
423 ioni->rd_ops, ioni->wr_ops,
424 ioni->fopens, ioni->fcloses,
428 write_cifs_stat(curr, itv, fctr, clist, ioni, ionj);
434 ***************************************************************************
435 * Main loop: Read stats from the relevant sources and display them.
438 * @count Number of lines of stats to print.
439 * @rectime Current date and time.
440 ***************************************************************************
442 void rw_io_stat_loop(long int count, struct tm *rectime)
446 /* Set a handler for SIGALRM */
447 memset(&alrm_act, 0, sizeof(alrm_act));
448 alrm_act.sa_handler = alarm_handler;
449 sigaction(SIGALRM, &alrm_act, NULL);
453 /* Every device is potentially nonexistent */
454 set_cifs_nonexistent(cifs_list);
456 /* Read system uptime in 1/100th of a second */
457 read_uptime(&(uptime_cs[curr]));
459 /* Read CIFS stats */
460 read_cifs_stat(curr);
463 get_localtime(rectime, 0);
466 write_stats(curr, rectime);
481 ***************************************************************************
482 * Main entry to the cifsiostat program.
483 ***************************************************************************
485 int main(int argc, char **argv)
491 struct utsname header;
495 /* Init National Language Support */
499 /* Init color strings */
502 /* Process args... */
506 if (!strcmp(argv[opt], "--debuginfo")) {
512 if (!strcmp(argv[opt], "--human")) {
517 else if (!strcmp(argv[opt], "--pretty")) {
518 /* Display an easy-to-read CIFS report */
523 else if (!strncmp(argv[opt], "--dec=", 6) && (strlen(argv[opt]) == 7)) {
524 /* Get number of decimal places */
525 dplaces_nr = atoi(argv[opt] + 6);
526 if ((dplaces_nr < 0) || (dplaces_nr > 2)) {
532 else if (!strncmp(argv[opt], "-", 1)) {
533 for (i = 1; *(argv[opt] + i); i++) {
535 switch (*(argv[opt] + i)) {
538 /* Option -h is equivalent to --pretty --human */
539 flags |= I_D_PRETTY + I_D_UNIT;
543 if (DISPLAY_MEGABYTES(flags)) {
546 /* Display stats in kB/s */
547 flags |= I_D_KILOBYTES;
551 if (DISPLAY_KILOBYTES(flags)) {
554 /* Display stats in MB/s */
555 flags |= I_D_MEGABYTES;
559 /* Display timestamp */
560 flags |= I_D_TIMESTAMP;
564 /* Print version number and exit */
576 interval = atol(argv[opt++]);
585 count = atol(argv[opt++]);
586 if ((count < 1) || !interval) {
600 /* How many processors on this machine? */
601 cpu_nr = get_cpu_nr(~0, FALSE);
603 get_localtime(&rectime, 0);
606 * Don't buffer data if redirected to a pipe.
607 * Note: With musl-c, the behavior of this function is undefined except
608 * when it is the first operation on the stream.
610 setbuf(stdout, NULL);
612 /* Get system name, release number and hostname */
614 if (print_gal_header(&rectime, header.sysname, header.release,
615 header.nodename, header.machine, cpu_nr,
622 rw_io_stat_loop(count, &rectime);