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
296void 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;
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 freeWatchFds_.appendUniq(watchFd);
503 return watcher_->removeWatch(watchFd);
509 return watchFile_[watchFd];
516 return state_[watchFd];
522 const bool masterOnly,
543 static_cast<unsigned int>(localState_[watchFd])
553 if (stats.
storage().size() == 1)
565 if (stats.
storage().size() == 1)
585 unsigned int stat = stats[watchFd];
591 if (state_[watchFd] != localState_[watchFd])
595 Pout<<
"fileMonitor : Delaying reading "
596 << watchFile_[watchFd]
597 <<
" due to inconsistent "
598 "file time-stamps between processors"
603 <<
"Delaying reading " << watchFile_[watchFd]
604 <<
" due to inconsistent "
605 "file time-stamps between processors" <<
endl;
612 state_ = localState_;
619 state_[watchFd] = UNMODIFIED;
620 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.
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.
A dynamic list of packed unsigned integers, with the number of bits per item specified by the <Width>...
bool set(const label i, unsigned int val=~0u)
Set value at index i, default value set is the max_value.
const List< unsigned int > & storage() const
Return the underlying storage blocks.
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.
Combine operator for PackedList of fileState.
void operator()(unsigned int &x, const unsigned int y) const
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)
~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.