本文整理汇总了Python中resource.getpagesize函数的典型用法代码示例。如果您正苦于以下问题:Python getpagesize函数的具体用法?Python getpagesize怎么用?Python getpagesize使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了getpagesize函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _makebundlefile
def _makebundlefile(part):
"""constructs a temporary bundle file
part.data should be an uncompressed v1 changegroup"""
fp = None
fd, bundlefile = tempfile.mkstemp()
try: # guards bundlefile
try: # guards fp
fp = os.fdopen(fd, 'wb')
magic = 'HG10UN'
fp.write(magic)
data = part.read(resource.getpagesize() - len(magic))
while data:
fp.write(data)
data = part.read(resource.getpagesize())
finally:
fp.close()
except:
try:
os.unlink(bundlefile)
except:
# we would rather see the original exception
pass
raise
return bundlefile
示例2: periodicCheck
def periodicCheck(_reactor=reactor):
# Measure how much garbage we have
garbage_count = len(gc.garbage)
MetricCountEvent.log('gc.garbage', garbage_count, absolute=True)
if garbage_count == 0:
level = ALARM_OK
else:
level = ALARM_WARN
MetricAlarmEvent.log('gc.garbage', level=level)
if resource:
r = resource.getrusage(resource.RUSAGE_SELF)
attrs = ['ru_utime', 'ru_stime', 'ru_maxrss', 'ru_ixrss', 'ru_idrss',
'ru_isrss', 'ru_minflt', 'ru_majflt', 'ru_nswap',
'ru_inblock', 'ru_oublock', 'ru_msgsnd', 'ru_msgrcv',
'ru_nsignals', 'ru_nvcsw', 'ru_nivcsw']
for i,a in enumerate(attrs):
# Linux versions prior to 2.6.32 didn't report this value, but we
# can calculate it from /proc/<pid>/statm
v = r[i]
if a == 'ru_maxrss' and v == 0:
v = _get_rss() * resource.getpagesize() / 1024
MetricCountEvent.log('resource.%s' % a, v, absolute=True)
MetricCountEvent.log('resource.pagesize', resource.getpagesize(), absolute=True)
# Measure the reactor delay
then = util.now(_reactor)
dt = 0.1
def cb():
now = util.now(_reactor)
delay = (now - then) - dt
MetricTimeEvent.log("reactorDelay", delay)
_reactor.callLater(dt, cb)
示例3: native_map_io_space
def native_map_io_space(self, base, size, unused_cache_type):
"""Map to memory a specific region."""
if self.devmem_available() and not self.memory_mapping(base, size):
if logger().VERBOSE:
logger().log("[helper] Mapping 0x%x to memory" % (base))
length = max(size, resource.getpagesize())
page_aligned_base = base - (base % resource.getpagesize())
mapping = MemoryMapping(self.dev_mem, length, mmap.MAP_SHARED,
mmap.PROT_READ | mmap.PROT_WRITE,
offset=page_aligned_base)
self.mappings.append(mapping)
示例4: __init__
def __init__(self, label=None):
"""
@param (basestring) label ロギング時に [label] と表示される
"""
self.start_time = time.time()
self.latest_time = time.time()
self.label = label
self._inner_log = []
self.start_mem = float(getrusage(RUSAGE_SELF)[6]*getpagesize())
self.latest_mem = float(getrusage(RUSAGE_SELF)[6]*getpagesize())
示例5: periodicCheck
def periodicCheck(_reactor=reactor):
try:
# Measure how much garbage we have
garbage_count = len(gc.garbage)
MetricCountEvent.log("gc.garbage", garbage_count, absolute=True)
if garbage_count == 0:
level = ALARM_OK
else:
level = ALARM_WARN
MetricAlarmEvent.log("gc.garbage", level=level)
if resource:
r = resource.getrusage(resource.RUSAGE_SELF)
attrs = [
"ru_utime",
"ru_stime",
"ru_maxrss",
"ru_ixrss",
"ru_idrss",
"ru_isrss",
"ru_minflt",
"ru_majflt",
"ru_nswap",
"ru_inblock",
"ru_oublock",
"ru_msgsnd",
"ru_msgrcv",
"ru_nsignals",
"ru_nvcsw",
"ru_nivcsw",
]
for i, a in enumerate(attrs):
# Linux versions prior to 2.6.32 didn't report this value, but we
# can calculate it from /proc/<pid>/statm
v = r[i]
if a == "ru_maxrss" and v == 0:
v = _get_rss() * resource.getpagesize() / 1024
MetricCountEvent.log("resource.%s" % a, v, absolute=True)
MetricCountEvent.log("resource.pagesize", resource.getpagesize(), absolute=True)
# Measure the reactor delay
then = util.now(_reactor)
dt = 0.1
def cb():
now = util.now(_reactor)
delay = (now - then) - dt
MetricTimeEvent.log("reactorDelay", delay)
_reactor.callLater(dt, cb)
except Exception:
log.err(None, "while collecting VM metrics")
示例6: get_mem_usage_virt_and_res
def get_mem_usage_virt_and_res():
"""
This only works on Linux, and only if the /proc/$PID/statm output is the
same as that in linux kernel 2.6. Also `os.getpid()' must work.
"""
try:
import resource
except ImportError:
raise NotSupportedException
# sample output from cat /proc/$PID/statm:
# 14317 3092 832 279 0 2108 0
a = os.popen("cat /proc/%s/statm" % os.getpid()).read().split()
if not len(a) > 1:
raise NotSupportedException
return (int(a[0]) * resource.getpagesize(), int(a[1]) * resource.getpagesize(),)
示例7: _check_mlock_unevictable
def _check_mlock_unevictable(self):
"""
Check nr_mlock and nr_unevictable with guest memory
"""
if self.realtime_mlock == "on":
vm_pages = self.vm_mem * 1024 * 1024 / getpagesize()
nr_mlock = self.mlock_post - self.mlock_pre
nr_unevictable = self.unevictable_post - self.unevictable_pre
if nr_mlock < vm_pages:
self.test.fail("nr_mlock is not fit with VM memory"
" when mlock is %s!"
" nr_mlock = %d, vm_mem = %d."
% (self.realtime_mlock, nr_mlock, self.vm_mem))
if nr_unevictable < vm_pages:
self.test.fail("nr_unevictable is not fit with VM memory"
" when mlock is %s!"
" nr_unevictable = %d, vm_mem = %d."
% (self.realtime_mlock, nr_unevictable,
self.vm_mem))
else:
if self.mlock_post != self.mlock_pre:
self.test.fail("mlock_post != mlock_pre when mlock is %s!"
% self.realtime_mlock)
if self.unevictable_post != self.unevictable_pre:
self.test.fail("unevictable_post != unevictable_pre"
" when mlock is %s!"
% self.realtime_mlock)
示例8: main
def main():
"""Main Loop."""
APPNAME = str(__package__ or __doc__)[:99].lower().strip().replace(" ", "")
if not sys.platform.startswith("win") and sys.stderr.isatty():
def add_color_emit_ansi(fn):
"""Add methods we need to the class."""
def new(*args):
"""Method overload."""
if len(args) == 2:
new_args = (args[0], copy(args[1]))
else:
new_args = (args[0], copy(args[1]), args[2:])
if hasattr(args[0], 'baseFilename'):
return fn(*args)
levelno = new_args[1].levelno
if levelno >= 50:
color = '\x1b[31;5;7m\n ' # blinking red with black
elif levelno >= 40:
color = '\x1b[31m' # red
elif levelno >= 30:
color = '\x1b[33m' # yellow
elif levelno >= 20:
color = '\x1b[32m' # green
elif levelno >= 10:
color = '\x1b[35m' # pink
else:
color = '\x1b[0m' # normal
try:
new_args[1].msg = color + str(new_args[1].msg) + ' \x1b[0m'
except Exception as reason:
print(reason) # Do not use log here.
return fn(*new_args)
return new
# all non-Windows platforms support ANSI Colors so we use them
log.StreamHandler.emit = add_color_emit_ansi(log.StreamHandler.emit)
log.basicConfig(level=-1, format="%(levelname)s:%(asctime)s %(message)s")
log.getLogger().addHandler(log.StreamHandler(sys.stderr))
log.info(__doc__)
try:
os.nice(19) # smooth cpu priority
libc = cdll.LoadLibrary('libc.so.6') # set process name
buff = create_string_buffer(len(APPNAME) + 1)
buff.value = bytes(APPNAME.encode("utf-8"))
libc.prctl(15, byref(buff), 0, 0, 0)
except Exception as reason:
log.warning(reason)
signal.signal(signal.SIGINT, signal.SIG_DFL) # CTRL+C work to quit app
app = QApplication(sys.argv)
app.setApplicationName(APPNAME)
app.setOrganizationName(APPNAME)
app.setOrganizationDomain(APPNAME)
app.instance().setQuitOnLastWindowClosed(False) # no quit on dialog close
icon = QIcon(app.style().standardPixmap(QStyle.SP_FileIcon))
app.setWindowIcon(icon)
win = MainWindow(icon)
win.show()
log.info('Total Maximum RAM Memory used: ~{} MegaBytes.'.format(int(
resource.getrusage(resource.RUSAGE_SELF).ru_maxrss *
resource.getpagesize() / 1024 / 1024 if resource else 0)))
sys.exit(app.exec_())
示例9: memory_used
def memory_used():
"""Returns the amount of resident memory in use in MBs.
"""
# FIXME Need to fill out appropriate methods here for
# different platforms.
# For Linux use the proc filesystem. Use 'statm' as easier
# to parse than 'status' file.
#
# /proc/[number]/statm
# Provides information about memory usage, measured in pages.
# The columns are:
#
# size total program size
# (same as VmSize in /proc/[number]/status)
# resident resident set size
# (same as VmRSS in /proc/[number]/status)
# share shared pages (from shared mappings)
# text text (code)
# lib library (unused in Linux 2.6)
# data data + stack
# dt dirty pages (unused in Linux 2.6)
if sys.platform == 'linux2':
pid = os.getpid()
statm = '/proc/%d/statm' % pid
fp = None
try:
fp = open(statm, 'r')
rss_pages = float(fp.read().split()[1])
memory_bytes = rss_pages * resource.getpagesize()
return memory_bytes / (1024*1024)
except Exception:
pass
finally:
if fp:
fp.close()
# Fallback to trying to use getrusage(). The units returned
# can differ based on platform. Assume 1024 byte blocks as
# default.
if 'resource' in sys.modules:
rusage = resource.getrusage(resource.RUSAGE_SELF)
if sys.platform == 'darwin':
# On MacOS X, despite the manual page saying the
# value is in kilobytes, it is actually in bytes.
memory_bytes = float(rusage.ru_maxrss)
return memory_bytes / (1024*1024)
else:
memory_kbytes = float(rusage.ru_maxrss)
return memory_kbytes / 1024
# Fallback to indicating no memory usage.
return 0
示例10: getProcRss
def getProcRss(procId):
# http://man7.org/linux/man-pages/man5/proc.5.html
stats = open("/proc/%i/stat" % procId).read().split()
mstats = open("/proc/%i/statm" % procId).read().split()
rss1 = int(stats[23])
rss2 = int(mstats[1])
return rss2 * resource.getpagesize()
示例11: __init__
def __init__(self, commands, timeout, interval, output_dir=None, monitor_dir=None):
self.start_time = time()
self.end_time = self.start_time + timeout if timeout else 0 # Do not time out if time_limit is 0.
self._interval = interval
self._rm = ResourceMonitor(output_dir, commands)
if monitor_dir:
self.monitor_file = open(monitor_dir + "/resource_usage.log", "w", (1024 ** 2) * 10) # Set the file's buffering to 10MB
# We read the jiffie -> second conversion rate from the os, by dividing the utime
# and stime values by this conversion rate we will get the actual cpu seconds spend during this second.
try:
sc_clk_tck = float(sysconf(sysconf_names['SC_CLK_TCK']))
except AttributeError:
sc_clk_tck = 100.0
try:
import resource
pagesize = resource.getpagesize()
except:
pagesize = 4 * 1024
self.monitor_file.write(json.dumps({"sc_clk_tck": sc_clk_tck, 'pagesize': pagesize}) + "\n")
else:
self.monitor_file = None
# Capture SIGTERM to kill all the child processes before dying
self.stopping = False
signal(SIGTERM, self._termTrap)
示例12: get_socket_info
def get_socket_info(self, openr=open):
"""
get info from /proc/net/sockstat and sockstat6
Note: The mem value is actually kernel pages, but we return bytes
allocated based on the systems page size.
"""
sockstat = {}
try:
with openr('/proc/net/sockstat', 'r') as proc_sockstat:
for entry in proc_sockstat:
if entry.startswith("TCP: inuse"):
tcpstats = entry.split()
sockstat['tcp_in_use'] = int(tcpstats[2])
sockstat['orphan'] = int(tcpstats[4])
sockstat['time_wait'] = int(tcpstats[6])
sockstat['tcp_mem_allocated_bytes'] = \
int(tcpstats[10]) * getpagesize()
except IOError as e:
if e.errno != errno.ENOENT:
raise
try:
with openr('/proc/net/sockstat6', 'r') as proc_sockstat6:
for entry in proc_sockstat6:
if entry.startswith("TCP6: inuse"):
sockstat['tcp6_in_use'] = int(entry.split()[2])
except IOError as e:
if e.errno != errno.ENOENT:
raise
return sockstat
示例13: memoryWriteLoop
def memoryWriteLoop(logfileLocation, interval):
try:
if isinstance(logfileLocation, file):
outputFile = logfileLocation
else:
outputFile = open(logfileLocation, "w", 0)
def pad(s, finalLength):
return s + " " * (finalLength - len(s))
def formatCols(columns):
return "".join([pad(columns[0],30)] + [pad(c, 15) for c in columns[1:]])
columns = ["timestamp", "total size MB", "RSS MB", "shared MB", "code MB", "stack MB", "library MB", "dirty MB"]
print >> outputFile, formatCols(columns)
while True:
with open("/proc/%s/statm" % os.getpid(), "r") as f:
data = ["%.2f" % (int(x) * resource.getpagesize() / 1024 / 1024.0) for x in f.readline().split(" ")]
print >> outputFile, formatCols([time.strftime("%Y-%m-%dT%H:%M:%S")] + data)
time.sleep(interval)
except:
import traceback
traceback.format_exc()
示例14: get_page_size
def get_page_size():
global cache_page_size
if not cache_page_size:
cache_page_size = resource.getpagesize()
return cache_page_size
示例15: __init__
def __init__(self, config, logger, readq):
super(Netstat, self).__init__(config, logger, readq)
self.page_size = resource.getpagesize()
try:
self.sockstat = open("/proc/net/sockstat")
self.netstat = open("/proc/net/netstat")
self.snmp = open("/proc/net/snmp")
except IOError:
self.log_exception('open failed')
self.cleanup()
raise
with utils.lower_privileges(self._logger):
# Note: up until v2.6.37-rc2 most of the values were 32 bits.
# The first value is pretty useless since it accounts for some
# socket types but not others. So we don't report it because it's
# more confusing than anything else and it's not well documented
# what type of sockets are or aren't included in this count.
self.regexp = re.compile("sockets: used \d+\n"
"TCP: inuse (?P<tcp_inuse>\d+) orphan (?P<orphans>\d+)"
" tw (?P<tw_count>\d+) alloc (?P<tcp_sockets>\d+)"
" mem (?P<tcp_pages>\d+)\n"
"UDP: inuse (?P<udp_inuse>\d+)"
# UDP memory accounting was added in v2.6.25-rc1
"(?: mem (?P<udp_pages>\d+))?\n"
# UDP-Lite (RFC 3828) was added in v2.6.20-rc2
"(?:UDPLITE: inuse (?P<udplite_inuse>\d+)\n)?"
"RAW: inuse (?P<raw_inuse>\d+)\n"
"FRAG: inuse (?P<ip_frag_nqueues>\d+)"
" memory (?P<ip_frag_mem>\d+)\n")