我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用resource.RUSAGE_SELF。
def Debug(): try: global debugCount debugCount = debugCount + 1 resUsage=getrusage(RUSAGE_SELF) size=resUsage.ru_maxrss info("Memory usage : " + str(debugCount) + " size: " + str(size)) info("Resouce usage info: " + str(resUsage)) # Memory leaks display currently commented out # show_growth() # obj=get_leaking_objects() # warn('Leaking objects size='+str(len(obj))) # filepathdebug='/var/log/myDebug'+str(debugCount) # with open(filepathdebug, "w+") as f: #replace filepath & filename # f.write('Debug resouce iteration: ' + str(debugCount) + " size: " + str(size)) # f.write('Leaking objects size='+str(len(obj)) + '\n') # f.write('Leaking objects size='+str(typestats()) + '\n') # f.write('Leaking objects'+str(obj) + '\n') except Exception as e: error('failed to track memory: ' + str(e))
def profiler(frame, event, arg): if event not in ('call','return'): return profiler #### gather stats #### rusage = getrusage(RUSAGE_SELF) t_cpu = rusage[0] + rusage[1] # user time + system time code = frame.f_code fun = (code.co_name, code.co_filename, code.co_firstlineno) #### get stack with functions entry stats #### ct = threading.currentThread() try: p_stack = ct.p_stack except AttributeError: ct.p_stack = deque() p_stack = ct.p_stack #### handle call and return #### if event == 'call': p_stack.append((time(), t_cpu, fun)) elif event == 'return': try: t,t_cpu_prev,f = p_stack.pop() assert f == fun except IndexError: # TODO investigate t,t_cpu_prev,f = p_start_time, 0.0, None call_cnt, t_sum, t_cpu_sum = p_stats.get(fun, (0, 0.0, 0.0)) p_stats[fun] = (call_cnt+1, t_sum+time()-t, t_cpu_sum+t_cpu-t_cpu_prev) return profiler
def max_mem_usage(): """ Returns ------- The max memory used by this process and its children, in MB. """ denom = 1024. if sys.platform == 'darwin': denom *= denom total = getrusage(RUSAGE_SELF).ru_maxrss / denom total += getrusage(RUSAGE_CHILDREN).ru_maxrss / denom return total
def memusage( point = "") : usage = resource.getrusage( resource.RUSAGE_SELF) return '''%s: usertime = %s systime = %s mem = %s mb '''%( point, usage[ 0 ], usage[ 1 ], ( usage[ 2 ]*resource.getpagesize( ) ) /1000000.0 )
def _get_usage_info(): rusage = getrusage(RUSAGE_SELF) curr_usage = rusage.ru_utime + rusage.ru_stime curr_time = time.time() return curr_usage, curr_time