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