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