def getProcessName(pid, default = None, cacheFailure = True): """ Provides the name associated with the given process id. This isn't available on all platforms. Arguments: pid - process id for the process being returned default - result if the process name can't be retrieved (raises an IOError on failure instead if undefined) cacheFailure - if the lookup fails and there's a default then caches the default value to prevent further lookups """ if pid in PROCESS_NAME_CACHE: return PROCESS_NAME_CACHE[pid] processName, raisedExc = "", None # fetch it from proc contents if available if procTools.isProcAvailable(): try: processName = procTools.getStats(pid, procTools.Stat.COMMAND)[0] except IOError, exc: raisedExc = exc
def getProcessName(pid, default=None, cacheFailure=True): """ Provides the name associated with the given process id. This isn't available on all platforms. Arguments: pid - process id for the process being returned default - result if the process name can't be retrieved (raises an IOError on failure instead if undefined) cacheFailure - if the lookup fails and there's a default then caches the default value to prevent further lookups """ if pid in PROCESS_NAME_CACHE: return PROCESS_NAME_CACHE[pid] processName, raisedExc = "", None # fetch it from proc contents if available if procTools.isProcAvailable(): try: processName = procTools.getStats(pid, procTools.Stat.COMMAND)[0] except IOError, exc: raisedExc = exc
def run(self): while not self._halt: timeSinceReset = time.time() - self.lastLookup if self.resolveRate == 0: self._cond.acquire() if not self._halt: self._cond.wait(0.2) self._cond.release() continue elif timeSinceReset < self.resolveRate: sleepTime = max(0.2, self.resolveRate - timeSinceReset) self._cond.acquire() if not self._halt: self._cond.wait(sleepTime) self._cond.release() continue # done waiting, try again newValues = {} try: if self._useProc: utime, stime, startTime = procTools.getStats(self.processPid, procTools.Stat.CPU_UTIME, procTools.Stat.CPU_STIME, procTools.Stat.START_TIME) totalCpuTime = float(utime) + float(stime) cpuDelta = totalCpuTime - self._lastCpuTotal newValues["cpuSampling"] = cpuDelta / timeSinceReset newValues["cpuAvg"] = totalCpuTime / (time.time() - float(startTime)) newValues["_lastCpuTotal"] = totalCpuTime memUsage = int(procTools.getMemoryUsage(self.processPid)[0]) totalMemory = procTools.getPhysicalMemory() newValues["memUsage"] = memUsage newValues["memUsagePercentage"] = float(memUsage) / totalMemory else: # the ps call formats results as: # # TIME ELAPSED RSS %MEM # 3-08:06:32 21-00:00:12 121844 23.5 # # or if Tor has only recently been started: # # TIME ELAPSED RSS %MEM # 0:04.40 37:57 18772 0.9 psCall = call("ps -p %s -o cputime,etime,rss,%%mem" % self.processPid) isSuccessful = False if psCall and len(psCall) >= 2: stats = psCall[1].strip().split() if len(stats) == 4: try: totalCpuTime = uiTools.parseShortTimeLabel(stats[0]) uptime = uiTools.parseShortTimeLabel(stats[1]) cpuDelta = totalCpuTime - self._lastCpuTotal newValues["cpuSampling"] = cpuDelta / timeSinceReset newValues["cpuAvg"] = totalCpuTime / uptime newValues["_lastCpuTotal"] = totalCpuTime newValues["memUsage"] = int(stats[2]) * 1024 # ps size is in kb newValues["memUsagePercentage"] = float(stats[3]) / 100.0 isSuccessful = True except ValueError, exc: pass if not isSuccessful: raise IOError("unrecognized output from ps: %s" % psCall) except IOError, exc: newValues = {} self._failureCount += 1 if self._useProc: if self._failureCount >= 3: # We've failed three times resolving via proc. Warn, and fall back # to ps resolutions. msg = "Failed three attempts to get process resource usage from proc, falling back to ps (%s)" % exc log.log(CONFIG["log.stats.procResolutionFailover"], msg) self._useProc = False self._failureCount = 1 # prevents lastQueryFailed() from thinking that we succeeded else: # wait a bit and try again msg = "Unable to query process resource usage from proc (%s)" % exc log.log(CONFIG["log.stats.failedProcResolution"], msg) self._cond.acquire() if not self._halt: self._cond.wait(0.5) self._cond.release() else: # exponential backoff on making failed ps calls sleepTime = 0.01 * (2 ** self._failureCount) + self._failureCount msg = "Unable to query process resource usage from ps, waiting %0.2f seconds (%s)" % (sleepTime, exc) log.log(CONFIG["log.stats.failedProcResolution"], msg) self._cond.acquire() if not self._halt: self._cond.wait(sleepTime) self._cond.release() # sets the new values if newValues: # If this is the first run then the cpuSampling stat is meaningless # (there isn't a previous tick to sample from so it's zero at this # point). Setting it to the average, which is a fairer estimate. if self.lastLookup == -1: newValues["cpuSampling"] = newValues["cpuAvg"] self._valLock.acquire() self.cpuSampling = newValues["cpuSampling"] self.cpuAvg = newValues["cpuAvg"] self.memUsage = newValues["memUsage"] self.memUsagePercentage = newValues["memUsagePercentage"] self._lastCpuTotal = newValues["_lastCpuTotal"] self.lastLookup = time.time() self._runCount += 1 self._failureCount = 0 self._valLock.release()
def run(self): while not self._halt: timeSinceReset = time.time() - self.lastLookup if self.resolveRate == 0: self._cond.acquire() if not self._halt: self._cond.wait(0.2) self._cond.release() continue elif timeSinceReset < self.resolveRate: sleepTime = max(0.2, self.resolveRate - timeSinceReset) self._cond.acquire() if not self._halt: self._cond.wait(sleepTime) self._cond.release() continue # done waiting, try again newValues = {} try: if self._useProc: utime, stime, startTime = procTools.getStats( self.processPid, procTools.Stat.CPU_UTIME, procTools.Stat.CPU_STIME, procTools.Stat.START_TIME) totalCpuTime = float(utime) + float(stime) cpuDelta = totalCpuTime - self._lastCpuTotal newValues["cpuSampling"] = cpuDelta / timeSinceReset newValues["cpuAvg"] = totalCpuTime / (time.time() - float(startTime)) newValues["_lastCpuTotal"] = totalCpuTime memUsage = int( procTools.getMemoryUsage(self.processPid)[0]) totalMemory = procTools.getPhysicalMemory() newValues["memUsage"] = memUsage newValues["memUsagePercentage"] = float( memUsage) / totalMemory else: # the ps call formats results as: # # TIME ELAPSED RSS %MEM # 3-08:06:32 21-00:00:12 121844 23.5 # # or if Tor has only recently been started: # # TIME ELAPSED RSS %MEM # 0:04.40 37:57 18772 0.9 psCall = call("ps -p %s -o cputime,etime,rss,%%mem" % self.processPid) isSuccessful = False if psCall and len(psCall) >= 2: stats = psCall[1].strip().split() if len(stats) == 4: try: totalCpuTime = uiTools.parseShortTimeLabel( stats[0]) uptime = uiTools.parseShortTimeLabel(stats[1]) cpuDelta = totalCpuTime - self._lastCpuTotal newValues[ "cpuSampling"] = cpuDelta / timeSinceReset newValues["cpuAvg"] = totalCpuTime / uptime newValues["_lastCpuTotal"] = totalCpuTime newValues["memUsage"] = int( stats[2]) * 1024 # ps size is in kb newValues["memUsagePercentage"] = float( stats[3]) / 100.0 isSuccessful = True except ValueError, exc: pass if not isSuccessful: raise IOError("unrecognized output from ps: %s" % psCall) except IOError, exc: newValues = {} self._failureCount += 1 if self._useProc: if self._failureCount >= 3: # We've failed three times resolving via proc. Warn, and fall back # to ps resolutions. msg = "Failed three attempts to get process resource usage from proc, falling back to ps (%s)" % exc log.log(CONFIG["log.stats.procResolutionFailover"], msg) self._useProc = False self._failureCount = 1 # prevents lastQueryFailed() from thinking that we succeeded else: # wait a bit and try again msg = "Unable to query process resource usage from proc (%s)" % exc log.log(CONFIG["log.stats.failedProcResolution"], msg) self._cond.acquire() if not self._halt: self._cond.wait(0.5) self._cond.release() else: # exponential backoff on making failed ps calls sleepTime = 0.01 * ( 2**self._failureCount) + self._failureCount msg = "Unable to query process resource usage from ps, waiting %0.2f seconds (%s)" % ( sleepTime, exc) log.log(CONFIG["log.stats.failedProcResolution"], msg) self._cond.acquire() if not self._halt: self._cond.wait(sleepTime) self._cond.release() # sets the new values if newValues: # If this is the first run then the cpuSampling stat is meaningless # (there isn't a previous tick to sample from so it's zero at this # point). Setting it to the average, which is a fairer estimate. if self.lastLookup == -1: newValues["cpuSampling"] = newValues["cpuAvg"] self._valLock.acquire() self.cpuSampling = newValues["cpuSampling"] self.cpuAvg = newValues["cpuAvg"] self.memUsage = newValues["memUsage"] self.memUsagePercentage = newValues["memUsagePercentage"] self._lastCpuTotal = newValues["_lastCpuTotal"] self.lastLookup = time.time() self._runCount += 1 self._failureCount = 0 self._valLock.release()