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