pastebin - collaborative debugging tool
rovema.kpaste.net RSS

Difference between
modified post 131e6a1 by Anonymous on Sat 17th Sep 2022 15:52
original post be40685 by Anonymous on Sat 17th Sep 2022 15:25
Download diff
Show old version | new version | both versions

    
11
#!/usr/bin/ksh93
22
33
#
4-
# Count timer syscalls
4+
# count_timer_syscalls.ksh- Count timer syscalls
55
#
66
# Tests whether clock_gettime() is implemented via traditional
77
# syscalls or uses a shortcut (e.g. via shared memory/Linux VDSO)
88
#
9+
# Written by Roland Mainz <roland.mainz@nrubsig.org>
911
1012
builtin cat
1113
builtin wc
1214
#builtin rm
1315
1416
set -o nounset
1517
1618
function enumerate_clock_sources
1719
{
1820
	typeset s
1921
	typeset dummy aname avalue
2022
	nameref ar="$1"
2123
2224
	s="$( clang -E -dM -m32 -include time.h - < '/dev/null' | \
2325
		egrep \
2426
			'#[[:space:]]*define[[:space:]]+CLOCK_.+[[:space:]]+[[:digit:]]+'
2527
		)"
2628
2729
	while read dummy aname avalue ; do
2830
		if [[ \
2931
			"${aname}" != ~(El)CLOCK_ || \
3032
			"${avalue}" != ~(Elr)[[:digit:]]+ ]] ; then
3133
			continue
3234
		fi
3335
		integer ar["$aname"].value
3436
		ar["$aname"].name="${aname}"
3537
		ar["$aname"].value="${avalue}"
3638
	done <<<"$s"
3739
3840
	return 0
