Cgroups 데이터 구조 디자인

10836 단어 linux
우 리 는 프로 세 스 에서 출발 하여 cgroups 관련 데이터 구조 간 의 관 계 를 분석 합 니 다.
Linux 에서 관리 프로 세 스 의 데이터 구 조 는 task 입 니 다.struct, 그 중 cgroups 와 관련 된 것:
#ifdef CONFIG_CGROUPS
	/* Control Group info protected by css_set_lock */
	struct css_set __rcu *cgroups;
	/* cg_list protected by css_set_lock and tsk->alloc_lock */
	struct list_head cg_list;
#endif

그 중에서 cgroups 포인터 가 css 를 가리 키 고 있 습 니 다.set 구조, cssset 는 프로 세 스 와 관련 된 cgroups 정 보 를 저장 합 니 다.cg_list 는 끼 워 넣 은 listhead 구조, 같은 css 까지 연습 하 는 데 사용set 프로 세 스 가 링크 로 구성 되 었 습 니 다.다음은 cssset 의 구조:
struct css_set {

	/* Reference count */
	atomic_t refcount;

	/*
	 * List running through all cgroup groups in the same hash
	 * slot. Protected by css_set_lock
	 */
	struct hlist_node hlist;

	/*
	 * List running through all tasks using this cgroup
	 * group. Protected by css_set_lock
	 */
	struct list_head tasks;

	/*
	 * List of cg_cgroup_link objects on link chains from
	 * cgroups referenced from this css_set. Protected by
	 * css_set_lock
	 */
	struct list_head cg_links;

	/*
	 * 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) and modular subsystem
	 * loading/unloading.
	 */
	struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];

	/* For RCU-protected deletion */
	struct rcu_head rcu_head;
};

그 중 refcount 는 이 cssset 의 인용 수, cssset 는 여러 프로 세 스 가 공용 할 수 있 습 니 다. 이 프로 세 스 의 cgroups 정보 가 같 으 면, 예 를 들 어 만 든 모든 단계 에서 같은 cgroup 에 있 는 프로 세 스 입 니 다.
hlist 는 끼 워 넣 은 hlistnode, 모든 cssset 는 hash 표 로 구성 되 어 있 습 니 다. 커 널 은 특정한 css 를 빠르게 찾 을 수 있 습 니 다.set。
tasks 는 이 css 연습 을 가리 키 고 있 습 니 다.set 프로 세 스 가 연 습 된 링크 입 니 다.
cg_링크 는 struct cgcgroup_링크 링크
subsys 는 포인터 배열 로 cgroup 을 가리 키 는 그룹 을 저장 합 니 다.subsys_state 지침.하나의 cgroupsubsys_state 는 프로 세 스 가 특정 서브 시스템 과 관련 된 정보 입 니 다.이 포인터 배열 을 통 해 프로 세 스 는 해당 하 는 cgroups 제어 정 보 를 얻 을 수 있 습 니 다.
다음은 cgroupsubsys_state 의 구조:
struct cgroup_subsys_state {
	/*
	 * The cgroup that this subsystem is attached to. Useful
	 * for subsystems that want to know about the cgroup
	 * hierarchy structure
	 */
	struct cgroup *cgroup;

	/*
	 * State maintained by the cgroup system to allow subsystems
	 * to be "busy". Should be accessed via css_get(),
	 * css_tryget() and and css_put().
	 */

	atomic_t refcnt;

	unsigned long flags;
	/* ID for this css, if possible */
	struct css_id __rcu *id;
};

cgroup 지침 은 프로 세 스 가 속 하 는 cgroup 구 조 를 가리 키 고 있 습 니 다.프로 세 스 는 서브 시스템 의 통 제 를 받 습 니 다. 실제 적 으로 특정한 cgroup 에 가입 하여 이 루어 집 니 다. cgroup 은 특정한 등급 에 있 고 서브 시스템 은 등급 에 부가 되 기 때 문 입 니 다.상기 세 가지 구 조 를 통 해 프로 세 스 는 cgroup 와 연결 할 수 있 습 니 다: taskstruct->css_set->cgroup_subsys_state->cgroup。
다음은 cgroup 의 구 조 를 살 펴 보 겠 습 니 다.
struct cgroup {
	unsigned long flags;		/* "unsigned long" so bitops work */

