jobInfo.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "jobInfo.H"
27 #include "OSspecific.H"
28 #include "clock.H"
29 #include "OFstream.H"
30 #include "Pstream.H"
31 
32 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 
35 (
36  Foam::debug::infoSwitch("writeJobControl", 0)
37 );
38 
40 (
41  Foam::debug::infoSwitch("writeJobInfo", 0)
42 );
43 
45 
46 
47 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
48 
50 :
51  runningJobPath_(),
52  finishedJobPath_(),
53  cpuTime_()
54 {
55  name() = "jobInfo";
56 
57  if (Pstream::master())
58  {
59  if (writeJobControl)
60  {
61  string baseDir = getEnv("FOAM_JOB_DIR");
62  string jobFile = hostName() + '.' + Foam::name(pid());
63 
64  fileName runningDir(baseDir/"runningJobs");
65  fileName finishedDir(baseDir/"finishedJobs");
66 
67  runningJobPath_ = runningDir/jobFile;
68  finishedJobPath_ = finishedDir/jobFile;
69 
70  if (baseDir.empty())
71  {
73  << "Cannot get jobInfo directory $FOAM_JOB_DIR"
75  }
76 
77  if (!isDir(runningDir) && !mkDir(runningDir))
78  {
80  << "Cannot make jobInfo directory " << runningDir
82  }
83 
84  if (!isDir(finishedDir) && !mkDir(finishedDir))
85  {
87  << "Cannot make jobInfo directory " << finishedDir
89  }
90 
91  writeJobInfo = true;
92  }
93  }
94 
95  constructed = true;
96 }
97 
98 
99 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
100 
102 {
104  {
105  mv(runningJobPath_, finishedJobPath_);
106  }
107 
108  constructed = false;
109 }
110 
111 
112 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
113 
114 bool Foam::jobInfo::write(Ostream& os) const
115 {
116  if (writeJobInfo && Pstream::master())
117  {
118  if (os.good())
119  {
120  dictionary::write(os, false);
121  return true;
122  }
123  else
124  {
125  return false;
126  }
127  }
128  else
129  {
130  return true;
131  }
132 }
133 
134 
135 void Foam::jobInfo::write
136 (
137  const word& executable,
138  const fileName& casePath
139 ) const
140 {
141  if (writeJobInfo && Pstream::master())
142  {
143  if (!writeJobControl)
144  {
145  const fileName jobInfoPath(casePath/"jobInfo");
146 
147  if (!isDir(jobInfoPath) && !mkDir(jobInfoPath))
148  {
150  << "Cannot make jobInfo directory " << jobInfoPath
152  }
153 
154  const word jobFile = executable + '.' + Foam::name(pid());
155 
156  runningJobPath_ = jobInfoPath/jobFile;
157  finishedJobPath_ = jobInfoPath/jobFile;
158  }
159 
160  if (!write(OFstream(runningJobPath_)()))
161  {
163  << "Failed to write to jobInfo file "
164  << runningJobPath_
166  }
167  }
168 }
169 
170 
171 void Foam::jobInfo::end(const word& terminationType)
172 {
174  {
175  add("cpuTime", cpuTime_.elapsedCpuTime());
176  add("endDate", clock::date());
177  add("endTime", clock::clockTime());
178 
179  if (!found("termination"))
180  {
181  add("termination", terminationType);
182  }
183 
184  rm(runningJobPath_);
185  write(OFstream(finishedJobPath_)());
186  }
187 
188  constructed = false;
189 }
190 
191 
193 {
194  end("normal");
195 }
196 
197 
199 {
200  end("exit");
201 }
202 
203 
205 {
206  end("abort");
207 }
208 
209 
211 {
213  {
214  mv(runningJobPath_, finishedJobPath_);
215  }
216 
217  constructed = false;
218 }
219 
220 
221 // ************************************************************************* //
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:97
void write(Ostream &, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:207
Helper class for recording information about run/finished jobs.
Definition: jobInfo.H:54
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:663
static bool constructed
Definition: jobInfo.H:72
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: POSIX.C:948
A class for handling file names.
Definition: fileName.H:79
void exit()
Definition: jobInfo.C:198
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
jobInfo()
Construct null.
Definition: jobInfo.C:49
error FatalError
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
Output to file stream.
Definition: OFstream.H:82
void signalEnd() const
Definition: jobInfo.C:210
static bool writeJobControl
Definition: jobInfo.H:73
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
jobInfo jobInfo_
Definition: jobInfo.C:44
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
int infoSwitch(const char *name, const int defaultValue=0)
Lookup info switch or add default value.
Definition: debug.C:233
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1153
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
void end()
Definition: jobInfo.C:192
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:109
string hostName(const bool full=false)
Return the system&#39;s host name, as per hostname(1)
Definition: POSIX.C:125
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
Definition: POSIX.C:539
A class for handling words, derived from string.
Definition: word.H:59
static string clockTime()
Return the current wall-clock time as a string.
Definition: clock.C:93
pid_t pid()
Return the PID of this process.
Definition: POSIX.C:73
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:54
~jobInfo()
Destructor.
Definition: jobInfo.C:101
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:290
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
static string date()
Return the current wall-clock date as a string.
Definition: clock.C:77
double elapsedCpuTime() const
Return CPU time (in seconds) from the start.
Definition: cpuTime.C:53
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:1017
static bool writeJobInfo
Definition: jobInfo.H:74
void abort()
Definition: jobInfo.C:204