vmx_preempt.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 VMX registers change across preemption.
  6. * Two things should be noted here a) The check_vmx function in asm only checks
  7. * the non volatile registers as it is reused from the syscall test b) There is
  8. * no way to be sure preemption happened so this test just uses many threads
  9. * and a long wait. As such, a successful test doesn't mean much but a failure
  10. * is bad.
  11. */
  12. #include <stdio.h>
  13. #include <unistd.h>
  14. #include <sys/syscall.h>
  15. #include <sys/time.h>
  16. #include <sys/types.h>
  17. #include <sys/wait.h>
  18. #include <stdlib.h>
  19. #include <pthread.h>
  20. #include "utils.h"
  21. /* Time to wait for workers to get preempted (seconds) */
  22. #define PREEMPT_TIME 20
  23. /*
  24. * Factor by which to multiply number of online CPUs for total number of
  25. * worker threads
  26. */
  27. #define THREAD_FACTOR 8
  28. __thread vector int varray[] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10,11,12},
  29. {13,14,15,16},{17,18,19,20},{21,22,23,24},
  30. {25,26,27,28},{29,30,31,32},{33,34,35,36},
  31. {37,38,39,40},{41,42,43,44},{45,46,47,48}};
  32. int threads_starting;
  33. int running;
  34. extern int preempt_vmx(vector int *varray, int *threads_starting, int *running);
  35. void *preempt_vmx_c(void *p)
  36. {
  37. int i, j;
  38. long rc;
  39. srand(pthread_self());
  40. for (i = 0; i < 12; i++)
  41. for (j = 0; j < 4; j++)
  42. varray[i][j] = rand();
  43. rc = preempt_vmx(varray, &threads_starting, &running);
  44. return (void *)rc;
  45. }
  46. int test_preempt_vmx(void)
  47. {
  48. int i, rc, threads;
  49. pthread_t *tids;
  50. // vcmpequd used in vmx_asm.S is v2.07
  51. SKIP_IF(!have_hwcap2(PPC_FEATURE2_ARCH_2_07));
  52. threads = sysconf(_SC_NPROCESSORS_ONLN) * THREAD_FACTOR;
  53. tids = malloc(threads * sizeof(pthread_t));
  54. FAIL_IF(!tids);
  55. running = true;
  56. threads_starting = threads;
  57. for (i = 0; i < threads; i++) {
  58. rc = pthread_create(&tids[i], NULL, preempt_vmx_c, NULL);
  59. FAIL_IF(rc);
  60. }
  61. setbuf(stdout, NULL);
  62. /* Not really nessesary but nice to wait for every thread to start */
  63. printf("\tWaiting for all workers to start...");
  64. while(threads_starting)
  65. asm volatile("": : :"memory");
  66. printf("done\n");
  67. printf("\tWaiting for %d seconds to let some workers get preempted...", PREEMPT_TIME);
  68. sleep(PREEMPT_TIME);
  69. printf("done\n");
  70. printf("\tStopping workers...");
  71. /*
  72. * Working are checking this value every loop. In preempt_vmx 'cmpwi r5,0; bne 2b'.
  73. * r5 will have loaded the value of running.
  74. */
  75. running = 0;
  76. for (i = 0; i < threads; i++) {
  77. void *rc_p;
  78. pthread_join(tids[i], &rc_p);
  79. /*
  80. * Harness will say the fail was here, look at why preempt_vmx
  81. * returned
  82. */
  83. if ((long) rc_p)
  84. printf("oops\n");
  85. FAIL_IF((long) rc_p);
  86. }
  87. printf("done\n");
  88. return 0;
  89. }
  90. int main(int argc, char *argv[])
  91. {
  92. return test_harness(test_preempt_vmx, "vmx_preempt");
  93. }