clocksource-switch.c 4.0 KB

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