| # Gaudi Framework, version v23r1 |

Home |  | Generated: Wed Feb 29 2012

Public Member Functions | Public Attributes | Private Member Functions | Private Attributes

# GaudiTest::GaudiFilterExecutable Class Reference

List of all members.

## Public Member Functions
def | __init__
## Public Attributes
| stack_trace_file
## Private Member Functions
def | __UseSeparateProcessGroupForChild
def | _HandleChild
| Needs to replace the ones from RedirectedExecutable and TimeoutExecutable.
def | __Monitor
## Private Attributes
| __input
| __timeout
| __monitor_pid
| __monitor_thread
| This is the interesting part: dump the stack trace to a file.

## Detailed Description

Definition at line 819 of file GaudiTest.py.

## Constructor & Destructor Documentation

def GaudiTest::GaudiFilterExecutable::__init__ | ( |  | self,
|  |  | input,
|  |  | timeout = `-1`
| ) |  |

    Create a new 'Filter'.

    'input' -- The string containing the input to provide to the
    child process.

    'timeout' -- As for 'TimeoutExecutable.__init__'.

Definition at line 820 of file GaudiTest.py.

    00821                                            :
    00822         """Create a new 'Filter'.
    00823
    00824         'input' -- The string containing the input to provide to the
    00825         child process.
    00826
    00827         'timeout' -- As for 'TimeoutExecutable.__init__'."""
    00828
    00829         super(GaudiFilterExecutable, self).__init__(input, timeout)
    00830         self.__input = input
    00831         self.__timeout = timeout
    00832         self.stack_trace_file = None
    00833         # Temporary file to pass the stack trace from one process to the other
    00834         # The file must be closed and reopened when needed to avoid conflicts
    00835         # between the processes
    00836         tmpf = tempfile.mkstemp()
    00837         os.close(tmpf[0])
    00838         self.stack_trace_file = tmpf[1] # remember only the name

## Member Function Documentation

def GaudiTest::GaudiFilterExecutable::__Monitor | ( |  | self ) | `[private]`

    Code copied from FilterExecutable.
    Kill the child if the timeout expires.

    This function is run in the monitoring thread.

Definition at line 958 of file GaudiTest.py.

    00959                            :
    00960             """Code copied from FilterExecutable.
    00961             Kill the child if the timeout expires.
    00962
    00963             This function is run in the monitoring thread."""
    00964
    00965             # The timeout may be expressed as a floating-point value
    00966             # on UNIX, but it must be an integer number of
    00967             # milliseconds when passed to WaitForSingleObject.
    00968             timeout = int(self.__timeout * 1000)
    00969             # Wait for the child process to terminate or for the
    00970             # timer to expire.
    00971             result = win32event.WaitForSingleObject(self._GetChildPID(),
    00972                                                     timeout)
    00973             # If the timeout occurred, kill the child process.
    00974             if result == win32con.WAIT_TIMEOUT:
    00975                 self.Kill()

def GaudiTest::GaudiFilterExecutable::__UseSeparateProcessGroupForChild | ( |  | self ) | `[private]`

    Copied from TimeoutExecutable to allow the re-implementation of
       _HandleChild.

Definition at line 839 of file GaudiTest.py.

    00840                                                :
    00841         """Copied from TimeoutExecutable to allow the re-implementation of
    00842            _HandleChild.
    00843         """
    00844         if sys.platform == "win32":
    00845             # In Windows 2000 (or later), we should use "jobs" by
    00846             # analogy with UNIX process groups.  However, that
    00847             # functionality is not (yet) provided by the Python Win32
    00848             # extensions.
    00849             return 0
    00850
            return self.__timeout >= 0 or self.__timeout == -2

def GaudiTest::GaudiFilterExecutable::_HandleChild | ( |  | self ) | `[private]`

Needs to replace the ones from RedirectedExecutable and TimeoutExecutable.

    Code copied from both FilterExecutable and TimeoutExecutable.

