]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - utils/nfsstat/nfsstat.c
nfsstat: add get_stats()
[nfs-utils.git] / utils / nfsstat / nfsstat.c
index 613e53a6abd77f7efd77d81a9803a0ea05574265..619ce8b7ff428d23b38ce4387e36c3cc1c6577e9 100644 (file)
@@ -4,7 +4,9 @@
  * Copyright (C) 1995-2005 Olaf Kirch <okir@suse.de>
  */
 
-#include "config.h"
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 #define NFSSVCSTAT     "/proc/net/rpc/nfsd"
 #define NFSCLTSTAT     "/proc/net/rpc/nfs"
@@ -27,6 +29,7 @@ static unsigned int   svcv3info[24];  /* NFSv3 call counts ([0] == 22) */
 static unsigned int    cltv3info[24];  /* NFSv3 call counts ([0] == 22) */
 static unsigned int    svcv4info[4];   /* NFSv4 call counts ([0] == 2) */
 static unsigned int    cltv4info[34];  /* NFSv4 call counts ([0] == 32) */
+static unsigned int    svcv4opinfo[42];/* NFSv4 call counts ([0] == 40) */
 static unsigned int    svcnetinfo[5];  /* 0  # of received packets
                                         * 1  UDP packets
                                         * 2  TCP packets
@@ -97,6 +100,16 @@ static const char * nfscltv4name[32] = {
        "statfs",    "readlink",  "readdir", "server_caps", "delegreturn",
 };
 
+static const char *     nfssvrv4opname[40] = {
+        "op0-unused",   "op1-unused", "op2-future",  "access",     "close",       "commit",
+        "create",       "delegpurge", "delegreturn", "getattr",    "getfh",       "link",
+        "lock",         "lockt",      "locku",       "lookup",     "lookup_root", "nverify",
+        "open",         "openattr",   "open_conf",   "open_dgrd",  "putfh",       "putpubfh",
+        "putrootfh",    "read",       "readdir",     "readlink",   "remove",      "rename",
+        "renew",        "restorefh",  "savefh",      "secinfo",    "setattr",     "setcltid",
+        "setcltidconf", "verify",     "write",       "rellockowner"
+};
+
 typedef struct statinfo {
        char            *tag;
        int             nrvals;
@@ -113,6 +126,7 @@ static statinfo             svcinfo[] = {
        { "proc2",      STRUCTSIZE(svcv2info),  svcv2info  },
        { "proc3",      STRUCTSIZE(svcv3info),  svcv3info  },
        { "proc4",      STRUCTSIZE(svcv4info),  svcv4info  },
+       { "proc4ops",   STRUCTSIZE(svcv4opinfo),svcv4opinfo},
        { NULL,         0,                      NULL       }
 };
 
@@ -135,6 +149,9 @@ static statinfo             *get_stat_info(const char *, struct statinfo *);
 
 static int             mounts(const char *);
 
+static void            get_stats(const char *, statinfo *, int *, int, const char *);
+static int             has_stats(const unsigned int *);
+
 #define PRNT_CALLS     0x0001
 #define PRNT_RPC       0x0002
 #define PRNT_NET       0x0004
@@ -201,8 +218,6 @@ main(int argc, char **argv)
        int             opt_all = 0,
                        opt_srv = 0,
                        opt_clt = 0,
-                       srv_info = 0,
-                       clt_info = 0,
                        opt_prt = 0;
        int             c;
        char           *progname;
@@ -297,25 +312,10 @@ main(int argc, char **argv)
                        "server.\n");
        }
 
-       if (opt_srv) {
-               srv_info = parse_statfile(NFSSVCSTAT, svcinfo);
-               if (srv_info == 0 && opt_clt == 0) {
-                       fprintf(stderr, "Warning: No Server Stats (%s: %m).\n", NFSSVCSTAT);
-                       return 2;
-               }
-               if (srv_info == 0)
-                       opt_srv = 0;
-       }
-
-       if (opt_clt) {
-               clt_info = parse_statfile(NFSCLTSTAT, cltinfo);
-               if (opt_srv == 0 && clt_info == 0) {
-                       fprintf(stderr, "Warning: No Client Stats (%s: %m).\n", NFSCLTSTAT);
-                       return 2;
-               }
-               if (clt_info == 0)
-                       opt_clt = 0;
-       }
+       if (opt_srv)
+               get_stats(NFSSVCSTAT, svcinfo, &opt_srv, opt_clt, "Server");
+       if (opt_clt)
+               get_stats(NFSCLTSTAT, cltinfo, &opt_clt, opt_srv, "Client");
 
        if (opt_srv) {
                if (opt_prt & PRNT_NET) {
@@ -369,21 +369,26 @@ main(int argc, char **argv)
                        printf("\n");
                }
                if (opt_prt & PRNT_CALLS) {
-                       if ((opt_prt & PRNT_V2) || ((opt_prt & PRNT_AUTO) && svcv2info[0] && svcv2info[svcv2info[0]+1] != svcv2info[0]))
+                       if ((opt_prt & PRNT_V2) || ((opt_prt & PRNT_AUTO) && has_stats(svcv2info)))
                                print_callstats(
                                "Server nfs v2:\n",
                                    nfsv2name, svcv2info + 1, sizeof(nfsv2name)/sizeof(char *)
                                );
-                       if ((opt_prt & PRNT_V3) || ((opt_prt & PRNT_AUTO) && svcv3info[0] && svcv3info[svcv3info[0]+1] != svcv3info[0]))
+                       if ((opt_prt & PRNT_V3) || ((opt_prt & PRNT_AUTO) && has_stats(svcv3info)))
                                print_callstats(
                                "Server nfs v3:\n",
                                nfsv3name, svcv3info + 1, sizeof(nfsv3name)/sizeof(char *)
                                );
-                       if ((opt_prt & PRNT_V4) || ((opt_prt & PRNT_AUTO) && svcv4info[0] && svcv4info[svcv4info[0]+1] != svcv4info[0]))
+                       if ((opt_prt & PRNT_V4) || ((opt_prt & PRNT_AUTO) && has_stats(svcv4info))) {
                                print_callstats(
                                "Server nfs v4:\n",
                                nfssvrv4name, svcv4info + 1, sizeof(nfssvrv4name)/sizeof(char *)
                                );
+                               print_callstats(
+                               "Server nfs v4 operations:\n",
+                               nfssvrv4opname, svcv4opinfo + 1, sizeof(nfssvrv4opname)/sizeof(char *)
+                               );
+                       }
                }
        }
 
@@ -405,17 +410,17 @@ main(int argc, char **argv)
                        printf("\n");
                }
                if (opt_prt & PRNT_CALLS) {
-                       if ((opt_prt & PRNT_V2) || ((opt_prt & PRNT_AUTO) && cltv2info[0] && cltv2info[cltv2info[0]+1] != cltv2info[0]))
+                       if ((opt_prt & PRNT_V2) || ((opt_prt & PRNT_AUTO) && has_stats(cltv2info)))
                                print_callstats(
                                "Client nfs v2:\n",
                                nfsv2name, cltv2info + 1,  sizeof(nfsv2name)/sizeof(char *)
                                );
-                       if ((opt_prt & PRNT_V3) || ((opt_prt & PRNT_AUTO) && cltv3info[0] && cltv3info[cltv3info[0]+1] != cltv3info[0]))
+                       if ((opt_prt & PRNT_V3) || ((opt_prt & PRNT_AUTO) && has_stats(cltv3info)))
                                print_callstats(
                                "Client nfs v3:\n",
                                nfsv3name, cltv3info + 1, sizeof(nfsv3name)/sizeof(char *)
                                );
-                       if ((opt_prt & PRNT_V4) || ((opt_prt & PRNT_AUTO) && cltv4info[0] && cltv4info[cltv4info[0]+1] != cltv4info[0]))
+                       if ((opt_prt & PRNT_V4) || ((opt_prt & PRNT_AUTO) && has_stats(cltv4info)))
                                print_callstats(
                                "Client nfs v4:\n",
                                nfscltv4name, cltv4info + 1,  sizeof(nfscltv4name)/sizeof(char *)
@@ -568,3 +573,21 @@ mounts(const char *name)
        fclose(fp);
        return 1;
 }
+
+static void
+get_stats(const char *file, statinfo *info, int *opt, int other_opt, const char *label)
+{
+       if (!parse_statfile(file, info)) {
+               if (!other_opt) {
+                       fprintf(stderr, "Warning: No %s Stats (%s: %m). \n", label, file);
+                       exit(2);
+               }
+               *opt = 0;
+       }
+}
+
+static int
+has_stats(const unsigned int *info)
+{
+       return (info[0] && info[info[0] + 1] != info[0]);
+}