vsx_preempt.c 3.5 KB

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