37#ifdef FOAM_USE_INOTIFY
39 #include <sys/inotify.h>
40 #include <sys/ioctl.h>
42 #define EVENT_SIZE ( sizeof (struct inotify_event) )
43 #define EVENT_LEN (EVENT_SIZE + 16)
44 #define EVENT_BUF_LEN ( 1024 * EVENT_LEN )
55 { fileState::UNMODIFIED,
"unmodified" },
56 { fileState::MODIFIED,
"modified" },
57 { fileState::DELETED,
"deleted" },
66 class reduceFileStates
69 unsigned int operator()(
const unsigned int x,
const unsigned int y)
74 unsigned int mask = 3u;
75 unsigned int shift = 0;
76 unsigned int result = 0;
81 unsigned int xState = (
x & mask) >> shift;
82 unsigned int yState = (
y & mask) >> shift;
86 unsigned int state =
min(xState, yState);
87 result |= (state << shift);
97 class combineReduceFileStates
109 class fileMonitorWatcher
139 #ifdef FOAM_USE_INOTIFY
146 static bool hasWarned =
false;
151 <<
"Failed allocating an inotify descriptor : "
153 <<
" Please increase the number of allowable "
154 <<
"inotify instances" <<
endl
155 <<
" (/proc/sys/fs/inotify/max_user_instances"
156 <<
" on Linux)" <<
endl
157 <<
" , switch off runTimeModifiable." <<
endl
158 <<
" or compile this file without "
159 <<
"FOAM_USE_INOTIFY"
160 <<
" to use time stamps instead of inotify." <<
endl
161 <<
" Continuing without additional file"
168 <<
"You selected inotify but this file was compiled"
169 <<
" without FOAM_USE_INOTIFY"
170 <<
" Please select another fileModification test method"
183 #ifdef FOAM_USE_INOTIFY
193 <<
"Failed deleting directory watch "
211 #ifdef FOAM_USE_INOTIFY
218 label dirWatchID = -1;
221 dirWatchID = inotify_add_watch
231 <<
"Failed adding watch " << watchFd
232 <<
" to directory " << fName <<
" due to "
233 <<
string(strerror(errno))
242 <<
"Problem adding watch " << watchFd
243 <<
" to file " << fName
257 <<
"Problem adding watch " << watchFd
258 <<
" to file " << fName
292void Foam::fileMonitor::checkFiles()
const
296 #ifdef FOAM_USE_INOTIFY
298 char buffer[EVENT_BUF_LEN];
302 struct timeval zeroTimeout = {0, 0};
308 FD_SET(watcher_->inotifyFd_, &fdSet);
312 watcher_->inotifyFd_+1,
322 <<
"Problem in issuing select."
325 else if (FD_ISSET(watcher_->inotifyFd_, &fdSet))
330 watcher_->inotifyFd_,
338 <<
"read of " << watcher_->inotifyFd_
339 <<
" failed with " << label(nBytes)
347 const struct inotify_event* inotifyEvent =
348 reinterpret_cast<const struct inotify_event*
>
361 (inotifyEvent->mask & IN_CLOSE_WRITE)
366 forAll(watcher_->dirWatches_, i)
368 label
id = watcher_->dirWatches_[i];
371 id == inotifyEvent->wd
372 && inotifyEvent->name == watcher_->dirFiles_[i]
381 i += EVENT_SIZE + inotifyEvent->len;
394 forAll(watcher_->lastMod_, watchFd)
396 double oldTime = watcher_->lastMod_[watchFd];
400 const fileName& fName = watchFile_[watchFd];
405 localState_[watchFd] =
DELETED;
429 useInotify_(useInotify),
434 watcher_(new fileMonitorWatcher(useInotify_, 20))
452 Pout<<
"fileMonitor : adding watch on file " << fName <<
endl;
457 if (freeWatchFds_.size())
459 watchFd = freeWatchFds_.remove();
463 watchFd = state_.size();
466 watcher_->addWatch(watchFd, fName);
470 Pout<<
"fileMonitor : added watch " << watchFd <<
" on file "
477 <<
"could not add watch for file " << fName <<
endl;
481 localState_(watchFd) = UNMODIFIED;
482 state_(watchFd) = UNMODIFIED;
483 watchFile_(watchFd) = fName;
493 Pout<<
"fileMonitor : removing watch " << watchFd <<
" on file "
494 << watchFile_[watchFd] <<
endl;
497 freeWatchFds_.appendUniq(watchFd);
499 return watcher_->removeWatch(watchFd);
505 return watchFile_[watchFd];
512 return state_[watchFd];
518 const bool masterOnly,
531 PackedList<2> stats(state_.size(), MODIFIED);
539 static_cast<unsigned int>(localState_[watchFd])
549 if (stats.storage().size() == 1)
561 if (stats.storage().size() == 1)
564 reduce(stats.storage()[0], reduceFileStates());
571 combineReduceFileStates()
581 unsigned int stat = stats[watchFd];
582 state_[watchFd] = fileState(stat);
587 if (state_[watchFd] != localState_[watchFd])
591 Pout<<
"fileMonitor : Delaying reading "
592 << watchFile_[watchFd]
593 <<
" due to inconsistent "
594 "file time-stamps between processors"
599 <<
"Delaying reading " << watchFile_[watchFd]
600 <<
" due to inconsistent "
601 "file time-stamps between processors" <<
endl;
608 state_ = localState_;
615 state_[watchFd] = UNMODIFIED;
616 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.
void setCapacity(const label len)
Alter the size of the underlying storage.
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.
static void broadcast(Type &value, const label comm=UPstream::worldComm)
static void listCombineGather(const List< commsStruct > &comms, List< T > &values, const CombineOp &cop, const int tag, const label comm)
void size(const label n)
Older name for setAddressableSize.
void operator()(unsigned int &x, const unsigned int y) const
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)
~fileMonitorWatcher()
Remove all watches.
bool removeWatch(const label watchFd)
DynamicList< fileName > dirFiles_
Checking for changes to files.
fileState getState(const label watchFd) const
Check state using handle.
static const Enum< fileState > fileStateNames_
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_.
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.
Reduction operator for PackedList of fileState.
unsigned int operator()(const unsigned int x, const unsigned int y) const
splitCell * master() const
A class for handling character strings derived from std::string.
virtual void addWatch()
Add file watch on object (READ_IF_MODIFIED)
#define defineTypeNameAndDebug(Type, DebugSwitch)
Define the typeName and debug information.
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()
#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)
bool read(const char *buf, int32_t &val)
Same as readInt32.
Ostream & endl(Ostream &os)
Add newline and flush stream.
void reduce(const List< UPstream::commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
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.
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.