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 {
103  if (writeJobInfo && constructed && Pstream::master())
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 {
173  if (writeJobInfo && constructed && Pstream::master())
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 {
212  if (writeJobControl && constructed && Pstream::master())
213  {
214  mv(runningJobPath_, finishedJobPath_);
215  }
216 
217  constructed = false;
218 }
219 
220 
221 // ************************************************************************* //
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
bool found
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:330
Output to file stream.
Definition: OFstream.H:86
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
static string clockTime()
Return the current wall-clock time as a string.
Definition: clock.C:93
static string date()
Return the current wall-clock date as a string.
Definition: clock.C:77
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:109
void write(Ostream &, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:204
A class for handling file names.
Definition: fileName.H:82
Helper class for recording information about run/finished jobs.
Definition: jobInfo.H:57
jobInfo()
Construct null.
Definition: jobInfo.C:49
void exit()
Definition: jobInfo.C:198
~jobInfo()
Destructor.
Definition: jobInfo.C:101
static bool writeJobControl
Definition: jobInfo.H:73
static bool constructed
Definition: jobInfo.H:72
static bool writeJobInfo
Definition: jobInfo.H:74
void end()
Definition: jobInfo.C:192
void abort()
Definition: jobInfo.C:204
void signalEnd() const
Definition: jobInfo.C:210
A class for handling words, derived from string.
Definition: word.H:62
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
int infoSwitch(const char *name, const int defaultValue=0)
Lookup info switch or add default value.
Definition: debug.C:233
void write(std::ostream &os, const bool binary, List< floatScalar > &fField)
Write floats ascii or binary.
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:1017
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:290
jobInfo jobInfo_
Definition: jobInfo.C:44
string hostName(const bool full=false)
Return the system's host name, as per hostname(1)
Definition: POSIX.C:125
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a directory in the file system?
Definition: POSIX.C:539
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:97
pid_t pid()
Return the PID of this process.
Definition: POSIX.C:73
error FatalError
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: POSIX.C:948
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47