-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.h
38 lines (32 loc) · 858 Bytes
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef __UTILS_H__
#define __UTILS_H__
#include <stdint.h>
#include <pthread.h>
void fd_write(int fd, void *buffer, size_t size);
void fd_read(int fd, void *buffer, size_t size);
void socket_set_fast_reuse(int fd);
void pthread_set_name(pthread_t thread, const char *name);
unsigned long parse_size_to_mega(const char *str);
static inline uint64_t get_usec(void)
{
uint64_t val = 0;
struct timespec t;
int ret = clock_gettime(CLOCK_MONOTONIC, &t);
if (ret == -1) {
perror("clock_gettime() failed");
/* should never happen */
exit(-1);
}
val = t.tv_nsec / 1000; /* ns -> us */
val += t.tv_sec * 1000000; /* s -> us */
return val;
}
static inline uint64_t get_msec(void)
{
return get_usec() / 1000;
}
static inline uint64_t get_timestamp(void)
{
return (uint64_t)time(NULL);
}
#endif