vsx_preempt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2015, Cyril Bur, IBM Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * This test attempts to see if the VSX registers change across preemption.
  10. * There is no way to be sure preemption happened so this test just
  11. * uses many threads and a long wait. As such, a successful test
  12. * doesn't mean much but a failure is bad.
  13. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <unistd.h>
  17. #include <sys/syscall.h>
  18. #include <sys/time.h>
  19. #include <sys/types.h>
  20. #include <sys/wait.h>
  21. #include <stdlib.h>
  22. #include <pthread.h>
  23. #include "utils.h"
  24. /* Time to wait for workers to get preempted (seconds) */
  25. #define PREEMPT_TIME 20
  26. /*
  27. * Factor by which to multiply number of online CPUs for total number of
  28. * worker threads
  29. */
  30. #define THREAD_FACTOR 8
  31. /*
  32. * Ensure there is twice the number of non-volatile VMX regs!
  33. * check_vmx() is going to use the other half as space to put the live
  34. * registers before calling vsx_memcmp()
  35. */
  36. __thread vector int varray[24] = {
  37. {1, 2, 3, 4 }, {5, 6, 7, 8 }, {9, 10,11,12},
  38. {13,14,15,16}, {17,18,19,20}, {21,22,23,24},
  39. {25,26,27,28}, {29,30,31,32}, {33,34,35,36},
  40. {37,38,39,40}, {41,42,43,44}, {45,46,47,48}
  41. };
  42. int threads_starting;
  43. int running;
  44. extern long preempt_vsx(vector int *varray, int *threads_starting, int *running);
  45. long vsx_memcmp(vector int *a) {
  46. vector int zero = {0, 0, 0, 0};
  47. int i;
  48. FAIL_IF(a != varray);
  49. for(i = 0; i < 12; i++) {
  50. if (memcmp(&a[i + 12], &zero, sizeof(vector int)) == 0) {
  51. fprintf(stderr, "Detected zero from the VSX reg %d\n", i + 12);
  52. return 2;
  53. }
  54. }
  55. if (memcmp(a, &a[12], 12 * sizeof(vector int))) {
  56. long *p = (long *)a;
  57. fprintf(stderr, "VSX mismatch\n");
  58. for (i = 0; i < 24; i=i+2)
  59. fprintf(stderr, "%d: 0x%08lx%08lx | 0x%08lx%08lx\n",
  60. i/2 + i%2 + 20, p[i], p[i + 1], p[i + 24], p[i + 25]);
  61. return 1;
  62. }
  63. return 0;
  64. }
  65. void *preempt_vsx_c(void *p)
  66. {
  67. int i, j;
  68. long rc;
  69. srand(pthread_self());
  70. for (i = 0; i < 12; i++)
  71. for (j = 0; j < 4; j++) {
  72. varray[i][j] = rand();
  73. /* Don't want zero because it hides kernel problems */
  74. if (varray[i][j] == 0)
  75. j--;
  76. }
  77. rc = preempt_vsx(varray, &threads_starting, &running);
  78. if (rc == 2)
  79. fprintf(stderr, "Caught zeros in VSX compares\n");
  80. return (void *)rc;
  81. }
  82. int test_preempt_vsx(void)
  83. {
  84. int i, rc, threads;
  85. pthread_t *tids;
  86. threads = sysconf(_SC_NPROCESSORS_ONLN) * THREAD_FACTOR;
  87. tids = malloc(threads * sizeof(pthread_t));
  88. FAIL_IF(!tids);
  89. running = true;
  90. threads_starting = threads;
  91. for (i = 0; i < threads; i++) {
  92. rc = pthread_create(&tids[i], NULL, preempt_vsx_c, NULL);
  93. FAIL_IF(rc);
  94. }
  95. setbuf(stdout, NULL);
  96. /* Not really nessesary but nice to wait for every thread to start */
  97. printf("\tWaiting for %d workers to start...", threads_starting);
  98. while(threads_starting)
  99. asm volatile("": : :"memory");
  100. printf("done\n");
  101. printf("\tWaiting for %d seconds to let some workers get preempted...", PREEMPT_TIME);
  102. sleep(PREEMPT_TIME);
  103. printf("done\n");
  104. printf("\tStopping workers...");
  105. /*
  106. * Working are checking this value every loop. In preempt_vsx 'cmpwi r5,0; bne 2b'.
  107. * r5 will have loaded the value of running.
  108. */
  109. running = 0;
  110. for (i = 0; i < threads; i++) {
  111. void *rc_p;
  112. pthread_join(tids[i], &rc_p);
  113. /*
  114. * Harness will say the fail was here, look at why preempt_vsx
  115. * returned
  116. */
  117. if ((long) rc_p)
  118. printf("oops\n");
  119. FAIL_IF((long) rc_p);
  120. }
  121. printf("done\n");
  122. return 0;
  123. }
  124. int main(int argc, char *argv[])
  125. {
  126. return test_harness(test_preempt_vsx, "vsx_preempt");
  127. }