1
0
mirror of https://github.com/CumulusNetworks/ifupdown2.git synced 2024-05-06 15:54:50 +00:00

performance fix: better handling fd to allow subprocess.close_fds=False and code re-organisation

Ticket: None
Reviewed By: CCR-4692
Testing Done: smoke + scale tests

If called with close_fds=True the subprocess module will try to close every fd
from 3 to MAXFD before executing the specified command. This is done in Python
not even with a C-implementation which truly affecting performances.

This patch aims to better handle the file descriptor used by ifupdown2. Either
by closing them after use or by setting the close-on-exec flag for the file
descriptor, which causes the file descriptor to be automatically
(and atomically) closed when any of the exec-family functions succeed.

With the actual patch all tests are passing, I can't think of any future issue
but if any a possible future modification might be to use the parameter
'preexec_fn', which allows us to set function which will be executed in the
child process before executing the command line. We can always manually close
any remaining open file descriptors with something like:

>>> os.listdir('/proc/self/fd/')
['0', '1', '2', ‘3’, etc..]
>>> for fd in os.listdir('/proc/self/fd/')
>>>    if int(fd) > 2:
>>>    	  os.close(fd)

This patch is also totally re-organising the use of subprocesses. By removing
all subprocess code redundancy.
This commit is contained in:
Julien Fortin
2016-05-13 19:52:57 +02:00
parent afe5125163
commit a193d8d1c0
18 changed files with 343 additions and 464 deletions

View File

@@ -6,6 +6,7 @@
import os
from utilsbase import *
from ifupdown.utils import utils
class systemUtils():
@classmethod
@@ -22,7 +23,7 @@ class systemUtils():
if procname:
try:
utilsobj.exec_command('/bin/pidof %s' %procname)
utils.exec_command('/bin/pidof %s' % procname, stdout=False)
except:
return False
else:
@@ -34,10 +35,9 @@ class systemUtils():
def check_service_status(cls, servicename=None):
if not servicename:
return False
utilsobj = utilsBase()
try:
utilsobj.subprocess_check_call(['/usr/sbin/service',
'%s' %servicename, 'status'])
utils.exec_commandl(['/usr/sbin/service', servicename, 'status'],
stdout=False)
except Exception:
# XXX: check for subprocess errors vs os error
return False
@@ -47,9 +47,8 @@ class systemUtils():
def is_process_running(self, processname):
if not processname:
return False
utilsobj = utilsBase()
try:
utilsobj.exec_command('/bin/pidof %s' %processname)
utils.exec_command('/bin/pidof %s' % processname, stdout=False)
except:
return False
else: