40#ifdef FOAM_USE_INOTIFY
42 #include <sys/inotify.h>
43 #include <sys/ioctl.h>
45 #define EVENT_SIZE ( sizeof (struct inotify_event) )
46 #define EVENT_LEN (EVENT_SIZE + 16)
47 #define EVENT_BUF_LEN ( 1024 * EVENT_LEN )
58 { fileState::UNMODIFIED,
"unmodified" },
59 { fileState::MODIFIED,
"modified" },
60 { fileState::DELETED,
"deleted" },
72 unsigned int operator()(
const unsigned int x,
const unsigned int y)
77 unsigned int mask = 3u;
78 unsigned int shift = 0;
79 unsigned int result = 0;
84 unsigned int xState = (
x & mask) >> shift;
85 unsigned int yState = (
y & mask) >> shift;
89 unsigned int state =
min(xState, yState);
90 result |= (state << shift);
102 void operator()(
unsigned int&
x,
const unsigned int y)
const
141 #ifdef FOAM_USE_INOTIFY
148 static bool hasWarned =
false;
153 <<
"Failed allocating an inotify descriptor : "
155 <<
" Please increase the number of allowable "
156 <<
"inotify instances" <<
endl
157 <<
" (/proc/sys/fs/inotify/max_user_instances"
158 <<
" on Linux)" <<
endl
159 <<
" , switch off runTimeModifiable." <<
endl
160 <<
" or compile this file without "
161 <<
"FOAM_USE_INOTIFY"
162 <<
" to use time stamps instead of inotify." <<
endl
163 <<
" Continuing without additional file"
170 <<
"You selected inotify but this file was compiled"
171 <<
" without FOAM_USE_INOTIFY"
172 <<
" Please select another fileModification test method"
185 #ifdef FOAM_USE_INOTIFY
195 <<
"Failed deleting directory watch "
204 inline bool addWatch(
const label watchFd,
const fileName& fName)
213 #ifdef FOAM_USE_INOTIFY
220 label dirWatchID = -1;
223 dirWatchID = inotify_add_watch
233 <<
"Failed adding watch " << watchFd
234 <<
" to directory " << fName <<
" due to "
235 <<
string(strerror(errno))
244 <<
"Problem adding watch " << watchFd
245 <<
" to file " << fName
259 <<
"Problem adding watch " << watchFd
260 <<
" to file " << fName
294void Foam::fileMonitor::checkFiles()
const
298 #ifdef FOAM_USE_INOTIFY
300 char buffer[EVENT_BUF_LEN];
304 struct timeval zeroTimeout = {0, 0};
310 FD_SET(watcher_->inotifyFd_, &fdSet);
314 watcher_->inotifyFd_+1,
324 <<
"Problem in issuing select."
327 else if (FD_ISSET(watcher_->inotifyFd_, &fdSet))
332 watcher_->inotifyFd_,
340 <<
"read of " << watcher_->inotifyFd_
341 <<
" failed with " << label(nBytes)
349 const struct inotify_event* inotifyEvent =
350 reinterpret_cast<const struct inotify_event*
>
363 (inotifyEvent->mask & IN_CLOSE_WRITE)
368 forAll(watcher_->dirWatches_, i)
370 label
id = watcher_->dirWatches_[i];
373 id == inotifyEvent->wd
374 && inotifyEvent->name == watcher_->dirFiles_[i]
383 i += EVENT_SIZE + inotifyEvent->len;
396 forAll(watcher_->lastMod_, watchFd)
398 double oldTime = watcher_->lastMod_[watchFd];
402 const fileName& fName = watchFile_[watchFd];
407 localState_[watchFd] =
DELETED;
429Foam::fileMonitor::fileMonitor(
const bool useInotify)
431 useInotify_(useInotify),
454 Pout<<
"fileMonitor : adding watch on file " << fName <<
endl;
457 label watchFd = state_.size();
459 if (!freeWatchFds_.empty())
461 watchFd = freeWatchFds_.back();
462 freeWatchFds_.pop_back();
465 watcher_->addWatch(watchFd, fName);
469 Pout<<
"fileMonitor : added watch " << watchFd <<
" on file "
476 <<
"could not add watch for file " << fName <<
endl;
480 localState_(watchFd) = UNMODIFIED;
481 state_(watchFd) = UNMODIFIED;
482 watchFile_(watchFd) = fName;
492 Pout<<
"fileMonitor : removing watch " << watchFd <<
" on file "
493 << watchFile_[watchFd] <<
endl;
496 freeWatchFds_.push_uniq(watchFd);
498 return watcher_->removeWatch(watchFd);
504 return watchFile_[watchFd];
511 return state_[watchFd];
517 const bool masterOnly,
538 static_cast<unsigned int>(localState_[watchFd])
580 unsigned int stat = stats[watchFd];
586 if (state_[watchFd] != localState_[watchFd])
590 Pout<<
"fileMonitor : Delaying reading "
591 << watchFile_[watchFd]
592 <<
" due to inconsistent "
593 "file time-stamps between processors"
598 <<
"Delaying reading " << watchFile_[watchFd]
599 <<
" due to inconsistent "
600 "file time-stamps between processors" <<
endl;
607 state_ = localState_;
614 state_[watchFd] = UNMODIFIED;
615 localState_[watchFd] = UNMODIFIED;
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Inter-processor communication reduction functions.
A 1D vector of objects of type <T> that resizes itself as necessary to accept the new objects.
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
static float fileModificationSkew
Time skew (seconds) for file modification checks.
A dynamic list of packed unsigned integers, with the number of bits per item specified by the <Width>...
const List< block_type > & storage() const noexcept
Return the underlying storage blocks.
bool set(const label i, unsigned int val=~0u)
Set value at index i, default value set is the max_value.
static void listCombineGather(UList< T > &values, CombineOp cop, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm)
Forwards to Pstream::listGather with an in-place cop.
void size(const label n)
Older name for setAddressableSize.
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
@ broadcast
broadcast [MPI]
Internal tracking via stat(3p) or inotify(7).
fileMonitorWatcher(const bool useInotify, const label sz=20)
Initialise inotify.
DynamicList< double > lastMod_
From watch descriptor to modified time.
int inotifyFd_
File descriptor for the inotify instance.
DynamicList< label > dirWatches_
Current watchIDs and corresponding directory id.
bool addWatch(const label watchFd, const fileName &fName)
DynamicList< fileName > dirFiles_
~fileMonitorWatcher()
Remove all watches.
bool removeWatch(const label watchFd)
Checking for changes to files.
fileState getState(const label watchFd) const
Check state using handle.
label addWatch(const fileName &)
Add file to watch. Return watch descriptor.
void setUnmodified(const label watchFd)
Reset state (e.g. after having read it) using handle.
~fileMonitor()
Destructor.
const fileName & getFile(const label watchFd) const
Get name of file being watched.
void updateStates(const bool masterOnly, const bool syncPar) const
Check state of all files. Updates state_.
static int debug
Named/registered debug switch: 'fileMonitor'.
static const Enum< fileState > fileStateNames_
fileState
Enumeration defining the file state.
bool removeWatch(const label watchFd)
Remove file to watch. Return true if successful.
A class for handling file names.
static std::string path(const std::string &str)
Return directory path name (part before last /).
static std::string name(const std::string &str)
Return basename (part beyond last /), including its extension.
A class for handling character strings derived from std::string.
Info<< "Creating field kinetic energy K\n"<< endl;volScalarField K("K", 0.5 *magSqr(U));if(U.nOldTimes()){ volVectorField *Uold=&U.oldTime();volScalarField *Kold=&K.oldTime(); *Kold==0.5 *magSqr(*Uold);while(Uold->nOldTimes()) { Uold=&Uold-> oldTime()
Macro definitions for debug switches.
#define registerDebugSwitchWithName(Type, Tag, Name)
Define the debug information, lookup as Name.
#define defineDebugSwitchWithName(Type, Name, Value)
Define the debug information, lookup as Name.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
#define WarningInFunction
Report a warning using Foam::Warning.
List< bool > select(const label n, const labelUList &locations)
Construct a selection list of bools (all false) with the given pre-size, subsequently add specified l...
Namespace for handling debugging switches.
bool read(const char *buf, int32_t &val)
Same as readInt32.
Ostream & endl(Ostream &os)
Add newline and flush stream.
void reduce(T &value, BinaryOp bop, const int tag=UPstream::msgType(), const int communicator=UPstream::worldComm)
Reduce inplace (cf. MPI Allreduce).
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
errorManip< error > abort(error &err)
double highResLastModified(const fileName &, const bool followLink=true)
Return time of last file modification.
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
errorManipArg< error, int > exit(error &err, const int errNo=1)
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
#define forAll(list, i)
Loop across all elements in list.
Combine operator for PackedList of fileState.
void operator()(unsigned int &x, const unsigned int y) const
Reduction operator for PackedList of fileState.
unsigned int operator()(const unsigned int x, const unsigned int y) const