OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

JobInfo.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "JobInfo.H"
00028 #include "OSspecific.H"
00029 #include "clock.H"
00030 #include "OFstream.H"
00031 #include "Pstream.H"
00032 
00033 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00034 
00035 bool Foam::JobInfo::writeJobInfo(Foam::debug::infoSwitch("writeJobInfo", 0));
00036 Foam::JobInfo Foam::jobInfo;
00037 
00038 
00039 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00040 
00041 // Null constructor
00042 Foam::JobInfo::JobInfo()
00043 :
00044     runningJobPath_(),
00045     finishedJobPath_(),
00046     cpuTime_()
00047 {
00048     name() = "JobInfo";
00049 
00050     if (writeJobInfo && Pstream::master())
00051     {
00052         string baseDir = getEnv("FOAM_JOB_DIR");
00053         string jobFile = hostName() + '.' + Foam::name(pid());
00054 
00055         fileName runningDir(baseDir/"runningJobs");
00056         fileName finishedDir(baseDir/"finishedJobs");
00057 
00058         runningJobPath_  = runningDir/jobFile;
00059         finishedJobPath_ = finishedDir/jobFile;
00060 
00061         if (baseDir.empty())
00062         {
00063             FatalErrorIn("JobInfo::JobInfo()")
00064                 << "Cannot get JobInfo directory $FOAM_JOB_DIR"
00065                 << Foam::exit(FatalError);
00066         }
00067 
00068         if (!isDir(runningDir) && !mkDir(runningDir))
00069         {
00070             FatalErrorIn("JobInfo::JobInfo()")
00071                 << "Cannot make JobInfo directory " << runningDir
00072                 << Foam::exit(FatalError);
00073         }
00074 
00075         if (!isDir(finishedDir) && !mkDir(finishedDir))
00076         {
00077             FatalErrorIn("JobInfo::JobInfo()")
00078                 << "Cannot make JobInfo directory " << finishedDir
00079                 << Foam::exit(FatalError);
00080         }
00081     }
00082 
00083     constructed = true;
00084 }
00085 
00086 
00087 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
00088 
00089 Foam::JobInfo::~JobInfo()
00090 {
00091     if (writeJobInfo && constructed && Pstream::master())
00092     {
00093         mv(runningJobPath_, finishedJobPath_);
00094     }
00095 
00096     constructed = false;
00097 }
00098 
00099 
00100 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00101 
00102 bool Foam::JobInfo::write(Ostream& os) const
00103 {
00104     if (writeJobInfo && Pstream::master())
00105     {
00106         if (os.good())
00107         {
00108             dictionary::write(os, false);
00109             return true;
00110         }
00111         else
00112         {
00113             return false;
00114         }
00115     }
00116     else
00117     {
00118         return true;
00119     }
00120 }
00121 
00122 
00123 void Foam::JobInfo::write() const
00124 {
00125     if (writeJobInfo && Pstream::master())
00126     {
00127         if (!write(OFstream(runningJobPath_)()))
00128         {
00129             FatalErrorIn("JobInfo::write() const")
00130                 << "Failed to write to JobInfo file "
00131                 << runningJobPath_
00132                 << Foam::exit(FatalError);
00133         }
00134     }
00135 }
00136 
00137 
00138 void Foam::JobInfo::end(const word& terminationType)
00139 {
00140     if (writeJobInfo && constructed && Pstream::master())
00141     {
00142         add("cpuTime", cpuTime_.elapsedCpuTime());
00143         add("endDate", clock::date());
00144         add("endTime", clock::clockTime());
00145 
00146         if (!found("termination"))
00147         {
00148             add("termination", terminationType);
00149         }
00150 
00151         rm(runningJobPath_);
00152         write(OFstream(finishedJobPath_)());
00153     }
00154 
00155     constructed = false;
00156 }
00157 
00158 
00159 void Foam::JobInfo::end()
00160 {
00161     end("normal");
00162 }
00163 
00164 
00165 void Foam::JobInfo::exit()
00166 {
00167     end("exit");
00168 }
00169 
00170 
00171 void Foam::JobInfo::abort()
00172 {
00173     end("abort");
00174 }
00175 
00176 
00177 void Foam::JobInfo::signalEnd() const
00178 {
00179     if (writeJobInfo && constructed && Pstream::master())
00180     {
00181         mv(runningJobPath_, finishedJobPath_);
00182     }
00183 
00184     constructed = false;
00185 }
00186 
00187 
00188 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd