forked from wiedehopf/readsb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
1121 lines (955 loc) · 34.7 KB
/
util.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
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Part of readsb, a Mode-S/ADSB/TIS message decoder.
//
// util.c: misc utilities
//
// Copyright (c) 2019 Michael Wolf <michael@mictronics.de>
//
// This code is based on a detached fork of dump1090-fa.
//
// Copyright (c) 2015 Oliver Jowett <oliver@mutability.co.uk>
//
// This file is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This file is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// This file incorporates work covered by the following copyright and
// license:
//
// Copyright (C) 2012 by Salvatore Sanfilippo <antirez@gmail.com>
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "readsb.h"
int64_t mstime(void) {
if (Modes.synthetic_now)
return Modes.synthetic_now;
struct timeval tv;
int64_t mst;
gettimeofday(&tv, NULL);
mst = ((int64_t) tv.tv_sec)*1000;
mst += tv.tv_usec / 1000;
return mst;
}
int64_t microtime(void) {
if (Modes.synthetic_now)
return 1000 * Modes.synthetic_now;
struct timeval tv;
int64_t mst;
gettimeofday(&tv, NULL);
mst = ((int64_t) tv.tv_sec) * 1000LL * 1000LL;
mst += tv.tv_usec;
return mst;
}
void milli_micro_seconds(int64_t *milli, int64_t *micro) {
if (Modes.synthetic_now) {
*milli = Modes.synthetic_now;
*micro = 1000 * Modes.synthetic_now;
return;
}
struct timeval tv;
gettimeofday(&tv, NULL);
*milli = ((int64_t) tv.tv_sec) * 1000 + ((int64_t) tv.tv_usec) / 1000;
*micro = ((int64_t) tv.tv_sec) * (1000 * 1000) + ((int64_t) tv.tv_usec);
}
int64_t mono_micro_seconds() {
if (Modes.synthetic_now) {
return 1000 * Modes.synthetic_now;
}
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
int64_t micro = ((int64_t) ts.tv_sec) * (1000 * 1000) + ((int64_t) ts.tv_nsec) / 1000;
return micro;
}
int64_t mono_milli_seconds() {
if (Modes.synthetic_now) {
return Modes.synthetic_now;
}
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
int64_t milli = ((int64_t) ts.tv_sec) * 1000 + ((int64_t) ts.tv_nsec) / (1000 * 1000);
return milli;
}
int snprintHMS(char *buf, size_t bufsize, int64_t now) {
time_t nowTime = nearbyint(now / 1000.0);
struct tm local;
localtime_r(&nowTime, &local);
char timebuf[128];
strftime(timebuf, 128, "%T", &local);
return snprintf(buf, bufsize, "%s.%03d", timebuf, (int) (now % 1000));
}
int64_t msThreadTime(void) {
struct timespec ts;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
return ((int64_t) ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000));
}
int64_t nsThreadTime(void) {
struct timespec ts;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts);
return ((int64_t) ts.tv_sec * (1000LL * 1000LL * 1000LL) + ts.tv_nsec);
}
int64_t receiveclock_ns_elapsed(int64_t t1, int64_t t2) {
return (t2 - t1) * 1000U / 12U;
}
int64_t receiveclock_ms_elapsed(int64_t t1, int64_t t2) {
return (t2 - t1) / 12000U;
}
/* record current CPU time in start_time */
void start_cpu_timing(struct timespec *start_time) {
clock_gettime(CLOCK_THREAD_CPUTIME_ID, start_time);
}
/* add difference between start_time and the current CPU time to add_to */
void end_cpu_timing(const struct timespec *start_time, struct timespec *add_to) {
struct timespec end_time;
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &end_time);
add_to->tv_sec += end_time.tv_sec - start_time->tv_sec;
add_to->tv_nsec += end_time.tv_nsec - start_time->tv_nsec;
normalize_timespec(add_to);
}
void timespec_add_elapsed(const struct timespec *start_time, const struct timespec *end_time, struct timespec *add_to) {
add_to->tv_sec += end_time->tv_sec - start_time->tv_sec;
add_to->tv_nsec += end_time->tv_nsec - start_time->tv_nsec;
normalize_timespec(add_to);
}
void start_monotonic_timing(struct timespec *start_time) {
clock_gettime(CLOCK_MONOTONIC, start_time);
}
void end_monotonic_timing(const struct timespec *start_time, struct timespec *add_to) {
struct timespec end_time;
clock_gettime(CLOCK_MONOTONIC, &end_time);
add_to->tv_sec += end_time.tv_sec - start_time->tv_sec;
add_to->tv_nsec += end_time.tv_nsec - start_time->tv_nsec;
normalize_timespec(add_to);
}
/* record current monotonic time in start_time */
void startWatch(struct timespec *start_time) {
clock_gettime(CLOCK_MONOTONIC, start_time);
}
// return elapsed time
int64_t stopWatch(struct timespec *start_time) {
struct timespec end_time;
clock_gettime(CLOCK_MONOTONIC, &end_time);
int64_t res = ((int64_t) end_time.tv_sec * 1000UL + end_time.tv_nsec / 1000000UL)
- ((int64_t) start_time->tv_sec * 1000UL + start_time->tv_nsec / 1000000UL);
return res;
}
// return elapsed time and set start_time to current time
int64_t lapWatch(struct timespec *start_time) {
struct timespec end_time;
clock_gettime(CLOCK_MONOTONIC, &end_time);
int64_t res = ((int64_t) end_time.tv_sec * 1000UL + end_time.tv_nsec / 1000000UL)
- ((int64_t) start_time->tv_sec * 1000UL + start_time->tv_nsec / 1000000UL);
if (start_time->tv_sec == 0 && start_time->tv_nsec == 0) {
res = 0;
}
*start_time = end_time;
return res;
}
// this is not cryptographic but much better than mstime() as a seed
unsigned int get_seed() {
struct timespec time;
clock_gettime(CLOCK_REALTIME, &time);
unsigned int seed = (uint64_t) time.tv_sec ^ (uint64_t) time.tv_nsec ^ (((uint64_t) getpid()) << 16) ^ (((uint64_t) (uintptr_t) pthread_self()) << 10);
return seed;
}
// increment target by increment in ms, if result is in the past, set target to now.
// specialized function for scheduling threads using pthreadcondtimedwait
static void incTimedwait(struct timespec *target, int64_t increment) {
struct timespec inc = msToTimespec(increment);
target->tv_sec += inc.tv_sec;
target->tv_nsec += inc.tv_nsec;
normalize_timespec(target);
struct timespec now;
clock_gettime(CLOCK_REALTIME, &now);
int64_t min_sleep = 50 * 1000; // always wait a bit (50 us) to yield (i hope)
if (target->tv_sec < now.tv_sec || (target->tv_sec == now.tv_sec && target->tv_nsec <= now.tv_nsec + min_sleep)) {
target->tv_sec = now.tv_sec;
target->tv_nsec = now.tv_nsec + min_sleep;
normalize_timespec(target);
}
}
#define uThreadMax (32)
static threadT *uThreads[uThreadMax];
static int uThreadCount = 0;
void threadInit(threadT *thread, char *name) {
if (uThreadCount >= uThreadMax) {
fprintf(stderr, "util.c: increase uThreadmax!\n");
exit(1);
}
if (uThreadCount == 0) {
memset(uThreads, 0, sizeof (uThreads));
}
memset(thread, 0, sizeof (threadT));
pthread_mutex_init(&thread->mutex, NULL);
pthread_cond_init(&thread->cond, NULL);
thread->name = strdup(name);
uThreads[uThreadCount++] = thread;
thread->joined = 1;
}
void threadCreate(threadT *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg) {
if (!thread->joined) {
fprintf(stderr, "<3>FATAL: threadCreate() thread %s failed: already running?\n", thread->name);
setExit(2);
}
int res = pthread_create(&thread->pthread, attr, start_routine, arg);
if (res != 0) {
fprintf(stderr, "<3>FATAL: threadCreate() pthread_create() failed: %s\n", strerror(res));
setExit(2);
}
thread->joined = 0;
thread->joinFailed = 0;
}
static void threadDestroy(threadT *thread) {
// if the join didn't work, don't clean up
if (!thread->joined || thread->joinFailed) {
fprintf(stderr, "<3>FATAL: thread %s could not be joined, calling abort()!\n", thread->name);
abort();
}
pthread_mutex_destroy(&thread->mutex);
pthread_cond_destroy(&thread->cond);
sfree(thread->name);
}
void threadDestroyAll() {
for (int i = 0; i < uThreadCount; i++) {
threadDestroy(uThreads[i]);
}
uThreadCount = 0;
}
void threadTimedWait(threadT *thread, struct timespec *ts, int64_t increment) {
// don't wait when we want to exit
if (Modes.exit)
return;
incTimedwait(ts, increment);
int err = pthread_cond_timedwait(&thread->cond, &thread->mutex, ts);
if (err && err != ETIMEDOUT)
fprintf(stderr, "%s thread: pthread_cond_timedwait unexpected error: %s\n", thread->name, strerror(err));
}
void threadSignalJoin(threadT *thread) {
if (thread->joined)
return;
int64_t timeout = Modes.joinTimeout;
int err = 0;
while ((err = pthread_tryjoin_np(thread->pthread, NULL)) && timeout-- > 0) {
pthread_cond_signal(&thread->cond);
msleep(1);
}
if (err == 0) {
thread->joined = 1;
} else {
thread->joinFailed = 1;
fprintf(stderr, "%s thread: threadSignalJoin timed out after %.1f seconds, undefined behaviour may result!\n", thread->name, (float) Modes.joinTimeout / (float) SECONDS);
Modes.joinTimeout /= 2;
Modes.joinTimeout = imax(Modes.joinTimeout, 2 * SECONDS);
}
}
int threadAffinity(int core_id) {
int num_cores = Modes.num_procs;
if (core_id < 0 || core_id >= num_cores)
return EINVAL;
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(core_id, &cpuset);
return sched_setaffinity(0, sizeof(cpu_set_t), &cpuset);
}
struct char_buffer readWholeFile(int fd, char *errorContext) {
struct char_buffer cb = {0};
struct stat fileinfo = {0};
if (fstat(fd, &fileinfo)) {
fprintf(stderr, "%s: readWholeFile: fstat failed, wat?!\n", errorContext);
return cb;
}
size_t fsize = fileinfo.st_size;
int extra = 128 * 1024;
cb.buffer = cmalloc(fsize + extra);
memset(cb.buffer, 0x0, fsize + extra); // zero entire buffer
if (!cb.buffer) {
fprintf(stderr, "%s: readWholeFile couldn't allocate buffer!\n", errorContext);
return cb;
}
int64_t res = 0;
int toRead = fsize;
cb.len = 0;
while (toRead >= 0) {
res = read(fd, cb.buffer + cb.len, toRead);
if (res <= 0) {
if (errno == EINTR) {
continue;
}
break;
}
cb.len += res;
toRead -= res;
}
if (fstat(fd, &fileinfo)) {
fprintf(stderr, "%s: readWholeFile: fstat failed, wat?!\n", errorContext);
sfree(cb.buffer);
cb.len = 0;
}
if (toRead < 0 || res < 0 || cb.len != fsize || (size_t) fileinfo.st_size != fsize) {
fprintf(stderr, "%s: readWholeFile size mismatch! toRead %ld res %ld %s cb.len %ld fsize %ld fileinfo.st_size %ld\n",
errorContext, (long) toRead, (long) res, strerror(res), (long) cb.len, (long) fsize, (long) fileinfo.st_size);
sfree(cb.buffer);
cb.len = 0;
}
return cb;
}
struct char_buffer readWholeGz(gzFile gzfp, char *errorContext) {
struct char_buffer cb = {0};
if (gzbuffer(gzfp, GZBUFFER_BIG) < 0) {
fprintf(stderr, "reading %s: gzbuffer fail!\n", errorContext);
return cb;
}
int alloc = 8 * 1024 * 1024;
cb.buffer = cmalloc(alloc);
if (!cb.buffer) {
fprintf(stderr, "reading %s: readWholeGz alloc fail!\n", errorContext);
return cb;
}
int res;
int toRead = alloc;
while (true) {
res = gzread(gzfp, cb.buffer + cb.len, toRead);
if (res <= 0)
break;
cb.len += res;
toRead -= res;
if (toRead == 0) {
toRead = alloc;
alloc += toRead;
char *oldBuffer = cb.buffer;
cb.buffer = realloc(cb.buffer, alloc);
if (!cb.buffer) {
sfree(oldBuffer);
fprintf(stderr, "reading %s: readWholeGz alloc fail!\n", errorContext);
return (struct char_buffer) {0};
}
}
}
if (res < 0) {
sfree(cb.buffer);
int error;
fprintf(stderr, "readWholeGz: gzread failed: %s (res == %d)\n", gzerror(gzfp, &error), res);
if (error == Z_ERRNO)
perror(errorContext);
return (struct char_buffer) {0};
}
return cb;
}
// wrapper to write to an opened gzFile
int writeGz(gzFile gzfp, void *source, int toWrite, char *errorContext) {
int res, error;
int nwritten = 0;
char *p = source;
if (!gzfp) {
fprintf(stderr, "writeGz: gzfp was NULL .............\n");
return -1;
}
while (toWrite > 0) {
int len = toWrite;
//if (len > 8 * 1024 * 1024)
// len = 8 * 1024 * 1024;
res = gzwrite(gzfp, p, len);
if (res <= 0) {
fprintf(stderr, "gzwrite of length %d failed: %s (res == %d)\n", toWrite, gzerror(gzfp, &error), res);
if (error == Z_ERRNO)
perror(errorContext);
return -1;
}
p += res;
nwritten += res;
toWrite -= res;
}
return nwritten;
}
void printTimestamp(FILE *stream, int64_t time_ms) {
char timebuf[128];
char timebuf2[128];
time_t now;
struct tm local;
now = floor(time_ms / 1000.0);
localtime_r(&now, &local);
strftime(timebuf, 128, "%Y-%m-%d %T", &local);
timebuf[127] = 0;
strftime(timebuf2, 128, "%Z", &local);
timebuf2[127] = 0;
fprintf(stream, "[%s.%03d %s] ", timebuf, (int) (time_ms % 1000), timebuf2);
}
void log_with_timestamp(const char *format, ...) {
char msg[1024];
va_list ap;
va_start(ap, format);
vsnprintf(msg, 1024, format, ap);
va_end(ap);
msg[1023] = 0;
printTimestamp(stderr, mstime());
fprintf(stderr, "%s\n", msg);
}
int64_t roundSeconds(int interval, int offset, int64_t epoch_ms) {
if (offset >= interval)
fprintf(stderr, "roundSeconds was used wrong, interval must be greater than offset\n");
time_t epoch = epoch_ms / SECONDS + (epoch_ms % SECONDS >= SECONDS / 2);
struct tm utc;
gmtime_r(&epoch, &utc);
int sec = utc.tm_sec;
int step = nearbyint((sec - offset) / (float) interval);
int calc = offset + step * interval;
//fprintf(stderr, "%d %d\n", sec, calc);
return (epoch + (calc - sec)) * SECONDS;
}
ssize_t check_write(int fd, const void *buf, size_t count, const char *error_context) {
ssize_t res = write(fd, buf, count);
if (res < 0)
perror(error_context);
else if (res != (ssize_t) count)
fprintf(stderr, "%s: Only %zd of %zd bytes written!\n", error_context, res, count);
return res;
}
int my_epoll_create(int *event_fd_ptr) {
int fd = epoll_create(32); // argument positive, ignored
if (fd == -1) {
perror("FATAL: epoll_create() failed:");
exit(1);
}
// add exit signaling eventfd, we want that for all our epoll fds
struct epoll_event epollEvent = { .events = EPOLLIN, .data = { .ptr = event_fd_ptr }};
if (epoll_ctl(fd, EPOLL_CTL_ADD, *event_fd_ptr, &epollEvent)) {
perror("epoll_ctl fail:");
exit(1);
}
return fd;
}
void epollAllocEvents(struct epoll_event **events, int *maxEvents) {
if (!*events) {
*maxEvents = 32;
} else if (*maxEvents > 9000) {
return;
} else {
*maxEvents *= 2;
}
sfree(*events);
*events = cmalloc(*maxEvents * sizeof(struct epoll_event));
if (!*events) {
fprintf(stderr, "Fatal: epollAllocEvents malloc\n");
exit(1);
}
}
char *sprint_uuid1_partial(uint64_t id1, char *p) {
for (int i = 7; i >= 0; i--) {
//int j = 7 - i;
//if (j == 4)
//*p++ = '-';
uint64_t val = (id1 >> (4 * i)) & 15;
if (val > 9)
*p++ = val - 10 + 'a';
else
*p++ = val + '0';
}
*p = '\0';
return p;
}
char *sprint_uuid1(uint64_t id1, char *p) {
for (int i = 15; i >= 0; i--) {
int j = 15 - i;
if (j == 8 || j == 12)
*p++ = '-';
uint64_t val = (id1 >> (4 * i)) & 15;
if (val > 9)
*p++ = val - 10 + 'a';
else
*p++ = val + '0';
}
*p = '\0';
return p;
}
char *sprint_uuid2(uint64_t id2, char *p) {
for (int i = 15; i >= 0; i--) {
int j = 15 - i;
if (j == 0 || j == 4)
*p++ = '-';
uint64_t val = (id2 >> (4 * i)) & 15;
if (val > 9)
*p++ = val - 10 + 'a';
else
*p++ = val + '0';
}
*p = '\0';
return p;
}
char *sprint_uuid(uint64_t id1, uint64_t id2, char *p) {
p = sprint_uuid1(id1, p);
p = sprint_uuid2(id2, p);
*p = '\0';
return p;
}
int mkdir_error(const char *path, mode_t mode, FILE *err_stream) {
int err = mkdir(path, mode);
if (err != 0 && errno != EEXIST && err_stream) {
fprintf(err_stream, "mkdir: %s (%s)\n", strerror(errno), path);
}
return err;
}
// Distance between points on a spherical earth.
// This has up to 0.5% error because the earth isn't actually spherical
// (but we don't use it in situations where that matters)
// define for testing some approximations:
#define DEGR (0.017453292519943295) // 1 degree in radian
double greatcircle(double lat0, double lon0, double lat1, double lon1, int approx) {
if (lat0 == lat1 && lon0 == lon1) {
return 0;
}
// toRad converts degrees to radians
lat0 = toRad(lat0);
lon0 = toRad(lon0);
lat1 = toRad(lat1);
lon1 = toRad(lon1);
double dlat = fabs(lat1 - lat0);
double dlon = fabs(lon1 - lon0);
double hav = 0;
if (CHECK_APPROXIMATIONS) {
double a = sin(dlat / 2) * sin(dlat / 2) + cos(lat0) * cos(lat1) * sin(dlon / 2) * sin(dlon / 2);
hav = 6371e3 * 2 * atan2(sqrt(a), sqrt(1.0 - a));
}
// after checking this isn't necessary with doubles
// anyhow for small distance we can do a much cheaper approximation:
// anyhow, nice formular let's leave it in the code for reference
// for small distances the earth is flat enough that we can use this approximation
// don't use this approximation near the poles, would probably behave poorly
//
// in our particular case many calls of this function are by speed_check which usually is small distances
// thus having less trigonometric functions used should be a performance gain
//
// difference to haversine is less than 0.04 percent for up to 3 degrees of lat/lon difference
// this isn't an issue for us and due to the oblateness and this calculation taking it into account, this calculation might actually be more accurate for small distances but i can't be bothered to check.
//
if (approx || (dlat < 3 * DEGR && dlon < 3 * DEGR && fabs(lat1) < 80 * DEGR)) {
// calculate the equivalent length of the latitude and longitude difference
// use pythagoras to get the distance
// Equatorial radius: e = (6378.1370 km) -> circumference: 2 * pi * e = 40 075.016 km
// Polar radius: p = (6356.7523 km) -> quarter meridian from wiki: 10 001.965 km
// float ec = 40075016; // equatorial circumerence
// float mc = 4 * 10001965; // meridial circumference
// to have consistency to other calculations, use a circular earth
float ec = 2 * M_PI * 6371e3; // equatorial circumference
float mc = 2 * M_PI * 6371e3; // meridial circumference
float avglat = lat0 + (lat1 - lat0) / 2;
float dmer = (float) dlat / (2 * (float) M_PI) * mc;
float dequ = (float) dlon / (2 * (float) M_PI) * ec * cosf(avglat);
float pyth = sqrtf(dmer * dmer + dequ * dequ);
if (!approx && CHECK_APPROXIMATIONS) {
double errorPercent = fabs(hav - pyth) / hav * 100;
if (errorPercent > 0.03) {
fprintf(stderr, "pos: %.1f, %.1f dlat: %.5f dlon %.5f hav: %.1f errorPercent: %.3f\n", toDeg(lat0), toDeg(lon0), toDeg(dlat), toDeg(dlon), hav, errorPercent);
}
}
return pyth;
}
// spherical law of cosines
// use float calculations if latitudes differ sufficiently
if (dlat > 1 * DEGR && dlon > 1 * DEGR) {
// error
double slocf = 6371e3f * acosf(sinf(lat0) * sinf(lat1) + cosf(lat0) * cosf(lat1) * cosf(dlon));
if (CHECK_APPROXIMATIONS) {
double errorPercent = fabs(hav - slocf) / hav * 100;
if (errorPercent > 0.025) {
fprintf(stderr, "pos: %.1f, %.1f dlat: %.5f dlon %.5f hav: %.1f errorPercent: %.3f\n", toDeg(lat0), toDeg(lon0), toDeg(dlat), toDeg(dlon), hav, errorPercent);
}
}
return slocf;
}
double sloc = 6371e3 * acos(sin(lat0) * sin(lat1) + cos(lat0) * cos(lat1) * cos(dlon));
if (CHECK_APPROXIMATIONS) {
double errorPercent = fabs(hav - sloc) / hav * 100;
if (errorPercent > 0.025) {
fprintf(stderr, "pos: %.1f, %.1f dlat: %.5f dlon %.5f sloc: %.1f errorPercent: %.3f\n", toDeg(lat0), toDeg(lon0), toDeg(dlat), toDeg(dlon), sloc, errorPercent);
}
}
return sloc;
}
double bearing(double lat0, double lon0, double lat1, double lon1) {
lat0 = toRad(lat0);
lon0 = toRad(lon0);
lat1 = toRad(lat1);
lon1 = toRad(lon1);
// using float variants except for sin close to zero
float y = sinf(lon1-lon0)*cosf(lat1);
float x = cosf(lat0)*sinf(lat1) - sinf(lat0)*cosf(lat1)*cosf(lon1-lon0);
float res = atan2f(y, x) * (180 / (float) M_PI) + 360;
if (CHECK_APPROXIMATIONS) {
// check against using double trigonometric functions
// errors greater than 0.5 are rare and only happen for small distances
// bearings derived from small distances don't need to be accurate at all for our purposes
double y = sin(lon1-lon0)*cos(lat1);
double x = cos(lat0)*sin(lat1) - sin(lat0)*cos(lat1)*cos(lon1-lon0);
double res2 = (atan2(y, x) * (180 / M_PI) + 360);
double diff = fabs(res2 - res);
double dist = greatcircle(toDeg(lat0), toDeg(lon0), toDeg(lat1), toDeg(lon1), 1);
if ((diff > 0.2 && dist > 150) || diff > 2) {
fprintf(stderr, "errorDeg: %.2f %.2f %.2f dist: %.2f km\n",
diff, res, res2, dist / 1000.0);
}
}
while (res > 360)
res -= 360;
return res;
}
#undef DEGR
// allocate a group of task_info
task_group_t *allocate_task_group(uint32_t count) {
task_group_t *group = cmalloc(sizeof(task_group_t));
group->task_count = count;
group->infos = cmalloc(count * sizeof(task_info_t));
memset(group->infos, 0x0, count * sizeof(task_info_t));
/*
for (uint32_t k = 0; k < count; k++) {
task_info_t *info = &group->infos[k];
info->buffer_count = buffer_count;
info->buffers = cmalloc(buffer_count * sizeof(buffer_t));
memset(info->buffers, 0x0, buffer_count * sizeof(buffer_t));
}
*/
group->tasks = cmalloc(count * sizeof(threadpool_task_t));
memset(group->tasks, 0x0, count * sizeof(threadpool_task_t));
return group;
}
// destroy a group of task_info
void destroy_task_group(task_group_t *group) {
/*
for (uint32_t k = 0; k < group->task_count; k++) {
task_info_t *info = &group->infos[k];
for (uint32_t j = 0; j < info->buffer_count; j++) {
free(info->buffers[j].buf);
}
free(info->buffers);
}
*/
free(group->infos);
free(group->tasks);
memset(group, 0x0, sizeof(task_group_t));
free(group);
}
void threadpool_distribute_and_run(threadpool_t *pool, task_group_t *task_group, threadpool_function_t func, int totalRange, int taskCount, int64_t now) {
if (taskCount == 0 || taskCount > (int) task_group->task_count) {
taskCount = task_group->task_count;
}
threadpool_task_t *tasks = task_group->tasks;
task_info_t *infos = task_group->infos;
int section_len = totalRange / taskCount;
int extra = totalRange % taskCount;
int p = 0;
int actualTaskCount = 0;
// assign tasks
for (int i = 0; i < taskCount; i++) {
threadpool_task_t *task = &tasks[i];
task_info_t *range = &infos[i];
range->now = now;
range->from = p;
p += section_len;
if (extra) {
p++;
extra--;
}
range->to = p;
if (range->from == range->to) {
break;
}
task->function = func;
task->argument = range;
actualTaskCount++;
//fprintf(stderr, "%d %d\n", range->from, range->to);
}
if (p != totalRange) {
fprintf(stderr, "threadpool_distribute_and_run: range distribution error: p: %d totalRange: %d\n", p, totalRange);
}
threadpool_run(pool, tasks, actualTaskCount);
}
void gzipFile(char *filename) {
int fd;
char fileGz[PATH_MAX];
gzFile gzfp;
// read uncompressed file into buffer
fd = open(filename, O_RDONLY);
if (fd < 0) {
return;
}
struct char_buffer cb = readWholeFile(fd, filename);
close(fd);
if (!cb.buffer) {
fprintf(stderr, "gzipFile readWholeFile failed: %s\n", filename);
return;
}
snprintf(fileGz, PATH_MAX, "%s.gz", filename);
gzfp = gzopen(fileGz, "wb");
if (!gzfp) {
fprintf(stderr, "gzopen failed:");
perror(fileGz);
return;
}
int res = gzsetparams(gzfp, 9, Z_DEFAULT_STRATEGY);
if (res < 0) {
fprintf(stderr, "gzsetparams fail: %d", res);
}
if (cb.len > 0) {
writeGz(gzfp, cb.buffer, cb.len, fileGz);
}
sfree(cb.buffer);
cb.len = 0;
if (gzclose(gzfp) != Z_OK) {
fprintf(stderr, "compressACAS gzclose failed: %s\n", fileGz);
unlink(fileGz);
return;
}
}
void check_grow_buffer_t(buffer_t *buffer, ssize_t newSize) {
if (buffer->bufSize < newSize) {
sfree(buffer->buf);
buffer->buf = cmalloc(newSize);
}
}
void *check_grow_threadpool_buffer_t(threadpool_buffer_t *buffer, ssize_t newSize) {
if (buffer->size < newSize || !buffer->buf) {
//fprintf(stderr, "check_grow_threadpool_buffer: buffer->size %ld requested size %ld\n", (long) buffer->size, (long) newSize);
sfree(buffer->buf);
newSize = newSize * 9 / 8; // avoid super many mallocs when the size of something grows slowly
buffer->buf = cmalloc(newSize);
if (!buffer->buf) {
fprintf(stderr, "<3>FATAL: check_grow_threadpool_buffer_t no enough memory allocating %ld bytes!\n", (long) newSize);
abort();
}
buffer->size = newSize;
}
return buffer->buf;
}
struct char_buffer generateZstd(ZSTD_CCtx* cctx, threadpool_buffer_t *pbuffer, struct char_buffer src, int level) {
struct char_buffer cb;
check_grow_threadpool_buffer_t(pbuffer, ZSTD_compressBound(src.len));
//fprintf(stderr, "pbuffer->size: %ld src.len %ld\n", (long) pbuffer->size, (long) src.len);
/*
* size_t ZSTD_compressCCtx(ZSTD_CCtx* cctx,
void* dst, size_t dstCapacity,
const void* src, size_t srcSize,
int compressionLevel);
*/
size_t compressedSize = ZSTD_compressCCtx(cctx,
pbuffer->buf, pbuffer->size,
src.buffer, src.len,
level);
if (ZSTD_isError(compressedSize)) {
fprintf(stderr, "generateZstd() zstd error: %s\n", ZSTD_getErrorName(compressedSize));
cb.buffer = NULL;
cb.len = 0;
return cb;
}
cb.len = compressedSize;
cb.buffer = pbuffer->buf;
return cb;
}
struct char_buffer ident(struct char_buffer target) {
return target;
}
void setLowestPriorityPthread() {
int policy;
struct sched_param param = { 0 };
pthread_setschedparam(pthread_self(), SCHED_IDLE, ¶m);
return;
pthread_getschedparam(pthread_self(), &policy, ¶m);
fprintf(stderr, "priority before: %d\n", (int) param.sched_priority);
policy=SCHED_FIFO;
int priority_max = sched_get_priority_max(policy);
int priority_min = sched_get_priority_min(policy);
fprintf(stderr, "min prio: %d max prio: %d\n", priority_min, priority_max);
param.sched_priority = priority_min;
pthread_setschedparam(pthread_self(), policy, ¶m);
pthread_getschedparam(pthread_self(), &policy, ¶m);
fprintf(stderr, "priority after: %d\n", (int) param.sched_priority);
}
void setPriorityPthread() {
int policy = SCHED_FIFO;
struct sched_param param = { 0 };
param.sched_priority = sched_get_priority_min(policy);
pthread_setschedparam(pthread_self(), policy, ¶m);
}
zstd_fw_t *createZstdFw(size_t inBufSize) {
zstd_fw_t *fw = cmalloc(sizeof(zstd_fw_t));
memset(fw, 0x0, sizeof(zstd_fw_t));
fw->in.src = cmalloc(inBufSize);
fw->inAlloc = inBufSize;
fw->in.size = 0;
fw->in.pos = 0;
int outBufSize = ZSTD_compressBound(inBufSize);
fw->out.dst = cmalloc(outBufSize);
fw->out.size = outBufSize;
fw->out.pos = 0;
//fw->cctx = ZSTD_createCCtx();
fw->cstream = ZSTD_createCStream();
fw->fd = -1;
return fw;
}
void destroyZstdFw(zstd_fw_t *fw) {
//ZSTD_freeCCtx(fw->cctx);
ZSTD_freeCStream(fw->cstream);
free((void *) fw->in.src);
free((void *) fw->out.dst);
free(fw);
}
static size_t zstdFwAvailable(zstd_fw_t *fw) {
return fw->inAlloc - fw->in.size;
}
static void zstdFwWrite(zstd_fw_t *fw) {
if (fw->fd < 0) {
return;
}
check_write(fw->fd, fw->out.dst, fw->out.pos, fw->outFile);
fw->out.pos = 0;
}
static void zstdFwCompress(zstd_fw_t *fw) {
if (fw->in.size == 0) {
return;
}
if (fw->fd < 0) {
return;
}
size_t res;
// fw->in buffer is full, let's compress it
//res = ZSTD_compressStream2(fw->cctx, &fw->out, &fw->in, ZSTD_e_flush);
res = ZSTD_compressStream(fw->cstream, &fw->out, &fw->in);
if (ZSTD_isError(res)) {
fprintf(stderr, "ZSTD_compressStream failed: %ld %s\n", (long) res, ZSTD_getErrorName(res));
}
/*
res = ZSTD_flushStream(fw->cstream, &fw->out);
if (ZSTD_isError(res)) {
fprintf(stderr, "ZSTD_flushStream failed: %s\n", ZSTD_getErrorName(res));
}
*/
if (fw->in.size != fw->in.pos) {
fprintf(stderr, "<3>BAD: ohB6ooVi %ld %ld %ld\n", (long) fw->in.size, (long) fw->in.pos, (long) res);
}
fw->in.size = 0;
fw->in.pos = 0;
zstdFwWrite(fw);