2016-01-07 00:31:33 +00:00
/*
2016-02-06 01:12:22 +00:00
* This file is part of sched_xetaffinity strace test .
*
2016-01-07 00:31:33 +00:00
* Copyright ( c ) 2016 Dmitry V . Levin < ldv @ altlinux . org >
* All rights reserved .
*
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions
* are met :
* 1. Redistributions of source code must retain the above copyright
* notice , this list of conditions and the following disclaimer .
* 2. Redistributions in binary form must reproduce the above copyright
* notice , this list of conditions and the following disclaimer in the
* documentation and / or other materials provided with the distribution .
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission .
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ` ` AS IS ' ' AND ANY EXPRESS OR
* IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED .
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT , INDIRECT ,
* INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES ( INCLUDING , BUT
* NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE ,
* DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
* ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
*/
# include "tests.h"
2016-08-09 14:38:29 +00:00
# include <asm/unistd.h>
2016-01-07 00:31:33 +00:00
# include <sched.h>
# if defined __NR_sched_getaffinity && defined __NR_sched_setaffinity \
& & defined CPU_ISSET_S & & defined CPU_ZERO_S & & defined CPU_SET_S
# include <assert.h>
# include <errno.h>
# include <stdio.h>
# include <unistd.h>
2016-12-14 02:03:04 +00:00
static const char * errstr ;
2016-01-07 00:31:33 +00:00
static int
getaffinity ( unsigned long pid , unsigned long size , void * set )
{
2016-12-14 02:03:04 +00:00
int rc = syscall ( __NR_sched_getaffinity , pid , size , set ) ;
errstr = sprintrc ( rc ) ;
return rc ;
2016-01-07 00:31:33 +00:00
}
static int
setaffinity ( unsigned long pid , unsigned long size , void * set )
{
2016-12-14 02:03:04 +00:00
int rc = syscall ( __NR_sched_setaffinity , pid , size , set ) ;
errstr = sprintrc ( rc ) ;
return rc ;
2016-01-07 00:31:33 +00:00
}
int
main ( void )
{
unsigned int cpuset_size = 1 ;
2016-02-06 01:12:22 +00:00
const pid_t pid = getpid ( ) ;
2016-01-07 00:31:33 +00:00
while ( cpuset_size ) {
assert ( getaffinity ( pid , cpuset_size , NULL ) = = - 1 ) ;
if ( EFAULT = = errno )
break ;
if ( EINVAL ! = errno )
perror_msg_and_skip ( " sched_getaffinity " ) ;
2016-12-14 02:03:04 +00:00
printf ( " sched_getaffinity(%d, %u, NULL) = %s \n " ,
pid , cpuset_size , errstr ) ;
2016-01-07 00:31:33 +00:00
cpuset_size < < = 1 ;
}
assert ( cpuset_size ) ;
2016-12-14 02:03:04 +00:00
printf ( " sched_getaffinity(%d, %u, NULL) = %s \n " ,
pid , cpuset_size , errstr ) ;
2016-01-07 00:31:33 +00:00
cpu_set_t * cpuset = tail_alloc ( cpuset_size ) ;
2016-12-14 02:03:04 +00:00
getaffinity ( pid , cpuset_size , cpuset + 1 ) ;
printf ( " sched_getaffinity(%d, %u, %p) = %s \n " ,
pid , cpuset_size , cpuset + 1 , errstr ) ;
int ret_size = getaffinity ( pid , cpuset_size , cpuset ) ;
if ( ret_size < 0 )
perror_msg_and_fail ( " sched_getaffinity(%d, %u, %p) = %s \n " ,
pid , ( unsigned ) cpuset_size , cpuset , errstr ) ;
assert ( ret_size < = ( int ) cpuset_size ) ;
2016-02-06 01:12:22 +00:00
2016-01-07 00:31:33 +00:00
printf ( " sched_getaffinity(%d, %u, [ " , pid , cpuset_size ) ;
const char * sep ;
unsigned int i , cpu ;
2016-12-14 02:03:04 +00:00
for ( i = 0 , cpu = 0 , sep = " " ; i < ( unsigned ) ret_size * 8 ; + + i ) {
if ( CPU_ISSET_S ( i , ( unsigned ) ret_size , cpuset ) ) {
2016-01-07 00:31:33 +00:00
printf ( " %s%u " , sep , i ) ;
2016-11-30 13:42:13 +03:00
sep = " , " ;
2016-01-07 00:31:33 +00:00
cpu = i ;
}
}
2016-12-14 02:03:04 +00:00
printf ( " ]) = %s \n " , errstr ) ;
2016-01-07 00:31:33 +00:00
CPU_ZERO_S ( cpuset_size , cpuset ) ;
CPU_SET_S ( cpu , cpuset_size , cpuset ) ;
if ( setaffinity ( pid , cpuset_size , cpuset ) )
perror_msg_and_skip ( " sched_setaffinity " ) ;
printf ( " sched_setaffinity(%d, %u, [%u]) = 0 \n " ,
pid , cpuset_size , cpu ) ;
const unsigned int big_size = cpuset_size < 128 ? 128 : cpuset_size * 2 ;
cpuset = tail_alloc ( big_size ) ;
2016-12-14 02:03:04 +00:00
ret_size = getaffinity ( pid , big_size , cpuset ) ;
if ( ret_size < 0 )
perror_msg_and_fail ( " sched_getaffinity(%d, %u, %p) = %s \n " ,
pid , big_size , cpuset , errstr ) ;
assert ( ret_size < = ( int ) big_size ) ;
2016-01-07 00:31:33 +00:00
printf ( " sched_getaffinity(%d, %u, [ " , pid , big_size ) ;
for ( i = 0 , sep = " " ; i < ( unsigned ) ret_size * 8 ; + + i ) {
if ( CPU_ISSET_S ( i , ( unsigned ) ret_size , cpuset ) ) {
printf ( " %s%u " , sep , i ) ;
2016-11-30 13:42:13 +03:00
sep = " , " ;
2016-01-07 00:31:33 +00:00
}
}
2016-12-14 02:03:04 +00:00
printf ( " ]) = %s \n " , errstr ) ;
2016-01-07 00:31:33 +00:00
puts ( " +++ exited with 0 +++ " ) ;
return 0 ;
}
# else
SKIP_MAIN_UNDEFINED ( " __NR_sched_getaffinity && __NR_sched_setaffinity "
" && CPU_ISSET_S && CPU_ZERO_S && CPU_SET_S " )
# endif