From: Robert W. <ro...@us...> - 2004-05-27 15:28:18
|
Update of /cvsroot/ltp/ltp/testcases/open_posix_testsuite/conformance/interfaces/sched_yield In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv20883/conformance/interfaces/sched_yield Modified Files: 1-1.c Log Message: Update to 1.4.2 Index: 1-1.c =================================================================== RCS file: /cvsroot/ltp/ltp/testcases/open_posix_testsuite/conformance/interfaces/sched_yield/1-1.c,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** 1-1.c 4 May 2004 02:14:47 -0000 1.5 --- 1-1.c 27 May 2004 15:27:35 -0000 1.6 *************** *** 20,23 **** --- 20,28 ---- * changed since the call. */ + + #ifdef LINUX + #define _GNU_SOURCE + #endif + #include <sched.h> #include <stdio.h> *************** *** 27,30 **** --- 32,36 ---- #include <pthread.h> #include <errno.h> + #include <sys/wait.h> #include "posixtest.h" *************** *** 41,48 **** ! #define LOOP 10 /* Shall be >= 1 */ ! int nb_call = 0; /* Get the number of CPUs */ --- 47,54 ---- ! #define LOOP 1000 /* Shall be >= 1 */ ! volatile int nb_call = 0; /* Get the number of CPUs */ *************** *** 72,77 **** --- 78,137 ---- } + #ifdef LINUX + int set_process_affinity(int cpu) + { + int retval = -1; + cpu_set_t cpu_mask; + + CPU_ZERO(&cpu_mask); + if (cpu >= 0 && cpu <= CPU_SETSIZE) { + CPU_SET(cpu, &cpu_mask); + } else { + fprintf (stderr, "Wrong cpu id: %d\n", cpu); + return -1; + } + + //#ifndef P2_SCHED_SETAFFINITY + retval = sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask); + //#else + // retval = sched_setaffinity(0, &cpu_mask); + //#endif + if (retval == -1) + perror("Error at sched_setaffinity()"); + + return retval; + } + + int set_thread_affinity(int cpu) + { + int retval = -1; + cpu_set_t cpu_mask; + + CPU_ZERO(&cpu_mask); + if (cpu >= 0 && cpu <= CPU_SETSIZE) { + CPU_SET(cpu, &cpu_mask); + } else { + fprintf (stderr, "Wrong cpu id: %d\n", cpu); + return -1; + } + //#ifndef P2_PTHREAD_SETAFFINITY + retval = pthread_setaffinity_np(pthread_self(), + sizeof(cpu_mask), &cpu_mask); + //#else + // retval = pthread_setaffinity_np(pthread_self(), &cpu_mask); + //#endif + if (retval != 0) + fprintf (stderr, "Error at pthread_setaffinity_np():\n"); + return retval; + } + + #endif + void * runner(void * arg) { int i=0, nc, result = 0; + #ifdef LINUX + set_thread_affinity(*(int *)arg); + fprintf(stderr, "%ld bind to cpu: %d\n", pthread_self(), *(int*)arg); + #endif for(;i<LOOP;i++){ *************** *** 86,98 **** } } ! ! pthread_exit((void*)(&result)); return NULL; } ! void * busy_thread(){ ! while(1){ ! nb_call++; sched_yield(); } --- 146,162 ---- } } ! ! pthread_exit((void*)(result)); return NULL; } ! void * busy_thread(void *arg){ ! #ifdef LINUX ! set_thread_affinity(*(int *)arg); ! fprintf(stderr, "%ld bind to cpu: %d\n", pthread_self(), *(int*)arg); ! #endif ! while(1){ ! nb_call++; sched_yield(); } *************** *** 102,108 **** ! void buzy_process(){ struct sched_param param; param.sched_priority = sched_get_priority_max(SCHED_FIFO); if(sched_setscheduler(getpid(), SCHED_FIFO, ¶m) != 0) { --- 166,177 ---- ! void buzy_process(int cpu){ struct sched_param param; + #ifdef LINUX + /* Bind to a processor */ + set_process_affinity(cpu); + fprintf(stderr, "%d bind to cpu: %d\n", getpid(), cpu); + #endif param.sched_priority = sched_get_priority_max(SCHED_FIFO); if(sched_setscheduler(getpid(), SCHED_FIFO, ¶m) != 0) { *************** *** 126,129 **** --- 195,199 ---- pthread_attr_t attr; struct sched_param param; + int thread_cpu; *************** *** 134,141 **** } ! /*if(ncpu > 1) { ! printf("Not tested on multi-processors machines.\n"); ! return PTS_UNTESTED; ! }*/ param.sched_priority = sched_get_priority_min(SCHED_FIFO) + 1; --- 204,208 ---- } ! printf("System has %d processors\n", ncpu); param.sched_priority = sched_get_priority_min(SCHED_FIFO) + 1; *************** *** 158,162 **** } else if (child_pid[i] == 0){ ! buzy_process(); printf("This code should not be executed.\n"); --- 225,229 ---- } else if (child_pid[i] == 0){ ! buzy_process(i); printf("This code should not be executed.\n"); *************** *** 167,177 **** pthread_attr_init(&attr); ! if(pthread_create(&tid, &attr, busy_thread, NULL) != 0) { perror("An error occurs when calling pthread_create()"); return PTS_UNRESOLVED; } ! if(pthread_create(&tid_runner, &attr, runner, NULL) != 0) { perror("An error occurs when calling pthread_create()"); return PTS_UNRESOLVED; --- 234,246 ---- pthread_attr_init(&attr); + pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); ! thread_cpu = ncpu -1; ! if(pthread_create(&tid, &attr, busy_thread, &thread_cpu) != 0) { perror("An error occurs when calling pthread_create()"); return PTS_UNRESOLVED; } ! if(pthread_create(&tid_runner, &attr, runner, &thread_cpu) != 0) { perror("An error occurs when calling pthread_create()"); return PTS_UNRESOLVED; *************** *** 182,192 **** return PTS_UNRESOLVED; } ! result = *(int *)tmpresult; ! if(result){ printf("A thread does not relinquish the processor.\n"); return PTS_FAIL; } ! printf("Test PASSED\n"); return PTS_PASS; --- 251,264 ---- return PTS_UNRESOLVED; } ! ! for(i=0; i<ncpu-1; i++) ! waitpid(child_pid[i], NULL, 0); ! ! result = (int)tmpresult; if(result){ printf("A thread does not relinquish the processor.\n"); return PTS_FAIL; } ! printf("Test PASSED\n"); return PTS_PASS; |