xref: /freebsd/contrib/jemalloc/include/jemalloc/internal/nstime.h (revision c43cad87172039ccf38172129c79755ea79e6102)
1 #ifndef JEMALLOC_INTERNAL_NSTIME_H
2 #define JEMALLOC_INTERNAL_NSTIME_H
3 
4 /* Maximum supported number of seconds (~584 years). */
5 #define NSTIME_SEC_MAX KQU(18446744072)
6 
7 #define NSTIME_MAGIC ((uint32_t)0xb8a9ce37)
8 #ifdef JEMALLOC_DEBUG
9 #  define NSTIME_ZERO_INITIALIZER {0, NSTIME_MAGIC}
10 #else
11 #  define NSTIME_ZERO_INITIALIZER {0}
12 #endif
13 
14 typedef struct {
15 	uint64_t ns;
16 #ifdef JEMALLOC_DEBUG
17 	uint32_t magic; /* Tracks if initialized. */
18 #endif
19 } nstime_t;
20 
21 static const nstime_t nstime_zero = NSTIME_ZERO_INITIALIZER;
22 
23 void nstime_init(nstime_t *time, uint64_t ns);
24 void nstime_init2(nstime_t *time, uint64_t sec, uint64_t nsec);
25 uint64_t nstime_ns(const nstime_t *time);
26 uint64_t nstime_sec(const nstime_t *time);
27 uint64_t nstime_msec(const nstime_t *time);
28 uint64_t nstime_nsec(const nstime_t *time);
29 void nstime_copy(nstime_t *time, const nstime_t *source);
30 int nstime_compare(const nstime_t *a, const nstime_t *b);
31 void nstime_add(nstime_t *time, const nstime_t *addend);
32 void nstime_iadd(nstime_t *time, uint64_t addend);
33 void nstime_subtract(nstime_t *time, const nstime_t *subtrahend);
34 void nstime_isubtract(nstime_t *time, uint64_t subtrahend);
35 void nstime_imultiply(nstime_t *time, uint64_t multiplier);
36 void nstime_idivide(nstime_t *time, uint64_t divisor);
37 uint64_t nstime_divide(const nstime_t *time, const nstime_t *divisor);
38 uint64_t nstime_ns_since(const nstime_t *past);
39 
40 typedef bool (nstime_monotonic_t)(void);
41 extern nstime_monotonic_t *JET_MUTABLE nstime_monotonic;
42 
43 typedef void (nstime_update_t)(nstime_t *);
44 extern nstime_update_t *JET_MUTABLE nstime_update;
45 
46 typedef void (nstime_prof_update_t)(nstime_t *);
47 extern nstime_prof_update_t *JET_MUTABLE nstime_prof_update;
48 
49 void nstime_init_update(nstime_t *time);
50 void nstime_prof_init_update(nstime_t *time);
51 
52 enum prof_time_res_e {
53 	prof_time_res_default = 0,
54 	prof_time_res_high = 1
55 };
56 typedef enum prof_time_res_e prof_time_res_t;
57 
58 extern prof_time_res_t opt_prof_time_res;
59 extern const char *prof_time_res_mode_names[];
60 
61 JEMALLOC_ALWAYS_INLINE void
62 nstime_init_zero(nstime_t *time) {
63 	nstime_copy(time, &nstime_zero);
64 }
65 
66 JEMALLOC_ALWAYS_INLINE bool
67 nstime_equals_zero(nstime_t *time) {
68 	int diff = nstime_compare(time, &nstime_zero);
69 	assert(diff >= 0);
70 	return diff == 0;
71 }
72 
73 #endif /* JEMALLOC_INTERNAL_NSTIME_H */
74