LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Kernel (https://www.linuxquestions.org/questions/linux-kernel-70/)
-   -   sched_setaffinity() and sched_setattr() (https://www.linuxquestions.org/questions/linux-kernel-70/sched_setaffinity-and-sched_setattr-4175628874/)

PopWq 05-02-2018 11:51 AM

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.

smallpond 05-09-2018 09:12 AM

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.
        */



All times are GMT -5. The time now is 12:10 PM.