JobInfo.C 4.94 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           | www.openfoam.com
OpenFOAM bot's avatar
OpenFOAM bot committed
6 7
     \\/     M anipulation  |
-------------------------------------------------------------------------------
OpenFOAM bot's avatar
OpenFOAM bot committed
8 9
    Copyright (C) 2011-2016 OpenFOAM Foundation
    Copyright (C) 2017-2018 OpenCFD Ltd.
10 11 12 13
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

14 15 16 17
    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
18 19 20 21 22 23 24

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    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 30 31 32 33

\*---------------------------------------------------------------------------*/

#include "JobInfo.H"
#include "OSspecific.H"
#include "clock.H"
#include "OFstream.H"
#include "Pstream.H"
34
#include "foamVersion.H"
35

36 37 38 39 40
// Fallback for job-control directory is in the user-directory
// ~/.OpenFOAM/jobControl

#ifndef FOAM_RESOURCE_USER_CONFIG_DIRNAME
#define FOAM_RESOURCE_USER_CONFIG_DIRNAME ".OpenFOAM"
41 42 43
#ifdef FULLDEBUG
    #warning FOAM_RESOURCE_USER_CONFIG_DIRNAME was undefined (now ".OpenFOAM")
#endif
44 45
#endif

46 47
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //

48 49
bool Foam::JobInfo::writeJobInfo(Foam::debug::infoSwitch("writeJobInfo", 0));
Foam::JobInfo Foam::jobInfo;
50

51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
// * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //

bool Foam::JobInfo::write(Ostream& os) const
{
    if (writeJobInfo && Pstream::master())
    {
        if (os.good())
        {
            dictionary::write(os, false);
            return true;
        }
        else
        {
            return false;
        }
    }
68 69

    return true;
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
}


void Foam::JobInfo::end(const word& terminationType)
{
    if (writeJobInfo && constructed && Pstream::master())
    {
        add("cpuTime", cpuTime_.elapsedCpuTime());
        add("endDate", clock::date());
        add("endTime", clock::clockTime());

        if (!found("termination"))
        {
            add("termination", terminationType);
        }

        Foam::rm(runningDir_/jobFileName_);
        write(OFstream(finishedDir_/jobFileName_)());
    }

    constructed = false;
}

93 94 95

// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

96
Foam::JobInfo::JobInfo()
97
:
98 99 100
    jobFileName_(),
    runningDir_(),
    finishedDir_(),
101
    cpuTime_()
102 103 104 105 106
{
    name() = "JobInfo";

    if (writeJobInfo && Pstream::master())
    {
107
        string jobDir = Foam::getEnv("FOAM_JOB_DIR");
108 109
        if (jobDir.empty())
        {
110
            jobDir = home()/FOAM_RESOURCE_USER_CONFIG_DIRNAME/"jobControl";
111
        }
112

113 114 115
        jobFileName_ = hostName() + '.' + Foam::name(pid());
        runningDir_  = jobDir/"runningJobs";
        finishedDir_ = jobDir/"finishedJobs";
116

117
        if (!isDir(jobDir) && !mkDir(jobDir))
118
        {
119
            FatalErrorInFunction
120
                << "No JobInfo directory: " << jobDir
121 122
                << Foam::exit(FatalError);
        }
123
        if (!isDir(runningDir_) && !mkDir(runningDir_))
124
        {
125
            FatalErrorInFunction
126
                << "No JobInfo directory: " << runningDir_
127 128
                << Foam::exit(FatalError);
        }
129
        if (!isDir(finishedDir_) && !mkDir(finishedDir_))
130
        {
131
            FatalErrorInFunction
132
                << "No JobInfo directory: " << finishedDir_
133 134 135 136 137 138 139 140 141 142
                << Foam::exit(FatalError);
        }
    }

    constructed = true;
}


// * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //

143
Foam::JobInfo::~JobInfo()
144
{
145
    signalEnd();
146 147 148 149 150
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

151
void Foam::JobInfo::write() const
152
{
153
    if (writeJobInfo && constructed && Pstream::master())
154
    {
155 156
        const fileName output = runningDir_/jobFileName_;
        if (!write(OFstream(output)()))
157
        {
158
            FatalErrorInFunction
159
                << "Failed to write to JobInfo file " << output
160 161 162 163 164 165
                << Foam::exit(FatalError);
        }
    }
}


166
void Foam::JobInfo::end()
167 168 169 170 171
{
    end("normal");
}


172
void Foam::JobInfo::exit()
173 174 175 176 177
{
    end("exit");
}


178
void Foam::JobInfo::abort()
179 180 181 182 183
{
    end("abort");
}


184
void Foam::JobInfo::signalEnd() const
185 186 187
{
    if (writeJobInfo && constructed && Pstream::master())
    {
188
        Foam::mv(runningDir_/jobFileName_, finishedDir_/jobFileName_);
189 190 191 192 193 194
    }
    constructed = false;
}


// ************************************************************************* //