2 * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
4 * U.S. Government Rights - Commercial software. Government users are subject
5 * to the Sun Microsystems, Inc. standard license agreement and applicable
6 * provisions of the FAR and its supplements.
9 * This distribution may include materials developed by third parties. Sun,
10 * Sun Microsystems, the Sun logo and Solaris are trademarks or registered
11 * trademarks of Sun Microsystems, Inc. in the U.S. and other countries.
18 * nfsstat: Network File System statistics for client RPC
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/t_lock.h>
34 #include <sys/tiuser.h>
35 #include <sys/statvfs.h>
36 #include <sys/mntent.h>
37 #include <sys/mnttab.h>
38 #include <sys/sysmacros.h>
39 #include <sys/mkdev.h>
40 #include <rpc/types.h>
45 #include <nfs/nfs_clnt.h>
47 static kstat_ctl_t
*kc
= NULL
; /* libkstat cookie */
48 static kstat_t
*rpc_clts_client_kstat
;
49 static kstat_t
*rpc_cots_client_kstat
;
51 static void getstats(void);
53 static void clear_setup_data();
54 static void fail(int, char *, ...);
56 static kid_t
safe_kstat_read(kstat_ctl_t
*, kstat_t
*, void *);
58 #define MAX_COLUMNS 80
60 static int field_width
= 0;
63 int cflag
= 0; /* client stats */
64 int rflag
= 0; /* rpc stats */
65 int zflag
= 0; /* zero stats after printing */
67 static void stat_width(kstat_t
*);
73 if (rpc_clts_client_kstat
!= NULL
) {
74 safe_kstat_read(kc
, rpc_clts_client_kstat
, NULL
);
75 stat_width(rpc_clts_client_kstat
);
77 if (rpc_cots_client_kstat
!= NULL
) {
78 safe_kstat_read(kc
, rpc_cots_client_kstat
, NULL
);
79 stat_width(rpc_cots_client_kstat
);
89 * Verify and close the kstat control structure
95 * Set all the related variables to null
97 kc
= (kstat_ctl_t
*)NULL
;
98 rpc_clts_client_kstat
= (kstat_t
*)NULL
;
99 rpc_cots_client_kstat
= (kstat_t
*)NULL
;
106 /* Initialize kstat control structures */
107 if ((kc
= kstat_open()) == NULL
)
108 fail(1, "kstat_open(): can't open /dev/kstat");
110 /* Find a kstat by name */
111 rpc_clts_client_kstat
= kstat_lookup(kc
, "unix", 0, "rpc_clts_client");
112 rpc_cots_client_kstat
= kstat_lookup(kc
, "unix", 0, "rpc_cots_client");
116 stat_width(kstat_t
*req
)
122 knp
= KSTAT_NAMED_PTR(req
);
123 nreq
= req
->ks_ndata
;
125 for (i
= 0; i
< nreq
; i
++) {
126 len
= strlen(knp
[i
].name
) + 1;
127 if (field_width
< len
)
129 (void) sprintf(fixlen
, "%lu", knp
[i
].value
.ul
);
130 len
= strlen(fixlen
) + 1;
131 if (field_width
< len
)
137 * Get Client RPC data
138 * This function is called by Health Monitoring Module
140 krgetclientrpcdetail(double *calls
, int *badcalls
, int *retrans
, int *badxids
, int *timeouts
, int *newcreds
, int *badverfs
, int *timers
, int *nomem
, int *cantsend
)
155 ncolumns
= (MAX_COLUMNS
- 1) / field_width
;
157 /* Kstat lookup client rpc info */
158 if (rpc_clts_client_kstat
!= NULL
) {
160 knp
= KSTAT_NAMED_PTR(rpc_clts_client_kstat
);
161 nreq
= rpc_clts_client_kstat
->ks_ndata
;
163 for (i
= 0; i
< nreq
; i
+= ncolumns
) {
164 /* Don't print heading */
166 for (j = i; j < MIN(i + ncolumns, nreq); j++) {
167 printf("%-*s", field_width, knp[j].name);
171 for (j
= i
; j
< MIN(i
+ ncolumns
, nreq
); j
++) {
172 #if (defined SOLARIS2_5 || defined SOLARIS2_6)
173 (void) sprintf(fixlen
, "%lu ", knp
[j
].value
.ul
);
175 (void) sprintf(fixlen
, "%llu ", knp
[j
].value
.ui64
);
183 *calls
= atof(strtok(buf
, " "));
184 *badcalls
= atoi (strtok(NULL
, " "));
185 *retrans
= atoi (strtok(NULL
, " "));
186 *badxids
= atoi (strtok(NULL
, " "));
187 *timeouts
= atoi (strtok(NULL
, " "));
188 *newcreds
= atoi (strtok(NULL
, " "));
189 *badverfs
= atoi (strtok(NULL
, " "));
190 *timers
= atoi (strtok(NULL
, " "));
191 *nomem
= atoi (strtok(NULL
, " "));
192 *cantsend
= atoi (strtok(NULL
, " "));
195 * The following function closes the kstat control structure
196 * and sets the related variables to NULL.
203 #define MIN(a, b) ((a) < (b) ? (a) : (b))
206 fail(int do_perror
, char *message
, ...)
210 va_start(args
, message
);
211 fprintf(stderr
, "nfsstat: ");
212 vfprintf(stderr
, message
, args
);
215 fprintf(stderr
, ": %s", strerror(errno
));
216 fprintf(stderr
, "\n");
221 safe_kstat_read(kstat_ctl_t
*kc
, kstat_t
*ksp
, void *data
)
223 kid_t kstat_chain_id
= kstat_read(kc
, ksp
, data
);
225 if (kstat_chain_id
== -1)
226 fail(1, "kstat_read(%x, '%s') failed", kc
, ksp
->ks_name
);
227 return (kstat_chain_id
);