xref: /illumos-gate/usr/src/cmd/smbsrv/smbstat/smbstat.c (revision fcdb3229a31dd4ff700c69238814e326aad49098)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2020 Nexenta by DDN, Inc.  All rights reserved.
25  * Copyright 2023 RackTop Systems, Inc.
26  */
27 
28 /*
29  * smbstat: Server Message Block File System statistics
30  *
31  * The statistics this CLI displays come from two sources:
32  *
33  * 1) The kernel module 'smbsrv'.
34  * 2) The SMB workers task queue statistics the task queue manager of Solaris
35  *    maintains.
36  *
37  * The flow of the code is the following:
38  *
39  *
40  *			+----------------+
41  *			| Initialization |
42  *			+----------------+
43  *				|
44  *				|
45  *				v
46  *		  +--------------------------*
47  *		  | Take a snapshot the data | <--------+
48  *		  +--------------------------+		|
49  *				|			|
50  *				|			|
51  *				v			|
52  *		    +----------------------+		|
53  *		    | Process the snapshot |		|
54  *		    +----------------------+		|
55  *				|			|
56  *				|			|
57  *				v			|
58  *	     +------------------------------------+	|
59  *	     | Print the result of the processing |	|
60  *	     +------------------------------------+	|
61  *				|			|
62  *				|			|
63  *				v			|
64  *		Yes	---------------			|
65  *	+------------ < interval == 0 ? >		|
66  *	|		---------------			|
67  *	|		       |			|
68  *	|		       | No			|
69  *	|		       v			|
70  *	|	   +------------------------+		|
71  *	|	   | Sleep for the duration | ----------+
72  *	|	   |   of the interval.     |
73  *	|	   +------------------------+
74  *	|
75  *	+---------------------+
76  *			      |
77  *			      v
78  *
79  *			    Exit
80  *
81  * There are two sets of snapshots. One set for the smbsrv module and the other
82  * for the task queue (SMB workers). Each set contains 2 snapshots. One is
83  * labeled 'current' the other one 'previous'. Their role changes after each
84  * snapshot. The 'current' becomes 'previous' and vice versa.
85  * The first snapshot taken is compared against the data gathered since the
86  * smbsrv module was loaded. Subsequent snapshots will be compared against the
87  * previous snapshot.
88  */
89 
90 #include <stdio.h>
91 #include <stdlib.h>
92 #include <unistd.h>
93 #include <kstat.h>
94 #include <stdarg.h>
95 #include <errno.h>
96 #include <inttypes.h>
97 #include <strings.h>
98 #include <utility.h>
99 #include <libintl.h>
100 #include <zone.h>
101 #include <termios.h>
102 #include <stropts.h>
103 #include <math.h>
104 #include <umem.h>
105 #include <locale.h>
106 #include <sys/processor.h>
107 #include <smbsrv/smb_kstat.h>
108 #include <smbsrv/smb.h>
109 #include <smbsrv/smb2.h>
110 
111 #if !defined(TEXT_DOMAIN)
112 #define	TEXT_DOMAIN "SYS_TEST"
113 #endif /* TEXT_DOMAIN */
114 
115 #define	SMBSTAT_ID_NO_CPU	-1
116 #define	SMBSTAT_SNAPSHOT_COUNT	2		/* Must be a power of 2 */
117 #define	SMBSTAT_SNAPSHOT_MASK	(SMBSTAT_SNAPSHOT_COUNT - 1)
118 
119 #define	SMBSTAT_HELP	\
120 	"Usage: smbstat [-acnrtuz] [interval]\n" \
121 	"    -c: display counters\n" \
122 	"    -t: display throughput\n" \
123 	"    -u: display utilization\n" \
124 	"    -r: display requests\n" \
125 	"        -a: all the requests (supported and unsupported)\n" \
126 	"        -z: skip the requests not received\n" \
127 	"        -n: display in alphabetic order\n" \
128 	"    interval: refresh cycle in seconds\n"
129 
130 #define	SMBSRV_COUNTERS_BANNER	"\n  nbt   tcp users trees files pipes\n"
131 #define	SMBSRV_COUNTERS_FORMAT	"%5d %5d %5d %5d %5d %5d\n"
132 
133 #define	SMBSRV_THROUGHPUT_BANNER	\
134 	"\nrbytes/s   tbytes/s    reqs/s     reads/s   writes/s\n"
135 #define	SMBSRV_THROUGHPUT_FORMAT	\
136 	"%1.3e  %1.3e  %1.3e  %1.3e  %1.3e\n"
137 
138 #define	SMBSRV_UTILIZATION_BANNER	\
139 	"\n  wcnt       rcnt       wtime      rtime" \
140 	"     w%%   r%%   u%%  sat usr%% sys%%  idle%%\n"
141 #define	SMBSRV_UTILIZATION_FORMAT	\
142 	"%1.3e  %1.3e  %1.3e  %1.3e  %3.0f  %3.0f  %3.0f  %s " \
143 	"%3.0f  %3.0f    %3.0f\n"
144 
145 #define	SMBSRV_REQUESTS_BANNER	\
146 	"\n%30s code   %%   rbytes/s   tbytes/s     req/s     rt-mean"	\
147 	"   rt-stddev\n"
148 #define	SMBSRV_REQUESTS_FORMAT	\
149 	"%30s  %02X   %3.0f  %1.3e  %1.3e  %1.3e  %1.3e  %1.3e\n"
150 
151 typedef enum {
152 	CPU_TICKS_IDLE = 0,
153 	CPU_TICKS_USER,
154 	CPU_TICKS_KERNEL,
155 	CPU_TICKS_SENTINEL
156 } cpu_state_idx_t;
157 
158 typedef struct smbstat_cpu_snapshot {
159 	processorid_t	cs_id;
160 	int		cs_state;
161 	uint64_t	cs_ticks[CPU_TICKS_SENTINEL];
162 } smbstat_cpu_snapshot_t;
163 
164 typedef struct smbstat_srv_snapshot {
165 	hrtime_t	ss_snaptime;
166 	smbsrv_kstats_t	ss_data;
167 } smbstat_srv_snapshot_t;
168 
169 typedef struct smbstat_wrk_snapshot {
170 	uint64_t	ws_nbacklog;
171 } smbstat_wrk_snapshot_t;
172 
173 typedef struct smbstat_req_info {
174 	char		ri_name[KSTAT_STRLEN];
175 	int		ri_opcode;
176 	double		ri_pct;
177 	double		ri_tbs;
178 	double		ri_rbs;
179 	double		ri_rqs;
180 	double		ri_stddev;
181 	double		ri_mean;
182 } smbstat_req_info_t;
183 
184 typedef struct smbstat_srv_info {
185 	double		si_hretime;
186 	double		si_etime;
187 	double		si_total_nreqs;
188 	/*
189 	 * Counters
190 	 */
191 	uint32_t	si_nbt_sess;	/* NBT sessions */
192 	uint32_t	si_tcp_sess;	/* TCP sessions */
193 	uint32_t	si_users;	/* Users logged in */
194 	uint32_t	si_trees;	/* Trees connected */
195 	uint32_t	si_files;	/* Open files */
196 	uint32_t	si_pipes;	/* Open pipes */
197 	/*
198 	 * Throughput of the server
199 	 */
200 	double		si_tbs;		/* Bytes transmitted / second */
201 	double		si_rbs;		/* Bytes received / second */
202 	double		si_rqs;		/* Requests treated / second */
203 	double		si_rds;		/* Reads treated / second */
204 	double		si_wrs;		/* Writes treated / second */
205 	/*
206 	 * Utilization of the server
207 	 */
208 	double		si_wpct;	/* */
209 	double		si_rpct;	/* */
210 	double		si_upct;	/* Utilization in % */
211 	double		si_avw;		/* Average number of requests waiting */
212 	double		si_avr;		/* Average number of requests running */
213 	double		si_wserv;	/* Average waiting time */
214 	double		si_rserv;	/* Average running time */
215 	boolean_t	si_sat;
216 	double		si_ticks[CPU_TICKS_SENTINEL];
217 	/*
218 	 * Latency & Throughput per request
219 	 */
220 	smbstat_req_info_t	si_reqs1[SMBSRV_KS_NREQS1];
221 	smbstat_req_info_t	si_reqs2[SMBSRV_KS_NREQS2];
222 } smbstat_srv_info_t;
223 
224 static void smbstat_init(void);
225 static void smbstat_fini(void);
226 static void smbstat_kstat_snapshot(void);
227 static void smbstat_kstat_process(void);
228 static void smbstat_kstat_print(void);
229 
230 static void smbstat_print_counters(void);
231 static void smbstat_print_throughput(void);
232 static void smbstat_print_utilization(void);
233 static void smbstat_print_requests(void);
234 
235 static void smbstat_cpu_init(void);
236 static void smbstat_cpu_fini(void);
237 static smbstat_cpu_snapshot_t *smbstat_cpu_current_snapshot(void);
238 static smbstat_cpu_snapshot_t *smbstat_cpu_previous_snapshot(void);
239 static void smbstat_cpu_snapshot(void);
240 static void smbstat_cpu_process(void);
241 
242 static void smbstat_wrk_init(void);
243 static void smbstat_wrk_fini(void);
244 static void smbstat_wrk_snapshot(void);
245 static void smbstat_wrk_process(void);
246 static smbstat_wrk_snapshot_t *smbstat_wrk_current_snapshot(void);
247 
248 static void smbstat_srv_init(void);
249 static void smbstat_srv_fini(void);
250 static void smbstat_srv_snapshot(void);
251 static void smbstat_srv_process(void);
252 static void smbstat_srv_process_counters(smbstat_srv_snapshot_t *);
253 static void smbstat_srv_process_throughput(smbstat_srv_snapshot_t *,
254     smbstat_srv_snapshot_t *);
255 static void smbstat_srv_process_utilization(smbstat_srv_snapshot_t *,
256     smbstat_srv_snapshot_t *);
257 static void smbstat_srv_process_requests(smbstat_srv_snapshot_t *,
258     smbstat_srv_snapshot_t *);
259 static void smbstat_srv_process_one_req(smbstat_req_info_t *,
260     smb_kstat_req_t *, smb_kstat_req_t *, boolean_t);
261 
262 static smbstat_srv_snapshot_t *smbstat_srv_current_snapshot(void);
263 static smbstat_srv_snapshot_t *smbstat_srv_previous_snapshot(void);
264 
265 static void *smbstat_zalloc(size_t);
266 static void smbstat_free(void *, size_t);
267 static void smbstat_fail(int, char *, ...);
268 static void smbstat_snapshot_inc_idx(void);
269 static void smbstat_usage(FILE *, int) __NORETURN;
270 static uint_t smbstat_strtoi(char const *, char *);
271 static double smbstat_hrtime_delta(hrtime_t, hrtime_t);
272 static double smbstat_sub_64(uint64_t, uint64_t);
273 static void smbstat_req_order(void);
274 static double smbstat_zero(double);
275 static void smbstat_termio_init(void);
276 
277 static char *smbstat_cpu_states[CPU_TICKS_SENTINEL] = {
278 	"cpu_ticks_idle",
279 	"cpu_ticks_user",
280 	"cpu_ticks_kernel"
281 };
282 
283 static boolean_t	smbstat_opt_a = B_FALSE;	/* all */
284 static boolean_t	smbstat_opt_c = B_FALSE;	/* counters */
285 static boolean_t	smbstat_opt_n = B_FALSE;	/* by name */
286 static boolean_t	smbstat_opt_u = B_FALSE;	/* utilization */
287 static boolean_t	smbstat_opt_t = B_FALSE;	/* throughput */
288 static boolean_t	smbstat_opt_r = B_FALSE;	/* requests */
289 static boolean_t	smbstat_opt_z = B_FALSE;	/* non-zero requests */
290 
291 static uint_t		smbstat_interval = 0;
292 static long		smbstat_nrcpus = 0;
293 static kstat_ctl_t	*smbstat_ksc = NULL;
294 static kstat_t		*smbstat_srv_ksp = NULL;
295 static kstat_t		*smbstat_wrk_ksp = NULL;
296 static struct winsize	smbstat_ws;
297 static uint16_t		smbstat_rows = 0;
298 
299 static int smbstat_snapshot_idx = 0;
300 static smbstat_cpu_snapshot_t *smbstat_cpu_snapshots[SMBSTAT_SNAPSHOT_COUNT];
301 static smbstat_srv_snapshot_t smbstat_srv_snapshots[SMBSTAT_SNAPSHOT_COUNT];
302 static smbstat_wrk_snapshot_t smbstat_wrk_snapshots[SMBSTAT_SNAPSHOT_COUNT];
303 static smbstat_srv_info_t smbstat_srv_info;
304 
305 /*
306  * main
307  */
308 int
main(int argc,char * argv[])309 main(int argc, char *argv[])
310 {
311 	int	c;
312 
313 	(void) setlocale(LC_ALL, "");
314 	(void) textdomain(TEXT_DOMAIN);
315 
316 	if (is_system_labeled()) {
317 		(void) fprintf(stderr,
318 		    gettext("%s: Trusted Extensions not supported.\n"),
319 		    argv[0]);
320 		return (1);
321 	}
322 
323 	while ((c = getopt(argc, argv, "achnrtuz")) != EOF) {
324 		switch (c) {
325 		case 'a':
326 			smbstat_opt_a = B_TRUE;
327 			break;
328 		case 'n':
329 			smbstat_opt_n = B_TRUE;
330 			break;
331 		case 'u':
332 			smbstat_opt_u = B_TRUE;
333 			break;
334 		case 'c':
335 			smbstat_opt_c = B_TRUE;
336 			break;
337 		case 'r':
338 			smbstat_opt_r = B_TRUE;
339 			break;
340 		case 't':
341 			smbstat_opt_t = B_TRUE;
342 			break;
343 		case 'z':
344 			smbstat_opt_z = B_TRUE;
345 			break;
346 		case 'h':
347 			smbstat_usage(stdout, 0);
348 		default:
349 			smbstat_usage(stderr, 1);
350 		}
351 	}
352 
353 	if (!smbstat_opt_u &&
354 	    !smbstat_opt_c &&
355 	    !smbstat_opt_r &&
356 	    !smbstat_opt_t) {
357 		/* Default options when none is specified. */
358 		smbstat_opt_u = B_TRUE;
359 		smbstat_opt_t = B_TRUE;
360 	}
361 
362 	if (optind < argc) {
363 		smbstat_interval =
364 		    smbstat_strtoi(argv[optind], "invalid count");
365 		optind++;
366 	}
367 
368 	if ((argc - optind) > 1)
369 		smbstat_usage(stderr, 1);
370 
371 	(void) atexit(smbstat_fini);
372 	smbstat_init();
373 	for (;;) {
374 		smbstat_kstat_snapshot();
375 		smbstat_kstat_process();
376 		smbstat_kstat_print();
377 		if (smbstat_interval == 0)
378 			break;
379 		(void) sleep(smbstat_interval);
380 		smbstat_snapshot_inc_idx();
381 	}
382 	return (0);
383 }
384 
385 /*
386  * smbstat_init
387  *
388  * Global initialization.
389  */
390 static void
smbstat_init(void)391 smbstat_init(void)
392 {
393 	if ((smbstat_ksc = kstat_open()) == NULL)
394 		smbstat_fail(1, gettext("kstat_open(): can't open /dev/kstat"));
395 
396 	smbstat_cpu_init();
397 	smbstat_srv_init();
398 	smbstat_wrk_init();
399 	smbstat_req_order();
400 }
401 
402 /*
403  * smbstat_fini
404  *
405  * Releases the resources smbstat_init() allocated.
406  */
407 static void
smbstat_fini(void)408 smbstat_fini(void)
409 {
410 	smbstat_wrk_fini();
411 	smbstat_srv_fini();
412 	smbstat_cpu_fini();
413 	(void) kstat_close(smbstat_ksc);
414 }
415 
416 /*
417  * smbstat_kstat_snapshot
418  *
419  * Takes a snapshot of the data.
420  */
421 static void
smbstat_kstat_snapshot(void)422 smbstat_kstat_snapshot(void)
423 {
424 	smbstat_cpu_snapshot();
425 	smbstat_srv_snapshot();
426 	smbstat_wrk_snapshot();
427 }
428 
429 /*
430  * smbstat_kstat_process
431  */
432 static void
smbstat_kstat_process(void)433 smbstat_kstat_process(void)
434 {
435 	smbstat_cpu_process();
436 	smbstat_srv_process();
437 	smbstat_wrk_process();
438 }
439 
440 /*
441  * smbstat_kstat_print
442  *
443  * Print the data processed.
444  */
445 static void
smbstat_kstat_print(void)446 smbstat_kstat_print(void)
447 {
448 	smbstat_termio_init();
449 	smbstat_print_counters();
450 	smbstat_print_throughput();
451 	smbstat_print_utilization();
452 	smbstat_print_requests();
453 	(void) fflush(stdout);
454 }
455 
456 /*
457  * smbstat_print_counters
458  *
459  * Displays the SMB server counters (session, users...).
460  */
461 static void
smbstat_print_counters(void)462 smbstat_print_counters(void)
463 {
464 	if (!smbstat_opt_c)
465 		return;
466 
467 	if (smbstat_opt_u || smbstat_opt_r || smbstat_opt_t ||
468 	    (smbstat_rows == 0) || (smbstat_rows >= smbstat_ws.ws_row)) {
469 		(void) printf(SMBSRV_COUNTERS_BANNER);
470 		smbstat_rows = 1;
471 	}
472 
473 	(void) printf(SMBSRV_COUNTERS_FORMAT,
474 	    smbstat_srv_info.si_nbt_sess,
475 	    smbstat_srv_info.si_tcp_sess,
476 	    smbstat_srv_info.si_users,
477 	    smbstat_srv_info.si_trees,
478 	    smbstat_srv_info.si_files,
479 	    smbstat_srv_info.si_pipes);
480 
481 	++smbstat_rows;
482 }
483 /*
484  * smbstat_print_throughput
485  *
486  * Formats the SMB server throughput output.
487  */
488 static void
smbstat_print_throughput(void)489 smbstat_print_throughput(void)
490 {
491 	if (!smbstat_opt_t)
492 		return;
493 
494 	if (smbstat_opt_u || smbstat_opt_r || smbstat_opt_c ||
495 	    (smbstat_rows == 0) || (smbstat_rows >= smbstat_ws.ws_row)) {
496 		(void) printf(SMBSRV_THROUGHPUT_BANNER);
497 		smbstat_rows = 1;
498 	}
499 	(void) printf(SMBSRV_THROUGHPUT_FORMAT,
500 	    smbstat_zero(smbstat_srv_info.si_rbs),
501 	    smbstat_zero(smbstat_srv_info.si_tbs),
502 	    smbstat_zero(smbstat_srv_info.si_rqs),
503 	    smbstat_zero(smbstat_srv_info.si_rds),
504 	    smbstat_zero(smbstat_srv_info.si_wrs));
505 
506 	++smbstat_rows;
507 }
508 
509 /*
510  * smbstat_print_utilization
511  */
512 static void
smbstat_print_utilization(void)513 smbstat_print_utilization(void)
514 {
515 	char	*sat;
516 	if (!smbstat_opt_u)
517 		return;
518 
519 	if (smbstat_opt_t || smbstat_opt_r || smbstat_opt_c ||
520 	    (smbstat_rows == 0) || (smbstat_rows >= smbstat_ws.ws_row)) {
521 		(void) printf(SMBSRV_UTILIZATION_BANNER);
522 		smbstat_rows = 1;
523 	}
524 
525 	if (smbstat_srv_info.si_sat)
526 		sat = "yes";
527 	else
528 		sat = "no ";
529 
530 	(void) printf(SMBSRV_UTILIZATION_FORMAT,
531 	    smbstat_srv_info.si_avw,
532 	    smbstat_srv_info.si_avr,
533 	    smbstat_srv_info.si_wserv,
534 	    smbstat_srv_info.si_rserv,
535 	    smbstat_zero(smbstat_srv_info.si_wpct),
536 	    smbstat_zero(smbstat_srv_info.si_rpct),
537 	    smbstat_zero(smbstat_srv_info.si_upct),
538 	    sat,
539 	    smbstat_srv_info.si_ticks[CPU_TICKS_USER],
540 	    smbstat_srv_info.si_ticks[CPU_TICKS_KERNEL],
541 	    smbstat_srv_info.si_ticks[CPU_TICKS_IDLE]);
542 
543 	++smbstat_rows;
544 }
545 
546 /*
547  * smbstat_print_requests
548  */
549 static void
smbstat_print_requests(void)550 smbstat_print_requests(void)
551 {
552 	smbstat_req_info_t	*prq;
553 	int			i;
554 
555 	if (!smbstat_opt_r)
556 		return;
557 
558 	(void) printf(SMBSRV_REQUESTS_BANNER, "       ");
559 
560 	prq = smbstat_srv_info.si_reqs1;
561 	for (i = 0; i < SMBSRV_KS_NREQS1; i++) {
562 		if (!smbstat_opt_a &&
563 		    strncmp(prq[i].ri_name, "Invalid", sizeof ("Invalid")) == 0)
564 			continue;
565 
566 		if (!smbstat_opt_z || (prq[i].ri_pct != 0)) {
567 			(void) printf(SMBSRV_REQUESTS_FORMAT,
568 			    prq[i].ri_name,
569 			    prq[i].ri_opcode,
570 			    smbstat_zero(prq[i].ri_pct),
571 			    smbstat_zero(prq[i].ri_rbs),
572 			    smbstat_zero(prq[i].ri_tbs),
573 			    smbstat_zero(prq[i].ri_rqs),
574 			    prq[i].ri_mean,
575 			    prq[i].ri_stddev);
576 		}
577 	}
578 
579 	prq = smbstat_srv_info.si_reqs2;
580 	for (i = 0; i < SMBSRV_KS_NREQS2; i++) {
581 		if (!smbstat_opt_a && i == SMB2_INVALID_CMD)
582 			continue;
583 
584 		if (!smbstat_opt_z || (prq[i].ri_pct != 0)) {
585 			(void) printf(SMBSRV_REQUESTS_FORMAT,
586 			    prq[i].ri_name,
587 			    prq[i].ri_opcode,
588 			    smbstat_zero(prq[i].ri_pct),
589 			    smbstat_zero(prq[i].ri_rbs),
590 			    smbstat_zero(prq[i].ri_tbs),
591 			    smbstat_zero(prq[i].ri_rqs),
592 			    prq[i].ri_mean,
593 			    prq[i].ri_stddev);
594 		}
595 	}
596 }
597 
598 /*
599  * smbstat_cpu_init
600  */
601 static void
smbstat_cpu_init(void)602 smbstat_cpu_init(void)
603 {
604 	size_t	size;
605 	int	i;
606 
607 	smbstat_nrcpus = sysconf(_SC_CPUID_MAX) + 1;
608 	size = smbstat_nrcpus * sizeof (smbstat_cpu_snapshot_t);
609 
610 	for (i = 0; i < SMBSTAT_SNAPSHOT_COUNT; i++)
611 		smbstat_cpu_snapshots[i] = smbstat_zalloc(size);
612 }
613 
614 /*
615  * smbstat_cpu_fini
616  */
617 static void
smbstat_cpu_fini(void)618 smbstat_cpu_fini(void)
619 {
620 	size_t	size;
621 	int	i;
622 
623 	size = smbstat_nrcpus * sizeof (smbstat_cpu_snapshot_t);
624 
625 	for (i = 0; i < SMBSTAT_SNAPSHOT_COUNT; i++)
626 		smbstat_free(smbstat_cpu_snapshots[i], size);
627 }
628 
629 /*
630  * smbstat_cpu_current_snapshot
631  */
632 static smbstat_cpu_snapshot_t *
smbstat_cpu_current_snapshot(void)633 smbstat_cpu_current_snapshot(void)
634 {
635 	return (smbstat_cpu_snapshots[smbstat_snapshot_idx]);
636 }
637 
638 /*
639  * smbstat_cpu_previous_snapshot
640  */
641 static smbstat_cpu_snapshot_t *
smbstat_cpu_previous_snapshot(void)642 smbstat_cpu_previous_snapshot(void)
643 {
644 	int	idx;
645 
646 	idx = (smbstat_snapshot_idx - 1) & SMBSTAT_SNAPSHOT_MASK;
647 	return (smbstat_cpu_snapshots[idx]);
648 }
649 
650 /*
651  * smbstat_cpu_snapshot
652  */
653 static void
smbstat_cpu_snapshot(void)654 smbstat_cpu_snapshot(void)
655 {
656 	kstat_t			*ksp;
657 	kstat_named_t		*ksn;
658 	smbstat_cpu_snapshot_t	*curr;
659 	long			i;
660 	int			j;
661 
662 	curr =  smbstat_cpu_current_snapshot();
663 
664 	for (i = 0; i < smbstat_nrcpus;	i++, curr++) {
665 		curr->cs_id = SMBSTAT_ID_NO_CPU;
666 		curr->cs_state = p_online(i, P_STATUS);
667 		/* If no valid CPU is present, move on to the next one */
668 		if (curr->cs_state != P_ONLINE && curr->cs_state != P_NOINTR)
669 			continue;
670 
671 		curr->cs_id = i;
672 
673 		ksp = kstat_lookup(smbstat_ksc, "cpu", i, "sys");
674 		if (ksp == NULL)
675 			smbstat_fail(1,
676 			    gettext("kstat_lookup('cpu sys %d') failed"), i);
677 
678 		if (kstat_read(smbstat_ksc, ksp, NULL) == -1)
679 			smbstat_fail(1,
680 			    gettext("kstat_read('cpu sys %d') failed"), i);
681 
682 		for (j = 0; j < CPU_TICKS_SENTINEL; j++) {
683 			ksn = kstat_data_lookup(ksp, smbstat_cpu_states[j]);
684 			if (ksn == NULL)
685 				smbstat_fail(1,
686 				    gettext("kstat_data_lookup('%s') failed"),
687 				    smbstat_cpu_states[j]);
688 			curr->cs_ticks[j] = ksn->value.ui64;
689 		}
690 	}
691 }
692 
693 /*
694  * smbstat_cpu_process
695  */
696 static void
smbstat_cpu_process(void)697 smbstat_cpu_process(void)
698 {
699 	smbstat_cpu_snapshot_t	*curr, *prev;
700 	double			total_ticks;
701 	double			agg_ticks[CPU_TICKS_SENTINEL];
702 	int			i, j;
703 
704 	curr =  smbstat_cpu_current_snapshot();
705 	prev =  smbstat_cpu_previous_snapshot();
706 	bzero(agg_ticks, sizeof (agg_ticks));
707 	total_ticks = 0;
708 
709 	for (i = 0; i < smbstat_nrcpus; i++, curr++, prev++) {
710 		for (j = 0; j < CPU_TICKS_SENTINEL; j++) {
711 			agg_ticks[j] +=	smbstat_sub_64(curr->cs_ticks[j],
712 			    prev->cs_ticks[j]);
713 			total_ticks += smbstat_sub_64(curr->cs_ticks[j],
714 			    prev->cs_ticks[j]);
715 		}
716 	}
717 
718 	for (j = 0; j < CPU_TICKS_SENTINEL; j++)
719 		smbstat_srv_info.si_ticks[j] =
720 		    (agg_ticks[j] * 100.0) / total_ticks;
721 }
722 
723 /*
724  * smbstat_wrk_init
725  */
726 static void
smbstat_wrk_init(void)727 smbstat_wrk_init(void)
728 {
729 	smbstat_wrk_ksp =
730 	    kstat_lookup(smbstat_ksc, "unix", -1, SMBSRV_KSTAT_WORKERS);
731 	if (smbstat_wrk_ksp == NULL)
732 		smbstat_fail(1,
733 		    gettext("cannot retrieve smbsrv workers kstat\n"));
734 }
735 
736 static void
smbstat_wrk_fini(void)737 smbstat_wrk_fini(void)
738 {
739 	smbstat_wrk_ksp = NULL;
740 }
741 
742 /*
743  * smbstat_wrk_snapshot
744  */
745 static void
smbstat_wrk_snapshot(void)746 smbstat_wrk_snapshot(void)
747 {
748 	smbstat_wrk_snapshot_t	*curr;
749 	kstat_named_t		*kn;
750 
751 	curr = smbstat_wrk_current_snapshot();
752 
753 	if (kstat_read(smbstat_ksc, smbstat_wrk_ksp, NULL) == -1)
754 		smbstat_fail(1, gettext("kstat_read('%s') failed"),
755 		    smbstat_wrk_ksp->ks_name);
756 
757 	kn = kstat_data_lookup(smbstat_wrk_ksp, "nbacklog");
758 	if ((kn == NULL) || (kn->data_type != KSTAT_DATA_UINT64))
759 		smbstat_fail(1, gettext("kstat_read('%s') failed"),
760 		    "nbacklog");
761 	curr->ws_nbacklog = kn->value.ui64;
762 }
763 
764 /*
765  * smbstat_wrk_process
766  */
767 static void
smbstat_wrk_process(void)768 smbstat_wrk_process(void)
769 {
770 	smbstat_wrk_snapshot_t	*curr;
771 
772 	curr = smbstat_wrk_current_snapshot();
773 
774 	if (curr->ws_nbacklog != 0)
775 		smbstat_srv_info.si_sat = B_TRUE;
776 	else
777 		smbstat_srv_info.si_sat = B_FALSE;
778 }
779 
780 /*
781  * smbstat_wrk_current_snapshot
782  */
783 static smbstat_wrk_snapshot_t *
smbstat_wrk_current_snapshot(void)784 smbstat_wrk_current_snapshot(void)
785 {
786 	return (&smbstat_wrk_snapshots[smbstat_snapshot_idx]);
787 }
788 
789 /*
790  * smbstat_srv_init
791  */
792 static void
smbstat_srv_init(void)793 smbstat_srv_init(void)
794 {
795 	smbstat_srv_ksp = kstat_lookup(smbstat_ksc, SMBSRV_KSTAT_MODULE,
796 	    getzoneid(), SMBSRV_KSTAT_STATISTICS);
797 	if (smbstat_srv_ksp == NULL)
798 		smbstat_fail(1, gettext("cannot retrieve smbsrv kstat\n"));
799 }
800 
801 /*
802  * smbstat_srv_fini
803  */
804 static void
smbstat_srv_fini(void)805 smbstat_srv_fini(void)
806 {
807 	smbstat_srv_ksp = NULL;
808 }
809 
810 /*
811  * smbstat_srv_snapshot
812  *
813  * Take a snapshot of the smbsrv module statistics.
814  */
815 static void
smbstat_srv_snapshot(void)816 smbstat_srv_snapshot(void)
817 {
818 	smbstat_srv_snapshot_t	*curr;
819 
820 	curr = smbstat_srv_current_snapshot();
821 
822 	if ((kstat_read(smbstat_ksc, smbstat_srv_ksp, NULL) == -1) ||
823 	    (smbstat_srv_ksp->ks_data_size != sizeof (curr->ss_data)))
824 		smbstat_fail(1, gettext("kstat_read('%s') failed"),
825 		    smbstat_srv_ksp->ks_name);
826 
827 	curr->ss_snaptime = smbstat_srv_ksp->ks_snaptime;
828 	bcopy(smbstat_srv_ksp->ks_data, &curr->ss_data, sizeof (curr->ss_data));
829 }
830 
831 /*
832  * smbstat_srv_process
833  *
834  * Processes the snapshot data.
835  */
836 static void
smbstat_srv_process(void)837 smbstat_srv_process(void)
838 {
839 	smbstat_srv_snapshot_t	*curr, *prev;
840 
841 	curr = smbstat_srv_current_snapshot();
842 	prev = smbstat_srv_previous_snapshot();
843 
844 	if (prev->ss_snaptime == 0)
845 		smbstat_srv_info.si_hretime =
846 		    smbstat_hrtime_delta(curr->ss_data.ks_start_time,
847 		    curr->ss_snaptime);
848 	else
849 		smbstat_srv_info.si_hretime =
850 		    smbstat_hrtime_delta(prev->ss_snaptime, curr->ss_snaptime);
851 
852 	smbstat_srv_info.si_etime = smbstat_srv_info.si_hretime / NANOSEC;
853 	smbstat_srv_info.si_total_nreqs =
854 	    smbstat_sub_64(curr->ss_data.ks_nreq, prev->ss_data.ks_nreq);
855 
856 	if (smbstat_opt_c)
857 		smbstat_srv_process_counters(curr);
858 	if (smbstat_opt_t)
859 		smbstat_srv_process_throughput(curr, prev);
860 	if (smbstat_opt_u)
861 		smbstat_srv_process_utilization(curr, prev);
862 	if (smbstat_opt_r)
863 		smbstat_srv_process_requests(curr, prev);
864 }
865 
866 /*
867  * smbstat_srv_process_counters
868  */
869 static void
smbstat_srv_process_counters(smbstat_srv_snapshot_t * curr)870 smbstat_srv_process_counters(smbstat_srv_snapshot_t *curr)
871 {
872 	smbstat_srv_info.si_nbt_sess = curr->ss_data.ks_nbt_sess;
873 	smbstat_srv_info.si_tcp_sess = curr->ss_data.ks_tcp_sess;
874 	smbstat_srv_info.si_users = curr->ss_data.ks_users;
875 	smbstat_srv_info.si_trees = curr->ss_data.ks_trees;
876 	smbstat_srv_info.si_files = curr->ss_data.ks_files;
877 	smbstat_srv_info.si_pipes = curr->ss_data.ks_pipes;
878 }
879 
880 /*
881  * smbstat_srv_process_throughput
882  *
883  * Processes the data relative to the throughput of the smbsrv module and
884  * stores the results in the structure smbstat_srv_info.
885  */
886 static void
smbstat_srv_process_throughput(smbstat_srv_snapshot_t * curr,smbstat_srv_snapshot_t * prev)887 smbstat_srv_process_throughput(
888     smbstat_srv_snapshot_t	*curr,
889     smbstat_srv_snapshot_t	*prev)
890 {
891 	smbstat_srv_info.si_tbs =
892 	    smbstat_sub_64(curr->ss_data.ks_txb, prev->ss_data.ks_txb);
893 	smbstat_srv_info.si_tbs /= smbstat_srv_info.si_etime;
894 	smbstat_srv_info.si_rbs =
895 	    smbstat_sub_64(curr->ss_data.ks_rxb, prev->ss_data.ks_rxb);
896 	smbstat_srv_info.si_rbs /= smbstat_srv_info.si_etime;
897 	smbstat_srv_info.si_rqs = smbstat_srv_info.si_total_nreqs;
898 	smbstat_srv_info.si_rqs /= smbstat_srv_info.si_etime;
899 
900 	smbstat_srv_info.si_rds = smbstat_sub_64(
901 	    curr->ss_data.ks_reqs1[SMB_COM_READ].kr_nreq,
902 	    prev->ss_data.ks_reqs1[SMB_COM_READ].kr_nreq);
903 	smbstat_srv_info.si_rds += smbstat_sub_64(
904 	    curr->ss_data.ks_reqs1[SMB_COM_LOCK_AND_READ].kr_nreq,
905 	    prev->ss_data.ks_reqs1[SMB_COM_LOCK_AND_READ].kr_nreq);
906 	smbstat_srv_info.si_rds += smbstat_sub_64(
907 	    curr->ss_data.ks_reqs1[SMB_COM_READ_RAW].kr_nreq,
908 	    prev->ss_data.ks_reqs1[SMB_COM_READ_RAW].kr_nreq);
909 	smbstat_srv_info.si_rds += smbstat_sub_64(
910 	    curr->ss_data.ks_reqs1[SMB_COM_READ_ANDX].kr_nreq,
911 	    prev->ss_data.ks_reqs1[SMB_COM_READ_ANDX].kr_nreq);
912 	smbstat_srv_info.si_rds += smbstat_sub_64(
913 	    curr->ss_data.ks_reqs2[SMB2_READ].kr_nreq,
914 	    prev->ss_data.ks_reqs2[SMB2_READ].kr_nreq);
915 	smbstat_srv_info.si_rds /= smbstat_srv_info.si_etime;
916 
917 	smbstat_srv_info.si_wrs = smbstat_sub_64(
918 	    curr->ss_data.ks_reqs1[SMB_COM_WRITE].kr_nreq,
919 	    prev->ss_data.ks_reqs1[SMB_COM_WRITE].kr_nreq);
920 	smbstat_srv_info.si_wrs += smbstat_sub_64(
921 	    curr->ss_data.ks_reqs1[SMB_COM_WRITE_AND_UNLOCK].kr_nreq,
922 	    prev->ss_data.ks_reqs1[SMB_COM_WRITE_AND_UNLOCK].kr_nreq);
923 	smbstat_srv_info.si_wrs += smbstat_sub_64(
924 	    curr->ss_data.ks_reqs1[SMB_COM_WRITE_RAW].kr_nreq,
925 	    prev->ss_data.ks_reqs1[SMB_COM_WRITE_RAW].kr_nreq);
926 	smbstat_srv_info.si_wrs += smbstat_sub_64(
927 	    curr->ss_data.ks_reqs1[SMB_COM_WRITE_AND_CLOSE].kr_nreq,
928 	    prev->ss_data.ks_reqs1[SMB_COM_WRITE_AND_CLOSE].kr_nreq);
929 	smbstat_srv_info.si_wrs += smbstat_sub_64(
930 	    curr->ss_data.ks_reqs1[SMB_COM_WRITE_ANDX].kr_nreq,
931 	    prev->ss_data.ks_reqs1[SMB_COM_WRITE_ANDX].kr_nreq);
932 	smbstat_srv_info.si_wrs += smbstat_sub_64(
933 	    curr->ss_data.ks_reqs2[SMB2_WRITE].kr_nreq,
934 	    prev->ss_data.ks_reqs2[SMB2_WRITE].kr_nreq);
935 	smbstat_srv_info.si_wrs /= smbstat_srv_info.si_etime;
936 }
937 
938 /*
939  * smbstat_srv_process_utilization
940  *
941  * Processes the data relative to the utilization of the smbsrv module and
942  * stores the results in the structure smbstat_srv_info.
943  */
944 static void
smbstat_srv_process_utilization(smbstat_srv_snapshot_t * curr,smbstat_srv_snapshot_t * prev)945 smbstat_srv_process_utilization(
946     smbstat_srv_snapshot_t	*curr,
947     smbstat_srv_snapshot_t	*prev)
948 {
949 	double	tw_delta, tr_delta;
950 	double	w_delta, r_delta;
951 	double	tps, rqs;
952 
953 	w_delta = smbstat_hrtime_delta(prev->ss_data.ks_utilization.ku_wlentime,
954 	    curr->ss_data.ks_utilization.ku_wlentime);
955 	r_delta = smbstat_hrtime_delta(prev->ss_data.ks_utilization.ku_rlentime,
956 	    curr->ss_data.ks_utilization.ku_rlentime);
957 	tw_delta = smbstat_hrtime_delta(prev->ss_data.ks_utilization.ku_wtime,
958 	    curr->ss_data.ks_utilization.ku_wtime);
959 	tr_delta = smbstat_hrtime_delta(prev->ss_data.ks_utilization.ku_rtime,
960 	    curr->ss_data.ks_utilization.ku_rtime);
961 	rqs = smbstat_srv_info.si_total_nreqs / smbstat_srv_info.si_etime;
962 
963 	/* Average number of requests waiting */
964 	if (w_delta != 0)
965 		smbstat_srv_info.si_avw = w_delta / smbstat_srv_info.si_hretime;
966 	else
967 		smbstat_srv_info.si_avw = 0.0;
968 
969 	/* Average number of request running */
970 	if (r_delta != 0)
971 		smbstat_srv_info.si_avr = r_delta / smbstat_srv_info.si_hretime;
972 	else
973 		smbstat_srv_info.si_avr = 0.0;
974 
975 	/* Utilization */
976 	smbstat_srv_info.si_upct =
977 	    (smbstat_srv_info.si_avr / curr->ss_data.ks_maxreqs) * 100;
978 
979 	/* Average wait service time in milliseconds */
980 	smbstat_srv_info.si_rserv = 0.0;
981 	smbstat_srv_info.si_wserv = 0.0;
982 	if (rqs > 0.0 &&
983 	    (smbstat_srv_info.si_avw != 0.0 ||
984 	    smbstat_srv_info.si_avr != 0.0)) {
985 		tps = 1 / rqs;
986 		if (smbstat_srv_info.si_avw != 0.0)
987 			smbstat_srv_info.si_wserv =
988 			    smbstat_srv_info.si_avw * tps;
989 		if (smbstat_srv_info.si_avr != 0.0)
990 			smbstat_srv_info.si_rserv =
991 			    smbstat_srv_info.si_avr * tps;
992 	}
993 
994 	/* % of time there is a transaction waiting for service */
995 	if (tw_delta != 0) {
996 		smbstat_srv_info.si_wpct = tw_delta;
997 		smbstat_srv_info.si_wpct /= smbstat_srv_info.si_hretime;
998 		smbstat_srv_info.si_wpct *= 100.0;
999 	} else {
1000 		smbstat_srv_info.si_wpct = 0.0;
1001 	}
1002 
1003 	/* % of time there is a transaction running */
1004 	if (tr_delta != 0) {
1005 		smbstat_srv_info.si_rpct = tr_delta;
1006 		smbstat_srv_info.si_rpct /= smbstat_srv_info.si_hretime;
1007 		smbstat_srv_info.si_rpct *= 100.0;
1008 	} else {
1009 		smbstat_srv_info.si_rpct = 0.0;
1010 	}
1011 }
1012 
1013 /*
1014  * smbstat_srv_process_requests
1015  *
1016  * Processes the data relative to the SMB requests and stores the results in
1017  * the structure smbstat_srv_info.
1018  */
1019 static void
smbstat_srv_process_requests(smbstat_srv_snapshot_t * curr,smbstat_srv_snapshot_t * prev)1020 smbstat_srv_process_requests(
1021     smbstat_srv_snapshot_t	*curr,
1022     smbstat_srv_snapshot_t	*prev)
1023 {
1024 	smbstat_req_info_t	*info;
1025 	smb_kstat_req_t		*curr_req;
1026 	smb_kstat_req_t		*prev_req;
1027 	int			i, idx;
1028 	boolean_t	firstcall = (prev->ss_snaptime == 0);
1029 
1030 	for (i = 0; i < SMBSRV_KS_NREQS1; i++) {
1031 		info = &smbstat_srv_info.si_reqs1[i];
1032 		idx = info->ri_opcode;
1033 		if (idx >= SMBSRV_KS_NREQS1)
1034 			continue;
1035 		curr_req = &curr->ss_data.ks_reqs1[idx];
1036 		prev_req = &prev->ss_data.ks_reqs1[idx];
1037 		smbstat_srv_process_one_req(
1038 		    info, curr_req, prev_req, firstcall);
1039 	}
1040 
1041 	for (i = 0; i < SMBSRV_KS_NREQS2; i++) {
1042 		info = &smbstat_srv_info.si_reqs2[i];
1043 		idx = info->ri_opcode;
1044 		if (idx >= SMBSRV_KS_NREQS2)
1045 			continue;
1046 		curr_req = &curr->ss_data.ks_reqs2[idx];
1047 		prev_req = &prev->ss_data.ks_reqs2[idx];
1048 		smbstat_srv_process_one_req(
1049 		    info, curr_req, prev_req, firstcall);
1050 	}
1051 }
1052 
1053 static void
smbstat_srv_process_one_req(smbstat_req_info_t * info,smb_kstat_req_t * curr_req,smb_kstat_req_t * prev_req,boolean_t firstcall)1054 smbstat_srv_process_one_req(
1055 	smbstat_req_info_t	*info,
1056 	smb_kstat_req_t		*curr_req,
1057 	smb_kstat_req_t		*prev_req,
1058 	boolean_t		firstcall)
1059 {
1060 	double			nrqs;
1061 
1062 	nrqs = smbstat_sub_64(curr_req->kr_nreq,
1063 	    prev_req->kr_nreq);
1064 
1065 	info->ri_rqs = nrqs / smbstat_srv_info.si_etime;
1066 
1067 	info->ri_rbs = smbstat_sub_64(
1068 	    curr_req->kr_rxb,
1069 	    prev_req->kr_rxb) /
1070 	    smbstat_srv_info.si_etime;
1071 
1072 	info->ri_tbs = smbstat_sub_64(
1073 	    curr_req->kr_txb,
1074 	    prev_req->kr_txb) /
1075 	    smbstat_srv_info.si_etime;
1076 
1077 	info->ri_pct = nrqs * 100;
1078 	if (smbstat_srv_info.si_total_nreqs > 0)
1079 		info->ri_pct /= smbstat_srv_info.si_total_nreqs;
1080 
1081 	if (firstcall) {
1082 		/* First time. Take the aggregate */
1083 		info->ri_stddev =
1084 		    curr_req->kr_a_stddev;
1085 		info->ri_mean = curr_req->kr_a_mean;
1086 	} else {
1087 		/* Take the differential */
1088 		info->ri_stddev =
1089 		    curr_req->kr_d_stddev;
1090 		info->ri_mean = curr_req->kr_d_mean;
1091 	}
1092 	if (nrqs > 0) {
1093 		info->ri_stddev /= nrqs;
1094 		info->ri_stddev = sqrt(info->ri_stddev);
1095 	} else {
1096 		info->ri_stddev = 0;
1097 	}
1098 	info->ri_stddev /= NANOSEC;
1099 	info->ri_mean /= NANOSEC;
1100 }
1101 
1102 
1103 /*
1104  * smbstat_srv_current_snapshot
1105  *
1106  * Returns the current snapshot.
1107  */
1108 static smbstat_srv_snapshot_t *
smbstat_srv_current_snapshot(void)1109 smbstat_srv_current_snapshot(void)
1110 {
1111 	return (&smbstat_srv_snapshots[smbstat_snapshot_idx]);
1112 }
1113 
1114 /*
1115  * smbstat_srv_previous_snapshot
1116  *
1117  * Returns the previous snapshot.
1118  */
1119 static smbstat_srv_snapshot_t *
smbstat_srv_previous_snapshot(void)1120 smbstat_srv_previous_snapshot(void)
1121 {
1122 	int	idx;
1123 
1124 	idx = (smbstat_snapshot_idx - 1) & SMBSTAT_SNAPSHOT_MASK;
1125 	return (&smbstat_srv_snapshots[idx]);
1126 }
1127 
1128 /*
1129  * smbstat_usage
1130  *
1131  * Prints out a help message.
1132  */
1133 static void
smbstat_usage(FILE * fd,int exit_code)1134 smbstat_usage(FILE *fd, int exit_code)
1135 {
1136 	(void) fprintf(fd, gettext(SMBSTAT_HELP));
1137 	exit(exit_code);
1138 }
1139 
1140 /*
1141  * smbstat_fail
1142  *
1143  * Prints out to stderr an error message and exits the process.
1144  */
1145 static void
smbstat_fail(int do_perror,char * message,...)1146 smbstat_fail(int do_perror, char *message, ...)
1147 {
1148 	va_list args;
1149 
1150 	va_start(args, message);
1151 	(void) fprintf(stderr, gettext("smbstat: "));
1152 	/* LINTED E_SEC_PRINTF_VAR_FMT */
1153 	(void) vfprintf(stderr, message, args);
1154 	va_end(args);
1155 	if (do_perror)
1156 		(void) fprintf(stderr, ": %s", strerror(errno));
1157 	(void) fprintf(stderr, "\n");
1158 	exit(1);
1159 }
1160 
1161 /*
1162  * smbstat_sub_64
1163  *
1164  * Substract 2 uint64_t and returns a double.
1165  */
1166 static double
smbstat_sub_64(uint64_t a,uint64_t b)1167 smbstat_sub_64(uint64_t a, uint64_t b)
1168 {
1169 	return ((double)(a - b));
1170 }
1171 
1172 /*
1173  * smbstat_zero
1174  *
1175  * Returns zero if the value passed in is less than 1.
1176  */
1177 static double
smbstat_zero(double value)1178 smbstat_zero(double value)
1179 {
1180 	if (value < 1)
1181 		value = 0;
1182 	return (value);
1183 }
1184 
1185 /*
1186  * smbstat_strtoi
1187  *
1188  * Converts a string representing an integer value into its binary value.
1189  * If the conversion fails this routine exits the process.
1190  */
1191 static uint_t
smbstat_strtoi(char const * val,char * errmsg)1192 smbstat_strtoi(char const *val, char *errmsg)
1193 {
1194 	char	*end;
1195 	long	tmp;
1196 
1197 	errno = 0;
1198 	tmp = strtol(val, &end, 10);
1199 	if (*end != '\0' || errno)
1200 		smbstat_fail(1, "%s %s", errmsg, val);
1201 	return ((uint_t)tmp);
1202 }
1203 
1204 /*
1205  * smbstat_termio_init
1206  *
1207  * Determines the size of the terminal associated with the process.
1208  */
1209 static void
smbstat_termio_init(void)1210 smbstat_termio_init(void)
1211 {
1212 	char	*envp;
1213 
1214 	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &smbstat_ws) != -1) {
1215 		if (smbstat_ws.ws_row == 0) {
1216 			envp = getenv("LINES");
1217 			if (envp != NULL)
1218 				smbstat_ws.ws_row = atoi(envp);
1219 		}
1220 
1221 		if (smbstat_ws.ws_col == 0) {
1222 			envp = getenv("COLUMNS");
1223 			if (envp != NULL)
1224 				smbstat_ws.ws_row = atoi(envp);
1225 		}
1226 	}
1227 	if (smbstat_ws.ws_col == 0)
1228 		smbstat_ws.ws_col = 80;
1229 	if (smbstat_ws.ws_row == 0)
1230 		smbstat_ws.ws_row = 25;
1231 }
1232 
1233 /*
1234  * smbstat_snapshot_idx_inc
1235  *
1236  * Increments the snapshot index.
1237  */
1238 static void
smbstat_snapshot_inc_idx(void)1239 smbstat_snapshot_inc_idx(void)
1240 {
1241 	smbstat_snapshot_idx++;
1242 	smbstat_snapshot_idx &= SMBSTAT_SNAPSHOT_MASK;
1243 }
1244 
1245 /*
1246  * smbstat_req_cmp_name
1247  *
1248  * Call back function passed to qsort() when the list of requests must be sorted
1249  * by name.
1250  */
1251 static int
smbstat_req_cmp_name(const void * obj1,const void * obj2)1252 smbstat_req_cmp_name(const void *obj1, const void *obj2)
1253 {
1254 	return (strncasecmp(
1255 	    ((smbstat_req_info_t *)obj1)->ri_name,
1256 	    ((smbstat_req_info_t *)obj2)->ri_name,
1257 	    sizeof (((smbstat_req_info_t *)obj2)->ri_name)));
1258 }
1259 
1260 /*
1261  * smbstat_req_order
1262  *
1263  * Snapshots the smbsrv module statistics once to get the name of the requests.
1264  * The request list is smbstat_srv_info is then sorted by name or by code
1265  * depending on the boolean smbstat_opt_a.
1266  * The function should be called once during initialization.
1267  */
1268 static void
smbstat_req_order(void)1269 smbstat_req_order(void)
1270 {
1271 	smbstat_srv_snapshot_t  *ss;
1272 	smbstat_req_info_t	*info;
1273 	smb_kstat_req_t		*reqs;
1274 	int			i;
1275 
1276 	smbstat_srv_snapshot();
1277 	ss = smbstat_srv_current_snapshot();
1278 
1279 	reqs = ss->ss_data.ks_reqs1;
1280 	info = smbstat_srv_info.si_reqs1;
1281 	for (i = 0; i < SMBSRV_KS_NREQS1; i++) {
1282 		(void) strlcpy(info[i].ri_name, reqs[i].kr_name,
1283 		    sizeof (reqs[i].kr_name));
1284 		info[i].ri_opcode = i;
1285 	}
1286 	if (smbstat_opt_n)
1287 		qsort(info, SMBSRV_KS_NREQS1, sizeof (smbstat_req_info_t),
1288 		    smbstat_req_cmp_name);
1289 
1290 	reqs = ss->ss_data.ks_reqs2;
1291 	info = smbstat_srv_info.si_reqs2;
1292 	for (i = 0; i < SMBSRV_KS_NREQS2; i++) {
1293 		(void) strlcpy(info[i].ri_name, reqs[i].kr_name,
1294 		    sizeof (reqs[i].kr_name));
1295 		info[i].ri_opcode = i;
1296 	}
1297 	if (smbstat_opt_n)
1298 		qsort(info, SMBSRV_KS_NREQS2, sizeof (smbstat_req_info_t),
1299 		    smbstat_req_cmp_name);
1300 }
1301 
1302 /*
1303  * Return the number of ticks delta between two hrtime_t
1304  * values. Attempt to cater for various kinds of overflow
1305  * in hrtime_t - no matter how improbable.
1306  */
1307 static double
smbstat_hrtime_delta(hrtime_t old,hrtime_t new)1308 smbstat_hrtime_delta(hrtime_t old, hrtime_t new)
1309 {
1310 	uint64_t	del;
1311 
1312 	if ((new >= old) && (old >= 0L))
1313 		return ((double)(new - old));
1314 	/*
1315 	 * We've overflowed the positive portion of an hrtime_t.
1316 	 */
1317 	if (new < 0L) {
1318 		/*
1319 		 * The new value is negative. Handle the case where the old
1320 		 * value is positive or negative.
1321 		 */
1322 		uint64_t n1;
1323 		uint64_t o1;
1324 
1325 		n1 = -new;
1326 		if (old > 0L)
1327 			return ((double)(n1 - old));
1328 
1329 		o1 = -old;
1330 		del = n1 - o1;
1331 		return ((double)del);
1332 	}
1333 
1334 	/*
1335 	 * Either we've just gone from being negative to positive *or* the last
1336 	 * entry was positive and the new entry is also positive but *less* than
1337 	 * the old entry. This implies we waited quite a few days on a very fast
1338 	 * system between displays.
1339 	 */
1340 	if (old < 0L) {
1341 		uint64_t o2;
1342 		o2 = -old;
1343 		del = UINT64_MAX - o2;
1344 	} else {
1345 		del = UINT64_MAX - old;
1346 	}
1347 	del += new;
1348 	return ((double)del);
1349 }
1350 
1351 static void *
smbstat_zalloc(size_t size)1352 smbstat_zalloc(size_t size)
1353 {
1354 	void	*ptr;
1355 
1356 	ptr = umem_zalloc(size, UMEM_DEFAULT);
1357 	if (ptr == NULL)
1358 		smbstat_fail(1,	gettext("out of memory"));
1359 	return (ptr);
1360 }
1361 
1362 static void
smbstat_free(void * ptr,size_t size)1363 smbstat_free(void *ptr, size_t size)
1364 {
1365 	umem_free(ptr, size);
1366 }
1367