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-2015 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  {
63  << "Cannot get JobInfo directory $FOAM_JOB_DIR"
65  }
66 
67  if (!isDir(runningDir) && !mkDir(runningDir))
68  {
70  << "Cannot make JobInfo directory " << runningDir
72  }
73 
74  if (!isDir(finishedDir) && !mkDir(finishedDir))
75  {
77  << "Cannot make JobInfo directory " << finishedDir
79  }
80  }
81 
82  constructed = true;
83 }
84 
85 
86 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
87 
89 {
90  if (writeJobInfo && constructed && Pstream::master())
91  {
92  mv(runningJobPath_, finishedJobPath_);
93  }
94 
95  constructed = false;
96 }
97 
98 
99 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
100 
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  {
129  << "Failed to write to JobInfo file "
130  << runningJobPath_
132  }
133  }
134 }
135 
136 
137 void Foam::JobInfo::end(const word& terminationType)
138 {
139  if (writeJobInfo && constructed && Pstream::master())
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 {
178  if (writeJobInfo && constructed && Pstream::master())
179  {
180  mv(runningJobPath_, finishedJobPath_);
181  }
182 
183  constructed = false;
184 }
185 
186 
187 // ************************************************************************* //
Foam::JobInfo::exit
void exit()
Definition: JobInfo.C:164
Foam::JobInfo::writeJobInfo
static bool writeJobInfo
Definition: JobInfo.H:72
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:54
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::fileName
A class for handling file names.
Definition: fileName.H:69
Foam::dictionaryName::name
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
Foam::JobInfo::end
void end()
Definition: JobInfo.C:158
Foam::rm
bool rm(const fileName &)
Remove a file, returning true if successful otherwise false.
Definition: POSIX.C:954
Foam::debug::infoSwitch
int infoSwitch(const char *name, const int defaultValue=0)
Lookup info switch or add default value.
Definition: debug.C:173
Foam::clock::date
static string date()
Return the current wall-clock date as a string.
Definition: clock.C:77
Foam::JobInfo::finishedJobPath_
fileName finishedJobPath_
Definition: JobInfo.H:60
Foam::JobInfo::write
void write() const
Definition: JobInfo.C:122
Foam::mv
bool mv(const fileName &src, const fileName &dst)
Rename src to dst.
Definition: POSIX.C:891
Foam::clock::clockTime
static string clockTime()
Return the current wall-clock time as a string.
Definition: clock.C:93
OFstream.H
Foam::pid
PID_T pid()
Return the PID of this process.
Definition: POSIX.C:77
Foam::getEnv
string getEnv(const word &)
Return environment variable of given name.
Definition: POSIX.C:101
clock.H
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:870
Pstream.H
Foam::isDir
bool isDir(const fileName &)
Does the name exist as a DIRECTORY in the file system?
Definition: POSIX.C:615
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::OFstream
Output to file stream.
Definition: OFstream.H:81
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:399
Foam::JobInfo::JobInfo
JobInfo()
Construct null.
Definition: JobInfo.C:41
Foam::jobInfo
JobInfo jobInfo
Definition: JobInfo.C:35
found
bool found
Definition: TABSMDCalcMethod2.H:32
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::JobInfo::constructed
static bool constructed
Definition: JobInfo.H:71
Foam::hostName
string hostName(const bool full=false)
Return the system's host name, as per hostname(1)
Definition: POSIX.C:129
JobInfo.H
Foam::JobInfo::runningJobPath_
fileName runningJobPath_
Definition: JobInfo.H:59
Foam::JobInfo::~JobInfo
~JobInfo()
Destructor.
Definition: JobInfo.C:88
Foam::JobInfo::abort
void abort()
Definition: JobInfo.C:170
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Foam::mkDir
bool mkDir(const fileName &, mode_t=0777)
Make a directory and return an error if it could not be created.
Definition: POSIX.C:419
Foam::JobInfo::signalEnd
void signalEnd() const
Definition: JobInfo.C:176
write
Tcoeff write()
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::dictionary::write
void write(Ostream &, const bool subDict=true) const
Write dictionary, normally with sub-dictionary formatting.
Definition: dictionaryIO.C:173