Merge origin/netpush (networking code) (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 <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);
27 uint64_t tsc2sec(uint64_t tsc_time);
28 uint64_t tsc2msec(uint64_t tsc_time);
29 uint64_t tsc2usec(uint64_t tsc_time);
30 uint64_t tsc2nsec(uint64_t tsc_time);
31 uint64_t sec2tsc(uint64_t sec);
32 uint64_t msec2tsc(uint64_t msec);
33 uint64_t usec2tsc(uint64_t usec);
34 uint64_t nsec2tsc(uint64_t nsec);
35
36 /* Just takes a time measurement.  Meant to be paired with stop_timing.  Use
37  * this if you don't want to muck with overheads or subtraction. */
38 static inline __attribute__((always_inline))
39 uint64_t start_timing(void)
40 {
41     return read_tsc_serialized();
42 }
43
44 /* Takes a time measurement and subtracts the start time and timing overhead,
45  * to return the detected elapsed time.  Use this if you don't want to muck
46  * with overheads or subtraction. */
47 static inline __attribute__((always_inline))
48 uint64_t stop_timing(uint64_t start_time)
49 {
50     uint64_t diff = read_tsc_serialized();
51     diff -= start_time;
52     diff -= system_timing.timing_overhead;
53         if ((int64_t) diff < 0) {
54                 return 1;
55         }
56         return diff;
57 }
58
59 /* Ancient measurement crap below.  TODO: use or lose it */
60
61 #if 0
62 #include <pool.h>
63 #include <string.h>
64
65 #define TIMER_TAG_SIZE 20
66 #define MAX_TIMERS 20
67 /* timer_t
68  * This struct is used to keep track of counter values as they are spread
69  * throughput code and timing measurements are made calling TAGGED_TIMING_BEGIN
70  * and TAGGED_TIMING_END
71  */
72 typedef struct Timer{
73         uint64_t curr_run;
74         uint64_t aggr_run;
75         char label[TIMER_TAG_SIZE];
76 } timer_t;
77
78 #define TAGGED_TIMING_BEGIN(tag)                    \
79         static timer_t* _timer_##tag = NULL;            \
80         if (_timer_##tag == NULL) {                     \
81                 _timer_##tag = POOL_GET(&timer_pool);       \
82                 strcpy((_timer_##tag->label), #tag);        \
83                 _timer_##tag->aggr_run = 0;                 \
84         }                                               \
85         _timer_##tag->curr_run = start_timing();
86 #define TAGGED_TIMING_END(tag)                                              \
87 ({                                                                          \
88         _timer_##tag->curr_run = stop_timing(_timer_##tag->curr_run);           \
89         _timer_##tag->aggr_run += _timer_##tag->curr_run;                       \
90 })
91
92 #endif
93 #endif /* ROS_KERN_TIME_H */