本文整理汇总了C++中sched_setaffinity函数的典型用法代码示例。如果您正苦于以下问题:C++ sched_setaffinity函数的具体用法?C++ sched_setaffinity怎么用?C++ sched_setaffinity使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sched_setaffinity函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: CPU_ZERO
// Sets the processor affinity
//
// Mathilda works by breaking up sets of Instructions to be
// executed by child processes. This function is called
// by child processes to bind them to a specific cpu
//
// @param[in] CPU number to bind to
// @return Returns OK or ERR from sched_setaffinity
int MathildaFork::set_affinity(uint32_t c) {
if(c >= cores) {
c = 0;
}
cpu_set_t cpus;
CPU_ZERO(&cpus);
CPU_SET(c, &cpus);
int ret = sched_setaffinity(0, sizeof(cpus), &cpus);
core = c;
#ifdef DEBUG
if(ret == ERR) {
fprintf(stdout, "[MathildaFork] Failed to bind process %d to CPU %d. Cache invalidation may occur!\n", getpid(), c);
} else {
fprintf(stdout, "[MathildaFork] Child (pid: %d) successfully bound to CPU %d\n", getpid(), c);
}
#endif
return ret;
}
示例2: set_affinity
/**
* set_affinity
*
* When loading or unloading a system-wide context, we must pin the pfmsetup
* process to that CPU before making the system call. Also, get the current
* affinity and return it to the caller so we can change it back later.
**/
static int set_affinity(int cpu, cpu_set_t *old_cpu_set)
{
cpu_set_t new_cpu_set;
int rc;
rc = sched_getaffinity(0, sizeof(*old_cpu_set), old_cpu_set);
if (rc) {
rc = errno;
LOG_ERROR("Can't get current process affinity mask: %d\n", rc);
return rc;
}
CPU_ZERO(&new_cpu_set);
CPU_SET(cpu, &new_cpu_set);
rc = sched_setaffinity(0, sizeof(new_cpu_set), &new_cpu_set);
if (rc) {
rc = errno;
LOG_ERROR("Can't set process affinity to CPU %d: %d\n", cpu, rc);
return rc;
}
return 0;
}
示例3: PetscThreadCommCreate_OpenMP
PETSC_EXTERN PetscErrorCode PetscThreadCommCreate_OpenMP(PetscThreadComm tcomm)
{
PetscErrorCode ierr;
PetscFunctionBegin;
ierr = PetscStrcpy(tcomm->type,OPENMP);CHKERRQ(ierr);
tcomm->ops->runkernel = PetscThreadCommRunKernel_OpenMP;
tcomm->ops->getrank = PetscThreadCommGetRank_OpenMP;
#pragma omp parallel num_threads(tcomm->nworkThreads) shared(tcomm)
{
#if defined(PETSC_HAVE_SCHED_CPU_SET_T)
cpu_set_t mset;
PetscInt ncores, icorr,trank;
PetscGetNCores(&ncores);
CPU_ZERO(&mset);
trank = omp_get_thread_num();
icorr = tcomm->affinities[trank]%ncores;
CPU_SET(icorr,&mset);
sched_setaffinity(0,sizeof(cpu_set_t),&mset);
#endif
}
PetscFunctionReturn(0);
}
示例4: set_process_affinity
int set_process_affinity(int cpu)
{
int retval = -1;
#if defined(CPU_ZERO)
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;
}
retval = sched_setaffinity(0, sizeof(cpu_mask), &cpu_mask);
#elif defined(cpuset_create)
cpuset_t *cpu_mask = cpuset_create();
cpuset_zero(cpu_mask);
if (cpu >= 0 && cpu <= cpuset_size(cpu_mask)) {
cpuset_set(cpu, cpu_mask);
} else {
fprintf (stderr, "Wrong cpu id: %d\n", cpu);
return -1;
}
retval = _sched_setaffinity(0, 0, cpuset_size(cpu_mask), cpu_mask);
cpuset_destroy(cpu_mask);
#else
#error "no cpuset"
#endif
if (retval == -1)
perror("Error at sched_setaffinity()");
return retval;
}
示例5: set_cpu_affinity
int set_cpu_affinity(pid_t pid, unsigned long new_mask)
{
unsigned long cur_mask;
unsigned int len = sizeof(new_mask);
if (sched_getaffinity(pid, len, (cpu_set_t *) &cur_mask) < 0) {
perror("sched_getaffinity");
return -1;
}
printf("pid %d's old affinity: %08lx\n", pid, cur_mask);
if (sched_setaffinity(pid, len, (cpu_set_t *) &new_mask)) {
perror("sched_setaffinity");
return -1;
}
if (sched_getaffinity(pid, len, (cpu_set_t *) &cur_mask) < 0) {
perror("sched_getaffinity");
return -1;
}
printf(" pid %d's new affinity: %08lx\n", pid, cur_mask);
return 0;
}
示例6: __binding_cpu
static void
__binding_cpu(void)
{
int curr_cpu_max = __cpus_nums();
srand(time(NULL));
int num = rand() % curr_cpu_max;
while(!num) {
num = rand() % curr_cpu_max;
}
log_debug("CPU: %d\n", num);
cpu_set_t mask;
__CPU_ZERO(&mask);
__CPU_SET(num, &mask);
sched_setaffinity(0,sizeof(cpu_set_t),&mask);
}
示例7: main
int main(int argc, char **argv)
{
cpu_set_t mask;
timespec start, end;
list = NULL;
CPU_ZERO(&mask);
CPU_SET(0, &mask);
sched_setaffinity(0, sizeof(mask), &mask);
pgsz = sysconf(_SC_PAGESIZE);
allocatedsize = 0;
counter = 0;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
allocate();
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end);
print_time_diff(start, end);
return 0;
}
示例8: main
int main() {
int64_t value = -1;
// Lock to cpu0
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(0, &mask);
assert(!sched_setaffinity(0, sizeof(mask), &mask));
while (1) {
int fd = start_counter();
timing_computation();
ssize_t nread = read(fd, &value, sizeof(value));
assert(nread == sizeof(value));
close(fd);
printf("%lld\n", value);
}
return 0;
}
示例9: printf
RhsRobotBase::RhsRobotBase() //Constructor
{
cpu_set_t mask;
//struct sched_param param;
printf("\n\t\t%s \"%s\"\n\tVersion %s built %s at %s\n\n", ROBOT_NAME, ROBOT_NICKNAME, ROBOT_VERSION, __DATE__, __TIME__);
// run our code on the second core
CPU_ZERO(&mask);
CPU_SET(1, &mask);
sched_setaffinity(0, sizeof(mask), &mask);
//param.sched_priority = 10;
//printf("did this work %d\n", sched_setscheduler(0, SCHED_FIFO, ¶m));
// what are our priority limits?
previousRobotState = ROBOT_STATE_UNKNOWN;
currentRobotState = ROBOT_STATE_UNKNOWN;
SmartDashboard::init();
loop = 0; //Initializes the loop counter
}
示例10: worker
int worker(int cpuid)
{
/***********************************************/
/* Ensure we are running on the cpu */
/* specified. */
/***********************************************/
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(cpuid, &mask);
if (sched_setaffinity(getpid(), 64, &mask) < 0) {
perror("sched_setaffinity");
// exit(1);
}
/***********************************************/
/* Now we are locked to a cpu. */
/***********************************************/
/***********************************************/
/* Wait for master to give us the "go" */
/* signal. */
/***********************************************/
while (data[cpuid] == 0)
;
/***********************************************/
/* Let master know we saw it. */
/***********************************************/
data[cpuid] = 2;
/***********************************************/
/* Wait for master to see our response. */
/***********************************************/
while (data[cpuid] == 2)
;
exit(0);
}
示例11: CPU_ZERO
void *incrementer(void *arg)
{
int i;
int proc_num = *(int *)arg;
cpu_set_t set;
CPU_ZERO(&set);
CPU_SET(proc_num, &set);
if (sched_setaffinity(gettid(), sizeof(cpu_set_t), &set)) {
perror("sched_setaffinity");
return NULL;
}
for (i = 0; i < INC_TO; i++) {
#if defined _USE_ATOMIC
__sync_fetch_and_add(&global_int, 1);
#elif defined _USE_GTM
__transaction_atomic {
global_int++;
}
#elif defined _USE_MUTEX
pthread_mutex_lock(&mutex);
global_int++;
pthread_mutex_unlock(&mutex);
#elif defined _USE_SPIN
pthread_spin_lock(&spinlock);
global_int++;
pthread_spin_unlock(&spinlock);
#else
global_int++;
#endif
}
return NULL;
}
示例12: ngx_setaffinity
// 将当前进程绑定到参数cpu_affinity对应比特位为1的位置的CPU核心。
// cpu_affinity[in]: 如果为1将绑定到第0个CPU,如果为2将绑定到第1个CPU,如果为4将绑定到第2个CPU,以此类推。
// 同时支持绑定到多个CPU,比如为3可以绑定到第0和第1个CPU,为5可以绑定到第0和第2个CPU。
void
ngx_setaffinity(uint64_t cpu_affinity, ngx_log_t *log)
{
cpu_set_t mask;
ngx_uint_t i;
ngx_log_error(NGX_LOG_NOTICE, log, 0,
"sched_setaffinity(0x%08Xl)", cpu_affinity);
CPU_ZERO(&mask);
i = 0;
do {
if (cpu_affinity & 1) {
CPU_SET(i, &mask);
}
i++;
cpu_affinity >>= 1;
} while (cpu_affinity);
if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) == -1) {
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
"sched_setaffinity() failed");
}
}
示例13: CPU_ZERO
void *threadFunction(void *arg) {
int threadID = *((int*)arg);
#ifdef USE_AFFINITY
int cpu_id = threadID%4;
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(cpu_id,&mask);
sched_setaffinity(0,sizeof(mask),&mask);
#endif
int matrixNum = 0;
while (1) {
HMap detectedEvents[4];
AMap cops;
AMap criminals;
pthread_mutex_lock(&matrixFileMutex);
matrixNum = fMatrixReader(detectedEvents);
pthread_mutex_unlock(&matrixFileMutex);
if (!matrixNum) { pthread_exit(0); }
fGetMovement(detectedEvents[0],detectedEvents[1],cops);
fGetMovement(detectedEvents[2],detectedEvents[3],criminals);
fDetectCrucialEvent(cops, criminals, matrixNum);
}
pthread_exit(0);
}
示例14: pin_to_cpu
/**
* Try to ping process to a specific CPU. Returns the CPU we are
* currently running on.
*/
static int pin_to_cpu(int run_cpu)
{
cpu_set_t *cpusetp;
size_t size;
int num_cpus;
num_cpus = CPU_SETSIZE; /* take default, currently 1024 */
cpusetp = CPU_ALLOC(num_cpus);
if (cpusetp == NULL)
return sched_getcpu();
size = CPU_ALLOC_SIZE(num_cpus);
CPU_ZERO_S(size, cpusetp);
CPU_SET_S(run_cpu, size, cpusetp);
if (sched_setaffinity(0, size, cpusetp) < 0) {
CPU_FREE(cpusetp);
return sched_getcpu();
}
/* figure out on which cpus we actually run */
CPU_FREE(cpusetp);
return run_cpu;
}
示例15: set_affinity
/*
******************************************************************************
SUBROUTINE: set_affinity
Set this process to run on the input processor number.
The processor numbers start with 0 going to N-1 processors.
******************************************************************************
*/
int set_affinity(int processor)
{
extern int sched_getaffinity();
extern int sched_setaffinity();
unsigned long new_mask;
unsigned int len = sizeof(new_mask);
unsigned long cur_mask;
pid_t p = 0;
int ret;
new_mask = 1<<(processor);
//printf("set_affinity: %ld\n",new_mask);
ret = sched_getaffinity(p, len, &cur_mask);
// printf("sched_getaffinity = %d, cur_mask = %08lx\n",ret,cur_mask);
if(ret != 0) abort();
ret = sched_setaffinity(p, len, &new_mask);
// printf("sched_setaffinity = %d, new_mask = %08lx\n",ret,new_mask);
if(ret != 0) abort();
ret = sched_getaffinity(p, len, &cur_mask);
// printf("sched_getaffinity = %d, cur_mask = %08lx\n",ret,cur_mask);
if(ret != 0) abort();
if(cur_mask != new_mask)
{
printf("affinity did not get set! exiting\n");
exit(-1);
}
fflush(stdout);
return 0;
}