34 #ifdef FOAM_USE_INOTIFY
36 #include <sys/inotify.h>
37 #include <sys/ioctl.h>
39 #define EVENT_SIZE ( sizeof (struct inotify_event) )
40 #define EVENT_LEN (EVENT_SIZE + 16)
41 #define EVENT_BUF_LEN ( 1024 * EVENT_LEN )
64 unsigned int operator()(
const unsigned int x,
const unsigned int y)
69 unsigned int mask = 3u;
70 unsigned int shift = 0;
71 unsigned int result = 0;
76 unsigned int xState = (
x & mask) >> shift;
77 unsigned int yState = (
y & mask) >> shift;
81 unsigned int state =
min(xState, yState);
82 result |= (state << shift);
134 #ifdef FOAM_USE_INOTIFY
141 static bool hasWarned =
false;
146 <<
"Failed allocating an inotify descriptor : "
148 <<
" Please increase the number of allowable "
149 <<
"inotify instances" <<
endl
150 <<
" (/proc/sys/fs/inotify/max_user_instances"
151 <<
" on Linux)" <<
endl
152 <<
" , switch off runTimeModifiable." <<
endl
153 <<
" or compile this file without "
154 <<
"FOAM_USE_INOTIFY"
155 <<
" to use time stamps instead of inotify." <<
endl
156 <<
" Continuing without additional file"
163 <<
"You selected inotify but this file was compiled"
164 <<
" without FOAM_USE_INOTIFY"
165 <<
" Please select another fileModification test method"
178 #ifdef FOAM_USE_INOTIFY
188 <<
"Failed deleting directory watch "
206 #ifdef FOAM_USE_INOTIFY
213 label dirWatchID = -1;
216 dirWatchID = inotify_add_watch
226 <<
"Failed adding watch " << watchFd
227 <<
" to directory " << fName <<
" due to "
228 <<
string(strerror(errno))
237 <<
"Problem adding watch " << watchFd
238 <<
" to file " << fName
252 <<
"Problem adding watch " << watchFd
253 <<
" to file " << fName
287 void Foam::fileMonitor::checkFiles()
const
291 #ifdef FOAM_USE_INOTIFY
293 char buffer[EVENT_BUF_LEN];
297 struct timeval zeroTimeout = {0, 0};
303 FD_SET(watcher_->inotifyFd_, &fdSet);
307 watcher_->inotifyFd_+1,
317 <<
"Problem in issuing select."
320 else if (FD_ISSET(watcher_->inotifyFd_, &fdSet))
325 watcher_->inotifyFd_,
333 <<
"read of " << watcher_->inotifyFd_
334 <<
" failed with " <<
label(nBytes)
342 const struct inotify_event* inotifyEvent =
343 reinterpret_cast<const struct inotify_event*
>
356 (inotifyEvent->mask & IN_CLOSE_WRITE)
361 forAll(watcher_->dirWatches_, i)
363 label id = watcher_->dirWatches_[i];
366 id == inotifyEvent->wd
367 && inotifyEvent->name == watcher_->dirFiles_[i]
376 i += EVENT_SIZE + inotifyEvent->len;
389 forAll(watcher_->lastMod_, watchFd)
391 double oldTime = watcher_->lastMod_[watchFd];
395 const fileName& fName = watchFile_[watchFd];
400 localState_[watchFd] =
DELETED;
424 useInotify_(useInotify),
447 label sz = freeWatchFds_.size();
451 watchFd = freeWatchFds_[sz-1];
452 freeWatchFds_.setSize(sz-1);
456 watchFd = state_.size();
459 watcher_->addWatch(watchFd, fName);
463 Pout<<
"fileMonitor : added watch " << watchFd <<
" on file "
470 <<
"could not add watch for file " << fName <<
endl;
474 localState_(watchFd) = UNMODIFIED;
475 state_(watchFd) = UNMODIFIED;
476 watchFile_(watchFd) = fName;
486 Pout<<
"fileMonitor : removing watch " << watchFd <<
" on file "
487 << watchFile_[watchFd] <<
endl;
490 freeWatchFds_.append(watchFd);
491 return watcher_->removeWatch(watchFd);
497 return watchFile_[watchFd];
504 return state_[watchFd];
510 const bool masterOnly,
528 stats[watchFd] =
static_cast<unsigned int>
572 unsigned int stat = stats[watchFd];
578 if (state_[watchFd] != localState_[watchFd])
582 Pout<<
"fileMonitor : Delaying reading "
583 << watchFile_[watchFd]
584 <<
" due to inconsistent "
585 "file time-stamps between processors"
590 <<
"Delaying reading " << watchFile_[watchFd]
591 <<
" due to inconsistent "
592 "file time-stamps between processors" <<
endl;
599 state_ = localState_;
606 state_[watchFd] = UNMODIFIED;
607 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.
#define forAll(list, i)
Loop across all elements in list.
void setCapacity(const label)
Alter the size of the underlying storage.
void size(const label)
Override size to be inconsistent with allocated storage.
Initialise the NamedEnum HashTable from the static list of names.
List< unsigned int > & storage()
Return the underlying packed storage.
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
static void listCombineGather(const List< commsStruct > &comms, List< T > &Value, const CombineOp &cop, const int tag, const label comm)
static void listCombineScatter(const List< commsStruct > &comms, List< T > &Value, const int tag, const label comm)
Scatter data. Reverse of combineGather.
static bool master(const label communicator=0)
Am I the master process.
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)
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.
static const NamedEnum< fileState, 3 > fileStateNames_
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.
fileMonitor(const bool useInotify)
Construct null.
bool removeWatch(const label watchFd)
Remove file to watch. Return true if successful.
A class for handling file names.
word name() const
Return file name (part beyond last /)
fileName path() const
Return directory path name (part before last /)
Reduction operator for PackedList of fileState.
unsigned int operator()(const unsigned int x, const unsigned int y) const
static float fileModificationSkew
A class for handling character strings derived from std::string.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
#define WarningInFunction
Report a warning using Foam::Warning.
errorManipArg< error, int > exit(error &err, const int errNo=1)
bool read(const char *, int32_t &)
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Ostream & endl(Ostream &os)
Add newline and flush stream.
double highResLastModified(const fileName &, const bool checkVariants=true, const bool followLink=true)
Return time of last file modification.
errorManip< error > abort(error &err)
layerAndWeight min(const layerAndWeight &a, const layerAndWeight &b)
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
defineTypeNameAndDebug(combustionModel, 0)
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
prefixOSstream Pout(cout, "Pout")