| 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 | 
 | 
|---|
| 29 | #include <math.h>
 | 
|---|
| 30 | #include <stdio.h>
 | 
|---|
| 31 | #include <stdlib.h>
 | 
|---|
| 32 | #include <time.h>
 | 
|---|
| 33 | #include <ipc_test.h>
 | 
|---|
| 34 | #include <async.h>
 | 
|---|
| 35 | #include <errno.h>
 | 
|---|
| 36 | #include "../perf.h"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | #define MIN_DURATION_SECS  10
 | 
|---|
| 39 | #define NUM_SAMPLES 10
 | 
|---|
| 40 | 
 | 
|---|
| 41 | static errno_t ping_pong_measure(ipc_test_t *test, uint64_t niter,
 | 
|---|
| 42 |     uint64_t *rduration)
 | 
|---|
| 43 | {
 | 
|---|
| 44 |         struct timespec start;
 | 
|---|
| 45 |         uint64_t count;
 | 
|---|
| 46 | 
 | 
|---|
| 47 |         getuptime(&start);
 | 
|---|
| 48 | 
 | 
|---|
| 49 |         for (count = 0; count < niter; count++) {
 | 
|---|
| 50 |                 errno_t retval = ipc_test_ping(test);
 | 
|---|
| 51 | 
 | 
|---|
| 52 |                 if (retval != EOK) {
 | 
|---|
| 53 |                         printf("Error sending ping message.\n");
 | 
|---|
| 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 | {
 | 
|---|
| 67 |         printf("Completed %" PRIu64 " round trips in %" PRIu64 " us",
 | 
|---|
| 68 |             niter, duration);
 | 
|---|
| 69 | 
 | 
|---|
| 70 |         if (duration > 0) {
 | 
|---|
| 71 |                 printf(", %" PRIu64 " rt/s.\n", niter * 1000 * 1000 / duration);
 | 
|---|
| 72 |         } else {
 | 
|---|
| 73 |                 printf(".\n");
 | 
|---|
| 74 |         }
 | 
|---|
| 75 | }
 | 
|---|
| 76 | 
 | 
|---|
| 77 | const char *bench_ping_pong(void)
 | 
|---|
| 78 | {
 | 
|---|
| 79 |         errno_t rc;
 | 
|---|
| 80 |         uint64_t duration;
 | 
|---|
| 81 |         uint64_t dsmp[NUM_SAMPLES];
 | 
|---|
| 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.";
 | 
|---|
| 88 | 
 | 
|---|
| 89 |         printf("Warm up and determine work size...\n");
 | 
|---|
| 90 | 
 | 
|---|
| 91 |         struct timespec start;
 | 
|---|
| 92 |         getuptime(&start);
 | 
|---|
| 93 | 
 | 
|---|
| 94 |         uint64_t niter = 1;
 | 
|---|
| 95 | 
 | 
|---|
| 96 |         while (true) {
 | 
|---|
| 97 |                 rc = ping_pong_measure(test, niter, &duration);
 | 
|---|
| 98 |                 if (rc != EOK) {
 | 
|---|
| 99 |                         msg = "Failed.";
 | 
|---|
| 100 |                         goto error;
 | 
|---|
| 101 |                 }
 | 
|---|
| 102 | 
 | 
|---|
| 103 |                 ping_pong_report(niter, duration);
 | 
|---|
| 104 | 
 | 
|---|
| 105 |                 if (duration >= MIN_DURATION_SECS * 1000000)
 | 
|---|
| 106 |                         break;
 | 
|---|
| 107 | 
 | 
|---|
| 108 |                 niter *= 2;
 | 
|---|
| 109 |         }
 | 
|---|
| 110 | 
 | 
|---|
| 111 |         printf("Measure %d samples...\n", NUM_SAMPLES);
 | 
|---|
| 112 | 
 | 
|---|
| 113 |         int i;
 | 
|---|
| 114 | 
 | 
|---|
| 115 |         for (i = 0; i < NUM_SAMPLES; i++) {
 | 
|---|
| 116 |                 rc = ping_pong_measure(test, niter, &dsmp[i]);
 | 
|---|
| 117 |                 if (rc != EOK) {
 | 
|---|
| 118 |                         msg = "Failed.";
 | 
|---|
| 119 |                         goto error;
 | 
|---|
| 120 |                 }
 | 
|---|
| 121 | 
 | 
|---|
| 122 |                 ping_pong_report(niter, dsmp[i]);
 | 
|---|
| 123 |         }
 | 
|---|
| 124 | 
 | 
|---|
| 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;
 | 
|---|
| 136 |                 qd += d * d;
 | 
|---|
| 137 |         }
 | 
|---|
| 138 | 
 | 
|---|
| 139 |         double stddev = qd / (NUM_SAMPLES - 1); // XXX sqrt
 | 
|---|
| 140 | 
 | 
|---|
| 141 |         printf("Average: %.0f rt/s Std.dev^2: %.0f rt/s Samples: %d\n",
 | 
|---|
| 142 |             avg, stddev, NUM_SAMPLES);
 | 
|---|
| 143 | 
 | 
|---|
| 144 |         ipc_test_destroy(test);
 | 
|---|
| 145 |         return NULL;
 | 
|---|
| 146 | error:
 | 
|---|
| 147 |         ipc_test_destroy(test);
 | 
|---|
| 148 |         return msg;
 | 
|---|
| 149 | }
 | 
|---|