示例#1
0
/*
 * Assumes the cgroup is already mounted at /cgroup/memory/a
 *
 * Assumes some processes are already in the cgroup
 *
 * Assumes it is the memory controller is mounted in at that
 * point
 */
int main()
{
	int size;
	pid_t *pids;
	int ret;
	int i;

	ret = cgroup_init();
	if (ret) {
		printf("FAIL: cgroup_init failed with %s\n", cgroup_strerror(ret));
		exit(3);
	}

	ret = cgroup_get_procs("a", "memory", &pids, &size);
	if (ret) {
		printf("FAIL: cgroup_get_procs failed with %s\n", cgroup_strerror(ret));
		exit(3);
	}

	for (i = 0; i < size; i++)
		printf("%u\n", pids[i]);

	return 0;
}
示例#2
0
/*
 * Read the proc statistics information of given group name.
 *
 * IN:
 * @group_name :          The given group name.
 * @stats_lists:          The lists of proc statistics information.
 *
 * RETURN
 * 0                     Success.
 * -1                    Failed.
 */
int read_cg_proc_stats(const char* group_name, cg_proc_stats_lists* stats_lists) {

	FILE *fp = NULL;
	int i = 0, j = 0, num = 0;
	char* pre = NULL;
	pid_t* procs;
	char proc_name[FN_LEN] = { 0 };
	char line[MAX_LINE_LEN] = { 0 };
	cg_proc_stats* cg_stats = NULL;

	if (!group_name || !stats_lists) {
#ifdef DEBUG
		fprintf(stderr,
				"rd_stats: the group name or io stats lists is null.\n");
#endif
		return -1;
	}
	/** Clear the history information.*/
	clear_proc_stats(stats_lists);
	/** Get all of the process id in the group. But the variables procs should
	 *  be free at the end of this procedure.*/
	if (cgroup_get_procs(group_name, CG_CPUSET, &procs, &j)) {
#ifdef DEBUG
		fprintf(stderr,
				"rd_stats: read the value of procs in cpuset %s failed\n",
				group_name);
#endif
		return -1;
	}
	/** Read the io statistics information of each process id(/proc/<PID>/io).
	 *  Initialized the io statistics information and store the value of the
	 *  parameter. Finally add the io statistics information into the lists.*/
	for (i = 0; i < j; i++) {
		/** Initialized the io statistics information.*/
		if (init_proc_stats(&cg_stats, procs[i])) {
			fprintf(stderr, "rd_stats: initialize the io stats failed\n");
			goto err;
		}
		if (read_proc_io(cg_stats->io_stats, procs[i])
				|| read_proc_stat(cg_stats->proc_stat, procs[i])
				|| read_proc_statm(cg_stats->proc_statm, procs[i])) {
#ifdef DEBUG
			fprintf(stderr, "rd_stats: read the proc io stats, proc"
					" stat and proc statm into io stats list failed\n");
#endif
			goto err;
		}
		/** Add the io statistics information into the lists.*/
		if (add_proc_stats(stats_lists, cg_stats)) {
#ifdef DEBUG
			fprintf(stderr,
					"rd_stats: add the io stats into io stats list failed\n");
#endif
			goto err;
		}
	}
	if (procs)
		free(procs);
	return 0;

	err: if (cg_stats)
		uninit_proc_stats(cg_stats);
	clear_proc_stats(stats_lists);
	if (procs)
		free(procs);
	if (fp)
		fclose(fp);
	return -1;
}