pastebin - collaborative debugging tool
rovema.kpaste.net RSS

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

    
11
#!/usr/bin/ksh93
22
33
#
44
# 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
#
99
1010
builtin cat
1111
builtin wc
1212
#builtin rm
1313
1414
set -o nounset
1515
1616
function enumerate_clock_sources
1717
{
1818
	typeset s
1919
	typeset dummy aname avalue
2020
	nameref ar="$1"
2121
2222
	s="$( clang -E -dM -m32 -include time.h - < '/dev/null' | \
2323
		egrep \
2424
			'#[[:space:]]*define[[:space:]]+CLOCK_.+[[:space:]]+[[:digit:]]+'
2525
		)"
2626
2727
	while read dummy aname avalue ; do
2828
		if [[ \
2929
			"${aname}" != ~(El)CLOCK_ || \
3030
			"${avalue}" != ~(Elr)[[:digit:]]+ ]] ; then
3131
			continue
3232
		fi
3333
		integer ar["$aname"].value
3434
		ar["$aname"].name="${aname}"
3535
		ar["$aname"].value="${avalue}"
3636
	done <<<"$s"
3737
3838
	return 0
3939
}
4040
4141
function generate_test_code
4242
{
4343
	nameref ar="$1"
4444
4545
	set -o errexit
4646
4747
	printf $"## Generating test code...\n"
4848
4949
	rm -f \
5050
		'generated_count_timer_syscalls.c' \
5151
		'generated_count_timer_syscalls'
5252
5353
	{
5454
		cat <<EOF
5555
5656
/*
5757
 * count_timer_syscalls.c - runs timer NUM_TESTRUNS times,
5858
 * strace can then be used to count syscalls
5959
 *
6060
 * Compile with:
6161
 * $ clang -g -Wall -Wextra count_timer_syscalls.c
6262
 *
6363
 */
6464
6565
#include <sys/types.h>
6666
#include <sys/stat.h>
6767
#include <fcntl.h>
6868
#include <unistd.h>
6969
#include <time.h>
7070
#include <sched.h>
7171
#include <stdlib.h>
7272
#include <stdint.h>
7373
#include <stdio.h>
7474
#include <string.h>
7575
#include <errno.h>
7676
7777
7878
#ifndef CLOCK_INVALID
7979
#define CLOCK_INVALID -1
8080
#endif
8181
8282
#define NSEC_PER_SEC (1000000000LL)
8383
8484
#define NUM_TESTRUNS (32000UL)
8585
8686
8787
static
8888
clockid_t get_clockid(int fd)
8989
{
9090
	const int CLOCKFD = 3;
9191
9292
        return (((unsigned int) ~fd) << 3) | CLOCKFD;
9393
}
9494
9595
9696
static
9797
void test_loop_on_clock(clockid_t clkid, unsigned long num_cycles)
9898
{
9999
	struct timespec ts;
100100
	unsigned long cycles;
101101
102102
	for(cycles = 0UL ; cycles < num_cycles ; cycles++)
103103
	{
104104
		(void)clock_gettime(clkid, &ts);
105105
		(void)ts;
106106
	}
107107
}
108108
109109
110110
int main(int ac, char *av[])
111111
{
112112
	const char *clockname = av[1];
113113
114114
	(void)puts("# start.");
115115
116116
	if ((ac != 2) || (!clockname)) {
117117
		(void)fprintf(stderr, "%s: Missing clockname argument\n", av[0]);
118118
		return EXIT_FAILURE;
119119
	}
120120
121121
	if (!strcmp(clockname, "nic")) {
122122
		int fd;
123123
		clockid_t clkid;
124124
		const char *devicename = "/dev/ptp0";
125125
126126
		fd = open(devicename, O_RDWR);
127127
		if (fd < 0) {
128128
			(void)fprintf(stderr, "opening %s: %s\n", devicename,
129129
				strerror(errno));
130130
			return(EXIT_FAILURE);
131131
        	}
132132
133133
		clkid = get_clockid(fd);
134134
		if (CLOCK_INVALID == clkid) {
135135
			(void)fprintf(stderr, "failed to read clock id\n");
136136
			return(EXIT_FAILURE);
137137
		}
138138
139139
		(void)printf("## test run using nic clock\n");
140140
		test_loop_on_clock(clkid, NUM_TESTRUNS);
141141
142142
		(void)close(fd);
143143
EOF
144144
145145
		typeset i
146146
		for i in "${!ar[@]}" ; do
147147
			typeset name="${ar[$i].name}"
148148
			printf '\t} else if (!strcmp(clockname, "%s")) {\n' "$name"
149149
			printf '\t\t(void)printf("## test run using %s\\n");\n' "$name"
150150
			printf '\n'
151151
			printf '\t\ttest_loop_on_clock(%s, NUM_TESTRUNS);\n' "$name"
152152
		done
153153
154154
		cat <<EOF
155155
	} else {
156156
		(void)fprintf(stderr, "%s: Unknown clockname argument %s\n",
157157
			av[0], clockname);
158158
		return EXIT_FAILURE;
159159
	}
160160
161161
	(void)puts("# end.");
162162
163163
	return(EXIT_SUCCESS);
164164
}
165165
EOF
166166
	} >"generated_count_timer_syscalls.c"
167167
168168
	return 0
169169
}
170170
171171
function compile_test_code
172172
{
173173
	set -o errexit
174174
175175
	printf $"## Compiling test code...\n"
176176
177177
	clang -m32 -g -Wall -Wextra \
178178
		-o generated_count_timer_syscalls \
179179
		generated_count_timer_syscalls.c
180180
181181
	return 0
182182
}
183183
184184
function run_tests
185185
{
186186
	nameref ar="$1"
187187
	compound cmd
188188
	typeset cmd.output
189189
	integer cmd.exit_code
190190
	typeset i
191191
192192
	printf $"## Running tests...\n"
193193
194194
	for i in "${!ar[@]}" ; do
195195
		nameref n=ar["$i"]
196196
197197
		printf $"Probing %q...\n" "${n.name}"
198198
		cmd.output="${ strace ./generated_count_timer_syscalls "${n.name}" 2>&1 ; cmd.exit_code="$?" ; }"
199199
200200
		integer n.num_syscalls_clock_gettime="${ egrep 'clock_gettime' <<<"${cmd.output}" | wc -l ;}"
201201
		integer n.num_syscalls_total="${ wc -l <<<"${cmd.output}" ; }"
202202
		integer n.exit_code=${cmd.exit_code}
203203
	done
204204
}
205205
206206
function print_results
207207
{
208208
	nameref c=$1
209209
	printf $"## Raw test results:\n"
210210
	print -v c
211211
212-
	printf $"## Summary of test results\n"
212+
	printf $"## Summary of test results for %q:\n" "${c.uname}"
213213
	nameref ar="c.clock_names"
214214
	for i in "${!ar[@]}" ; do
215215
		nameref n=ar["$i"]
216216
217217
		printf $"Clock name %-20s \t" ${n.name}
218218
		if (( (n.exit_code != 0) || (n.num_syscalls_total < 30) )) ; then
219219
			printf $"TEST FAILED\n"
220220
		elif (( n.num_syscalls_clock_gettime >= 32000 )) ; then
221221
			printf $"uses traditional syscalls\n"
222222
		elif (( n.num_syscalls_clock_gettime <= 1 )) ; then
223223
			printf $"uses shortcut\n"
224224
		else
225225
			printf $"Data inconclusive\n"
226226
		fi
227227
	done
228228
	return 0
229229
}
230230
231231
function main
232232
{
233233
	compound c
234234
	compound -A c.clock_names
235235
236+
	typeset c.uname="$(uname -a)"
236239
	if ! enumerate_clock_sources c.clock_names ; then
237240
		print -u2 -f $"%s: Could not enumerate clock names.\n" "$1"
238241
		return 1
239242
	fi
240243
241244
	if ! generate_test_code c.clock_names ; then
242245
		print -u2 -f $"%s: Could not generate test code.\n" "$1"
243246
		return 1
244247
	fi
245248
246249
	if ! compile_test_code ; then
247250
		print -u2 -f $"%s: Could not compile test code.\n" "$1"
248251
		return 1
249252
	fi
250253
251254
	run_tests c.clock_names
252255
253256
	print_results c
254257
255258
	print $"## Done."
256259
	return 0
257260
}
258261
259262
main "$@"
260263
# EOF.
261264

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