Go to the documentation of this file.
38 #undef FOAM_USE_INOTIFY
41 #ifdef FOAM_USE_INOTIFY
43 #include <sys/inotify.h>
44 #include <sys/ioctl.h>
46 #define EVENT_SIZE ( sizeof (struct inotify_event) )
47 #define EVENT_LEN (EVENT_SIZE + 16)
48 #define EVENT_BUF_LEN ( 1024 * EVENT_LEN )
59 { fileState::UNMODIFIED,
"unmodified" },
60 { fileState::MODIFIED,
"modified" },
61 { fileState::DELETED,
"deleted" },
73 unsigned int operator()(
const unsigned int x,
const unsigned int y)
78 unsigned int mask = 3u;
79 unsigned int shift = 0;
80 unsigned int result = 0;
85 unsigned int xState = (
x & mask) >> shift;
86 unsigned int yState = (
y & mask) >> shift;
90 unsigned int state =
min(xState, yState);
91 result |= (state << shift);
143 #ifdef FOAM_USE_INOTIFY
150 static bool hasWarned =
false;
155 <<
"Failed allocating an inotify descriptor : "
157 <<
" Please increase the number of allowable "
158 <<
"inotify instances" <<
endl
159 <<
" (/proc/sys/fs/inotify/max_user_instances"
160 <<
" on Linux)" <<
endl
161 <<
" , switch off runTimeModifiable." <<
endl
162 <<
" or compile this file without "
163 <<
"FOAM_USE_INOTIFY"
164 <<
" to use time stamps instead of inotify." <<
endl
165 <<
" Continuing without additional file"
172 <<
"You selected inotify but this file was compiled"
173 <<
" without FOAM_USE_INOTIFY"
174 <<
" Please select another fileModification test method"
187 #ifdef FOAM_USE_INOTIFY
197 <<
"Failed deleting directory watch "
215 #ifdef FOAM_USE_INOTIFY
222 label dirWatchID = -1;
225 dirWatchID = inotify_add_watch
235 <<
"Failed adding watch " << watchFd
236 <<
" to directory " << fName <<
" due to "
237 <<
string(strerror(errno))
246 <<
"Problem adding watch " << watchFd
247 <<
" to file " << fName
261 <<
"Problem adding watch " << watchFd
262 <<
" to file " << fName
296 void Foam::fileMonitor::checkFiles()
const
300 #ifdef FOAM_USE_INOTIFY
302 char buffer[EVENT_BUF_LEN];
306 struct timeval zeroTimeout = {0, 0};
312 FD_SET(watcher_->inotifyFd_, &fdSet);
316 watcher_->inotifyFd_+1,
326 <<
"Problem in issuing select."
329 else if (FD_ISSET(watcher_->inotifyFd_, &fdSet))
334 watcher_->inotifyFd_,
342 <<
"read of " << watcher_->inotifyFd_
343 <<
" failed with " << label(nBytes)
351 const struct inotify_event* inotifyEvent =
352 reinterpret_cast<const struct inotify_event*
>
365 (inotifyEvent->mask & IN_CLOSE_WRITE)
370 forAll(watcher_->dirWatches_, i)
372 label
id = watcher_->dirWatches_[i];
375 id == inotifyEvent->wd
376 && inotifyEvent->name == watcher_->dirFiles_[i]
385 i += EVENT_SIZE + inotifyEvent->len;
398 forAll(watcher_->lastMod_, watchFd)
400 double oldTime = watcher_->lastMod_[watchFd];
404 const fileName& fName = watchFile_[watchFd];
409 localState_[watchFd] =
DELETED;
431 Foam::fileMonitor::fileMonitor(
const bool useInotify)
433 useInotify_(useInotify),
456 Pout<<
"fileMonitor : adding watch on file " << fName <<
endl;
461 if (freeWatchFds_.size())
463 watchFd = freeWatchFds_.remove();
467 watchFd = state_.size();
470 watcher_->addWatch(watchFd, fName);
474 Pout<<
"fileMonitor : added watch " << watchFd <<
" on file "
481 <<
"could not add watch for file " << fName <<
endl;
485 localState_(watchFd) = UNMODIFIED;
486 state_(watchFd) = UNMODIFIED;
487 watchFile_(watchFd) = fName;
497 Pout<<
"fileMonitor : removing watch " << watchFd <<
" on file "
498 << watchFile_[watchFd] <<
endl;
501 if (!freeWatchFds_.found(watchFd))
503 freeWatchFds_.append(watchFd);
505 return watcher_->removeWatch(watchFd);
511 return watchFile_[watchFd];
518 return state_[watchFd];
524 const bool masterOnly,
545 static_cast<unsigned int>(localState_[watchFd])
555 if (stats.storage().size() == 1)
567 if (stats.storage().size() == 1)
587 unsigned int stat = stats[watchFd];
593 if (state_[watchFd] != localState_[watchFd])
597 Pout<<
"fileMonitor : Delaying reading "
598 << watchFile_[watchFd]
599 <<
" due to inconsistent "
600 "file time-stamps between processors"
605 <<
"Delaying reading " << watchFile_[watchFd]
606 <<
" due to inconsistent "
607 "file time-stamps between processors" <<
endl;
614 state_ = localState_;
621 state_[watchFd] = UNMODIFIED;
622 localState_[watchFd] = UNMODIFIED;
int debug
Static debugging option.
Combine operator for PackedList of fileState.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
A class for handling file names.
static std::string path(const std::string &str)
Return directory path name (part before last /)
const fileName & getFile(const label watchFd) const
Get name of file being watched.
bool removeWatch(const label watchFd)
Remove file to watch. Return true if successful.
static float fileModificationSkew
DynamicList< fileName > dirFiles_
static std::string name(const std::string &str)
Return basename (part beyond last /), including its extension.
bool read(const char *buf, int32_t &val)
Same as readInt32.
fileMonitorWatcher(const bool useInotify, const label sz=20)
Initialise inotify.
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()
label addWatch(const fileName &)
Add file to watch. Return watch descriptor.
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
fileState
Enumeration defining the file state.
Ostream & endl(Ostream &os)
Add newline and flush stream.
A class for handling character strings derived from std::string.
prefixOSstream Pout
An Ostream wrapper for parallel output to std::cout.
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
#define forAll(list, i)
Loop across all elements in list.
~fileMonitor()
Destructor.
int inotifyFd_
File descriptor for the inotify instance.
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
static const Enum< fileState > fileStateNames_
void operator()(unsigned int &x, const unsigned int y) const
double highResLastModified(const fileName &, const bool followLink=true)
Return time of last file modification.
errorManip< error > abort(error &err)
Inter-processor communication reduction functions.
Reduction operator for PackedList of fileState.
errorManipArg< error, int > exit(error &err, const int errNo=1)
void setCapacity(const label nElem)
Alter the size of the underlying storage.
DynamicList< double > lastMod_
From watch descriptor to modified time.
static bool master(const label communicator=0)
Am I the master process.
static void listCombineGather(const List< commsStruct > &comms, List< T > &Value, const CombineOp &cop, const int tag, const label comm)
DynamicList< label > dirWatches_
Current watchIDs and corresponding directory id.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
fileState getState(const label watchFd) const
Check state using handle.
void setUnmodified(const label watchFd)
Reset state (e.g. after having read it) using handle.
bool addWatch(const label watchFd, const fileName &fName)
void updateStates(const bool masterOnly, const bool syncPar) const
Check state of all files. Updates state_.
bool removeWatch(const label watchFd)
Internal tracking via stat(3p) or inotify(7)
unsigned int operator()(const unsigned int x, const unsigned int y) const
static void listCombineScatter(const List< commsStruct > &comms, List< T > &Value, const int tag, const label comm)
Scatter data. Reverse of combineGather.
defineTypeNameAndDebug(combustionModel, 0)
#define WarningInFunction
Report a warning using Foam::Warning.
~fileMonitorWatcher()
Remove all watches.
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?