Forward compatibility: flex
[foam-extend-3.2.git] / src / foam / global / profiling / profilingPool.C
blob16b23a362a8da68fff76a336dda95a4b1e86378f
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | foam-extend: Open Source CFD
4    \\    /   O peration     | Version:     3.2
5     \\  /    A nd           | Web:         http://www.foam-extend.org
6      \\/     M anipulation  | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
8 License
9     This file is part of foam-extend.
11     foam-extend is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by the
13     Free Software Foundation, either version 3 of the License, or (at your
14     option) any later version.
16     foam-extend is distributed in the hope that it will be useful, but
17     WITHOUT ANY WARRANTY; without even the implied warranty of
18     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19     General Public License for more details.
21     You should have received a copy of the GNU General Public License
22     along with foam-extend.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "profilingPool.H"
28 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
30 Foam::profilingPool* Foam::profilingPool::thePool_(NULL);
33 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
36 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
38 Foam::profilingPool::profilingPool(
39     const IOobject &ob,
40     const Time &owner
42     :
43     regIOobject(ob),
44     globalTime_(),
45     owner_(owner)
50 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
52 Foam::profilingPool::~profilingPool()
54     for(mapIterator it = map().begin(); it != map().end(); ++it)
55     {
56         delete it->second;
57     }
59     map().erase(allInfo_.begin(), allInfo_.end());
63 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
65 void Foam::profilingPool::initProfiling(
66     const IOobject &ob,
67     const Time &owner
70     if (!thePool_)
71     {
72         thePool_ = new profilingPool(ob,owner);
73         profilingInfo *master=new profilingInfo();
74         thePool_->map().insert(make_pair(master->description(),master));
75         thePool_->stack().push(*master);
76         profilingPool::rememberTimer(*master,thePool_->globalTime_);
77     }
80 void Foam::profilingPool::stopProfiling(
81     const Time &owner
84     if (thePool_ && (&owner)==&(thePool_->owner()))
85     {
86         delete thePool_;
87         thePool_=NULL;
88     }
91 Foam::profilingInfo &Foam::profilingPool::getInfo(const string& name)
93     if (!thePool_)
94     {
95         FatalErrorIn("profilingPool::addInfo(const string& name)")
96             << "Singleton not initialized\n" << endl
97             << abort(FatalError);
98     }
100     profilingStack& stack = thePool_->stack();
101     mapType& map = thePool_->map();
103     profilingInfo* found = NULL;
105     for
106     (
107         mapIterator it = map.lower_bound(name);
108         it != map.upper_bound(name);
109         ++it
110     )
111     {
112         if (it->second->parent().id()==stack.top().id())
113         {
114             found = it->second;
115             break;
116         }
117     }
119     if (!found)
120     {
121         found = new profilingInfo(stack.top(),name);
123         map.insert(make_pair(name,found));
124     }
126     stack.push(*found);
127     return *found;
131 void Foam::profilingPool::rememberTimer
133     const profilingInfo& info,
134     clockTime& timer
137     if(!thePool_)
138     {
139         FatalErrorIn
140         (
141             "profilingPool::rememberTimer(const profilingInfo Foam&info, "
142             "clockTime& timer)"
143         )   << "Singleton not initialized\n" << endl
144             << abort(FatalError);
145     }
147     thePool_->stack().addTimer(info, timer);
151 void Foam::profilingPool::remove(const profilingInfo &info)
153     if(!thePool_)
154     {
155         FatalErrorIn("profilingPool::addInfo(const string& name)")
156             << "Singleton not initialized\n" << endl
157             << abort(FatalError);
158     }
160     profilingStack& stack = thePool_->stack();
162     if(info.id() != stack.top().id())
163     {
164         FatalErrorIn("profilingPool::update(const string &name)")
165             << "The id " << info.id() << " of the updated info "
166             << info.description()
167             << " is no the same as the one on top of the stack: "
168             << stack.top().id() << " (" << stack.top().description()
169             << ")\n" << endl
170             << abort(FatalError);
171     }
173     stack.pop();
177 bool Foam::profilingPool::writeData(Ostream& os) const
179     os  << "profilingInfo" << nl << indent
180         << token::BEGIN_LIST << incrIndent << nl;
182     stack().writeStackContents(os);
184     for(mapConstIterator it = map().begin(); it != map().end(); ++it)
185     {
186         if(!it->second->onStack())
187         {
188             os << *(it->second);
189         }
190     }
192     os  << decrIndent << indent << token::END_LIST
193         << token::END_STATEMENT << endl;
195     return os;
198 // ************************************************************************* //