clocksource-switch.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* Clocksource change test
  2. * by: john stultz (johnstul@us.ibm.com)
  3. * (C) Copyright IBM 2012
  4. * Licensed under the GPLv2
  5. *
  6. * NOTE: This is a meta-test which quickly changes the clocksourc 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 <stdio.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <sys/time.h>
  28. #include <sys/timex.h>
  29. #include <time.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <fcntl.h>
  33. #include <string.h>
  34. #include <sys/wait.h>
  35. #ifdef KTEST
  36. #include "../kselftest.h"
  37. #else
  38. static inline int ksft_exit_pass(void)
  39. {
  40. exit(0);
  41. }
  42. static inline int ksft_exit_fail(void)
  43. {
  44. exit(1);
  45. }
  46. #endif
  47. int get_clocksources(char list[][30])
  48. {
  49. int fd, i;
  50. size_t size;
  51. char buf[512];
  52. char *head, *tmp;
  53. fd = open("/sys/devices/system/clocksource/clocksource0/available_clocksource", O_RDONLY);
  54. size = read(fd, buf, 512);
  55. close(fd);
  56. for (i = 0; i < 30; i++)
  57. list[i][0] = '\0';
  58. head = buf;
  59. i = 0;
  60. while (head - buf < size) {
  61. /* Find the next space */
  62. for (tmp = head; *tmp != ' '; tmp++) {
  63. if (*tmp == '\n')
  64. break;
  65. if (*tmp == '\0')
  66. break;
  67. }
  68. *tmp = '\0';
  69. strcpy(list[i], head);
  70. head = tmp + 1;
  71. i++;
  72. }
  73. return i-1;
  74. }
  75. int get_cur_clocksource(char *buf, size_t size)
  76. {
  77. int fd;
  78. fd = open("/sys/devices/system/clocksource/clocksource0/current_clocksource", O_RDONLY);
  79. size = read(fd, buf, size);
  80. return 0;
  81. }
  82. int change_clocksource(char *clocksource)
  83. {
  84. int fd;
  85. ssize_t size;
  86. fd = open("/sys/devices/system/clocksource/clocksource0/current_clocksource", O_WRONLY);
  87. if (fd < 0)
  88. return -1;
  89. size = write(fd, clocksource, strlen(clocksource));
  90. if (size < 0)
  91. return -1;
  92. close(fd);
  93. return 0;
  94. }
  95. int run_tests(int secs)
  96. {
  97. int ret;
  98. char buf[255];
  99. sprintf(buf, "./inconsistency-check -t %i", secs);
  100. ret = system(buf);
  101. if (ret)
  102. return ret;
  103. ret = system("./nanosleep");
  104. return ret;
  105. }
  106. char clocksource_list[10][30];
  107. int main(int argv, char **argc)
  108. {
  109. char orig_clk[512];
  110. int count, i, status;
  111. pid_t pid;
  112. get_cur_clocksource(orig_clk, 512);
  113. count = get_clocksources(clocksource_list);
  114. if (change_clocksource(clocksource_list[0])) {
  115. printf("Error: You probably need to run this as root\n");
  116. return -1;
  117. }
  118. /* Check everything is sane before we start switching asyncrhonously */
  119. for (i = 0; i < count; i++) {
  120. printf("Validating clocksource %s\n", clocksource_list[i]);
  121. if (change_clocksource(clocksource_list[i])) {
  122. status = -1;
  123. goto out;
  124. }
  125. if (run_tests(5)) {
  126. status = -1;
  127. goto out;
  128. }
  129. }
  130. printf("Running Asyncrhonous Switching Tests...\n");
  131. pid = fork();
  132. if (!pid)
  133. return run_tests(60);
  134. while (pid != waitpid(pid, &status, WNOHANG))
  135. for (i = 0; i < count; i++)
  136. if (change_clocksource(clocksource_list[i])) {
  137. status = -1;
  138. goto out;
  139. }
  140. out:
  141. change_clocksource(orig_clk);
  142. if (status)
  143. return ksft_exit_fail();
  144. return ksft_exit_pass();
  145. }