Linuxカーネルスレッドによるスケジューリングステータスのリアルタイム取得方法
2355 ワード
最近プロジェクトをする中で、スレッドはリアルタイムで自分が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:
2.プロセスsched in
schedule->__schedule->context_switch->finish_task_switch->
fire_sched_in_preempt_notifiers->
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は以下のように実現される.
4.任意のプロセスによるリスニングのスケジュール
preempt_notifier_register関数は、現在のプロセスに対してのみスケジューリングnotifierを登録できます.適切に変更して、任意のプロセススケジューリングNotifierを登録できます.
5.用途
1.命令データ収集など、プロセススケジューリングの特定シーンに注目する必要がある
2.セキュリティレルムプロセス間の対抗
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
");
/* 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.セキュリティレルムプロセス間の対抗