当前位置: 首页>>代码示例>>C++>>正文


C++ sched_getaffinity函数代码示例

本文整理汇总了C++中sched_getaffinity函数的典型用法代码示例。如果您正苦于以下问题:C++ sched_getaffinity函数的具体用法?C++ sched_getaffinity怎么用?C++ sched_getaffinity使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了sched_getaffinity函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: 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;
}
开发者ID:msf,项目名称:configs,代码行数:23,代码来源:cpu_bind.c

示例2: main

int main(int argc, char **argv)
{
	int opt;
	bool do_suspend = true;
	bool succeeded = true;
	cpu_set_t available_cpus;
	int err;
	int cpu;

	ksft_print_header();

	while ((opt = getopt(argc, argv, "n")) != -1) {
		switch (opt) {
		case 'n':
			do_suspend = false;
			break;
		default:
			printf("Usage: %s [-n]\n", argv[0]);
			printf("        -n: do not trigger a suspend/resume cycle before the test\n");
			return -1;
		}
	}

	if (do_suspend)
		suspend();

	err = sched_getaffinity(0, sizeof(available_cpus), &available_cpus);
	if (err < 0)
		ksft_exit_fail_msg("sched_getaffinity() failed\n");

	for (cpu = 0; cpu < CPU_SETSIZE; cpu++) {
		bool test_success;

		if (!CPU_ISSET(cpu, &available_cpus))
			continue;

		test_success = run_test(cpu);
		if (test_success) {
			ksft_test_result_pass("CPU %d\n", cpu);
		} else {
			ksft_test_result_fail("CPU %d\n", cpu);
			succeeded = false;
		}
	}

	if (succeeded)
		ksft_exit_pass();
	else
		ksft_exit_fail();
}
开发者ID:AlexShiLucky,项目名称:linux,代码行数:50,代码来源:step_after_suspend_test.c

示例3: affinity_processGetProcessorId

int
affinity_processGetProcessorId()
{
    int ret;
    cpu_set_t cpu_set;
    CPU_ZERO(&cpu_set);
    ret = sched_getaffinity(getpid(),sizeof(cpu_set_t), &cpu_set);

    if (ret < 0)
    {
        ERROR;
    }

    return getProcessorID(&cpu_set);
}
开发者ID:gbfree,项目名称:likwid,代码行数:15,代码来源:affinity.c

示例4: 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;
}
开发者ID:petermilne,项目名称:ks-2G-4G-llc,代码行数:44,代码来源:testrtmode.c

示例5: CPU_ZERO

/**
 * \brief Get CPU affinity
 *
 * Example of usage:
 * \code
 * std::vector<bool> v (2);
 * getAffinity(&v);
 * std::cout << "Affinity: " << v[0] << " " << v[1] << std::endl;
 * \endcode
 * @param v: vector of booleans containing the current affinity (true/false)
 * @exception std::runtime_error in case affinity cannot be get
 */
void Process::getAffinity(std::vector<bool>* v)
{
	cpu_set_t s;
	CPU_ZERO(&s);

	if ((sched_getaffinity(pid_, sizeof(s), &s) != 0))
		throw std::runtime_error ("Get affinity error");

        for (unsigned int j = 0; (j < CPU_SETSIZE) && (j < v->size()); ++j) {
		if (CPU_ISSET(j, &s))
			(*v)[j] = true;
		else
		(*v)[j] = false;
	}
}
开发者ID:ksergy1,项目名称:OnPosix,代码行数:27,代码来源:Process.cpp

示例6: get_process_cpu_affinity

/*
 * Return process CPU affinity as a Python long (the bitmask)
 */
static PyObject*
get_process_cpu_affinity(PyObject* self, PyObject* args)
{
    unsigned long mask;
    unsigned int len = sizeof(mask);
    long pid;

    if (!PyArg_ParseTuple(args, "i", &pid)) {
        return NULL;
    }
    if (sched_getaffinity(pid, len, (cpu_set_t *)&mask) < 0) {
        return PyErr_SetFromErrno(PyExc_OSError);
    }
    return Py_BuildValue("l", mask);
}
开发者ID:AshishNamdev,项目名称:mozilla-central,代码行数:18,代码来源:_psutil_linux.c

示例7: showCurCpu

void showCurCpu(int cpuNum) {
    cpu_set_t get;
    CPU_ZERO(&get);
    //if (pthread_getaffinity_np(pthread_self(), sizeof(get), &get) == -1) {
    if (sched_getaffinity(0, sizeof(get), &get) == -1) {
        printf("warning: cound not get cpu affinity\n");
        exit(1);
    }

    for (int i = 0; i < cpuNum; i++) {
        if (CPU_ISSET(i, &get)) {
            printf("this process %d is running processor : %d\n", getpid(), i);
        }
    }
}
开发者ID:killwing,项目名称:mytests,代码行数:15,代码来源:main.cpp