	/*
	 * count users of this cgroup. >0 means busy, but doesn't
	 * necessarily indicate the number of tasks in the cgroup
	 */
	atomic_t count;

	/*
	 * We link our 'sibling' struct into our parent's 'children'.
	 * Our children link their 'sibling' into our 'children'.
	 */
	struct list_head sibling;	/* my parent's children */
	struct list_head children;	/* my children */

	struct cgroup *parent;		/* my parent */
	struct dentry __rcu *dentry;	/* cgroup fs entry, RCU protected */

	/* Private pointers for each registered subsystem */
	struct cgroup_subsys_state *subsys[CGROUP_SUBSYS_COUNT];

	struct cgroupfs_root *root;
	struct cgroup *top_cgroup;

	/*
	 * List of cg_cgroup_links pointing at css_sets with
	 * tasks in this cgroup. Protected by css_set_lock
	 */
	struct list_head css_sets;

	/*
	 * Linked list running through all cgroups that can
	 * potentially be reaped by the release agent. Protected by
	 * release_list_lock
	 */
	struct list_head release_list;

	/*
	 * list of pidlists, up to two for each namespace (one for procs, one
	 * for tasks); created on demand.
	 */
	struct list_head pidlists;
	struct mutex pidlist_mutex;

	/* For RCU-protected deletion */
	struct rcu_head rcu_head;

	/* List of events which userspace want to receive */
	struct list_head event_list;
	spinlock_t event_list_lock;
};

sibling, children, parent 세 개의 끼 워 넣 은 listhead 는 같은 등급 의 cgroup 를 cgroup 트 리 로 연결 하 는 것 을 책임 집 니 다.
subsys 는 포인터 배열 로 cgroup 을 가리 키 는 그룹 을 저장 합 니 다.subsys_state 지침.이 지침 은 이 cgroup 가 각 하위 시스템 과 관련 된 정 보 를 가리 키 는데 이것 은 cssset 의 이 치 는 같다.
root 는 cgroupfs 를 가리 키 고 있 습 니 다.루트 의 구 조 는 cgroup 이 있 는 등급 에 대응 하 는 구조 체 입 니 다.이렇게 되면 이전의 몇 개의 cgroups 개념 이 모두 연결 되 었 다.
top_cgroup 는 해당 등급 의 루트 cgroup 를 가리 키 고 있 습 니 다. 즉, 등급 을 만 들 때 자동 으로 만 든 cgroup 입 니 다.
css_set 는 struct cgcgroup_링크 연 결 된 링크, cssset 의 cg링크 는 같다.
다음은 css 분석set 와 cgroup 사이 의 관계.먼저 cgcgroup_링크 의 구조:
struct cg_cgroup_link {
	/*
	 * List running through cg_cgroup_links associated with a
	 * cgroup, anchored on cgroup->css_sets
	 */
	struct list_head cgrp_link_list;
	struct cgroup *cgrp;
	/*
	 * List running through cg_cgroup_links pointing at a
	 * single css_set object, anchored on css_set->cg_links
	 */
	struct list_head cg_link_list;
	struct css_set *cg;
};

