argo_container.cpp 10.8 KB
Newer Older
Swann Perarnau's avatar
Swann Perarnau committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
#include "argo_container.hpp"
#include <algorithm>
#include <pwd.h>
#include <stdio.h>
#include <sstream>
#include "utils.hpp"
#include <fstream>
#include "int_list_parser.hpp"
#include <signal.h>

Argo_container::Argo_container(const string& parent_path, 
		String_parser& sp)
{
	_created = false;
	_parent_path = parent_path;
	if(_parent_path[_parent_path.size() - 1] != '/')
		_parent_path += '/';
	_commit_type = CCT_NONE;
	_owner = ruid;

	_cpu_exclusive = true;
	_mem_exclusive = false;
	_load_balancing = false;
	_mem_migrate = true;

	sp.get_string("name", _name); 
	sp.get_int_list("cpus", _cpus);
	sp.get_int_list("mems", _mems);
	sp.get_bool("cpu_exclusive", _cpu_exclusive);
	sp.get_bool("mem_exclusive", _mem_exclusive);
	sp.get_bool("load_balancing", _load_balancing);
	sp.get_bool("mem_migrate", _mem_migrate);

	_older_name = _name;
}


/*This one builds a non-functional (empty) Argo_container that must
*subsequently be reloaded from teh cgroup fs*/
Argo_container::Argo_container(const string& parent_path,
		const string& name, uid_t owner)
{
	_created = false;
	_parent_path = parent_path;
	if(_parent_path[_parent_path.size() - 1] != '/')
		_parent_path += '/';
	_older_name = _name = name;
	_owner = owner;
	_cpu_exclusive = true;
	_mem_exclusive = false;
	_load_balancing = false;
	_mem_migrate = true;
}


/*This assumes that checks that must be made by the caller
 * are all made already*/
void Argo_container::alter(String_parser& sp)
{
		
}

Argo_container::~Argo_container()
{
}


void Argo_container::remove()
{
	ASSERT_MSG(_created, "The container was not commited.");
	reload_task_list();
	THROW_IF(_tasks.size() != 0, 
			"Trying to delete a container that still hosts some tasks");
	string path = _parent_path + _older_name;
	int ret = rmdir(path.c_str());
	THROW_WITH_ERRNO_IF(ret, 
			string("Failed to remove container ") + _older_name);
}


void Argo_container::add_cpu(int cpu)
{
	if(get_object_index(_cpus, cpu) == -1)
	{
		_cpus.push_back(cpu);
		_commit_type |= CCT_CPUS;
	}
}

void Argo_container::add_cpus(const vector<int>& cpus)
{
	size_t old_nb_cpus = _cpus.size();
	add_objects<int>(_cpus, cpus);
	if(old_nb_cpus != _cpus.size())
		_commit_type |= CCT_CPUS;
}
void Argo_container::replace_cpus(const vector<int>& cpus)
{
	_cpus.assign(cpus.begin(), cpus.end());
	_commit_type |= CCT_CPUS;
}
void Argo_container::remove_cpu(int cpu)
{
	size_t old_nb_cpus = _cpus.size();
	remove_object<int>(_cpus, cpu);
	if(old_nb_cpus != _cpus.size())
		_commit_type |= CCT_CPUS;
}

void Argo_container::remove_cpus(const vector<int>& cpus)
{
	size_t old_nb_cpus = _cpus.size();
	remove_objects<int>(_cpus, cpus);
	if(old_nb_cpus != _cpus.size())
		_commit_type |= CCT_CPUS;
}

void Argo_container::get_cpus(vector<int>& out) const
{
	out.assign(_cpus.begin(), _cpus.end());
}

void Argo_container::add_mem(int mem)
{
	if(get_object_index(_mems, mem) == -1)
	{
		_mems.push_back(mem);
		_commit_type |= CCT_MEMS;
	}
}

void Argo_container::add_mems(const vector<int>& mems)
{
	size_t nb_old_mems = _mems.size();
	add_objects<int>(_mems, mems);
	if(nb_old_mems != _mems.size())
		_commit_type |= CCT_MEMS;
}
void Argo_container::replace_mems(const vector<int>& mems)
{
	_mems.assign(mems.begin(), mems.end());
	_commit_type |= CCT_MEMS;
}

void Argo_container::remove_mem(int mem)
{
	size_t nb_old_mems = _mems.size();
	remove_object<int>(_mems, mem);
	if(nb_old_mems != _mems.size())
		_commit_type |= CCT_MEMS;
}

void Argo_container::remove_mems(const vector<int>& mems)
{
	size_t nb_old_mems = _mems.size();
	remove_objects<int>(_mems, mems);
	if(nb_old_mems != _mems.size())
		_commit_type |= CCT_MEMS;
}

