def isMemEnoughEigen(D, extraVarsRatio=5):
mem = virtual_memory()
installedMemGB = round( mem.total * 1.0 / (1<<30) )
# 15 is an empirical estimation. when D=30K, it takes around 50GB mem
requiredMemGB = D * D * 4.0 * ( extraVarsRatio + 8 ) / 1000000000
# installed mem is enough
if requiredMemGB <= installedMemGB:
isEnough = 2
# give a warning, will use some paging file and make the computer very slow
elif requiredMemGB <= installedMemGB * 1.2:
isEnough = 1
# not enough
else:
isEnough = 0
return isEnough, installedMemGB, requiredMemGB
python类virtual_memory()的实例源码
def monitor(frist_invoke=1):
mem = psutil.virtual_memory()
value_dic = {
'memory': {
'mem.total': int(mem.total/(1024*1024)),
'mem.free': int(mem.free/(1024*1024)),
'mem.buffers': int(mem.buffers/(1024*1024)),
'mem.cache': int(mem.cached/(1024*1024)),
'mem.used': int(mem.used/(1024*1024)),
'mem.percent': mem.percent
}
}
return value_dic
def poll():
global lastChecksum, voltage
if socketio != None:
mempercent = psutil.virtual_memory().percent
cpupercent = psutil.cpu_percent(interval=None)
cputemp = get_cpu_temperature()
secs = int(round(time.time()))
# every 10 seconds update voltage (or if it is zero)
if(secs % 10 == 0 or voltage == 0):
v = robot.getVoltage()
# sometimes getVoltage fails and returns 0...don't show it..wait for next read
if(v != 0):
voltage = v
data = {'mp': mempercent, 'cp': cpupercent, 'ct': cputemp, 'v': voltage}
checksum = hashlib.sha224(json.dumps(data)).hexdigest()
if lastChecksum != checksum:
socketio.emit('sysinfo', data)
lastChecksum = checksum
time.sleep(0.5)
def update(self):
"""Function to update the entire class information."""
self.cpu["percentage"] = psutil.cpu_percent(interval=0.7)
self.boot = datetime.datetime.fromtimestamp(psutil.boot_time()).strftime(
"%Y-%m-%d %H:%M:%S")
virtual_memory = psutil.virtual_memory()
self.memory["used"] = virtual_memory.used
self.memory["free"] = virtual_memory.free
self.memory["cached"] = virtual_memory.cached
net_io_counters = psutil.net_io_counters()
self.network["packet_sent"] = net_io_counters.packets_sent
self.network["packet_recv"] = net_io_counters.packets_recv
disk_usage = psutil.disk_usage('/')
self.disk["total"] = int(disk_usage.total/1024)
self.disk["used"] = int(disk_usage.used/1024)
self.disk["free"] = int(disk_usage.free/1024)
self.timestamp = time.time()
def show_platform():
''' Show information on platform'''
swrite('\n=== SYSTEM ===\n\n')
try:
linux_distribution = platform.linux_distribution()
except:
linux_distribution = "N/A"
swrite("""
dist: %s
linux_distribution: %s
system: %s
machine: %s
platform: %s
uname: %s
version: %s
mac_ver: %s
memory: %s
number of CPU: %s
""" % (
str(platform.dist()),
linux_distribution,
platform.system(),
platform.machine(),
platform.platform(),
platform.uname(),
platform.version(),
platform.mac_ver(),
psutil.virtual_memory(),
str(psutil.cpu_count())
))
def collect_meminfo(self):
meminfo = psutil.virtual_memory()
memdict = {}
memdict['total'] = meminfo.total/1024
memdict['used'] = meminfo.used/1024
memdict['free'] = meminfo.free/1024
memdict['buffers'] = meminfo.buffers/1024
memdict['cached'] = meminfo.cached/1024
memdict['percent'] = meminfo.percent
#print(output)
#print(memparts)
return memdict
# collect cpu used information and processors information
def printMemUsage():
psutil = getPSUtil()
if psutil:
u = mem_usage()
M = psutil.virtual_memory()
print( "Process Mem: %0.1fMb System: %0.1fMb / %0.1fMb"%(u, M.used/1048576., M.total/1048576.) )
def print_mem_time(tag):
print(tag," time %.2f seconds"%(time.time()-_start), end='')
try:
summ = psutil.virtual_memory()
print(" Used: %.2f GB %.2f%%"%((summ.used)/(1024.0*1024*1024),summ.percent))
except:
print()
def __get_mem_usage(self):
return psutil.virtual_memory().percent / 100.0
def __init__(self, cfgParams, memory_factor):
"""
Constructor
:param cfgParams: initialized NetTrainerParams
:param memory_factor: fraction of memory used for single shared variable
"""
self.cfgParams = cfgParams
if not isinstance(cfgParams, NetTrainerParams):
raise ValueError("cfgParams must be an instance of NetTrainerParams")
if 'gpu' in theano.config.device:
# get GPU memory info
mem_info = theano.sandbox.cuda.cuda_ndarray.cuda_ndarray.mem_info()
self.memory = (mem_info[0] / 1024 ** 2) / float(memory_factor) # MB, use third of free memory
elif 'cpu' in theano.config.device:
# get CPU memory info
self.memory = (psutil.virtual_memory().available / 1024 ** 2) / float(memory_factor) # MB, use third of free memory
else:
raise EnvironmentError("Neither GPU nor CPU device in theano.config found!")
self.currentMacroBatch = -1 # current batch on GPU, load on first run
self.trainSize = 0
self.sampleSize = 0
self.numTrainSamples = 0
self.numValSamples = 0
self.managedVar = []
def get_system_load(self):
return {
MEM_AVAILABLE: psutil.virtual_memory().available,
CPU_IDLE_PERCENT: psutil.cpu_times_percent(
self.cpu_sample_interval).idle,
CLIENT_NUMBER: len(self.protocol.server.clients),
}
def get_memo_usage_available():
psy_mem = psutil.virtual_memory()
vrt_mem = psutil.swap_memory()
return psy_mem.available, vrt_mem.free + psy_mem.available
def get_init_resource():
cpu = cpu_count() * 100
psy_mem = psutil.virtual_memory()
vrt_mem = psutil.swap_memory()
disk = list(psutil.disk_usage(get_config("env", "workspace")))[0]
return cpu, psy_mem.total, disk, vrt_mem.total + psy_mem.total
def init_with_context(self, context):
ram = psutil.virtual_memory().percent
cpu = psutil.cpu_percent()
green, orange, red, grey = '#00FF38', '#FFB400', '#FF3B00', '#EBEBEB'
ram_color = green
if ram >= 75:
ram_color = red
elif ram >= 50:
ram_color = orange
cpu_color = green
if cpu >= 75:
cpu_color = red
elif cpu >= 50:
cpu_color = orange
self.cpu_idel = 100 - cpu
self.cpu_color = cpu_color
self.cpu = cpu
self.ram = 100 - ram
self.ram_used = ram
self.ram_color = ram_color
def memory(self, type):
mem = psutil.virtual_memory()
if type == 'total':
return format(mem.total / 1024 / 1024, '.0f')
elif type == 'available':
return format(mem.available / 1024 / 1024, '.0f')
elif type == 'used':
return format(mem.used / 1024 / 1024, '.0f')
elif type == 'free':
return format(mem.free / 1024 / 1024, '.0f')
elif type == 'cached':
return format(mem.cached / 1024 / 1024, '.0f')
def get_total_mem():
total_mem = virtual_memory().total
return jsonify(status=0, total_mem=total_mem)
def show_platform():
''' Show information on platform'''
swrite('\n=== SYSTEM ===\n\n')
try:
linux_distribution = platform.linux_distribution()
except:
linux_distribution = "N/A"
swrite("""
dist: %s
linux_distribution: %s
system: %s
machine: %s
platform: %s
uname: %s
version: %s
mac_ver: %s
memory: %s
number of CPU: %s
""" % (
str(platform.dist()),
linux_distribution,
platform.system(),
platform.machine(),
platform.platform(),
platform.uname(),
platform.version(),
platform.mac_ver(),
psutil.virtual_memory(),
str(psutil.cpu_count())
))
def get_memory(self):
return int(psutil.virtual_memory()[2])
def get_total_memory(self):
return psutil.virtual_memory()[0]
def get(self, request, show=False):
if show == 'all':
show = True
cpu_count = get_cpu_count()
return ('processes.html', {
'panel_pid': prism.settings.PANEL_PID,
'cpu_count': cpu_count[0],
'cpu_count_logical': cpu_count[1],
'ram': psutil.virtual_memory()[0],
'processes': self.get_processes(show,
lambda x: x['memory_percent'] + x['cpu_percent'])
})