pastebin - collaborative debugging tool
rovema.kpaste.net RSS

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
210
/*
311
 * x86_rdtscp2_perf.c - compare execution time x86 RDTSCP instruction
412
 * vs. |sched_getcpu()|
513
 *
614
 * Compile with:
715
 * $ gcc -std=gnu17 -m32 -Wall x86_rdtscp2_perf.c -lpthread
816
 *
917
 * Written by Roland Mainz <roland.mainz@nrubsig.org>
1018
 *
1119
 */
1220
1321
#define _XOPEN_SOURCE 700
1422
#define _GNU_SOURCE 1
1523
1624
#include <stdio.h>
1725
#include <stdlib.h>
1826
#include <errno.h>
1927
2028
#include <pthread.h>
2129
#include <sched.h>
2230
2331
#include <x86intrin.h>
2432
2533
2634
int main(int ac, char *av[])
2735
{
2836
        (void)puts("#start.");
2937
3038
        unsigned int A = 0;
3139
3240
#define USE_FIXED_CPU_NUMBER 1
3341
#if USE_FIXED_CPU_NUMBER
3442
        int             cpu;
3543
        cpu_set_t       cpuset;
3644
        pthread_t       thread;
3745
3846
        thread = pthread_self();
3947
        cpu = sched_getcpu();
4048
4149
        CPU_ZERO(&cpuset);
4250
        CPU_SET(cpu, &cpuset);
4351
4452
        if (pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset) == 0) {
4553
                (void)printf("# pthread_setaffinity_np(), "
4654
                        "thread fixed to cpu %d\n",
4755
                        cpu);
4856
        }
4957
        else {
5058
                /*
5159
                 * If you specify a CPU number beyond the maximum number
5260
                 * of CPUs, then |pthread_setaffinity_np()| can have a
5361
                 * |errno == 0|
5462
                 */
5563
                perror("pthread_setaffinity_np");
5664
        }
5765
#endif /* USE_FIXED_CPU_NUMBER */
5866
5967
        struct timespec start, stop;
6068
        long long accum;
6169
        long long dummy = 0LL; /* use dummy variable to defeat optimiser */
6270
6371
        if (clock_gettime(CLOCK_REALTIME, &start) == -1) {
6472
                perror("clock gettime");
6573
                exit(EXIT_FAILURE);
6674
        }
6775
6876
        for (long l = 0 ; l < 100000000 ; l++) {
6977
                dummy += _rdtscp(&A);
7078
                dummy += A;
7179
        }
7280
7381
        if (clock_gettime(CLOCK_REALTIME, &stop) == -1 ) {
7482
                perror("clock gettime");
7583
                exit(EXIT_FAILURE);
7684
        }
7785
78-
#define NSECPERSEC (1000000000UL)
86+
#define NSECPERSEC (1000000000LL)
7987
        accum = (stop.tv_sec - start.tv_sec)*NSECPERSEC +
8088
                (stop.tv_nsec - start.tv_nsec);
8189
        (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)));
8391
8492
8593
8694
        if (clock_gettime(CLOCK_REALTIME, &start) == -1) {
8795
                perror( "clock gettime" );
8896
                exit(EXIT_FAILURE);
8997
        }
9098
9199
        for (long l = 0 ; l < 100000000 ; l++) {
92100
                dummy += sched_getcpu();
93101
        }
94102
95103
96104
        if (clock_gettime(CLOCK_REALTIME, &stop) == -1) {
97105
                perror("clock gettime");
98106
                exit(EXIT_FAILURE);
99107
        }
100108
101109
        accum = (stop.tv_sec - start.tv_sec)*NSECPERSEC +
102110
                (stop.tv_nsec - start.tv_nsec);
103111
        (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)));
105113
106114
        (void)dummy;
107115
108116
        (void)puts("#done.");
109117
110118
        return EXIT_SUCCESS;
111119
}
112-
rmainz@derfwpc5131:~/work/x86_rdtscp/try3_perf$ cat Makefile
120+
+ cat Makefile
113121
#
114122
# Makefile for x86_rdtscp2_perf.c - compare execution time x86 RDTSCP
115123
# instruction vs. |sched_getcpu()|
116124
#
117125
118126
all: \
119127
        x86_rdtscp2_perf_32 \
120128
        x86_rdtscp2_perf_64
121129
122130
# explicitly use -O0 to disable peephole optimisations
123131
x86_rdtscp2_perf_32: x86_rdtscp2_perf.c
124132
        gcc -std=gnu17 -m32 -Wall -g -O0 x86_rdtscp2_perf.c -lpthread -o x86_rdtscp2_perf_32
125133
126134
x86_rdtscp2_perf_64: x86_rdtscp2_perf.c
127135
        gcc -std=gnu17 -m64 -Wall -g -O0 x86_rdtscp2_perf.c -lpthread -o x86_rdtscp2_perf_64
128136
129137
130138
tests: \
131139
        x86_rdtscp2_perf_32 \
132140
        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 ; \
134146
135147
clean:
136148
        rm -f \
137149
                x86_rdtscp2_perf_32 \
138150
                x86_rdtscp2_perf_64
139151
# EOF.
142152
+ ./x86_rdtscp2_perf_32
143153
#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
147157
#done.
148158
149-
real    0m34.77s
159+
real    0m34.68s
150-
user    0m21.45s
160+
user    0m22.13s
151-
sys     0m13.29s
161+
sys     0m12.53s
152162
+ ./x86_rdtscp2_perf_64
153163
#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
157167
#done.
158168
159-
real    0m2.60s
169+
real    0m3.19s
160-
user    0m2.60s
170+
user    0m3.19s
161171
sys     0m0.00s
162172

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.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at