| [8ebc8bf4] | 1 | /*
 | 
|---|
 | 2 |  * Copyright (c) 2009 Jiri Svoboda
 | 
|---|
 | 3 |  * All rights reserved.
 | 
|---|
 | 4 |  *
 | 
|---|
 | 5 |  * Redistribution and use in source and binary forms, with or without
 | 
|---|
 | 6 |  * modification, are permitted provided that the following conditions
 | 
|---|
 | 7 |  * are met:
 | 
|---|
 | 8 |  *
 | 
|---|
 | 9 |  * - Redistributions of source code must retain the above copyright
 | 
|---|
 | 10 |  *   notice, this list of conditions and the following disclaimer.
 | 
|---|
 | 11 |  * - Redistributions in binary form must reproduce the above copyright
 | 
|---|
 | 12 |  *   notice, this list of conditions and the following disclaimer in the
 | 
|---|
 | 13 |  *   documentation and/or other materials provided with the distribution.
 | 
|---|
 | 14 |  * - The name of the author may not be used to endorse or promote products
 | 
|---|
 | 15 |  *   derived from this software without specific prior written permission.
 | 
|---|
 | 16 |  *
 | 
|---|
 | 17 |  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 | 
|---|
 | 18 |  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 | 
|---|
 | 19 |  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 | 
|---|
 | 20 |  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 | 
|---|
 | 21 |  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 | 
|---|
 | 22 |  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 | 
|---|
 | 23 |  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 | 
|---|
 | 24 |  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 | 
|---|
 | 25 |  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 | 
|---|
 | 26 |  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 | 
|---|
 | 27 |  */
 | 
|---|
 | 28 | 
 | 
|---|
| [d6d6b3e] | 29 | #include <math.h>
 | 
|---|
| [8ebc8bf4] | 30 | #include <stdio.h>
 | 
|---|
 | 31 | #include <stdlib.h>
 | 
|---|
| [bd41ac52] | 32 | #include <time.h>
 | 
|---|
| [1edd6d0] | 33 | #include <ipc_test.h>
 | 
|---|
| [cbff4c2] | 34 | #include <async.h>
 | 
|---|
 | 35 | #include <errno.h>
 | 
|---|
| [d230358] | 36 | #include "../perf.h"
 | 
|---|
| [8ebc8bf4] | 37 | 
 | 
|---|
| [d6d6b3e] | 38 | #define MIN_DURATION_SECS  10
 | 
|---|
 | 39 | #define NUM_SAMPLES 10
 | 
|---|
 | 40 | 
 | 
|---|
| [1edd6d0] | 41 | static errno_t ping_pong_measure(ipc_test_t *test, uint64_t niter,
 | 
|---|
 | 42 |     uint64_t *rduration)
 | 
|---|
| [d6d6b3e] | 43 | {
 | 
|---|
 | 44 |         struct timespec start;
 | 
|---|
 | 45 |         uint64_t count;
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 |         getuptime(&start);
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 |         for (count = 0; count < niter; count++) {
 | 
|---|
| [1edd6d0] | 50 |                 errno_t retval = ipc_test_ping(test);
 | 
|---|
| [d6d6b3e] | 51 | 
 | 
|---|
 | 52 |                 if (retval != EOK) {
 | 
|---|
| [d230358] | 53 |                         printf("Error sending ping message.\n");
 | 
|---|
| [d6d6b3e] | 54 |                         return EIO;
 | 
|---|
 | 55 |                 }
 | 
|---|
 | 56 |         }
 | 
|---|
 | 57 | 
 | 
|---|
 | 58 |         struct timespec now;
 | 
|---|
 | 59 |         getuptime(&now);
 | 
|---|
 | 60 | 
 | 
|---|
 | 61 |         *rduration = ts_sub_diff(&now, &start) / 1000;
 | 
|---|
 | 62 |         return EOK;
 | 
|---|
 | 63 | }
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 | static void ping_pong_report(uint64_t niter, uint64_t duration)
 | 
|---|
 | 66 | {
 | 
|---|
| [d230358] | 67 |         printf("Completed %" PRIu64 " round trips in %" PRIu64 " us",
 | 
|---|
| [d6d6b3e] | 68 |             niter, duration);
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 |         if (duration > 0) {
 | 
|---|
| [d230358] | 71 |                 printf(", %" PRIu64 " rt/s.\n", niter * 1000 * 1000 / duration);
 | 
|---|
| [d6d6b3e] | 72 |         } else {
 | 
|---|
| [d230358] | 73 |                 printf(".\n");
 | 
|---|
| [d6d6b3e] | 74 |         }
 | 
|---|
 | 75 | }
 | 
|---|
| [8ebc8bf4] | 76 | 
 | 
|---|
| [d230358] | 77 | const char *bench_ping_pong(void)
 | 
