Difference between
modified post 0bbd6628 by Anonymous on Sat 26th Nov 2022 15:49
original post 8504236b4 by Anonymous on Sat 26th Nov 2022 15:28
Download diff
Show
old version |
new version |
both versions
| 1 | - | rmainz@derfwpc5131:~/work/x86_rdtscp/try3_perf$ cat x86_rdtscp2_perf.c | |
| 1 | + | rmainz@derfwpc5131:~/work/x86_rdtscp/try3_perf$ make tests | |
| 2 | + | gcc -std=gnu17 -m32 -Wall -g -O0 x86_rdtscp2_perf.c -lpthread -o x86_rdtscp2_perf_32 | |
| 2 | 10 | /* | |
| 3 | 11 | * x86_rdtscp2_perf.c - compare execution time x86 RDTSCP instruction | |
| 4 | 12 | * vs. |sched_getcpu()| | |
| 5 | 13 | * | |
| 6 | 14 | * Compile with: | |
| 7 | 15 | * $ gcc -std=gnu17 -m32 -Wall x86_rdtscp2_perf.c -lpthread | |
| 8 | 16 | * | |
| 9 | 17 | * Written by Roland Mainz <roland.mainz@nrubsig.org> | |
| 10 | 18 | * | |
| 11 | 19 | */ | |
| 12 | 20 | ||
| 13 | 21 | #define _XOPEN_SOURCE 700 | |
| 14 | 22 | #define _GNU_SOURCE 1 | |
| 15 | 23 | ||
| 16 | 24 | #include <stdio.h> | |
| 17 | 25 | #include <stdlib.h> | |
| 18 | 26 | #include <errno.h> | |
| 19 | 27 | ||
| 20 | 28 | #include <pthread.h> | |
| 21 | 29 | #include <sched.h> | |
| 22 | 30 | ||
| 23 | 31 | #include <x86intrin.h> | |
| 24 | 32 | ||
| 25 | 33 | ||
| 26 | 34 | int main(int ac, char *av[]) | |
| 27 | 35 | { | |
| 28 | 36 | (void)puts("#start."); | |
| 29 | 37 | ||
| 30 | 38 | unsigned int A = 0; | |
| 31 | 39 | ||
| 32 | 40 | #define USE_FIXED_CPU_NUMBER 1 | |
| 33 | 41 | #if USE_FIXED_CPU_NUMBER | |
| 34 | 42 | int cpu; | |
| 35 | 43 | cpu_set_t cpuset; | |
| 36 | 44 | pthread_t thread; | |
| 37 | 45 | ||
| 38 | 46 | thread = pthread_self(); | |
| 39 | 47 | cpu = sched_getcpu(); | |
| 40 | 48 | ||
| 41 | 49 | CPU_ZERO(&cpuset); | |
| 42 | 50 | CPU_SET(cpu, &cpuset); | |
| 43 | 51 | ||
| 44 | 52 | if (pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset) == 0) { | |
| 45 | 53 | (void)printf("# pthread_setaffinity_np(), " | |
| 46 | 54 | "thread fixed to cpu %d\n", | |
| 47 | 55 | cpu); | |
| 48 | 56 | } | |
| 49 | 57 | else { | |
| 50 | 58 | /* | |
| 51 | 59 | * If you specify a CPU number beyond the maximum number | |
| 52 | 60 | * of CPUs, then |pthread_setaffinity_np()| can have a | |
| 53 | 61 | * |errno == 0| | |
| 54 | 62 | */ | |
| 55 | 63 | perror("pthread_setaffinity_np"); | |
| 56 | 64 | } | |
| 57 | 65 | #endif /* USE_FIXED_CPU_NUMBER */ | |
| 58 | 66 | ||
| 59 | 67 | struct timespec start, stop; | |
| 60 | 68 | long long accum; | |
| 61 | 69 | long long dummy = 0LL; /* use dummy variable to defeat optimiser */ | |
| 62 | 70 | ||
| 63 | 71 | if (clock_gettime(CLOCK_REALTIME, &start) == -1) { | |
| 64 | 72 | perror("clock gettime"); | |
| 65 | 73 | exit(EXIT_FAILURE); | |
| 66 | 74 | } | |
| 67 | 75 | ||
| 68 | 76 | for (long l = 0 ; l < 100000000 ; l++) { | |
| 69 | 77 | dummy += _rdtscp(&A); | |
| 70 | 78 | dummy += A; | |
| 71 | 79 | } | |
| 72 | 80 | ||
| 73 | 81 | if (clock_gettime(CLOCK_REALTIME, &stop) == -1 ) { | |
| 74 | 82 | perror("clock gettime"); | |
| 75 | 83 | exit(EXIT_FAILURE); | |
| 76 | 84 | } | |
| 77 | 85 | ||
| 78 | - | #define NSECPERSEC (1000000000UL) | |
| 86 | + | #define NSECPERSEC (1000000000LL) | |
| 79 | 87 | accum = (stop.tv_sec - start.tv_sec)*NSECPERSEC + | |
| 80 | 88 | (stop.tv_nsec - start.tv_nsec); | |
| 81 | 89 | (void)printf("# time needed for x86 rdtscp instruction:\t%20.20f\n", | |
| 82 | - | (double)((long double)accum/(long double)NSECPERSEC)); | |
| 90 | + | (double)(((long double)accum)/((long double)NSECPERSEC))); | |
| 83 | 91 | ||
| 84 | 92 | ||
| 85 | 93 | ||
| 86 | 94 | if (clock_gettime(CLOCK_REALTIME, &start) == -1) { | |
| 87 | 95 | perror( "clock gettime" ); | |
| 88 | 96 | exit(EXIT_FAILURE); | |
| 89 | 97 | } | |
| 90 | 98 | ||
| 91 | 99 | for (long l = 0 ; l < 100000000 ; l++) { | |
| 92 | 100 | dummy += sched_getcpu(); | |
| 93 | 101 | } | |
| 94 | 102 | ||
| 95 | 103 | ||
| 96 | 104 | if (clock_gettime(CLOCK_REALTIME, &stop) == -1) { | |
| 97 | 105 | perror("clock gettime"); | |
| 98 | 106 | exit(EXIT_FAILURE); | |
| 99 | 107 | } | |
| 100 | 108 | ||
| 101 | 109 | accum = (stop.tv_sec - start.tv_sec)*NSECPERSEC + | |
| 102 | 110 | (stop.tv_nsec - start.tv_nsec); | |
| 103 | 111 | (void)printf("# time needed for |sched_getcpu()|: \t%20.20f\n", | |
| 104 | - | (double)((long double)accum/(long double)NSECPERSEC)); | |
| 112 | + | (double)(((long double)accum)/((long double)NSECPERSEC))); | |
| 105 | 113 | ||
| 106 | 114 | (void)dummy; | |
| 107 | 115 | ||
| 108 | 116 | (void)puts("#done."); | |
| 109 | 117 | ||
| 110 | 118 | return EXIT_SUCCESS; | |
| 111 | 119 | } | |
| 112 | - | rmainz@derfwpc5131:~/work/x86_rdtscp/try3_perf$ cat Makefile | |
| 120 | + | + cat Makefile | |
| 113 | 121 | # | |
| 114 | 122 | # Makefile for x86_rdtscp2_perf.c - compare execution time x86 RDTSCP | |
| 115 | 123 | # instruction vs. |sched_getcpu()| | |
| 116 | 124 | # | |
| 117 | 125 | ||
| 118 | 126 | all: \ | |
| 119 | 127 | x86_rdtscp2_perf_32 \ | |
| 120 | 128 | x86_rdtscp2_perf_64 | |
| 121 | 129 | ||
| 122 | 130 | # explicitly use -O0 to disable peephole optimisations | |
| 123 | 131 | x86_rdtscp2_perf_32: x86_rdtscp2_perf.c | |
| 124 | 132 | gcc -std=gnu17 -m32 -Wall -g -O0 x86_rdtscp2_perf.c -lpthread -o x86_rdtscp2_perf_32 | |
| 125 | 133 | ||
| 126 | 134 | x86_rdtscp2_perf_64: x86_rdtscp2_perf.c | |
| 127 | 135 | gcc -std=gnu17 -m64 -Wall -g -O0 x86_rdtscp2_perf.c -lpthread -o x86_rdtscp2_perf_64 | |
| 128 | 136 | ||
| 129 | 137 | ||
| 130 | 138 | tests: \ | |
| 131 | 139 | x86_rdtscp2_perf_32 \ | |
| 132 | 140 | x86_rdtscp2_perf_64 | |
| 133 | - | ksh93 -c 'set -o xtrace ; time ./x86_rdtscp2_perf_32 ; time ./x86_rdtscp2_perf_64' | |
| 141 | + | ksh93 -c 'set -o xtrace ; \ | |
| 142 | + | cat x86_rdtscp2_perf.c ; \ | |
| 134 | 146 | ||
| 135 | 147 | clean: | |
| 136 | 148 | rm -f \ | |
| 137 | 149 | x86_rdtscp2_perf_32 \ | |
| 138 | 150 | x86_rdtscp2_perf_64 | |
| 139 | 151 | # EOF. | |
| 142 | 152 | + ./x86_rdtscp2_perf_32 | |
| 143 | 153 | #start. | |
| 144 | - | # pthread_setaffinity_np(), thread fixed to cpu 0 | |
| 154 | + | # pthread_setaffinity_np(), thread fixed to cpu 1 | |
| 145 | - | # time needed for x86 rdtscp instruction: 1.29958247999999998434 | |
| 155 | + | # time needed for x86 rdtscp instruction: 1.32610883200000007065 | |
| 146 | - | # time needed for |sched_getcpu()|: 3.39503941599999992107 | |
| 156 | + | # time needed for |sched_getcpu()|: 33.35152474899999930358 | |
| 147 | 157 | #done. | |
| 148 | 158 | ||
| 149 | - | real 0m34.77s | |
| 159 | + | real 0m34.68s | |
| 150 | - | user 0m21.45s | |
| 160 | + | user 0m22.13s | |
| 151 | - | sys 0m13.29s | |
| 161 | + | sys 0m12.53s | |
| 152 | 162 | + ./x86_rdtscp2_perf_64 | |
| 153 | 163 | #start. | |
| 154 | - | # pthread_setaffinity_np(), thread fixed to cpu 6 | |
| 164 | + | # pthread_setaffinity_np(), thread fixed to cpu 0 | |
| 155 | - | # time needed for x86 rdtscp instruction: 1.20903293999999994490 | |
| 165 | + | # time needed for x86 rdtscp instruction: 1.23842487100000009370 | |
| 156 | - | # time needed for |sched_getcpu()|: 1.38745017499999989674 | |
| 166 | + | # time needed for |sched_getcpu()|: 1.95299446999999992691 | |
| 157 | 167 | #done. | |
| 158 | 168 | ||
| 159 | - | real 0m2.60s | |
| 169 | + | real 0m3.19s | |
| 160 | - | user 0m2.60s | |
| 170 | + | user 0m3.19s | |
| 161 | 171 | sys 0m0.00s | |
| 162 | 172 |
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
rovema.kpaste.net RSS