def run(self): # Elevate our priority, set us to the highest so that we can more effectively throttle success = windows_api.set_current_thread_priority(windows_api.THREAD_PRIORITY_HIGHEST) # If we failed to get HIGHEST priority, try above normal, else we're still at default if not success: windows_api.set_current_thread_priority(windows_api.THREAD_PRIORITY_ABOVE_NORMAL) # Run while the process is running while True: try: # Get the frequency frequency = repy_constants.CPU_POLLING_FREQ_WIN # Base amount of sleeping on return value of # win_check_cpu_use to prevent under/over sleeping slept = win_check_cpu_use(nanny.get_resource_limit("cpu"), self.pid) if slept == -1: # Something went wrong, try again pass elif (slept < frequency): time.sleep(frequency-slept) except windows_api.DeadProcess: # Process may be dead harshexit.harshexit(97) except: tracebackrepy.handle_exception() print("CPU Nanny died! Trying to kill everything else") harshexit.harshexit(25)
def run(self): # Elevate our priority, set us to the highest so that we can more effectively throttle success = windows_api.set_current_thread_priority(windows_api.THREAD_PRIORITY_HIGHEST) # If we failed to get HIGHEST priority, try above normal, else we're still at default if not success: windows_api.set_current_thread_priority(windows_api.THREAD_PRIORITY_ABOVE_NORMAL) # Run while the process is running while True: try: # Get the frequency frequency = repy_constants.CPU_POLLING_FREQ_WIN # Base amount of sleeping on return value of # win_check_cpu_use to prevent under/over sleeping slept = win_check_cpu_use(nanny_resource_limits.resource_limit("cpu"), self.pid) if slept == -1: # Something went wrong, try again pass elif (slept < frequency): time.sleep(frequency-slept) except windows_api.DeadProcess: # Process may be dead harshexit.harshexit(97) except: tracebackrepy.handle_exception() print >> sys.stderr, "CPU Nanny died! Trying to kill everything else" harshexit.harshexit(25)
def run(self): # How often the memory will be checked (seconds) memory_check_interval = repy_constants.CPU_POLLING_FREQ_WIN # The ratio of the disk polling time to memory polling time. disk_to_memory_ratio = int(repy_constants.DISK_POLLING_HDD / memory_check_interval) # Which cycle number we're on counter = 0 # Elevate our priority, above normal is higher than the usercode, and is enough for disk/mem windows_api.set_current_thread_priority( windows_api.THREAD_PRIORITY_ABOVE_NORMAL) # need my pid to get a process handle... mypid = os.getpid() # run forever (only exit if an error occurs) while True: try: # Increment the interval counter counter += 1 # Check memory use, get the WorkingSetSize or RSS memused = windows_api.process_memory_info( mypid)['WorkingSetSize'] if memused > nanny.get_resource_limit("memory"): # We will be killed by the other thread... raise Exception, "Memory use '" + str( memused) + "' over limit '" + str( nanny.get_resource_limit("memory")) + "'" # Check if we should check the disk if (counter % disk_to_memory_ratio) == 0: # Check diskused diskused = compute_disk_use( repy_constants.REPY_CURRENT_DIR) if diskused > nanny.get_resource_limit("diskused"): raise Exception, "Disk use '" + str( diskused) + "' over limit '" + str( nanny.get_resource_limit("diskused")) + "'" # Sleep until the next iteration of checking the memory time.sleep(memory_check_interval) except windows_api.DeadProcess: # Process may be dead, or die while checking memory use # In any case, there is no reason to continue running, just exit harshexit.harshexit(99) except: tracebackrepy.handle_exception() print >> sys.stderr, "Nanny died! Trying to kill everything else" harshexit.harshexit(20)
def run(self): # How often the memory will be checked (seconds) memory_check_interval = repy_constants.CPU_POLLING_FREQ_WIN # The ratio of the disk polling time to memory polling time. disk_to_memory_ratio = int(repy_constants.DISK_POLLING_HDD / memory_check_interval) # Which cycle number we're on counter = 0 # Elevate our priority, above normal is higher than the usercode, and is enough for disk/mem windows_api.set_current_thread_priority(windows_api.THREAD_PRIORITY_ABOVE_NORMAL) # need my pid to get a process handle... mypid = os.getpid() # run forever (only exit if an error occurs) while True: try: # Increment the interval counter counter += 1 # Check memory use, get the WorkingSetSize or RSS memused = windows_api.process_memory_info(mypid)["WorkingSetSize"] if memused > nanny.get_resource_limit("memory"): # We will be killed by the other thread... raise Exception, "Memory use '" + str(memused) + "' over limit '" + str( nanny.get_resource_limit("memory") ) + "'" # Check if we should check the disk if (counter % disk_to_memory_ratio) == 0: # Check diskused diskused = compute_disk_use(repy_constants.REPY_CURRENT_DIR) if diskused > nanny.get_resource_limit("diskused"): raise Exception, "Disk use '" + str(diskused) + "' over limit '" + str( nanny.get_resource_limit("diskused") ) + "'" # Sleep until the next iteration of checking the memory time.sleep(memory_check_interval) except windows_api.DeadProcess: # Process may be dead, or die while checking memory use # In any case, there is no reason to continue running, just exit harshexit.harshexit(99) except: tracebackrepy.handle_exception() print >> sys.stderr, "Nanny died! Trying to kill everything else" harshexit.harshexit(20)
def run(self): # Calculate how often disk should be checked if ostype == "WindowsCE": disk_interval = int(repy_constants.RESOURCE_POLLING_FREQ_WINCE / repy_constants.CPU_POLLING_FREQ_WINCE) else: disk_interval = int(repy_constants.RESOURCE_POLLING_FREQ_WIN / repy_constants.CPU_POLLING_FREQ_WIN) current_interval = 0 # What cycle are we on # Elevate our priority, above normal is higher than the usercode, and is enough for disk/mem windows_api.set_current_thread_priority(windows_api.THREAD_PRIORITY_ABOVE_NORMAL) # need my pid to get a process handle... mypid = os.getpid() # run forever (only exit if an error occurs) while True: try: # Check memory use, get the WorkingSetSize or RSS memused = windows_api.process_memory_info(mypid)['WorkingSetSize'] if memused > nanny_resource_limits.resource_limit("memory"): # We will be killed by the other thread... raise Exception, "Memory use '"+str(memused)+"' over limit '"+str(nanny_resource_limits.resource_limit("memory"))+"'" # Increment the interval we are on current_interval += 1 # Check if we should check the disk if (current_interval % disk_interval) == 0: # Check diskused diskused = compute_disk_use(repy_constants.REPY_CURRENT_DIR) if diskused > nanny_resource_limits.resource_limit("diskused"): raise Exception, "Disk use '"+str(diskused)+"' over limit '"+str(nanny_resource_limits.resource_limit("diskused"))+"'" if ostype == 'WindowsCE': time.sleep(repy_constants.CPU_POLLING_FREQ_WINCE) else: time.sleep(repy_constants.CPU_POLLING_FREQ_WIN) except windows_api.DeadProcess: # Process may be dead, or die while checking memory use # In any case, there is no reason to continue running, just exit harshexit.harshexit(99) except: tracebackrepy.handle_exception() print >> sys.stderr, "Nanny died! Trying to kill everything else" harshexit.harshexit(20)
def set_high_priority(): ''' <Purpose> Set current thread the priority as high as possible <Arguments> None <Exceptions> None <Side Effects> None <Returns> None ''' if ostype == 'Windows': # Elevate our priority, set us to the highest so that we can more effectively throttle success = windows_api.set_current_thread_priority(windows_api.THREAD_PRIORITY_HIGHEST) # If we failed to get HIGHEST priority, try above normal, else we're still at default if not success: windows_api.set_current_thread_priority(windows_api.THREAD_PRIORITY_ABOVE_NORMAL) # print 'priority success is ', success elif ostype == 'Linux': # Linux version is to be added later. pass
def run(self): global stopfilename, frequency, run_thread_lock # On Windows elevate our priority above the user code. if harshexit.ostype in ["Windows", "WindowsCE"]: # Elevate our priority, above normal is higher than the usercode windows_api.set_current_thread_priority(windows_api.THREAD_PRIORITY_ABOVE_NORMAL) while True: # Attempt to get the lock have_lock = run_thread_lock.acquire(False) # If we have the lock, release and continue. Else break and exit the thread if have_lock: run_thread_lock.release() else: break # Get the status lock statuslock.acquire() # Write out our status statusstorage.write_status("Started") # Release the status lock statuslock.release() # Look for the stopfile if stopfilename != None and os.path.exists(stopfilename): try: # Get a file object for the file fileobject = safe_open(stopfilename) # Read in the contents, close the object contents = fileobject.read() fileobject.close() # Check the length, if there is nothing then just close as stopped if len(contents) > 0: # Split, at most we have 2 parts, the exit code and message (exitcode, mesg) = contents.split(";",1) exitcode = int(exitcode) # Check if exitcode is 56, which stands for ThreadErr is specified # ThreadErr cannot be specified externally, since it has side-affects # such as changing global thread restrictions if exitcode == 56: raise Exception, "ThreadErr exit code specified. Exit code not allowed." # Print the message, then call harshexit with the exitcode if mesg != "": print mesg _stopfile_exit(exitcode, self.repy_process_id) else: raise Exception, "Stopfile has no content." except: # On any issue, just do "Stopped" (44) _stopfile_exit(44, self.repy_process_id) # Sleep until the next loop around. time.sleep(frequency)
def run(self): global stopfilename, frequency, run_thread_lock # On Windows elevate our priority above the user code. if harshexit.ostype in ["Windows", "WindowsCE"]: # Elevate our priority, above normal is higher than the usercode windows_api.set_current_thread_priority( windows_api.THREAD_PRIORITY_ABOVE_NORMAL) while True: # Attempt to get the lock have_lock = run_thread_lock.acquire(False) # If we have the lock, release and continue. Else break and exit the thread if have_lock: run_thread_lock.release() else: break # Get the status lock statuslock.acquire() # Write out our status statusstorage.write_status("Started") # Release the status lock statuslock.release() # Look for the stopfile if stopfilename != None and os.path.exists(stopfilename): try: # Get a file object for the file fileobject = safe_open(stopfilename) # Read in the contents, close the object contents = fileobject.read() fileobject.close() # Check the length, if there is nothing then just close as stopped if len(contents) > 0: # Split, at most we have 2 parts, the exit code and message (exitcode, mesg) = contents.split(";", 1) exitcode = int(exitcode) # Check if exitcode is 56, which stands for ThreadErr is specified # ThreadErr cannot be specified externally, since it has side-affects # such as changing global thread restrictions if exitcode == 56: raise Exception, "ThreadErr exit code specified. Exit code not allowed." # Print the message, then call harshexit with the exitcode if mesg != "": print mesg _stopfile_exit(exitcode, self.repy_process_id) else: raise Exception, "Stopfile has no content." except: # On any issue, just do "Stopped" (44) _stopfile_exit(44, self.repy_process_id) # Sleep until the next loop around. time.sleep(frequency)