cgroups的实现
1. cgroups体系结构
每个内核的子系统如果想要挂载到cgroup系统中,必须要先拥有一个cgroup_subsys对象,通过这个对象将对子系统资源的操作函数以接口的形式进行约定,各个子系统再根据自己的需求去实现这些接口,除了这些函数指针,cgroup_subsys结构体还包括id, name, early_init等属性,分别表示该子系统在cgroup->subsys[]数组中的索引,子系统名称,在系统启动时是否需要尽早初始化等。
struct cgroup_subsys{
struct cgroup_subsys_state* (*css_alloc)(struct cgroup_subsys_state *parent_css);
void (*attach)(struct cgroup_taskset *tset);
...
bool early_init: 1;
ind id;
const char *name;
struct cgroup_root *root;
}
除了这些,cgroup_subsys还有一个指向cgroup_root类型的root指针,cgroup_root表示一个cgroup hierarchy的根,注意与根结点相区别,所以说一个子系统只能通过cgroup_root结构与一个cgroup hierarchy绑定(但是反过来一个cgroup hierarchy可以绑定多个子系统 )。关于cgroup_root下面具体讨论。
再看第一个函数指针css_alloc,其返回了一个cgroup_subsys_state结构体,每一个注册到系统中的cgroup子系统,除了代表它的cgroup_subsys,还有与之相关联的cgroup_subsys_state,简称css,用来表示某个子系统与某个cgroup相关联的状态,将一个子系统与一个cgroup相连接。每个subsys会对调用css_alloc的cgroup分配一个自己的cgroup_subsys_state结构。
struct cgroup_subsys_state {
/* PI: the cgroup that this css is attached to */
struct cgroup *cgroup;
/* PI: the cgroup subsystem that this css is attached to */
struct cgroup_subsys *ss;
/* reference count - access via css_[try]get() and css_put() */
struct percpu_ref refcnt;
/* siblings list anchored at the parent's ->children */
struct list_head sibling;
struct list_head children;
/*
* PI: Subsys-unique ID. 0 is unused and root is always 1. The
* matching css can be looked up using css_from_id().
*/
int id;
unsigned int flags;
...
struct cgroup_subsys_state *parent;
};
- cgroup指向此css所关联的cgroup
- ss指向此css所关联的cgroup_subsys
- sibling是一个双向链表,保存所关联的cgroup拥有的其他css
- parent指向父css的指针
提到css,那就不得不提css_set,css_set是保存指向cgroup_subsys_state对象的一组引用计数指针的集合,用来保存与task相关的cgroup信息,同时也代表了一组子系统资源的组合,css_set的使用节省了cgroups在task_struct结构体中所占用的空间,而且加快了task fork()/exit()的速度。
struct css_set {
/*
* Set of subsystem states, one for each subsystem. This array is
* immutable after creation apart from the init_css_set during
* subsystem registration (at boot time).
*/
struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];
/* reference count */
refcount_t refcount;
...
/*
* Lists running through all tasks using this cgroup group.
*/
struct list_head tasks;
/*
* List running through all cgroup groups in the same hash
* slot. Protected by css_set_lock
*/
struct hlist_node hlist;
/*
* List of cgrp_cset_links pointing at cgroups referenced from this
* css_set. Protected by css_set_lock.
*/
struct list_head cgrp_links;
/* dead and being drained, ignore for migration */
bool dead;
/* For RCU-protected deletion */
struct

最低0.47元/天 解锁文章
1361

被折叠的 条评论
为什么被折叠?



