Kernel alarm tchains use locks
[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 <arch/time.h>
6
7 /* (newlib) Time Value Specification Structures, P1003.1b-1993, p. 261 */
8 typedef long time_t;
9
10 struct timespec {
11   time_t  tv_sec;   /* Seconds */
12   long    tv_nsec;  /* Nanoseconds */
13 };
14
15 struct itimerspec {
16   struct timespec  it_interval;  /* Timer period */
17   struct timespec  it_value;     /* Timer expiration */
18 };
19
20 struct timeval {
21         time_t tv_sec;  /* seconds */
22         time_t tv_usec; /* microseconds */
23 };
24
25 void train_timing();
26 void udelay(uint64_t usec);     /* done in arch-specific files */
27 void udelay_sched(uint64_t usec);
28 uint64_t tsc2sec(uint64_t tsc_time);
29 uint64_t tsc2msec(uint64_t tsc_time);
30 uint64_t tsc2usec(uint64_t tsc_time);
31 uint64_t tsc2nsec(uint64_t tsc_time);
32 uint64_t sec2tsc(uint64_t sec);
33 uint64_t msec2tsc(uint64_t msec);
34 uint64_t usec2tsc(uint64_t usec);
35 uint64_t nsec2tsc(uint64_t nsec);
36
37 /* Just takes a time measurement.  Meant to be paired with stop_timing.  Use
38  * this if you don't want to muck with overheads or subtraction. */
39 static inline __attribute__((always_inline))
40 uint64_t start_timing(void)
41 {
42     return read_tsc_serialized();
43 }
44
45 /* Takes a time measurement and subtracts the start time and timing overhead,
46  * to return the detected elapsed time.  Use this if you don't want to muck
47  * with overheads or subtraction. */
48 static inline __attribute__((always_inline))
49 uint64_t stop_timing(uint64_t start_time)
50 {
51     uint64_t diff = read_tsc_serialized();
52     diff -= start_time;
53     diff -= system_timing.timing_overhead;
54         if ((int64_t) diff < 0) {
55                 return 1;
56         }
57         return diff;
58 }
59
60 /* Ancient measurement crap below.  TODO: use or lose it */
61
62 #if 0
63 #include <pool.h>
64 #include <string.h>
65
66 #define TIMER_TAG_SIZE 20
67 #define MAX_TIMERS 20
68 /* timer_t
69  * This struct is used to keep track of counter values as they are spread
70  * throughput code and timing measurements are made calling TAGGED_TIMING_BEGIN
71  * and TAGGED_TIMING_END
72  */
73 typedef struct Timer{
74         uint64_t curr_run;
75         uint64_t aggr_run;
76         char label[TIMER_TAG_SIZE];
77 } timer_t;
78
79 #define TAGGED_TIMING_BEGIN(tag)                    \
80         static timer_t* _timer_##tag = NULL;            \
81         if (_timer_##tag == NULL) {                     \
82                 _timer_##tag = POOL_GET(&timer_pool);       \
83                 strcpy((_timer_##tag->label), #tag);        \
84                 _timer_##tag->aggr_run = 0;                 \
85         }                                               \
86         _timer_##tag->curr_run = start_timing();
87 #define TAGGED_TIMING_END(tag)                                              \
88 ({                                                                          \
89         _timer_##tag->curr_run = stop_timing(_timer_##tag->curr_run);           \
90         _timer_##tag->aggr_run += _timer_##tag->curr_run;                       \
91 })
92
93 #endif
94 #endif /* ROS_KERN_TIME_H */