LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 05-02-2018, 12:51 PM   #1
PopWq
LQ Newbie
 
Registered: May 2018
Posts: 1

Rep: Reputation: Disabled
sched_setaffinity() and sched_setattr()


Is there a way I can use deadline scheduling and at the same time set cpu affinity to a process? Below is my testing code:

Code:
#define _GNU_SOURCE
#include "include/my_sched.h"
#include <stdio.h>
#include <time.h>
#include <sys/time.h>

int main() {
	struct sched_attr attr;
	int x = 0;
	int ret;
	unsigned int flags = 0;
	long int tid = gettid();

	printf("deadline thread started [%ld]\n", tid);

	/* Set scheduling properties */
	attr.size = sizeof(attr);
	attr.sched_flags = 0;
	attr.sched_nice = 0;
	attr.sched_priority = 0;

	/* This creates a 100ms/300ms reservation */
	attr.sched_policy = SCHED_DEADLINE;
	attr.sched_runtime = 100 * 1000 * 1000;
	attr.sched_period = attr.sched_deadline = 300 * 1000 * 1000;

	ret = sched_setattr(0, &attr, flags);
	if (ret != 0) {
		done = 0;
		perror("sched_setattr");
		printf("exit!\n");
		exit(-1);
	}

	/* Set CPU affinity */
	cpu_set_t  mask;
	CPU_ZERO(&mask);
	CPU_SET(0, &mask);
	ret = sched_setaffinity(0, sizeof(mask), &mask);

	if (ret != 0) {
		done = 0;
		perror("sched_setaffinity");
		printf("exit!\n");
		exit(-1);
	}

	
	return 0;
}
Even if I compile the above program and run it in sudo, I got error
Code:
sched_setaffinity: Device or resource busy
If I swap the order of sched_setattr() and sched_setaffinity(), I got a different error:
Code:
sched_setattr: Operation not permitted
This happens even if I'm in sudo.

Is there any problem with my code? Why I cannot use sched_setaffinity() and sched_setattr() with deadline scheduling in the same program?

For anyone who is interested to compile and try the program, below is code for my_include.h:

Code:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <linux/unistd.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <sys/syscall.h>
#include <pthread.h>

#define gettid() syscall(__NR_gettid)

#define SCHED_DEADLINE	6

/* XXX use the proper syscall numbers */
#ifdef __x86_64__
#define __NR_sched_setattr		314
#define __NR_sched_getattr		315
#endif

#ifdef __i386__
#define __NR_sched_setattr		351
#define __NR_sched_getattr		352
#endif

#ifdef __arm__
#define __NR_sched_setattr		380
#define __NR_sched_getattr		381
#endif

static volatile int done;

struct sched_attr {
	__u32 size;

	__u32 sched_policy;
	__u64 sched_flags;

	/* SCHED_NORMAL, SCHED_BATCH */
	__s32 sched_nice;

	/* SCHED_FIFO, SCHED_RR */
	__u32 sched_priority;

	/* SCHED_DEADLINE (nsec) */
	__u64 sched_runtime;
	__u64 sched_deadline;
	__u64 sched_period;
};

int sched_setattr(pid_t pid,
		const struct sched_attr *attr,
		unsigned int flags)
{
	return syscall(__NR_sched_setattr, pid, attr, flags);
}

int sched_getattr(pid_t pid,
		struct sched_attr *attr,
		unsigned int size,
		unsigned int flags)
{
	return syscall(__NR_sched_getattr, pid, attr, size, flags);
}
This question has been asked in a different thread (https://www.linuxquestions.org/quest...ty-4175617801/), but there's no answer for it.

Last edited by PopWq; 05-02-2018 at 12:53 PM. Reason: add reference
 
Old 05-09-2018, 10:12 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 & 7
Posts: 3,041

Rep: Reputation: 811Reputation: 811Reputation: 811Reputation: 811Reputation: 811Reputation: 811Reputation: 811
What kernel are you running?

Edit: I think you are running into this check in sched/core.c which says deadline tasks can't run on fewer CPUs:

Code:
	/*
	 * Since bandwidth control happens on root_domain basis,
	 * if admission test is enabled, we only admit -deadline
	 * tasks allowed to run on all the CPUs in the task's
	 * root_domain.
	 */

Last edited by smallpond; 05-09-2018 at 10:20 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Real time application programming - sched_setattr() fails if I set CPU affinity using sched_setaffinity(). barz_83_LQ Linux - Newbie 2 11-20-2017 11:11 AM
How immediate is sched_setaffinity dpoznan Linux - Software 0 03-19-2008 09:00 AM
difference between set_cpus_allowed() and sched_setaffinity() asurya Linux - General 1 12-01-2006 10:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 10:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration