def extend(self, seq): if isinstance(seq, Iterator): start_available_memory = psutil.avail_phymem() # Two bounds to avoid hysteresis target_low = 0.4 * start_available_memory target_high = 0.6 * start_available_memory # Pull chunksize from last run chunksize = global_chunksize[0] empty = False while not empty: chunk = tuple(take(chunksize, seq)) self.extend_chunk(chunk) # tweak chunksize if necessary available_memory = psutil.avail_phymem() if len(chunk) == chunksize: if available_memory > target_high: chunksize = int(chunksize * 1.6) elif available_memory < target_low: chunksize = int(chunksize / 1.6) empty, seq = isempty(seq) global_chunksize[0] = chunksize else: self.extend_chunk(seq)
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 mem(self, widget, data=None) : print ' \n DETAILS OF PHYSICAL AND VIRTUAL MEMORY\n ' print 'Total physical memory (in bytes) - ' print psutil.TOTAL_PHYMEM print 'Available physical memory (in bytes) - ' print psutil.avail_phymem() print 'Used physical memory (in bytes) - ' print psutil.used_phymem() print 'Total virtual memory (in bytes) - ' print psutil.total_virtmem() print 'Available virtual memory (in bytes) - ' print psutil.avail_virtmem() print 'Used virtual memory (in bytes) - ' print psutil.used_virtmem() print 'Total cached memory (in bytes) - ' print psutil.cached_phymem()
def main(): usedRAM = psutil.TOTAL_PHYMEM - psutil.avail_phymem() myRAM = usedRAM * 100 / psutil.TOTAL_PHYMEM myCPU = psutil.cpu_percent(interval = 2) print("CPU:", getStatusBar(myCPU)) print("RAM:", getStatusBar(myRAM))
def _computeMaximumBufferSize(nodes): """number of nodes. This actually depends on the system mcvine is running on. If we are running on one machine that has shared memory, we need to set here nodes to the number of mpi instances. If we are running on a cluster of nodes without shared memory, we should not need to divide the maximum buffer size by nodes. """ import psutil memsize = min(psutil.TOTAL_PHYMEM / 2, (psutil.avail_phymem() + psutil.avail_virtmem()) * 0.7) memsize = int(memsize) from mcni.neutron_storage.idfneutron import ndblsperneutron bytesperdble = 8 minsize = MINIMUM_BUFFER_SIZE n = int(memsize/nodes/ndblsperneutron/bytesperdble/minsize) \ *MINIMUM_BUFFER_SIZE if n < minsize: raise RuntimeError, "Not enough memory" return n
def update_memory(self, name, config): self.g[name] = ["memory_used","memory_available"] if("virtual_memory" in dir(psutil)): # new style function call data = psutil.virtual_memory() self.d["memory_used"] = data.used self.d["memory_available"] = data.available else: self.d["memory_used"] = psutil.used_phymem() self.d["memory_available"] = psutil.avail_phymem() self.c[name] =[ "graph_title Memory", "graph_order memory_used memory_available", "graph_category system", "graph_period second", "memory_used.label in use", "memory_used.min 0", "memory_used.type GAUGE", "memory_used.draw AREA", "memory_available.label available", "memory_available.type GAUGE", "memory_available.draw STACK", "."]
def _computeMaximumBufferSize(nodes): """number of nodes. This actually depends on the system mcvine is running on. If we are running on one machine that has shared memory, we need to set here nodes to the number of mpi instances. If we are running on a cluster of nodes without shared memory, we should not need to divide the maximum buffer size by nodes. """ import psutil memsize = min(psutil.TOTAL_PHYMEM/2, (psutil.avail_phymem() + psutil.avail_virtmem())*0.7) memsize = int(memsize) from mcni.neutron_storage.idfneutron import ndblsperneutron bytesperdble = 8 minsize = MINIMUM_BUFFER_SIZE n = int(memsize/nodes/ndblsperneutron/bytesperdble/minsize) \ *MINIMUM_BUFFER_SIZE if n<minsize: raise RuntimeError, "Not enough memory" return n
def getPhyMem(): """取本机的物理内存,可用物理内存,剩余可用物理内存,单位K """ phymem = int(psutil.TOTAL_PHYMEM)/1024 availphymem = int(psutil.avail_phymem())/1024 usedphymem = int(psutil.used_phymem())/1024 return phymem,availphymem,usedphymem
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 doit(): cpu_pc = psutil.cpu_percent() mem_avail_mb = psutil.avail_phymem() / 1000000 cpu_temp = round( int(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1e3, 1) params = urllib.urlencode({ 'field1': cpu_pc, 'field2': mem_avail_mb, 'field3': cpu_temp, 'key': '5KZO92TTCY5OEB8K' }) headers = { "Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain" } conn = httplib.HTTPConnection("api.thingspeak.com:80") try: conn.request("POST", "/update", params, headers) response = conn.getresponse() print cpu_pc print mem_avail_mb print cpu_temp print strftime("%a, %d %b %Y %H:%M:%S", localtime()) print response.status, response.reason data = response.read() conn.close() except: print "connection failed"
def getMemoryInfo(): """ return a tuple(total_phymen,avi_phymen.used_phymen) KB .get System Memory Inforation""" if psutilUsed==False: return None totalPhymen=psutil.TOTAL_PHYMEM aviPhymen=psutil.avail_phymem() usedPhymen=psutil.used_phymem() return (totalPhymen/1024,aviPhymen/1024,usedPhymen/1024)
def getStat(self): return {'memTotal': psutil.TOTAL_PHYMEM, 'memFree': psutil.avail_phymem(), 'swapTotal': psutil.total_virtmem(), 'swapFree': psutil.avail_virtmem(), 'buffers': psutil.phymem_buffers(), 'cached': psutil.cached_phymem()}
def doit(): cpu_pc = psutil.cpu_percent() mem_avail_mb = psutil.avail_phymem() / 1000000 params = urllib.urlencode({ 'field1': cpu_pc, 'field2': mem_avail_mb, 'key': 'NJQGS40ZVNGNK36S' }) headers = { "Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain" } conn = httplib.HTTPConnection("api.thingspeak.com:80") try: conn.request("POST", "/update", params, headers) response = conn.getresponse() print cpu_pc print mem_avail_mb print strftime("%a, %d %b %Y %H:%M:%S", localtime()) print response.status, response.reason data = response.read() conn.close() except: print "connection failed"
def getPhyMem(): """取本机的物理内存,可用物理内存,剩余可用物理内存,单位K """ phymem = int(psutil.TOTAL_PHYMEM) / 1024 availphymem = int(psutil.avail_phymem()) / 1024 usedphymem = int(psutil.used_phymem()) / 1024 return phymem, availphymem, usedphymem
def getData(): temp = round(int(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1e3,1) perc = psutil.cpu_percent() memAvail = round(psutil.avail_phymem()/1000000,1) diskUsage = psutil.disk_usage('/').percent j = {'cpu_temp': temp, 'cpu_perc': perc, 'mem_avail': memAvail, 'disk_usage': diskUsage} return json.dumps(j,indent=4, separators=(',', ': '))
def memInfo(): logging.debug("Mem info") logging.debug(psutil.virtual_memory()) logging.debug(psutil.swap_memory()) logging.debug(psutil.phymem_usage()) logging.debug(psutil.avail_virtmem()) logging.debug(psutil.avail_phymem())
def main(expr): vars = {'free': psutil.avail_virtmem(), 'available': psutil.avail_virtmem(), 'pfree': psutil.avail_phymem(), 'buffers': psutil.phymem_buffers(), 'total': psutil.total_virtmem(), 'cached': psutil.cached_phymem(), } return eval(expr, {}, vars)
def generate(self, **kwargs): result = {'mem_phys_total': psutil.TOTAL_PHYMEM, 'mem_phys_avail': psutil.avail_phymem(), 'mem_phys_used': psutil.used_phymem(), 'mem_virt_total': psutil.total_virtmem(), 'mem_virt_avail': psutil.used_virtmem(), 'cpu_cores': self._get_total_core(), } return result
def generate(self, **kwargs): result = { 'mem_phys_total': psutil.TOTAL_PHYMEM, 'mem_phys_avail': psutil.avail_phymem(), 'mem_phys_used': psutil.used_phymem(), 'mem_virt_total': psutil.total_virtmem(), 'mem_virt_avail': psutil.avail_virtmem(), 'mem_virt_used': psutil.used_virtmem(), 'cpu_cores': self._get_total_cores(), } return result
def memorypercent(self): import psutil from distutils.version import StrictVersion if StrictVersion(psutil.__version__) < '0.4': # deprecated total_mem = psutil.avail_phymem() + \ psutil.cached_phymem() + psutil.phymem_buffers() return psutil.used_phymem() * 100 / total_mem else: return int(psutil.phymem_usage().percent)
def info(): ps = get_ps() print "PID,\tPPID,\tCPU,\tRSS - \tVMS" total = 0 for p in ps: rss, vms = p.get_memory_info() total += rss print "%s\t%s\t%0.2f%%\t%s - \t%s" % (p.pid, p.ppid, p.get_cpu_percent(), mb(rss), mb(vms)) print 'total watched RSS: %d' % mb(total) used, all, avail = mb(psutil.used_phymem()), mb(psutil.TOTAL_PHYMEM), mb(psutil.avail_phymem()) print 'Used memory %dMB out of %dMB' % (used, all) print 'Available memory %dMB' % avail
def xOnTimer(self, event): # ------------------------------------------------------------------------ if self.posTick: self.SetStatusText( datetime.now().strftime("%H:%M:%S"), self.posTick) if self.posCpu and 0 == self.counter % 1: self.SetStatusText( "CPU: %s%%" % psutil.cpu_percent(0), self.posCpu) if self.posMem and 0 == self.counter % 1: self.SetStatusText( "Free: %.1fMB" % (psutil.avail_phymem()*1e-6,), self.posMem) self.counter += 1
def foo(self): perc = psutil.cpu_percent(interval = None, percpu = False) mem = psutil.avail_phymem() self.gaz_arr, self.gaz = self.usred(self.gaz_arr, perc) d = 4; aa = int(round(self.gaz, 0)) / d; aa *= d self.gaz = aa self.tacho_arr, self.tacho = self.usred(self.tacho_arr, self.gaz) self.tacho_angle = (MAX_ANGLE / 100.0) * self.tacho self.gaz_angle = (MAX_ANGLE / 100.0) * self.gaz self.mem_angle = MAX_ANGLE * (1.0 * mem / self.MEM) self.update() pass
def doit(): cpu_pc = psutil.cpu_percent() mem_avail_mb = psutil.avail_phymem()/1000000 print cpu_pc print mem_avail_mb params = urllib.urlencode({'field1': cpu_pc, 'field2': mem_avail_mb,'key':'662EQ8O35OEBQ547'}) headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"} conn = httplib.HTTPConnection("api.thingspeak.com:80") conn.request("POST", "/update", params, headers) response = conn.getresponse() print response.status, response.reason data = response.read() conn.close()
def doit(channel): cpu_pc = psutil.cpu_percent() mem_avail_mb = psutil.avail_phymem()/1000000 try: response = channel.update([cpu_pc, mem_avail_mb]) print cpu_pc print mem_avail_mb print strftime("%a, %d %b %Y %H:%M:%S", localtime()) print response.status, response.reason data = response.read() except: print "connection failed"
def doit(channel): cpu_pc = psutil.cpu_percent() mem_avail_mb = psutil.avail_phymem() / 1000000 try: response = channel.update([cpu_pc, mem_avail_mb]) print cpu_pc print mem_avail_mb print strftime("%a, %d %b %Y %H:%M:%S", localtime()) print response.status, response.reason data = response.read() except: print "connection failed"
def processReportSystemResourcesRequest(self, packet): def getDiskFree(): import ctypes, platform, os if platform.system() == 'Windows': free_bytes = ctypes.c_ulonglong(0) ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p('C:\\'), None, None, ctypes.pointer(free_bytes)) return free_bytes.value else: return os.statvfs('/').f_bfree response = ReportSystemResourcesResponse() import platform if packet.reportNode: response.clientVersion = CLIENT_VERSION response.system, response.node, response.release, response.version, response.machine, response.processor = platform.uname() response.python_version = platform.python_version() if packet.reportResources: import psutil response.memoryFree = psutil.avail_phymem() response.totalMemory = psutil.TOTAL_PHYMEM response.cpuUsage = psutil.cpu_percent() response.diskFree = getDiskFree() from time import time response.actualTime = int(time()); if packet.reportProcessList: import psutil for process in psutil.process_iter(): proc = response.processList.add() proc.pid = process.pid proc.name = process.name try: proc.exe = process.exe except: pass proc.userName = process.username #proc.cpuPercent = process.cpu_percent() proc.memoryPercent = process.get_memory_percent() self.sendResponse(response)
def __init__(self): self.memfree = int(psutil.avail_phymem()) / (1024 * 1024) self.memused = int(psutil.used_phymem()) / (1024 * 1024) self.memcached = int(psutil.cached_phymem()) / (1024 * 1024) self.memtotal = self.memfree + self.memused self.memload = psutil.phymem_usage()[3] self.load = os.getloadavg() self.uptime = ProcParser().getuptime() self.coresload = psutil.cpu_percent(0.7, True) self.time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) statvfs = os.statvfs('/') self.hddtotal = statvfs.f_frsize * statvfs.f_blocks / 1024 / 1024 # Size of filesystem in bytes self.hddfree = statvfs.f_frsize * statvfs.f_bfree / 1024 / 1024 # Actual number of free bytes self.hddused = self.hddtotal - self.hddfree
def on_global(self, msg=None): global_cpu = psutil.get_system_cpu_times() global_cpu['percent'] = psutil.cpu_percent() global_mem = { 'used_physical': psutil.used_phymem(), 'used_virtual': psutil.used_virtmem(), 'available_physical': psutil.avail_phymem(), 'available_virtual': psutil.avail_virtmem(), } result = { 'hostnome': socket.gethostname(), 'cpu': global_cpu, 'mem': global_mem, } return {'global': result}
def foo(self): perc = psutil.cpu_percent(interval=None, percpu=False) mem = psutil.avail_phymem() self.gaz_arr, self.gaz = self.usred(self.gaz_arr, perc) d = 4 aa = int(round(self.gaz, 0)) / d aa *= d self.gaz = aa self.tacho_arr, self.tacho = self.usred(self.tacho_arr, self.gaz) self.tacho_angle = (MAX_ANGLE / 100.0) * self.tacho self.gaz_angle = (MAX_ANGLE / 100.0) * self.gaz self.mem_angle = MAX_ANGLE * (1.0 * mem / self.MEM) self.update() pass
def getSystemStats(self): physMem = psutil.TOTAL_PHYMEM phyMemAvailable = psutil.avail_phymem() try: cpuPercent = psutil.cpu_percent(interval=1) except: cpuPercent = psutil.cpu_percent() server_is_running = self.minecraft.isRunning() data = { 'total_physical_memory':physMem, 'available_physical_memory':phyMemAvailable, 'cpu_usage':cpuPercent, 'server_is_running':server_is_running } #cherrypy.response.headers['Content-Type']= 'application/json' return json.dumps(data);
def convert_to_texture(self, imInfo): # Convert image to OpenGL texture glGenTextures(1, imInfo['i']) glBindTexture(GL_TEXTURE_2D, imInfo['i']) # 2d texture (x and y size) glPixelStorei(GL_UNPACK_ALIGNMENT, 1) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL) glTexImage2D(GL_TEXTURE_2D, 0, 3, imInfo['size'][0], imInfo['size'][1], 0, GL_RGBA, GL_UNSIGNED_BYTE, imInfo['img']) print str(imInfo['i']) + ': ' + os.path.basename( imInfo['filepath']) + ' loaded: ' + self.convert_bytes( psutil.avail_phymem()) + ' free'
def _main(): global available_memory 'Command line entry point.' allow_destructive = False try: if os.environ['DESTRUCTIVE_MODE']: allow_destructive = True except Exception: True hrdw = eval(open(sys.argv[1]).read(-1)) try: available_memory = psutil.virtual_memory().total except Exception: available_memory = psutil.avail_phymem() sys.stderr.write("Available memory before run = %s\n" % available_memory) mode = 'cpu,memory,storage' try: mode = sys.argv[2] except Exception: True if 'cpu-burn' in mode: cpu_perf(hrdw, 60, True) elif 'cpu' in mode: cpu_perf(hrdw) if 'memory-burn' in mode: mem_perf_burn(hrdw, 60) elif 'memory' in mode: mem_perf(hrdw) if 'storage-burn' in mode: storage_perf_burn(hrdw, allow_destructive, 30) elif 'storage' in mode: storage_perf(hrdw, allow_destructive) # Saving result to stdout but also to a filename based on the hw properties output_filename = get_output_filename(hrdw) sys.stderr.write("Saving results in %s\n" % output_filename) with open(output_filename, 'w') as state_file: pprint.pprint(hrdw, stream=state_file) pprint.pprint(hrdw)
def test_avail_phymem(self): # This test is not particularly accurate and may fail if the OS is # consuming memory for other applications. # We just want to test that the difference between psutil result # and sysctl's is not too high. _sum = sum((sysctl("sysctl vm.stats.vm.v_inactive_count"), sysctl("sysctl vm.stats.vm.v_cache_count"), sysctl("sysctl vm.stats.vm.v_free_count"))) _pagesize = sysctl("sysctl hw.pagesize") sysctl_avail_phymem = _sum * _pagesize psutil_avail_phymem = psutil.avail_phymem() difference = abs(psutil_avail_phymem - sysctl_avail_phymem) # On my system both sysctl and psutil report the same values. # Let's use a tollerance of 0.5 MB and consider the test as failed # if we go over it. if difference > (0.5 * 2**20): self.fail("sysctl=%s; psutil=%s; difference=%s;" % (sysctl_avail_phymem, psutil_avail_phymem, difference))
def doit(): cpu_pc = psutil.cpu_percent() mem_avail_mb = psutil.avail_phymem()/1000000 params = urllib.urlencode({'field1': cpu_pc, 'field2': mem_avail_mb,'key':'NJQGS40ZVNGNK36S'}) headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"} conn = httplib.HTTPConnection("api.thingspeak.com:80") try: conn.request("POST", "/update", params, headers) response = conn.getresponse() print cpu_pc print mem_avail_mb print strftime("%a, %d %b %Y %H:%M:%S", localtime()) print response.status, response.reason data = response.read() conn.close() except: print "connection failed"
def usedmemory(self): import psutil from distutils.version import StrictVersion # print sizes in human readable (MB) MB = 1024 * 1024 if StrictVersion(psutil.__version__) < '0.4': # deprecated total_mem = psutil.avail_phymem() + \ psutil.cached_phymem() + psutil.phymem_buffers() return "%d of %d" % (psutil.used_phymem() / MB, total_mem / MB) else: # I don't care about cached memory.. return "%d of %d" % \ ((psutil.phymem_usage().used - psutil.cached_phymem() - psutil.phymem_buffers()) / MB, psutil.phymem_usage().total / MB)
def test_avail_phymem(self): # This test is not particularly accurate and may fail if the OS is # consuming memory for other applications. # We just want to test that the difference between psutil result # and sysctl's is not too high. _sum = sum((sysctl("sysctl vm.stats.vm.v_inactive_count"), sysctl("sysctl vm.stats.vm.v_cache_count"), sysctl("sysctl vm.stats.vm.v_free_count") )) _pagesize = sysctl("sysctl hw.pagesize") sysctl_avail_phymem = _sum * _pagesize psutil_avail_phymem = psutil.avail_phymem() difference = abs(psutil_avail_phymem - sysctl_avail_phymem) # On my system both sysctl and psutil report the same values. # Let's use a tollerance of 0.5 MB and consider the test as failed # if we go over it. if difference > (0.5 * 2**20): self.fail("sysctl=%s; psutil=%s; difference=%s;" %( sysctl_avail_phymem, psutil_avail_phymem, difference))
def collect(self): return dict( cpu_times=psutil.cpu_times()._asdict(), mem=dict( total_phymem=psutil.TOTAL_PHYMEM, avail_phymem=psutil.avail_phymem(), avail_virtmem=psutil.avail_virtmem(), cached_phymem=psutil.cached_phymem(), phymem_buffers=psutil.phymem_buffers(), total_virtmem=psutil.total_virtmem(), used_phymem=psutil.used_phymem(), used_virtmem=psutil.used_virtmem(), ), processes=list(self._processes()), net=dict(ifaces=self._net_dev(), proto=self._net_proto()), io=self._io_stats(), fs=dict(self._fs_usage()), fh=self._file_handles(), )
def refresh(self): logging.debug("Host.refresh(): Refreshing all host entries") self.refreshVirtServiceSpecs() self.__machine_type=platform.machine() self.__network_name=platform.node() self.__processor=platform.processor() self.__python_version=platform.python_version() self.__linuxKernel_version=platform.release() #self.__name=platform.system() dist_info=platform.linux_distribution(distname='', version='', id='', supported_dists=('SuSE', 'debian', 'redhat'), full_distribution_name=1) self.__distroName=dist_info[0] self.__distroVersion=dist_info[1] self.__distroCode=dist_info[2] self.__vm_manager_pid=os.getpid() self.__freePhyMem=psutil.avail_phymem()/1048576 self.__freeVirtMem=psutil.avail_virtmem()/1048576 self.__totalVirtMem=psutil.total_virtmem()/1048576 self.__usedPhyMem=psutil.used_phymem()/1048576 self.__manager_process=psutil.Process(self.__vm_manager_pid) self.__virt_process=psutil.Process(int(self.__virtProcessPid)) self.__processUptime=datetime.datetime.fromtimestamp(self.__manager_process.create_time).strftime("%Y-%m-%d %H:%M") #self.__processOwner=self.__manager_process.username #self.__numOfProcessThreads=self.__manager_process.get_num_threads() #self.__numOfVirtProcessThreads=self.__virt_process.get_num_threads() #for thread in self.__virt_process.get_threads(): # self.__virtProcessThreadIDs.append(thread[0]) #for thread in self.__manager_process.get_threads(): # self.__processThreadIDs.append(thread[0]) self.__virtProcessCpuUsage=self.__virt_process.get_cpu_percent() self.__proccessCpuUsage=self.__manager_process.get_cpu_percent() mem=self.__virt_process.get_memory_info() self.__virtProcessMemUsage=mem[1]/1048576 mem=self.__manager_process.get_memory_info() self.__processMemUsage=mem[1]/1048576 #self.__processOpenedFilesSpecs=self.__manager_process.get_open_files() #self.__virtProcessOpenedFilesSpecs=self.__virt_process.get_open_files() #self.__processConnectionsSpecs=self.__manager_process.get_connections() #self.__virtProcessConnectionsSpecs=self.__virt_process.get_connections() self.__cpuUsage=psutil.cpu_percent()
def test2(self): "Instrument: _getBufferSize" instrument = Instrument('t') # for lower values of ncount, buffer_size=ncount/mpisize/DEFAULT_NUMBER_SIM_LOOPS instrument.inventory.ncount = ncount = 1e3 self.assertEqual(instrument._getBufferSize(), ncount / DEFAULT_NUMBER_SIM_LOOPS) instrument.inventory.ncount = ncount = 2e3 self.assertEqual(instrument._getBufferSize(), ncount / DEFAULT_NUMBER_SIM_LOOPS) instrument.mpiSize = mpiSize = 10 self.assertEqual(instrument._getBufferSize(), int(ncount / mpiSize / DEFAULT_NUMBER_SIM_LOOPS)) # for higher values, buffer_size is set by memory limit import psutil temp = min(psutil.TOTAL_PHYMEM / 2, (psutil.avail_phymem() + psutil.avail_virtmem()) * 0.7) temp = int(temp) from mcni.neutron_storage.idfneutron import ndblsperneutron max = int(temp / ndblsperneutron / 8 / 100 / mpiSize) * 100 instrument.inventory.ncount = ncount = 1e9 self.assertEqual(instrument._getBufferSize(), max) instrument.inventory.ncount = ncount = 2e9 self.assertEqual(instrument._getBufferSize(), max) # if user set a too high number for buffer size, it is ignored instrument.inventory.ncount = ncount = 2e9 instrument.inventory.buffer_size = 2e9 self.assertEqual(instrument._getBufferSize(), max) # if user set a too low value for buffer_size, a warning would be issued instrument.inventory.ncount = 1e8 instrument.inventory.buffer_size = 100 instrument._getBufferSize() return
def _main(): global available_memory 'Command line entry point.' allow_destructive = False try: if os.environ['DESTRUCTIVE_MODE']: allow_destructive = True except: True hrdw = eval(open(sys.argv[1]).read(-1)) available_memory = psutil.avail_phymem() sys.stderr.write("Available memory before run = %s\n" % available_memory) mode = 'cpu,memory,storage' try: mode = sys.argv[2] except: True if 'cpu-burn' in mode: cpu_perf(hrdw, 60, True) elif 'cpu' in mode: cpu_perf(hrdw) if 'memory-burn' in mode: mem_perf_burn(hrdw, 60) elif 'memory' in mode: mem_perf(hrdw) if 'storage-burn' in mode: storage_perf_burn(hrdw, allow_destructive, 30) elif 'storage' in mode: storage_perf(hrdw, allow_destructive) # Saving result to stdout but also to a filename based on the hw properties output_filename = get_output_filename(hrdw) sys.stderr.write("Saving results in %s\n" % output_filename) with open(output_filename, 'w') as state_file: pprint.pprint(hrdw, stream=state_file) pprint.pprint(hrdw)
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 += ': ' pm = psutil.avail_phymem() vm = psutil.avail_virtmem() tim = seconds() print('{msg}{pm:.2g} GB phys mem, {vm:.2g} 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 doit(): cpu_pc = psutil.cpu_percent() mem_avail_mb = psutil.avail_phymem()/1000000 cpu_temp = round(int(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1e3,1) params = urllib.urlencode({'field1': cpu_pc, 'field2': mem_avail_mb, 'field3': cpu_temp,'key':'YOUR_KEY_HERE'}) headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"} conn = httplib.HTTPConnection("api.thingspeak.com:80") try: conn.request("POST", "/update", params, headers) response = conn.getresponse() print cpu_pc print mem_avail_mb print cpu_temp print strftime("%a, %d %b %Y %H:%M:%S", localtime()) print response.status, response.reason data = response.read() conn.close() except: print "connection failed"
def get_maxmem(): mem = psutil.avail_phymem() sl = [] i = 0 fill_size = 1024 if sys.version.startswith('2.7'): fill_size = 1003 if sys.version.startswith('3'): fill_size = 497 fill_size *= 1024 MiB = 0 while True: max_mem = mem/1024/1024*0.8 if MiB > max_mem: break s = str(i).zfill(fill_size) sl.append(s) i += 1 # MiB += 1024 MiB += 1 return MiB*1024*1024
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 += ': ' pm = psutil.avail_phymem() vm = psutil.avail_virtmem() tim = seconds() print('{msg}{pm:.2g} GB phys mem, {vm:.2g} 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 collect(self): return dict( cpu_times=psutil.cpu_times()._asdict(), mem=dict( total_phymem=psutil.TOTAL_PHYMEM, avail_phymem=psutil.avail_phymem(), avail_virtmem=psutil.avail_virtmem(), cached_phymem=psutil.cached_phymem(), phymem_buffers=psutil.phymem_buffers(), total_virtmem=psutil.total_virtmem(), used_phymem=psutil.used_phymem(), used_virtmem=psutil.used_virtmem(), ), processes=list(self._processes()), net=dict( ifaces=self._net_dev(), proto=self._net_proto() ), io=self._io_stats(), fs=dict(self._fs_usage()), fh=self._file_handles(), )
def doit(): cpu_pc = list(psutil.process_iter()) mem_avail_mb = psutil.avail_phymem() / 1000000 params = urllib.urlencode({ 'field1': cpu_pc, 'field2': mem_avail_mb, 'key': 'YOURKEYHERE' }) headers = { "Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain" } conn = httplib.HTTPConnection("api.thingspeak.com:80") try: conn.request("POST", "/update", params, headers) response = conn.getresponse() data = response.read() conn.close() except: print("connection failed")
def get_available_memory(): try: return psutil.virtual_memory().total except Exception: return psutil.avail_phymem()
import psutil, requests, time, json, subprocess SERVER_ADDRESS = "http://11.0.0.100:9009/upload" TYPE = "rpi" ID = 60 while True: try: packet_loss = 100 avg_ping = 0 cpu_usage = psutil.cpu_percent() mem_free = psutil.avail_phymem() / 1000000 disk_free = psutil.disk_usage('/')[3] proc = subprocess.Popen("ping -i 2 -c 5 -q google.com", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) return_code = proc.wait() stdout_lines = list(proc.stdout) if "error" in stdout_lines: pass else: for line in stdout_lines: if "received" in line and "transmitted" in line: split_comma = line.split(",") packet_loss = int(split_comma[2].split("%")[0]) if "rtt min/avg/max/mdev" in line: avg_ping = int(float(line.split("=")[1].split("/")[1])) client_time = int(time.time()) except Exception, e: print e
def mem_available(): mem = psutil.avail_phymem() return mem * 1024.** -2
def sort(filename, key, outputFile, fields=None, watermark=1024 * 1024 * 100): """Sort a potentially big file filename - the input file (standard File format) key - a list of field names to sort by outputFile - the name of the output file fields - a list of fields that should be included (all fields if None) watermark - when available memory goes bellow the watermark create a new chunk sort() works by reading as records from the file into memory and calling _sortChunk() on each chunk. In the process it gets rid of unneeded fields if any. Once all the chunks have been sorted and written to chunk files it calls _merge() to merge all the chunks into a single sorted file. Note, that sort() gets a key that contains field names, which it converts into field indices for _sortChunk() becuase _sortChunk() doesn't need to know the field name. sort() figures out by itself how many chunk files to use by reading records from the file until the low watermark value of availabel memory is hit and then it sorts the current records, generates a chunk file, clears the sorted records and starts on a new chunk. The key field names are turned into indices """ if fields is not None: assert set(key).issubset(set([f[0] for f in fields])) with FileRecordStream(filename) as f: # Find the indices of the requested fields if fields: fieldNames = [ff[0] for ff in fields] indices = [f.getFieldNames().index(name) for name in fieldNames] assert len(indices) == len(fields) else: fileds = f.getFields() fieldNames = f.getFieldNames() indices = None # turn key fields to key indices key = [fieldNames.index(name) for name in key] chunk = 0 records = [] for i, r in enumerate(f): # Select requested fields only if indices: temp = [] for i in indices: temp.append(r[i]) r = temp # Store processed record records.append(r) # Check memory available_memory = psutil.avail_phymem() # If bellow the watermark create a new chunk, reset and keep going if available_memory < watermark: _sortChunk(records, key, chunk, fields) records = [] chunk += 1 # Sort and write the remainder if len(records) > 0: _sortChunk(records, key, chunk, fields) chunk += 1 # Marge all the files _mergeFiles(key, chunk, outputFile, fields)
def test(long): import shutil from tempfile import gettempdir print 'Running sorter self-test...' # Switch to a temp dir in order to create files freely workDir = os.path.join(gettempdir(), 'sorter_test') if os.path.exists(workDir): shutil.rmtree(workDir) os.makedirs(workDir) os.chdir(workDir) print 'cwd:', os.getcwd() # The fields definition used by all tests fields = [ ('f1', 'int', ''), ('f2', 'int', ''), ('f3', 'int', ''), ('payload', 'string', '') ] # Create a test file testFile = '1.csv' if not os.path.isfile(testFile): writeTestFile(testFile, fields, big=long) # Set watermark here to 300MB bellow current available memory. That ensures # multiple chunk files in the big testcase mem = psutil.avail_phymem() watermark = mem - 300 * 1024 * 1024 print 'Test sorting by f1 and f2, watermak:', watermark results = [] sort(testFile, key=['f1', 'f2'], fields=fields, outputFile='f1_f2.csv', watermark=watermark) with FileRecordStream('f1_f2.csv') as f: for r in f: results.append(r[:3]) assert results == [ [1, 3, 6], [1, 3, 5], [1, 4, 6], [1, 4, 5], [2, 3, 6], [2, 3, 5], [2, 4, 6], [2, 4, 5], ] mem = psutil.avail_phymem() watermark = mem - 300 * 1024 * 1024 print 'Test sorting by f2 and f1, watermark:', watermark results = [] sort(testFile, key=['f2', 'f1'], fields=fields, outputFile='f2_f1.csv', watermark=watermark) with FileRecordStream('f2_f1.csv') as f: for r in f: results.append(r[:3]) assert results == [ [1, 3, 6], [1, 3, 5], [2, 3, 6], [2, 3, 5], [1, 4, 6], [1, 4, 5], [2, 4, 6], [2, 4, 5], ] mem = psutil.avail_phymem() watermark = mem - 300 * 1024 * 1024 print 'Test sorting by f3 and f2, watermark:', watermark results = [] sort(testFile, key=['f3', 'f2'], fields=fields, outputFile='f3_f2.csv', watermark=watermark) with FileRecordStream('f3_f2.csv') as f: for r in f: results.append(r[:3]) assert results == [ [1, 3, 5], [2, 3, 5], [1, 4, 5], [2, 4, 5], [1, 3, 6], [2, 3, 6], [1, 4, 6], [2, 4, 6], ] # Cleanup the work dir os.chdir('..') shutil.rmtree(workDir) print 'done'
def update(self): """ Record information about the cpu and memory usage for this host into a buffer """ self.cpu_load_log.append(psutil.cpu_percent(interval=0, percpu=True)) self.phymem_used_log.append(psutil.used_phymem()) self.phymem_avail_log.append(psutil.avail_phymem())
def test_avail_phymem(self): x = psutil.avail_phymem() self.assertTrue(isinstance(x, (int, long))) self.assertTrue(x > 0)
for i in range(0, 1): humidity = sense.get_humidity() #print("Humidity: %s %%H" % humidity) sense_pressure = sense.get_pressure() #print("Pressure: %s Millibars" % sense_pressure) convert_pressure = sense_pressure * 100 #print("Pressure: %s Pascal" % convert_pressure) temp1 = sense.get_temperature_from_humidity() #print("Temperature1: %sC" % temp1) temp2 = sense.get_temperature_from_pressure() #print("Temperature2: %sC" % temp2) temp3 = (((temp1 + temp2) / 2) - 11) #print("Temperature3: %sC" % temp3) cpu_pc = psutil.cpu_percent() #print cpu_pc mem_avail_mb = psutil.avail_phymem() / 1000000 #print mem_avail_mb cpu_temp = round( int(open('/sys/class/thermal/thermal_zone0/temp').read()) / 1e3, 1) #print cpu_temp if temp3 < 10: #print "cas 1" color = white elif temp3 >= 10 and temp3 < 16: #print "cas 2" color = blue elif temp3 >= 16 and temp3 < 29: #print "cas 3" color = green elif temp3 >= 29 and temp3 < 35:
# Author:Wl # -*- coding=utf-8 -*- import psutil import datetime # 查看cpu的信息 print u"CPU 个数 %s" % psutil.cpu_count() print u"物理CPU个数 {}".format(psutil.cpu_count(logical=False)) print u"CPU uptimes" print psutil.cpu_times() print "" # 查看内存信息 print u"系统总内存 %s M" % (psutil.TOTAL_PHYMEM / 1024 / 1024) print u"系统可用内存 %s M" % (psutil.avail_phymem() / 1024 / 1024) mem_rate = int(psutil.avail_phymem()) / float(psutil.TOTAL_PHYMEM) print u"系统内存使用率 %s %%" % int(mem_rate * 100) # 系统启动时间 print u"系统启动时间 %s" % datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S") # 系统用户 users_count = len(psutil.users()) users_list = ",".join([u.name for u in psutil.users()]) print u"当前有%s个用户,分别是%s" % (users_count, users_list) # 网卡,可以得到网卡属性,连接数,当前流量等信息 net = psutil.net_io_counters() bytes_sent = '{0:.2f} kb'.format(net.bytes_rPcv / 1024) bytes_rcvd = '{0:.2f} kb'.format(net.bytes_sent / 1024)