最近在做項目中,需要線程實時的知道自身被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:
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->
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實現如下:
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(¬ifier->link, ¤t->preempt_notifiers);
}
4.任意進程調度監聽
preempt_notifier_register函數只能爲當前進程註冊調度notifier,可以進行適當修改,從而註冊任意進程調度Notifier
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(¬ifier->link, &task->preempt_notifiers);
return 0;
}
5. 用途
1.需要關注進程調度的特定場景,如指令數據採集
2. 用於安全領域進程之間的對抗