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 ) 71 unsigned int operator()(
const unsigned int x,
const unsigned int y)
76 unsigned int mask = 3u;
77 unsigned int shift = 0;
78 unsigned int result = 0;
83 unsigned int xState = (x & mask) >> shift;
84 unsigned int yState = (y & mask) >> shift;
88 unsigned int state =
min(xState, yState);
89 result |= (state << shift);
136 useInotify_(useInotify),
141 #ifdef FOAM_USE_INOTIFY 142 inotifyFd_ = inotify_init();
148 static bool hasWarned =
false;
152 WarningIn(
"fileMonitorWatcher(const bool, const label)")
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" 169 FatalErrorIn(
"fileMonitorWatcher(const bool, const label)")
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 186 if (useInotify_ && inotifyFd_ >= 0)
190 if (dirWatches_[i] >= 0)
192 if (inotify_rm_watch(inotifyFd_,
int(dirWatches_[i])))
195 <<
"Failed deleting directory watch " 196 << dirWatches_[i] <<
endl;
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))
240 if (watchFd < dirWatches_.
size() && dirWatches_[watchFd] != -1)
244 <<
"Problem adding watch " << watchFd
245 <<
" to file " << fName
249 dirWatches_(watchFd) = dirWatchID;
250 dirFiles_(watchFd) = fName.
name();
255 if (watchFd < lastMod_.
size() && lastMod_[watchFd] != 0)
259 <<
"Problem adding watch " << watchFd
260 <<
" to file " << fName
279 dirWatches_[watchFd] = -1;
283 lastMod_[watchFd] = 0;
294 void 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]
378 localState_[i] = MODIFIED;
383 i += EVENT_SIZE + inotifyEvent->len;
396 forAll(watcher_->lastMod_, watchFd)
398 time_t
oldTime = watcher_->lastMod_[watchFd];
402 const fileName& fName = watchFile_[watchFd];
407 localState_[watchFd] = DELETED;
413 localState_[watchFd] = MODIFIED;
417 localState_[watchFd] = UNMODIFIED;
429 Foam::fileMonitor::fileMonitor(
const bool useInotify)
431 useInotify_(useInotify),
458 watchFd = freeWatchFds_[sz-1];
463 watchFd = state_.
size();
466 watcher_->addWatch(watchFd, fName);
470 Pout<<
"fileMonitor : added watch " << watchFd <<
" on file " 476 WarningIn(
"fileMonitor::addWatch(const fileName&)")
477 <<
"could not add watch for file " << fName <<
endl;
483 watchFile_(watchFd) = fName;
493 Pout<<
"fileMonitor : removing watch " << watchFd <<
" on file " 494 << watchFile_[watchFd] <<
endl;
497 freeWatchFds_.
append(watchFd);
498 return watcher_->removeWatch(watchFd);
504 return watchFile_[watchFd];
511 return state_[watchFd];
517 const bool masterOnly,
535 stats[watchFd] =
static_cast<unsigned int> 547 if (stats.storage().size() == 1)
559 if (stats.storage().size() == 1)
579 unsigned int stat = stats[watchFd];
585 if (state_[watchFd] != localState_[watchFd])
589 Pout<<
"fileMonitor : Delaying reading " 590 << watchFile_[watchFd]
591 <<
" due to inconsistent " 592 "file time-stamps between processors" 598 "fileMonitor::updateStates" 599 "(const bool, const bool) const" 600 ) <<
"Delaying reading " << watchFile_[watchFd]
601 <<
" due to inconsistent " 602 "file time-stamps between processors" <<
endl;
609 state_ = localState_;
621 watcher_->lastMod_[watchFd] =
lastModified(watchFile_[watchFd]);
void reduce(const List< UPstream::commsStruct > &comms, T &Value, const BinaryOp &bop, const int tag, const label comm)
static const NamedEnum< fileState, 3 > fileStateNames_
fileState getState(const label watchFd) const
Check state using handle.
~fileMonitor()
Destructor.
word name() const
Return file name (part beyond last /)
fileMonitorWatcher(const bool useInotify, const label sz=20)
Initialise inotify.
void setCapacity(const label)
Alter the size of the underlying storage.
~fileMonitorWatcher()
Remove all watches.
const fileName & getFile(const label watchFd) const
Get name of file being watched.
bool addWatch(const label watchFd, const fileName &fName)
void updateStates(const bool masterOnly, const bool syncPar) const
Check state of all files. Updates state_.
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
A class for handling character strings derived from std::string.
void size(const label)
Override size to be inconsistent with allocated storage.
DynamicList< T, SizeInc, SizeMult, SizeDiv > & append(const T &)
Append an element at the end of the list.
errorManipArg< error, int > exit(error &err, const int errNo=1)
label addWatch(const fileName &)
Add file to watch. Return watch descriptor.
bool removeWatch(const label watchFd)
Remove file to watch. Return true if successful.
fileName path() const
Return directory path name (part before last /)
Combine operator for PackedList of fileState.
DynamicList< fileName > dirFiles_
DynamicList< time_t > lastMod_
From watch descriptor to modified time.
fileState
Enumeration defining the file state.
unsigned int operator()(const unsigned int x, const unsigned int y) const
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
DynamicList< label > dirWatches_
Current watchIDs and corresponding directory id.
Ostream & endl(Ostream &os)
Add newline and flush stream.
#define WarningIn(functionName)
Report a warning using Foam::Warning.
void operator()(unsigned int &x, const unsigned int y) const
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 int fileModificationSkew
void setSize(const label)
Alter the addressed list size.
static void scatter(const List< commsStruct > &comms, T &Value, const int tag, const label comm)
Scatter data. Distribute without modification. Reverse of gather.
errorManip< error > abort(error &err)
Initialise the NamedEnum HashTable from the static list of names.
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
int inotifyFd_
File descriptor for the inotify instance.
A class for handling file names.
static bool master(const label communicator=0)
Am I the master process.
bool read(const char *, int32_t &)
time_t lastModified(const fileName &)
Return time of last file modification.
dimensioned< Type > min(const dimensioned< Type > &, const dimensioned< Type > &)
void setUnmodified(const label watchFd)
Reset state (e.g. after having read it) using handle.
defineTypeNameAndDebug(combustionModel, 0)
Internal tracking via stat(3p) or inotify(7)
bool removeWatch(const label watchFd)
prefixOSstream Pout(cout,"Pout")
Reduction operator for PackedList of fileState.