3941
}
4042
4143
function generate_test_code
4244
{
4345
	nameref ar="$1"
46+
	integer num_testruns=$2
4447
4548
	set -o errexit
49+
	set -o nounset
4650
4751
	printf $"## Generating test code...\n"
4852
4953
	rm -f \
5054
		'generated_count_timer_syscalls.c' \
5155
		'generated_count_timer_syscalls'
5256
5357
	{
5458
		cat <<EOF
5559
5660
/*
5761
 * count_timer_syscalls.c - runs timer NUM_TESTRUNS times,
5862
 * strace can then be used to count syscalls
5963
 *
6064
 * Compile with:
6165
 * $ clang -g -Wall -Wextra count_timer_syscalls.c
6266
 *
67+
 * Written by Roland Mainz <roland.mainz@nrubsig.org>
6369
 */
6470
6571
#include <sys/types.h>
6672
#include <sys/stat.h>
6773
#include <fcntl.h>
6874
#include <unistd.h>
6975
#include <time.h>
7076
#include <sched.h>
7177
#include <stdlib.h>
7278
#include <stdint.h>
7379
#include <stdio.h>
7480
#include <string.h>
7581
#include <errno.h>
7682
7783
7884
#ifndef CLOCK_INVALID
7985
#define CLOCK_INVALID -1
8086
#endif
8187
8288
#define NSEC_PER_SEC (1000000000LL)
8389
84-
#define NUM_TESTRUNS (32000UL)
90+
#define NUM_TESTRUNS (${num_testruns}UL)
8591
8692
8793
static
8894
clockid_t get_clockid(int fd)
8995
{
9096
	const int CLOCKFD = 3;
9197
9298
        return (((unsigned int) ~fd) << 3) | CLOCKFD;
9399
}
94100
95101
96102
static
97103
void test_loop_on_clock(clockid_t clkid, unsigned long num_cycles)
98104
{
99105
	struct timespec ts;
100106
	unsigned long cycles;
101107
102108
	for(cycles = 0UL ; cycles < num_cycles ; cycles++)
103109
	{
104110
		(void)clock_gettime(clkid, &ts);
105111
		(void)ts;
106112
	}
107113
}
108114
109115
110116
int main(int ac, char *av[])
111117
{
112118
	const char *clockname = av[1];
113119
114120
	(void)puts("# start.");
115121
116122
	if ((ac != 2) || (!clockname)) {
117123
		(void)fprintf(stderr, "%s: Missing clockname argument\n", av[0]);
118124
		return EXIT_FAILURE;
119125
	}
120126
121127
	if (!strcmp(clockname, "nic")) {
122128
		int fd;
123129
		clockid_t clkid;
124130
		const char *devicename = "/dev/ptp0";
125131
126132
		fd = open(devicename, O_RDWR);
127133
		if (fd < 0) {
128134
			(void)fprintf(stderr, "opening %s: %s\n", devicename,
129135
				strerror(errno));
130136
			return(EXIT_FAILURE);
131137
        	}
132138
133139
		clkid = get_clockid(fd);
134140
		if (CLOCK_INVALID == clkid) {
135141
			(void)fprintf(stderr, "failed to read clock id\n");
136142
			return(EXIT_FAILURE);
137143
		}
138144
139145
		(void)printf("## test run using nic clock\n");
140146
		test_loop_on_clock(clkid, NUM_TESTRUNS);
141147
142148
		(void)close(fd);
143149
EOF
144150
145151
		typeset i
146152
		for i in "${!ar[@]}" ; do
147153
			typeset name="${ar[$i].name}"
148154
			printf '\t} else if (!strcmp(clockname, "%s")) {\n' "$name"
149155
			printf '\t\t(void)printf("## test run using %s\\n");\n' "$name"
150156
			printf '\n'
151157
			printf '\t\ttest_loop_on_clock(%s, NUM_TESTRUNS);\n' "$name"
152158
		done
153159
154160
		cat <<EOF
155161
	} else {
156162
		(void)fprintf(stderr, "%s: Unknown clockname argument %s\n",
157163
			av[0], clockname);
158164
		return EXIT_FAILURE;
159165
	}
160166
161167
	(void)puts("# end.");
162168
163169
	return(EXIT_SUCCESS);
164170
}
165171
EOF
166172
	} >"generated_count_timer_syscalls.c"
167173
168174
	return 0
169175
}
170176
171177
function compile_test_code
172178
{
173179
	set -o errexit
174180
175181
	printf $"## Compiling test code...\n"
176182
177183
	clang -m32 -g -Wall -Wextra \
178184
		-o generated_count_timer_syscalls \
179185
		generated_count_timer_syscalls.c
180186
181187
	return 0
182188
}
183189
184190
function run_tests
185191
{
186192
	nameref ar="$1"
187193
	compound cmd
188194
	typeset cmd.output
189195
	integer cmd.exit_code
190196
	typeset i
191197
192198
	printf $"## Running tests...\n"
193199
194200
	for i in "${!ar[@]}" ; do
195201
		nameref n=ar["$i"]
196202
197203
		printf $"Probing %q...\n" "${n.name}"
198204
		cmd.output="${ strace ./generated_count_timer_syscalls "${n.name}" 2>&1 ; cmd.exit_code="$?" ; }"
199205
200206
		integer n.num_syscalls_clock_gettime="${ egrep 'clock_gettime' <<<"${cmd.output}" | wc -l ;}"
201207
		integer n.num_syscalls_total="${ wc -l <<<"${cmd.output}" ; }"
202208
		integer n.exit_code=${cmd.exit_code}
203209
	done
204210
}
205211
206212
function print_results
207213
{
208214
	nameref c=$1
209-
	printf $"## Raw test results:\n"
210-
	print -v c
211215
216+
	set -o nounset
216217
218+
	printf $"## Raw test results:\n"
217226
		printf $"Clock name %-20s \t" ${n.name}
218227
		if (( (n.exit_code != 0) || (n.num_syscalls_total < 30) )) ; then
219228
			printf $"TEST FAILED\n"
220-
		elif (( n.num_syscalls_clock_gettime >= 32000 )) ; then
229+
		elif (( n.num_syscalls_clock_gettime >= c.num_testruns )) ; then
221230
			printf $"uses traditional syscalls\n"
222231
		elif (( n.num_syscalls_clock_gettime <= 1 )) ; then
223232
			printf $"uses shortcut\n"
224233
		else
225234
			printf $"Data inconclusive\n"
226235
		fi
227236
	done
228237
	return 0
229238
}
230239
231240
function main
232241
{
233242
	compound c
234243
	compound -A c.clock_names
235244
245+
	integer c.num_testruns=32100
236246
	typeset c.uname="$(uname -a)"
237247
	typeset c.hostname="$(hostname)"
238248
239249
	if ! enumerate_clock_sources c.clock_names ; then
240250
		print -u2 -f $"%s: Could not enumerate clock names.\n" "$1"
241251
		return 1
242252
	fi
243253
244-
	if ! generate_test_code c.clock_names ; then
254+
	if ! generate_test_code c.clock_names ${c.num_testruns} ; then
245255
		print -u2 -f $"%s: Could not generate test code.\n" "$1"
246256
		return 1
247257
	fi
248258
249259
	if ! compile_test_code ; then
250260
		print -u2 -f $"%s: Could not compile test code.\n" "$1"
251261
		return 1
252262
	fi
253263
254264
	run_tests c.clock_names
255265
256266
	print_results c
257267
258268
	print $"## Done."
259269
	return 0
260270
}
261271
262272
main "$@"
263273
# EOF.
264274

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