1/**
2 * \file
3 * \brief Flounder stubs buffer
4 */
5
6/*
7 * Copyright (c) 2007, 2008, 2009, 2010, ETH Zurich.
8 * All rights reserved.
9 *
10 * This file is distributed under the terms in the attached LICENSE file.
11 * If you do not find this file, copies can be found by writing to:
12 * ETH Zurich D-INFK, Universitaetstrasse 6, CH-8092 Zurich. Attn: Systems Group.
13 */
14
15#include <stdio.h>
16#include <barrelfish/barrelfish.h>
17#include <string.h>
18#include <barrelfish/nameservice_client.h>
19#include <barrelfish/spawn_client.h>
20#include <bench/bench.h>
21#include <if/bench_defs.h>
22
23static char my_name[100];
24static uint8_t buffer;
25
26static struct bench_binding *binding;
27static coreid_t my_core_id;
28
29#define MAX_COUNT 1000
30
31struct timestamp {
32    cycles_t time0;
33    cycles_t time1;
34};
35static struct timestamp timestamps[MAX_COUNT];
36
37static void experiment(void)
38{
39    errval_t err;
40    static bool flag = false;
41    static int i = 0;
42
43    // Experiment finished
44    if (i == MAX_COUNT - 1) {
45        timestamps[i].time1 = bench_tsc();
46
47        for (int j = MAX_COUNT / 10; j < MAX_COUNT; j++) {
48            printf("page %d took %"PRIuCYCLES"\n", j,
49                   timestamps[j].time1 - bench_tscoverhead() -
50                   timestamps[j].time0);
51        }
52
53        printf("client done\n");
54        return;
55    }
56
57    if (!flag) { // Start experiment
58        timestamps[i].time0 = bench_tsc();
59        flag = true;
60    } else { // Continue experiment
61        timestamps[i].time1 = bench_tsc();
62        timestamps[i+1].time0 = timestamps[i].time1;
63        i++;
64    }
65
66    err = binding->tx_vtbl.fsb_buffer_request(binding, NOP_CONT, &buffer, 1);
67    assert(err_is_ok(err));
68}
69
70static void fsb_init_msg(struct bench_binding *b, coreid_t id)
71{
72    binding = b;
73    printf("Running flounder_stubs_buffer between core %d and core %d\n", my_core_id, 1);
74    experiment();
75}
76
77static void fsb_buffer_reply(struct bench_binding *b, const uint8_t *payload, size_t size)
78{
79    experiment();
80}
81
82static void fsb_buffer_request(struct bench_binding *b, const uint8_t *payload, size_t size)
83{
84    errval_t err;
85    err = b->tx_vtbl.fsb_buffer_reply(b, NOP_CONT, &buffer, 1);
86    assert(err_is_ok(err));
87}
88
89static struct bench_rx_vtbl rx_vtbl = {
90    .fsb_init_msg   = fsb_init_msg,
91    .fsb_buffer_request = fsb_buffer_request,
92    .fsb_buffer_reply = fsb_buffer_reply,
93};
94
95static void bind_cb(void *st, errval_t binderr, struct bench_binding *b)
96{
97    // copy my message receive handler vtable to the binding
98    b->rx_vtbl = rx_vtbl;
99
100    errval_t err;
101    err = b->tx_vtbl.fsb_init_msg(b, NOP_CONT, my_core_id);
102    assert(err_is_ok(err));
103}
104
105static void export_cb(void *st, errval_t err, iref_t iref)
106{
107    if (err_is_fail(err)) {
108        DEBUG_ERR(err, "export failed");
109        abort();
110    }
111
112    // register this iref with the name service
113    err = nameservice_register("fsb_server", iref);
114    if (err_is_fail(err)) {
115        DEBUG_ERR(err, "nameservice_register failed");
116        abort();
117    }
118}
119
120static errval_t connect_cb(void *st, struct bench_binding *b)
121{
122    // copy my message receive handler vtable to the binding
123    b->rx_vtbl = rx_vtbl;
124
125    // accept the connection
126    return SYS_ERR_OK;
127}
128
129int main(int argc, char *argv[])
130{
131    errval_t err;
132
133    /* Set my core id */
134    my_core_id = disp_get_core_id();
135    strcpy(my_name, argv[0]);
136
137    bench_init();
138
139    if (argc == 1) { /* bsp core */
140        /*
141          1. spawn domain,
142          2. setup a server,
143          3. wait for client to connect,
144          4. run experiment
145        */
146        char *xargv[] = {my_name, "dummy", NULL};
147        err = spawn_program(1, my_name, xargv, NULL,
148                            SPAWN_FLAGS_DEFAULT, NULL);
149        assert(err_is_ok(err));
150
151        /* Setup a server */
152        err = bench_export(NULL, export_cb, connect_cb, get_default_waitset(),
153                           IDC_BIND_FLAGS_DEFAULT);
154        assert(err_is_ok(err));
155    } else {
156        /* Connect to the server */
157        iref_t iref;
158
159        err = nameservice_blocking_lookup("fsb_server", &iref);
160        if (err_is_fail(err)) {
161            DEBUG_ERR(err, "nameservice_blocking_lookup failed");
162            abort();
163        }
164
165        err = bench_bind(iref, bind_cb, NULL,
166                         get_default_waitset(), IDC_BIND_FLAGS_DEFAULT);
167        if (err_is_fail(err)) {
168            DEBUG_ERR(err, "bind failed");
169            abort();
170        }
171    }
172
173    messages_handler_loop();
174}
175