-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
flb_time.c
208 lines (182 loc) · 5.33 KB
/
flb_time.c
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2019 The Fluent Bit Authors
* Copyright (C) 2015-2018 Treasure Data Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <msgpack.h>
#include <fluent-bit/flb_compat.h>
#include <fluent-bit/flb_macros.h>
#include <fluent-bit/flb_log.h>
#include <fluent-bit/flb_time.h>
#ifdef FLB_HAVE_CLOCK_GET_TIME
# include <mach/clock.h>
# include <mach/mach.h>
#endif
#include <string.h>
#include <inttypes.h>
#include <time.h>
#define ONESEC_IN_NSEC 1000000000
static int is_valid_format(int fmt)
{
return (FLB_TIME_ETFMT_INT <= fmt) && (fmt < FLB_TIME_ETFMT_OTHER) ?
FLB_TRUE : FLB_FALSE;
}
static int _flb_time_get(struct flb_time *tm)
{
if (tm == NULL) {
return -1;
}
#if defined FLB_TIME_FORCE_FMT_INT
tm->tm.tv_sec = time(NULL);
tm->tm.tv_nsec = 0;
return 0;
#elif defined FLB_HAVE_TIMESPEC_GET
/* C11 supported! */
return timespec_get(&tm->tm, TIME_UTC);
#elif defined FLB_CLOCK_GET_TIME
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
tm->tv_sec = mts.tv_sec;
tm->tv_nsec = mts.tv_nsec;
return mach_port_deallocate(mach_task_self(), cclock);
#else /* __STDC_VERSION__ */
return clock_gettime(CLOCK_REALTIME, &tm->tm);
#endif
}
int flb_time_get(struct flb_time *tm)
{
return _flb_time_get(tm);
}
/* A portable function to sleep N msec */
int flb_time_msleep(uint32_t ms)
{
#ifdef _MSC_VER
Sleep((DWORD) ms);
return 0;
#else
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000;
return nanosleep(&ts, NULL);
#endif
}
double flb_time_to_double(struct flb_time *tm)
{
return (double)(tm->tm.tv_sec) + ((double)tm->tm.tv_nsec/(double)ONESEC_IN_NSEC);
}
int flb_time_diff(struct flb_time *time1,
struct flb_time *time0,struct flb_time *result)
{
if (time1 == NULL || time0 == NULL || result == NULL) {
return -1;
}
if (time1->tm.tv_sec >= time0->tm.tv_sec) {
result->tm.tv_sec = time1->tm.tv_sec - time0->tm.tv_sec;
if (time1->tm.tv_nsec >= time0->tm.tv_nsec) {
result->tm.tv_nsec = time1->tm.tv_nsec - time0->tm.tv_nsec;
}
else if(result->tm.tv_sec == 0){
/* underflow */
return -1;
}
else{
result->tm.tv_nsec = ONESEC_IN_NSEC
+ time1->tm.tv_nsec - time0->tm.tv_nsec;
result->tm.tv_sec--;
}
}
else {
/* underflow */
return -1;
}
return 0;
}
int flb_time_append_to_msgpack(struct flb_time *tm, msgpack_packer *pk, int fmt)
{
int ret = 0;
struct flb_time l_time;
char ext_data[8];
uint32_t tmp;
if (!is_valid_format(fmt)) {
#ifdef FLB_TIME_FORCE_FMT_INT
fmt = FLB_TIME_ETFMT_INT;
#else
fmt = FLB_TIME_ETFMT_V1_FIXEXT;
#endif
}
if (tm == NULL) {
if (fmt == FLB_TIME_ETFMT_INT) {
l_time.tm.tv_sec = time(NULL);
}
else {
_flb_time_get(&l_time);
}
tm = &l_time;
}
switch(fmt) {
case FLB_TIME_ETFMT_INT:
msgpack_pack_uint64(pk, tm->tm.tv_sec);
break;
case FLB_TIME_ETFMT_V0:
case FLB_TIME_ETFMT_V1_EXT:
/* We can't set with msgpack-c !! */
/* see pack_template.h and msgpack_pack_inline_func(_ext) */
case FLB_TIME_ETFMT_V1_FIXEXT:
tmp = htonl((uint32_t)tm->tm.tv_sec); /* second from epoch */
memcpy(&ext_data, &tmp, 4);
tmp = htonl((uint32_t)tm->tm.tv_nsec);/* nanosecond */
memcpy(&ext_data[4], &tmp, 4);
msgpack_pack_ext(pk, 8/*fixext8*/, 0);
msgpack_pack_ext_body(pk, ext_data, sizeof(ext_data));
break;
default:
ret = -1;
}
return ret;
}
int flb_time_pop_from_msgpack(struct flb_time *time, msgpack_unpacked *upk,
msgpack_object **map)
{
msgpack_object obj;
uint32_t tmp;
if(time == NULL || upk == NULL) {
return -1;
}
obj = upk->data.via.array.ptr[0];
*map = &upk->data.via.array.ptr[1];
switch(obj.type){
case MSGPACK_OBJECT_POSITIVE_INTEGER:
time->tm.tv_sec = obj.via.u64;
time->tm.tv_nsec = 0;
break;
case MSGPACK_OBJECT_FLOAT:
time->tm.tv_sec = obj.via.f64;
time->tm.tv_nsec = ((obj.via.f64 - time->tm.tv_sec) * ONESEC_IN_NSEC);
break;
case MSGPACK_OBJECT_EXT:
memcpy(&tmp, &obj.via.ext.ptr[0], 4);
time->tm.tv_sec = (uint32_t)ntohl(tmp);
memcpy(&tmp, &obj.via.ext.ptr[4], 4);
time->tm.tv_nsec = (uint32_t)ntohl(tmp);
break;
default:
flb_warn("unknown time format %x", obj.type);
return -1;
}
return 0;
}