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 | Copyright (C) 2011-2016 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 
36 
37 
38 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
39 
41 :
42  runningJobPath_(),
43  finishedJobPath_(),
44  cpuTime_()
45 {
46  name() = "JobInfo";
47 
49  {
50  string baseDir = getEnv("FOAM_JOB_DIR");
51  string jobFile = hostName() + '.' + Foam::name(pid());
52 
53  fileName runningDir(baseDir/"runningJobs");
54  fileName finishedDir(baseDir/"finishedJobs");
55 
56  runningJobPath_ = runningDir/jobFile;
57  finishedJobPath_ = finishedDir/jobFile;
58 
59  if (baseDir.empty())
60  {
62  << "Cannot get JobInfo directory $FOAM_JOB_DIR"
64  }
65 
66  if (!isDir(runningDir) && !mkDir(runningDir))
67  {
69  << "Cannot make JobInfo directory " << runningDir
71  }
72 
73  if (!isDir(finishedDir) && !mkDir(finishedDir))
74  {
76  << "Cannot make JobInfo directory " << finishedDir
78  }
79  }
80 
81  constructed = true;
82 }
83 
84 
85 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
86 
88 {
90  {
91  mv(runningJobPath_, finishedJobPath_);
92  }
93 
94  constructed = false;
95 }
96 
97 
98 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
99 
100 bool Foam::JobInfo::write(Ostream& os) const
101 {
102  if (writeJobInfo && Pstream::master())
103  {
104  if (os.good())
105  {
106  dictionary::write(os, false);
107  return true;
108  }
109  else
110  {
111  return false;
112  }
113  }
114  else
115  {
116  return true;
117  }
118 }
119 
120 
122 {
123  if (writeJobInfo && Pstream::master())
124  {
125  if (!write(OFstream(runningJobPath_)()))
126  {
128  << "Failed to write to JobInfo file "
129  << runningJobPath_
131  }
132  }
133 }
134 
135 
136 void Foam::JobInfo::end(const word& terminationType)
137 {
139  {
140  add("cpuTime", cpuTime_.elapsedCpuTime());
141  add("endDate", clock::date());
142  add("endTime", clock::clockTime());
143 
144  if (!found("termination"))
145  {
146  add("termination", terminationType);
147  }
148 
149  rm(runningJobPath_);
150  write(OFstream(finishedJobPath_)());
151  }
152 
153  constructed = false;
154 }
155 
156 
158 {
159  end("normal");
160 }
161 
162 
164 {
165  end("exit");
166 }
167 
168 
170 {
171  end("abort");
172 }
173 
174 
176 {
178  {
179  mv(runningJobPath_, finishedJobPath_);
180  }
181 
182  constructed = false;
183 }
184 
185 
186 // ************************************************************************* //
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:104
void write(Ostream &, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:172
JobInfo()
Construct null.
Definition: JobInfo.C:40
bool found(const word &, bool recursive=false, bool patternMatch=true) const
Search dictionary for given keyword.
Definition: dictionary.C:431
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: POSIX.C:913
A class for handling file names.
Definition: fileName.H:69
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
void signalEnd() const
Definition: JobInfo.C:175
Helper class for recording information about run/finished jobs.
Definition: JobInfo.H:54
error FatalError
~JobInfo()
Destructor.
Definition: JobInfo.C:87
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
Output to file stream.
Definition: OFstream.H:82
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:412
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
int infoSwitch(const char *name, const int defaultValue=0)
Lookup info switch or add default value.
Definition: debug.C:177
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:814
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
string hostName(const bool full=false)
Return the system&#39;s host name, as per hostname(1)
Definition: POSIX.C:132
bool isDir(const fileName &, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:536
A class for handling words, derived from string.
Definition: word.H:59
void end()
Definition: JobInfo.C:157
void write() const
Definition: JobInfo.C:121
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:80
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:53
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:297
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:67
JobInfo jobInfo
Definition: JobInfo.C:35
static bool writeJobInfo
Definition: JobInfo.H:72
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:984
void abort()
Definition: JobInfo.C:169
static bool constructed
Definition: JobInfo.H:71
void exit()
Definition: JobInfo.C:163