cgrp_link_list cgroup - > cssset 가 가리 키 는 링크, cgrp 는 이 cg 를 가리 키 고 있 습 니 다.cgroup_링크 관련 cgroup
cg_link_list 는 css 에 연 결 됩 니 다.set->cg_링크 가 가리 키 는 링크, cg 는 이 cgcgroup_링크 관련 cssset。
설계 원리:
cgroup 와 css 때문에set 는 여러 쌍 의 관계 로 중간 구 조 를 추가 하여 이들 을 연결 시 켜 야 한다. 이것 은 데이터 베이스 모델 디자인 과 일리 가 있다.cg_cgroup_링크 의 cgrp 와 cg 는 이 구조 체 의 결합 키 이 고 cgrplink_list 와 cglink_list 는 각각 cgroup 와 css 에 연결 되 어 있 습 니 다.set 해당 링크, cgroup 또는 cssset 에서 검색 을 할 수 있 습 니 다.
프로 세 스 대응 cssset, cssset 는 각 하위 시스템 과 관련 된 정 보 를 저장 합 니 다. 그러나 이 정 보 는 하나의 cgroup 에서 얻 은 것 이 아 닐 수도 있 습 니 다. 하나의 프로 세 스 가 몇 개의 cgroup 에 동시에 속 할 수 있 기 때 문 입 니 다. 이 cgroup 가 같은 등급 에 있 지 않 으 면.예 를 들 어 우 리 는 하나의 등급 A 를 만 들 었 습 니 다. A 위 에 cpu 와 memory 두 개의 서브 시스템 이 추가 되 었 습 니 다. 프로 세 스 B 는 A 의 루트 cgroup 에 속 합 니 다.그 다음 에 우 리 는 한 단계 의 C 를 만 들 었 습 니 다. C 위 에 ns 와 blkio 두 개의 키 시스템 이 추가 되 었 습 니 다. 프로 세 스 B 역시 C 의 루트 cgroup 에 속 합 니 다.프로 세 스 B 에 대응 하 는 cpu 와 memory 의 정 보 는 A 와 cgroups 에서 얻 었 고 ns 와 blkio 정 보 는 C 와 cgroup 에서 얻 었 습 니 다.그래서 하나의 cssset 저 장 된 cgroupsubsys_state 는 여러 개의 cgroup 에 대응 할 수 있 습 니 다.다른 한편, cgroup 도 cgroup 을 저장 했다.subsys_state, 이 그룹 cgroupsubsys_state 는 cgroup 이 있 는 등급 에 추 가 된 서브 시스템 에서 얻 은 것 입 니 다.하나의 cgroup 시 계 는 여러 프로 세 스 를 가 질 수 있 으 며, 이 프로 세 스 의 cssset 가 모두 같은 것 은 아 닙 니 다. 일부 프로 세 스 가 다른 cgroup 에 추가 되 었 을 수도 있 기 때 문 입 니 다.그러나 같은 cgroup 의 프로 세 스 가 이 cgroup 와 연 결 된 cgroupsubsys_state 는 모두 이 cgroup 의 관 리 를 받 기 때문에 하나의 cgroup 도 여러 css 에 대응 할 수 있 습 니 다.set。
앞의 분석 을 통 해 알 수 있 듯 이 task 에서 cgroup 까지 쉽게 위 치 를 정할 수 있 지만 cgroup 에서 이 cgroup 의 모든 task 를 얻 으 려 면 이 구 조 를 통과 해 야 합 니 다.모든 프로 세 스 가 css 를 가리 키 고 있 습 니 다.set, 그리고 이 cssset 와 연 결 된 모든 프로 세 스 가 css - > tasks 링크 에 연 결 됩 니 다.그리고 cgroup 는 또 하나의 중간 구 조 를 통 해 cgcgroup_링크 는 그 와 관련 된 모든 css 를 찾 습 니 다.set, cgroup 와 연 결 된 모든 프로 세 스 를 얻 을 수 있 습 니 다.
마지막 으로 하층 급 과 서브 시스템 에 대응 하 는 구조 체 를 보면 층계 에 대응 하 는 구조 체 는 cgroupfs 이다.root:
struct cgroupfs_root {
	struct super_block *sb;

	/*
	 * The bitmask of subsystems intended to be attached to this
	 * hierarchy
	 */
	unsigned long subsys_bits;

	/* Unique id for this hierarchy. */
	int hierarchy_id;

	/* The bitmask of subsystems currently attached to this hierarchy */
	unsigned long actual_subsys_bits;

	/* A list running through the attached subsystems */
	struct list_head subsys_list;

	/* The root cgroup for this hierarchy */
	struct cgroup top_cgroup;

	/* Tracks how many cgroups are currently defined in hierarchy.*/
	int number_of_cgroups;

	/* A list running through the active hierarchies */
	struct list_head root_list;

	/* Hierarchy-specific flags */
	unsigned long flags;