示例8: conf_sched

static void conf_sched(void)
{
	cpu_set_t set;
	int res, i;

	CPU_ZERO(&set);
	res = sched_getaffinity(0, sizeof(set), &set);
	if (res < 0)
		die("sched_getaffinity");
	for (i = 0; i < 256; i++) {
		if (!CPU_ISSET(i, &set))
			continue;
		printf("allowed cpu: %d\n", i);
	}
}
开发者ID:clcarwin,项目名称:mdrotor,代码行数:15,代码来源:mdrotor.c

示例9: gettid

void *func_nonrt(void *arg)
{
	Thread *pthr = (Thread *) arg;
	int rc, i, j, policy, tid = gettid();
	struct sched_param schedp;
	cpu_set_t mask;
	CPU_ZERO(&mask);
	CPU_SET(0, &mask);

	rc = sched_setaffinity(0, sizeof(mask), &mask);
	if (rc < 0) {
		printf("Thread %d: Can't set affinity: %d %s\n", tid, rc,
		       strerror(rc));
		exit(-1);
	}
	rc = sched_getaffinity(0, sizeof(mask), &mask);

	printf("Thread started %d on CPU %ld\n", pthr->priority,
	       (long)mask.__bits[0]);
	pthread_getschedparam(pthr->pthread, &policy, &schedp);
	printf("Thread running %d\n", pthr->priority);

	while (1) {
		pthread_mutex_lock(&glob_mutex);
		printf
		    ("Thread %d at start pthread pol %d pri %d - Got global lock\n",
		     pthr->priority, policy, schedp.sched_priority);
		sleep(2);
		for (i = 0; i < 10000; i++) {
			if ((i % 100) == 0) {
				sched_getparam(tid, &schedp);
				policy = sched_getscheduler(tid);
				printf("Thread %d(%d) loop %d pthread pol %d "
				       "pri %d\n", tid, pthr->priority, i,
				       policy, schedp.sched_priority);
				fflush(NULL);
			}
			pthr->id++;
			for (j = 0; j < 5000; j++) {
				pthread_mutex_lock(&(pthr->mutex));
				pthread_mutex_unlock(&(pthr->mutex));
			}
		}
		pthread_mutex_unlock(&glob_mutex);
		sched_yield();
	}
	return NULL;
}
开发者ID:kraj,项目名称:ltp,代码行数:48,代码来源:testpi-3.c

示例10: tegra_cache_smc

static void tegra_cache_smc(bool enable, u32 arg)
{
	void __iomem *p = IO_ADDRESS(TEGRA_ARM_PERIF_BASE) + 0x3000;
	bool need_affinity_switch;
	bool can_switch_affinity;
	bool l2x0_enabled;
	cpumask_t local_cpu_mask;
	cpumask_t saved_cpu_mask;
	unsigned long flags;
	long ret;

	/*
	 * ISSUE : Some registers of PL310 controler must be written
	 *              from Secure context (and from CPU0)!
	 *
	 * When called form Normal we obtain an abort or do nothing.
	 * Instructions that must be called in Secure:
	 *      - Write to Control register (L2X0_CTRL==0x100)
	 *      - Write in Auxiliary controler (L2X0_AUX_CTRL==0x104)
	 *      - Invalidate all entries (L2X0_INV_WAY==0x77C),
	 *              mandatory at boot time.
	 *      - Tag and Data RAM Latency Control Registers
	 *              (0x108 & 0x10C) must be written in Secure.
	 */
	need_affinity_switch = (smp_processor_id() != 0);
	can_switch_affinity = !irqs_disabled();

	WARN_ON(need_affinity_switch && !can_switch_affinity);
	if (need_affinity_switch && can_switch_affinity) {
		cpu_set(0, local_cpu_mask);
		sched_getaffinity(0, &saved_cpu_mask);
		ret = sched_setaffinity(0, &local_cpu_mask);
		WARN_ON(ret != 0);
	}

	local_irq_save(flags);
	l2x0_enabled = readl_relaxed(p + L2X0_CTRL) & 1;
	if (enable && !l2x0_enabled)
		tegra_generic_smc(0xFFFFF100, 0x00000001, arg);
	else if (!enable && l2x0_enabled)
		tegra_generic_smc(0xFFFFF100, 0x00000002, arg);
	local_irq_restore(flags);

	if (need_affinity_switch && can_switch_affinity) {
		ret = sched_setaffinity(0, &saved_cpu_mask);
		WARN_ON(ret != 0);
	}
}
开发者ID:binkybear,项目名称:kangaroo,代码行数:48,代码来源:common.c