void Argo_container::get_mems(vector<int>& out) const
{
	out.assign(_mems.begin(), _mems.end());
}

void Argo_container::add_task(int task)
{
	if(get_object_index<int>(_tasks, task) == -1)
	{
		_added_tasks.push_back(task);
		_commit_type |= CCT_TASKS;
	}
}

void Argo_container::add_tasks(const vector<int>& tasks)
{
	size_t nb_old_tasks = _added_tasks.size();
	for(int i=0; i<(int)tasks.size(); i++)
	{
		if(get_object_index<int>(_tasks, tasks[i]) == -1 &&
				get_object_index(_added_tasks, tasks[i]) == -1)
			_added_tasks.push_back(tasks[i]);
	}
	if(nb_old_tasks != _added_tasks.size())
		_commit_type |= CCT_TASKS;
}

void Argo_container::remove_task(int task)
{
	remove_object<int>(_tasks, task);
	//No need for commit for removed tasks
}

void Argo_container::remove_tasks(const vector<int>& tasks)
{
	remove_objects<int>(_tasks, tasks);
	//No need for commit for removed tasks
}

void Argo_container::rename(const string& new_name)
{
	if(new_name != _name)
		_commit_type |= CCT_NAME;
	_name = new_name;
}

void Argo_container::get_tasks(vector<int>& out) const
{
	out.assign(_tasks.begin(), _tasks.end());
}

void Argo_container::display() const
{
	string str_cpus, str_mems, str_tasks;
	struct passwd* pwd;
	pwd = getpwuid(_owner);
	string str_cpu_exclusive = _cpu_exclusive ? "(exclusive)" : "(non exclusive)";
	string str_mem_exclusive = _mem_exclusive ? "(exclusive)" : "(non exclusive)";
	format_int_list_for_display(_cpus, str_cpus, "-CPU cores" + str_cpu_exclusive + ": ");
	format_int_list_for_display(_mems, str_mems, "-Memory nodes" + 
			str_mem_exclusive + ": ");
	format_int_list_for_display(_tasks, str_tasks, "-Tasks: ");

	printf(
		"----- COMPUTE CONTAINER -----\n"
		"-Name: %s\n"
		"-Owner: %s (%d)\n"
		"-Hardware threads%s: "
		"%s\n"
		"-Memory nodes%s: "
		"%s\n"
		"-Tasks: "
		"%s\n"
		"-%s"
		"-%s"
		"\n",
		_name.c_str(),
		pwd->pw_name, _owner,
		str_cpu_exclusive.c_str(),
		str_cpus.c_str(),
		str_mem_exclusive.c_str(),
		str_mems.c_str(),
		str_tasks.c_str(),
		_load_balancing ? "Balancing load\n" : "Not balancing load\n",
		_mem_migrate ? "Memory migration is enabled\n" : "Memory migration is NOT enabled\n"
		);
}

void Argo_container::display_resources() const
{
	string str_cpus, str_mems;
	format_int_list_for_display(_cpus, str_cpus, "-CPU cores: ");
	format_int_list_for_display(_mems, str_mems, "-Memory nodes: ");

	printf(
			"-Hardware threads: "
			 "%s\n" 
			 "-Memory nodes: "
			 "%s\n" 
			 "%s\n" 
			 ,
			  str_cpus.c_str(),
			  str_mems.c_str(),
			  _mem_migrate ? "Memory migration is enabled" : "Memory migration is NOT enabled"
		  );
}


void Argo_container::reload_task_list()
{
	retrieve_from_file<int>(_parent_path + _older_name + "/tasks",
			_tasks);
	_added_tasks.clear();
}

void Argo_container::reload(bool reload_tasks)
{
	string str;
	retrieve_from_file(_parent_path + _older_name + "/cpuset.cpus",
			str);
	Int_list_parser ilp;
	ilp.convert_to_comprehensive(str, _cpus);

	retrieve_from_file(_parent_path + _older_name + "/cpuset.mems",
			str);
	ilp.convert_to_comprehensive(str, _mems);

	if(reload_tasks)
	{
		retrieve_from_file<int>(_parent_path + _older_name + "/tasks",
				_tasks);
		_added_tasks.clear();
	}

	retrieve_from_file(_parent_path + _older_name + "/cpuset.cpu_exclusive",
			str);
	_cpu_exclusive = str == "1" ? true : false;

	retrieve_from_file(_parent_path + _older_name + "/cpuset.mem_exclusive",
			str);
	_mem_exclusive = str == "1" ? true : false;

	retrieve_from_file(_parent_path + _older_name + "/cpuset.memory_migrate",
			str);
	_mem_migrate = str == "1" ? true : false;

	retrieve_from_file(_parent_path + _older_name + "/cpuset.sched_load_balance",
			str);
	_load_balancing = str == "1" ? true : false;

	_created = true;
}