Definition at line 853 of file GaudiTest.py.

    00854                           :
    00855         """Code copied from both FilterExecutable and TimeoutExecutable.
    00856         """
    00857         # Close the pipe ends that we do not need.
    00858         if self._stdin_pipe:
    00859             self._ClosePipeEnd(self._stdin_pipe[0])
    00860         if self._stdout_pipe:
    00861             self._ClosePipeEnd(self._stdout_pipe[1])
    00862         if self._stderr_pipe:
    00863             self._ClosePipeEnd(self._stderr_pipe[1])
    00864
    00865         # The pipes created by 'RedirectedExecutable' must be closed
    00866         # before the monitor process (created by 'TimeoutExecutable')
    00867         # is created.  Otherwise, if the child process dies, 'select'
    00868         # in the parent will not return if the monitor process may
    00869         # still have one of the file descriptors open.
    00870
    00871         super(qm.executable.TimeoutExecutable, self)._HandleChild()
    00872
    00873         if self.__UseSeparateProcessGroupForChild():
    00874             # Put the child into its own process group.  This step is
    00875             # performed in both the parent and the child; therefore both
    00876             # processes can safely assume that the creation of the process
    00877             # group has taken place.
    00878             child_pid = self._GetChildPID()
    00879             try:
    00880                 os.setpgid(child_pid, child_pid)
    00881             except:
    00882                 # The call to setpgid may fail if the child has exited,
    00883                 # or has already called 'exec'.  In that case, we are
    00884                 # guaranteed that the child has already put itself in the
    00885                 # desired process group.
    00886                 pass
    00887             # Create the monitoring process.
    00888             #
    00889             # If the monitoring process is in parent's process group and
    00890             # kills the child after waitpid has returned in the parent, we
    00891             # may end up trying to kill a process group other than the one
    00892             # that we intend to kill.  Therefore, we put the monitoring
    00893             # process in the same process group as the child; that ensures
    00894             # that the process group will persist until the monitoring
    00895             # process kills it.
    00896             self.__monitor_pid = os.fork()
    00897             if self.__monitor_pid != 0:
    00898                 # Make sure that the monitoring process is placed into the
    00899                 # child's process group before the parent process calls
    00900                 # 'waitpid'.  In this way, we are guaranteed that the process
    00901                 # group as the child
    00902                 os.setpgid(self.__monitor_pid, child_pid)
    00903             else:
    00904                 # Put the monitoring process into the child's process
    00905                 # group.  We know the process group still exists at
    00906                 # this point because either (a) we are in the process
    00907                 # group, or (b) the parent has not yet called waitpid.
    00908                 os.setpgid(0, child_pid)
    00909
    00910                 # Close all open file descriptors.  They are not needed
    00911                 # in the monitor process.  Furthermore, when the parent
    00912                 # closes the write end of the stdin pipe to the child,
    00913                 # we do not want the pipe to remain open; leaving the
    00914                 # pipe open in the monitor process might cause the child
    00915                 # to block waiting for additional input.
    00916                 try:
    00917                     max_fds = os.sysconf("SC_OPEN_MAX")
    00918                 except:
    00919                     max_fds = 256
    00920                 for fd in xrange(max_fds):
    00921                     try:
    00922                         os.close(fd)
    00923                     except:
    00924                         pass
    00925                 try:
    00926                     if self.__timeout >= 0:
    00927                         # Give the child time to run.
    00928                         time.sleep (self.__timeout)
    00929                         #######################################################
    00930                         ### This is the interesting part: dump the stack trace to a file
    00931                         if sys.platform == "linux2": # we should be have /proc and gdb
    00932                             cmd = ["gdb",
    00933                                    os.path.join("/proc", str(child_pid), "exe"),
    00934                                    str(child_pid),
    00935                                    "-batch", "-n", "-x",
    00936                                    "'%s'" % os.path.join(os.path.dirname(__file__), "stack-trace.gdb")]
    00937                             # FIXME: I wanted to use subprocess.Popen, but it doesn't want to work
    00938                             #        in this context.
    00939                             o = os.popen(" ".join(cmd)).read()
    00940                             open(self.stack_trace_file,"w").write(o)
    00941                         #######################################################
    00942
    00943                         # Kill all processes in the child process group.
    00944                         os.kill(0, signal.SIGKILL)
    00945                     else:
    00946                         # This call to select will never terminate.
    00947                         select.select ([], [], [])
    00948                 finally:
    00949                     # Exit.  This code is in a finally clause so that
    00950                     # we are guaranteed to get here no matter what.
    00951                     os._exit(0)
    00952         elif self.__timeout >= 0 and sys.platform == "win32":
    00953             # Create a monitoring thread.
    00954             self.__monitor_thread = Thread(target = self.__Monitor)
    00955             self.__monitor_thread.start()

## Member Data Documentation

GaudiTest::GaudiFilterExecutable::__input`[private]`

Definition at line 825 of file GaudiTest.py.

GaudiTest::GaudiFilterExecutable::__monitor_pid`[private]`

Definition at line 854 of file GaudiTest.py.

GaudiTest::GaudiFilterExecutable::__monitor_thread`[private]`

This is the interesting part: dump the stack trace to a file.

Definition at line 857 of file GaudiTest.py.

GaudiTest::GaudiFilterExecutable::__timeout`[private]`

Definition at line 825 of file GaudiTest.py.

GaudiTest::GaudiFilterExecutable::stack_trace_file

Definition at line 825 of file GaudiTest.py.

The documentation for this class was generated from the following file:

- /afs/cern.ch/sw/Gaudi/releases/GAUDI/GAUDI_v23r1/GaudiPolicy/qmtest_classes/GaudiTest.py

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines