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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2017-2021 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "JobInfo.H"
30 #include "clock.H"
31 #include "OFstream.H"
32 #include "OSspecific.H"
33 #include "Pstream.H"
34 #include "foamVersion.H"
35 
36 // Fallback for job-control directory is in the user-directory
37 // ~/.OpenFOAM/jobControl
38 
39 #ifndef FOAM_RESOURCE_USER_CONFIG_DIRNAME
40 #define FOAM_RESOURCE_USER_CONFIG_DIRNAME ".OpenFOAM"
41 #ifdef FULLDEBUG
42  #warning FOAM_RESOURCE_USER_CONFIG_DIRNAME was undefined (now ".OpenFOAM")
43 #endif
44 #endif
45 
46 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
47 
50 
51 // Foam::JobInfo::constructed defined in globals.C
52 
53 
54 // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //
55 
56 namespace Foam
57 {
58 
59 // Ensure given directory exists (called on master only)
60 static inline bool ensureJobDirExists(const fileName& dir)
61 {
62  if (!Foam::isDir(dir) && !Foam::mkDir(dir))
63  {
64  std::cerr
65  << "WARNING: no JobInfo directory: " << dir << nl
66  << " disabling JobInfo" << nl;
67 
68  return false;
69  }
70 
71  return true;
72 }
73 
74 
75 // Write dictionary entries (called on master only)
76 static inline bool writeJobDict(Ostream& os, const dictionary& dict)
77 {
78  if (os.good())
79  {
80  dict.writeEntries(os, true); // With extraNewLine=true
81  return true;
82  }
83 
84  return false;
85 }
86 
87 } // End namespace Foam
88 
89 
90 // * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
91 
92 void Foam::JobInfo::disable() noexcept
93 {
94  writeJobInfo = false;
95 }
96 
97 
99 {
100  jobInfo.jobEnding();
101 }
102 
103 
104 void Foam::JobInfo::shutdown(bool isAbort)
105 {
106  if (isAbort)
107  {
108  jobInfo.jobEnding("abort");
109  }
110  else
111  {
112  jobInfo.jobEnding("exit");
113  }
114 }
115 
116 
117 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
118 
119 void Foam::JobInfo::jobEnding()
120 {
121  if (!running_.empty())
122  {
123  if (!Foam::mv(running_, finished_))
124  {
125  Foam::rm(running_);
126  }
127  }
128 
129  running_.clear();
130  finished_.clear();
131  constructed = false;
132 }
133 
134 
135 void Foam::JobInfo::jobEnding(const word& terminationType)
136 {
137  if (writeJobInfo && !finished_.empty())
138  {
139  add("cpuTime", cpuTime_.elapsedCpuTime());
140  add("endDate", clock::date());
141  add("endTime", clock::clockTime());
142 
143  if (!terminationType.empty() && !found("termination"))
144  {
145  add("termination", terminationType);
146  }
147 
148  Foam::rm(running_);
149  OFstream os(finished_);
150  if (!writeJobDict(os, *this))
151  {
152  std::cerr
153  << "WARNING: could not write JobInfo file: "
154  << finished_ << nl;
155  }
156  }
157 
158  running_.clear();
159  finished_.clear();
160  constructed = false;
161 }
162 
163 
164 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
165 
167 {
168  if (constructed)
169  {
170  std::cerr
171  << "WARNING: JobInfo was already constructed. "
172  "Should be a singleton!!" << nl;
173  }
174 
175  // Only populate on master process, and when enabled
176  if (writeJobInfo && Pstream::master())
177  {
178  string jobDir = Foam::getEnv("FOAM_JOB_DIR");
179  if (jobDir.empty())
180  {
181  jobDir = home()/FOAM_RESOURCE_USER_CONFIG_DIRNAME/"jobControl";
182  }
183  string jobFile = hostName() + '.' + Foam::name(pid());
184  running_ = jobDir/"runningJobs"/jobFile;
185  finished_ = jobDir/"finishedJobs"/jobFile;
186 
187  if
188  (
189  !ensureJobDirExists(jobDir)
190  || !ensureJobDirExists(running_.path())
191  || !ensureJobDirExists(finished_.path())
192  )
193  {
194  running_.clear();
195  finished_.clear();
196  }
197  }
198 
199  dictionary::name() = "JobInfo";
200  constructed = true;
201 }
202 
203 
204 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
205 
207 {
208  jobEnding();
209 }
210 
211 
212 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
213 
215 {
216  if (writeJobInfo && !running_.empty())
217  {
218  OFstream os(running_);
219  if (!writeJobDict(os, *this))
220  {
221  std::cerr
222  << "WARNING: could not write JobInfo file: "
223  << running_ << nl;
224 
225  // Normally does not happen
226  const_cast<fileName&>(running_).clear();
227  }
228  }
229 }
230 
231 
232 void Foam::JobInfo::stop() { jobEnding("normal"); }
233 
234 void Foam::JobInfo::exit() { jobEnding("exit"); }
235 
236 void Foam::JobInfo::abort() { jobEnding("abort"); }
237 
238 void Foam::JobInfo::signalEnd() { jobEnding(); }
239 
240 
241 // ************************************************************************* //
Foam::JobInfo::exit
void exit()
Job end with "exit" termination.
Definition: JobInfo.C:234
Foam::JobInfo::writeJobInfo
static bool writeJobInfo
Global value for writeJobInfo enabled.
Definition: JobInfo.H:104
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, acts like global singleton.
Definition: JobInfo.H:64
Foam::fileName
A class for handling file names.
Definition: fileName.H:73
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:457
Foam::rm
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: MSwindows.C:1004
Foam::ensureJobDirExists
static bool ensureJobDirExists(const fileName &dir)
Definition: JobInfo.C:60
Foam::dictionary::name
const fileName & name() const noexcept
The dictionary name.
Definition: dictionaryI.H:48
Foam::JobInfo::write
void write() const
Write job info to its file in the "running" jobs directory.
Definition: JobInfo.C:214
Foam::getEnv
string getEnv(const std::string &envName)
Get environment value for given envName.
Definition: MSwindows.C:371
Foam::IOstream::good
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
OFstream.H
Foam::JobInfo::disable
static void disable() noexcept
Disallow JobInfo by forcing writeJobInfo (InfoSwitch) off.
Definition: JobInfo.C:92
Foam::debug::infoSwitch
int infoSwitch(const char *name, const int deflt=0)
Lookup info switch or add default value.
Definition: debug.C:231
Foam::JobInfo::signalEnd
void signalEnd()
Relocate job file from "running" to "finished" directory.
Definition: JobInfo.C:238
clock.H
Foam::pid
pid_t pid()
Return the PID of this process.
Definition: MSwindows.C:330
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary::writeEntries
void writeEntries(Ostream &os, const bool extraNewLine=false) const
Write dictionary entries.
Definition: dictionaryIO.C:180
Foam::JobInfo::shutdown
static void shutdown()
Simple shutdown (finalize) of JobInfo.
Definition: JobInfo.C:98
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
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:939
Foam::mv
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: MSwindows.C:939
os
OBJstream os(runTime.globalPath()/outputName)
Foam::clock::date
static std::string date()
Definition: clock.C:80
Foam::writeJobDict
static bool writeJobDict(Ostream &os, const dictionary &dict)
Definition: JobInfo.C:76
Pstream.H
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::OFstream
Output to file stream, using an OSstream.
Definition: OFstream.H:53
Foam::JobInfo::JobInfo
JobInfo()
Default construct.
Definition: JobInfo.C:166
Foam::jobInfo
JobInfo jobInfo
Definition: JobInfo.C:49
found
bool found
Definition: TABSMDCalcMethod2.H:32
FOAM_RESOURCE_USER_CONFIG_DIRNAME
#define FOAM_RESOURCE_USER_CONFIG_DIRNAME
Definition: JobInfo.C:40
Foam::home
fileName home()
Return home directory path name for the current user.
Definition: MSwindows.C:449
Foam::JobInfo::stop
void stop()
Job end with "normal" termination.
Definition: JobInfo.C:232
clear
patchWriters clear()
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::hostName
string hostName(bool full=false)
Return the system's host name, as per hostname(1)
Definition: MSwindows.C:410
Foam::clock::clockTime
static std::string clockTime()
Definition: clock.C:95
JobInfo.H
Foam::name
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
Foam::JobInfo::~JobInfo
~JobInfo()
Destructor, relocates the job file from running to finished.
Definition: JobInfo.C:206
Foam::JobInfo::abort
void abort()
Job end with "abort" termination.
Definition: JobInfo.C:236
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::mkDir
bool mkDir(const fileName &pathName, mode_t mode=0777)
Make a directory and return an error if it could not be created.
Definition: MSwindows.C:507
Foam::isDir
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: MSwindows.C:643
foamVersion.H