Jarvis
Jarvis

Reputation: 8564

How to use the macro SCHED_DEADLINE in linux?

We know that there are several scheduling policies in linux like SCHED_FIFO, SCHED_RR, SCHED_OTHER, etc. and one can change the scheduler of a real-time process using the sched_setscheduler system call.

But I'm not able to change the scheduler of a program to Earliest-deadline-first using the SCHED_DEADLINE macro ? Can anyone suggest a way how to achieve this ?

Upvotes: 3

Views: 4515

Answers (2)

Claudio
Claudio

Reputation: 10947

First of all, you need a 3.14+ Linux kernel.

Moreover, since glibc does not yet provide the API wrapping the new scheduler syscall (i.e. ) you need to wrap them by yourself:

#define _GNU_SOURCE
#include <linux/kernel.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <time.h>
#include <linux/types.h>
#include <sched.h>
#include <linux/sched.h>
#include <sys/types.h>

#define SCHED_DEADLINE  6

/* __NR_sched_setattr number */
#ifndef __NR_sched_setattr
#ifdef __x86_64__
#define __NR_sched_setattr      314
#endif

#ifdef __i386__
#define __NR_sched_setattr      351
#endif

#ifdef __arm__
#define __NR_sched_setattr      380
#endif

#ifdef __aarch64__
#define __NR_sched_setattr      274
#endif
#endif

/* __NR_sched_getattr number */
#ifndef __NR_sched_getattr
#ifdef __x86_64__
#define __NR_sched_getattr      315
#endif

#ifdef __i386__
#define __NR_sched_getattr      352
#endif

#ifdef __arm__
#define __NR_sched_getattr      381
#endif

#ifdef __aarch64__
#define __NR_sched_getattr      275
#endif
#endif

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 */
    __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);
}

Upvotes: 1

DevBee
DevBee

Reputation: 77

This link has example code for EDF algorithm ie deadline scheduling.
http://www.admin-magazine.com/Archive/2015/25/Optimizing-utilization-with-the-EDF-scheduler

sched_setattr() has to be used for deadline scheduling, instead of sched_setscheduler() which can be used to invoke sched_rr/sched_fifo/sched_other....etc. Also, the period of threads must be compatible enough to accommodate the threads' periods otherwise the sched_setattr returns error.

Upvotes: 4

Related Questions