Make some macros correct when used within IF clauses without brackets
[akaros.git] / kern / include / time.h
1 #pragma once
2
3 #include <ros/common.h>
4 #include <ros/time.h>
5 #include <arch/time.h>
6
7 void train_timing();
8 void udelay(uint64_t usec);     /* done in arch-specific files */
9 uint64_t tsc2sec(uint64_t tsc_time);
10 uint64_t tsc2msec(uint64_t tsc_time);
11 uint64_t tsc2usec(uint64_t tsc_time);
12 uint64_t tsc2nsec(uint64_t tsc_time);
13 uint64_t sec2tsc(uint64_t sec);
14 uint64_t msec2tsc(uint64_t msec);
15 uint64_t usec2tsc(uint64_t usec);
16 uint64_t nsec2tsc(uint64_t nsec);
17 uint64_t epoch_tsc(void);
18 uint64_t epoch_sec(void);
19 uint64_t epoch_msec(void);
20 uint64_t epoch_usec(void);
21 uint64_t epoch_nsec(void);
22 void tsc2timespec(uint64_t tsc_time, struct timespec *ts);
23
24 /* Just takes a time measurement.  Meant to be paired with stop_timing.  Use
25  * this if you don't want to muck with overheads or subtraction. */
26 static inline __attribute__((always_inline))
27 uint64_t start_timing(void)
28 {
29     return read_tsc_serialized();
30 }
31
32 /* Takes a time measurement and subtracts the start time and timing overhead,
33  * to return the detected elapsed time.  Use this if you don't want to muck
34  * with overheads or subtraction. */
35 static inline __attribute__((always_inline))
36 uint64_t stop_timing(uint64_t start_time)
37 {
38     uint64_t diff = read_tsc_serialized();
39     diff -= start_time;
40     diff -= system_timing.timing_overhead;
41         if ((int64_t) diff < 0) {
42                 return 1;
43         }
44         return diff;
45 }
46
47 static inline __attribute__((always_inline))
48 uint64_t nsec(void)
49 {
50         return tsc2nsec(read_tsc());
51 }
52
53
54 /* Ancient measurement crap below.  TODO: use or lose it */
55
56 #if 0
57 #include <pool.h>
58 #include <string.h>
59
60 #define TIMER_TAG_SIZE 20
61 #define MAX_TIMERS 20
62 /* timer_t
63  * This struct is used to keep track of counter values as they are spread
64  * throughput code and timing measurements are made calling TAGGED_TIMING_BEGIN
65  * and TAGGED_TIMING_END
66  */
67 typedef struct Timer{
68         uint64_t curr_run;
69         uint64_t aggr_run;
70         char label[TIMER_TAG_SIZE];
71 } timer_t;
72
73 #define TAGGED_TIMING_BEGIN(tag)                    \
74         static timer_t* _timer_##tag = NULL;            \
75         if (_timer_##tag == NULL) {                     \
76                 _timer_##tag = POOL_GET(&timer_pool);       \
77                 strcpy((_timer_##tag->label), #tag);        \
78                 _timer_##tag->aggr_run = 0;                 \
79         }                                               \
80         _timer_##tag->curr_run = start_timing();
81 #define TAGGED_TIMING_END(tag)                                              \
82 ({                                                                          \
83         _timer_##tag->curr_run = stop_timing(_timer_##tag->curr_run);           \
84         _timer_##tag->aggr_run += _timer_##tag->curr_run;                       \
85 })
86
87 #endif