Click
Loading...
Searching...
No Matches
tsctimestamp.hh
1// -*- c-basic-offset: 4 -*-
2#ifndef CLICK_TSCTIMESTAMP_HH
3#define CLICK_TSCTIMESTAMP_HH
4#include <click/glue.hh>
5#include <click/timestamp.hh>
6
7CLICK_DECLS
8
9class TSCTimestamp {
10 public:
11 typedef uninitialized_type uninitialized_t;
12 typedef int64_t cycles_t;
13
14 TSCTimestamp() {
15 val = 0;
16 }
17
18 explicit TSCTimestamp(cycles_t v) {
19 val = v;
20 }
21
22 static inline click_cycles_t cycles_hz_warp() {
23#if TIMESTAMP_WARPABLE
24 if (Timestamp::warp_class() == Timestamp::warp_simulation) {
25 return 3000000000;
26 }
27#endif
28 return cycles_hz();
29 }
30
32 inline TSCTimestamp(const uninitialized_t &unused) {
33 (void) unused;
34 }
35
36 static inline TSCTimestamp now() {
37#if TIMESTAMP_WARPABLE
38 if (Timestamp::warp_class() == Timestamp::warp_simulation) {
39 return TSCTimestamp((cycles_t)Timestamp::now_steady().nsecval() * 3);
40 }
41#endif
42 return TSCTimestamp(click_get_cycles());
43 }
44
45 static inline TSCTimestamp now_steady() {
46 return now();
47 }
48
49 friend inline bool operator>(const TSCTimestamp &a, const TSCTimestamp &b);
50 friend inline bool operator<(const TSCTimestamp &a, const TSCTimestamp &b);
51 friend inline bool operator>=(const TSCTimestamp &a, const TSCTimestamp &b);
52 friend inline bool operator<=(const TSCTimestamp &a, const TSCTimestamp &b);
53 friend inline bool operator==(const TSCTimestamp &a, const TSCTimestamp &b);
54 friend inline bool operator!=(const TSCTimestamp &a, const TSCTimestamp &b);
55 friend inline TSCTimestamp operator+(const TSCTimestamp &a, const TSCTimestamp &b);
56 friend inline TSCTimestamp operator-(const TSCTimestamp &a, const TSCTimestamp &b);
57
58
59 inline String unparse() const {
60 double t = (double)(val) / (double)cycles_hz_warp();
61 return String(t);
62 }
63
64
68 inline int64_t msecval() {
69 return val / (cycles_hz_warp() / 1000);
70 }
71
75 inline int64_t usecval() {
76 return val / (cycles_hz_warp() / 1000000);
77 }
78
82 inline double nsecval() {
83 return (double)val / ((double)cycles_hz_warp() / 1000000000.0f);
84 }
85
86 inline int64_t tsc_val() {
87 return val;
88 }
89
90 static inline TSCTimestamp make_msec(unsigned n) {
91 return TSCTimestamp((n * cycles_hz_warp()) / 1000);
92 }
93
94 static inline TSCTimestamp make_usec(unsigned n) {
95 return TSCTimestamp((n * cycles_hz_warp()) / 1000000);
96 }
97
98 inline operator Timestamp() {
99 return Timestamp::make_nsec((double)val / ((double)cycles_hz_warp() / 1000000000.0));
100 }
101
102
103 private:
104 cycles_t val;
105};
106
111inline bool
112operator!=(const TSCTimestamp &a, const TSCTimestamp &b)
113{
114 return a.val != b.val;
115}
116
117
122inline bool
123operator==(const TSCTimestamp &a, const TSCTimestamp &b)
124{
125 return a.val == b.val;
126}
127
128inline bool operator<(const TSCTimestamp &a, const TSCTimestamp &b) {
129 return a.val < b.val;
130}
131
132inline bool operator<=(const TSCTimestamp &a, const TSCTimestamp &b) {
133 return a.val <= b.val;
134}
135
136inline bool operator>(const TSCTimestamp &a, const TSCTimestamp &b) {
137 return a.val > b.val;
138}
139
140inline bool operator>=(const TSCTimestamp &a, const TSCTimestamp &b) {
141 return a.val >= b.val;
142}
143
144
145inline TSCTimestamp operator+(const TSCTimestamp &a, const TSCTimestamp &b) {
146 return TSCTimestamp(a.val + b.val);
147}
148
149inline TSCTimestamp operator-(const TSCTimestamp &a, const TSCTimestamp &b) {
150 return TSCTimestamp(a.val - b.val);
151}
152
153CLICK_ENDDECLS
154
155#endif
A string of characters.
Definition string.hh:22
Represents a moment or interval in time.
Definition timestamp.hh:125
static Timestamp now_steady()
Return the current steady-clock time.
Definition timestamp.hh:986
static Timestamp make_nsec(seconds_type sec, uint32_t nsec)
Return a timestamp representing sec seconds plus nsec nanoseconds.
Definition timestamp.hh:366
The Timestamp class represents a moment or interval in time.