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-------------------------------------------------------------------------------
11License
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
56namespace Foam
57{
58
59// Ensure given directory exists (called on master only)
60static 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)
76static 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
93{
94 writeJobInfo = false;
95}
96
97
99{
100 jobInfo.jobEnding();
101}
102
103
104void 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
119void 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
135void 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
232void Foam::JobInfo::stop() { jobEnding("normal"); }
233
234void Foam::JobInfo::exit() { jobEnding("exit"); }
235
236void Foam::JobInfo::abort() { jobEnding("abort"); }
237
238void Foam::JobInfo::signalEnd() { jobEnding(); }
239
240
241// ************************************************************************* //
#define FOAM_RESOURCE_USER_CONFIG_DIRNAME
Definition: JobInfo.C:40
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
bool found
bool good() const noexcept
True if next operation might succeed.
Definition: IOstream.H:233
Helper class for recording information about run/finished jobs, acts like global singleton.
Definition: JobInfo.H:67
static void disable() noexcept
Disallow JobInfo by forcing writeJobInfo (InfoSwitch) off.
Definition: JobInfo.C:92
void exit()
Job end with "exit" termination.
Definition: JobInfo.C:234
void write() const
Write job info to its file in the "running" jobs directory.
Definition: JobInfo.C:214
JobInfo()
Default construct.
Definition: JobInfo.C:166
~JobInfo()
Destructor, relocates the job file from running to finished.
Definition: JobInfo.C:206
static void shutdown()
Simple shutdown (finalize) of JobInfo.
Definition: JobInfo.C:98
static bool writeJobInfo
Global value for writeJobInfo enabled.
Definition: JobInfo.H:104
void stop()
Job end with "normal" termination.
Definition: JobInfo.C:232
void abort()
Job end with "abort" termination.
Definition: JobInfo.C:236
void signalEnd()
Relocate job file from "running" to "finished" directory.
Definition: JobInfo.C:238
Output to file stream, using an OSstream.
Definition: OFstream.H:57
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
static std::string date()
Definition: clock.C:80
static std::string clockTime()
Definition: clock.C:95
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
const fileName & name() const noexcept
The dictionary name.
Definition: dictionaryI.H:48
void writeEntries(Ostream &os, const bool extraNewLine=false) const
Write dictionary entries.
Definition: dictionaryIO.C:180
A class for handling file names.
Definition: fileName.H:76
splitCell * master() const
Definition: splitCell.H:113
patchWriters clear()
OBJstream os(runTime.globalPath()/outputName)
int infoSwitch(const char *name, const int deflt=0)
Lookup info switch or add default value.
Definition: debug.C:231
Namespace for OpenFOAM.
bool rm(const fileName &file)
Remove a file (or its gz equivalent), returning true if successful.
Definition: MSwindows.C:1012
string getEnv(const std::string &envName)
Get environment value for given envName.
Definition: MSwindows.C:371
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:515
string hostName()
Return the system's host name, as per hostname(1)
Definition: MSwindows.C:410
void add(FieldField< Field1, typename typeOfSum< Type1, Type2 >::type > &f, const FieldField< Field1, Type1 > &f1, const FieldField< Field2, Type2 > &f2)
static bool writeJobDict(Ostream &os, const dictionary &dict)
Definition: JobInfo.C:76
pid_t pid()
Return the PID of this process.
Definition: MSwindows.C:330
word name(const expressions::valueTypeCode typeCode)
A word representation of a valueTypeCode. Empty for INVALID.
Definition: exprTraits.C:59
JobInfo jobInfo
Definition: JobInfo.C:49
bool mv(const fileName &src, const fileName &dst, const bool followLink=false)
Rename src to dst.
Definition: MSwindows.C:947
fileName home()
Return home directory path name for the current user.
Definition: MSwindows.C:457
static bool ensureJobDirExists(const fileName &dir)
Definition: JobInfo.C:60
bool isDir(const fileName &name, const bool followLink=true)
Does the name exist as a DIRECTORY in the file system?
Definition: MSwindows.C:651
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict