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