^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* Clocksource change test
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * by: john stultz (johnstul@us.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * (C) Copyright IBM 2012
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Licensed under the GPLv2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * NOTE: This is a meta-test which quickly changes the clocksourc and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * then uses other tests to detect problems. Thus this test requires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * that the inconsistency-check and nanosleep tests be present in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * same directory it is run from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * To build:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * $ gcc clocksource-switch.c -o clocksource-switch -lrt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * This program is free software: you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * it under the terms of the GNU General Public License as published by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * the Free Software Foundation, either version 2 of the License, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * (at your option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <sys/time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <sys/timex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <sys/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include "../kselftest.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int get_clocksources(char list[][30])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int fd, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) char buf[512];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) char *head, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) fd = open("/sys/devices/system/clocksource/clocksource0/available_clocksource", O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) size = read(fd, buf, 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) for (i = 0; i < 10; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) list[i][0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) head = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) while (head - buf < size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* Find the next space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) for (tmp = head; *tmp != ' '; tmp++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (*tmp == '\n')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (*tmp == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) *tmp = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) strcpy(list[i], head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) head = tmp + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return i-1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int get_cur_clocksource(char *buf, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) fd = open("/sys/devices/system/clocksource/clocksource0/current_clocksource", O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) size = read(fd, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int change_clocksource(char *clocksource)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ssize_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) fd = open("/sys/devices/system/clocksource/clocksource0/current_clocksource", O_WRONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) size = write(fd, clocksource, strlen(clocksource));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (size < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int run_tests(int secs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) char buf[255];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) sprintf(buf, "./inconsistency-check -t %i", secs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) ret = system(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) ret = system("./nanosleep");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) char clocksource_list[10][30];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) int main(int argv, char **argc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) char orig_clk[512];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int count, i, status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) pid_t pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) get_cur_clocksource(orig_clk, 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) count = get_clocksources(clocksource_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (change_clocksource(clocksource_list[0])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) printf("Error: You probably need to run this as root\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) /* Check everything is sane before we start switching asyncrhonously */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) printf("Validating clocksource %s\n", clocksource_list[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (change_clocksource(clocksource_list[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) status = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (run_tests(5)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) status = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) printf("Running Asynchronous Switching Tests...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) pid = fork();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (!pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return run_tests(60);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) while (pid != waitpid(pid, &status, WNOHANG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) for (i = 0; i < count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (change_clocksource(clocksource_list[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) status = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) change_clocksource(orig_clk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return ksft_exit_fail();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return ksft_exit_pass();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }