Linux内核线程实时获取调度状态的方法

最近在做项目中,需要线程实时的知道自身被sched out 和sched in. 查看了schedule()函数实现,发现内核提供了对应的notifier机制.

1.进程sched out流程

schedule->__schedule->context_switch->prepare_task_switch->

fire_sched_out_preempt_notifiers->__fire_sched_out_preempt_notifiers:

1
2
3
4
5
6
7
8
9
static void
__fire_sched_out_preempt_notifiers(struct task_struct *curr,
                   struct task_struct *next)
{
    struct preempt_notifier *notifier;
        /*调用curr注册的notifier,通知当前进程被sched out */
    hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
        notifier->ops->sched_out(notifier, next);
}

2.进程sched in

schedule->__schedule->context_switch->finish_task_switch->

fire_sched_in_preempt_notifiers->

1
2
3
4
5
6
7
static void __fire_sched_in_preempt_notifiers(struct task_struct *curr)
{
    struct preempt_notifier *notifier;
        /*通知进程sched in  */
    hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
        notifier->ops->sched_in(notifier, raw_smp_processor_id());
}

3. notifier注册

Linux内核提供API为当前进程注册调度notifier

void preempt_notifier_register(struct preempt_notifier *notifier)

void preempt_notifier_unregister(struct preempt_notifier *notifier)

void preempt_notifier_inc(void)

void preempt_notifier_dec(void)

notifer_register实现如下:

1
2
3
4
5
6
7
void preempt_notifier_register(struct preempt_notifier *notifier)
{
    if (!static_branch_unlikely(&preempt_notifier_key))
        WARN(1, "registering preempt_notifier while notifiers disabled\n");
        /*把notifer加到当前进程preempt_notifier链表*/
    hlist_add_head(&notifier->link, &current->preempt_notifiers);
}

4.任意进程调度监听

preempt_notifier_register函数只能为当前进程注册调度notifier,可以进行适当修改,从而注册任意进程调度Notifier

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int task_notifier_register(struct preempt_notifier *notifier,pid_t pid)
{
    struct pid *pid ;
    struct task_struct *task ;

    pid = find_get_pid(pid);
    if(!pid)
        return -EIO ;
    task = get_pid_task(pid,PIDTYPE_PID);
    if(!task)
        return -EIO ;
    hlist_add_head(&notifier->link, &task->preempt_notifiers);
    return 0;
}

5. 用途

1.需要关注进程调度的特定场景,如指令数据采集

2. 用于安全领域进程之间的对抗