vmx_preempt.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 VMX registers change across preemption.
  10. * Two things should be noted here a) The check_vmx 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 vector int varray[] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10,11,12},
  33. {13,14,15,16},{17,18,19,20},{21,22,23,24},
  34. {25,26,27,28},{29,30,31,32},{33,34,35,36},
  35. {37,38,39,40},{41,42,43,44},{45,46,47,48}};
  36. int threads_starting;
  37. int running;
  38. extern void preempt_vmx(vector int *varray, int *threads_starting, int *running);
  39. void *preempt_vmx_c(void *p)
  40. {
  41. int i, j;
  42. srand(pthread_self());
  43. for (i = 0; i < 12; i++)
  44. for (j = 0; j < 4; j++)
  45. varray[i][j] = rand();
  46. /* Test fails if it ever returns */
  47. preempt_vmx(varray, &threads_starting, &running);
  48. return p;
  49. }
  50. int test_preempt_vmx(void)
  51. {
  52. int i, rc, threads;
  53. pthread_t *tids;
  54. threads = sysconf(_SC_NPROCESSORS_ONLN) * THREAD_FACTOR;
  55. tids = malloc(threads * sizeof(pthread_t));
  56. FAIL_IF(!tids);
  57. running = true;
  58. threads_starting = threads;
  59. for (i = 0; i < threads; i++) {
  60. rc = pthread_create(&tids[i], NULL, preempt_vmx_c, NULL);
  61. FAIL_IF(rc);
  62. }
  63. setbuf(stdout, NULL);
  64. /* Not really nessesary but nice to wait for every thread to start */
  65. printf("\tWaiting for all workers to start...");
  66. while(threads_starting)
  67. asm volatile("": : :"memory");
  68. printf("done\n");
  69. printf("\tWaiting for %d seconds to let some workers get preempted...", PREEMPT_TIME);
  70. sleep(PREEMPT_TIME);
  71. printf("done\n");
  72. printf("\tStopping workers...");
  73. /*
  74. * Working are checking this value every loop. In preempt_vmx 'cmpwi r5,0; bne 2b'.
  75. * r5 will have loaded the value of running.
  76. */
  77. running = 0;
  78. for (i = 0; i < threads; i++) {
  79. void *rc_p;
  80. pthread_join(tids[i], &rc_p);
  81. /*
  82. * Harness will say the fail was here, look at why preempt_vmx
  83. * returned
  84. */
  85. if ((long) rc_p)
  86. printf("oops\n");
  87. FAIL_IF((long) rc_p);
  88. }
  89. printf("done\n");
  90. return 0;
  91. }
  92. int main(int argc, char *argv[])
  93. {
  94. return test_harness(test_preempt_vmx, "vmx_preempt");
  95. }