Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright 2015, Cyril Bur, IBM Corp.
0004  *
0005  * This test attempts to see if the VMX registers change across preemption.
0006  * Two things should be noted here a) The check_vmx function in asm only checks
0007  * the non volatile registers as it is reused from the syscall test b) There is
0008  * no way to be sure preemption happened so this test just uses many threads
0009  * and a long wait. As such, a successful test doesn't mean much but a failure
0010  * is bad.
0011  */
0012 
0013 #include <stdio.h>
0014 #include <unistd.h>
0015 #include <sys/syscall.h>
0016 #include <sys/time.h>
0017 #include <sys/types.h>
0018 #include <sys/wait.h>
0019 #include <stdlib.h>
0020 #include <pthread.h>
0021 
0022 #include "utils.h"
0023 
0024 /* Time to wait for workers to get preempted (seconds) */
0025 #define PREEMPT_TIME 20
0026 /*
0027  * Factor by which to multiply number of online CPUs for total number of
0028  * worker threads
0029  */
0030 #define THREAD_FACTOR 8
0031 
0032 __thread vector int varray[] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10,11,12},
0033     {13,14,15,16},{17,18,19,20},{21,22,23,24},
0034     {25,26,27,28},{29,30,31,32},{33,34,35,36},
0035     {37,38,39,40},{41,42,43,44},{45,46,47,48}};
0036 
0037 int threads_starting;
0038 int running;
0039 
0040 extern void preempt_vmx(vector int *varray, int *threads_starting, int *running);
0041 
0042 void *preempt_vmx_c(void *p)
0043 {
0044     int i, j;
0045     srand(pthread_self());
0046     for (i = 0; i < 12; i++)
0047         for (j = 0; j < 4; j++)
0048             varray[i][j] = rand();
0049 
0050     /* Test fails if it ever returns */
0051     preempt_vmx(varray, &threads_starting, &running);
0052     return p;
0053 }
0054 
0055 int test_preempt_vmx(void)
0056 {
0057     int i, rc, threads;
0058     pthread_t *tids;
0059 
0060     // vcmpequd used in vmx_asm.S is v2.07
0061     SKIP_IF(!have_hwcap2(PPC_FEATURE2_ARCH_2_07));
0062 
0063     threads = sysconf(_SC_NPROCESSORS_ONLN) * THREAD_FACTOR;
0064     tids = malloc(threads * sizeof(pthread_t));
0065     FAIL_IF(!tids);
0066 
0067     running = true;
0068     threads_starting = threads;
0069     for (i = 0; i < threads; i++) {
0070         rc = pthread_create(&tids[i], NULL, preempt_vmx_c, NULL);
0071         FAIL_IF(rc);
0072     }
0073 
0074     setbuf(stdout, NULL);
0075     /* Not really nessesary but nice to wait for every thread to start */
0076     printf("\tWaiting for all workers to start...");
0077     while(threads_starting)
0078         asm volatile("": : :"memory");
0079     printf("done\n");
0080 
0081     printf("\tWaiting for %d seconds to let some workers get preempted...", PREEMPT_TIME);
0082     sleep(PREEMPT_TIME);
0083     printf("done\n");
0084 
0085     printf("\tStopping workers...");
0086     /*
0087      * Working are checking this value every loop. In preempt_vmx 'cmpwi r5,0; bne 2b'.
0088      * r5 will have loaded the value of running.
0089      */
0090     running = 0;
0091     for (i = 0; i < threads; i++) {
0092         void *rc_p;
0093         pthread_join(tids[i], &rc_p);
0094 
0095         /*
0096          * Harness will say the fail was here, look at why preempt_vmx
0097          * returned
0098          */
0099         if ((long) rc_p)
0100             printf("oops\n");
0101         FAIL_IF((long) rc_p);
0102     }
0103     printf("done\n");
0104 
0105     return 0;
0106 }
0107 
0108 int main(int argc, char *argv[])
0109 {
0110     return test_harness(test_preempt_vmx, "vmx_preempt");
0111 }