| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | #ifndef __RT_UTILS_H |
| 3 | #define __RT_UTILS_H |
| 4 | |
| 5 | #include <stdint.h> |
| 6 | |
| 7 | #define _STR(x) #x |
| 8 | #define STR(x) _STR(x) |
| 9 | #define MAX_PATH 256 |
| 10 | |
| 11 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 12 | |
| 13 | int check_privs(void); |
| 14 | char *get_tracefs_prefix(void); |
| 15 | int get_tracers(char ***); |
| 16 | int valid_tracer(char *); |
| 17 | |
| 18 | int setevent(char *event, char *val); |
| 19 | int event_enable(char *event); |
| 20 | int event_disable(char *event); |
| 21 | int event_enable_all(void); |
| 22 | int event_disable_all(void); |
| 23 | |
| 24 | const char *policy_to_string(int policy); |
| 25 | uint32_t string_to_policy(const char *str); |
| 26 | |
| 27 | pid_t gettid(void); |
| 28 | |
| 29 | int parse_time_string(char *val); |
| 30 | int parse_mem_string(char *str, uint64_t *val); |
| 31 | |
| 32 | void enable_trace_mark(void); |
| 33 | void tracemark(char *fmt, ...) __attribute__((format(printf, 1, 2))); |
| 34 | void tracing_stop(void); |
| 35 | void disable_trace_mark(void); |
| 36 | |
| 37 | #define MSEC_PER_SEC 1000 |
| 38 | #define USEC_PER_SEC 1000000 |
| 39 | #define NSEC_PER_SEC 1000000000 |
| 40 | #define USEC_TO_NSEC(u) ((u) * 1000) |
| 41 | #define USEC_TO_SEC(u) (u) / USEC_PER_SEC) |
| 42 | #define NSEC_TO_USEC(n) ((n) / 1000) |
| 43 | #define SEC_TO_NSEC(s) ((s) * NSEC_PER_SEC) |
| 44 | #define SEC_TO_USEC(s) ((s) * USEC_PER_SEC) |
| 45 | |
| 46 | static inline void tsnorm(struct timespec *ts) |
| 47 | { |
| 48 | while (ts->tv_nsec >= NSEC_PER_SEC) { |
| 49 | ts->tv_nsec -= NSEC_PER_SEC; |
| 50 | ts->tv_sec++; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static inline int tsgreater(struct timespec *a, struct timespec *b) |
| 55 | { |
| 56 | return ((a->tv_sec > b->tv_sec) || |
| 57 | (a->tv_sec == b->tv_sec && a->tv_nsec > b->tv_nsec)); |
| 58 | } |
| 59 | |
| 60 | static inline int64_t calcdiff(struct timespec t1, struct timespec t2) |
| 61 | { |
| 62 | int64_t diff = USEC_PER_SEC * (long long)((int) t1.tv_sec - (int) t2.tv_sec); |
| 63 | diff += ((int) t1.tv_nsec - (int) t2.tv_nsec) / 1000; |
| 64 | return diff; |
| 65 | } |
| 66 | |
| 67 | static inline int64_t calcdiff_ns(struct timespec t1, struct timespec t2) |
| 68 | { |
| 69 | int64_t diff; |
| 70 | diff = NSEC_PER_SEC * (int64_t)((int) t1.tv_sec - (int) t2.tv_sec); |
| 71 | diff += ((int) t1.tv_nsec - (int) t2.tv_nsec); |
| 72 | return diff; |
| 73 | } |
| 74 | |
| 75 | static inline int64_t calctime(struct timespec t) |
| 76 | { |
| 77 | int64_t time; |
| 78 | time = USEC_PER_SEC * t.tv_sec; |
| 79 | time += ((int) t.tv_nsec) / 1000; |
| 80 | return time; |
| 81 | } |
| 82 | |
| 83 | void rt_init(int argc, char *argv[]); |
| 84 | |
| 85 | void rt_write_json(const char *filename, int return_code, |
| 86 | void (*cb)(FILE *, void *), |
| 87 | void *data); |
| 88 | |
| 89 | #endif /* __RT_UTILS.H */ |