1112394Ssam/*-
2112394Ssam * Copyright (c) 2002, 2003	Sam Leffler, Errno Consulting
3112394Ssam * All rights reserved.
4112394Ssam *
5112394Ssam * Redistribution and use in source and binary forms, with or without
6112394Ssam * modification, are permitted provided that the following conditions
7112394Ssam * are met:
8112394Ssam * 1. Redistributions of source code must retain the above copyright
9112394Ssam *    notice, this list of conditions and the following disclaimer.
10112394Ssam * 2. Redistributions in binary form must reproduce the above copyright
11112394Ssam *    notice, this list of conditions and the following disclaimer in the
12112394Ssam *    documentation and/or other materials provided with the distribution.
13112394Ssam *
14112394Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15112394Ssam * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16112394Ssam * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17112394Ssam * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18112394Ssam * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19112394Ssam * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20112394Ssam * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21112394Ssam * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22112394Ssam * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23112394Ssam * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24112394Ssam * SUCH DAMAGE.
25112394Ssam *
26112394Ssam * $FreeBSD$
27112394Ssam */
28108833Ssam
29108833Ssam/*
30108833Ssam * Little program to dump the crypto statistics block and, optionally,
31108833Ssam * zero all the stats or just the timing stuff.
32108833Ssam */
33108833Ssam#include <stdio.h>
34175979Smatteo#include <stdlib.h>
35175979Smatteo#include <strings.h>
36108833Ssam
37108833Ssam#include <sys/types.h>
38108833Ssam#include <sys/sysctl.h>
39108833Ssam#include <sys/time.h>
40108833Ssam#include <crypto/cryptodev.h>
41108833Ssam
42108833Ssamstatic void
43108833Ssamprintt(const char* tag, struct cryptotstat *ts)
44108833Ssam{
45108833Ssam	uint64_t avg, min, max;
46108833Ssam
47108833Ssam	if (ts->count == 0)
48108833Ssam		return;
49108833Ssam	avg = (1000000000LL*ts->acc.tv_sec + ts->acc.tv_nsec) / ts->count;
50108833Ssam	min = 1000000000LL*ts->min.tv_sec + ts->min.tv_nsec;
51108833Ssam	max = 1000000000LL*ts->max.tv_sec + ts->max.tv_nsec;
52108833Ssam	printf("%16.16s: avg %6llu ns : min %6llu ns : max %7llu ns [%u samps]\n",
53108833Ssam		tag, avg, min, max, ts->count);
54108833Ssam}
55108833Ssam
56108833Ssamint
57108833Ssammain(int argc, char *argv[])
58108833Ssam{
59108833Ssam	struct cryptostats stats;
60108833Ssam	size_t slen;
61108833Ssam
62108833Ssam	slen = sizeof (stats);
63175979Smatteo	if (sysctlbyname("kern.crypto_stats", &stats, &slen, NULL, 0) < 0)
64108833Ssam		err(1, "kern.cryptostats");
65108833Ssam
66108833Ssam	if (argc > 1 && strcmp(argv[1], "-z") == 0) {
67108833Ssam		bzero(&stats.cs_invoke, sizeof (stats.cs_invoke));
68108833Ssam		bzero(&stats.cs_done, sizeof (stats.cs_done));
69108833Ssam		bzero(&stats.cs_cb, sizeof (stats.cs_cb));
70108833Ssam		bzero(&stats.cs_finis, sizeof (stats.cs_finis));
71108833Ssam		stats.cs_invoke.min.tv_sec = 10000;
72108833Ssam		stats.cs_done.min.tv_sec = 10000;
73108833Ssam		stats.cs_cb.min.tv_sec = 10000;
74108833Ssam		stats.cs_finis.min.tv_sec = 10000;
75108833Ssam		if (sysctlbyname("kern.crypto_stats", NULL, NULL, &stats, sizeof (stats)) < 0)
76108833Ssam			err(1, "kern.cryptostats");
77108833Ssam		exit(0);
78108833Ssam	}
79108833Ssam	if (argc > 1 && strcmp(argv[1], "-Z") == 0) {
80108833Ssam		bzero(&stats, sizeof (stats));
81108833Ssam		stats.cs_invoke.min.tv_sec = 10000;
82108833Ssam		stats.cs_done.min.tv_sec = 10000;
83108833Ssam		stats.cs_cb.min.tv_sec = 10000;
84108833Ssam		stats.cs_finis.min.tv_sec = 10000;
85108833Ssam		if (sysctlbyname("kern.crypto_stats", NULL, NULL, &stats, sizeof (stats)) < 0)
86108833Ssam			err(1, "kern.cryptostats");
87108833Ssam		exit(0);
88108833Ssam	}
89108833Ssam
90108833Ssam
91108833Ssam	printf("%u symmetric crypto ops (%u errors, %u times driver blocked)\n"
92108833Ssam		, stats.cs_ops, stats.cs_errs, stats.cs_blocks);
93108833Ssam	printf("%u key ops (%u errors, %u times driver blocked)\n"
94108833Ssam		, stats.cs_kops, stats.cs_kerrs, stats.cs_kblocks);
95108833Ssam	printf("%u crypto dispatch thread activations\n", stats.cs_intrs);
96108833Ssam	printf("%u crypto return thread activations\n", stats.cs_rets);
97108833Ssam	if (stats.cs_invoke.count) {
98108833Ssam		printf("\n");
99108833Ssam		printt("dispatch->invoke", &stats.cs_invoke);
100108833Ssam		printt("invoke->done", &stats.cs_done);
101108833Ssam		printt("done->cb", &stats.cs_cb);
102108833Ssam		printt("cb->finis", &stats.cs_finis);
103108833Ssam	}
104108833Ssam	return 0;
105108833Ssam}
106