def bgCmd(self,host,force_multiple_processes,*args): # here it's a little workaround for tracing the resulting pid # it launch the new process using the mininet interface # but it check the newly created process id using psutil host_proc = Process(host.pid) host_ps = set(host_proc.children()) debug("Sending cmd: \n\t"+str(" ".join(args))+"\n") # disable bg process output tmp_wait = host.waiting host.waiting = False host.sendCmd(*(("set +m",))) host.waiting = tmp_wait if force_multiple_processes: host.waiting = False host.sendCmd(*(args+("&",))) sleep(0.5) try : pid = (set(host_proc.children()).difference(host_ps)).pop().pid info("BGProcess: "+str(pid)+"; ") self.pendingProc[pid] = host except: info("*** Unable to launch command:\n\t "+str(" ".join(args))) return None return pid
def test_cleanup_children_on_terminate(self): """ Subprocesses spawned by tasks should be terminated on terminate """ class HangingSubprocessTask(luigi.Task): def run(self): python = sys.executable check_call([python, '-c', 'while True: pass']) task = HangingSubprocessTask() queue = mock.Mock() worker_id = 1 task_process = TaskProcess(task, worker_id, queue, lambda: None, lambda: None) task_process.start() parent = Process(task_process.pid) while not parent.children(): # wait for child process to startup sleep(0.01) [child] = parent.children() task_process.terminate() child.wait(timeout=1.0) # wait for terminate to complete self.assertFalse(parent.is_running()) self.assertFalse(child.is_running())
def collect_status(pid, appname, site): ip_out = get_host_info()[0] ip_inner = get_host_info()[1] server_id = get_host_info()[2] physical_mem = psutil.virtual_memory().total / 1024 / 1024 #Unit of M cpu_count = psutil.cpu_count() its = int(time.time()) p_ins = Process(pid) pstatus = p_ins.status() create_time = time.strftime("%Y%m%d %H:%M:%S", time.localtime(p_ins.create_time())) memory_percent = p_ins.memory_percent() memory_used = memory_percent * physical_mem cpu_calc_list = [] for i in range(6): cpu_calc = p_ins.cpu_percent(interval=0.1) cpu_calc_list.append(cpu_calc) cpu_percent = float(sum(cpu_calc_list) / len(cpu_calc_list)) num_fds = p_ins.num_fds() connections = p_ins.connections() connections_num = len(connections) #appname=p_ins.cwd() if p_ins.name() == 'jsvc': app_path = p_ins.exe().split('/')[:-2] else: app_path = p_ins.cwd() #appname = app_path.split('/')[-1] appname = appname if p_ins.children(recursive=True): children_list = str(p_ins.children(recursive=True)) else: children_list = None message = { 'site': site, 'ip': ip_out, 'ip_inner': ip_inner, 'server_id': server_id, 'pstatus': pstatus, 'metric_name': 'app_monitor', 'its': its, 'pid': pid, 'physical_mem': physical_mem, 'memory_used': memory_used, 'memory_percent': memory_percent, 'cpu_count': cpu_count, 'cpu_percent': cpu_percent, 'num_fds': num_fds, 'connections_num': connections_num, 'create_time': create_time, 'appname': appname, 'app_path': app_path, 'children': children_list } return message
def kill_proc_tree(pid, include_parent=True, timeout=1, on_terminate=None): # adapted from: https://psutil.readthedocs.io/en/latest/#kill-process-tree if pid_exists(pid): assert pid != os.getpid(), "won't kill myself" parent = Process(pid) children = parent.children(recursive=True) if include_parent: children.append(parent) for p in children: try: p.terminate() except Exception as e: pass _, survived_terminate = wait_procs(children, timeout=timeout, callback=on_terminate) for p in survived_terminate: try: p.kill() except Exception as e: pass _, survived_kill = wait_procs(survived_terminate, timeout=timeout, callback=on_terminate) if len(survived_kill) > 0: return False else: return True else: return True
def test_infinite_loop(self): # Given user_answer = ("class Test {\n\tint square_num(int a)" " {\n\t\twhile(0==0){\n\t\t}\n\t}\n}") kwargs = { 'metadata': { 'user_answer': user_answer, 'file_paths': self.file_paths, 'partial_grading': False, 'language': 'java' }, 'test_case_data': self.test_case_data, } # When grader = Grader(self.in_dir) result = grader.evaluate(kwargs) # Then self.assertFalse(result.get("success")) self.assert_correct_output(self.timeout_msg, result.get("error")[0]["message"] ) parent_proc = Process(os.getpid()).children() if parent_proc: children_procs = Process(parent_proc[0].pid) self.assertFalse(any(children_procs.children(recursive=True)))
def test_infinite_loop(self): # Given user_answer = ("#!/bin/bash\nwhile [ 1 ] ;" " do echo "" > /dev/null ; done") kwargs = { 'metadata': { 'user_answer': user_answer, 'file_paths': self.file_paths, 'partial_grading': False, 'language': 'bash' }, 'test_case_data': self.test_case_data, } # When grader = Grader(self.in_dir) result = grader.evaluate(kwargs) # Then self.assertFalse(result.get("success")) self.assert_correct_output(self.timeout_msg, result.get("error")[0]["message"] ) parent_proc = Process(os.getpid()).children() if parent_proc: children_procs = Process(parent_proc[0].pid) self.assertFalse(any(children_procs.children(recursive=True)))
def test_infinite_loop(self): # Given user_answer = dedent(""" #include<stdio.h> int main(void){ while(0==0){ printf("abc");} }""") kwargs = { 'metadata': { 'user_answer': user_answer, 'file_paths': self.file_paths, 'partial_grading': False, 'language': 'cpp' }, 'test_case_data': self.test_case_data, } # When grader = Grader(self.in_dir) result = grader.evaluate(kwargs) # Then self.assertFalse(result.get("success")) self.assert_correct_output(self.timeout_msg, result.get("error")) parent_proc = Process(os.getpid()).children() if parent_proc: children_procs = Process(parent_proc[0].pid) self.assertFalse(any(children_procs.children(recursive=True)))
def test_infinite_loop(self): # Given user_answer = dedent(""" #include<stdio.h> int main(void){ while(0==0){ printf("abc");} }""") kwargs = { 'metadata': { 'user_answer': user_answer, 'file_paths': self.file_paths, 'partial_grading': False, 'language': 'cpp' }, 'test_case_data': self.test_case_data, } # When grader = Grader(self.in_dir) result = grader.evaluate(kwargs) # Then self.assertFalse(result.get("success")) self.assert_correct_output(self.timeout_msg, result.get("error")[0]["message"] ) parent_proc = Process(os.getpid()).children() if parent_proc: children_procs = Process(parent_proc[0].pid) self.assertFalse(any(children_procs.children(recursive=True)))
def click(x, notify=False, pid=None, pids=None, webdriver=None, window_name=None, debug=False): if debug: print("[beryl] starting click") print("\tpid: " + str(pid)) print("\twebdriver: " + str(webdriver)) print("\twindow_name: " + str(window_name)) print("\tstr(type(webdriver)): " + str(type(webdriver))) type_as_string = str(type(x)) webdriver_type_as_string = str(type(webdriver)) if webdriver_type_as_string == "<class 'selenium.webdriver.firefox.webdriver.WebDriver'>": pids = [webdriver.binary.process.pid] elif webdriver_type_as_string == "<class 'selenium.webdriver.chrome.webdriver.WebDriver'>": process = Process(webdriver.service.process.pid) if hasattr(process, "children"): pids = [p.pid for p in process.children()] elif hasattr(process, "get_children"): pids = [p.pid for p in process.get_children()] if isinstance(x, str) or isinstance(x, unicode): if x.endswith(".png") or x.endswith(".jpg"): click_image(x, notify=notify) else: click_text(x, notify=notify, pids=pids, window_name=window_name, debug=debug) elif isinstance(x, PngImageFile): click_image(x,notify=notify) elif isinstance(x, tuple): click_location(x,notify=notify)
def test_infinite_loop(self): # Given user_answer = ("#!/bin/bash\nwhile [ 1 ] ;" " do echo "" > /dev/null ; done") kwargs = { 'metadata': { 'user_answer': user_answer, 'file_paths': self.file_paths, 'partial_grading': False, 'language': 'bash' }, 'test_case_data': self.test_case_data, } # When grader = Grader(self.in_dir) result = grader.evaluate(kwargs) # Then self.assertFalse(result.get("success")) self.assert_correct_output(self.timeout_msg, result.get("error")) parent_proc = Process(os.getpid()).children() if parent_proc: children_procs = Process(parent_proc[0].pid) self.assertFalse(any(children_procs.children(recursive=True)))
def test_infinite_loop(self): # Given user_answer = dedent("""\ #include<stdio.h> int main(void){ while(0==0){ printf("abc");} }""") hook_code = dedent("""\ def check_answer(user_answer): with open("Test.c", "w+") as f: f.write(user_answer) import subprocess success = False err = "Incorrect Answer" mark_fraction = 0.0 def _run_command(cmd): proc = subprocess.Popen("{}".format(cmd), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) stdout,stderr = proc.communicate() return stdout,stderr cmds = ["gcc Test.c", "./a.out"] for cmd in cmds: stdout, stderr = _run_command(cmd) if stdout.decode("utf-8") == "Hello, world!": success, err, mark_fraction = True, "", 1.0 return success, err, mark_fraction """) test_case_data = [{ "test_case_type": "hooktestcase", "hook_code": hook_code, "weight": 1.0 }] kwargs = { 'metadata': { 'user_answer': user_answer, 'file_paths': self.file_paths, 'partial_grading': False, 'language': 'cpp' }, 'test_case_data': test_case_data, } # When grader = Grader(self.in_dir) result = grader.evaluate(kwargs) # Then self.assertFalse(result.get('success')) self.assert_correct_output(self.timeout_msg, result.get('error')) parent_proc = Process(os.getpid()).children() if parent_proc: children_procs = Process(parent_proc[0].pid) self.assertFalse(any(children_procs.children(recursive=True)))
def test_infinite_loop(self): # Given user_answer = ("class Test {\n\tint square_num(int a)" " {\n\t\twhile(0==0){\n\t\t}\n\t}\n}") kwargs = { 'metadata': { 'user_answer': user_answer, 'file_paths': self.file_paths, 'partial_grading': False, 'language': 'java' }, 'test_case_data': self.test_case_data, } # When grader = Grader(self.in_dir) result = grader.evaluate(kwargs) # Then self.assertFalse(result.get("success")) self.assert_correct_output(self.timeout_msg, result.get("error")[0]["message"]) parent_proc = Process(os.getpid()).children() if parent_proc: children_procs = Process(parent_proc[0].pid) self.assertFalse(any(children_procs.children(recursive=True)))
async def assign_scope(self, app_id: str, proc: Process): """ Assign process (and all unassigned children) to the app-{app_id}.slice/app{app_id}-{pid}.scope cgroup """ app_id = escape_app_id(app_id) sd_slice = SD_SLICE_FORMAT.format(app_id=app_id) sd_unit = SD_UNIT_FORMAT.format(app_id=app_id, unique=proc.pid) # Collect child processes as systemd assigns a scope only to explicitly # specified PIDs. # There's a risk of race as the child processes may exit by the time dbus call # reaches systemd, hence the @retry decorator is applied to the method. pids = [proc.pid] + [ x.pid for x in proc.children(recursive=True) if self.cgroup_change_needed(get_cgroup(x.pid)) ] await self._sd_manager.call_start_transient_unit( sd_unit, "fail", [["PIDs", Variant("au", pids)], ["Slice", Variant("s", sd_slice)]], [], ) LOG.debug("window %s successfully assigned to cgroup %s/%s", app_id, sd_slice, sd_unit)
def test_infinite_loop(self): # Given user_answer = dedent(""" class Test {public static void main(String[] args){ while(0==0) { System.out.print("a");} }}""") kwargs = { 'metadata': { 'user_answer': user_answer, 'file_paths': self.file_paths, 'partial_grading': False, 'language': 'java' }, 'test_case_data': self.test_case_data, } # When grader = Grader(self.in_dir) result = grader.evaluate(kwargs) # Then self.assertFalse(result.get("success")) self.assert_correct_output(self.timeout_msg, result.get("error")[0]["message"]) parent_proc = Process(os.getpid()).children() if parent_proc: children_procs = Process(parent_proc[0].pid) self.assertFalse(any(children_procs.children(recursive=True)))
def test_infinite_loop(self): # Given user_answer = dedent(''' odd_or_even <- function(n){ while(0 == 0){ a <- 1 } } ''') kwargs = { 'metadata': { 'user_answer': user_answer, 'file_paths': self.file_paths, 'partial_grading': False, 'language': 'r' }, 'test_case_data': self.test_case_data, } # When grader = Grader(self.in_dir) result = grader.evaluate(kwargs) # Then self.assertFalse(result.get("success")) self.assert_correct_output(self.timeout_msg, result.get("error")[0]["message"]) parent_proc = Process(os.getpid()).children() if parent_proc: children_procs = Process(parent_proc[0].pid) self.assertFalse(any(children_procs.children(recursive=True)))
def get_nvim_socket(): """ 1/ get pid of focused window 2/ look for nvim processes in its children 3/ search for socket name in the nvim child process """ try: pid = check_output("xdotool getwindowfocus getwindowpid", shell=True).decode() pid = pid.rstrip() pid = int(pid) #log.debug("Retreived terminal pid %d, nvim should be one of its children" % pid) proc = Process(pid) #log.debug( "proc name %s with %d children" % (proc.name(), len(proc.children(recursive=True)))) for child in proc.children(recursive=True): #log.debug("child name & pid %s/%d" % (child.name(), child.pid)) if child.name() == "nvim": unix_sockets = child.connections(kind="unix") #log.debug("Found an nvim subprocess with %d " % len(unix_sockets)) # look for socket # for filename, fd in child.open_files(): # log.debug("Open file %s " % filename) for con in unix_sockets: filename = con.laddr #log.debug("Socket %s " % filename) if "/tmp/nvim" in filename: #log.debug("Found a match: %s" % filename) return True, filename return False, "" except Exception as e: #log.error('Could not find neovim socket %s' % e) print('Could not find neovim socket %s' % e) #log.error(traceback.format_exc()) return False, ""
def kill_process_group(self, pid): from psutil import Process parent = Process(pid) for child in parent.children(recursive=True): child.kill() parent.kill() parent.wait()
def kill_proc_tree(pid, including_parent=True, timeout=5): try: parent = Process(pid) except NoSuchProcess: return children = parent.children(recursive=True) for child in children: if verbose.kill: print("killing {}".format(child.pid)) try: child.kill() child.terminate() except NoSuchProcess: pass gone, still_alive = wait_procs(children, timeout=timeout) if including_parent: try: if verbose.kill: print("killing {}".format(parent.pid)) parent.kill() parent.terminate() try: parent.wait(timeout) except TimeoutExpired: print("timeout expired, process may still be around: {}".format(parent.pid)) except NoSuchProcess: pass
def test_infinite_loop(self): # Given user_answer = dedent("""\ #include<stdio.h> int main(void){ while(0==0){ printf("abc");} }""") hook_code = dedent("""\ def check_answer(user_answer): with open("Test.c", "w+") as f: f.write(user_answer) import subprocess success = False err = "Incorrect Answer" mark_fraction = 0.0 def _run_command(cmd): proc = subprocess.Popen( "{}".format(cmd), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) stdout,stderr = proc.communicate() return stdout,stderr cmds = ["gcc Test.c", "./a.out"] for cmd in cmds: stdout, stderr = _run_command(cmd) if stdout.decode("utf-8") == "Hello, world!": success, err, mark_fraction = True, "", 1.0 return success, err, mark_fraction """) test_case_data = [{"test_case_type": "hooktestcase", "hook_code": hook_code, "weight": 1.0}] kwargs = { 'metadata': { 'user_answer': user_answer, 'file_paths': self.file_paths, 'partial_grading': False, 'language': 'cpp' }, 'test_case_data': test_case_data, } # When grader = Grader(self.in_dir) result = grader.evaluate(kwargs) # Then self.assertFalse(result.get('success')) self.assert_correct_output(self.timeout_msg, result.get("error")[0]["message"] ) parent_proc = Process(os.getpid()).children() if parent_proc: children_procs = Process(parent_proc[0].pid) self.assertFalse(any(children_procs.children(recursive=True)))
def get_info(component_path, format): component_details = open(os.path.join(component_path, ZATO_INFO_FILE)).read() out = { 'component_details': component_details, 'component_full_path': component_path, 'component_host': current_host(), 'component_running': False, 'current_time': datetime.now().isoformat(), 'current_time_utc': datetime.utcnow().isoformat(), 'master_proc_connections': None, 'master_proc_pid': None, 'master_proc_name': None, 'master_proc_create_time': None, 'master_proc_create_time_utc': None, 'master_proc_username': None, 'master_proc_workers_no': None, 'master_proc_workers_pids': None, } master_proc_pid = None try: master_proc_pid = int( open(os.path.join(component_path, MISC.PIDFILE)).read()) except (IOError, ValueError): # Ok, no such file or it's empty pass if master_proc_pid: out['component_running'] = True master_proc = Process(master_proc_pid) workers_pids = sorted(elem.pid for elem in master_proc.children()) out['master_proc_connections'] = format_connections( master_proc.connections(), format) out['master_proc_pid'] = master_proc.pid out['master_proc_create_time'] = datetime.fromtimestamp( master_proc.create_time()).isoformat() out['master_proc_create_time_utc'] = datetime.fromtimestamp( master_proc.create_time(), UTC).isoformat() out['master_proc_username'] = master_proc.username() out['master_proc_name'] = master_proc.name() out['master_proc_workers_no'] = len(workers_pids) out['master_proc_workers_pids'] = workers_pids for pid in workers_pids: worker = Process(pid) out['worker_{}_create_time'.format(pid)] = datetime.fromtimestamp( worker.create_time()).isoformat() out['worker_{}_create_time_utc'.format( pid)] = datetime.fromtimestamp(worker.create_time(), UTC).isoformat() out['worker_{}_connections'.format(pid)] = format_connections( worker.connections(), format) return out
def lower_child_priority(): try: from psutil import Process, BELOW_NORMAL_PRIORITY_CLASS parent = Process() parent.nice(BELOW_NORMAL_PRIORITY_CLASS) for child in parent.children(): child.nice(BELOW_NORMAL_PRIORITY_CLASS) except: pass
def kill_process_and_subs(): proc = Process(getpid()) subprocs = proc.children() for subproc in subprocs: subproc.terminate() stillalive = wait_procs(subprocs, timeout=2)[1] for p in stillalive: p.kill() proc.kill()
def terminate(self): self.alive = False try: parent = Process(self.process.pid) for child in parent.children(recursive=True): child.kill() parent.kill() except NoSuchProcess: pass
def kill_recursively(p: Process): """Kill a process and all its children.""" children = p.children() if children: for child in children: kill_recursively(child) try: p.kill() except NoSuchProcess: pass
def close(self): try: process = Process(self.chrome.pid) for proc in process.children(recursive=True): proc.kill() process.kill() self.url = None print("chromium closed") except Exception as e: print("could not close chromium with exception: " + str(e))
def killProcs(PID): #try: p = Process(PID) #try: while len(p.children()) > 0: try: child = p.children()[0] while len(child.children()) > 0: print len(child.children()), child child = child.children()[0] print "kill" child.terminate() print "Restart" except: pass try: p.terminate() except: pass
def _psutil_kill_pid(pid): """ http://stackoverflow.com/questions/1230669/subprocess-deleting-child-processes-in-windows """ try: parent = Process(pid) for child in parent.children(recursive=True): child.kill() parent.kill() except NoSuchProcess: return
def sig_int(signal_num, frame): LOG.error('signal: %s frame: %s', signal_num, frame) parent = Process(parent_id) for child in parent.children(): if child.pid != os.getpid(): LOG.error("exiting child: %s" % child.pid) child.kill() LOG.error("exiting parent: %s" % parent_id) parent.kill() LOG.error("exiting all: %s" % os.getpid()) Process(os.getpid()).kill()
def kill_proc_tree(parent: Process, include_parent=True): fails = [] try: children = parent.children(recursive=True) except NoSuchProcess: children = [] if include_parent: children.append(parent) for p in children: if not kill(p): fails.append(p) return fails
def kill_process_tree(self, proc: psutil.Process): """Kill process tree with given process object. Caller should be prepared to catch psutil Process class exceptions. """ children = proc.children(recursive=True) children.append(proc) for c in children: c.terminate() gone, alive = psutil.wait_procs(children, timeout=10, callback=self.on_terminate) for survivor in alive: survivor.kill()
def tear_down_driver(self): try: process = Process(self.process.pid) for pro in process.children(recursive=True): pro.kill() pro.wait() self.process.kill() self.process.wait() self.process = None except (NoSuchProcess, AttributeError): subprocess.call("C:/Windows/system32/taskkill.exe /f /im WinAppDriver.exe", shell=False) self.process = None
def kill(self, pid=None): if self.running: pid = self._pid if pid is None else pid if pid_exists(pid): log.debug("killing process with pid %s" % pid) process = Process(pid) self._out_stream.stop() self._error_stream.stop() for proc in process.children(recursive=True): proc.kill() process.kill()
def a_proc_children(proc: psutil.Process, recursive: bool = False) -> List[psutil.Process]: """Get the children of a process asyncly Args: proc: The process recursive: Whether get the children recursively Returns: The children of the process """ return proc.children(recursive=recursive)
def tearDown(self): # cleanup all child processes if os.getenv("SIMPLEFLOW_CLEANUP_PROCESSES"): process = Process() for child in process.children(recursive=True): # TODO: have a warning here? normal? try: child.kill() except NoSuchProcess: pass # reset SIGTERM handler signal.signal(signal.SIGTERM, signal.SIG_DFL)
def test_infinite_loop(self): # Given user_answer = ("#!/bin/bash\nwhile [ 1 ] ;" " do echo " " > /dev/null ; done") hook_code = dedent("""\ def check_answer(user_answer): import subprocess success = False err = "Incorrect Answer" mark_fraction = 0.0 proc = subprocess.Popen( user_answer, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) stdout,stderr = proc.communicate() if stdout.decode("utf-8") == "Hello, world!": success, err, mark_fraction = True, "", 1.0 return success, err, mark_fraction """) test_case_data = [{ "test_case_type": "hooktestcase", "hook_code": hook_code, "weight": 1.0, "hidden": False }] kwargs = { 'metadata': { 'user_answer': user_answer, 'file_paths': self.file_paths, 'partial_grading': False, 'language': 'bash' }, 'test_case_data': test_case_data, } # When grader = Grader(self.in_dir) result = grader.evaluate(kwargs) # Then self.assertFalse(result.get('success')) self.assert_correct_output(self.timeout_msg, result.get("error")[0]["message"]) parent_proc = Process(os.getpid()).children() if parent_proc: children_procs = Process(parent_proc[0].pid) self.assertFalse(any(children_procs.children(recursive=True)))
def instant_process_statistics(pid): """ Return the instant jiffies and memory values for the process identified by pid. """ work = memory = 0 try: proc = Process(pid) for p in [proc] + proc.children(recursive=True): work += sum(p.cpu_times()) memory += p.memory_percent() except (NoSuchProcess, ValueError): # process may have disappeared in the interval pass # take into account the number of processes for the process work return work, memory
def test_cleanup_children_on_terminate(self): """ Subprocesses spawned by tasks should be terminated on terminate """ task = HangingSubprocessTask() queue = mock.Mock() worker_id = 1 task_process = TaskProcess(task, worker_id, queue) task_process.start() parent = Process(task_process.pid) while not parent.children(): # wait for child process to startup sleep(0.01) [child] = parent.children() task_process.terminate() child.wait(timeout=1.0) # wait for terminate to complete self.assertFalse(parent.is_running()) self.assertFalse(child.is_running())
def test_infinite_loop(self): # Given user_answer = ("#!/bin/bash\nwhile [ 1 ] ;" " do echo "" > /dev/null ; done") hook_code = dedent("""\ def check_answer(user_answer): import subprocess success = False err = "Incorrect Answer" mark_fraction = 0.0 proc = subprocess.Popen(user_answer, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) stdout,stderr = proc.communicate() if stdout.decode("utf-8") == "Hello, world!": success, err, mark_fraction = True, "", 1.0 return success, err, mark_fraction """ ) test_case_data = [{"test_case_type": "hooktestcase", "hook_code": hook_code,"weight": 1.0 }] kwargs = { 'metadata': { 'user_answer': user_answer, 'file_paths': self.file_paths, 'partial_grading': False, 'language': 'bash' }, 'test_case_data': test_case_data, } # When grader = Grader(self.in_dir) result = grader.evaluate(kwargs) # Then self.assertFalse(result.get('success')) self.assert_correct_output(self.timeout_msg, result.get("error")[0]["message"] ) parent_proc = Process(os.getpid()).children() if parent_proc: children_procs = Process(parent_proc[0].pid) self.assertFalse(any(children_procs.children(recursive=True)))
def trace(): start_time = datetime.datetime.now() # Spin up the main process cmd_list = sys.argv[1:] sp = subprocess.Popen(cmd_list) main_process = Process(sp.pid) while sp.poll() is None: now = time.time() pids = [Process(sp.pid)] pids.extend(main_process.children(recursive=True)) get_samples(pids) time.sleep(max(1 - (time.time() - now), 0.001)) logs_dir = '/tmp/trace_logs' LOG.info("Checking for the logs directory {0}".format(logs_dir)) if not exists(logs_dir): LOG.info("Creating the logs directory {0}".format(logs_dir)) makedirs(logs_dir) sqlite_file = join(logs_dir, '{0}_{1}_log.db'.format(start_time.strftime('%Y%m%d%H%M%S'), sp.pid)) engine = create_engine('sqlite:///{0}'.format(sqlite_file)) sqlite_connection = engine.connect() TRACE_METADATA.create_all(sqlite_connection) LOG.info("Processing samples ...") transaction = sqlite_connection.begin() insert = PROCESS_DETAILS.insert() for key in MAP_PROCESS_DETAILS.keys(): row = MAP_PROCESS_DETAILS[key] sqlite_connection.execute( insert, pid=row[0], ppid=row[1], name=row[2], cmd_line=' '.join(row[3]), create_time=row[4], ) transaction.commit() for key in MAP_SAMPLES.keys(): LOG.info('Writing data for {0}'.format(key)) pas = [process_sample(x) for x in MAP_SAMPLES.get(key)] LOG.info("Compute CPU statistics ...") compute_usage(pas, key, print_list=False, sqlite=sqlite_connection) sqlite_connection.close()
def get_info(component_path, format): component_details = open(os.path.join(component_path, ZATO_INFO_FILE)).read() out = { 'component_details': component_details, 'component_full_path': component_path, 'component_host': current_host(), 'component_running': False, 'current_time': datetime.now().isoformat(), 'current_time_utc': datetime.utcnow().isoformat(), 'master_proc_connections': None, 'master_proc_pid': None, 'master_proc_name': None, 'master_proc_create_time': None, 'master_proc_create_time_utc': None, 'master_proc_username': None, 'master_proc_workers_no': None, 'master_proc_workers_pids': None, } master_proc_pid = None try: master_proc_pid = int(open(os.path.join(component_path, MISC.PIDFILE)).read()) except(IOError, ValueError): # Ok, no such file or it's empty pass if master_proc_pid: out['component_running'] = True master_proc = Process(master_proc_pid) workers_pids = sorted(elem.pid for elem in master_proc.children()) out['master_proc_connections'] = format_connections(master_proc.connections(), format) out['master_proc_pid'] = master_proc.pid out['master_proc_create_time'] = datetime.fromtimestamp(master_proc.create_time()).isoformat() out['master_proc_create_time_utc'] = datetime.fromtimestamp(master_proc.create_time(), UTC).isoformat() out['master_proc_username'] = master_proc.username() out['master_proc_name'] = master_proc.name() out['master_proc_workers_no'] = len(workers_pids) out['master_proc_workers_pids'] = workers_pids for pid in workers_pids: worker = Process(pid) out['worker_{}_create_time'.format(pid)] = datetime.fromtimestamp(worker.create_time()).isoformat() out['worker_{}_create_time_utc'.format(pid)] = datetime.fromtimestamp(worker.create_time(), UTC).isoformat() out['worker_{}_connections'.format(pid)] = format_connections(worker.connections(), format) return out
def wait_output(settings, popen): """Returns `True` if we can get output of the command in the `settings.wait_command` time. Command will be killed if it wasn't finished in the time. """ proc = Process(popen.pid) try: proc.wait(settings.wait_command) return True except TimeoutExpired: for child in proc.children(recursive=True): child.kill() proc.kill() return False
def run_tests(self, runner): pg_data = local.path("/test-data/") pg_path = local.path("/usr/bin/postgres") postgres = wrapping.wrap(pg_path, self) def pg_su(command): return su['-c', command, '-g', 'postgres', 'postgres'] dropdb = pg_su('/usr/bin/dropdb') createdb = pg_su("/usr/bin/createdb") pgbench = pg_su("/usr/bin/pgbench") initdb = pg_su("/usr/bin/initdb") pg_server = pg_su(pg_path) with local.env(PGPORT="54329", PGDATA=pg_data): if not pg_data.exists(): runner(initdb) with pg_server.bgrun() as postgres: #We get the PID of the running 'pg_server, which is actually #the PID of the uchroot binary. This is not the PID we #want to send a SIGTERM to. #We need to enumerate all children of 'postgres' recursively #and select the one PID that is named 'postgres.bin' and has #not a process with the same name as parent. #This should be robust enough, as long as postgres doesn't #switch process names after forking. sleep(3) postgres_root = Process(pid=postgres.pid) real_postgres = [ c.pid for c in postgres_root.children(True) if c.name() == 'postgres.bin' and c.parent().name() != 'postgres.bin' ] try: runner(createdb) runner(pgbench["-i", "portage"]) runner(pgbench["-c", 1, "-S", "-t", 1000000, "portage"]) runner(dropdb["portage"]) finally: kill("-sSIGTERM", real_postgres[0])
def _wait_output(popen, is_slow): """Returns `True` if we can get output of the command in the `settings.wait_command` time. Command will be killed if it wasn't finished in the time. :type popen: Popen :rtype: bool """ proc = Process(popen.pid) try: proc.wait(settings.wait_slow_command if is_slow else settings.wait_command) return True except TimeoutExpired: for child in proc.children(recursive=True): _kill_process(child) _kill_process(proc) return False
def run_tests(self, experiment): pg_data = "/test-data/" pg_path = "/usr/lib64/postgresql-9.4/bin/postgres" wrap(self.outside(pg_path), experiment, self.builddir) cuchroot = uchroot(uid=250, gid=250) dropdb = cuchroot["/usr/bin/dropdb"] createdb = cuchroot["/usr/bin/createdb"] pgbench = cuchroot["/usr/bin/pgbench"] initdb = cuchroot["/usr/bin/initdb"] pg_server = cuchroot[pg_path] with local.env(PGPORT="54329", PGDATA=pg_data): if not path.exists(self.outside(pg_data)): run(initdb) with pg_server.bgrun() as postgres: #We get the PID of the running 'pg_server, which is actually #the PID of the uchroot binary. This is not the PID we #want to send a SIGTERM to. #We need to enumerate all children of 'postgres' recursively #and select the one PID that is named 'postgres.bin' and has #not a process with the same name as parent. #This should be robust enough, as long as postgres doesn't #switch process names after forking. sleep(3) postgres_root = Process(pid=postgres.pid) real_postgres = [c.pid for c in postgres_root.children(True) if c.name() == 'postgres.bin' and c.parent().name() != 'postgres.bin'] try: run(createdb) run(pgbench["-i", "portage"]) run(pgbench["-c", 1, "-S", "-t", 1000000, "portage"]) run(dropdb["portage"]) finally: kill("-sSIGTERM", real_postgres[0])
def test_infinite_loop(self): user_answer = ("funcprot(0)\nfunction[c]=add(a,b)" "\n\tc=a;\nwhile(1==1)\nend\nendfunction") kwargs = { 'metadata': { 'user_answer': user_answer, 'file_paths': self.file_paths, 'partial_grading': False, 'language': 'scilab' }, 'test_case_data': self.test_case_data, } grader = Grader(self.in_dir) result = grader.evaluate(kwargs) self.assertFalse(result.get("success")) self.assert_correct_output(self.timeout_msg, result.get("error")[0]["message"] ) parent_proc = Process(os.getpid()).children() if parent_proc: children_procs = Process(parent_proc[0].pid) self.assertFalse(any(children_procs.children(recursive=True)))
def run(self): # Get the start time start_time = datetime.now() self._date_string = start_time.strftime('%Y%m%d%H%M%S') timestamp = start_time.strftime(TIMESTAMP_FORMAT) # Spin up the main process sp = subprocess.Popen(self._command_list) # Open trace file trace_details_file_name = self._get_file_name(sp.pid, TRACE_DETAILS) with open(trace_details_file_name, 'w', 1) as trace_file: writer = csv.writer(trace_file, lineterminator='\n') writer.writerow(['start_time', 'cmd_line', 'sample_rate', 'tick', 'page_size', 'cpu_count']) writer.writerow([timestamp, ' '.join(self._command_list), self._sample_rate, os.sysconf(os.sysconf_names['SC_CLK_TCK']), resource.getpagesize(), cpu_count()]) stat_file = open(self._get_file_name(sp.pid, STAT_DETAILS), 'w', BUFFER_SIZE_10K) self._csv_stat_writer = csv.writer(stat_file, lineterminator='\n') self._csv_stat_writer.writerow( ['timestamp', 'user', 'nice', 'system', 'idle', 'iowait', 'irq', 'softirq', 'steal', 'guest', 'guest_nice'] ) process_file = open(self._get_file_name(sp.pid, PROCESS_DETAILS), 'w', BUFFER_SIZE_10K) self._csv_process_writer = csv.writer(process_file, lineterminator='\n') self._csv_process_writer.writerow( ['pid', 'ppid', 'name', 'cmd_line', 'create_time'] ) log_file = open(self._get_file_name(sp.pid, LOG_DETAILS), 'w', BUFFER_SIZE_100K) self._csv_log_writer = csv.writer(log_file, lineterminator='\n') self._csv_log_writer.writerow( ['pid', 'timestamp', 'state', 'utime', 'stime', 'cutime', 'cstime', 'priority', 'nice', 'num_threads', 'vsize', 'rss', 'blkio_ticks', 'rchar', 'wchar', 'syscr', 'syscw', 'read_bytes', 'write_bytes', 'cancelled_write_bytes'] ) # noinspection PyBroadException try: main_process = Process(sp.pid) while sp.poll() is None: now = time.time() pids = [Process(sp.pid)] pids.extend(main_process.children(recursive=True)) self._get_samples(pids) time.sleep(max(1 - (time.time() - now), 0.001)) except Exception: LOG.exception('An exception slipped through') finally: # Close the writers stat_file.close() process_file.close() log_file.close()
def _on_server(self, args): os.chdir(self.original_dir) abs_args_path = os.path.abspath(args.path) component_details = open(os.path.join(abs_args_path, ZATO_INFO_FILE)).read() out = { 'component_details': component_details, 'component_full_path': abs_args_path, 'component_host': current_host(), 'component_running': False, 'current_time': datetime.now().isoformat(), 'current_time_utc': datetime.utcnow().isoformat(), 'master_proc_connections': None, 'master_proc_pid': None, 'master_proc_name': None, 'master_proc_create_time': None, 'master_proc_create_time_utc': None, 'master_proc_username': None, 'master_proc_workers_no': None, 'master_proc_workers_pids': None, } master_proc_pid = None try: master_proc_pid = int(open(os.path.join(abs_args_path, MISC.PIDFILE)).read()) except(IOError, ValueError): # Ok, no such file or it's empty pass if master_proc_pid: out['component_running'] = True master_proc = Process(master_proc_pid) workers_pids = sorted(elem.pid for elem in master_proc.children()) out['master_proc_connections'] = master_proc.connections() out['master_proc_pid'] = master_proc.pid out['master_proc_create_time'] = datetime.fromtimestamp(master_proc.create_time()).isoformat() out['master_proc_create_time_utc'] = datetime.fromtimestamp(master_proc.create_time(), UTC).isoformat() out['master_proc_username'] = master_proc.username() out['master_proc_name'] = master_proc.name() out['master_proc_workers_no'] = len(workers_pids) out['master_proc_workers_pids'] = workers_pids for pid in workers_pids: worker = Process(pid) out['worker_{}_create_time'.format(pid)] = datetime.fromtimestamp(worker.create_time()).isoformat() out['worker_{}_create_time_utc'.format(pid)] = datetime.fromtimestamp(worker.create_time(), UTC).isoformat() out['worker_{}_connections'.format(pid)] = worker.connections() if getattr(args, 'json', False): out['component_details'] = loads(out['component_details']) self.logger.info(dumps(out)) else: cols_width = args.cols_width if args.cols_width else DEFAULT_COLS_WIDTH cols_width = (elem.strip() for elem in cols_width.split(',')) cols_width = [int(elem) for elem in cols_width] table = Texttable() table.set_cols_width(cols_width) # Use text ('t') instead of auto so that boolean values don't get converted into ints table.set_cols_dtype(['t', 't']) rows = [['Key', 'Value']] rows.extend(sorted(out.items())) table.add_rows(rows) self.logger.info(table.draw())
def test_sigint(self): ''' Setup test server sleep 0.01 for each request Start spider in separate python shell (untill sigin or max 200 requests) Wait 1 sec (~100 requests, in reality less because of process start-up time) Send SIGINT to the process Check it returned with 13 or 139 codes 139 code means segfault (yeah...o_O) But as I see from logs it segfaults after successfully processing the SIGINT and this is all I need from this test ''' #logging.error('step-0') # pylint: disable=no-member self.server.response['sleep'] = 0.01 # pylint: enable=no-member with temp_file() as path: with open(path, 'w') as out: # pylint: disable=no-member out.write(self.script_tpl % ('', self.server.get_url())) # pylint: enable=no-member ret_codes = [] for _ in range(10): #logging.error('step-1') proc = Popen('python %s' % path, shell=True) #logging.error('step-2') parent = Process(proc.pid) #logging.error('step-3') time.sleep(1) #logging.error('killing children') for child in parent.children(): #logging.error('CHILD: %s', child.pid) # Sending multiple SIGINTs # because in very rare cases the only # sigint signals is ignored :-/ # do not send too fast for _ in range(1): try: #logging.error('sending sigint') child.send_signal(SIGNAL_INT) except NoSuchProcess: break else: time.sleep(1) if platform.system() == 'Darwin': # On OSX the Popen(shell=True) spawns only # one process, no child #logging.error('Killing parent') #logging.error('PARENT: %s', parent.pid) # Sending multiple SIGINTs # because in very rare cases the only # sigint signals is ignored :-/ # do not send too fast for _ in range(1): try: #logging.error('sending sigint') parent.send_signal(SIGNAL_INT) except NoSuchProcess: break else: time.sleep(1) #logging.error('step-4') ret = None for _ in range(20): #print('before proc-poll-%d' % step) ret = proc.poll() if ret is not None: break time.sleep(0.1) else: #logging.error('CHILD PROCESS DID NOT RETURN') #raise Exception('Child process did not return') # try to clean processes try: for child in parent.children(): child.send_signal(signal.SIGTERM) except NoSuchProcess: pass time.sleep(0.5) try: parent.send_signal(signal.SIGTERM) except NoSuchProcess: pass #logging.error('step-5') # FIXME: find out the reasonf of segfault # the 130 signal means the program was terminated by ctrl-c #print('RET CODE: %s' % ret) ret_codes.append(ret) # Could fail in 10% (1 of 10) # pylint: disable=no-member self.assertTrue(sum(1 for x in ret_codes if x in (13, 130, 139)) >= 9)