Debian Codegraph @

lib_pypy/resource.py

raw symbols SCIP
1 """http://docs.python.org/library/resource"""
2
3 from _resource_cffi import ffi, lib
4 from errno import EINVAL, EPERM
5 import _structseq, os, sys
6
7 try:
8 from __pypy__ import builtinify
9 except ImportError:
10 builtinify = lambda f: f
11
12
13 error = OSError
14
15 class struct_rusage(metaclass=_structseq.structseqtype):
16 """struct_rusage: Result from getrusage.
17
18 This object may be accessed either as a tuple of
19 (utime,stime,maxrss,ixrss,idrss,isrss,minflt,majflt,
20 nswap,inblock,oublock,msgsnd,msgrcv,nsignals,nvcsw,nivcsw)
21 or via the attributes ru_utime, ru_stime, ru_maxrss, and so on."""
22
23 name = "resource.struct_rusage"
24
25 ru_utime = _structseq.structseqfield(0, "user time used")
26 ru_stime = _structseq.structseqfield(1, "system time used")
27 ru_maxrss = _structseq.structseqfield(2, "max. resident set size")
28 ru_ixrss = _structseq.structseqfield(3, "shared memory size")
29 ru_idrss = _structseq.structseqfield(4, "unshared data size")
30 ru_isrss = _structseq.structseqfield(5, "unshared stack size")
31 ru_minflt = _structseq.structseqfield(6, "page faults not requiring I/O")
32 ru_majflt = _structseq.structseqfield(7, "page faults requiring I/O")
33 ru_nswap = _structseq.structseqfield(8, "number of swap outs")
34 ru_inblock = _structseq.structseqfield(9, "block input operations")
35 ru_oublock = _structseq.structseqfield(10, "block output operations")
36 ru_msgsnd = _structseq.structseqfield(11, "IPC messages sent")
37 ru_msgrcv = _structseq.structseqfield(12, "IPC messages received")
38 ru_nsignals = _structseq.structseqfield(13, "signals received")
39 ru_nvcsw = _structseq.structseqfield(14, "voluntary context switches")
40 ru_nivcsw = _structseq.structseqfield(15, "involuntary context switches")
41
42 def _make_struct_rusage(ru):
43 return struct_rusage((
44 lib.my_utime(ru),
45 lib.my_stime(ru),
46 ru.ru_maxrss,
47 ru.ru_ixrss,
48 ru.ru_idrss,
49 ru.ru_isrss,
50 ru.ru_minflt,
51 ru.ru_majflt,
52 ru.ru_nswap,
53 ru.ru_inblock,
54 ru.ru_oublock,
55 ru.ru_msgsnd,
56 ru.ru_msgrcv,
57 ru.ru_nsignals,
58 ru.ru_nvcsw,
59 ru.ru_nivcsw,
60 ))
61
62 @builtinify
63 def getrusage(who):
64 ru = ffi.new("struct rusage *")
65 if lib.getrusage(who, ru) == -1:
66 if ffi.errno == EINVAL:
67 raise ValueError("invalid who parameter")
68 raise OSError(ffi.errno, os.strerror(ffi.errno))
69 return _make_struct_rusage(ru)
70
71 @builtinify
72 def getrlimit(resource):
73 if not (0 <= resource < lib.RLIM_NLIMITS):
74 return ValueError("invalid resource specified")
75
76 result = ffi.new("long long[2]")
77 if lib.my_getrlimit(resource, result) == -1:
78 raise OSError(ffi.errno, os.strerror(ffi.errno))
79 return (result[0], result[1])
80
81 @builtinify
82 def setrlimit(resource, limits):
83 if not (0 <= resource < lib.RLIM_NLIMITS):
84 return ValueError("invalid resource specified")
85
86 limits = tuple(limits)
87 if len(limits) != 2:
88 raise ValueError("expected a tuple of 2 integers")
89
90 # accept and round down floats, like CPython does
91 limit0 = int(limits[0])
92 limit1 = int(limits[1])
93
94 if lib.my_setrlimit(resource, limit0, limit1) == -1:
95 if ffi.errno == EINVAL:
96 raise ValueError("current limit exceeds maximum limit")
97 elif ffi.errno == EPERM:
98 raise ValueError("not allowed to raise maximum limit")
99 else:
100 raise OSError(ffi.errno, os.strerror(ffi.errno))
101
102 if sys.platform.startswith("linux") and hasattr(lib, '_prlimit'):
103 @builtinify
104 def prlimit(pid, resource, limits = None):
105 if not (0 <= resource < lib.RLIM_NLIMITS):
106 return ValueError("invalid resource specified")
107
108 if limits is not None:
109 limits = tuple(limits)
110 if len(limits) != 2:
111 raise ValueError("expected a tuple of 2 integers")
112
113 # accept and round down floats, like CPython does
114 limit0 = int(limits[0])
115 limit1 = int(limits[1])
116 else:
117 limit0 = 0
118 limit1 = 0
119
120 result = ffi.new("long long[2]")
121
122 if lib._prlimit(pid, resource, limits is not None, limit0, limit1, result) == -1:
123 if ffi.errno == EINVAL:
124 raise ValueError("current limit exceeds maximum limit")
125 else:
126 raise OSError(ffi.errno, os.strerror(ffi.errno))
127
128 return (result[0], result[1])
129
130
131 @builtinify
132 def getpagesize():
133 return os.sysconf("SC_PAGESIZE")
134
135
136 def _setup():
137 all_constants = []
138 p = lib.my_rlimit_consts
139 while p.name:
140 name = ffi.string(p.name).decode()
141 globals()[name] = int(p.value)
142 all_constants.append(name)
143 p += 1
144 return all_constants
145
146 __all__ = tuple(_setup()) + (
147 'error', 'getpagesize', 'struct_rusage',
148 'getrusage', 'getrlimit', 'setrlimit',
149 )
150 del _setup