def update_conf(event): logger.info('update conf triggered at: %f' % time.time()) context = global_value['context'] zk_path = '%s/arch_conf' % context['zk_prefix'] while True: try: zk_client = context['zk_client'] resp = zk_client.get(zk_path, watch=update_conf) break except Exception as e: logger.exception('failed to get from zk: ' + repr(e)) time.sleep(5) context['arch_conf'] = { 'version': resp[1].version, 'value': utfjson.load(resp[0]), } logger.info('arch conf in zk changed at: %f, current verrsion: %d' % (time.time(), resp[1].version)) cgroup_manager.build_all_subsystem_cgroup_arch(context)
def update_conf(event): logger.info('update conf triggered at: %f' % time.time()) context = global_value['context'] zk_path = '%s/arch_conf' % context['zk_prefix'] while True: try: zk_client = context['zk_client'] resp = zk_client.get(zk_path, watch=update_conf) break except Exception as e: logger.exception('failed to get from zk: ' + repr(e)) time.sleep(5) context['arch_conf'] = { 'version': resp[1].version, 'value': utfjson.load(resp[0]), } logger.info('arch conf in zk changed at: %f, current verrsion: %d' % (time.time(), resp[1].version)) cgroup_manager.build_all_subsystem_cgroup_arch(context)
def run(**argkv): context = { 'get_cgroup_pid_file': argkv['get_cgroup_pid_file'], 'cgroup_dir': argkv.get('cgroup_dir', '/sys/fs/cgroup'), 'communicate_ip': argkv.get('communicate_ip', '0.0.0.0'), 'communicate_port': argkv.get('communicate_port', 43409), 'tasks_update_interval': argkv.get('tasks_update_interval', 30), 'redis_ip': argkv['redis_ip'], 'redis_port': argkv['redis_port'], 'redis_prefix': argkv.get('redis_prefix', 'cgroup_arch'), 'redis_client': None, 'redis_expire_time': argkv.get('redis_expire_time', 60 * 5), 'get_zk_host': argkv['get_zk_host'], 'zk_prefix': argkv['zk_prefix'], 'zk_auth_data': argkv['zk_auth_data'], 'zk_client': None, 'protected_cgroup': argkv.get('protected_cgroup'), 'arch_conf': None, } init_redis_client(context) init_arch_conf(context) global_value['context'] = context cgroup_manager.build_all_subsystem_cgroup_arch(context) cgroup_manager.set_cgroup(context) cgroup_manager.reset_statistics(context) threadutil.start_daemon_thread(account.run, args=(context,)) threadutil.start_daemon_thread(cgroup_manager.loop_set_cgroup, args=(context,)) communicate.run(context, ip=context['communicate_ip'], port=context['communicate_port'])
def run(**argkv): context = { 'get_cgroup_pid_file': argkv['get_cgroup_pid_file'], 'cgroup_dir': argkv.get('cgroup_dir', '/sys/fs/cgroup'), 'communicate_ip': argkv.get('communicate_ip', '0.0.0.0'), 'communicate_port': argkv.get('communicate_port', 43409), 'tasks_update_interval': argkv.get('tasks_update_interval', 30), 'redis_ip': argkv['redis_ip'], 'redis_port': argkv['redis_port'], 'redis_prefix': argkv.get('redis_prefix', 'cgroup_arch'), 'redis_client': None, 'redis_expire_time': argkv.get('redis_expire_time', 60 * 5), 'get_zk_host': argkv['get_zk_host'], 'zk_prefix': argkv['zk_prefix'], 'zk_auth_data': argkv['zk_auth_data'], 'zk_client': None, 'protected_cgroup': argkv.get('protected_cgroup'), 'arch_conf': None, } init_redis_client(context) init_arch_conf(context) global_value['context'] = context cgroup_manager.build_all_subsystem_cgroup_arch(context) cgroup_manager.set_cgroup(context) cgroup_manager.reset_statistics(context) threadutil.start_daemon_thread(account.run, args=(context, )) threadutil.start_daemon_thread(cgroup_manager.loop_set_cgroup, args=(context, )) communicate.run(context, ip=context['communicate_ip'], port=context['communicate_port'])
def get_cgexec_arg(cgroup_names, **argkv): context = { 'cgroup_dir': argkv.get('cgroup_dir', '/sys/fs/cgroup'), 'get_zk_host': argkv['get_zk_host'], 'zk_prefix': argkv['zk_prefix'], 'zk_auth_data': argkv['zk_auth_data'], } cgexec_arg = {} for cgroup_name in cgroup_names: cgexec_arg[cgroup_name] = '' try: zk_client = get_zk_client(context) zk_path = '%s/arch_conf' % context['zk_prefix'] resp = zk_client.get(zk_path) zkutil.close_zk(zk_client) context['arch_conf'] = { 'version': resp[1].version, 'value': utfjson.load(resp[0]), } cgroup_manager.build_all_subsystem_cgroup_arch(context) arch_conf_value = context['arch_conf']['value'] for subsystem_name, subsystem_conf in arch_conf_value.iteritems(): cgroup_relative_path = '' cgroup_conf = subsystem_conf update_cgexec_arg(cgexec_arg, subsystem_name, cgroup_relative_path, cgroup_conf) return cgexec_arg except Exception as e: logger.exception('failed to get cgexec arg: ' + repr(e)) return cgexec_arg
def get_cgexec_arg(cgroup_names, **argkv): context = { 'cgroup_dir': argkv.get('cgroup_dir', '/sys/fs/cgroup'), 'get_zk_host': argkv['get_zk_host'], 'zk_prefix': argkv['zk_prefix'], 'zk_auth_data': argkv['zk_auth_data'], } cgexec_arg = {} for cgroup_name in cgroup_names: cgexec_arg[cgroup_name] = '' try: zk_client = get_zk_client(context) zk_path = '%s/arch_conf' % context['zk_prefix'] resp = zk_client.get(zk_path) zkutil.close_zk(zk_client) context['arch_conf'] = { 'version': resp[1].version, 'value': utfjson.load(resp[0]), } cgroup_manager.build_all_subsystem_cgroup_arch(context) arch_conf_value = context['arch_conf']['value'] for subsystem_name, subsystem_conf in arch_conf_value.iteritems(): cgroup_relative_path = '' cgroup_conf = subsystem_conf update_cgexec_arg(cgexec_arg, subsystem_name, cgroup_relative_path, cgroup_conf) return cgexec_arg except Exception as e: logger.exception('failed to get cgexec arg: ' + repr(e)) return cgexec_arg
def test_cpu_share(self): manager = multiprocessing.Manager() result_dict = manager.dict() p1 = multiprocessing.Process(target=self.worker, args=(1, 10, result_dict)) p1.daemon = True p1.start() p2 = multiprocessing.Process(target=self.worker, args=(2, 10, result_dict)) p2.daemon = True p2.start() p3 = multiprocessing.Process(target=self.worker, args=(3, 10, result_dict)) p3.daemon = True p3.start() arch_conf = { 'cpu': { 'sub_cgroup': { 'test_cgroup_a': { 'conf': { 'share': 500, 'pids': [p1.pid], }, }, 'test_cgroup_b': { 'conf': { 'share': 1000, }, 'sub_cgroup': { 'test_cgroup_b_sub1': { 'conf': { 'share': 100, 'pids': [p2.pid], }, }, 'test_cgroup_b_sub2': { 'conf': { 'share': 300, 'pids': [p3.pid], }, }, }, }, }, }, } context = { 'cgroup_dir': '/sys/fs/cgroup', 'arch_conf': { 'value': arch_conf }, } cgroup_manager.build_all_subsystem_cgroup_arch(context) cgroup_manager.set_cgroup(context) p1.join() p2.join() p3.join() dd(result_dict) for cgrou_name in arch_conf['cpu']['sub_cgroup'].keys(): cgroup_util.remove_cgroup( os.path.join(context['cgroup_dir'], 'cpu'), os.path.join(context['cgroup_dir'], 'cpu', cgrou_name)) level1_rate = float(result_dict[2] + result_dict[3]) / result_dict[1] dd(level1_rate) self.assertAlmostEqual(2.0, level1_rate, delta=0.5) level2_rate = float(result_dict[3]) / result_dict[2] dd(level2_rate) self.assertAlmostEqual(3.0, level2_rate, delta=0.5)
def test_blkio_weight(self): if ututil.has_env('TRAVIS=true'): return manager = multiprocessing.Manager() result_dict = manager.dict() p1 = multiprocessing.Process(target=self.worker, args=(1, 10, result_dict)) p1.daemon = True p1.start() p2 = multiprocessing.Process(target=self.worker, args=(2, 10, result_dict)) p2.daemon = True p2.start() p3 = multiprocessing.Process(target=self.worker, args=(3, 10, result_dict)) p3.daemon = True p3.start() p4 = multiprocessing.Process(target=self.worker, args=(4, 10, result_dict)) p4.daemon = True p4.start() arch_conf = { 'blkio': { 'sub_cgroup': { 'test_cgroup_a': { 'conf': { 'weight': int(500 * 0.95), }, 'sub_cgroup': { 'test_cgroup_a_sub1': { 'conf': { 'weight': 500, 'pids': [p1.pid], }, }, 'test_cgroup_a_sub2': { 'conf': { 'weight': 500, 'pids': [p2.pid], }, }, }, }, 'test_cgroup_b': { 'conf': { 'weight': int(500 * 0.05), }, 'sub_cgroup': { 'test_cgroup_b_sub1': { 'conf': { 'weight': 500, 'pids': [p3.pid], }, }, 'test_cgroup_b_sub2': { 'conf': { 'weight': 500, 'pids': [p4.pid], }, }, }, }, }, }, } context = { 'cgroup_dir': '/sys/fs/cgroup', 'arch_conf': { 'value': arch_conf }, } cgroup_manager.build_all_subsystem_cgroup_arch(context) cgroup_manager.set_cgroup(context) p1.join() p2.join() p3.join() p4.join() for cgrou_name in arch_conf['blkio']['sub_cgroup'].keys(): cgroup_util.remove_cgroup( os.path.join(context['cgroup_dir'], 'blkio'), os.path.join(context['cgroup_dir'], 'blkio', cgrou_name)) for i in range(1, 5): fsutil.remove(os.path.join(base_dir, 'test_file_%d' % i)) dd(result_dict) self.assertGreater(result_dict[1] + result_dict[2], result_dict[3] + result_dict[4])