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
| 1 | 1 | #!/usr/bin/ksh93 | |
| 2 | 2 | ||
| 3 | 3 | # | |
| 4 | - | # Count timer syscalls | |
| 4 | + | # count_timer_syscalls.ksh- Count timer syscalls | |
| 5 | 5 | # | |
| 6 | 6 | # Tests whether clock_gettime() is implemented via traditional | |
| 7 | 7 | # syscalls or uses a shortcut (e.g. via shared memory/Linux VDSO) | |
| 8 | 8 | # | |
| 9 | + | # Written by Roland Mainz <roland.mainz@nrubsig.org> | |
| 9 | 11 | ||
| 10 | 12 | builtin cat | |
| 11 | 13 | builtin wc | |
| 12 | 14 | #builtin rm | |
| 13 | 15 | ||
| 14 | 16 | set -o nounset | |
| 15 | 17 | ||
| 16 | 18 | function enumerate_clock_sources | |
| 17 | 19 | { | |
| 18 | 20 | typeset s | |
| 19 | 21 | typeset dummy aname avalue | |
| 20 | 22 | nameref ar="$1" | |
| 21 | 23 | ||
| 22 | 24 | s="$( clang -E -dM -m32 -include time.h - < '/dev/null' | \ | |
| 23 | 25 | egrep \ | |
| 24 | 26 | '#[[:space:]]*define[[:space:]]+CLOCK_.+[[:space:]]+[[:digit:]]+' | |
| 25 | 27 | )" | |
| 26 | 28 | ||
| 27 | 29 | while read dummy aname avalue ; do | |
| 28 | 30 | if [[ \ | |
| 29 | 31 | "${aname}" != ~(El)CLOCK_ || \ | |
| 30 | 32 | "${avalue}" != ~(Elr)[[:digit:]]+ ]] ; then | |
| 31 | 33 | continue | |
| 32 | 34 | fi | |
| 33 | 35 | integer ar["$aname"].value | |
| 34 | 36 | ar["$aname"].name="${aname}" | |
| 35 | 37 | ar["$aname"].value="${avalue}" | |
| 36 | 38 | done <<<"$s" | |
| 37 | 39 | ||
| 38 | 40 | return 0 | |
| 39 | 41 | } | |
| 40 | 42 | ||
| 41 | 43 | function generate_test_code | |
| 42 | 44 | { | |
| 43 | 45 | nameref ar="$1" | |
| 46 | + | integer num_testruns=$2 | |
| 44 | 47 | ||
| 45 | 48 | set -o errexit | |
| 49 | + | set -o nounset | |
| 46 | 50 | ||
| 47 | 51 | printf $"## Generating test code...\n" | |
| 48 | 52 | ||
| 49 | 53 | rm -f \ | |
| 50 | 54 | 'generated_count_timer_syscalls.c' \ | |
| 51 | 55 | 'generated_count_timer_syscalls' | |
| 52 | 56 | ||
| 53 | 57 | { | |
| 54 | 58 | cat <<EOF | |
| 55 | 59 | ||
| 56 | 60 | /* | |
| 57 | 61 | * count_timer_syscalls.c - runs timer NUM_TESTRUNS times, | |
| 58 | 62 | * strace can then be used to count syscalls | |
| 59 | 63 | * | |
| 60 | 64 | * Compile with: | |
| 61 | 65 | * $ clang -g -Wall -Wextra count_timer_syscalls.c | |
| 62 | 66 | * | |
| 67 | + | * Written by Roland Mainz <roland.mainz@nrubsig.org> | |
| 63 | 69 | */ | |
| 64 | 70 | ||
| 65 | 71 | #include <sys/types.h> | |
| 66 | 72 | #include <sys/stat.h> | |
| 67 | 73 | #include <fcntl.h> | |
| 68 | 74 | #include <unistd.h> | |
| 69 | 75 | #include <time.h> | |
| 70 | 76 | #include <sched.h> | |
| 71 | 77 | #include <stdlib.h> | |
| 72 | 78 | #include <stdint.h> | |
| 73 | 79 | #include <stdio.h> | |
| 74 | 80 | #include <string.h> | |
| 75 | 81 | #include <errno.h> | |
| 76 | 82 | ||
| 77 | 83 | ||
| 78 | 84 | #ifndef CLOCK_INVALID | |
| 79 | 85 | #define CLOCK_INVALID -1 | |
| 80 | 86 | #endif | |
| 81 | 87 | ||
| 82 | 88 | #define NSEC_PER_SEC (1000000000LL) | |
| 83 | 89 | ||
| 84 | - | #define NUM_TESTRUNS (32000UL) | |
| 90 | + | #define NUM_TESTRUNS (${num_testruns}UL) | |
| 85 | 91 | ||
| 86 | 92 | ||
| 87 | 93 | static | |
| 88 | 94 | clockid_t get_clockid(int fd) | |
| 89 | 95 | { | |
| 90 | 96 | const int CLOCKFD = 3; | |
| 91 | 97 | ||
| 92 | 98 | return (((unsigned int) ~fd) << 3) | CLOCKFD; | |
| 93 | 99 | } | |
| 94 | 100 | ||
| 95 | 101 | ||
| 96 | 102 | static | |
| 97 | 103 | void test_loop_on_clock(clockid_t clkid, unsigned long num_cycles) | |
| 98 | 104 | { | |
| 99 | 105 | struct timespec ts; | |
| 100 | 106 | unsigned long cycles; | |
| 101 | 107 | ||
| 102 | 108 | for(cycles = 0UL ; cycles < num_cycles ; cycles++) | |
| 103 | 109 | { | |
| 104 | 110 | (void)clock_gettime(clkid, &ts); | |
| 105 | 111 | (void)ts; | |
| 106 | 112 | } | |
| 107 | 113 | } | |
| 108 | 114 | ||
| 109 | 115 | ||
| 110 | 116 | int main(int ac, char *av[]) | |
| 111 | 117 | { | |
| 112 | 118 | const char *clockname = av[1]; | |
| 113 | 119 | ||
| 114 | 120 | (void)puts("# start."); | |
| 115 | 121 | ||
| 116 | 122 | if ((ac != 2) || (!clockname)) { | |
| 117 | 123 | (void)fprintf(stderr, "%s: Missing clockname argument\n", av[0]); | |
| 118 | 124 | return EXIT_FAILURE; | |
| 119 | 125 | } | |
| 120 | 126 | ||
| 121 | 127 | if (!strcmp(clockname, "nic")) { | |
| 122 | 128 | int fd; | |
| 123 | 129 | clockid_t clkid; | |
| 124 | 130 | const char *devicename = "/dev/ptp0"; | |
| 125 | 131 | ||
| 126 | 132 | fd = open(devicename, O_RDWR); | |
| 127 | 133 | if (fd < 0) { | |
| 128 | 134 | (void)fprintf(stderr, "opening %s: %s\n", devicename, | |
| 129 | 135 | strerror(errno)); | |
| 130 | 136 | return(EXIT_FAILURE); | |
| 131 | 137 | } | |
| 132 | 138 | ||
| 133 | 139 | clkid = get_clockid(fd); | |
| 134 | 140 | if (CLOCK_INVALID == clkid) { | |
| 135 | 141 | (void)fprintf(stderr, "failed to read clock id\n"); | |
| 136 | 142 | return(EXIT_FAILURE); | |
| 137 | 143 | } | |
| 138 | 144 | ||
| 139 | 145 | (void)printf("## test run using nic clock\n"); | |
| 140 | 146 | test_loop_on_clock(clkid, NUM_TESTRUNS); | |
| 141 | 147 | ||
| 142 | 148 | (void)close(fd); | |
| 143 | 149 | EOF | |
| 144 | 150 | ||
| 145 | 151 | typeset i | |
| 146 | 152 | for i in "${!ar[@]}" ; do | |
| 147 | 153 | typeset name="${ar[$i].name}" | |
| 148 | 154 | printf '\t} else if (!strcmp(clockname, "%s")) {\n' "$name" | |
| 149 | 155 | printf '\t\t(void)printf("## test run using %s\\n");\n' "$name" | |
| 150 | 156 | printf '\n' | |
| 151 | 157 | printf '\t\ttest_loop_on_clock(%s, NUM_TESTRUNS);\n' "$name" | |
| 152 | 158 | done | |
| 153 | 159 | ||
| 154 | 160 | cat <<EOF | |
| 155 | 161 | } else { | |
| 156 | 162 | (void)fprintf(stderr, "%s: Unknown clockname argument %s\n", | |
| 157 | 163 | av[0], clockname); | |
| 158 | 164 | return EXIT_FAILURE; | |
| 159 | 165 | } | |
| 160 | 166 | ||
| 161 | 167 | (void)puts("# end."); | |
| 162 | 168 | ||
| 163 | 169 | return(EXIT_SUCCESS); | |
| 164 | 170 | } | |
| 165 | 171 | EOF | |
| 166 | 172 | } >"generated_count_timer_syscalls.c" | |
| 167 | 173 | ||
| 168 | 174 | return 0 | |
| 169 | 175 | } | |
| 170 | 176 | ||
| 171 | 177 | function compile_test_code | |
| 172 | 178 | { | |
| 173 | 179 | set -o errexit | |
| 174 | 180 | ||
| 175 | 181 | printf $"## Compiling test code...\n" | |
| 176 | 182 | ||
| 177 | 183 | clang -m32 -g -Wall -Wextra \ | |
| 178 | 184 | -o generated_count_timer_syscalls \ | |
| 179 | 185 | generated_count_timer_syscalls.c | |
| 180 | 186 | ||
| 181 | 187 | return 0 | |
| 182 | 188 | } | |
| 183 | 189 | ||
| 184 | 190 | function run_tests | |
| 185 | 191 | { | |
| 186 | 192 | nameref ar="$1" | |
| 187 | 193 | compound cmd | |
| 188 | 194 | typeset cmd.output | |
| 189 | 195 | integer cmd.exit_code | |
| 190 | 196 | typeset i | |
| 191 | 197 | ||
| 192 | 198 | printf $"## Running tests...\n" | |
| 193 | 199 | ||
| 194 | 200 | for i in "${!ar[@]}" ; do | |
| 195 | 201 | nameref n=ar["$i"] | |
| 196 | 202 | ||
| 197 | 203 | printf $"Probing %q...\n" "${n.name}" | |
| 198 | 204 | cmd.output="${ strace ./generated_count_timer_syscalls "${n.name}" 2>&1 ; cmd.exit_code="$?" ; }" | |
| 199 | 205 | ||
| 200 | 206 | integer n.num_syscalls_clock_gettime="${ egrep 'clock_gettime' <<<"${cmd.output}" | wc -l ;}" | |
| 201 | 207 | integer n.num_syscalls_total="${ wc -l <<<"${cmd.output}" ; }" | |
| 202 | 208 | integer n.exit_code=${cmd.exit_code} | |
| 203 | 209 | done | |
| 204 | 210 | } | |
| 205 | 211 | ||
| 206 | 212 | function print_results | |
| 207 | 213 | { | |
| 208 | 214 | nameref c=$1 | |
| 209 | - | printf $"## Raw test results:\n" | |
| 210 | - | print -v c | |
| 211 | 215 | ||
| 216 | + | set -o nounset | |
| 216 | 217 | ||
| 218 | + | printf $"## Raw test results:\n" | |
| 217 | 226 | printf $"Clock name %-20s \t" ${n.name} | |
| 218 | 227 | if (( (n.exit_code != 0) || (n.num_syscalls_total < 30) )) ; then | |
| 219 | 228 | printf $"TEST FAILED\n" | |
| 220 | - | elif (( n.num_syscalls_clock_gettime >= 32000 )) ; then | |
| 229 | + | elif (( n.num_syscalls_clock_gettime >= c.num_testruns )) ; then | |
| 221 | 230 | printf $"uses traditional syscalls\n" | |
| 222 | 231 | elif (( n.num_syscalls_clock_gettime <= 1 )) ; then | |
| 223 | 232 | printf $"uses shortcut\n" | |
| 224 | 233 | else | |
| 225 | 234 | printf $"Data inconclusive\n" | |
| 226 | 235 | fi | |
| 227 | 236 | done | |
| 228 | 237 | return 0 | |
| 229 | 238 | } | |
| 230 | 239 | ||
| 231 | 240 | function main | |
| 232 | 241 | { | |
| 233 | 242 | compound c | |
| 234 | 243 | compound -A c.clock_names | |
| 235 | 244 | ||
| 245 | + | integer c.num_testruns=32100 | |
| 236 | 246 | typeset c.uname="$(uname -a)" | |
| 237 | 247 | typeset c.hostname="$(hostname)" | |
| 238 | 248 | ||
| 239 | 249 | if ! enumerate_clock_sources c.clock_names ; then | |
| 240 | 250 | print -u2 -f $"%s: Could not enumerate clock names.\n" "$1" | |
| 241 | 251 | return 1 | |
| 242 | 252 | fi | |
| 243 | 253 | ||
| 244 | - | if ! generate_test_code c.clock_names ; then | |
| 254 | + | if ! generate_test_code c.clock_names ${c.num_testruns} ; then | |
| 245 | 255 | print -u2 -f $"%s: Could not generate test code.\n" "$1" | |
| 246 | 256 | return 1 | |
| 247 | 257 | fi | |
| 248 | 258 | ||
| 249 | 259 | if ! compile_test_code ; then | |
| 250 | 260 | print -u2 -f $"%s: Could not compile test code.\n" "$1" | |
| 251 | 261 | return 1 | |
| 252 | 262 | fi | |
| 253 | 263 | ||
| 254 | 264 | run_tests c.clock_names | |
| 255 | 265 | ||
| 256 | 266 | print_results c | |
| 257 | 267 | ||
| 258 | 268 | print $"## Done." | |
| 259 | 269 | return 0 | |
| 260 | 270 | } | |
| 261 | 271 | ||
| 262 | 272 | main "$@" | |
| 263 | 273 | # EOF. | |
| 264 | 274 |
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