Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
util.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * PB, Patrick Bos, Netherlands eScience Center, p.bos@esciencecenter.nl
5 * IP, Inti Pelupessy, Netherlands eScience Center, i.pelupessy@esciencecenter.nl
6 *
7 * Copyright (c) 2021, CERN
8 *
9 * Redistribution and use in source and binary forms,
10 * with or without modification, are permitted according to the terms
11 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
12 */
13
16
17#include <csignal> // kill, SIGKILL
18#include <iostream> // cerr, and indirectly WNOHANG, EINTR, W* macros
19#include <stdexcept> // runtime_error
20#include <sys/wait.h> // waitpid
21#include <string>
22
23namespace RooFit {
24namespace MultiProcess {
25
27{
28 int status = 0;
30 pid_t tmp;
31 do {
32 if (patience-- < 1) {
34 }
35 tmp = waitpid(child_pid, &status, WNOHANG);
36 } while (tmp == 0 // child has not yet changed state, try again
37 || (-1 == tmp && EINTR == errno) // retry on interrupted system call
38 );
39
40 if (patience < 1) {
41 std::cout << "Had to send PID " << child_pid << " " << (-patience + 1) << " SIGKILLs";
42 }
43
44 if (0 != status) {
45 if (WIFEXITED(status)) {
46 printf("exited, status=%d\n", WEXITSTATUS(status));
47 } else if (WIFSIGNALED(status)) {
48 if (WTERMSIG(status) != SIGTERM) {
49 printf("killed by signal %d\n", WTERMSIG(status));
50 }
51 } else if (WIFSTOPPED(status)) {
52 printf("stopped by signal %d\n", WSTOPSIG(status));
53 } else if (WIFCONTINUED(status)) {
54 printf("continued\n");
55 }
56 }
57
58 if (-1 == tmp && may_throw)
59 throw std::runtime_error(std::string("waitpid, errno ") + std::to_string(errno));
60
61 return status;
62}
63
64// returns a tuple containing first the poll result and second a boolean flag that tells the caller whether it should
65// abort the enclosing loop because a SIGTERM was received
66std::tuple<std::vector<std::size_t>, bool> careful_poll(Poller &poller)
67{
68 // Benign signal interruptions are already retried inside Channel::wait, so
69 // an exception here means a termination request.
70 std::vector<std::size_t> poll_result;
71 bool abort = true;
72 try {
73 poll_result = poller.poll(-1);
74 abort = false;
75 } catch (ppoll_error_t &) {
76 }
77 return std::make_tuple(poll_result, abort);
78}
79
80} // namespace MultiProcess
81} // namespace RooFit
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Waits for input on a set of registered Channels.
Definition Poller.h:34
Thrown when a blocking wait on a Channel is interrupted, e.g.
Definition Channel.h:31
int wait_for_child(pid_t child_pid, bool may_throw, int retries_before_killing)
Definition util.cxx:26
std::tuple< std::vector< std::size_t >, bool > careful_poll(Poller &poller)
Definition util.cxx:66
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73