示例11: sched_getaffinity

/*
 * getCPUMask
 */
uint64 CPProcess::getCPUMask()
{
	uint64 cpuMask = 1;

#ifdef __USE_GNU
	sint res = sched_getaffinity(getpid(), sizeof(uint64), (cpu_set_t*)&cpuMask);

	if (res)
	{
		nlwarning("sched_getaffinity() returned %d, errno = %d: %s", res, errno, strerror(errno));
		return 0;
	}
#endif // __USE_GNU

	return cpuMask;
}
开发者ID:Darkhunter,项目名称:Tranquillien-HCRP-Project-using-NeL,代码行数:19,代码来源:p_thread.cpp

示例12: get_cpu_count

int get_cpu_count()
{
    cpu_set_t cpu_mask;

    CPU_ZERO(&cpu_mask);
    int err = sched_getaffinity(0, sizeof(cpu_set_t), &cpu_mask);
	if (err) {
		LOG_ERROR << "sched_getaffinity failed\n";
		exit(1);
	}

    int count = CPU_COUNT(&cpu_mask);
    printf("%d\n", count);
    
    return count;
}
开发者ID:moloned,项目名称:sparsex,代码行数:16,代码来源:Affinity.cpp

示例13: get_cpunum

int get_cpunum()
{
    int num = 0;

#ifdef __linux__
    cpu_set_t cpuset;
    CPU_ZERO(&cpuset);
    sched_getaffinity(0, sizeof(cpuset), &cpuset);
    for (int i = 0; i < 32; i++)
    {
        if (CPU_ISSET(i, &cpuset))
            num++;
    }
#endif
    return num;
}
开发者ID:YuchongHu,项目名称:memc3,代码行数:16,代码来源:memc3_util.c

示例14: CPU_ZERO

int32 FLinuxMisc::NumberOfCores()
{
	cpu_set_t AvailableCpusMask;
	CPU_ZERO(&AvailableCpusMask);

	if (0 != sched_getaffinity(0, sizeof(AvailableCpusMask), &AvailableCpusMask))
	{
		return 1;	// we are running on something, right?
	}

	char FileNameBuffer[1024];
	unsigned char PossibleCores[CPU_SETSIZE] = { 0 };

	for(int32 CpuIdx = 0; CpuIdx < CPU_SETSIZE; ++CpuIdx)
	{
		if (CPU_ISSET(CpuIdx, &AvailableCpusMask))
		{
			sprintf(FileNameBuffer, "/sys/devices/system/cpu/cpu%d/topology/core_id", CpuIdx);
			
			FILE* CoreIdFile = fopen(FileNameBuffer, "r");
			unsigned int CoreId = 0;
			if (CoreIdFile)
			{
				if (1 != fscanf(CoreIdFile, "%d", &CoreId))
				{
					CoreId = 0;
				}
				fclose(CoreIdFile);
			}

			if (CoreId >= ARRAY_COUNT(PossibleCores))
			{
				CoreId = 0;
			}
			
			PossibleCores[ CoreId ] = 1;
		}
	}

	int32 NumCoreIds = 0;
	for(int32 Idx = 0; Idx < ARRAY_COUNT(PossibleCores); ++Idx)
	{
		NumCoreIds += PossibleCores[Idx];
	}

	return NumCoreIds;
}
开发者ID:Tigrouzen,项目名称:UnrealEngine-4,代码行数:47,代码来源:LinuxPlatformMisc.cpp

示例15: cwGetCPUaffinity

//-----------------------------------------------------------------------------
// Return CPU affinity in a form suitable for messages.  Single CPU affinity
// returns a non-negative integer CPU number.  Multi CPU affinity returns the
// negative of the bit mask of affine CPUs.  Affinity to no CPUs returns -1.
//----------------------------------------------------------------------------
int32_t cwGetCPUaffinity(void) {
  int numCPU = sysconf( _SC_NPROCESSORS_CONF );
  cpu_set_t af;
  int32_t i, afmask = 0, afcount = 0, afCPU=-1;

  sched_getaffinity(0, sizeof(af), &af);

  for (i=0; i<numCPU; ++i) {
    if (CPU_ISSET(i, &af)) {
      afCPU = i;
      afmask |= (1 << i);
      afcount++;
    }
  }
  if (afcount <= 1) return afCPU;
  else return -afmask;
}
开发者ID:HPCNow,项目名称:supermagic,代码行数:22,代码来源:cw_util.c


注:本文中的sched_getaffinity函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。