1/**
2 * \file
3 * \brief Microbenchmark to report the cost
4 * of reading from the timestamp counter.
5 */
6
7/*
8 * Copyright (c) 2007, 2008, 2009, ETH Zurich.
9 * All rights reserved.
10 *
11 * This file is distributed under the terms in the attached LICENSE file.
12 * If you do not find this file, copies can be found by writing to:
13 * ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
14 */
15
16#include <stdio.h>
17#include <barrelfish/barrelfish.h>
18#include <bench/bench.h>
19
20struct timestamp {
21    uint64_t time0;
22    uint64_t time1;
23};
24
25#define MAX_COUNT 1000
26
27static void tsc_bench(void)
28{
29    bench_init();
30
31    struct timestamp *timestamp = malloc(sizeof(struct timestamp) * MAX_COUNT);
32    assert(timestamp);
33
34    timestamp[0].time0 = bench_tsc();
35    for (int i = 0; i < MAX_COUNT - 1; i++) {
36        timestamp[i].time1 = timestamp[i + 1].time0 = bench_tsc();
37    }
38    timestamp[MAX_COUNT - 1].time1 = bench_tsc();
39
40    for (int i = 0; i < MAX_COUNT - 1; i++) {
41        printf("Iteration %d: time0 %"PRIu64" time1 %"PRIu64" difference %"PRIu64"\n",
42               i, timestamp[i].time0, timestamp[i].time1,
43               timestamp[i].time1 - timestamp[i].time0);
44    }
45}
46
47#if 0
48static void counter_reply(struct hpet_client_response *st, struct capref cap,
49                          uint64_t index)
50{
51    errval_t err;
52    void* buf;
53    err = vspace_map_one_frame_attr(&buf, BASE_PAGE_SIZE, cap,
54                                    VREGION_FLAGS_READ_WRITE_NOCACHE,
55                                    NULL, NULL);
56    if (err_is_fail(err)) {
57        DEBUG_ERR(err, "vspace_map_one_frame_attr failed");
58        printf("vspace_map_one_frame_attr failed in counter_reply\n");
59    }
60
61    bench_init();
62}
63
64static void _connected(struct hpet_client_response *st)
65{
66    errval_t err = st->call_vtbl->counter_request(st);
67    if (err_is_fail(err)) {
68        DEBUG_ERR(err, "Failed to send counter request");
69        printf("Failed to send counter request\n");
70        exit(EXIT_FAILURE);
71    }
72}
73
74static void hpet_bench(uint64_t iterations)
75{
76    errval_t err;
77
78    /* Connect to HPET driver */
79    iref_t iref;
80    err = chips_blocking_lookup(chips_get_context(), "hpet", &iref);
81    if (err_is_fail(err)) {
82        DEBUG_ERR(err, "failed to lookup server");
83        exit(EXIT_FAILURE);
84    }
85    assert(iref != 0);
86
87    static struct hpet_client_response_vtbl crv = {
88        ._disconnect = NULL,
89        ._connected  = _connected,
90        .counter_reply = counter_reply,
91    };
92    static struct hpet_client_response hcr = {
93        .f = &crv,
94    };
95
96    hpet_connect(iref, &hcr, 0);
97}
98#endif
99
100int main(int argc, char *argv[])
101{
102    tsc_bench();
103    printf("client done\n");
104    return 0;
105}
106