	/* The path to use for release notifications. */
	char release_agent_path[PATH_MAX];

	/* The name for this hierarchy - may be empty */
	char name[MAX_CGROUP_ROOT_NAMELEN];
};

sb 이 계층 과 연 결 된 파일 시스템 슈퍼 블록 을 가리 키 고 있 습 니 다.
subsys_bits 와 actualsubsys_bits 는 각각 등급 에 부가 할 서브 시스템 과 현재 실제 등급 에 부가 할 서브 시스템 을 가리 키 며, 서브 시스템 이 등급 에 부가 할 때 사용 합 니 다.
hierarchy_id 는 이 등급 의 유일한 id 입 니 다.
top_cgroup 은 이 단계 의 루트 cgroup 를 가리 키 고 있 습 니 다.
number_of_cgroups 는 이 등급 의 cgroup 개 수 를 기록 합 니 다.
root_list 는 끼 워 넣 은 listhead, 시스템 의 모든 등급 을 링크 로 연결 하 는 데 사용 합 니 다.
서브 시스템 에 대응 하 는 구조 체 는 cgroupsubsys:
struct cgroup_subsys {
	struct cgroup_subsys_state *(*create)(struct cgroup *cgrp);
	int (*pre_destroy)(struct cgroup *cgrp);
	void (*destroy)(struct cgroup *cgrp);
	int (*can_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
	void (*cancel_attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
	void (*attach)(struct cgroup *cgrp, struct cgroup_taskset *tset);
	void (*fork)(struct task_struct *task);
	void (*exit)(struct cgroup *cgrp, struct cgroup *old_cgrp,
		     struct task_struct *task);
	int (*populate)(struct cgroup_subsys *ss, struct cgroup *cgrp);
	void (*post_clone)(struct cgroup *cgrp);
	void (*bind)(struct cgroup *root);

	int subsys_id;
	int active;
	int disabled;
	int early_init;
	/*
	 * True if this subsys uses ID. ID is not available before cgroup_init()
	 * (not available in early_init time.)
	 */
	bool use_id;
#define MAX_CGROUP_TYPE_NAMELEN 32
	const char *name;

	/*
	 * Protects sibling/children links of cgroups in this
	 * hierarchy, plus protects which hierarchy (or none) the
	 * subsystem is a part of (i.e. root/sibling).  To avoid
	 * potential deadlocks, the following operations should not be
	 * undertaken while holding any hierarchy_mutex:
	 *
	 * - allocating memory
	 * - initiating hotplug events
	 */
	struct mutex hierarchy_mutex;
	struct lock_class_key subsys_key;

	/*
	 * Link to parent, and list entry in parent's children.
	 * Protected by this->hierarchy_mutex and cgroup_lock()
	 */
	struct cgroupfs_root *root;
	struct list_head sibling;
	/* used when use_id == true */
	struct idr idr;
	spinlock_t id_lock;

	/* should be defined only by modular subsystems */
	struct module *module;
};

cgroup_subsys 는 각 하위 시스템 이 각자 의 수요 에 따라 이 루어 지도 록 한 조 의 조작 을 정의 했다.이것 은 C + + 에서 추상 적 인 기본 클래스 에 해당 하 며, 각 특정한 서브 시스템 은 cgroup 에 대응 합 니 다.subsys 는 해당 조작 을 실현 한 하위 클래스 입 니 다.유사 한 사상 은 cgroup 에 도 사용 되 었 다.subsys_state 중, cgroupsubsys_state 는 제어 정 보 를 정의 하지 않 고 각 서브 시스템 에 필요 한 공 통 된 정 보 를 정의 합 니 다. 예 를 들 어 이 cgroupsubsys_state 종속 cgroup.그리고 각 서브 시스템 은 각자 의 수요 에 따라 자신의 프로 세 스 제어 정보 구조 체 를 정의 하고 마지막 으로 각자 의 구조 체 에서 cgroupsubsys_state 포함, 이렇게 Linux 커 널 을 통과 하 는 containerof 매크로 는 cgroupsubsys_state 에서 해당 하 는 구조 체 를 가 져 옵 니 다.

좋은 웹페이지 즐겨찾기