1/**
2 * \file
3 * \brief Benchmark retype with no remote relations present for source cap
4 */
5
6/*
7 * Copyright (c) 2017, 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, Universitaetstr 6, CH-8092 Zurich. Attn: Systems Group.
13 */
14
15#include <barrelfish/barrelfish.h>
16#include <if/bench_distops_defs.h>
17
18#include <bitmacros.h>
19
20#include <bench/bench.h>
21#include <trace/trace.h>
22
23#include "benchapi.h"
24
25//{{{1 debugging helpers
26static void debug_capref(const char *prefix, struct capref cap)
27{
28    char buf[128];
29    debug_print_capref(buf, 128, cap);
30    printf("%s capref = %s\n", prefix, buf);
31}
32
33//{{{1 shared commands
34enum bench_cmd {
35    BENCH_CMD_CREATE_COPIES,
36    BENCH_CMD_COPIES_DONE,
37    BENCH_CMD_DO_RETYPE,
38    BENCH_CMD_PRINT_STATS,
39    BENCH_CMD_PRINT_DONE,
40};
41
42//{{{1 Managment node: implement orchestration for benchmark
43
44//{{{2 Management node: state management
45
46struct global_state {
47    struct capref ram;
48    coreid_t *nodes;
49    int nodes_seen;
50    int nodecount;
51    int copies_done;
52    int printnode;
53    int currcopies;
54};
55
56errval_t mgmt_init_benchmark(void **st, int nodecount)
57{
58     *st = calloc(1, sizeof(struct global_state));
59     if (!*st) {
60         return LIB_ERR_MALLOC_FAIL;
61     }
62     struct global_state *gs = *st;
63     gs->nodes = calloc(nodecount, sizeof(coreid_t));
64     gs->nodecount = nodecount;
65     gs->copies_done = 0;
66     gs->printnode = 0;
67     return ram_alloc(&gs->ram, BASE_PAGE_BITS);
68}
69
70static int sort_coreid(const void *a_, const void *b_)
71{
72    // deref pointers as coreids, store as ints
73    int a = *((coreid_t*)a_);
74    int b = *((coreid_t*)b_);
75    // subtract as ints
76    return a-b;
77}
78
79void mgmt_register_node(void *st, coreid_t nodeid)
80{
81    struct global_state *gs = st;
82    gs->nodes[gs->nodes_seen++] = nodeid;
83    // if we've seen all nodes, sort nodes array and configure printnode
84    if (gs->nodes_seen == gs->nodecount) {
85        qsort(gs->nodes, gs->nodecount, sizeof(coreid_t), sort_coreid);
86    }
87}
88
89struct mgmt_node_state {
90};
91
92errval_t mgmt_init_node(void **st)
93{
94     *st = malloc(sizeof(struct mgmt_node_state));
95     if (!*st) {
96         return LIB_ERR_MALLOC_FAIL;
97     }
98    return SYS_ERR_OK;
99}
100
101//{{{2 Management node: benchmark impl
102void mgmt_run_benchmark(void *st)
103{
104    struct global_state *gs = st;
105
106    printf("All clients sent hello! Benchmark starting...\n");
107
108    printf("# Benchmarking RETYPE LOCAL: nodes=%d\n", gs->nodecount);
109
110    printf("# Starting out with %d copies, will increase by factors of 2 up to %d...\n",
111            NUM_COPIES_START, NUM_COPIES_END);
112
113    TRACE(CAPOPS, START, 0);
114
115    gs->currcopies = NUM_COPIES_START;
116    broadcast_caps(BENCH_CMD_CREATE_COPIES, NUM_COPIES_START, gs->ram);
117}
118
119void mgmt_cmd(uint32_t cmd, uint32_t arg, struct bench_distops_binding *b)
120{
121    struct global_state *gs = get_global_state(b);
122    switch(cmd) {
123        case BENCH_CMD_COPIES_DONE:
124            gs->copies_done++;
125            if (gs->copies_done == gs->nodecount) {
126                printf("# All copies made!\n");
127                broadcast_cmd(BENCH_CMD_DO_RETYPE, ITERS);
128                unicast_cmd(gs->nodes[gs->printnode++], BENCH_CMD_PRINT_STATS, 0);
129            }
130            break;
131        case BENCH_CMD_PRINT_DONE:
132            if (gs->printnode == gs->nodecount) {
133                if (gs->currcopies == NUM_COPIES_END) {
134                    printf("# Benchmark done!\n");
135                    TRACE(CAPOPS, STOP, 0);
136                    mgmt_trace_flush(NOP_CONT);
137                    return;
138                }
139                printf("# Round done!\n");
140                // Reset counters for next round
141                gs->currcopies *= 2;
142                gs->copies_done = 0;
143                gs->printnode = 0;
144                // Start new round
145                broadcast_cmd(BENCH_CMD_CREATE_COPIES, gs->currcopies);
146                return;
147            }
148            unicast_cmd(gs->nodes[gs->printnode++], BENCH_CMD_PRINT_STATS, 0);
149            break;
150        default:
151            printf("mgmt node got unknown command %d over binding %p\n", cmd, b);
152            break;
153    }
154}
155
156void mgmt_cmd_caps(uint32_t cmd, uint32_t arg, struct capref cap1,
157                   struct bench_distops_binding *b)
158{
159    printf("mgmt node got caps + command %"PRIu32", arg=%d over binding %p:\n",
160            cmd, arg, b);
161    debug_capref("cap1:", cap1);
162}
163
164//{{{1 Node
165
166struct node_state {
167    struct capref cap;
168    struct capref ram;
169    uint32_t numcopies;
170    struct capref *copies;
171    uint64_t *delcycles;
172    uint32_t benchcount;
173};
174
175static coreid_t my_core_id = -1;
176
177void init_node(struct bench_distops_binding *b)
178{
179    printf("%s: binding = %p\n", __FUNCTION__, b);
180
181    my_core_id = disp_get_core_id();
182
183    bench_init();
184
185    // Allocate client state struct
186    b->st = malloc(sizeof(struct node_state));
187    assert(b->st);
188    if (!b->st) {
189        USER_PANIC("state malloc() in client");
190    }
191}
192
193static void node_create_copies(struct node_state *ns)
194{
195    errval_t err;
196    ns->copies = calloc(ns->numcopies, sizeof(struct capref));
197    for (int i = 0; i < ns->numcopies; i++) {
198        err = slot_alloc(&ns->copies[i]);
199        PANIC_IF_ERR(err, "slot_alloc for copy %d\n", i);
200        err = cap_copy(ns->copies[i], ns->ram);
201        PANIC_IF_ERR(err, "cap_copy for copy %d\n", i);
202    }
203}
204
205static size_t get_mdb_size(void)
206{
207    errval_t err;
208    size_t cap_base_count = 0;
209    err = sys_debug_get_mdb_size(&cap_base_count);
210    assert(err_is_ok(err));
211    return cap_base_count;
212}
213
214void node_cmd(uint32_t cmd, uint32_t arg, struct bench_distops_binding *b)
215{
216    struct node_state *ns = b->st;
217    errval_t err;
218
219    switch(cmd) {
220        case BENCH_CMD_CREATE_COPIES:
221            printf("# node %d: creating %d cap copies\n", my_core_id, arg);
222            ns->numcopies = arg;
223            node_create_copies(ns);
224            printf("# node %d: %zu capabilities on node\n", my_core_id, get_mdb_size());
225            err = bench_distops_cmd__tx(b, NOP_CONT, BENCH_CMD_COPIES_DONE, 1);
226            PANIC_IF_ERR(err, "signaling cap_copy() done\n");
227            break;
228        case BENCH_CMD_DO_RETYPE:
229            ns->benchcount = arg;
230            ns->delcycles = calloc(arg, sizeof(uint64_t));
231            assert(ns->delcycles);
232            struct capref slot;
233            err = slot_alloc(&slot);
234            assert(err_is_ok(err));
235            //printf("node %d: doing delete\n", my_core_id);
236            for (int i = 0; i < ns->benchcount; i++) {
237                uint64_t start, end;
238                start = bench_tsc();
239                // Cycle through pages in source cap
240                TRACE(CAPOPS, USER_RETYPE_CALL, (ns->numcopies << 16) | i);
241                err = cap_retype(slot, ns->cap,
242                        (i*BASE_PAGE_SIZE) % LARGE_PAGE_SIZE,
243                        ObjType_Frame, BASE_PAGE_SIZE, 1);
244                TRACE(CAPOPS, USER_RETYPE_RESP, (ns->numcopies << 16) | i);
245                end = bench_tsc();
246                ns->delcycles[i] = end - start;
247                assert(err_is_ok(err));
248                err = cap_delete(slot);
249                assert(err_is_ok(err));
250            }
251            //printf("node %d: deletes done\n", my_core_id);
252            break;
253        case BENCH_CMD_PRINT_STATS:
254            printf("# node %d: tsc_per_us = %ld; numcopies = %d\n",
255                    my_core_id, bench_tsc_per_us(), ns->numcopies);
256            printf("# retype latency in cycles\n");
257            for (int i = 0; i < ns->benchcount; i++) {
258                printf("%ld\n", ns->delcycles[i]);
259            }
260            err = bench_distops_cmd__tx(b, NOP_CONT, BENCH_CMD_PRINT_DONE, 0);
261            assert(err_is_ok(err));
262            // Cleanup before next round
263            for (int i = 0; i < ns->numcopies; i++) {
264                err = cap_destroy(ns->copies[i]);
265                assert(err_is_ok(err));
266            }
267            free(ns->copies);
268            free(ns->delcycles);
269            break;
270        default:
271            printf("node %d got command %"PRIu32"\n", my_core_id, cmd);
272            break;
273    }
274}
275
276void node_cmd_caps(uint32_t cmd, uint32_t arg, struct capref cap1,
277                   struct bench_distops_binding *b)
278{
279    errval_t err;
280    struct node_state *ns = b->st;
281
282    switch (cmd) {
283        case BENCH_CMD_CREATE_COPIES:
284            printf("# node %d: creating %d cap copies\n", my_core_id, arg);
285            ns->ram = cap1;
286            ns->numcopies = arg;
287            node_create_copies(ns);
288            // First round, also get a local RAM cap for the benchmark later
289            err = ram_alloc(&ns->cap, LARGE_PAGE_BITS);
290            assert(err_is_ok(err));
291            printf("# node %d: %zu caps on node\n", my_core_id, get_mdb_size());
292            err = bench_distops_cmd__tx(b, NOP_CONT, BENCH_CMD_COPIES_DONE, 0);
293            PANIC_IF_ERR(err, "signaling cap_copy() done\n");
294            break;
295        default:
296            printf("node %d got caps + command %"PRIu32", arg=%d:\n",
297                my_core_id, cmd, arg);
298            debug_capref("cap1:", cap1);
299            break;
300    }
301}
302