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