JobInfo.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2018 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "JobInfo.H"
30 #include "OSspecific.H"
31 #include "clock.H"
32 #include "OFstream.H"
33 #include "Pstream.H"
34 #include "foamVersion.H"
35 
36 // Fallback for job-control directory is in the user-directory
37 // ~/.OpenFOAM/jobControl
38 
39 #ifndef FOAM_RESOURCE_USER_CONFIG_DIRNAME
40 #define FOAM_RESOURCE_USER_CONFIG_DIRNAME ".OpenFOAM"
41 #ifdef FULLDEBUG
42  #warning FOAM_RESOURCE_USER_CONFIG_DIRNAME was undefined (now ".OpenFOAM")
43 #endif
44 #endif
45 
46 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
47 
50 
51 
52 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
53 
54 bool Foam::JobInfo::write(Ostream& os) const
55 {
57  {
58  if (os.good())
59  {
60  dictionary::write(os, false);
61  return true;
62  }
63  else
64  {
65  return false;
66  }
67  }
68 
69  return true;
70 }
71 
72 
73 void Foam::JobInfo::end(const word& terminationType)
74 {
75  if (writeJobInfo && constructed && Pstream::master())
76  {
77  add("cpuTime", cpuTime_.elapsedCpuTime());
78  add("endDate", clock::date());
79  add("endTime", clock::clockTime());
80 
81  if (!found("termination"))
82  {
83  add("termination", terminationType);
84  }
85 
86  Foam::rm(runningDir_/jobFileName_);
87  write(OFstream(finishedDir_/jobFileName_)());
88  }
89 
90  constructed = false;
91 }
92 
93 
94 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
95 
97 :
98  jobFileName_(),
99  runningDir_(),
100  finishedDir_(),
101  cpuTime_()
102 {
103  name() = "JobInfo";
104 
105  if (writeJobInfo && Pstream::master())
106  {
107  string jobDir = Foam::getEnv("FOAM_JOB_DIR");
108  if (jobDir.empty())
109  {
110  jobDir = home()/FOAM_RESOURCE_USER_CONFIG_DIRNAME/"jobControl";
111  }
112 
113  jobFileName_ = hostName() + '.' + Foam::name(pid());
114  runningDir_ = jobDir/"runningJobs";
115  finishedDir_ = jobDir/"finishedJobs";
116 
117  if (!isDir(jobDir) && !mkDir(jobDir))
118  {
120  << "No JobInfo directory: " << jobDir
122  }
123  if (!isDir(runningDir_) && !mkDir(runningDir_))
124  {
126  << "No JobInfo directory: " << runningDir_
128  }
129  if (!isDir(finishedDir_) && !mkDir(finishedDir_))
130  {
132  << "No JobInfo directory: " << finishedDir_
134  }
135  }
136 
137  constructed = true;
138 }
139 
140 
141 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
142 
144 {
145  signalEnd();
146 }
147 
148 
149 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
150 
152 {
153  if (writeJobInfo && constructed && Pstream::master())
154  {
155  const fileName output = runningDir_/jobFileName_;
156  if (!write(OFstream(output)()))
157  {
159  << "Failed to write to JobInfo file " << output
161  }
162  }
163 }
164 
165 
167 {
168  end("normal");
169 }
170 
171 
173 {
174  end("exit");
175 }
176 
177 
179 {
180  end("abort");
181 }
182 
183 
185 {
186  if (writeJobInfo && constructed && Pstream::master())
187  {
188  Foam::mv(runningDir_/jobFileName_, finishedDir_/jobFileName_);
189  }
190  constructed = false;
191 }
192 
193 
194 // ************************************************************************* //
Foam::JobInfo::exit
void exit()
End with "termination=exit".
Definition: JobInfo.C:172
Foam::JobInfo::writeJobInfo
static bool writeJobInfo
Global value for writeJobInfo enabled.
Definition: JobInfo.H:83
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::JobInfo
Helper class for recording information about run/finished jobs.
Definition: JobInfo.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::JobInfo::end
void end()
End with "termination=normal".
Definition: JobInfo.C:166
Foam::rm
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: MSwindows.C:994
Foam::JobInfo::write
void write() const
Write the job info to its file in the runningJobs directory.
Definition: JobInfo.C:151
Foam::getEnv
string getEnv(const std::string &envName)
Get environment value for given envName.
Definition: MSwindows.C:371
OFstream.H
Foam::debug::infoSwitch
int infoSwitch(const char *name, const int deflt=0)
Lookup info switch or add default value.
Definition: debug.C:231
Foam::dictionary::name
const fileName & name() const
The dictionary name.
Definition: dictionary.H:446
Foam::name
word name(const complex &c)
Return string representation of complex.
Definition: complex.C:76
clock.H
Foam::pid
pid_t pid()
Return the PID of this process.
Definition: MSwindows.C:330
Foam::FatalError
error FatalError
Foam::add
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
Definition: FieldFieldFunctions.C:939
Foam::mv
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: MSwindows.C:929
stdFoam::end
constexpr auto end(C &c) -> decltype(c.end())
Return iterator to the end of the container c.
Definition: stdFoam.H:115
Foam::clock::date
static std::string date()
Return the current wall-clock date as a string.
Definition: clock.C:80
Pstream.H
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:99
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:438
Foam::JobInfo::JobInfo
JobInfo()
Construct null.
Definition: JobInfo.C:96
Foam::jobInfo
JobInfo jobInfo
Definition: JobInfo.C:49
found
bool found
Definition: TABSMDCalcMethod2.H:32
FOAM_RESOURCE_USER_CONFIG_DIRNAME
#define FOAM_RESOURCE_USER_CONFIG_DIRNAME
Definition: JobInfo.C:40
Foam::home
fileName home()
Return home directory path name for the current user.
Definition: MSwindows.C:449
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::dictionary::write
void write(Ostream &os, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:198
Foam::JobInfo::constructed
static bool constructed
Global value for constructed job info.
Definition: JobInfo.H:80
Foam::hostName
string hostName(const bool full=false)
Return the system's host name, as per hostname(1)
Definition: MSwindows.C:410
Foam::clock::clockTime
static std::string clockTime()
Return the current wall-clock time as a string.
Definition: clock.C:95
Foam::vtk::write
void write(vtk::formatter &fmt, const Type &val, const label n=1)
Component-wise write of a value (N times)
Definition: foamVtkOutputTemplates.C:35
JobInfo.H
Foam::JobInfo::~JobInfo
~JobInfo()
Destructor.
Definition: JobInfo.C:143
Foam::JobInfo::abort
void abort()
End with "termination=abort".
Definition: JobInfo.C:178
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:216
Foam::mkDir
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: MSwindows.C:507
Foam::JobInfo::signalEnd
void signalEnd() const
Update job info and relocate the file from running to finished.
Definition: JobInfo.C:184
Foam::isDir
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: MSwindows.C:643
foamVersion.H