benchmark silo added
[c11concurrency-benchmarks.git] / silo / masstree / timestamp.hh
1 /* Masstree
2  * Eddie Kohler, Yandong Mao, Robert Morris
3  * Copyright (c) 2012-2013 President and Fellows of Harvard College
4  * Copyright (c) 2012-2013 Massachusetts Institute of Technology
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, subject to the conditions
9  * listed in the Masstree LICENSE file. These conditions include: you must
10  * preserve this copyright notice, and you cannot mention the copyright
11  * holders in advertising related to the Software without their permission.
12  * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
13  * notice is a summary of the Masstree LICENSE file; the license in that file
14  * is legally binding.
15  */
16 #ifndef TIMESTAMP_HH
17 #define TIMESTAMP_HH
18 #include "compiler.hh"
19 #include <time.h>
20 #include <sys/time.h>
21 #include <math.h>
22
23 #if HAVE_INT64_T_IS_LONG_LONG
24 #define PRIuKVTS "llu"
25 #else
26 #define PRIuKVTS "lu"
27 #endif
28 #define PRIKVTSPARTS "%lu.%06lu"
29
30 #define KVTS_HIGHPART(t) ((unsigned long) ((t) >> 32))
31 #define KVTS_LOWPART(t) ((unsigned long) (uint32_t) (t))
32
33 typedef uint64_t kvtimestamp_t;
34
35 inline kvtimestamp_t timestamp() {
36     struct timeval tv;
37     gettimeofday(&tv, 0);
38     return ((kvtimestamp_t) tv.tv_sec << 32) | (unsigned int)tv.tv_usec;
39 }
40
41 inline kvtimestamp_t timestamp_sub(kvtimestamp_t a, kvtimestamp_t b) {
42     a -= b;
43     if (KVTS_LOWPART(a) > 999999)
44         a -= ((kvtimestamp_t) 1 << 32) - 1000000;
45     return a;
46 }
47
48 extern kvtimestamp_t initial_timestamp;
49
50 inline double now() {
51     struct timeval tv;
52     gettimeofday(&tv, 0);
53     return tv.tv_sec + tv.tv_usec / 1000000.0;
54 }
55
56 inline struct timespec &set_timespec(struct timespec &x, double y) {
57     double ipart = floor(y);
58     x.tv_sec = (long) ipart;
59     x.tv_nsec = (long) ((y - ipart) * 1e9);
60     return x;
61 }
62
63 #endif