SCHED_DEADLINE运行时溢出信号(SIGXCPU)如何工作?

SCHED_DEADLINE运行时溢出信号(SIGXCPU)如何工作?

我正在实施一个rt-thread使用SCHED_DEADLINE.SIGXCPU如果线程超过指定的值sched_runtime,我试图捕获- 信号。根据我对手册页的理解,这可以通过设置 -flag 来实现SCHED_FLAG_DL_OVERRUN。然而,在下面的示例程序中,没有收到信号(在第三次迭代时)。根据该kernelshark图,线程在达到运行时后被挂起,但没有创建信号。

我对 -flag 的理解SCHED_FLAG_DL_OVERRUN正确吗?如果没有,是否有另一种方法可以检测线程是否超过指定的运行时间,而无需测量每次迭代后的时间?

我的内核版本是 5.4.3-rt1-1-rt #1 SMP PREEMPT_RT。

#include <unistd.h>
#include <linux/unistd.h>
#include <linux/types.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/sched.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);
}


void set_rt(int pid, unsigned long runtime, unsigned long period, unsigned long deadline){
    struct sched_attr attr;
    unsigned int flags = 0;
    attr.size = sizeof(attr);
    attr.sched_flags =  SCHED_FLAG_DL_OVERRUN;
    attr.sched_nice = 0;
    attr.sched_priority = 0;
    attr.sched_policy = SCHED_DEADLINE;
    attr.sched_runtime = runtime;
    attr.sched_period = period;
    attr.sched_deadline = deadline;

    int ret = sched_setattr(pid, &attr, 0);
    if (ret < 0) {
        done = 0;
        perror("sched_setattr");
        exit(-1);
    }

    struct sched_attr param;
    ret = sched_getattr(pid, &param, sizeof(param), 0);
    if (ret < 0) {
        done = 0;
        perror("sched_getattr");
        exit(-1);
    }
}

void sig_handler(int signo)
{
    if (signo == SIGXCPU)
        printf("received SIGXCPU\n");
}

int main (void) {
    printf("MY PID: %d\n", getpid());
    set_rt(gettid(), 500000000Ul, 1000000000Ul, 1000000000Ul);

    struct sigaction s;
    bzero(&s, sizeof(s));
    s.sa_handler = sig_handler;
    sigaction (SIGXCPU, &s, NULL);

    //test signal handler
    kill(getpid(), SIGXCPU);

    int i = 0;
    while (1) {
        i++;
        printf("Loop Number %d\n", i);
        int k = 900000000;
        if (i == 3) {
            while (k > 0) k--;
        }
        sched_yield();
    }
}

相关内容