Linux 2.6.33 的 sched_fair.c 中定义的 run_node 在哪里?
Where is run_node defined in the sched_fair.c of Linux 2.6.33?
我正在阅读Robert Love's Book Linux Kernel Development。
演示使用2.6.33内核
我一直在浏览源代码的某些部分,但无法找出很多东西的初始定义在哪里。很多东西都是随便用的,比如"magic"我没找到定义
一个例子:
static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
{
struct rb_node *left = cfs_rq->rb_leftmost;
if (!left)
return NULL;
return rb_entry(left, struct sched_entity, run_node);
}
static struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
{
struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
if (!last)
return NULL;
return rb_entry(last, struct sched_entity, run_node);
}
这是在 2.6.33 内核的 kernel/sched_fair.c
行 384
和 394
中。
run_node
是从哪里来的?
我已经在 here 上 grep 了整个源代码库,但我还没有找到 run_node
的任何定义,允许它像这样使用。
在 sched_entity
结构中有一个 declaration,但在它之外没有任何东西可以像这样使用它。
我不明白事情是如何组织的,真的很混乱。
这是怎么回事?
你看到的run_node
不是变量,是sched_entity
结构体的.run_node
字段。
rb_entry()
is a macro which is basically an alias for container_of()
:
#define rb_entry(ptr, type, member) container_of(ptr, type, member)
container_of()
宏用于在给定指向已知字段 (ptr
)、结构类型 (type
) 和字段名称的指针的情况下获取指向结构的指针(member
)。在您的情况下,left
是指向某些 struct sched_entity
的 run_node
字段的指针,因此 container_of()
基本上用于获取指向适当 sched_entity
的指针。
另请参阅:Understanding container_of macro in the Linux kernel。
我正在阅读Robert Love's Book Linux Kernel Development。
演示使用2.6.33内核
我一直在浏览源代码的某些部分,但无法找出很多东西的初始定义在哪里。很多东西都是随便用的,比如"magic"我没找到定义
一个例子:
static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
{
struct rb_node *left = cfs_rq->rb_leftmost;
if (!left)
return NULL;
return rb_entry(left, struct sched_entity, run_node);
}
static struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
{
struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
if (!last)
return NULL;
return rb_entry(last, struct sched_entity, run_node);
}
这是在 2.6.33 内核的 kernel/sched_fair.c
行 384
和 394
中。
run_node
是从哪里来的?
我已经在 here 上 grep 了整个源代码库,但我还没有找到 run_node
的任何定义,允许它像这样使用。
在 sched_entity
结构中有一个 declaration,但在它之外没有任何东西可以像这样使用它。
我不明白事情是如何组织的,真的很混乱。
这是怎么回事?
你看到的run_node
不是变量,是sched_entity
结构体的.run_node
字段。
rb_entry()
is a macro which is basically an alias for container_of()
:
#define rb_entry(ptr, type, member) container_of(ptr, type, member)
container_of()
宏用于在给定指向已知字段 (ptr
)、结构类型 (type
) 和字段名称的指针的情况下获取指向结构的指针(member
)。在您的情况下,left
是指向某些 struct sched_entity
的 run_node
字段的指针,因此 container_of()
基本上用于获取指向适当 sched_entity
的指针。
另请参阅:Understanding container_of macro in the Linux kernel。