npf_perf_test.c revision 1.4.4.2 1 1.4.4.2 tls /* $NetBSD: npf_perf_test.c,v 1.4.4.2 2014/08/20 00:05:11 tls Exp $ */
2 1.4.4.2 tls
3 1.4.4.2 tls /*
4 1.4.4.2 tls * NPF benchmarking.
5 1.4.4.2 tls *
6 1.4.4.2 tls * Public Domain.
7 1.4.4.2 tls */
8 1.4.4.2 tls
9 1.4.4.2 tls #include <sys/types.h>
10 1.4.4.2 tls #include <sys/param.h>
11 1.4.4.2 tls
12 1.4.4.2 tls #include <sys/kernel.h>
13 1.4.4.2 tls #include <sys/kmem.h>
14 1.4.4.2 tls #include <sys/kthread.h>
15 1.4.4.2 tls
16 1.4.4.2 tls #include "npf_impl.h"
17 1.4.4.2 tls #include "npf_test.h"
18 1.4.4.2 tls
19 1.4.4.2 tls #define NSECS 10 /* seconds */
20 1.4.4.2 tls
21 1.4.4.2 tls static volatile int run;
22 1.4.4.2 tls static volatile int done;
23 1.4.4.2 tls
24 1.4.4.2 tls static uint64_t * npackets;
25 1.4.4.2 tls static bool stateful;
26 1.4.4.2 tls
27 1.4.4.2 tls static struct mbuf *
28 1.4.4.2 tls fill_packet(unsigned i)
29 1.4.4.2 tls {
30 1.4.4.2 tls struct mbuf *m;
31 1.4.4.2 tls struct ip *ip;
32 1.4.4.2 tls struct udphdr *uh;
33 1.4.4.2 tls char buf[32];
34 1.4.4.2 tls
35 1.4.4.2 tls m = mbuf_construct(IPPROTO_UDP);
36 1.4.4.2 tls uh = mbuf_return_hdrs(m, false, &ip);
37 1.4.4.2 tls
38 1.4.4.2 tls snprintf(buf, sizeof(buf), "192.0.2.%u", i + i);
39 1.4.4.2 tls ip->ip_src.s_addr = inet_addr(PUB_IP1);
40 1.4.4.2 tls ip->ip_dst.s_addr = inet_addr(stateful ? LOCAL_IP2 : LOCAL_IP3);
41 1.4.4.2 tls uh->uh_sport = htons(80);
42 1.4.4.2 tls uh->uh_dport = htons(15000 + i);
43 1.4.4.2 tls return m;
44 1.4.4.2 tls }
45 1.4.4.2 tls
46 1.4.4.2 tls __dead static void
47 1.4.4.2 tls worker(void *arg)
48 1.4.4.2 tls {
49 1.4.4.2 tls ifnet_t *ifp = ifunit(IFNAME_INT);
50 1.4.4.2 tls unsigned int i = (uintptr_t)arg;
51 1.4.4.2 tls struct mbuf *m = fill_packet(i);
52 1.4.4.2 tls uint64_t n = 0;
53 1.4.4.2 tls
54 1.4.4.2 tls while (!run)
55 1.4.4.2 tls /* spin-wait */;
56 1.4.4.2 tls while (!done) {
57 1.4.4.2 tls int error;
58 1.4.4.2 tls
59 1.4.4.2 tls error = npf_packet_handler(NULL, &m, ifp, PFIL_OUT);
60 1.4.4.2 tls KASSERT(error == 0);
61 1.4.4.2 tls n++;
62 1.4.4.2 tls }
63 1.4.4.2 tls npackets[i] = n;
64 1.4.4.2 tls kthread_exit(0);
65 1.4.4.2 tls }
66 1.4.4.2 tls
67 1.4.4.2 tls void
68 1.4.4.2 tls npf_test_conc(bool st, unsigned nthreads)
69 1.4.4.2 tls {
70 1.4.4.2 tls uint64_t total = 0;
71 1.4.4.2 tls int error;
72 1.4.4.2 tls lwp_t **l;
73 1.4.4.2 tls
74 1.4.4.2 tls printf("THREADS\tPKTS\n");
75 1.4.4.2 tls stateful = st;
76 1.4.4.2 tls done = false;
77 1.4.4.2 tls run = false;
78 1.4.4.2 tls
79 1.4.4.2 tls npackets = kmem_zalloc(sizeof(uint64_t) * nthreads, KM_SLEEP);
80 1.4.4.2 tls l = kmem_zalloc(sizeof(lwp_t *) * nthreads, KM_SLEEP);
81 1.4.4.2 tls
82 1.4.4.2 tls for (unsigned i = 0; i < nthreads; i++) {
83 1.4.4.2 tls const int flags = KTHREAD_MUSTJOIN | KTHREAD_MPSAFE;
84 1.4.4.2 tls error = kthread_create(PRI_NONE, flags, NULL,
85 1.4.4.2 tls worker, (void *)(uintptr_t)i, &l[i], "npfperf");
86 1.4.4.2 tls KASSERT(error == 0);
87 1.4.4.2 tls }
88 1.4.4.2 tls
89 1.4.4.2 tls /* Let them spin! */
90 1.4.4.2 tls run = true;
91 1.4.4.2 tls kpause("perf", false, NSECS * hz, NULL);
92 1.4.4.2 tls done = true;
93 1.4.4.2 tls
94 1.4.4.2 tls /* Wait until all threads exit and sum the counts. */
95 1.4.4.2 tls for (unsigned i = 0; i < nthreads; i++) {
96 1.4.4.2 tls kthread_join(l[i]);
97 1.4.4.2 tls total += npackets[i];
98 1.4.4.2 tls }
99 1.4.4.2 tls kmem_free(npackets, sizeof(uint64_t) * nthreads);
100 1.4.4.2 tls kmem_free(l, sizeof(lwp_t *) * nthreads);
101 1.4.4.2 tls
102 1.4.4.2 tls printf("%u\t%" PRIu64 "\n", nthreads, total / NSECS);
103 1.4.4.2 tls }
104