clocksource-switch.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #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 (ret)
  91. return ret;
  92. ret = system("./nanosleep");
  93. return ret;
  94. }
  95. char clocksource_list[10][30];
  96. int main(int argv, char **argc)
  97. {
  98. char orig_clk[512];
  99. int count, i, status;
  100. pid_t pid;
  101. get_cur_clocksource(orig_clk, 512);
  102. count = get_clocksources(clocksource_list);
  103. if (change_clocksource(clocksource_list[0])) {
  104. printf("Error: You probably need to run this as root\n");
  105. return -1;
  106. }
  107. /* Check everything is sane before we start switching asyncrhonously */
  108. for (i = 0; i < count; i++) {
  109. printf("Validating clocksource %s\n", clocksource_list[i]);
  110. if (change_clocksource(clocksource_list[i])) {
  111. status = -1;
  112. goto out;
  113. }
  114. if (run_tests(5)) {
  115. status = -1;
  116. goto out;
  117. }
  118. }
  119. printf("Running Asynchronous Switching Tests...\n");
  120. pid = fork();
  121. if (!pid)
  122. return run_tests(60);
  123. while (pid != waitpid(pid, &status, WNOHANG))
  124. for (i = 0; i < count; i++)
  125. if (change_clocksource(clocksource_list[i])) {
  126. status = -1;
  127. goto out;
  128. }
  129. out:
  130. change_clocksource(orig_clk);
  131. if (status)
  132. return ksft_exit_fail();
  133. return ksft_exit_pass();
  134. }