|---|
| [8ebc8bf4] | 78 | {
 | 
|---|
| [d6d6b3e] | 79 |         errno_t rc;
 | 
|---|
 | 80 |         uint64_t duration;
 | 
|---|
 | 81 |         uint64_t dsmp[NUM_SAMPLES];
 | 
|---|
| [1edd6d0] | 82 |         ipc_test_t *test;
 | 
|---|
 | 83 |         const char *msg;
 | 
|---|
 | 84 | 
 | 
|---|
 | 85 |         rc = ipc_test_create(&test);
 | 
|---|
 | 86 |         if (rc != EOK)
 | 
|---|
 | 87 |                 return "Failed contacting IPC test server.";
 | 
|---|
| [d6d6b3e] | 88 | 
 | 
|---|
| [d230358] | 89 |         printf("Warm up and determine work size...\n");
 | 
|---|
| [a35b458] | 90 | 
 | 
|---|
| [bd41ac52] | 91 |         struct timespec start;
 | 
|---|
 | 92 |         getuptime(&start);
 | 
|---|
| [a35b458] | 93 | 
 | 
|---|
| [d6d6b3e] | 94 |         uint64_t niter = 1;
 | 
|---|
 | 95 | 
 | 
|---|
| [8ebc8bf4] | 96 |         while (true) {
 | 
|---|
| [1edd6d0] | 97 |                 rc = ping_pong_measure(test, niter, &duration);
 | 
|---|
 | 98 |                 if (rc != EOK) {
 | 
|---|
 | 99 |                         msg = "Failed.";
 | 
|---|
 | 100 |                         goto error;
 | 
|---|
 | 101 |                 }
 | 
|---|
| [a35b458] | 102 | 
 | 
|---|
| [d6d6b3e] | 103 |                 ping_pong_report(niter, duration);
 | 
|---|
 | 104 | 
 | 
|---|
 | 105 |                 if (duration >= MIN_DURATION_SECS * 1000000)
 | 
|---|
| [8ebc8bf4] | 106 |                         break;
 | 
|---|
| [a35b458] | 107 | 
 | 
|---|
| [d6d6b3e] | 108 |                 niter *= 2;
 | 
|---|
 | 109 |         }
 | 
|---|
 | 110 | 
 | 
|---|
| [d230358] | 111 |         printf("Measure %d samples...\n", NUM_SAMPLES);
 | 
|---|
| [a35b458] | 112 | 
 | 
|---|
| [d6d6b3e] | 113 |         int i;
 | 
|---|
| [a35b458] | 114 | 
 | 
|---|
| [d6d6b3e] | 115 |         for (i = 0; i < NUM_SAMPLES; i++) {
 | 
|---|
| [1edd6d0] | 116 |                 rc = ping_pong_measure(test, niter, &dsmp[i]);
 | 
|---|
 | 117 |                 if (rc != EOK) {
 | 
|---|
 | 118 |                         msg = "Failed.";
 | 
|---|
 | 119 |                         goto error;
 | 
|---|
 | 120 |                 }
 | 
|---|
| [d6d6b3e] | 121 | 
 | 
|---|
 | 122 |                 ping_pong_report(niter, dsmp[i]);
 | 
|---|
| [8ebc8bf4] | 123 |         }
 | 
|---|
| [a35b458] | 124 | 
 | 
|---|
| [d6d6b3e] | 125 |         double sum = 0.0;
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 |         for (i = 0; i < NUM_SAMPLES; i++)
 | 
|---|
 | 128 |                 sum += (double)niter / ((double)dsmp[i] / 1000000.0l);
 | 
|---|
 | 129 | 
 | 
|---|
 | 130 |         double avg = sum / NUM_SAMPLES;
 | 
|---|
 | 131 | 
 | 
|---|
 | 132 |         double qd = 0.0;
 | 
|---|
 | 133 |         double d;
 | 
|---|
 | 134 |         for (i = 0; i < NUM_SAMPLES; i++) {
 | 
|---|
 | 135 |                 d = (double)niter / ((double)dsmp[i] / 1000000.0l) - avg;
 | 
|---|
| [e131bd05] | 136 |                 qd += d * d;
 | 
|---|
| [d6d6b3e] | 137 |         }
 | 
|---|
 | 138 | 
 | 
|---|
| [ad2cf04] | 139 |         double stddev = qd / (NUM_SAMPLES - 1); // XXX sqrt
 | 
|---|
| [d6d6b3e] | 140 | 
 | 
|---|
| [d230358] | 141 |         printf("Average: %.0f rt/s Std.dev^2: %.0f rt/s Samples: %d\n",
 | 
|---|
| [d6d6b3e] | 142 |             avg, stddev, NUM_SAMPLES);
 | 
|---|
| [a35b458] | 143 | 
 | 
|---|
| [1edd6d0] | 144 |         ipc_test_destroy(test);
 | 
|---|
| [8ebc8bf4] | 145 |         return NULL;
 | 
|---|
| [1edd6d0] | 146 | error:
 | 
|---|
 | 147 |         ipc_test_destroy(test);
 | 
|---|
 | 148 |         return msg;
 | 
|---|
| [8ebc8bf4] | 149 | }
 | 
|---|