1/**
2 * \file
3 * \brief delete local copy of 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_DELETE,
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 DELETE LOCAL COPY: nodes=%d, nodeids=", gs->nodecount);
109    for (int i=0; i < gs->nodecount; i++) {
110        printf("%d ", gs->nodes[i]);
111    }
112    printf("\n");
113
114    printf("# Starting out with %d copies, will increase by factors of two up to %d...\n",
115            NUM_COPIES_START, NUM_COPIES_END);
116
117    TRACE(CAPOPS, START, 0);
118
119    gs->currcopies = NUM_COPIES_START;
120    broadcast_caps(BENCH_CMD_CREATE_COPIES, NUM_COPIES_START, gs->ram);
121}
122
123void mgmt_cmd(uint32_t cmd, uint32_t arg, struct bench_distops_binding *b)
124{
125    struct global_state *gs = get_global_state(b);
126    switch(cmd) {
127        case BENCH_CMD_COPIES_DONE:
128            gs->copies_done++;
129            if (gs->copies_done == gs->nodecount) {
130                printf("# All copies made!\n");
131                broadcast_cmd(BENCH_CMD_DO_DELETE, ITERS);
132                unicast_cmd(gs->nodes[gs->printnode++], BENCH_CMD_PRINT_STATS, 0);
133            }
134            break;
135        case BENCH_CMD_PRINT_DONE:
136            if (gs->printnode == gs->nodecount) {
137                if (gs->currcopies == NUM_COPIES_END) {
138                    printf("# Benchmark done!\n");
139                    TRACE(CAPOPS, STOP, 0);
140                    mgmt_trace_flush(NOP_CONT);
141                    return;
142                }
143                printf("# Round done!\n");
144                // Reset counters for next round
145                gs->currcopies *= 2;
146                gs->copies_done = 0;
147                gs->printnode = 0;
148                // Start new round
149                broadcast_cmd(BENCH_CMD_CREATE_COPIES, gs->currcopies);
150                return;
151            }
152            unicast_cmd(gs->nodes[gs->printnode++], BENCH_CMD_PRINT_STATS, 0);
153            break;
154        default:
155            printf("mgmt node got unknown command %d over binding %p\n", cmd, b);
156            break;
157    }
158}
159
160void mgmt_cmd_caps(uint32_t cmd, uint32_t arg, struct capref cap1,
161                   struct bench_distops_binding *b)
162{
163    printf("mgmt node got caps + command %"PRIu32", arg=%d over binding %p:\n",
164            cmd, arg, b);
165    debug_capref("cap1:", cap1);
166}
167
168//{{{1 Node
169
170struct node_state {
171    struct capref cap;
172    struct capref ram;
173    uint32_t numcopies;
174    struct capref *copies;
175    uint64_t *delcycles;
176    uint32_t benchcount;
177};
178
179static coreid_t my_core_id = -1;
180
181void init_node(struct bench_distops_binding *b)
182{
183    printf("%s: binding = %p\n", __FUNCTION__, b);
184
185    my_core_id = disp_get_core_id();
186
187    bench_init();
188
189    // Allocate client state struct
190    b->st = malloc(sizeof(struct node_state));
191    assert(b->st);
192    if (!b->st) {
193        USER_PANIC("state malloc() in client");
194    }
195}
196
197static void node_create_copies(struct node_state *ns)
198{
199    errval_t err;
200    ns->copies = calloc(ns->numcopies, sizeof(struct capref));
201    for (int i = 0; i < ns->numcopies; i++) {
202        err = slot_alloc(&ns->copies[i]);
203        PANIC_IF_ERR(err, "slot_alloc for copy %d\n", i);
204        err = cap_copy(ns->copies[i], ns->ram);
205        PANIC_IF_ERR(err, "cap_copy for copy %d\n", i);
206    }
207}
208
209static size_t get_mdb_size(void)
210{
211    errval_t err;
212    size_t cap_base_count = 0;
213    err = sys_debug_get_mdb_size(&cap_base_count);
214    assert(err_is_ok(err));
215    return cap_base_count;
216}
217
218void node_cmd(uint32_t cmd, uint32_t arg, struct bench_distops_binding *b)
219{
220    struct node_state *ns = b->st;
221    errval_t err;
222
223    switch(cmd) {
224        case BENCH_CMD_CREATE_COPIES:
225            printf("# node %d: creating %d cap copies\n", my_core_id, arg);
226            ns->numcopies = arg;
227            node_create_copies(ns);
228            printf("# node %d: %zu capabilities on node\n", my_core_id, get_mdb_size());
229            err = bench_distops_cmd__tx(b, NOP_CONT, BENCH_CMD_COPIES_DONE, 1);
230            PANIC_IF_ERR(err, "signaling cap_copy() done\n");
231            break;
232        case BENCH_CMD_DO_DELETE:
233            ns->benchcount = arg;
234            ns->delcycles = calloc(arg, sizeof(uint64_t));
235            assert(ns->delcycles);
236            struct capref slot;
237            err = slot_alloc(&slot);
238            assert(err_is_ok(err));
239            err = cap_copy(slot, ns->cap);
240            assert(err_is_ok(err));
241            //printf("node %d: doing delete\n", my_core_id);
242            for (int i = 0; i < ns->benchcount; i++) {
243                uint64_t start, end;
244                start = bench_tsc();
245                TRACE(CAPOPS, USER_DELETE_CALL, (ns->numcopies << 16) | i);
246                err = cap_delete(slot);
247                TRACE(CAPOPS, USER_DELETE_RESP, (ns->numcopies << 16) | i);
248                end = bench_tsc();
249                ns->delcycles[i] = end - start;
250                assert(err_is_ok(err));
251                err = cap_copy(slot, ns->cap);
252                assert(err_is_ok(err));
253            }
254            //printf("node %d: deletes done\n", my_core_id);
255            break;
256        case BENCH_CMD_PRINT_STATS:
257            printf("# node %d: tsc_per_us = %ld; numcopies = %d\n",
258                    my_core_id, bench_tsc_per_us(), ns->numcopies);
259            printf("# delete latency in cycles\n");
260            for (int i = 0; i < ns->benchcount; i++) {
261                printf("%ld\n", ns->delcycles[i]);
262            }
263            err = bench_distops_cmd__tx(b, NOP_CONT, BENCH_CMD_PRINT_DONE, 0);
264            assert(err_is_ok(err));
265            // Cleanup before next round
266            for (int i = 0; i < ns->numcopies; i++) {
267                err = cap_destroy(ns->copies[i]);
268                assert(err_is_ok(err));
269            }
270            free(ns->copies);
271            free(ns->delcycles);
272            break;
273        default:
274            printf("node %d got command %"PRIu32"\n", my_core_id, cmd);
275            break;
276    }
277}
278
279void node_cmd_caps(uint32_t cmd, uint32_t arg, struct capref cap1,
280                   struct bench_distops_binding *b)
281{
282    errval_t err;
283    struct node_state *ns = b->st;
284
285    switch (cmd) {
286        case BENCH_CMD_CREATE_COPIES:
287            printf("# node %d: creating %d cap copies\n", my_core_id, arg);
288            ns->ram = cap1;
289            ns->numcopies = arg;
290            node_create_copies(ns);
291            // First round, also get a local RAM cap for the benchmark later
292            err = ram_alloc(&ns->cap, BASE_PAGE_BITS);
293            assert(err_is_ok(err));
294            printf("# node %d: %zu caps on node\n", my_core_id, get_mdb_size());
295            err = bench_distops_cmd__tx(b, NOP_CONT, BENCH_CMD_COPIES_DONE, 0);
296            PANIC_IF_ERR(err, "signaling cap_copy() done\n");
297            break;
298        default:
299            printf("node %d got caps + command %"PRIu32", arg=%d:\n",
300                my_core_id, cmd, arg);
301            debug_capref("cap1:", cap1);
302            break;
303    }
304}
305