Skip to content

Commit 4ea0c09

Browse files
committed
Updated mininet/util.py to support better resource setting semantics and protected with try block
1 parent 501a164 commit 4ea0c09

1 file changed

Lines changed: 36 additions & 29 deletions

File tree

mininet/util.py

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"Utility functions for Mininet."
22

3-
from mininet.log import output, info, error, warn
3+
from mininet.log import output, info, error, warn, debug
44

55
from time import sleep
66
from resource import getrlimit, setrlimit, RLIMIT_NPROC, RLIMIT_NOFILE
@@ -361,16 +361,17 @@ def sysctlTestAndSet( name, limit ):
361361
if '/' not in name:
362362
name = '/proc/sys/' + name.replace( '.', '/' )
363363
#read limit
364-
f = open( name, 'r+' )
365-
oldLimit = f.readline()
366-
if type( limit ) is int:
367-
#compare integer limits before overriding
368-
if int( oldLimit ) < limit:
369-
f.write( "%d" % limit )
370-
else:
371-
#overwrite non-integer limits
372-
f.write( limit )
373-
f.close()
364+
with open( name, 'r' ) as readFile:
365+
oldLimit = readFile.readline()
366+
if type( limit ) is int:
367+
#compare integer limits before overriding
368+
if int( oldLimit ) < limit:
369+
with open( name, 'w' ) as writeFile:
370+
writeFile.write( "%d" % limit )
371+
else:
372+
#overwrite non-integer limits
373+
with open( name, 'w' ) as writeFile:
374+
writeFile.write( limit )
374375

375376
def rlimitTestAndSet( name, limit ):
376377
"Helper function to set rlimits"
@@ -381,24 +382,30 @@ def rlimitTestAndSet( name, limit ):
381382

382383
def fixLimits():
383384
"Fix ridiculously small resource limits."
384-
rlimitTestAndSet( RLIMIT_NPROC, 8192 )
385-
rlimitTestAndSet( RLIMIT_NOFILE, 16384 )
386-
#Increase open file limit
387-
sysctlTestAndSet( 'fs.file-max', 10000 )
388-
#Increase network buffer space
389-
sysctlTestAndSet( 'net.core.wmem_max', 16777216 )
390-
sysctlTestAndSet( 'net.core.rmem_max', 16777216 )
391-
sysctlTestAndSet( 'net.ipv4.tcp_rmem', '10240 87380 16777216' )
392-
sysctlTestAndSet( 'net.ipv4.tcp_wmem', '10240 87380 16777216' )
393-
sysctlTestAndSet( 'net.core.netdev_max_backlog', 5000 )
394-
#Increase arp cache size
395-
sysctlTestAndSet( 'net.ipv4.neigh.default.gc_thresh1', 4096 )
396-
sysctlTestAndSet( 'net.ipv4.neigh.default.gc_thresh2', 8192 )
397-
sysctlTestAndSet( 'net.ipv4.neigh.default.gc_thresh3', 16384 )
398-
#Increase routing table size
399-
sysctlTestAndSet( 'net.ipv4.route.max_size', 32768 )
400-
#Increase number of PTYs for nodes
401-
sysctlTestAndSet( 'kernel.pty.max', 20000 )
385+
debug( "*** Setting resource limits\n" )
386+
try:
387+
rlimitTestAndSet( RLIMIT_NPROC, 8192 )
388+
rlimitTestAndSet( RLIMIT_NOFILE, 16384 )
389+
#Increase open file limit
390+
sysctlTestAndSet( 'fs.file-max', 10000 )
391+
#Increase network buffer space
392+
sysctlTestAndSet( 'net.core.wmem_max', 16777216 )
393+
sysctlTestAndSet( 'net.core.rmem_max', 16777216 )
394+
sysctlTestAndSet( 'net.ipv4.tcp_rmem', '10240 87380 16777216' )
395+
sysctlTestAndSet( 'net.ipv4.tcp_wmem', '10240 87380 16777216' )
396+
sysctlTestAndSet( 'net.core.netdev_max_backlog', 5000 )
397+
#Increase arp cache size
398+
sysctlTestAndSet( 'net.ipv4.neigh.default.gc_thresh1', 4096 )
399+
sysctlTestAndSet( 'net.ipv4.neigh.default.gc_thresh2', 8192 )
400+
sysctlTestAndSet( 'net.ipv4.neigh.default.gc_thresh3', 16384 )
401+
#Increase routing table size
402+
sysctlTestAndSet( 'net.ipv4.route.max_size', 32768 )
403+
#Increase number of PTYs for nodes
404+
sysctlTestAndSet( 'kernel.pty.max', 20000 )
405+
assert False
406+
except:
407+
warn( "*** Error setting resource limits. "
408+
"Mininet's performance may be affected.\n" )
402409

403410
def mountCgroups():
404411
"Make sure cgroups file system is mounted"

0 commit comments

Comments
 (0)