void Argo_container::commit_last_changes()
{
	int status;
	elevate_privileges();
	if(_commit_type & CCT_NAME)
	{
		string older = _parent_path + _older_name;
		string newer = _parent_path + _name;
		status = ::rename(older.c_str(), newer.c_str());
		THROW_WITH_ERRNO_IF(status, "Failed to rename container");
		_older_name = _name;
	}
	if(_commit_type & CCT_CPUS)
	{
		string cpus_str;
		stringify_int_list(_cpus, ',', cpus_str);
		commit_in_file<string>(_parent_path + _older_name + "/cpuset.cpus",
				cpus_str);
	}

	if(_commit_type & CCT_MEMS)
	{
		string mems_str;
		stringify_int_list(_mems, ',', mems_str);
		commit_in_file<string>(_parent_path + _older_name + "/cpuset.mems",
				mems_str);
	}
	if((_commit_type & CCT_TASKS) && _added_tasks.size() != 0)
	{
		string dest = _parent_path + _older_name + "/tasks";
		std::ofstream file;
		file.open(dest.c_str(), std::ofstream::app);
		for(int i=0; i<(int)_added_tasks.size(); i++)
		{
			file<<_added_tasks[i]<<std::endl;
			file.flush();
			usleep(100);
			_tasks.push_back(_added_tasks[i]);
		}
		file.close();
		_added_tasks.clear();
	}
	if(_commit_type & CCT_CPU_EXCLUSIVE)
	{
		commit_in_file<bool>(_parent_path + _older_name + "/cpuset.cpu_exclusive",
				_cpu_exclusive);
	}
	if(_commit_type & CCT_MEM_EXCLUSIVE)
	{
		commit_in_file<bool>(_parent_path + _older_name + "/cpuset.mem_exclusive",
				_mem_exclusive);
	}
	if(_commit_type & CCT_MEM_MIGRATE)
	{
		commit_in_file<bool>(_parent_path + _older_name + "/cpuset.memory_migrate",
				_mem_migrate);
	}
	if(_commit_type & CCT_LOAD_BALANCING)
	{
		commit_in_file<bool>(_parent_path + _older_name + "/cpuset.sched_load_balance",
				_load_balancing);
	}
	_commit_type = CCT_NONE;
	lower_privileges();
}

void Argo_container::commit_all()
{
	_commit_type = CCT_ALL;
	commit_last_changes();
}

void Argo_container::create()
{
	if(_created)
		return;
	string path = _parent_path + _older_name;
	int ret = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
	THROW_WITH_ERRNO_IF(ret && errno != EEXIST, 
			string("Failed to create container ") + _older_name);
	_created = true;
}


void Argo_container::set_cpu_exclusive(bool status)
{
	if(_cpu_exclusive != status)
	{
		_commit_type |= CCT_CPU_EXCLUSIVE;
		_cpu_exclusive = status;
	}
}

void Argo_container::set_mem_exclusive(bool status)
{
	if(_mem_exclusive != status)
	{
		_commit_type |= CCT_MEM_EXCLUSIVE;
		_mem_exclusive = status;
	}
}

void Argo_container::set_mem_migrate(bool status)
{
	if(_mem_migrate != status)
	{
		_commit_type |= CCT_MEM_MIGRATE;
		_mem_migrate = status;
	}
}

void Argo_container::set_load_balancing(bool status)
{
	if(_load_balancing != status)
	{
		_commit_type |= CCT_LOAD_BALANCING;
		_load_balancing = status;
	}
}

bool Argo_container::check_task_ownership(bool reload, bool throw_on_failure)
{
	uid_t uid;
	if(reload)
		reload_task_list();
	for(int i=0; i<(int)_tasks.size(); i++)
	{
		uid = get_task_owner(_tasks[i]);
		if(ruid != 0 && uid != _owner && uid != UID_INVALID)
		{
			if(throw_on_failure)
			{
				std::ostringstream ss;
				ss<<"The task of pid "<<_tasks[i]<<" belongs to another user";
				THROW(ss.str());
			}
			return false;
		}
	}
	return true;
}

void Argo_container::kill_content(bool reload)
{
	if(reload)
		reload_task_list();
	int ret;

	for(int i=0; i<(int)_tasks.size(); i++)
	{
		ret = kill(_tasks[i], SIGKILL);
		if(ret != 0 && errno == EPERM)
		{
			std::ostringstream ss;
			ss<<"The task of pid "<<_tasks[i]<<" belongs to another user. "
				<<"This task seem to have been forked or added in the middle "
				<<"of clening up the content of the container. A race condition occured!";
			THROW(ss.str());
		}
	}
	_tasks.clear();
}