@@ -115,94 +115,6 @@ def test_psinfo(self):
115
115
libc = ctypes .CDLL ("libc.so" )
116
116
self .assertEqual (psinfo .pr_zoneid , libc .getzoneid ())
117
117
118
- def test_memory_limit (self ):
119
- """Verify that set_memory_limit works."""
120
-
121
- # memory limit to test, keep small to avoid test slowdown
122
- mem_cap = 100 * 1024 * 1024
123
- # measured process resources. Note, that we have a static
124
- # overhead in waste.py for the forking of ps, so while 20M seems
125
- # large compared to a 100M limit, in a real world example with
126
- # 8G limit it's fairly small.
127
- mem_tol = 20 * 1024 * 1024
128
-
129
- waste_mem_py = """
130
- import os
131
- import resource
132
- import subprocess
133
-
134
- import pkg.misc as misc
135
-
136
- misc.set_memory_limit({0})
137
- i = 0
138
- x = {{}}
139
- try:
140
- while True:
141
- i += 1
142
- x[i] = range(i)
143
- except MemoryError:
144
- # give us some breathing room (enough so the test with env var works)
145
- misc.set_memory_limit({0} * 3, allow_override=False)
146
- print(subprocess.check_output(['ps', '-o', 'rss=', '-p',
147
- str(os.getpid())], universal_newlines=True).strip())
148
- """ .format (
149
- str (mem_cap )
150
- )
151
-
152
- # Re-setting limits which are higher than original limit can
153
- # only be done by root.
154
- self .assertTrue (os .geteuid () == 0 , "must be root to run this test" )
155
-
156
- tmpdir = tempfile .mkdtemp (dir = self .test_root )
157
- tmpfile = os .path .join (tmpdir , "waste.py" )
158
- with open (tmpfile , "w" ) as f :
159
- f .write (waste_mem_py )
160
-
161
- pyv = "." .join (platform .python_version_tuple ()[:2 ])
162
-
163
- res = int (subprocess .check_output ([f"python{ pyv } " , tmpfile ]))
164
- # convert from kB to bytes
165
- res *= 1024
166
-
167
- self .debug ("mem_cap: " + str (mem_cap ))
168
- self .debug ("proc size: " + str (res ))
169
-
170
- self .assertTrue (
171
- res < mem_cap + mem_tol , "process mem consumption too high"
172
- )
173
- self .assertTrue (
174
- res > mem_cap - mem_tol , "process mem consumption too low"
175
- )
176
-
177
- # test if env var works
178
- os .environ ["PKG_CLIENT_MAX_PROCESS_SIZE" ] = str (mem_cap * 2 )
179
- res = int (subprocess .check_output ([f"python{ pyv } " , tmpfile ]))
180
- res *= 1024
181
-
182
- self .debug ("mem_cap: " + str (mem_cap ))
183
- self .debug ("proc size: " + str (res ))
184
-
185
- self .assertTrue (
186
- res < mem_cap * 2 + mem_tol , "process mem consumption too high"
187
- )
188
- # self.assertTrue(res > mem_cap * 2 - mem_tol,
189
- # "process mem consumption too low")
190
-
191
- # test if invalid env var is handled correctly
192
- os .environ ["PKG_CLIENT_MAX_PROCESS_SIZE" ] = "octopus"
193
- res = int (subprocess .check_output ([f"python{ pyv } " , tmpfile ]))
194
- res *= 1024
195
-
196
- self .debug ("mem_cap: " + str (mem_cap ))
197
- self .debug ("proc size: " + str (res ))
198
-
199
- self .assertTrue (
200
- res < mem_cap + mem_tol , "process mem consumption too high"
201
- )
202
- self .assertTrue (
203
- res > mem_cap - mem_tol , "process mem consumption too low"
204
- )
205
-
206
118
207
119
if __name__ == "__main__" :
208
120
unittest .main ()
0 commit comments