Exemple #1
0
int
ProcFamily::migrate_to_cgroup(pid_t pid)
{
	// Attempt to migrate a given process to a cgroup.
	// This can be done without regards to whether the
	// process is already in the cgroup
	if (!m_cgroup.isValid()) {
		return 1;
	}

	// We want to make sure task migration is turned on for the
	// associated memory controller.  So, we get to look up the original cgroup.
	//
	// If there is no memory controller present, we skip all this and just attempt a migrate
	int err;
	u_int64_t orig_migrate;
	bool changed_orig = false;
	char * orig_cgroup_string = NULL;
	struct cgroup * orig_cgroup;
	struct cgroup_controller * memory_controller;
	if (m_cm.isMounted(CgroupManager::MEMORY_CONTROLLER) && (err = cgroup_get_current_controller_path(pid, MEMORY_CONTROLLER_STR, &orig_cgroup_string))) {
		dprintf(D_PROCFAMILY,
			"Unable to determine current memory cgroup for PID %u (ProcFamily %u): %u %s\n",
			pid, m_root_pid, err, cgroup_strerror(err));
		return 1;
	}
	// We will migrate the PID to the new cgroup even if it is in the proper memory controller cgroup
	// It is possible for the task to be in multiple cgroups.
	if (m_cm.isMounted(CgroupManager::MEMORY_CONTROLLER) && (orig_cgroup_string != NULL) && (strcmp(m_cgroup_string.c_str(), orig_cgroup_string))) {
		// Yes, there are race conditions here - can't really avoid this.
		// Throughout this block, we can assume memory controller exists.
		// Get original value of migrate.
		orig_cgroup = cgroup_new_cgroup(orig_cgroup_string);
		ASSERT (orig_cgroup != NULL);
		if ((err = cgroup_get_cgroup(orig_cgroup))) {
			dprintf(D_PROCFAMILY,
				"Unable to read original cgroup %s (ProcFamily %u): %u %s\n",
				orig_cgroup_string, m_root_pid, err, cgroup_strerror(err));
			goto after_migrate;
		}
		if ((memory_controller = cgroup_get_controller(orig_cgroup, MEMORY_CONTROLLER_STR)) == NULL) {
			cgroup_free(&orig_cgroup);
			goto after_migrate;
		}
		if ((err = cgroup_get_value_uint64(memory_controller, "memory.move_charge_at_immigrate", &orig_migrate))) {
			if (err == ECGROUPVALUENOTEXIST) {
				// Older kernels don't have the ability to migrate memory accounting to the new cgroup.
				dprintf(D_PROCFAMILY,
					"This kernel does not support memory usage migration; cgroup %s memory statistics"
					" will be slightly incorrect (ProcFamily %u)\n",
					m_cgroup_string.c_str(), m_root_pid);
			} else {
				dprintf(D_PROCFAMILY,
					"Unable to read cgroup %s memory controller settings for "
					"migration (ProcFamily %u): %u %s\n",
					orig_cgroup_string, m_root_pid, err, cgroup_strerror(err));
			}
			cgroup_free(&orig_cgroup);
			goto after_migrate;
		}
		if (orig_migrate != 3) {
			orig_cgroup = cgroup_new_cgroup(orig_cgroup_string);
			memory_controller = cgroup_add_controller(orig_cgroup, MEMORY_CONTROLLER_STR);
			ASSERT (memory_controller != NULL); // Memory controller must already exist
			cgroup_add_value_uint64(memory_controller, "memory.move_charge_at_immigrate", 3);
			if ((err = cgroup_modify_cgroup(orig_cgroup))) {
				// Not allowed to change settings
				dprintf(D_ALWAYS,
					"Unable to change cgroup %s memory controller settings for migration. "
					"Some memory accounting will be inaccurate (ProcFamily %u): %u %s\n",
					orig_cgroup_string, m_root_pid, err, cgroup_strerror(err));
			} else {
				changed_orig = true;
			}
		}
		cgroup_free(&orig_cgroup);
	}

after_migrate:

	orig_cgroup = NULL;
	err = cgroup_attach_task_pid(& const_cast<struct cgroup &>(m_cgroup.getCgroup()), pid);
	if (err) {
		dprintf(D_PROCFAMILY,
			"Cannot attach pid %u to cgroup %s for ProcFamily %u: %u %s\n",
			pid, m_cgroup_string.c_str(), m_root_pid, err, cgroup_strerror(err));
	}

	if (changed_orig) {
		if ((orig_cgroup = cgroup_new_cgroup(orig_cgroup_string))) {
			goto after_restore;
		}
		if (((memory_controller = cgroup_add_controller(orig_cgroup, MEMORY_CONTROLLER_STR)) != NULL) &&
			(!cgroup_add_value_uint64(memory_controller, "memory.move_charge_at_immigrate", orig_migrate))) {
			cgroup_modify_cgroup(orig_cgroup);
		}
		cgroup_free(&orig_cgroup);
	}


after_restore:
	if (orig_cgroup_string != NULL) {
		free(orig_cgroup_string);
	}
	return err;
}
Exemple #2
0
static int corosync_move_to_root_cgroup(void) {
	int res = -1;
#ifdef HAVE_LIBCGROUP
	int cg_ret;
	struct cgroup *root_cgroup = NULL;
	struct cgroup_controller *root_cpu_cgroup_controller = NULL;
	char *current_cgroup_path = NULL;

	cg_ret = cgroup_init();
	if (cg_ret) {
		log_printf(LOGSYS_LEVEL_WARNING, "Unable to initialize libcgroup: %s ",
		    cgroup_strerror(cg_ret));

		goto exit_res;
	}

	cg_ret = cgroup_get_current_controller_path(getpid(), "cpu", &current_cgroup_path);
	if (cg_ret) {
		log_printf(LOGSYS_LEVEL_WARNING, "Unable to get current cpu cgroup path: %s ",
		    cgroup_strerror(cg_ret));

		goto exit_res;
	}

	if (strcmp(current_cgroup_path, "/") == 0) {
		log_printf(LOGSYS_LEVEL_DEBUG, "Corosync is already in root cgroup path");

		res = 0;
		goto exit_res;
	}

	root_cgroup = cgroup_new_cgroup("/");
	if (root_cgroup == NULL) {
		log_printf(LOGSYS_LEVEL_WARNING, "Can't create root cgroup");

		goto exit_res;
	}

	root_cpu_cgroup_controller = cgroup_add_controller(root_cgroup, "cpu");
	if (root_cpu_cgroup_controller == NULL) {
		log_printf(LOGSYS_LEVEL_WARNING, "Can't create root cgroup cpu controller");

		goto exit_res;
	}

	cg_ret = cgroup_attach_task(root_cgroup);
	if (cg_ret) {
		log_printf(LOGSYS_LEVEL_WARNING, "Can't attach task to root cgroup: %s ",
		    cgroup_strerror(cg_ret));

		goto exit_res;
	}

	cg_ret = cgroup_get_current_controller_path(getpid(), "cpu", &current_cgroup_path);
	if (cg_ret) {
		log_printf(LOGSYS_LEVEL_WARNING, "Unable to get current cpu cgroup path: %s ",
		    cgroup_strerror(cg_ret));

		goto exit_res;
	}

	if (strcmp(current_cgroup_path, "/") == 0) {
		log_printf(LOGSYS_LEVEL_NOTICE, "Corosync successfully moved to root cgroup");
		res = 0;
	} else {
		log_printf(LOGSYS_LEVEL_WARNING, "Can't move Corosync to root cgroup");
	}

exit_res:
	if (root_cgroup != NULL) {
		cgroup_free(&root_cgroup);
	}

	/*
	 * libcgroup doesn't define something like cgroup_fini so there is no way how to clean
	 * it's cache. It has to be called when libcgroup authors decide to implement it.
	 */

#endif
	 return (res);
}