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