1/*
2 * Copyright (c) 2014, University of Washington.
3 * All rights reserved.
4 *
5 * This file is distributed under the terms in the attached LICENSE file.
6 * If you do not find this file, copies can be found by writing to:
7 * ETH Zurich D-INFK, CAB F.78, Universitaetstrasse 6, CH-8092 Zurich.
8 * Attn: Systems Group.
9 */
10
11#ifndef ARRANET_DEBUG_H
12#define ARRANET_DEBUG_H
13
14//#define DEBUG_LATENCIES
15
16//#include <lwip/udp.h>
17#include <arranet_impl.h>
18#include <barrelfish/sys_debug.h>
19
20#define POSIX_TRANSA    1000
21
22#define MIN(a,b)        ((a) < (b) ? (a) : (b))
23
24#define	timersub(a, b, result)						      \
25  do {									      \
26    (result)->tv_sec = (a)->tv_sec - (b)->tv_sec;			      \
27    (result)->tv_usec = (a)->tv_usec - (b)->tv_usec;			      \
28    if ((result)->tv_usec < 0) {					      \
29      --(result)->tv_sec;						      \
30      (result)->tv_usec += 1000000;					      \
31    }									      \
32  } while (0)
33
34    /**
35     * Definition of the header structure for a request packet.
36     * See section 2
37     */
38    typedef union {
39        struct {
40            uint8_t magic;
41            uint8_t opcode;
42            uint16_t keylen;
43            uint8_t extlen;
44            uint8_t datatype;
45            uint16_t reserved;
46            uint32_t bodylen;
47            uint32_t opaque;
48            uint64_t cas;
49        } request;
50        uint8_t bytes[24];
51    } protocol_binary_request_header;
52
53    /**
54     * Definition of the header structure for a response packet.
55     * See section 2
56     */
57    typedef union {
58        struct {
59            uint8_t magic;
60            uint8_t opcode;
61            uint16_t keylen;
62            uint8_t extlen;
63            uint8_t datatype;
64            uint16_t status;
65            uint32_t bodylen;
66            uint32_t opaque;
67            uint64_t cas;
68        } response;
69        uint8_t bytes[24];
70    } protocol_binary_response_header;
71
72    /**
73     * Definition of a request-packet containing no extras
74     */
75    typedef union {
76        struct {
77            protocol_binary_request_header header;
78        } message;
79        uint8_t bytes[sizeof(protocol_binary_request_header)];
80    } protocol_binary_request_no_extras;
81
82    /**
83     * Definition of a response-packet containing no extras
84     */
85    typedef union {
86        struct {
87            protocol_binary_response_header header;
88        } message;
89        uint8_t bytes[sizeof(protocol_binary_response_header)];
90    } protocol_binary_response_no_extras;
91
92typedef struct {
93  uint16_t req_id;
94  uint16_t seq_id;
95  uint16_t n_data;
96  uint16_t extras;
97} memcached_udp_header;
98
99#define UDP_HEADLEN sizeof(memcached_udp_header)
100
101static inline int get_time(void)
102{
103    uint64_t now = rdtsc();
104    static uint64_t tscperms = 0;
105
106    if(tscperms == 0) {
107        errval_t err = sys_debug_get_tsc_per_ms(&tscperms);
108        assert(err_is_ok(err));
109        assert(tscperms >= 100000);
110    }
111
112    uint64_t tod_us = now / (tscperms / 100000);
113
114    return tod_us;
115}
116
117#endif
118