This repository has been archived by the owner on Sep 22, 2022. It is now read-only.
/
mera.c
1631 lines (1467 loc) · 50.1 KB
/
mera.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
/*
* Copyright (c) 2016-2018 Positive Technologies, https://www.ptsecurity.com,
* Fast Positive Hash.
*
* Portions Copyright (c) 2010-2018 Leonid Yuriev <leo@yuriev.ru>,
* The 1Hippeus project (t1h).
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgement in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#ifndef NOMINMAX
#define NOMINMAX
#endif
#ifndef _ISOC99_SOURCE
#define _ISOC99_SOURCE 1
#endif
#ifndef _ISOC11_SOURCE
#define _ISOC11_SOURCE 1
#endif
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS 1
#endif
#ifndef _THREAD_SAFE
#define _THREAD_SAFE 1
#endif
#ifndef _REENTRANT
#define _REENTRANT 1
#endif
#if defined(_MSC_VER)
#pragma warning(disable : 4711) /* function 'xyz' selected for \
automatic inline expansion */
#pragma warning(disable : 4127) /* conditional expression is constant */
#pragma warning(disable : 4702) /* unreachable code */
#if _MSC_VER < 1900
#define snprintf _snprintf
#pragma warning(disable : 4996) /* '_snprintf': This function or variable \
may be unsafe */
#endif
#if _MSC_VER > 1800
#pragma warning(disable : 4464) /* relative include path contains '..' */
#endif
#endif /* MSVC */
/* OS's includes for time/clock */
#if defined(__linux__) || defined(__gnu_linux__)
#include <linux/hw_breakpoint.h>
#include <linux/perf_event.h>
#include <sched.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#endif /* Linux */
#if defined(EMSCRIPTEN)
#include <emscripten.h>
#elif defined(__APPLE__) || defined(__MACH__)
#include <mach/mach_time.h>
#include <mach/thread_policy.h>
#endif
#if defined(_WIN64) || defined(_WIN32) || defined(__TOS_WIN__) || \
defined(__WINDOWS__)
#include <time.h>
#include <windows.h>
#include <winnt.h>
#else
#include <errno.h>
#include <fcntl.h>
#include <setjmp.h>
#include <setjmp.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#endif /* OS */
#include "mera.h"
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
/*****************************************************************************/
/* Compiler's includes for builtins/intrinsics */
#if defined(_MSC_VER) || defined(__INTEL_COMPILER)
#include <intrin.h>
#elif __GNUC_PREREQ(4, 4) || defined(__clang__)
#if defined(__ia32__) || defined(__e2k__)
#include <x86intrin.h>
#endif /* __ia32__ */
#if defined(__ia32__)
#include <cpuid.h>
#endif /* __ia32__ */
#elif defined(__SUNPRO_C) || defined(__sun) || defined(sun)
#include <mbarrier.h>
#elif (defined(_HPUX_SOURCE) || defined(__hpux) || defined(__HP_aCC)) && \
(defined(HP_IA64) || defined(__ia64))
#include <machine/sys/inline.h>
#elif defined(__IBMC__) && defined(__powerpc)
#include <atomic.h>
#elif defined(_AIX)
#include <builtins.h>
#include <sys/atomic_op.h>
#elif (defined(__osf__) && defined(__DECC)) || defined(__alpha)
#include <c_asm.h>
#include <machine/builtins.h>
#elif defined(__MWERKS__)
/* CodeWarrior - troubles ? */
#pragma gcc_extensions
#elif defined(__SNC__)
/* Sony PS3 - troubles ? */
#elif defined(__hppa__) || defined(__hppa)
#include <machine/inline.h>
#else
#error Unsupported C compiler, please use GNU C 4.4 or newer
#endif /* Compiler */
static __inline void compiler_barrier(void) {
#if defined(__clang__) || defined(__GNUC__)
__asm__ __volatile__("" ::: "memory");
#elif defined(_MSC_VER)
_ReadWriteBarrier();
#elif defined(__INTEL_COMPILER) /* LY: Intel Compiler may mimic GCC and MSC */
__memory_barrier();
if (type > MDBX_BARRIER_COMPILER)
#if defined(__ia64__) || defined(__ia64) || defined(_M_IA64)
__mf();
#elif defined(__i386__) || defined(__x86_64__)
_mm_mfence();
#else
#error "Unknown target for Intel Compiler, please report to us."
#endif
#elif defined(__SUNPRO_C) || defined(__sun) || defined(sun)
__compiler_barrier();
#elif (defined(_HPUX_SOURCE) || defined(__hpux) || defined(__HP_aCC)) && \
(defined(HP_IA64) || defined(__ia64))
_Asm_sched_fence(/* LY: no-arg meaning 'all expect ALU', e.g. 0x3D3D */);
#elif defined(_AIX) || defined(__ppc__) || defined(__powerpc__) || \
defined(__ppc64__) || defined(__powerpc64__)
__fence();
#else
#error "Could not guess the kind of compiler, please report to us."
#endif
}
#ifndef likely
#if defined(__GNUC__) || defined(__clang__)
#define likely(cond) __builtin_expect(!!(cond), 1)
#else
#define likely(x) (x)
#endif
#endif /* likely */
#ifndef unlikely
#if defined(__GNUC__) || defined(__clang__)
#define unlikely(cond) __builtin_expect(!!(cond), 0)
#else
#define unlikely(x) (x)
#endif
#endif /* unlikely */
/*****************************************************************************/
#if defined(_WIN64) || defined(_WIN32) || defined(__TOS_WIN__) || \
defined(__WINDOWS__)
static unsigned seh_filter(unsigned exception_code) {
switch (exception_code) {
case EXCEPTION_ILLEGAL_INSTRUCTION:
case EXCEPTION_PRIV_INSTRUCTION:
case EXCEPTION_ACCESS_VIOLATION:
return EXCEPTION_EXECUTE_HANDLER;
}
return EXCEPTION_CONTINUE_SEARCH;
}
#else
static sigjmp_buf sigaction_jump;
static void sigaction_handler(int signum, siginfo_t *info, void *context) {
(void)context;
(void)info;
siglongjmp(sigaction_jump, signum);
}
#endif
/* LY: dedicated function to avoid clobber args by ‘longjmp’ */
static int do_probe(unsigned (*start)(timestamp_t *),
unsigned (*finish)(timestamp_t *)) {
#if defined(_WIN64) || defined(_WIN32) || defined(__TOS_WIN__) || \
defined(__WINDOWS__)
__try {
#else
struct sigaction act, prev_sigsegv, prev_sigill, prev_sigbus;
memset(&act, 0, sizeof(act));
act.sa_sigaction = sigaction_handler;
if (sigaction(SIGSEGV, &act, &prev_sigsegv)) {
perror(MERA_PERROR_PREFIX "sigaction(SIGSEGV)");
return -1;
}
if (sigaction(SIGILL, &act, &prev_sigill)) {
perror(MERA_PERROR_PREFIX "sigaction(SIGILL)");
return -1;
}
if (sigaction(SIGBUS, &act, &prev_sigbus)) {
perror(MERA_PERROR_PREFIX "sigaction(SIGBUS)");
return -1;
}
if (sigsetjmp(sigaction_jump, 0) != 0) {
sigaction(SIGSEGV, &prev_sigsegv, NULL);
sigaction(SIGILL, &prev_sigill, NULL);
sigaction(SIGBUS, &prev_sigbus, NULL);
return -2;
}
#endif
for (unsigned n = 0; n < 42; ++n) {
timestamp_t timestamp_start, timestamp_finish;
unsigned coreid = start(×tamp_start);
#if defined(_WIN64) || defined(_WIN32) || defined(__TOS_WIN__) || \
defined(__WINDOWS__)
Sleep(1);
#else
usleep(42);
#endif
if (coreid != finish(×tamp_finish))
continue;
if (timestamp_finish > timestamp_start)
return 1;
if (timestamp_finish == timestamp_start || n > 5)
break;
}
#if defined(_WIN64) || defined(_WIN32) || defined(__TOS_WIN__) || \
defined(__WINDOWS__)
} __except (seh_filter(GetExceptionCode())) {
return -2;
}
#else
sigaction(SIGSEGV, &prev_sigsegv, NULL);
sigaction(SIGILL, &prev_sigill, NULL);
sigaction(SIGBUS, &prev_sigbus, NULL);
#endif
return 0;
}
static bool probe(unsigned (*start)(timestamp_t *),
unsigned (*finish)(timestamp_t *),
double (*convert)(timestamp_t), unsigned flags,
const char *source_name, const char *time_units) {
if (is_option_set(bench_verbose)) {
printf(" - probe for %s", source_name);
fflush(stdout);
}
flags |= timestamp_clock_have;
if (mera.flags >= flags) {
if (is_option_set(bench_verbose))
printf(": Skip (already have)\n");
return false;
}
int rc = do_probe(start, finish);
switch (rc) {
case 1:
if (is_option_set(bench_verbose))
printf(": Ok\n");
mera.start = start;
mera.finish = finish;
mera.source = source_name;
mera.convert = convert;
if (flags & timestamp_cycles)
mera.units = "cycle";
else if (flags & timestamp_ticks)
mera.units = "tick";
else
mera.units = time_units;
mera.flags = flags;
return true;
case 0:
if (is_option_set(bench_verbose))
printf(": Doesnt work\n");
break;
case -2:
if (is_option_set(bench_verbose))
printf(": Not available (SIGSEGV/SIGILL)\n");
break;
}
return false;
}
/*****************************************************************************/
static int set_single_affinity(void) {
#if defined(_WIN64) || defined(_WIN32) || defined(__TOS_WIN__) || \
defined(__WINDOWS__)
return -1;
#elif defined(__GLIBC__) || defined(__GNU_LIBRARY__) || defined(__ANDROID__)
const int current_cpu = sched_getcpu();
if (current_cpu < 0) {
perror(MERA_PERROR_PREFIX "sched_getcpu()");
return -1;
}
const int ncpu = sysconf(_SC_NPROCESSORS_CONF);
const unsigned cpuset_size = CPU_ALLOC_SIZE(ncpu);
cpu_set_t *affinity = CPU_ALLOC(ncpu);
if (!affinity) {
perror(MERA_PERROR_PREFIX "CPU_ALLOC()");
return -1;
}
CPU_ZERO_S(cpuset_size, affinity);
CPU_SET_S(current_cpu, cpuset_size, affinity);
if (sched_setaffinity(0, cpuset_size, affinity)) {
perror(MERA_PERROR_PREFIX "sched_setaffinity()");
CPU_FREE(affinity);
return -1;
}
CPU_FREE(affinity);
return current_cpu;
#elif defined(__APPLE__) || defined(__MACH__)
return -1;
#else
return -1;
#endif
}
/*****************************************************************************/
union timestamp {
uint64_t u64;
struct {
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
uint32_t h, l;
#else
uint32_t l, h;
#endif
} u32;
};
#if defined(EMSCRIPTEN)
static unsigned clock_emscripten(timestamp_t *now) {
compiler_barrier();
*now = (timestamp_t)(emscripten_get_now() * 1e6);
compiler_barrier();
return 0;
}
#endif /* EMSCRIPTEN */
#if defined(__APPLE__) || defined(__MACH__)
static unsigned clock_mach(timestamp_t *now) {
compiler_barrier();
*now = mach_absolute_time();
compiler_barrier();
return 0;
}
static double convert_mach(timestamp_t timestamp) {
static double ratio /* from mach_absolute_time() to seconds */;
if (!ratio) {
mach_timebase_info_data_t ti;
if (mach_timebase_info(&ti) != 0) {
perror(MERA_PERROR_PREFIX "mach_timebase_info()");
return -1;
}
ratio = (double)ti.numer / ti.denom;
}
return ratio * (double)timestamp;
}
#endif /* defined(__APPLE__) || defined(__MACH__) */
#if defined(_WIN64) || defined(_WIN32) || defined(__TOS_WIN__) || \
defined(__WINDOWS__)
static unsigned clock_windows(timestamp_t *now) {
compiler_barrier();
if (!QueryPerformanceCounter((LARGE_INTEGER *)now)) {
perror(MERA_PERROR_PREFIX "QueryPerformanceCounter()");
*now = 42;
}
compiler_barrier();
return 0;
}
static double convert_windows(timestamp_t timestamp) {
static double ratio /* from QueryPerformanceCounter() to seconds */;
if (!ratio) {
LARGE_INTEGER frequency;
if (!QueryPerformanceFrequency(&frequency)) {
perror(MERA_PERROR_PREFIX "QueryPerformanceFrequency()");
return -1;
}
ratio = 1e9 / frequency.QuadPart;
}
return ratio * (double)timestamp;
}
#else /* Windows */
static unsigned clock_gettimeofday(timestamp_t *now) {
compiler_barrier();
struct timeval tv;
if (gettimeofday(&tv, NULL)) {
perror(MERA_PERROR_PREFIX "gettimeofday()");
tv.tv_sec = tv.tv_usec = 0;
}
*now = tv.tv_sec * UINT64_C(1000000) + tv.tv_usec;
compiler_barrier();
return 0;
}
static double convert_us2ns(timestamp_t timestamp) { return 1e3 * timestamp; }
#endif /* ! Windows */
#if defined(TIMEBASE_SZ) || defined(__OS400__)
static unsigned clock_os400(timestamp_t *now) {
compiler_barrier();
timebasestruct_t tb;
if (read_wall_time(&tb, TIMEBASE_SZ) != 0) {
perror(MERA_PERROR_PREFIX "read_wall_time(TIMEBASE_SZ)");
abort();
}
union timestamp *u = (union timestamp *)now;
u->u32.h = tb.tb_high;
u->u32.l = tb.tb_low;
compiler_barrier();
return 0;
}
static double convert_os400(timestamp_t timestamp) {
static double ratio /* from read_wall_time() to seconds */;
if (!ratio) {
timebasestruct_t tb;
tb.tb_high = 0x7fff;
tb.tb_low = 0;
if (time_base_to_time(&tb, TIMEBASE_SZ) != 0) {
perror(MERA_PERROR_PREFIX "time_base_to_time()");
abort();
}
ratio = (tb.tb_high * 1e9 + tb.tb_low) / UINT64_C(0x7fff00000000);
}
return ratio * (double)timestamp;
}
#endif /* __OS400__ */
#if defined(CLOCK_MONOTONIC) || defined(CLOCK_MONOTONIC_RAW) || \
defined(CLOCK_SGI_CYCLE)
static clockid_t posix_clockid = CLOCK_REALTIME;
static unsigned clock_posix(timestamp_t *now) {
compiler_barrier();
struct timespec ts;
if (clock_gettime(posix_clockid, &ts)) {
perror(MERA_PERROR_PREFIX "clock_gettime()");
ts.tv_sec = ts.tv_nsec = 0;
}
*now = ts.tv_sec * UINT64_C(1000000000) + ts.tv_nsec;
compiler_barrier();
return 0;
}
#endif /* CLOCK_MONOTONIC || CLOCK_MONOTONIC_RAW || CLOCK_SGI_CYCLE */
#if defined(__sun__) || defined(__sun)
static unsigned clock_solaris(timestamp_t *now) {
compiler_barrier();
*now = gethrtime();
compiler_barrier();
}
#endif /* __sun__ */
/*****************************************************************************/
#if defined(__e2k__) || defined(__elbrus__)
static unsigned clock_elbrus(timestamp_t *now) {
compiler_barrier();
unsigned coreid;
*now = __rdtscp(&coreid);
compiler_barrier();
return coreid;
}
#endif /* __e2k__ || __elbrus__ */
#if (defined(__powerpc64__) || defined(__ppc64__) || defined(__ppc64) || \
defined(__powerpc64))
static unsigned clock_powerpc64_mfspr268(timestamp_t *now) {
compiler_barrier();
#if defined(__GNUC__)
uint64_t ticks;
__asm __volatile("mfspr %0, 268" : "=r"(ticks));
*now = ticks;
#else
*now = 42 /* FIXME */;
#endif
compiler_barrier();
return 0;
}
#endif /* __powerpc64__ */
#if (defined(__powerpc__) || defined(__ppc__) || defined(__powerpc) || \
defined(__ppc))
static unsigned clock_powerpc_mftb(timestamp_t *now) {
/* A time-base timer, which is not always precisely a cycle-count. */
compiler_barrier();
#if UINTPTR_MAX > 0xffffFFFFul || ULONG_MAX > 0xffffFFFFul
#if defined(__GNUC__)
uint64_t ticks;
__asm __volatile("mftb %0" : "=r"(ticks));
*now = ticks;
#else
*now = 42 /* FIXME */;
#endif
#else
#if defined(__GNUC__)
uint32_t low, high_before, high_after;
__asm __volatile("mftbu %0; mftb %1; mftbu %2"
: "=r"(high_before), "=r"(low), "=r"(high_after));
union timestamp *u = (union timestamp *)now;
u->u32.h = high_after;
u->u32.l = low & /* zeroes if high part has changed */
~(high_before - high_after);
#else
*now = 42 /* FIXME */;
#endif
#endif
compiler_barrier();
return 0;
}
#endif /* __powerpc__ */
#if defined(__sparc__) || defined(__sparc) || defined(__sparc64__) || \
defined(__sparc64) || defined(__sparc_v8plus__) || \
defined(__sparc_v8plus) || defined(__sparc_v8plusa__) || \
defined(__sparc_v8plusa) || defined(__sparc_v9__) || defined(__sparc_v9)
static unsigned clock_sparc(timestamp_t *now) {
compiler_barrier();
union timestamp cycles;
#ifndef __GNUC__
#warning FIXME
#else
#if defined(__sparc_v8plus__) || defined(__sparc_v8plusa__) || \
defined(__sparc_v9__) || defined(__sparc_v8plus) || \
defined(__sparc_v8plusa) || defined(__sparc_v9)
#if UINTPTR_MAX > 0xffffFFFFul || ULONG_MAX > 0xffffFFFFul || \
defined(__sparc64__) || defined(__sparc64)
__asm __volatile("rd %%tick, %0" : "=r"(cycles.u64));
#else
__asm __volatile("rd %%tick, %1; srlx %1, 32, %0"
: "=r"(cycles.u32.h), "=r"(cycles.u32.l));
#endif /* __sparc64__ */
#else
__asm __volatile(".byte 0x83, 0x41, 0x00, 0x00; mov %%g1, %0"
: "=r"(cycles.u64)
:
: "%g1");
#endif /* __sparc8plus__ || __sparc_v9__ */
#endif /* GCC */
*now = cycles.u64;
compiler_barrier();
return 0;
}
#endif /* __sparc__ */
#if defined(__ia64__) || defined(__ia64)
static unsigned clock_ia64(timestamp_t *now) {
compiler_barrier();
#if defined(__GNUC__)
uint64_t ticks;
__asm __volatile("mov %0 = ar.itc" : "=r"(ticks));
*now = ticks;
#elif defined(__EDG_VERSION) || defined(__ECC) || defined(IA64_REG_AR_ITC)
*now = __getReg(_IA64_REG_AR_ITC);
#elif defined(__hpux) || defined(_AREG_ITC)
*now = _Asm_mov_from_ar(_AREG_ITC);
#else
*now = 42 /* FIXME */;
#endif
compiler_barrier();
return 0;
}
#endif /* __ia64__ */
#if (defined(__hppa__) || defined(__hppa) || defined(__hppa64__) || \
defined(__hppa64))
static unsigned clock_hppa(timestamp_t *now) {
compiler_barrier();
uint64_t cycles;
#ifdef __GNUC__
__asm __volatile("mfctl 16, %0" : "=r"(cycles));
#else
_MFCTL(16, ticks);
#endif
*now = cycles;
compiler_barrier();
return 0;
}
#endif /* __hppa__ */
#if defined(__s390__) || defined(__s390) || defined(__zarch__) || \
defined(__zarch)
static unsigned clock_stcke(timestamp_t *now) {
compiler_barrier();
uint8_t clk[16];
#ifdef __GNUC__
__asm __volatile("stcke %0" : "=Q"(clk) : : "cc");
#else
#warning FIXME
#endif
*now = *((unsigned long long *)&clk[1]) >> 2;
compiler_barrier();
return (clk[14] << 8) | (clk[15]);
}
static unsigned clock_stckf(timestamp_t *now) {
compiler_barrier();
#ifdef __GNUC__
__asm __volatile("stckf 0(%1)" : "=m"(*now) : "a"(now) : "cc");
#else
#warning FIXME
#endif
compiler_barrier();
return 0;
}
static unsigned clock_stck(timestamp_t *now) {
compiler_barrier();
#ifdef __GNUC__
__asm __volatile("stck 0(%1)" : "=m"(*now) : "a"(now) : "cc");
#else
#warning FIXME
#endif
compiler_barrier();
return 0;
}
#endif /* __s390__ */
#if defined(__alpha__) || defined(__alpha)
static unsigned clock_alpha(timestamp_t *now) {
compiler_barrier();
#ifdef __GNUC__
unsigned long cycles;
__asm__ __volatile("rpcc %0" : "=r"(cycles));
*now = cycles & 0xFFFFfffful;
#else
*now = 42 /* FIXME */;
#endif
compiler_barrier();
return 0;
}
#endif /* __alpha__ */
/*****************************************************************************/
static double convert_1to1(timestamp_t timestamp) { return (double)timestamp; }
#if (defined(__ARM_ARCH) && __ARM_ARCH > 5 && __ARM_ARCH < 8) || defined(_M_ARM)
static unsigned clock_pmccntr(timestamp_t *now) {
compiler_barrier();
#ifdef _M_ARM
*now = __rdpmccntr64();
#else
unsigned long pmccntr;
__asm __volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(pmccntr));
*now = (uint64_t)pmccntr;
#endif
compiler_barrier();
return 0;
}
static double convert_pmccntr_x64(timestamp_t timestamp) {
/* The counter is set up to count every 64th cycle */
return timestamp * 64.0;
}
#endif /* __ARM_ARCH >= 6 || _M_ARM */
#if defined(__aarch64__) || (defined(__ARM_ARCH) && __ARM_ARCH > 7) || \
defined(_M_ARM64)
static unsigned clock_cntvct_el0(timestamp_t *now) {
compiler_barrier();
/* System timer of ARMv8 runs at a different frequency than the CPU's.
* The frequency is fixed, typically in the range 1-50MHz. It can be
* read at CNTFRQ special register. We assume the OS has set up
* the virtual timer properly. */
#ifdef _M_ARM64
*now = _ReadStatusReg(42 /* FIXME: cntvct_el0 */);
#else
uint64_t virtual_timer;
__asm __volatile("mrs %0, cntvct_el0" : "=r"(virtual_timer));
*now = virtual_timer;
#endif
compiler_barrier();
return 0;
}
#endif /* __aarch64__ || __ARM_ARCH > 7 || _M_ARM64 */
#if defined(__mips__) || defined(__mips) || defined(_R4000)
#if defined(PROT_READ) && defined(MAP_SHARED)
static volatile uint64_t *mips_tsc_addr;
static unsigned clock_zbustimer(timestamp_t *now) {
compiler_barrier();
*now = *mips_tsc_addr;
compiler_barrier();
return 0;
}
#endif /* PROT_READ && MAP_SHARED */
#if (defined(_MIPS_ISA) && defined(_MIPS_ISA_MIPS2) && \
_MIPS_ISA >= _MIPS_ISA_MIPS2) || \
(defined(__mips) && __mips >= 2 && __mips < 16) || defined(_R4000) || \
defined(__MIPS_ISA2) || defined(__MIPS_ISA3) || defined(__MIPS_ISA4) || \
(defined(__mips_isa_rev) && __mips_isa_rev >= 2)
static unsigned clock_mfc0_25_1(timestamp_t *now) {
compiler_barrier();
#if (defined(_MIPS_SIM) && defined(_ABI64) && _MIPS_SIM == _ABI64) || \
(defined(_MIPS_SIM) && defined(_ABIO64) && _MIPS_SIM == _ABIO64) || \
defined(__mips64) || defined(__mips64__) || \
(defined(__mips) && (__mips >= 64))
uint64_t count;
__asm __volatile("dmfc0 %0, $25, 1" : "=r"(count));
#else
uint32_t count;
__asm __volatile("mfc0 %0, $25, 1" : "=r"(count));
#endif
*now = count;
compiler_barrier();
return 0;
}
static unsigned clock_mfc0_9_0(timestamp_t *now) {
compiler_barrier();
#if (defined(_MIPS_SIM) && defined(_ABI64) && _MIPS_SIM == _ABI64) || \
(defined(_MIPS_SIM) && defined(_ABIO64) && _MIPS_SIM == _ABIO64) || \
defined(__mips64) || defined(__mips64__) || \
(defined(__mips) && (__mips >= 64))
uint64_t count;
__asm __volatile("dmfc0 %0, $9, 0" : "=r"(count));
#else
uint32_t count;
__asm __volatile("mfc0 %0, $9, 0" : "=r"(count));
#endif
*now = count;
compiler_barrier();
return 0;
}
static unsigned mips_rdhwr_resolution;
static unsigned clock_rdhwr(timestamp_t *now) {
compiler_barrier();
unsigned count, coreid;
__asm __volatile("rdhwr %0, $2; rdhwr %1, $0" : "=r"(count), "=r"(coreid));
*now = count;
compiler_barrier();
return coreid;
}
static double convert_rdhwr(timestamp_t timestamp) {
return (double)timestamp * mips_rdhwr_resolution;
}
#endif /* MIPS >= 2 */
#endif /* MIPS */
#if defined(__ia32__)
enum ia32_fixed_perfomance_counters {
/* count of retired instructions on the current core in the low-order 48 bits
of an unsigned 64-bit integer */
ia32_COUNT_HW_INSTRUCTIONS = 1 << 30,
/* count of actual CPU core cycles executed by the current core. Core cycles
are not accumulated while the processor is in the "HALT" state, which is
used when the operating system has no task(s) to run on a processor core.
*/
ia32_COUNT_HW_CPU_CYCLES = (1 << 30) + 1,
/* count of "reference" (or "nominal") CPU core cycles executed by the current
core. This counts at the same rate as the TSC, but does not count when the
core is in the "HALT" state. If a timed section of code shows a larger
change in TSC than in rdpmc_reference_cycles, the processor probably spent
some time in a HALT state. */
ia32_COUNT_HW_REF_CPU_CYCLES = (1 << 30) + 2,
};
static unsigned clock_rdpmc_start(timestamp_t *now) {
compiler_barrier();
#if __GNUC__
uint32_t low, high;
__asm __volatile("cpuid" ::: "%eax", "%ebx", "%ecx", "%edx");
__asm __volatile("rdpmc"
: "=a"(low), "=d"(high)
: "c"(ia32_COUNT_HW_CPU_CYCLES));
union timestamp *u = (union timestamp *)now;
u->u32.l = low;
u->u32.h = high;
#elif defined(_MSC_VER)
int unused[4];
__cpuid(unused, 0);
*now = __readpmc(ia32_COUNT_HW_CPU_CYCLES);
#else
#error "FIXME: Unsupported compiler"
#endif
compiler_barrier();
return 0;
}
static unsigned clock_rdpmc_finish(timestamp_t *now) {
compiler_barrier();
#if __GNUC__
uint32_t low, high;
__asm __volatile("mov %2, %%ecx; rdpmc; mov %%eax, %0; mov %%edx, %1; cpuid"
: "=r"(low), "=r"(high)
: "i"(ia32_COUNT_HW_CPU_CYCLES)
: "%eax", "%ebx", "%ecx", "%edx");
union timestamp *u = (union timestamp *)now;
u->u32.l = low;
u->u32.h = high;
#elif defined(_MSC_VER)
*now = __readpmc(ia32_COUNT_HW_CPU_CYCLES);
int unused[4];
__cpuid(unused, 0);
#else
#error "FIXME: Unsupported compiler"
#endif
return 0;
}
static unsigned clock_rdtscp_start(timestamp_t *now) {
compiler_barrier();
unsigned coreid;
*now = __rdtscp(&coreid);
return coreid;
}
static unsigned clock_rdtscp_finish(timestamp_t *now) {
compiler_barrier();
#if __GNUC__
uint32_t low, high, coreid;
__asm __volatile("rdtscp; mov %%eax, %0; mov %%edx, %1; mov %%ecx, %2; cpuid"
: "=r"(low), "=r"(high), "=r"(coreid)
:
: "%eax", "%ebx", "%ecx", "%edx");
union timestamp *u = (union timestamp *)now;
u->u32.l = low;
u->u32.h = high;
return coreid;
#elif defined(_MSC_VER)
unsigned coreid;
*now = __rdtscp(&coreid);
int unused[4];
__cpuid(unused, 0);
return coreid;
#else
#error "FIXME: Unsupported compiler"
#endif
}
static unsigned clock_rdtsc_start(timestamp_t *now) {
compiler_barrier();
#if __GNUC__
uint32_t low, high;
__asm __volatile("cpuid; rdtsc" : "=a"(low), "=d"(high) : : "%ebx", "%ecx");
union timestamp *u = (union timestamp *)now;
u->u32.l = low;
u->u32.h = high;
#elif defined(_MSC_VER)
int unused[4];
__cpuid(unused, 0);
*now = __rdtsc();
#else
#error "FIXME: Unsupported compiler"
#endif
compiler_barrier();
return 0;
}
static unsigned clock_rdtsc_finish(timestamp_t *now) {
compiler_barrier();
#if __GNUC__
uint32_t low, high;
__asm __volatile("rdtsc; mov %%eax, %0; mov %%edx, %1; cpuid"
: "=r"(low), "=r"(high)
:
: "%eax", "%ebx", "%ecx", "%edx");
union timestamp *u = (union timestamp *)now;
u->u32.l = low;
u->u32.h = high;
#elif defined(_MSC_VER)
int unused[4];
__cpuid(unused, 0);
*now = __rdtsc();
#else
#error "FIXME: Unsupported compiler"
#endif
compiler_barrier();
return 0;
}
ia32_cpu_features_t ia32_cpu_features;
void ia32_fetch_cpu_features(void) {
memset(&ia32_cpu_features, 0, sizeof(ia32_cpu_features));
#ifdef __GNUC__
uint32_t unused_eax, unused_ebx, cpuid_max;
cpuid_max = __get_cpuid_max(0, NULL);
if (cpuid_max >= 1) {
__cpuid_count(1, 0, unused_eax, ia32_cpu_features.basic.ebx,
ia32_cpu_features.basic.ecx, ia32_cpu_features.basic.edx);
if (cpuid_max >= 7)
__cpuid_count(7, 0, unused_eax, ia32_cpu_features.extended_7.ebx,
ia32_cpu_features.extended_7.ecx,
ia32_cpu_features.extended_7.edx);
}
cpuid_max = __get_cpuid_max(0x80000000, NULL);
if (cpuid_max >= 0x80000001) {
__cpuid_count(0x80000001, 0, unused_eax, unused_ebx,
ia32_cpu_features.extended_80000001.ecx,
ia32_cpu_features.extended_80000001.edx);
if (cpuid_max >= 0x80000007)
__cpuid_count(0x80000007, 0, unused_eax, unused_ebx,
ia32_cpu_features.extended_80000007.ecx,
ia32_cpu_features.extended_80000007.edx);
}
#elif defined(_MSC_VER)
int info[4];
__cpuid(info, 0);
unsigned cpuid_max = info[0];
if (cpuid_max >= 1) {
__cpuidex(info, 1, 0);
ia32_cpu_features.basic.ebx = info[1];
ia32_cpu_features.basic.ecx = info[2];
ia32_cpu_features.basic.edx = info[3];
if (cpuid_max >= 7) {
__cpuidex(info, 7, 0);
ia32_cpu_features.extended_7.ebx = info[1];
ia32_cpu_features.extended_7.ecx = info[2];
ia32_cpu_features.extended_7.edx = info[3];
}
}
__cpuid(info, 0x80000000);
cpuid_max = info[0];
if (cpuid_max >= 0x80000001) {
__cpuidex(info, 0x80000001, 0);
ia32_cpu_features.extended_80000001.ecx = info[2];
ia32_cpu_features.extended_80000001.edx = info[3];
if (cpuid_max >= 0x80000007) {
__cpuidex(info, 0x80000007, 0);
ia32_cpu_features.extended_80000007.ecx = info[2];
ia32_cpu_features.extended_80000007.edx = info[3];
}
}
#else
#error "FIXME: Unsupported compiler"
#endif
}
#endif /* __ia32__ */
/*****************************************************************************/
#ifdef __NR_perf_event_open
static int perf_fd, perf_error;
#if defined(__ia32__)
static const struct perf_event_mmap_page volatile *perf_page;