fpu_preempt.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 FPU registers change across preemption.
  10. * Two things should be noted here a) The check_fpu function in asm only checks
  11. * the non volatile registers as it is reused from the syscall test b) There is
  12. * no way to be sure preemption happened so this test just uses many threads
  13. * and a long wait. As such, a successful test doesn't mean much but a failure
  14. * is bad.
  15. */
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <sys/syscall.h>
  19. #include <sys/time.h>
  20. #include <sys/types.h>
  21. #include <sys/wait.h>
  22. #include <stdlib.h>
  23. #include <pthread.h>
  24. #include "utils.h"
  25. /* Time to wait for workers to get preempted (seconds) */
  26. #define PREEMPT_TIME 20
  27. /*
  28. * Factor by which to multiply number of online CPUs for total number of
  29. * worker threads
  30. */
  31. #define THREAD_FACTOR 8
  32. __thread double darray[] = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,
  33. 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0,
  34. 2.1};
  35. int threads_starting;
  36. int running;
  37. extern void preempt_fpu(double *darray, int *threads_starting, int *running);
  38. void *preempt_fpu_c(void *p)
  39. {
  40. int i;
  41. srand(pthread_self());
  42. for (i = 0; i < 21; i++)
  43. darray[i] = rand();
  44. /* Test failed if it ever returns */
  45. preempt_fpu(darray, &threads_starting, &running);
  46. return p;
  47. }
  48. int test_preempt_fpu(void)
  49. {
  50. int i, rc, threads;
  51. pthread_t *tids;
  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_fpu_c, NULL);
  59. FAIL_IF(rc);
  60. }
  61. setbuf(stdout, NULL);
  62. /* Not really necessary 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_fpu '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_fpu
  81. * returned
  82. */
  83. if ((long) rc_p)
  84. printf("oops\n");
  85. FAIL_IF((long) rc_p);
  86. }
  87. printf("done\n");
  88. free(tids);
  89. return 0;
  90. }
  91. int main(int argc, char *argv[])
  92. {
  93. return test_harness(test_preempt_fpu, "fpu_preempt");
  94. }