Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests: timers: Convert posix_timers test to generate KTAP output

Currently the posix_timers test does not produce KTAP output but rather a
custom format. This means that we only get a pass/fail for the suite, not
for each individual test that the suite does. Convert to using the standard
kselftest output functions which result in KTAP output being generated.

As part of this fix the printing of diagnostics in the unlikely event that
the pthread APIs fail, these were using perror() but the API functions
directly return an error code instead of setting errno.

Signed-off-by: Mark Brown <broonie@kernel.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Mark Brown and committed by
Shuah Khan
071af0c9 47903c1d

+41 -40
+41 -40
tools/testing/selftests/timers/posix_timers.c
··· 76 76 77 77 static int check_itimer(int which) 78 78 { 79 + const char *name; 79 80 int err; 80 81 struct timeval start, end; 81 82 struct itimerval val = { 82 83 .it_value.tv_sec = DELAY, 83 84 }; 84 85 85 - printf("Check itimer "); 86 - 87 86 if (which == ITIMER_VIRTUAL) 88 - printf("virtual... "); 87 + name = "ITIMER_VIRTUAL"; 89 88 else if (which == ITIMER_PROF) 90 - printf("prof... "); 89 + name = "ITIMER_PROF"; 91 90 else if (which == ITIMER_REAL) 92 - printf("real... "); 93 - 94 - fflush(stdout); 91 + name = "ITIMER_REAL"; 92 + else 93 + return -1; 95 94 96 95 done = 0; 97 96 ··· 103 104 104 105 err = gettimeofday(&start, NULL); 105 106 if (err < 0) { 106 - perror("Can't call gettimeofday()\n"); 107 + ksft_perror("Can't call gettimeofday()"); 107 108 return -1; 108 109 } 109 110 110 111 err = setitimer(which, &val, NULL); 111 112 if (err < 0) { 112 - perror("Can't set timer\n"); 113 + ksft_perror("Can't set timer"); 113 114 return -1; 114 115 } 115 116 ··· 122 123 123 124 err = gettimeofday(&end, NULL); 124 125 if (err < 0) { 125 - perror("Can't call gettimeofday()\n"); 126 + ksft_perror("Can't call gettimeofday()"); 126 127 return -1; 127 128 } 128 129 129 - if (!check_diff(start, end)) 130 - printf("[OK]\n"); 131 - else 132 - printf("[FAIL]\n"); 130 + ksft_test_result(check_diff(start, end) == 0, "%s\n", name); 133 131 134 132 return 0; 135 133 } 136 134 137 135 static int check_timer_create(int which) 138 136 { 137 + const char *type; 139 138 int err; 140 139 timer_t id; 141 140 struct timeval start, end; ··· 141 144 .it_value.tv_sec = DELAY, 142 145 }; 143 146 144 - printf("Check timer_create() "); 145 147 if (which == CLOCK_THREAD_CPUTIME_ID) { 146 - printf("per thread... "); 148 + type = "thread"; 147 149 } else if (which == CLOCK_PROCESS_CPUTIME_ID) { 148 - printf("per process... "); 150 + type = "process"; 151 + } else { 152 + ksft_print_msg("Unknown timer_create() type %d\n", which); 153 + return -1; 149 154 } 150 - fflush(stdout); 151 155 152 156 done = 0; 153 157 err = timer_create(which, NULL, &id); 154 158 if (err < 0) { 155 - perror("Can't create timer\n"); 159 + ksft_perror("Can't create timer"); 156 160 return -1; 157 161 } 158 162 signal(SIGALRM, sig_handler); 159 163 160 164 err = gettimeofday(&start, NULL); 161 165 if (err < 0) { 162 - perror("Can't call gettimeofday()\n"); 166 + ksft_perror("Can't call gettimeofday()"); 163 167 return -1; 164 168 } 165 169 166 170 err = timer_settime(id, 0, &val, NULL); 167 171 if (err < 0) { 168 - perror("Can't set timer\n"); 172 + ksft_perror("Can't set timer"); 169 173 return -1; 170 174 } 171 175 ··· 174 176 175 177 err = gettimeofday(&end, NULL); 176 178 if (err < 0) { 177 - perror("Can't call gettimeofday()\n"); 179 + ksft_perror("Can't call gettimeofday()"); 178 180 return -1; 179 181 } 180 182 181 - if (!check_diff(start, end)) 182 - printf("[OK]\n"); 183 - else 184 - printf("[FAIL]\n"); 183 + ksft_test_result(check_diff(start, end) == 0, 184 + "timer_create() per %s\n", type); 185 185 186 186 return 0; 187 187 } ··· 216 220 .it_interval.tv_nsec = 1000 * 1000, 217 221 }; 218 222 219 - printf("Check timer_create() per process signal distribution... "); 220 - fflush(stdout); 221 - 222 223 remain = nthreads + 1; /* worker threads + this thread */ 223 224 signal(SIGALRM, distribution_handler); 224 225 err = timer_create(CLOCK_PROCESS_CPUTIME_ID, NULL, &id); 225 226 if (err < 0) { 226 - perror("Can't create timer\n"); 227 + ksft_perror("Can't create timer"); 227 228 return -1; 228 229 } 229 230 err = timer_settime(id, 0, &val, NULL); 230 231 if (err < 0) { 231 - perror("Can't set timer\n"); 232 + ksft_perror("Can't set timer"); 232 233 return -1; 233 234 } 234 235 235 236 for (i = 0; i < nthreads; i++) { 236 - if (pthread_create(&threads[i], NULL, distribution_thread, NULL)) { 237 - perror("Can't create thread\n"); 237 + err = pthread_create(&threads[i], NULL, distribution_thread, 238 + NULL); 239 + if (err) { 240 + ksft_print_msg("Can't create thread: %s (%d)\n", 241 + strerror(errno), errno); 238 242 return -1; 239 243 } 240 244 } ··· 243 247 while (__atomic_load_n(&remain, __ATOMIC_RELAXED)); 244 248 245 249 for (i = 0; i < nthreads; i++) { 246 - if (pthread_join(threads[i], NULL)) { 247 - perror("Can't join thread\n"); 250 + err = pthread_join(threads[i], NULL); 251 + if (err) { 252 + ksft_print_msg("Can't join thread: %s (%d)\n", 253 + strerror(errno), errno); 248 254 return -1; 249 255 } 250 256 } 251 257 252 258 if (timer_delete(id)) { 253 - perror("Can't delete timer\n"); 259 + ksft_perror("Can't delete timer"); 254 260 return -1; 255 261 } 256 262 257 - printf("[OK]\n"); 263 + ksft_test_result_pass("check_timer_distribution\n"); 258 264 return 0; 259 265 } 260 266 261 267 int main(int argc, char **argv) 262 268 { 263 - printf("Testing posix timers. False negative may happen on CPU execution \n"); 264 - printf("based timers if other threads run on the CPU...\n"); 269 + ksft_print_header(); 270 + ksft_set_plan(6); 271 + 272 + ksft_print_msg("Testing posix timers. False negative may happen on CPU execution \n"); 273 + ksft_print_msg("based timers if other threads run on the CPU...\n"); 265 274 266 275 if (check_itimer(ITIMER_VIRTUAL) < 0) 267 276 return ksft_exit_fail(); ··· 295 294 if (check_timer_distribution() < 0) 296 295 return ksft_exit_fail(); 297 296 298 - return ksft_exit_pass(); 297 + ksft_finished(); 299 298 }