pastebin - collaborative debugging tool
rovema.kpaste.net RSS

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 ; \
1826
 *
27+
 * Compile with:
1932
 */
2033
2134
#define _XOPEN_SOURCE 700
2235
#define _GNU_SOURCE 1
2336
2437
#include <stdio.h>
2538
#include <stdlib.h>
2639
#include <errno.h>
2740
2841
#include <pthread.h>
2942
#include <sched.h>
3043
3144
#include <x86intrin.h>
3245
3346
3447
int main(int ac, char *av[])
3548
{
3649
        (void)puts("#start.");
3750
38-
        unsigned int A = 0;
51+
        unsigned int    A = 0;
52+
        long long       li; /* loop iterator */
3953
4054
#define USE_FIXED_CPU_NUMBER 1
4155
#if USE_FIXED_CPU_NUMBER
4256
        int             cpu;
4357
        cpu_set_t       cpuset;
4458
        pthread_t       thread;
4559
4660
        thread = pthread_self();
4761
        cpu = sched_getcpu();
4862
4963
        CPU_ZERO(&cpuset);
5064
        CPU_SET(cpu, &cpuset);
5165
5266
        if (pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset) == 0) {
5367
                (void)printf("# pthread_setaffinity_np(), "
5468
                        "thread fixed to cpu %d\n",
5569
                        cpu);
5670
        }
5771
        else {
5872
                /*
5973
                 * If you specify a CPU number beyond the maximum number
6074
                 * of CPUs, then |pthread_setaffinity_np()| can have a
6175
                 * |errno == 0|
6276
                 */
6377
                perror("pthread_setaffinity_np");
6478
        }
6579
#endif /* USE_FIXED_CPU_NUMBER */
6680
6781
        struct timespec start, stop;
6882
        long long accum;
6983
        long long dummy = 0LL; /* use dummy variable to defeat optimiser */
7084
7185
        if (clock_gettime(CLOCK_REALTIME, &start) == -1) {
7286
                perror("clock gettime");
7387
                exit(EXIT_FAILURE);
7488
        }
89+
#define NUMTESTITERATIONS (1000000000LL)
7791
                dummy += _rdtscp(&A);
7892
                dummy += A;
7993
        }
8094
8195
        if (clock_gettime(CLOCK_REALTIME, &stop) == -1 ) {
8296
                perror("clock gettime");
8397
                exit(EXIT_FAILURE);
8498
        }
8599
86100
#define NSECPERSEC (1000000000LL)
87101
        accum = (stop.tv_sec - start.tv_sec)*NSECPERSEC +
88102
                (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"
90107
                (double)(((long double)accum)/((long double)NSECPERSEC)));
91108
92109
93110
94111
        if (clock_gettime(CLOCK_REALTIME, &start) == -1) {
95112
                perror( "clock gettime" );
96113
                exit(EXIT_FAILURE);
97114
        }
98115
99-
        for (long l = 0 ; l < 100000000 ; l++) {
116+
        for (li = 0 ; li < NUMTESTITERATIONS ; li++) {
100117
                dummy += sched_getcpu();
101118
        }
102119
103120
104121
        if (clock_gettime(CLOCK_REALTIME, &stop) == -1) {
105122
                perror("clock gettime");
106123
                exit(EXIT_FAILURE);
107124
        }
108125
109126
        accum = (stop.tv_sec - start.tv_sec)*NSECPERSEC +
110127
                (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"
112132
                (double)(((long double)accum)/((long double)NSECPERSEC)));
113133
114134
        (void)dummy;
115135
116136
        (void)puts("#done.");
117137
118138
        return EXIT_SUCCESS;
119139
}
120140
+ cat Makefile
121141
#
122142
# Makefile for x86_rdtscp2_perf.c - compare execution time x86 RDTSCP
123143
# instruction vs. |sched_getcpu()|
124144
#
125145
126146
all: \
127147
        x86_rdtscp2_perf_32 \
128148
        x86_rdtscp2_perf_64
129149
130150
# explicitly use -O0 to disable peephole optimisations
131151
x86_rdtscp2_perf_32: x86_rdtscp2_perf.c
132152
        gcc -std=gnu17 -m32 -Wall -g -O0 x86_rdtscp2_perf.c -lpthread -o x86_rdtscp2_perf_32
133153
134154
x86_rdtscp2_perf_64: x86_rdtscp2_perf.c
135155
        gcc -std=gnu17 -m64 -Wall -g -O0 x86_rdtscp2_perf.c -lpthread -o x86_rdtscp2_perf_64
136156
137157
138158
tests: \
139159
        x86_rdtscp2_perf_32 \
140160
        x86_rdtscp2_perf_64
141161
        ksh93 -c 'set -o xtrace ; \
142162
                cat x86_rdtscp2_perf.c ; \
143163
                cat Makefile ; \
144164
                time ./x86_rdtscp2_perf_32 ; \
145165
                time ./x86_rdtscp2_perf_64'
146166
147167
clean:
148168
        rm -f \
149169
                x86_rdtscp2_perf_32 \
150170
                x86_rdtscp2_perf_64
151171
# EOF.
152172
+ ./x86_rdtscp2_perf_32
153173
#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
157177
#done.
158178
159-
real    0m34.68s
179+
real    5m48.15s
160-
user    0m22.13s
180+
user    3m42.13s
161-
sys     0m12.53s
181+
sys     2m5.94s
162182
+ ./x86_rdtscp2_perf_64
163183
#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
167187
#done.
168188
169-
real    0m3.19s
189+
real    0m25.94s
170-
user    0m3.19s
190+
user    0m25.92s
171191
sys     0m0.00s
172192

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