linux/tools/testing/selftests/powerpc/math/vsx_preempt.c
<<
>>
Prefs
   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 VSX registers change across preemption.
   6 * There is no way to be sure preemption happened so this test just
   7 * uses many threads and a long wait. As such, a successful test
   8 * doesn't mean much but a failure is bad.
   9 */
  10
  11#include <stdio.h>
  12#include <string.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
  21#include "utils.h"
  22
  23/* Time to wait for workers to get preempted (seconds) */
  24#define PREEMPT_TIME 20
  25/*
  26 * Factor by which to multiply number of online CPUs for total number of
  27 * worker threads
  28 */
  29#define THREAD_FACTOR 8
  30
  31/*
  32 * Ensure there is twice the number of non-volatile VMX regs!
  33 * check_vmx() is going to use the other half as space to put the live
  34 * registers before calling vsx_memcmp()
  35 */
  36__thread vector int varray[24] = {
  37        {1, 2, 3, 4 }, {5, 6, 7, 8 }, {9, 10,11,12},
  38        {13,14,15,16}, {17,18,19,20}, {21,22,23,24},
  39        {25,26,27,28}, {29,30,31,32}, {33,34,35,36},
  40        {37,38,39,40}, {41,42,43,44}, {45,46,47,48}
  41};
  42
  43int threads_starting;
  44int running;
  45
  46extern long preempt_vsx(vector int *varray, int *threads_starting, int *running);
  47
  48long vsx_memcmp(vector int *a) {
  49        vector int zero = {0, 0, 0, 0};
  50        int i;
  51
  52        FAIL_IF(a != varray);
  53
  54        for(i = 0; i < 12; i++) {
  55                if (memcmp(&a[i + 12], &zero, sizeof(vector int)) == 0) {
  56                        fprintf(stderr, "Detected zero from the VSX reg %d\n", i + 12);
  57                        return 2;
  58                }
  59        }
  60
  61        if (memcmp(a, &a[12], 12 * sizeof(vector int))) {
  62                long *p = (long *)a;
  63                fprintf(stderr, "VSX mismatch\n");
  64                for (i = 0; i < 24; i=i+2)
  65                        fprintf(stderr, "%d: 0x%08lx%08lx | 0x%08lx%08lx\n",
  66                                        i/2 + i%2 + 20, p[i], p[i + 1], p[i + 24], p[i + 25]);
  67                return 1;
  68        }
  69        return 0;
  70}
  71
  72void *preempt_vsx_c(void *p)
  73{
  74        int i, j;
  75        long rc;
  76        srand(pthread_self());
  77        for (i = 0; i < 12; i++)
  78                for (j = 0; j < 4; j++) {
  79                        varray[i][j] = rand();
  80                        /* Don't want zero because it hides kernel problems */
  81                        if (varray[i][j] == 0)
  82                                j--;
  83                }
  84        rc = preempt_vsx(varray, &threads_starting, &running);
  85        if (rc == 2)
  86                fprintf(stderr, "Caught zeros in VSX compares\n");
  87        return (void *)rc;
  88}
  89
  90int test_preempt_vsx(void)
  91{
  92        int i, rc, threads;
  93        pthread_t *tids;
  94
  95        threads = sysconf(_SC_NPROCESSORS_ONLN) * THREAD_FACTOR;
  96        tids = malloc(threads * sizeof(pthread_t));
  97        FAIL_IF(!tids);
  98
  99        running = true;
 100        threads_starting = threads;
 101        for (i = 0; i < threads; i++) {
 102                rc = pthread_create(&tids[i], NULL, preempt_vsx_c, NULL);
 103                FAIL_IF(rc);
 104        }
 105
 106        setbuf(stdout, NULL);
 107        /* Not really nessesary but nice to wait for every thread to start */
 108        printf("\tWaiting for %d workers to start...", threads_starting);
 109        while(threads_starting)
 110                asm volatile("": : :"memory");
 111        printf("done\n");
 112
 113        printf("\tWaiting for %d seconds to let some workers get preempted...", PREEMPT_TIME);
 114        sleep(PREEMPT_TIME);
 115        printf("done\n");
 116
 117        printf("\tStopping workers...");
 118        /*
 119         * Working are checking this value every loop. In preempt_vsx 'cmpwi r5,0; bne 2b'.
 120         * r5 will have loaded the value of running.
 121         */
 122        running = 0;
 123        for (i = 0; i < threads; i++) {
 124                void *rc_p;
 125                pthread_join(tids[i], &rc_p);
 126
 127                /*
 128                 * Harness will say the fail was here, look at why preempt_vsx
 129                 * returned
 130                 */
 131                if ((long) rc_p)
 132                        printf("oops\n");
 133                FAIL_IF((long) rc_p);
 134        }
 135        printf("done\n");
 136
 137        return 0;
 138}
 139
 140int main(int argc, char *argv[])
 141{
 142        return test_harness(test_preempt_vsx, "vsx_preempt");
 143}
 144