int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg) { int core; int ret; cpu_set_t mask; CPU_ZERO(&mask); ret = old_pthread_create(thread, attr, start_routine, arg); if(!get_shm()->active) return ret; core = get_next_core(); if(!get_shm()->per_node) { CPU_SET(core, &mask); } else { int i, node = numa_node_of_cpu(core); struct bitmask * bmp = numa_allocate_cpumask(); numa_node_to_cpus(node, bmp); for(i = 0; i < numa_num_configured_cpus(); i++) { if(numa_bitmask_isbitset(bmp, i)) CPU_SET(i, &mask); } numa_free_cpumask(bmp); } old_pthread_setaffinity_np(*thread, sizeof(mask), &mask); VERBOSE("-> Set affinity to %d\n", core); return ret; }
pid_t fork(void) { pid_t ret; // When a new process is forked, the refcounter must be incremented pthread_mutex_lock(&get_shm()->pin_lock); get_shm()->refcount++; pthread_mutex_unlock(&get_shm()->pin_lock); ret = old_fork(); if(ret > 0) { set_affinity(ret, get_next_core()); } return ret; }
pid_t fork(void) { pid_t ret; // Increment refcount on fork to avoid parent dying before child and destroying the shm __sync_fetch_and_add(&get_shm()->refcount, 1); ret = old_fork(); if(ret > 0) { set_affinity(ret, get_next_core()); } else if (ret < 0) { // fork failed, decrement __sync_fetch_and_sub(&get_shm()->refcount, 1); } return ret; }
static int init_cuda(NvEncoder*enc) { NVENCSTATUS nvResult; CUresult cuResult; int devid; cuResult=cuInit(0); CHK_CUDA_ERR(cuResult); cuResult=cuDeviceGetCount(&enc->devcnt); CHK_CUDA_ERR(cuResult); fprintf(stderr,"Device Count = %d\n",enc->devcnt); devid=enc->encCfg.deviceID;//default is 0 if(devid==-1 || devid>=enc->devcnt){ devid=get_next_core(); fprintf(stderr,"\033[33mAuto Select DevID:= %d\n\033[0m",devid); enc->encCfg.deviceID=devid; }else{ fprintf(stderr,"\033[33mGiven DevID:= %d\n\033[0m",devid); } cuResult=cuDeviceGet(&enc->cuDevice,devid); CHK_CUDA_ERR(cuResult); cuResult=cuCtxCreate(&enc->cuContext,0,enc->cuDevice); CHK_CUDA_ERR(cuResult); return 0; }