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" },
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);
100 void operator()(
unsigned int&
x,
const unsigned int y)
const 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 " 202 inline bool addWatch(
const label watchFd,
const fileName& fName)
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
292 void 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;
427 Foam::fileMonitor::fileMonitor(
const bool useInotify)
429 useInotify_(useInotify),
434 watcher_(new fileMonitorWatcher(useInotify_, 20))
452 Pout<<
"fileMonitor : adding watch on file " << fName <<
endl;
455 label watchFd = state_.size();
457 if (!freeWatchFds_.empty())
459 watchFd = freeWatchFds_.back();
460 freeWatchFds_.pop_back();
463 watcher_->addWatch(watchFd, fName);
467 Pout<<
"fileMonitor : added watch " << watchFd <<
" on file " 474 <<
"could not add watch for file " << fName <<
endl;
478 localState_(watchFd) = UNMODIFIED;
479 state_(watchFd) = UNMODIFIED;
480 watchFile_(watchFd) = fName;
490 Pout<<
"fileMonitor : removing watch " << watchFd <<
" on file " 491 << watchFile_[watchFd] <<
endl;
494 freeWatchFds_.appendUniq(watchFd);
496 return watcher_->removeWatch(watchFd);
502 return watchFile_[watchFd];
509 return state_[watchFd];
515 const bool masterOnly,
536 static_cast<unsigned int>(localState_[watchFd])
546 if (stats.storage().size() == 1)
558 if (stats.storage().size() == 1)
578 unsigned int stat = stats[watchFd];
584 if (state_[watchFd] != localState_[watchFd])
588 Pout<<
"fileMonitor : Delaying reading " 589 << watchFile_[watchFd]
590 <<
" due to inconsistent " 591 "file time-stamps between processors" 596 <<
"Delaying reading " << watchFile_[watchFd]
597 <<
" due to inconsistent " 598 "file time-stamps between processors" <<
endl;
605 state_ = localState_;
612 state_[watchFd] = UNMODIFIED;
613 localState_[watchFd] = UNMODIFIED;
DynamicList< double > lastMod_
From watch descriptor to modified time.
static const Enum< fileState > fileStateNames_
void size(const label n)
Older name for setAddressableSize.
Info<< "Creating field kinetic energy K\"<< 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()
A class for handling file names.
Inter-processor communication reduction functions.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Internal tracking via stat(3p) or inotify(7)
static float fileModificationSkew
Time skew (seconds) for file modification checks.
error FatalError
Error stream (stdout output on all processes), with additional 'FOAM FATAL ERROR' header text and sta...
Checking for changes to files.
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
fileState getState(const label watchFd) const
Check state using handle.
fileState
Enumeration defining the file state.
void setUnmodified(const label watchFd)
Reset state (e.g. after having read it) using handle.
List< bool > select(const label n, const labelUList &locations)
Construct a selection list of bools (all false) with the given pre-size, subsequently add specified l...
Ostream & endl(Ostream &os)
Add newline and flush stream.
void setCapacity(const label len)
Alter the size of the underlying storage.
static std::string path(const std::string &str)
Return directory path name (part before last /)
DynamicList< label > dirWatches_
Current watchIDs and corresponding directory id.
Reduction operator for PackedList of fileState.
static void broadcast(Type &value, const label comm=UPstream::worldComm)
Broadcast content (contiguous or non-contiguous) to all processes in communicator.
bool read(const char *buf, int32_t &val)
Same as readInt32.
#define forAll(list, i)
Loop across all elements in list.
Useful combination of include files which define Sin, Sout and Serr and the use of IO streams general...
fileMonitorWatcher(const bool useInotify, const label sz=20)
Initialise inotify.
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
void operator()(unsigned int &x, const unsigned int y) const
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
bool removeWatch(const label watchFd)
Remove file to watch. Return true if successful.
void updateStates(const bool masterOnly, const bool syncPar) const
Check state of all files. Updates state_.
const fileName & getFile(const label watchFd) const
Get name of file being watched.
unsigned int operator()(const unsigned int x, const unsigned int y) const
label min(const labelHashSet &set, label minValue=labelMax)
Find the min value in labelHashSet, optionally limited by second argument.
errorManip< error > abort(error &err)
int inotifyFd_
File descriptor for the inotify instance.
int debug
Static debugging option.
defineTypeNameAndDebug(combustionModel, 0)
~fileMonitor()
Destructor.
#define WarningInFunction
Report a warning using Foam::Warning.
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
label addWatch(const fileName &)
Add file to watch. Return watch descriptor.
DynamicList< fileName > dirFiles_
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
void reduce(const List< UPstream::commsStruct > &comms, T &value, const BinaryOp &bop, const int tag, const label comm)
Reduce inplace (cf. MPI Allreduce) using specified communication schedule.
bool removeWatch(const label watchFd)
static void listCombineGather(const List< commsStruct > &comms, List< T > &values, const CombineOp &cop, const int tag, const label comm)
~fileMonitorWatcher()
Remove all watches.
Combine operator for PackedList of fileState.
bool addWatch(const label watchFd, const fileName &fName)
A class for handling character strings derived from std::string.
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
double highResLastModified(const fileName &, const bool followLink=true)
Return time of last file modification.