-
Notifications
You must be signed in to change notification settings - Fork 9
/
fTypes.h
543 lines (433 loc) · 10.1 KB
/
fTypes.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
//---------------------------------------------------------------------------------------------
//
// Copyright (c) 2015, fmad engineering llc
//
// The MIT License (MIT) see LICENSE file for details
//
// pcap latency diff
//
//---------------------------------------------------------------------------------------------
#ifndef __F_TYPES_H__
#define __F_TYPES_H__
#include <math.h>
#include <limits.h>
#include <assert.h>
#include <time.h>
#include <sys/time.h>
typedef unsigned int bool;
#define true 1
#define false 0
typedef unsigned char u8;
typedef char s8;
typedef unsigned short u16;
typedef short s16;
typedef unsigned int u32;
typedef int s32;
typedef unsigned long long u64;
typedef long long s64;
typedef unsigned __int128 u128;
typedef union
{
u128 d128;
u32 d32[4];
u64 d64[2];
} u128_u;
#define k1E9 1000000000ULL
#define kKB(a) ( ((u64)a)*1024ULL)
#define kMB(a) ( ((u64)a)*1024ULL*1024ULL)
#define kGB(a) ( ((u64)a)*1024ULL*1024ULL*1024ULL)
#define kTB(a) ( ((u64)a)*1024ULL*1024ULL*1024ULL*1024ULL)
// time utils
typedef struct
{
int year;
int month;
int day;
int hour;
int sec;
int min;
} clock_date_t;
static clock_date_t clock_date(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
struct tm* t = localtime(&tv.tv_sec);
clock_date_t c;
c.year = 1900 + t->tm_year;
c.month = 1 + t->tm_mon;
c.day = t->tm_mday;
c.hour = t->tm_hour;
c.min = t->tm_min;
c.sec = t->tm_sec;
return c;
}
// 0 - Sunday
// 1 - Monday
// ...
// http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Implementation-dependent_methods_of_Sakamoto.2C_Lachman.2C_Keith_and_Craver
static inline int dayofweek(int d, int m, int y)
{
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
y -= m < 3;
return ( y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
}
// generates date in web format RFC1123
static inline void clock_rfc1123(u8* Str, clock_date_t c)
{
const char *DayStr[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
const char *MonthStr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
struct tm t;
t.tm_year = c.year - 1900;
t.tm_mon = c.month - 1;
t.tm_mday = c.day;
t.tm_hour = c.hour;
t.tm_min = c.min;
t.tm_sec = c.sec;
int wday = dayofweek(c.day, c.month, c.year);
const int RFC1123_TIME_LEN = 29;
strftime(Str, RFC1123_TIME_LEN+1, "---, %d --- %Y %H:%M:%S GMT", &t);
memcpy(Str, DayStr [wday], 3);
memcpy(Str+8, MonthStr[c.month - 1], 3);
}
static inline void clock_str(u8* Str, clock_date_t c)
{
sprintf(Str, "%04i%02i%02i_%02i-%02i-%02i", c.year, c.month, c.day, c.hour, c.min, c.sec);
}
static inline void ns_str(u8* Str, u64 NS)
{
u64 sec = NS % k1E9;
int msec = sec / 1000000ULL;
int usec = (sec - msec*1000000ULL)/ 1000ULL;
int nsec = (sec - msec*1000000ULL- usec*1000ULL);
sprintf(Str, "%03i.%03i.%03i", msec, usec, nsec);
}
// epoch nanos -> year, mont, day, ..
static clock_date_t ns2clock(u64 ts)
{
time_t t0 = ts / 1e9;
struct tm* t = localtime(&t0);
clock_date_t c;
c.year = 1900 + t->tm_year;
c.month = 1 + t->tm_mon;
c.day = t->tm_mday;
c.hour = t->tm_hour;
c.min = t->tm_min;
c.sec = t->tm_sec;
return c;
}
// verbose -> nanos since epoch
static u64 clock2ns(int year, int month, int day, int hour, int min, int sec)
{
struct tm t;
t.tm_year = year - 1900;
t.tm_mon = month-1;
t.tm_mday = day;
t.tm_hour = hour;
t.tm_min = min;
t.tm_sec = sec;
time_t epoch = mktime(&t);
return (u64)epoch * (u64)1e9;
}
static u64 clock_date2ns(clock_date_t d)
{
struct tm t;
t.tm_year = d.year - 1900;
t.tm_mon = d.month-1;
t.tm_mday = d.day;
t.tm_hour = d.hour;
t.tm_min = d.min;
t.tm_sec = d.sec;
time_t epoch = mktime(&t);
return (u64)epoch * (u64)1e9;
}
// returns the first day of the week
static clock_date_t clock_startofweek(clock_date_t d)
{
struct tm t;
int wday = dayofweek(d.day, d.month, d.year);
t.tm_year = d.year - 1900;
t.tm_mon = d.month-1;
t.tm_mday = d.day - wday;
t.tm_hour = d.hour;
t.tm_min = d.min;
t.tm_sec = d.sec;
mktime(&t);
clock_date_t r;
r.year = 1900 + t.tm_year;
r.month = 1 + t.tm_mon;
r.day = t.tm_mday;
r.hour = t.tm_hour;
r.min = t.tm_min;
r.sec = t.tm_sec;
return r;
}
// epoch in nanos
static u64 clock_ns(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (u64)tv.tv_sec *(u64)1e9 +(u64)tv.tv_usec * (u64)1e3;
}
static inline volatile u64 rdtsc(void)
{
u32 hi, lo;
__asm__ volatile("rdtsc" : "=a"(lo), "=d"(hi) );
return (((u64)hi)<<32ULL) | (u64)lo;
}
extern double TSC2Nano;
static inline volatile u64 rdtsc_ns(void)
{
u32 hi, lo;
__asm__ volatile("rdtsc" : "=a"(lo), "=d"(hi) );
u64 ts = (((u64)hi)<<32ULL) | (u64)lo;
return ts * TSC2Nano;
}
static inline volatile u64 rdtsc2ns(u64 ts)
{
return ts * TSC2Nano;
}
static inline volatile u64 tsc2ns(u64 ts)
{
return ts * TSC2Nano;
}
static inline u64 ns2tsc(u64 ns)
{
return (u64)( (double)ns / TSC2Nano);
}
static void ndelay(u64 ns)
{
u64 NextTS = rdtsc() + ns2tsc(ns);
while (rdtsc() < NextTS)
{
__asm__ volatile("pause");
__asm__ volatile("pause");
__asm__ volatile("pause");
__asm__ volatile("pause");
}
}
static inline void prefetchnta(void* ptr)
{
__asm__ volatile("prefetchnta (%0)" : : "r"(ptr));
}
static inline u32 swap32(const u32 a)
{
return (((a>>24)&0xFF)<<0) | (((a>>16)&0xFF)<<8) | (((a>>8)&0xFF)<<16) | (((a>>0)&0xFF)<<24);
}
static inline u16 swap16(const u16 a)
{
return (((a>>8)&0xFF)<<0) | (((a>>0)&0xFF)<<8);
}
static inline u64 swap64(const u64 a)
{
return swap32(a>>32ULL) | ( (u64)swap32(a) << 32ULL);
}
static inline u128 swap128(const u128 a)
{
u128_u u0;
u128_u u1;
u0.d128 = a;
u1.d128 = a;
u64 d0 = swap64(u0.d64[0]);
u64 d1 = swap64(u0.d64[1]);
u0.d64[0] = d1;
u0.d64[1] = d0;
return u0.d128;
}
static inline u32 min32(const u32 a, const u32 b)
{
return (a < b) ? a : b;
}
static inline s32 min32s(const s32 a, const s32 b)
{
return (a < b) ? a : b;
}
static inline u32 max32(const u32 a, const u32 b)
{
return (a > b) ? a : b;
}
static inline s32 max32s(const s32 a, const s32 b)
{
return (a > b) ? a : b;
}
static inline s32 sign32(const s32 a)
{
if (a == 0) return 0;
return (a > 0) ? 1 : -1;
}
static inline u64 min64(const u64 a, const u64 b)
{
return (a < b) ? a : b;
}
static inline u64 max64(const u64 a, const u64 b)
{
return (a > b) ? a : b;
}
static inline double maxf(const double a, const double b)
{
return (a > b) ? a : b;
}
static inline double minf(const double a, const double b)
{
return (a < b) ? a : b;
}
static inline double clampf(const double min, const double v, const double max)
{
return maxf(min, minf(v, max));
}
static inline double inverse(const double a)
{
if (a == 0) return 0;
return 1.0 / a;
}
static inline double fSqrt(const double a)
{
if (a <= 0) return 0;
return sqrtf(a);
}
static inline double signf(const double a)
{
if (a > 0) return 1.0;
if (a < 0) return -1.0;
// keep it simple..
return 1;
}
static inline double alog(const double a)
{
if (a == 0) return 0;
if (a < 0) return -logf(-a);
return -logf(a);
}
static inline char* FormatTS(u64 ts)
{
u64 usec = ts / 1000ULL;
u64 msec = usec / 1000ULL;
u64 sec = msec / 1000ULL;
u64 min = sec / 60ULL;
u64 hour = min / 60ULL;
u64 nsec = ts - usec*1000ULL;
usec = usec - msec*1000ULL;
msec = msec - sec*1000ULL;
sec = sec - min*60ULL;
min = min - hour*60ULL;
static char List[16][128];
static int Pos = 0;
char* S = List[Pos];
Pos = (Pos + 1) & 0xf;
sprintf(S, "%02lli:%02lli:%02lli.%03lli.%03lli.%03lli", hour % 24, min, sec, msec,usec, nsec);
return S;
}
static inline void CycleCalibration(void)
{
fprintf(stderr, "calibrating...\n");
u64 StartTS[16];
u64 EndTS[16];
u64 CyclesSum = 0;
u64 CyclesSum2 = 0;
u64 CyclesCnt = 0;
for (int i=0; i < 1; i++)
{
u64 NextTS = clock_ns() + 1e9;
u64 StartTS = rdtsc();
while (clock_ns() < NextTS)
{
}
u64 EndTS = rdtsc();
u64 Cycles = EndTS - StartTS;
CyclesSum += Cycles;
CyclesSum2 += Cycles*Cycles;
CyclesCnt++;
fprintf(stderr, "%i : %016llx %16.4f cycles/nsec\n", i, Cycles, Cycles / 1e9);
}
double CyclesSec = CyclesSum / CyclesCnt;
double CyclesStd = sqrt(CyclesCnt *CyclesSum2 - CyclesSum *CyclesSum) / CyclesCnt;
fprintf(stderr, "Cycles/Sec %12.4f Std:%8.fcycle std(%12.8f)\n", CyclesSec, CyclesStd, CyclesStd / CyclesSec);
// set global
TSC2Nano = 1e9 / CyclesSec;
}
// convert pcap style sec : nsec format into pure nano
static inline u64 nsec2ts(u32 sec, u32 nsec)
{
return (u64)sec * 1000000000ULL + (u64)nsec;
}
// ethernet header
typedef struct
{
u8 Dst[6];
u8 Src[6];
u16 Proto;
} fEther_t;
#define ETHER_PROTO_IPV4 0x0800
#define ETHER_PROTO_MPLS 0x8847
typedef struct
{
u32 TTL : 8;
u32 BOS : 1;
u32 TC : 3;
u32 Label : 20;
} __attribute__((packed)) MPLS_t;
typedef struct
{
union
{
u32 IP4;
u8 IP[4];
};
} IP4_t;
typedef struct
{
u8 Version;
u8 Service;
u16 Len;
u16 Ident;
u16 Frag;
u8 TTL;
u8 Proto;
u16 CSum;
IP4_t Src;
IP4_t Dst;
} __attribute__((packed)) IP4Header_t;
#define IPv4_PROTO_TCP 6
#define IPv4_PROTO_UDP 17
typedef struct
{
u16 PortSrc;
u16 PortDst;
u32 SeqNo;
u32 AckNo;
u16 Flags;
u16 Window;
u16 CSUM;
u16 Urgent;
} __attribute__((packed)) TCPHeader_t;
typedef struct
{
u16 PortSrc;
u16 PortDst;
u16 Length;
u16 CSUM;
} __attribute__((packed)) UDPHeader_t;
// pcap headers
#define PCAPHEADER_MAGIC_NANO 0xa1b23c4d
#define PCAPHEADER_MAGIC_USEC 0xa1b2c3d4
#define PCAPHEADER_MAJOR 2
#define PCAPHEADER_MINOR 4
#define PCAPHEADER_LINK_ETHERNET 1
typedef struct
{
u32 Sec; // time stamp sec since epoch
u32 NSec; // nsec fraction since epoch
u32 LengthCapture; // captured length, inc trailing / aligned data
u32 Length; // length on the wire
} __attribute__((packed)) PCAPPacket_t;
// per file header
typedef struct
{
u32 Magic;
u16 Major;
u16 Minor;
u32 TimeZone;
u32 SigFlag;
u32 SnapLen;
u32 Link;
} __attribute__((packed)) PCAPHeader_t;
#endif