def get_physmem(): usage = {} fields = list(phymem_usage()._fields) mem = phymem_usage() for field in fields: usage["physical_memory_" + field] = mem.__getattribute__(field) return usage
def runNACESS (path_file_in, pr_result, probe = "", verbose = 1, multi_run = 0): """ Run NACCESS with file pdb args: -> filin pdb return: path files .asa and .rsa """ # check if file exist if os.path.isfile(path_file_in[0:-4] + ".asa") and os.path.isfile(path_file_in[0:-4] + ".rsa") : print "continue" return [path_file_in[0:-4] + ".asa", path_file_in[0:-4] + ".rsa"] # if multi_run == 1 : #CPU time_cpu = CPUUsage.CPUsage().compute() #MEM proc = psutil.phymem_usage() memory = proc.percent print memory while float(time_cpu) > 70 or float(memory) > 30 : print "pause ", CPUUsage.CPUsage().compute(), memory sleep (0.5) time_cpu = CPUUsage.CPUsage().compute() memory = psutil.phymem_usage().percent continue cmd_run = naccess + " " + path_file_in + "&" print cmd_run os.system (cmd_run) try : os.system("mv *.asa " + pr_result) os.system("mv *.rsa " + pr_result) os.system("rm *.log") except : pass return [path_file_in[0:-4] + ".asa", path_file_in[0:-4] + ".rsa"] else : cmd_run = naccess + " " + path_file_in print cmd_run os.system (cmd_run) try : os.system("mv *.asa " + pr_result) os.system("mv *.rsa " + pr_result) os.system("rm *.log") except : pass return [path_file_in[0:-4] + ".asa", path_file_in[0:-4] + ".rsa"] return "ERROR"
def CorrectThreadCountAccordingToAvailableMem(ThreadsCount): available_mem_gib = None try: available_mem_gib = psutil.phymem_usage().total / float( 2**30) * (100 - psutil.phymem_usage().percent) / 100 except: pass used_mem_gib = None try: process = psutil.Process(os.getpid()) used_mem_gib = process.get_memory_info()[0] / float(2**30) except: try: used_mem_gib = resource.getrusage( resource.RUSAGE_SELF).ru_maxrss / float(2**20) except: pass if used_mem_gib != None and available_mem_gib != None: print('memory used: %g Gb' % used_mem_gib) print('memory available: %g Gb' % available_mem_gib) ThreadsCount = min(ThreadsCount, 4 + int(available_mem_gib / used_mem_gib * 5)) print('Thread count was corrected to %d' % ThreadsCount) return ThreadsCount else: print( 'Cannot identify sizes of total, available and used memory. Threads count will not be corrected' ) return ThreadsCount
def calculate_resizing_tofitmemory(x_size,y_size,n_slices,byte): """ Predicts the percentage (between 0 and 1) to resize the image to fit the memory, giving the following information: x_size, y_size: image size n_slices: number of slices byte: bytes allocated for each pixel sample """ imagesize = x_size * y_size * n_slices * byte * 28 # USING LIBSIGAR #import sigar #sg = sigar.open() #ram_free = sg.mem().actual_free() #ram_total = sg.mem().total() #swap_free = sg.swap().free() #sg.close() # USING PSUTIL import psutil try: if (psutil.version_info>=(0,6,0)): ram_free = psutil.virtual_memory().available ram_total = psutil.virtual_memory().total swap_free = psutil.swap_memory().free else: ram_free = psutil.phymem_usage().free + psutil.cached_phymem() + psutil.phymem_buffers() ram_total = psutil.phymem_usage().total swap_free = psutil.virtmem_usage().free except: print "Exception! psutil version < 0.3 (not recommended)" ram_total = psutil.TOTAL_PHYMEM # this is for psutil < 0.3 ram_free = 0.8 * psutil.TOTAL_PHYMEM swap_free = psutil.avail_virtmem() print "RAM_FREE=", ram_free print "RAM_TOTAL=", ram_total if (sys.platform == 'win32'): if (platform.architecture()[0] == '32bit'): if ram_free>1400000000: ram_free=1400000000 if ram_total>1400000000: ram_total=1400000000 if (sys.platform == 'linux2'): if (platform.architecture()[0] == '32bit'): if ram_free>3500000000: ram_free=3500000000 if ram_total>3500000000: ram_total=3500000000 if (swap_free>ram_total): swap_free=ram_total resize = (float((ram_free+0.5*swap_free)/imagesize)) resize=math.sqrt(resize) # this gives the "resize" for each axis x and y if (resize>1): resize=1 return round(resize,2)
def r_engine_status(): cpu_used = int(round((psutil.cpu_times().user * 100) + \ (psutil.cpu_times().system * 100), 0)) cpu_free = int(round(psutil.cpu_times().idle * 100, 0)) s = { "cpu": { "used": cpu_used, "free": cpu_free }, "memory": { "physical": { "used": psutil.phymem_usage().used, "free": psutil.phymem_usage().free }, "virtual": { "used": psutil.virtmem_usage().used, "free": psutil.virtmem_usage().free } }, "disk": { "used": psutil.disk_usage('/').used, "free": psutil.disk_usage('/').free } } r = Response("success", "status", s).get() return r
def main(): summary_stats = { "count" : 0, "size" : 0, "indexSize" : 0 } all_stats = [] all_db_stats = {} for db in connection.database_names(): # FIXME: Add an option to include oplog stats. if db == "local": continue database = connection[db] all_db_stats[database.name] = [] for collection_name in database.collection_names(): stats = get_collection_stats(database, database[collection_name]) all_stats.append(stats) all_db_stats[database.name].append(stats) summary_stats["count"] += stats["count"] summary_stats["size"] += stats["size"] summary_stats["indexSize"] += stats.get("totalIndexSize", 0) x = PrettyTable(["Collection", "Count", "% Size", "DB Size", "Avg Obj Size", "Indexes", "Index Size"]) x.set_field_align("Collection", "l") x.set_field_align("% Size", "r") x.set_field_align("Count", "r") x.set_field_align("DB Size", "r") x.set_field_align("Avg Obj Size", "r") x.set_field_align("Index Size", "r") x.set_padding_width(1) print for db in all_db_stats: db_stats = all_db_stats[db] count = 0 for stat in db_stats: count += stat["count"] x.add_row([stat["ns"], stat["count"], "%0.1f%%" % ((stat["size"] / float(summary_stats["size"])) * 100), convert_bytes(stat["size"]), convert_bytes(stat.get("avgObjSize", 0)), stat.get("nindexes", 0), convert_bytes(stat.get("totalIndexSize", 0))]) print x.printt(sortby="% Size") print "Total Documents:", summary_stats["count"] print "Total Data Size:", convert_bytes(summary_stats["size"]) print "Total Index Size:", convert_bytes(summary_stats["indexSize"]) ram_headroom = psutil.phymem_usage()[0] - summary_stats["indexSize"] print "RAM Headroom:", convert_bytes(ram_headroom) print "RAM Used: %s (%s%%)" % (convert_bytes(psutil.phymem_usage()[1]), psutil.phymem_usage()[3]) print "Available RAM Headroom:", convert_bytes((100 - psutil.phymem_usage()[3]) / 100 * ram_headroom)
def do_GET(self): print("memory_load.py got a request") port = sys.argv[1] if psutil.phymem_usage().percent < 80: # allocate 50MiB memory = ' ' * 52428800 # wait a bit duration = randint(1, 2) sleep(duration) # free up the memory again memory = None response = [ '<html>', '<head><title>Memory load test</title></head><body>', '<h1>Memory load test</h1>', 'Allocated memory: %d bytes' % (52428800), '<br>Server port: %s' % (port), '<br>Client address: %s (%s)' % (self.client_address, self.address_string()), '<br>Server version: %s' % self.server_version, '<br>System version: %s' % self.sys_version, '<br>Protocol version: %s' % self.protocol_version, '</body></html>', ] else: usage = psutil.phymem_usage() response = [ '<html>', '<head><title>Memory load test</title></head><body>', '<h1>Memory load test</h1>', 'Unable to allocate enough memory for the test, host memory full.', '<br><h2>Memory usage</h2>', '<br>total: %d, available: %d, percent: %d, used: %d, free: %d' % (usage.total, usage.available, usage.percent, usage.used, usage.free), '<br>Server port: %s' % (port), '<br>Client address: %s (%s)' % (self.client_address, self.address_string()), '<br>Server version: %s' % self.server_version, '<br>System version: %s' % self.sys_version, '<br>Protocol version: %s' % self.protocol_version, '</body></html>', ] message = '\r\n'.join(response) self.send_response(200) self.end_headers() self.wfile.write(message.encode('utf-8'))
def stat(self): ds = dict() ds['pt'] = int(psutil.phymem_usage().total / 1024) ds['pf'] = int(psutil.phymem_usage().free / 1024) ds['pu'] = int(psutil.phymem_usage().used / 1024) ds['pb'] = int(psutil.phymem_buffers() / 1024) ds['pc'] = int(psutil.cached_phymem() / 1024) ds['st'] = int(psutil.virtmem_usage().total / 1024) ds['su'] = int(psutil.virtmem_usage().used / 1024) return ds
def getMemStatus(self): memstatus = {} memstatus['total_phymem'] = str(int(psutil.TOTAL_PHYMEM ) /1024 /1024) + " MB" memstatus['total_virtmem'] = str(int(psutil.total_virtmem()) /1024 /1024) + " MB" memstatus['phymem_usage_percent'] = psutil.phymem_usage().percent memstatus['phymem_usage_used'] = str(int(psutil.phymem_usage().used) /1024 /1024) + "MB" memstatus['phtmem_usage_free'] = str(int(psutil.phymem_usage().free) /1024 /1024) + "MB" memstatus['virtmem_usage_percent'] = psutil.virtmem_usage().percent memstatus['virtmem_usage_used'] = str(int(psutil.virtmem_usage().used) /1024 /1024) + "MB" memstatus['virtmem_usage_free'] = str(int(psutil.virtmem_usage().free) /1024 /1024) + "MB" return memstatus
def get_memory_stats(self): memory = { "physical": { "used": psutil.phymem_usage().used, "free": psutil.phymem_usage().free }, "virtual": { "used": psutil.virtmem_usage().used, "free": psutil.virtmem_usage().free } } return memory
def _get_meminfo(self): """ gather memory info into a dictionary for sendEvent """ usage = ps.phymem_usage() meminfo = { 'mem_percent': ps.phymem_usage().percent, 'mem_free': self._try_get_mem_attr(usage, 'free'), 'mem_used': self._try_get_mem_attr(usage, 'used'), 'mem_available': self._try_get_mem_attr(usage, 'available'), 'mem_total': self._try_get_mem_attr(usage, 'total'), 'mem_active': self._try_get_mem_attr(usage, 'active'), 'mem_buffer': self._try_get_mem_attr(usage, 'buffers'), 'mem_cached': self._try_get_mem_attr(usage, 'cached'), 'mem_inactive': self._try_get_mem_attr(usage, 'inactive'), } return meminfo
def lowkeywords_proc(self, matches): gc.disable() with self.r1.pipeline() as pipe: pipe_prep_count = 0 pipe_size = 10000 pipe.multi() for m in matches: for word, count in json.loads( m.document.get_value(self.keywordsvi)).items(): pipe.hincrby(self.keywords_hash_rds, word, count) pipe_prep_count += 1 if pipe_prep_count % pipe_size == 0: print '<----------------', pipe_prep_count print 'keywords_hash mapreduce begin', psutil.phymem_usage( )[1] / (1024 * 1024), 'M' print 'mapreduce begin: ', str( time.strftime("%H:%M:%S", time.gmtime())) pipe.execute() pipe.reset() print 'keywords_hash mapreduce end', psutil.phymem_usage( )[1] / (1024 * 1024), 'M' print 'mapreduce end: ', str( time.strftime("%H:%M:%S", time.gmtime())) with self.r1.pipeline() as pipe: pipe_prep_count = 0 pipe_size = 10000 pipe.multi() for word, count in self.r1.hgetall(self.keywords_hash_rds).items(): pipe_prep_count += 1 if count <= 3: self.r.sadd(self.lowkeywords_set_rds, word) else: pipe.hdel(self.keywords_hash_rds, word) if pipe_prep_count % pipe_size == 0: print '<----------------', pipe_prep_count pipe.execute() pipe.reset() print 'keywords_hash clean', psutil.phymem_usage()[1] / ( 1024 * 1024), 'M' gc.enable() self.r1.flushdb() print 'keywords_hash clean', psutil.phymem_usage()[1] / (1024 * 1024), 'M' return True
def display_memory(self, sender, arguments): ''' Display the total, used and free memory values for the current system ''' usage = psutil.phymem_usage() return self.message( sender, "Total: %s | Used: %s | Free: %s | Percent Free: %s" % (byte_format(usage.total), byte_format( usage.used), byte_format(usage.free), usage.percent))
def apply_log(datadir, memory=None): """ Apply redo logs for an unpacked and uncompressed instance Args: datadir - The datadir on wich to apply logs memory - A string of how much memory can be used to apply logs. Default 10G """ if not memory: # Determine how much RAM to use for applying logs based on the # system's total RAM size; all our boxes have 32G or more, so # this will always be better than before, but not absurdly high. memory = psutil.phymem_usage()[0] / 1024 / 1024 / 1024 / 3 cmd = ' '.join(('/usr/bin/innobackupex', '--apply-log', '--use-memory={memory}G', datadir)).format(memory=memory) log_file = os.path.join(datadir, 'xtrabackup-apply-logs.log') with open(log_file, 'w+') as log_handle: log.info(cmd) apply_logs = subprocess.Popen(cmd.split(), stderr=log_handle) if apply_logs.wait() != 0: raise Exception('Fatal error: innobackupex apply-logs did not ' 'return return 0') log_handle.seek(0) log_data = log_handle.readlines() if INNOBACKUP_OK not in log_data[-1]: msg = ('Fatal error: innobackupex apply-log did not end with ' '"{}"'.format(INNOBACKUP_OK)) raise Exception(msg)
def get_memory_usage(self): try: return psutil.phymem_usage().percent except RuntimeError: return 0.0 except ValueError: return 0.0
def used_memory(pl, steps=5, circle_glyph='●', memory_glyph='🔲'): try: memory = psutil.virtual_memory().percent except: memory = psutil.phymem_usage().percent ret = [] denom = int(steps) numer = int(denom * memory / 100) ret.append({ 'contents': memory_glyph + ' ', 'draw_soft_divider': False, 'divider_highlight_group': 'background:divider', 'highlight_groups': ['used_memory'], 'gradient_level': 99, }) ret.append({ 'contents': circle_glyph * numer, 'draw_soft_divider': False, 'highlight_groups': ['used_memory'], 'gradient_level': 99, }) ret.append({ 'contents': circle_glyph * (denom - numer), 'draw_soft_divider': False, 'highlight_groups': ['used_memory'], 'gradient_level': 1, }) return ret
def heartbeat(self): while not self.drain: with self.lock: heartbeat = { 'shard': self.shard, 'entries': len(self.entries), 'errors': len(self.errors), 'results': len(self.results), 'resources': { "cpu_usage.percent": psutil.cpu_percent(), "mem_usage.percent": psutil.phymem_usage().percent, "disk_usage.percent": psutil.disk_usage('/').percent, "disk_usage.free": psutil.disk_usage('/').free, }, 'services': self._service_info(), 'queues': { 'max_inflight': self.high, 'control': self.control_queue.length(), 'ingest': q.length(self.ingest_queue), 'response': q.length(self.response_queue), }, } heartbeat['hostinfo'] = self.hostinfo msg = message.Message(to="*", sender='dispatcher', mtype=message.MT_DISPHEARTBEAT, body=heartbeat) CommsQueue('status').publish(msg.as_dict()) time.sleep(1)
def drop(): drop = { "elems": { "cpu_temp": { "type": "float", "value": cpu_temp() }, "ram": { "type": "map", "value": {} }, "disk": { "type": "map", "value": {} } } } ram = psutil.phymem_usage() drop['elems']['ram']['value']['total'] = ram.total / 2 ** 20 drop['elems']['ram']['value']['used'] = ram.used / 2 ** 20 drop['elems']['ram']['value']['free'] = ram.free / 2 ** 20 disk = psutil.disk_usage('/') drop['elems']['disk']['value']['total'] = disk.total / 2 ** 20 drop['elems']['disk']['value']['used'] = disk.used / 2 ** 20 drop['elems']['disk']['value']['free'] = disk.free / 2 ** 20 return drop
def report_mem(prev_values=None, msg=None): """ Show status of phy/virt mem, and if given previous values, report differences requires the psutil module - avail in apt-get, but I used pip """ if msg is None: msg = "" else: msg += ": " if type(prev_values) == type(""): # catch error in prev_values msg += prev_values prev_values = None print("need msg= in call to report_mem") # available in linux is free + buffers + cached if hasattr(psutil, "swap_memory"): # new version (0.7+) pm = psutil.virtual_memory().available # was psutil.avail_phymem() vm = psutil.swap_memory().free # was psutil.avail_virtmem() else: pm = psutil.phymem_usage().free # was .avail_phymem() vm = psutil.virtmem_usage().free # avail_virtmem() tim = seconds() print("{msg}{pm:.3g} GB phys mem, {vm:.3g} GB virt mem avail".format(msg=msg, pm=pm / 1e9, vm=vm / 1e9)), if prev_values is None: print() else: print( "- dt={dt:.2g}s, used {pm:.2g} GB phys, {vm:.2g} GB virt".format( pm=(prev_values[0] - pm) / 1e9, vm=(prev_values[1] - vm) / 1e9, dt=tim - prev_values[2] ) ) return (pm, vm, tim)
def get_system_information(self): mem = psutil.phymem_usage() cpu_frequency = 0 try: cpuinfo = fileinput.input(files='/proc/cpuinfo') for line in cpuinfo: if 'cpu MHz' in line: str_token = line.split(':') cpu_frequency = float(str_token[1].strip()) break cpuinfo.close() except Exception as e: logger.exception(e) disk = psutil.disk_usage( config.Configurator.settings["nokkhum.processor.record_path"]) system_information = { 'name': platform.node(), 'system': platform.system(), 'machine': platform.machine(), 'cpu_count': multiprocessing.cpu_count(), 'cpu_frequency': cpu_frequency, 'total_memory': mem.total, 'total_disk': disk.total, 'ip': self.ip, } return system_information
def _get_sys_mem_info(self): phymem_info = psutil.phymem_usage() sys_mem_info = SysMemInfo() sys_mem_info.total = phymem_info[0] / 1024 sys_mem_info.used = phymem_info[1] / 1024 sys_mem_info.free = phymem_info[2] / 1024 return sys_mem_info
def prepare_memory(self): physmem = psutil.phymem_usage() vmem = psutil.virtual_memory() swap = psutil.virtmem_usage() return { 'virtual': { 'total': vmem.total, 'used': vmem.used, 'available': vmem.available, 'percent': vmem.percent, 'free': vmem.free, 'active': vmem.active, 'inactive': vmem.inactive, 'wired': vmem.wired, }, 'physical': { 'total': physmem.total, 'used': physmem.used, 'free': physmem.free, 'percent': physmem.percent, }, 'swap': { 'total': swap.total, 'used': swap.used, 'free': swap.free, 'percent': swap.percent, }, }
def display_memory(self, sender, arguments): ''' Display the total, used and free memory values for the current system ''' usage = psutil.phymem_usage() return self.message(sender, "Total: %s | Used: %s | Free: %s | Percent Free: %s"%(byte_format(usage.total), byte_format(usage.used), byte_format(usage.free), usage.percent))
def get_memory(self): # psutil returns a long, so we force it to an int if psutil.version_info[0] == 1: total = int(psutil.TOTAL_PHYMEM) elif psutil.version_info[0] == 2: total = int(psutil.phymem_usage().total) try: out, _e = utils.execute("dmidecode --type memory | grep Size", shell=True) except (processutils.ProcessExecutionError, OSError) as e: LOG.warning("Cannot get real physical memory size: %s", e) physical = None else: physical = 0 for line in out.strip().split('\n'): line = line.strip() if not line: continue try: value = line.split(None, 1)[1].strip() physical += int(UNIT_CONVERTER(value).to_base_units()) except Exception as exc: LOG.error('Cannot parse size expression %s: %s', line, exc) if not physical: LOG.warning( 'failed to get real physical RAM, dmidecode ' 'returned %s', out) return Memory(total=total, physical_mb=physical)
def cpu_stats(self): cfg_process = psutil.Process(os.getpid()) while True: # collect Vmsizes self._ip_change = 0 self._build_change = 0 rss = cfg_process.get_memory_info().rss if (self._rss != rss): self._rss = rss vms = cfg_process.get_memory_info().vms if (self._vms != vms): self._vms = vms pvms = vms if (pvms > self._pvms): self._pvms = pvms if self._sysinfo: # collect CPU Load avg load_avg = os.getloadavg() if (load_avg != self._load_avg): self._load_avg = load_avg # collect systemmeory info phymem_usage = psutil.phymem_usage() if (phymem_usage != self._phymem_usage): self._phymem_usage = phymem_usage phymem_buffers = psutil.phymem_buffers() if (phymem_buffers != self._phymem_buffers): self._phymem_buffers = phymem_buffers if (self._new_ip != self._curr_ip): self._new_ip = self.get_config_node_ip() self._ip_change = 1 # Retrieve build_info from package/rpm and cache it if self._curr_build_info is None: command = "contrail-version contrail-config | grep 'contrail-config'" version = os.popen(command).read() _, rpm_version, build_num = version.split() self._new_build_info = build_info + '"build-id" : "' + \ rpm_version + '", "build-number" : "' + \ build_num + '"}]}' if (self._new_build_info != self._curr_build_info): self._curr_build_info = self._new_build_info self._build_change = 1 num_cpus = psutil.NUM_CPUS if (num_cpus != self._num_cpus): self._num_cpus = num_cpus cpu_percent = cfg_process.get_cpu_percent(interval=0.1) cpu_share = cpu_percent / num_cpus self._cpu_share = cpu_share self._send_cpustats() gevent.sleep(self._time_interval)
def get_free_memory(self): try: return 100.0 - psutil.phymem_usage().percent except RuntimeError: return 0.0 except ValueError: return 0.0
def _next_line_builtin(self, args): if hasattr(psutil, 'virtual_memory'): mem = psutil.virtual_memory() else: mem = psutil.phymem_usage() # Deprecated in psutil 0.3.0 and 0.6.0 return "Memory usage %s%% |Memusage=%s%%;" % (mem.percent, mem.percent)
def get_memory(self): # psutil returns a long, so we force it to an int if psutil.version_info[0] == 1: total = int(psutil.TOTAL_PHYMEM) elif psutil.version_info[0] == 2: total = int(psutil.phymem_usage().total) try: out, _e = utils.execute("dmidecode --type memory | grep Size", shell=True) except (processutils.ProcessExecutionError, OSError) as e: LOG.warning("Cannot get real physical memory size: %s", e) physical = None else: physical = 0 for line in out.strip().split('\n'): line = line.strip() if not line: continue try: value = line.split(None, 1)[1].strip() physical += int(UNIT_CONVERTER(value).to_base_units()) except Exception as exc: LOG.error('Cannot parse size expression %s: %s', line, exc) if not physical: LOG.warning('failed to get real physical RAM, dmidecode ' 'returned %s', out) return Memory(total=total, physical_mb=physical)
def get_memory_status(self): """ Method called every x times, definied by Gtk.timeout_add method This get memory information using psutil library and show in indicator. """ memory_status = psutil.phymem_usage() self.menu_info_used.set_label( "Memory used: %dMB / %dMB (%.1f%%)" % (memory_status.used / self.MEGABYTE_DIVISOR, self.total_memory_available, memory_status.percent)) round_percent_memory_available = int(round(memory_status.percent, -1)) self.mem_indicator.set_icon("%d" % round_percent_memory_available) # Attention status if memory used is above 90 if round_percent_memory_available >= 90 and not self.attention: self.mem_indicator.set_attention_icon("100") self.mem_indicator.set_status( appindicator.IndicatorStatus.ATTENTION) self.attention = True elif round_percent_memory_available >= 90 and self.attention: self.mem_indicator.set_status(appindicator.IndicatorStatus.ACTIVE) self.attention = False # Set label if show_label is True if self.show_label: self.mem_indicator.set_label("%.1f%%" % memory_status.percent, "100%") # Return True for Gtk.timeout loop control return True
def get_ram_usage(): ram = psutil.phymem_usage() ram_total = ram.total / 2**20 # MiB. ram_used = ram.used / 2**20 ram_free = ram.free / 2**20 ram_percent_used = ram.percent return (ram_total, ram_used, ram_free, ram_percent_used)
def psutil_phymem_usage(): """Return physical memory usage (float) Requires the cross-platform psutil (>=v0.3) library (http://code.google.com/p/psutil/)""" import psutil return psutil.phymem_usage().percent
def __init__(self, config): Reporter.__init__(self, config) self.service = "Memory Report" self.list = CircularList(self.config.send_rate) self.maximum = psutil.phymem_usage().total / (1024 * 1024) self.warning = (self.config.memory_warning * self.maximum) / 100 self.critical = (self.config.memory_critical * self.maximum) / 100
def lookup(self, *name): cpus = 0 # psutil is unstable on how to get the number of # cpus, different versions call it differently if hasattr(psutil, 'cpu_count'): cpus = psutil.cpu_count() elif hasattr(psutil, 'NUM_CPUS'): cpus = psutil.NUM_CPUS elif hasattr(psutil, '_psplatform'): for method_name in ['_get_num_cpus', 'get_num_cpus']: method = getattr(psutil._psplatform, method_name, None) if method is not None: cpus = method() break self.host_info['cpus'] = cpus if hasattr(psutil, 'phymem_usage'): self.host_info['memory'] = psutil.phymem_usage().total elif hasattr(psutil, 'virtual_memory'): self.host_info['memory'] = psutil.virtual_memory().total return self.host_info
def getConsumerCount(): """ 拿到系统当前CPU和内存使用情况 根据配置计算当前能够启动多少个consumer 具体算法是: CPU 利用率50以上,每一个percent可以分配10(经验值)个进程,直到50停止分配 且内存使用率也在50以上,鉴于benstalk使用CPU较高,所以以CPU为主 另外,在CPU范围内的最大值会和配置文件的最大值做交集,以二者最小值为准 """ #读取conf配置 cf = ConfigParser.ConfigParser() cf.read( CONF_NAME ) consumerConfig = cf.options("consumer") max_consumer = cf.get("consumer","max_consumer") min_consumer = cf.get("consumer","min_consumer") cpu_idle = cf.get("consumer","cpu_idle") mem_idle = cf.get("consumer","mem_idle") cpu = psutil.cpu_percent(1) phymem = psutil.phymem_usage() buffers = getattr( psutil, 'phymem_buffers', lambda: 0)() cached = getattr( psutil , 'cached_phymem', lambda: 0)() used = phymem.total - (phymem.free + buffers + cached) #算法待完成 #FIXME return max_consumer
def dump_tolog(cCpu, cSensor, cData): logFile = open("data/ForensicLog.txt", 'a+') cPhyMem = psutil.phymem_usage() cVirMem = psutil.virtmem_usage() cNetTraffic = psutil.network_io_counters(pernic=True) cPartitionInfo = psutil.disk_partitions() cActiveProcess = '' logFile.write('*===================================================*' + '\n') logFile.write('|Forensic Activity Logged at: ' + datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '\n') logFile.write('*===================================================*' + '\n') logFile.write('SYSCOND: MALFUNCTION | CPU LOAD: ' + cCpu + '% | SENSOR: ' + cSensor + ' | READ: ' + cData + '\n') logFile.write('PHYSICAL MEMORY:\n' + str(cPhyMem) + '\n\n') logFile.write('VIRTUAL MEMORY :\n' + str(cVirMem) + '\n\n') logFile.write('NETWORK STATUS :\n' + str(cNetTraffic) + '\n\n') logFile.write('MOUNTED DISKS :\n' + str(cPartitionInfo) + '\n\n') logFile.write('PROCESS LIST :\n') for proc in psutil.process_iter(): logFile.write(str(proc) + ',') logFile.write('\n') logFile.close() return
def __init__(self, interval, history_len): """ :param interval: Collect statistics according to this interval. :param history_len: Use this many to compute avg/max statistics. """ self.interval = interval self.history_len = history_len try: import psutil # @UnresolvedImport @Reimport except: self._available = False else: self._available = True self.cpu = Collect('cpu', lambda: psutil.cpu_percent(interval=0), interval, history_len) try: # new in 0.8 psutil.virtual_memory().percent get_mem = lambda: psutil.virtual_memory().percent except: get_mem = lambda: psutil.phymem_usage().percent self.mem = Collect('mem', get_mem, interval, history_len) try: # new in 0.8 psutil.swap_memory().percent get_mem = lambda: psutil.swap_memory().percent except: get_mem = lambda: psutil.virtmem_usage().percent self.swap_mem = Collect('swap', get_mem, interval, history_len)
def store_integrated_experiences(parallel=True): episodes = get_episodes() auth = fb.FirebaseAuthentication(FIREBASE_KEY, ADMIN_EMAIL, ADMIN_PASSWORD) fire = fb.FirebaseApplication(FIREBASE_URL, auth) i = 0 mem_pct = psutil.phymem_usage().percent if parallel: process_pool = Pool(processes=10) while i < len(episodes) and mem_pct < 100: episode = episodes[i] episode_directory, episode_number = episode.key.split('/') print str(i) + ' of ' + str(len(episodes)) + ' #' + episode_number pre_dir = DQN_ROOT + '/data/s3/episodes/' + episode_directory post_dir = INTEGRATE_DIR + episode_directory if not os.path.exists(pre_dir): os.makedirs(pre_dir) pre_filename = pre_dir + episode_number post_filename = post_dir + '_' + episode_number + '.snappy' if not os.path.exists(pre_filename): episode.get_contents_to_filename(pre_filename) if os.path.exists(post_filename): print post_filename + ' already loaded' else: if parallel: save_snappy_file_pll(episode_directory, episode_number, fire, post_filename, pre_filename, process_pool) else: save_snappy_file(episode_directory, episode_number, fire, post_filename, pre_filename) i += 1 if parallel: process_pool.close() process_pool.join()
def runinsert(): cpu_usage = psutil.cpu_percent() mem_usage = psutil.phymem_usage().percent disk_usage = psutil.disk_usage('/').percent virtual = psutil.virtmem_usage().percent network = simplejson.dumps(psutil.network_io_counters(True)) conn = sqlite3.connect('stats.db') cur = conn.cursor() cur.execute( "DELETE FROM stats WHERE timestamp < datetime('now','localtime','-7 days')" ) # cur.execute("DELETE FROM stats WHERE timestamp < datetime('now', 'localtime','-1 hour')") conn.commit() cur.execute( "INSERT INTO stats (cpu, memory, disk, virtual, network) VALUES (?, ?, ?, ?, ? )", (cpu_usage, mem_usage, disk_usage, virtual, network)) # DUBUG # cur.execute("SELECT * FROM stats") # print(cur.fetchall()) conn.commit() conn.close()
def _make_general_stats(self): now = time.time() stats = [] stats.append("%f" % (now - self._start)) # elapsed time stats.append("%f" % psutil.cpu_percent()) # total cpu stats.append("%f" % psutil.phymem_usage().percent) # total memory return ' '.join(stats)
def get_mem_swap(self,): """ Get memory and swap usage """ try: # psutil >= 0.6.0 phymem = psutil.virtual_memory() buffers = psutil.virtual_memory().buffers cached = psutil.virtual_memory().cached vmem = psutil.swap_memory() except AttributeError: # psutil > 0.4.0 and < 0.6.0 phymem = psutil.phymem_usage() buffers = getattr(psutil, 'phymem_buffers', lambda: 0)() cached = getattr(psutil, 'cached_phymem', lambda: 0)() vmem = psutil.virtmem_usage() mem_used = phymem.total - (phymem.free + buffers + cached) return ( phymem.percent, mem_used, phymem.total, vmem.percent, vmem.used, vmem.total)
def memory_usage(): """ Get used and total memory usage. :returns: Used and total memory in bytes :rtype: tuple """ # Handle optional psutil support try: import psutil psutil_version = version_tuple(psutil.__version__) if psutil_version < (0, 6, 0): usage = psutil.phymem_usage() used = usage.used else: usage = psutil.virtual_memory() used = usage.total - usage.available return used, usage.total except ImportError: pass return None, None
def getMEM(): # devuelve un arreglo con la memoria fisica total, la memoria fisica utilizada, la memoria fisica disponible y el % de utilizacion try: getMEM = psutil.phymem_usage() miMEM = str(getMEM[0]) + "," + str(getMEM[1]) + "," + str(getMEM[2]) + "," + str(getMEM[3]) return miMEM except: return ""
def info_loop(max_ctr = 100): exit_loop = False internal_ctr = 0 while not exit_loop: q = psutil.cpu_percent(interval=1) #q = q/10 cpuload = '%.0f'%(q) cpuload = 'cpu load: ' + cpuload + '%' print cpuload #ser.write(cpuload) q = psutil.phymem_usage() # mem = '%.0f'%(q.percent/10) mem = '%.0f'%(q.percent) mem = 'memory: ' + mem + '%' print mem #ser.write(mem) q = psutil.virtmem_usage() virtmem = '%.0f'%(q.percent/10) virtmem = 'virtmem: '+virtmem + '%' print virtmem time.sleep(0.5) internal_ctr = internal_ctr + 1 if (internal_ctr > max_ctr): self.exit_loop = True print '\n'
def build_getters(job_id, counts_taxonomy, calc): """ :param job_id: ID of the current risk job :param counts_taxonomy: a sorted list of pairs (counts, taxonomy) for each bunch of assets :param calc: :class:`openquake.engine.calculators.risk.base.RiskCalculator` instance """ for counts, taxonomy in counts_taxonomy: logs.LOG.info('taxonomy=%s, assets=%d', taxonomy, counts) # building the GetterBuilder with calc.monitor("associating asset->site"): builder = hazard_getters.GetterBuilder( taxonomy, calc.rc, calc.eps_sampling) # estimating the needed memory haz_outs = calc.rc.hazard_outputs() nbytes = builder.calc_nbytes(haz_outs) if nbytes: # TODO: the estimate should be revised by taking into account # the number of realizations estimate_mb = nbytes / 1024 / 1024 * 3 phymem = psutil.phymem_usage() available_memory = (1 - phymem.percent / 100) * phymem.total available_mb = available_memory / 1024 / 1024 if nbytes * 3 > available_memory: raise MemoryError(MEMORY_ERROR % (estimate_mb, available_mb)) # initializing the epsilons builder.init_epsilons(haz_outs) # building the tasks task_no = 0 name = calc.core_calc_task.__name__ + '[%s]' % taxonomy otm = tasks.OqTaskManager(calc.core_calc_task, logs.LOG.progress, name) for offset in range(0, counts, BLOCK_SIZE): with calc.monitor("getting asset chunks"): assets = models.ExposureData.objects.get_asset_chunk( calc.rc, taxonomy, offset, BLOCK_SIZE) with calc.monitor("building getters"): try: getters = builder.make_getters( calc.getter_class, haz_outs, assets) except hazard_getters.AssetSiteAssociationError as err: # TODO: add a test for this corner case # https://bugs.launchpad.net/oq-engine/+bug/1317796 logs.LOG.warn('Taxonomy %s: %s', taxonomy, err) continue # submitting task task_no += 1 logs.LOG.info('Built task #%d for taxonomy %s', task_no, taxonomy) risk_model = calc.risk_models[taxonomy] otm.submit(job_id, risk_model, getters, calc.outputdict, calc.calculator_parameters) return otm
def onSystemTimerFire(self, e): # update CPU self.cpu_usage = psutil.cpu_percent() self.cpu_gauge.SetValue(self.cpu_usage) self.cpu_label.SetLabel("CPU Usage: %d%%" % self.cpu_usage) # update RAM self.ram_usage = psutil.phymem_usage().percent self.ram_gauge.SetValue(self.ram_usage) self.ram_label.SetLabel("RAM Usage: %d%%" % self.ram_usage) # update battery remaining_capacity_re = re.compile(r'^remaining capacity:\W+(\d+) mAh') full_capacity_re = re.compile(r'^design capacity:\W+(\d+) mAh') b_state_file = open('/proc/acpi/battery/BAT1/state', 'r') b_state = b_state_file.readlines() b_state_file.close() for line in b_state: m = remaining_capacity_re.match(line) if m: self.main_battery_remaining_capacity = float(m.group(1)) b_info_file = open('/proc/acpi/battery/BAT1/info', 'r') b_info = b_info_file.readlines() b_info_file.close() for line in b_info: m = full_capacity_re.match(line) if m: self.main_battery_full_capacity = float(m.group(1)) self.main_battery_remaining = (100 * self.main_battery_remaining_capacity/self.main_battery_full_capacity) self.main_battery_label.SetLabel("Main Battery Remaining: %d%%" % self.main_battery_remaining)
def lookup(self, *name): cpus = psutil.NUM_CPUS # psutil is unstable on how to get the number of # cpus, different versions call it differently if hasattr(psutil, 'cpu_count'): cpus = psutil.cpu_count() elif hasattr(psutil, 'NUM_CPUS'): cpus = psutil.NUM_CPUS elif hasattr(psutil, '_psplatform'): for method_name in ['_get_num_cpus', 'get_num_cpus']: method = getattr(psutil._psplatform, method_name, None) if method is not None: cpus = method() break self.host_info['cpus'] = cpus if hasattr(psutil, 'phymem_usage'): self.host_info['memory'] = psutil.phymem_usage().total elif hasattr(psutil, 'virtual_memory'): self.host_info['memory'] = psutil.virtual_memory().total return self.host_info
def stats(): # OS identification yield 'osname', osname() # Uptime yield 'uptime', time.time() - psutil.get_boot_time() # CPU temperature try: temps = [] for line in check_output('sensors').split('\n'): m = re.match(r'^Core \d+:\s*\+(\d+\.\d+)', line) if m: temps.append(float(m.group(1))) assert len(temps) == psutil.NUM_CPUS yield 'temps', temps except: pass # CPU usage yield 'cpu_usage', round(psutil.cpu_percent(), 2) # Memory usage mem = psutil.phymem_usage() yield 'memory', (mem.used, mem.total) # Disk usage disk = psutil.disk_usage('/') yield 'disk', (disk.used, disk.total)
def _get_sys_mem_info(self): phymem_info = psutil.phymem_usage() sys_mem_info = SysMemInfo() sys_mem_info.total = phymem_info[0]/1024 sys_mem_info.used = phymem_info[1]/1024 sys_mem_info.free = phymem_info[2]/1024 return sys_mem_info
def cpustat(self): tags = "node:%s %s" % (self.nodename, self.tags) val = int(psutil.cpu_percent()) measurement = "cpu_perc" key = "%s.%s" % (self.nodename, measurement) self.measure(key, measurement, tags, val, aggrkey=measurement) val = int(psutil.avail_phymem() / 1024 / 1024) measurement = "mem_free_mb" key = "%s.%s" % (self.nodename, measurement) self.measure(key, measurement, tags, val, aggrkey=measurement) val = int(psutil.virtmem_usage()[3]) measurement = "mem_virt_perc" key = "%s.%s" % (self.nodename, measurement) self.measure(key, measurement, tags, val, aggrkey=measurement) val = int(psutil.phymem_usage()[2]) measurement = "mem_phy_perc" key = "%s.%s" % (self.nodename, measurement) self.measure(key, measurement, tags, val, aggrkey=measurement) res = psutil.cpu_times_percent() names = [ "cputimeperc_user", "cputimeperc_nice", "cputimeperc_system", "cputimeperc_idle", "cputimeperc_iowait", "cputimeperc_irq", "cputimeperc_softirq", "steal", "guest", "guest_nice" ] for i in range(len(names)): if names[i].startswith("cputime"): val = int(res[i]) measurement = names[i] key = "%s.%s" % (self.nodename, measurement) self.measure(key, measurement, tags, val, aggrkey=measurement)
def get_defaults(): (system, node, release, version, machine, processor) = platform.uname() if sys.platform.startswith('linux'): try: from numpy.distutils import cpuinfo except ImportError: cpu = '' else: info = cpuinfo.cpuinfo().info cpu = "{0} ({1} cores)".format( info[0]['model name'], len(info)) else: # TODO: Get this on a Mac cpu = '' try: import psutil except ImportError: ram = '' else: ram = util.human_file_size(psutil.phymem_usage().total) return { 'machine': node, 'os': "{0} {1}".format(system, release), 'arch': platform.machine(), 'cpu': cpu, 'ram': ram }