2 * A tool for measuring FWP latency. This program aims to be simpler
3 * than wme_test and is intended to be run on a single machine with
4 * multiple wifi interfaces and send-to-self kernel patch applied.
6 * This program creates both sides of communication and measures the
7 * communication delays.
10 #include <netinet/in.h>
11 #include <arpa/inet.h>
15 #include <netinet/in.h>
17 #include <sys/socket.h>
18 #include <sys/types.h>
20 #include <sys/param.h>
24 #include <semaphore.h>
25 #include <ul_logreg.h>
27 bool opt_verbose = false;
28 bool opt_quiet = false;
30 #define HIST_MAX_US 1000000
31 #define HIST_RES_US 10
34 unsigned cnt[(HIST_MAX_US+1)/HIST_RES_US];
38 /* static void hist_init(struct histogram *h) */
40 /* memset(h, 0, sizeof(*h)); */
43 static void hist_add(struct histogram *h, int us)
48 __sync_fetch_and_add(&h->cnt[us/HIST_RES_US], 1);
53 } while (us > max && ! __sync_bool_compare_and_swap(&h->max, max, us));
57 static unsigned hist_get_percentile(struct histogram *h, unsigned p)
59 uint64_t sum = 0, psum;
61 for (i=0; i<(HIST_MAX_US+1)/HIST_RES_US; i++)
66 for (i=0; i<(HIST_MAX_US+1)/HIST_RES_US; i++) {
80 struct histogram hist;
82 void set_rt_prio(int priority)
85 static struct sched_param param;
87 if ((maxpri = sched_get_priority_max(SCHED_FIFO)) == -1) {
88 fprintf(stderr, "warning: sched_get_priority_max failed\n");
91 if ((minpri = sched_get_priority_min(SCHED_FIFO)) == -1) {
92 fprintf(stderr, "warning: sched_get_priority_min failed\n");
95 if (priority > maxpri) {
96 fprintf(stderr, "warning: maximum priority allowed is %d.\n", maxpri);
98 if (priority < minpri) {
99 fprintf(stderr, "warning: minimum priority allowed is %d.\n", minpri);
102 param.sched_priority = priority;
104 if (sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) {
105 fprintf(stderr, "warning: sched_setscheduler failed\n");
108 mlockall(MCL_CURRENT | MCL_FUTURE);
111 struct stream_params {
115 struct in_addr src, dst;
124 int negotiate_contract(struct stream_params *p)
126 frsh_contract_t contract;
128 frsh_rel_time_t period;
129 frsh_rel_time_t budget;
130 frsh_rel_time_t deadline;
133 ret = frsh_contract_init(&contract);
134 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_init");
136 ret = frsh_contract_set_resource_and_label(
138 FRSH_RT_NETWORK, FRSH_NETPF_FWP,
140 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_resource_and_label");
142 frsh_network_bytes_to_budget(FRSH_NETPF_FWP, p->budget, &budget);
143 period = fosa_msec_to_rel_time(p->period_ms);
144 ret = frsh_contract_set_basic_params(&contract,
149 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_basic_params");
152 /* FWP doesn't accept smaller deadlines than 30 ms. */
153 if (frsh_rel_time_smaller(period, frsh_msec_to_rel_time(30)))
154 deadline = frsh_msec_to_rel_time(30);
157 ret = frsh_contract_set_timing_reqs(&contract, false, &deadline);
158 if (ret) PERROR_AND_EXIT(ret, "frsh_contract_set_timing_reqs");
160 fwp = malloc(sizeof(*fwp));
161 if (!fwp) PERROR_AND_EXIT(errno, "malloc");
162 fwp->src = p->src.s_addr;
163 ret = fres_contract_add_fwp(contract, fwp);
164 if (ret) PERROR_AND_EXIT(ret, "fres_contract_add_fwp");
166 ret = frsh_contract_negotiate(&contract, &p->vres);
168 frsh_contract_destroy(&contract);
173 void create_endpoints(struct stream_params *p,
174 frsh_send_endpoint_t *epsrc,
175 frsh_receive_endpoint_t *epdst)
178 frsh_send_endpoint_protocol_info_t spi = { NULL, 0 };
179 frsh_receive_endpoint_protocol_info_t rpi = { NULL, 0 };
180 frsh_endpoint_queueing_info_t qi = { .queue_size=0,
181 .queue_policy=FRSH_QRP_OLDEST };
183 ret = frsh_receive_endpoint_create(FRSH_NETPF_FWP, 0, qi, rpi,
185 if (ret != 0) error(1, errno, "fwp_receive_endpoint_create");
188 frsh_receive_endpoint_get_params(*epdst, NULL, &port, NULL, NULL);
190 ret = frsh_send_endpoint_create(FRSH_NETPF_FWP,
191 p->dst.s_addr, port, spi,
193 if (ret < 0) error(1, errno, "frsh_send_endpoint_create()");
195 ret = frsh_send_endpoint_bind(p->vres, *epsrc);
196 if (ret != 0) error(1, errno, "frsh_send_endpoint_bind");
200 static struct option long_opts[] = {
201 { "loglevel",required_argument, 0, 'l' },
202 { "period", required_argument, 0, 'p' },
203 { "budget", required_argument, 0, 'b' },
204 { "source", required_argument, 0, 's' },
205 { "dest", required_argument, 0, 'd' },
206 { "async", no_argument, 0, 'a' },
207 { "number", required_argument, 0, 'n' },
208 { "count", required_argument, 0, 'c' },
209 { "verbose",no_argument, 0, 'v' },
210 { "quiet", no_argument, 0, 'q' },
217 printf("usage: fwp-timing [ options ]\n");
218 printf(" -l, --loglevel <number>|<domain>=<number>,...\n");
219 printf(" -p, --period <ms> period in miliseconds\n");
220 printf(" -b, --budget <bytes> how many bytes is sent in each period\n");
221 printf(" -s, --source <ip> source IP address\n");
222 printf(" -d, --dest <ip:port> destination IP address and port\n");
223 printf(" -a, --async Send packets asynchronously\n");
224 printf(" -n, --number Number of streams with the same parameters\n");
225 printf(" -c, --count Number of messages to send [infinity]\n");
226 printf(" -q, --quiet Print only final statistics\n");
227 printf(" -/, --stream New stream separator\n");
228 printf(" -v, --verbose Be more verbose\n");
231 int parse_opts(int *argc, char **argv[], struct stream_params *p)
235 bool options_found = false;
237 while ((opt = getopt_long(*argc, *argv, "/ab:c:d:l:n:p:qs:v", long_opts, NULL)) != -1) {
238 options_found = true;
244 p->budget = atoi(optarg);
247 p->count = atoi(optarg);
250 ret = inet_aton(optarg, &p->dst);
252 fprintf(stderr, "Destination IP address not recognized: %s\n",
259 ul_log_domain_arg2levels(optarg);
262 p->number = atoi(optarg);
265 p->period_ms = atoi(optarg);
268 ret = inet_aton(optarg, &p->src);
270 fprintf(stderr, "Source IP address not recognized: %s\n",
291 return (options_found) ? 0 : -1;
294 volatile bool exit_flag = false;
302 timespec_subtract (result, x, y)
303 struct timespec *result, *x, *y;
305 /* Perform the carry for the later subtraction by updating Y. */
306 if (x->tv_nsec < y->tv_nsec) {
307 int nsec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
308 y->tv_nsec -= 1000000000 * nsec;
311 if (x->tv_nsec - y->tv_nsec > 1000000000) {
312 int nsec = (x->tv_nsec - y->tv_nsec) / 1000000000;
313 y->tv_nsec += 1000000000 * nsec;
317 /* Compute the time remaining to wait.
318 `tv_nsec' is certainly positive. */
319 result->tv_sec = x->tv_sec - y->tv_sec;
320 result->tv_nsec = x->tv_nsec - y->tv_nsec;
322 /* Return 1 if result is negative. */
323 return x->tv_sec < y->tv_sec;
326 static inline double ts2d(struct timespec *ts)
328 return ts->tv_sec + 1e-9*ts->tv_nsec;
331 static inline double tsdiff2d(struct timespec *x,
335 timespec_subtract(&r, x, y);
339 static inline int tsdiff2us(struct timespec *x,
343 timespec_subtract(&r, x, y);
344 return r.tv_sec*1000000 + r.tv_nsec/1000;
352 struct receiver_params {
354 frsh_receive_endpoint_t epdst;
358 void *receiver(void *arg)
360 struct receiver_params *rp = arg;
361 frsh_receive_endpoint_t epdst = rp->epdst;
365 struct timespec tss, tsr;
367 msg = malloc(rp->budget);
368 if (!msg) error(1, errno, "malloc msg");
371 ret = frsh_receive_sync(epdst, msg, rp->budget, &mlen, NULL);
372 clock_gettime(CLOCK_MONOTONIC, &tsr);
374 if (msg->cnt != last_cnt+1) {
376 printf("%3d: packet(s) lost!\n", rp->id);
377 __sync_fetch_and_add(&stats.lost, msg->cnt - last_cnt+1);
379 hist_add(&hist, tsdiff2us(&tsr, &tss));
380 __sync_fetch_and_add(&stats.received, 1);
383 printf("%3d: %10d: %10.3lf ms\n",
384 rp->id, msg->cnt, tsdiff2d(&tsr, &tss)*1000);
393 void *sender(void *arg)
395 struct stream_params *p = arg;
396 frsh_send_endpoint_t epsrc;
397 struct receiver_params *rp;
401 pthread_t receiver_id;
403 msg = malloc(p->budget);
404 if (!msg) error(1, errno, "malloc msg");
406 rp = malloc(sizeof(*rp));
407 rp->budget = p->budget;
410 create_endpoints(p, &epsrc, &rp->epdst);
414 ret = pthread_create(&receiver_id, NULL, receiver, rp);
416 struct timespec next_period;
418 clock_gettime(CLOCK_MONOTONIC, &next_period);
419 while (!exit_flag && (p->count == -1 || p->count--)) {
420 clock_gettime(CLOCK_MONOTONIC, &tss);
424 ret = frsh_send_async(epsrc, msg, p->budget);
426 ret = frsh_send_sync(epsrc, msg, p->budget);
427 clock_gettime(CLOCK_MONOTONIC, &next_period);
429 __sync_fetch_and_add(&stats.sent, 1);
430 next_period.tv_sec += (p->period_ms/1000);
431 next_period.tv_nsec += (p->period_ms%1000) * 1000000;
432 if (next_period.tv_nsec >= 1000000000) {
433 next_period.tv_nsec -= 1000000000;
434 next_period.tv_sec++;
436 clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME,
445 void print_stat(bool final)
447 printf("Sent: %5d Received: %5d Lost: %5d Max: %8.3f ms",
448 stats.sent, stats.received, stats.lost, hist.max/1000.0);
450 printf(" Packetloss: %7.3f %% 90%%: %8.3f ms\n",
451 100.0*stats.lost/stats.sent,
452 hist_get_percentile(&hist, 90)/1000.0);
459 int main(int argc, char *argv[])
464 bool negotiation_failure = false;
466 struct stream_params sp = {
470 .src.s_addr = htonl(INADDR_LOOPBACK),
471 .dst.s_addr = htonl(INADDR_LOOPBACK),
475 struct stream_params *p[100];
477 memset(p, 0, sizeof(p));
479 if (signal(SIGTERM, stopper) == SIG_ERR)
480 error(1, errno, "Error in signal registration");
481 if (signal(SIGINT, stopper) == SIG_ERR)
482 error(1, errno, "Signal handler registration error");
484 sem_init(&finished, 0, 0);
487 if (ret) PERROR_AND_EXIT(ret, "frsh_init");
490 ret = parse_opts(&argc, &argv, &sp);
491 if (num == 0 || ret == 0) {
492 for (i=0; i<sp.number; i++) {
493 p[num] = malloc(sizeof(*p[0]));
494 if (!p[num]) error(1, errno, "malloc");
497 ret = negotiate_contract(p[num]);
501 PERROR_FRESCOR(ret, "frsh_contract_negotiate");
503 negotiation_failure = true;
510 if (negotiation_failure) {
514 for (i=0; i<num; i++)
515 pthread_create(&p[i]->thread, NULL, sender, p[i]);
517 while (!exit_flag && !opt_quiet) {
520 sem_getvalue(&finished, &v);
526 for (i=0; i<num; i++)
527 pthread_join(p[i]->thread, NULL);
529 for (i=0; i<num; i++) {
530 frsh_contract_cancel(p[i]->vres);
534 stats.lost = stats.sent - stats.received;
537 return negotiation_failure ? 1 : 0;