Commit 97dc9f40 authored by Mark Olesen's avatar Mark Olesen
Browse files

EHN: make signal verbosity an optional calling argument.

parent 297570de
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2016 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2016-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -29,10 +29,11 @@ License
#include "OSspecific.H"
#include "IOstreams.H"
#include "Switch.H"
#include "UList.H"
#ifdef LINUX_GNUC
#if defined(__linux__) && defined(__GNUC__)
#ifndef __USE_GNU
#define __USE_GNU
#define __USE_GNU // To use feenableexcept()
#endif
#include <fenv.h>
#include <malloc.h>
......@@ -48,21 +49,23 @@ bool Foam::sigFpe::switchFpe_(Foam::debug::optimisationSwitch("trapFpe", 0));
bool Foam::sigFpe::switchNan_(Foam::debug::optimisationSwitch("setNaN", 0));
bool Foam::sigFpe::sigActive_ = false;
bool Foam::sigFpe::mallocNanActive_ = false;
bool Foam::sigFpe::nanActive_ = false;
struct sigaction Foam::sigFpe::oldAction_;
// File-scope function.
// Controlled by env variable containing a bool (true|false|on|off ...)
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
// Can turn on/off via env variable containing a bool (true|false|on|off ...)
// or by the specified flag
static bool isTrue(const char* envName, const bool flag)
static bool isTrue(const char* envName, bool deflt)
{
const std::string str = Foam::getEnv(envName);
const auto str(Foam::getEnv(envName));
if (str.size())
{
Foam::Switch sw(str, true); // silently ignore bad input
Foam::Switch sw(str, true); // Silently ignores bad input
if (sw.valid())
{
return sw;
......@@ -70,17 +73,11 @@ static bool isTrue(const char* envName, const bool flag)
}
// Env was not set or did not contain a valid bool value
return flag;
}
void Foam::sigFpe::fillNan(UList<scalar>& lst)
{
lst = std::numeric_limits<scalar>::signaling_NaN();
return deflt;
}
#ifdef LINUX
#ifdef __linux__
extern "C"
{
extern void* __libc_malloc(size_t size);
......@@ -88,7 +85,7 @@ extern "C"
// Override the GLIBC malloc to support mallocNan
void* malloc(size_t size)
{
if (Foam::sigFpe::mallocNanActive_)
if (Foam::sigFpe::nanActive())
{
return Foam::sigFpe::mallocNan(size);
}
......@@ -99,21 +96,21 @@ extern "C"
}
}
void* Foam::sigFpe::mallocNan(size_t size)
{
// Call the low-level GLIBC malloc function
void * result = __libc_malloc(size);
void* result = __libc_malloc(size);
// Initialize to signalling NaN
UList<scalar> lst(reinterpret_cast<scalar*>(result), size/sizeof(scalar));
sigFpe::fillNan(lst);
UList<scalar> list(reinterpret_cast<scalar*>(result), size/sizeof(scalar));
sigFpe::fillNan(list);
return result;
}
#endif
#ifdef LINUX_GNUC
#ifdef __GNUC__
void Foam::sigFpe::sigHandler(int)
{
// Reset old handling
......@@ -124,15 +121,12 @@ void Foam::sigFpe::sigHandler(int)
<< abort(FatalError);
}
// Update jobInfo file
jobInfo.signalEnd();
jobInfo.signalEnd(); // Update jobInfo file
error::printStack(Perr);
// Throw signal (to old handler)
raise(SIGFPE);
raise(SIGFPE); // Throw signal (to old handler)
}
#endif
#endif // __GNUC__
#endif // __linux__
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
......@@ -159,14 +153,11 @@ bool Foam::sigFpe::requested()
}
void Foam::sigFpe::set(const bool verbose)
void Foam::sigFpe::set(bool verbose)
{
if (!sigActive_ && requested())
{
bool supported = false;
#ifdef LINUX_GNUC
supported = true;
#if defined(__linux__) && defined(__GNUC__)
feenableexcept
(
......@@ -179,6 +170,7 @@ void Foam::sigFpe::set(const bool verbose)
newAction.sa_handler = sigHandler;
newAction.sa_flags = SA_NODEFER;
sigemptyset(&newAction.sa_mask);
if (sigaction(SIGFPE, &newAction, &oldAction_) < 0)
{
FatalErrorInFunction
......@@ -189,7 +181,6 @@ void Foam::sigFpe::set(const bool verbose)
sigActive_ = true;
#elif defined(sgiN32) || defined(sgiN32Gcc)
supported = true;
sigfpe_[_DIVZERO].abort=1;
sigfpe_[_OVERFL].abort=1;
......@@ -211,7 +202,6 @@ void Foam::sigFpe::set(const bool verbose)
);
sigActive_ = true;
#endif
......@@ -219,7 +209,7 @@ void Foam::sigFpe::set(const bool verbose)
{
Info<< "trapFpe: Floating point exception trapping ";
if (supported)
if (sigActive_)
{
Info<< "enabled (FOAM_SIGFPE)." << endl;
}
......@@ -231,17 +221,18 @@ void Foam::sigFpe::set(const bool verbose)
}
nanActive_ = false;
if (isTrue("FOAM_SETNAN", switchNan_))
{
#ifdef LINUX
mallocNanActive_ = true;
#ifdef __linux__
nanActive_ = true;
#endif
if (verbose)
{
Info<< "setNaN : Initialise allocated memory to NaN ";
if (mallocNanActive_)
if (nanActive_)
{
Info<< "enabled (FOAM_SETNAN)." << endl;
}
......@@ -254,10 +245,9 @@ void Foam::sigFpe::set(const bool verbose)
}
void Foam::sigFpe::unset(const bool verbose)
void Foam::sigFpe::unset(bool verbose)
{
#ifdef LINUX_GNUC
// Reset signal
#if defined(__linux__) && defined(__GNUC__)
if (sigActive_)
{
if (verbose)
......@@ -274,7 +264,7 @@ void Foam::sigFpe::unset(const bool verbose)
}
// Reset exception raising
int oldExcept = fedisableexcept
const int oldExcept = fedisableexcept
(
FE_DIVBYZERO
| FE_INVALID
......@@ -287,14 +277,18 @@ void Foam::sigFpe::unset(const bool verbose)
<< "Cannot reset SIGFPE trapping"
<< abort(FatalError);
}
sigActive_ = false;
}
#endif
#ifdef LINUX
// Disable initialization to NaN
mallocNanActive_ = false;
#endif
nanActive_ = false;
}
void Foam::sigFpe::fillNan(UList<scalar>& list)
{
list = std::numeric_limits<scalar>::signaling_NaN();
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -55,23 +55,16 @@ SourceFiles
#define sigFpe_H
#include <signal.h>
#if defined(linux) || defined(linux64) || defined(linuxIA64) || \
defined(linuxARM7) || defined(linuxPPC64) || defined(linuxPPC64le)
#define LINUX
#endif
#if defined(LINUX) && defined(__GNUC__)
#define LINUX_GNUC
#endif
#include "UList.H"
#include "scalar.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// Forward declarations
template<class T> class UList;
/*---------------------------------------------------------------------------*\
Class sigFpe Declaration
\*---------------------------------------------------------------------------*/
......@@ -91,13 +84,16 @@ class sigFpe
//- Flag to indicate floating point trapping is currently active
static bool sigActive_;
//- Flag to indicate mallocNan is currently active
static bool nanActive_;
//- Saved old signal trapping setting
static struct sigaction oldAction_;
// Static data members
#ifdef LINUX_GNUC
#if defined(__linux__) && defined(__GNUC__)
//- Handler for caught signals
static void sigHandler(int);
#endif
......@@ -105,12 +101,6 @@ class sigFpe
public:
// Public data
//- Flag to indicate mallocNan is enabled
static bool mallocNanActive_;
// Constructors
//- Construct null
......@@ -128,20 +118,32 @@ public:
// environment variable
static bool requested();
//- True if SIGFPE handling is currently active.
static inline bool active()
{
return sigActive_;
}
//- True if NaN memory initialisation is currently active.
static inline bool nanActive()
{
return nanActive_;
}
//- Activate SIGFPE signal handler when FOAM_SIGFPE is %set
// Fill memory with NaN when FOAM_SETNAN is %set
static void set(const bool verbose);
static void set(bool verbose=false);
//- Deactivate SIGFPE signal handler and NaN memory initialisation
static void unset(const bool verbose);
static void unset(bool verbose=false);
#ifdef LINUX
#ifdef __linux__
//- Malloc function which initializes to NaN
static void* mallocNan(size_t size);
#endif
//- Fill block of data with NaN
static void fillNan(UList<scalar>& lst);
//- Fill data block with NaN values
static void fillNan(UList<scalar>& list);
};
......
......@@ -47,11 +47,8 @@ void Foam::sigInt::sigHandler(int)
<< abort(FatalError);
}
// Update jobInfo file
jobInfo.signalEnd();
// Throw signal (to old handler)
raise(SIGINT);
jobInfo.signalEnd(); // Update jobInfo file
raise(SIGINT); // Throw signal (to old handler)
}
......@@ -73,7 +70,7 @@ Foam::sigInt::~sigInt()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::sigInt::set(const bool)
void Foam::sigInt::set(bool)
{
if (!sigActive_)
{
......@@ -92,7 +89,7 @@ void Foam::sigInt::set(const bool)
}
void Foam::sigInt::unset(const bool)
void Foam::sigInt::unset(bool)
{
if (sigActive_)
{
......
......@@ -87,10 +87,10 @@ public:
// Member functions
//- Activate SIGINT signal handler
static void set(const bool verbose);
static void set(bool verbose=false);
//- Deactivate SIGINT signal handler
static void unset(const bool verbose);
static void unset(bool verbose=false);
};
......
......@@ -47,13 +47,9 @@ void Foam::sigQuit::sigHandler(int)
<< abort(FatalError);
}
// Update jobInfo file
jobInfo.signalEnd();
jobInfo.signalEnd(); // Update jobInfo file
error::printStack(Perr);
// Throw signal (to old handler)
raise(SIGQUIT);
raise(SIGQUIT); // Throw signal (to old handler)
}
......@@ -75,7 +71,7 @@ Foam::sigQuit::~sigQuit()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::sigQuit::set(const bool verbose)
void Foam::sigQuit::set(bool)
{
if (!sigActive_)
{
......@@ -94,7 +90,7 @@ void Foam::sigQuit::set(const bool verbose)
}
void Foam::sigQuit::unset(const bool)
void Foam::sigQuit::unset(bool)
{
if (sigActive_)
{
......
......@@ -87,10 +87,10 @@ public:
// Member functions
//- Activate SIGQUIT signal handler
static void set(const bool verbose);
static void set(bool verbose=false);
//- Deactivate SIGQUIT signal handler
static void unset(const bool verbose);
static void unset(bool verbose=false);
};
......
......@@ -47,13 +47,9 @@ void Foam::sigSegv::sigHandler(int)
<< abort(FatalError);
}
// Update jobInfo file
jobInfo.signalEnd();
jobInfo.signalEnd(); // Update jobInfo file
error::printStack(Perr);
// Throw signal (to old handler)
raise(SIGSEGV);
raise(SIGSEGV); // Throw signal (to old handler)
}
......@@ -75,7 +71,7 @@ Foam::sigSegv::~sigSegv()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::sigSegv::set(const bool)
void Foam::sigSegv::set(bool)
{
if (!sigActive_)
{
......@@ -94,7 +90,7 @@ void Foam::sigSegv::set(const bool)
}
void Foam::sigSegv::unset(const bool)
void Foam::sigSegv::unset(bool)
{
if (sigActive_)
{
......
......@@ -87,10 +87,10 @@ public:
// Member functions
//- Activate SIGSEGV signal handler
static void set(const bool verbose);
static void set(bool verbose=false);
//- Deactivate SIGSEGV signal handler
static void unset(const bool verbose);
static void unset(bool verbose=false);
};
......
......@@ -31,15 +31,19 @@ License
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
// Signal number to catch
int sigStopAtWriteNow::signal_
int Foam::sigStopAtWriteNow::signal_
(
debug::optimisationSwitch("stopAtWriteNowSignal", -1)
Foam::debug::optimisationSwitch("stopAtWriteNowSignal", -1)
);
Foam::Time const* Foam::sigStopAtWriteNow::runTimePtr_ = nullptr;
struct sigaction Foam::sigStopAtWriteNow::oldAction_;
namespace Foam
{
// Register re-reader
class addstopAtWriteNowSignalToOpt
:
......@@ -53,8 +57,7 @@ public:
::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name)
{}
virtual ~addstopAtWriteNowSignalToOpt()
{}
virtual ~addstopAtWriteNowSignalToOpt() = default;
virtual void readData(Foam::Istream& is)
{
......@@ -73,12 +76,7 @@ addstopAtWriteNowSignalToOpt addstopAtWriteNowSignalToOpt_
"stopAtWriteNowSignal"
);
}
Foam::Time const* Foam::sigStopAtWriteNow::runTimePtr_ = nullptr;
struct sigaction Foam::sigStopAtWriteNow::oldAction_;
} // end of namespace Foam
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
......@@ -93,16 +91,15 @@ void Foam::sigStopAtWriteNow::sigHandler(int)
<< abort(FatalError);
}
// Update jobInfo file
jobInfo.signalEnd();
jobInfo.signalEnd(); // Update jobInfo file
Info<< "sigStopAtWriteNow :"
<< " setting up write and stop at end of the next iteration"
<< nl << endl;
runTimePtr_->stopAt(Time::saWriteNow);
//// Throw signal (to old handler)
//raise(signal_);
if (runTimePtr_)
{
Info<< "sigStopAtWriteNow :"
<< " setting up write and stop at end of the next iteration"
<< nl << endl;
runTimePtr_->stopAt(Time::saWriteNow);
}
}
......@@ -112,15 +109,9 @@ Foam::sigStopAtWriteNow::sigStopAtWriteNow()
{}
Foam::sigStopAtWriteNow::sigStopAtWriteNow
(
const bool verbose,
const Time& runTime
)
Foam::sigStopAtWriteNow::sigStopAtWriteNow(const Time& runTime, bool verbose)
{
// Store runTime
runTimePtr_ = &runTime;
runTimePtr_ = &runTime; // Store runTime
set(verbose);
}
......@@ -144,7 +135,7 @@ Foam::sigStopAtWriteNow::~sigStopAtWriteNow()
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::sigStopAtWriteNow::set(const bool verbose)
void Foam::sigStopAtWriteNow::set(bool verbose)
{
if (signal_ > 0)
{
......
......@@ -83,7 +83,7 @@ public:
sigStopAtWriteNow();
//- Construct from components
sigStopAtWriteNow(const bool verbose, const Time& runTime);
sigStopAtWriteNow(const Time& runTime, bool verbose=false);
//- Destructor
......@@ -93,7 +93,7 @@ public:
// Member functions
//- (re)set signal catcher
static void set(const bool verbose);
static void set(bool verbose=false);
//- Is active?
bool active() const;
......
......@@ -31,14 +31,19 @@ License
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
namespace Foam
{
// Signal number to catch
int sigWriteNow::signal_
int Foam::sigWriteNow::signal_
(
debug::optimisationSwitch("writeNowSignal", -1)
Foam::debug::optimisationSwitch("writeNowSignal", -1)
);
Foam::Time* Foam::sigWriteNow::runTimePtr_ = nullptr;
struct sigaction Foam::sigWriteNow::oldAction_;
namespace Foam
{
// Register re-reader
class addwriteNowSignalToOpt
......@@ -53,8 +58,7 @@ public:
::Foam::simpleRegIOobject(Foam::debug::addOptimisationObject, name)
{}
virtual ~addwriteNowSignalToOpt()
{}
virtual ~addwriteNowSignalToOpt() = default;