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