change_skew.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* ADJ_FREQ Skew 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 cranks the ADJ_FREQ knob and
  7. * then uses other tests to detect problems. Thus this test requires
  8. * that the raw_skew, inconsistency-check and nanosleep tests be
  9. * present in the same directory it is run from.
  10. *
  11. * To build:
  12. * $ gcc change_skew.c -o change_skew -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 <stdlib.h>
  26. #include <sys/time.h>
  27. #include <sys/timex.h>
  28. #include <time.h>
  29. #ifdef KTEST
  30. #include "../kselftest.h"
  31. #else
  32. static inline int ksft_exit_pass(void)
  33. {
  34. exit(0);
  35. }
  36. static inline int ksft_exit_fail(void)
  37. {
  38. exit(1);
  39. }
  40. #endif
  41. #define NSEC_PER_SEC 1000000000LL
  42. int change_skew_test(int ppm)
  43. {
  44. struct timex tx;
  45. int ret;
  46. tx.modes = ADJ_FREQUENCY;
  47. tx.freq = ppm << 16;
  48. ret = adjtimex(&tx);
  49. if (ret < 0) {
  50. printf("Error adjusting freq\n");
  51. return ret;
  52. }
  53. ret = system("./raw_skew");
  54. ret |= system("./inconsistency-check");
  55. ret |= system("./nanosleep");
  56. return ret;
  57. }
  58. int main(int argv, char **argc)
  59. {
  60. struct timex tx;
  61. int i, ret;
  62. int ppm[5] = {0, 250, 500, -250, -500};
  63. /* Kill ntpd */
  64. ret = system("killall -9 ntpd");
  65. /* Make sure there's no offset adjustment going on */
  66. tx.modes = ADJ_OFFSET;
  67. tx.offset = 0;
  68. ret = adjtimex(&tx);
  69. if (ret < 0) {
  70. printf("Maybe you're not running as root?\n");
  71. return -1;
  72. }
  73. for (i = 0; i < 5; i++) {
  74. printf("Using %i ppm adjustment\n", ppm[i]);
  75. ret = change_skew_test(ppm[i]);
  76. if (ret)
  77. break;
  78. }
  79. /* Set things back */
  80. tx.modes = ADJ_FREQUENCY;
  81. tx.offset = 0;
  82. adjtimex(&tx);
  83. if (ret) {
  84. printf("[FAIL]");
  85. return ksft_exit_fail();
  86. }
  87. printf("[OK]");
  88. return ksft_exit_pass();
  89. }