fpu_preempt.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2015, Cyril Bur, IBM Corp.
  4. * Copyright 2023, Michael Ellerman, IBM Corp.
  5. *
  6. * This test attempts to see if the FPU registers change across preemption.
  7. * There is no way to be sure preemption happened so this test just uses many
  8. * threads and a long wait. As such, a successful test doesn't mean much but
  9. * a failure is bad.
  10. */
  11. #include <stdio.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. #include "fpu.h"
  21. /* Time to wait for workers to get preempted (seconds) */
  22. #define PREEMPT_TIME 60
  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 double darray[32];
  29. int threads_starting;
  30. int running;
  31. extern int preempt_fpu(double *darray, int *threads_starting, int *running);
  32. void *preempt_fpu_c(void *p)
  33. {
  34. long rc;
  35. srand(pthread_self());
  36. randomise_darray(darray, ARRAY_SIZE(darray));
  37. rc = preempt_fpu(darray, &threads_starting, &running);
  38. return (void *)rc;
  39. }
  40. int test_preempt_fpu(void)
  41. {
  42. int i, rc, threads;
  43. pthread_t *tids;
  44. threads = sysconf(_SC_NPROCESSORS_ONLN) * THREAD_FACTOR;
  45. tids = malloc((threads) * sizeof(pthread_t));
  46. FAIL_IF(!tids);
  47. running = true;
  48. threads_starting = threads;
  49. for (i = 0; i < threads; i++) {
  50. rc = pthread_create(&tids[i], NULL, preempt_fpu_c, NULL);
  51. FAIL_IF(rc);
  52. }
  53. setbuf(stdout, NULL);
  54. /* Not really necessary but nice to wait for every thread to start */
  55. printf("\tWaiting for all workers to start...");
  56. while(threads_starting)
  57. asm volatile("": : :"memory");
  58. printf("done\n");
  59. printf("\tWaiting for %d seconds to let some workers get preempted...", PREEMPT_TIME);
  60. sleep(PREEMPT_TIME);
  61. printf("done\n");
  62. printf("\tStopping workers...");
  63. /*
  64. * Working are checking this value every loop. In preempt_fpu 'cmpwi r5,0; bne 2b'.
  65. * r5 will have loaded the value of running.
  66. */
  67. running = 0;
  68. for (i = 0; i < threads; i++) {
  69. void *rc_p;
  70. pthread_join(tids[i], &rc_p);
  71. /*
  72. * Harness will say the fail was here, look at why preempt_fpu
  73. * returned
  74. */
  75. if ((long) rc_p)
  76. printf("oops\n");
  77. FAIL_IF((long) rc_p);
  78. }
  79. printf("done\n");
  80. free(tids);
  81. return 0;
  82. }
  83. int main(int argc, char *argv[])
  84. {
  85. return test_harness(test_preempt_fpu, "fpu_preempt");
  86. }