dscr_default_test.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * POWER Data Stream Control Register (DSCR) default test
  3. *
  4. * This test modifies the system wide default DSCR through
  5. * it's sysfs interface and then verifies that all threads
  6. * see the correct changed DSCR value immediately.
  7. *
  8. * Copyright 2012, Anton Blanchard, IBM Corporation.
  9. * Copyright 2015, Anshuman Khandual, IBM Corporation.
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License version 2 as published
  13. * by the Free Software Foundation.
  14. */
  15. #include "dscr.h"
  16. static unsigned long dscr; /* System DSCR default */
  17. static unsigned long sequence;
  18. static unsigned long result[THREADS];
  19. static void *do_test(void *in)
  20. {
  21. unsigned long thread = (unsigned long)in;
  22. unsigned long i;
  23. for (i = 0; i < COUNT; i++) {
  24. unsigned long d, cur_dscr, cur_dscr_usr;
  25. unsigned long s1, s2;
  26. s1 = READ_ONCE(sequence);
  27. if (s1 & 1)
  28. continue;
  29. rmb();
  30. d = dscr;
  31. cur_dscr = get_dscr();
  32. cur_dscr_usr = get_dscr_usr();
  33. rmb();
  34. s2 = sequence;
  35. if (s1 != s2)
  36. continue;
  37. if (cur_dscr != d) {
  38. fprintf(stderr, "thread %ld kernel DSCR should be %ld "
  39. "but is %ld\n", thread, d, cur_dscr);
  40. result[thread] = 1;
  41. pthread_exit(&result[thread]);
  42. }
  43. if (cur_dscr_usr != d) {
  44. fprintf(stderr, "thread %ld user DSCR should be %ld "
  45. "but is %ld\n", thread, d, cur_dscr_usr);
  46. result[thread] = 1;
  47. pthread_exit(&result[thread]);
  48. }
  49. }
  50. result[thread] = 0;
  51. pthread_exit(&result[thread]);
  52. }
  53. int dscr_default(void)
  54. {
  55. pthread_t threads[THREADS];
  56. unsigned long i, *status[THREADS];
  57. unsigned long orig_dscr_default;
  58. orig_dscr_default = get_default_dscr();
  59. /* Initial DSCR default */
  60. dscr = 1;
  61. set_default_dscr(dscr);
  62. /* Spawn all testing threads */
  63. for (i = 0; i < THREADS; i++) {
  64. if (pthread_create(&threads[i], NULL, do_test, (void *)i)) {
  65. perror("pthread_create() failed");
  66. goto fail;
  67. }
  68. }
  69. srand(getpid());
  70. /* Keep changing the DSCR default */
  71. for (i = 0; i < COUNT; i++) {
  72. double ret = uniform_deviate(rand());
  73. if (ret < 0.0001) {
  74. sequence++;
  75. wmb();
  76. dscr++;
  77. if (dscr > DSCR_MAX)
  78. dscr = 0;
  79. set_default_dscr(dscr);
  80. wmb();
  81. sequence++;
  82. }
  83. }
  84. /* Individual testing thread exit status */
  85. for (i = 0; i < THREADS; i++) {
  86. if (pthread_join(threads[i], (void **)&(status[i]))) {
  87. perror("pthread_join() failed");
  88. goto fail;
  89. }
  90. if (*status[i]) {
  91. printf("%ldth thread failed to join with %ld status\n",
  92. i, *status[i]);
  93. goto fail;
  94. }
  95. }
  96. set_default_dscr(orig_dscr_default);
  97. return 0;
  98. fail:
  99. set_default_dscr(orig_dscr_default);
  100. return 1;
  101. }
  102. int main(int argc, char *argv[])
  103. {
  104. return test_harness(dscr_default, "dscr_default_test");
  105. }