summaryrefslogtreecommitdiff
path: root/src/base/ThreadProfiler.cpp
blob: 1f1a3544887e9ba9adbfc76d038c415ca04eaab4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library 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
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#include "ThreadProfiler.h"

#include "Logger.h"
#include "Exception.h"
#include "ProfilingZone.h"
#include "ScopeTimer.h"

#include <sstream>
#include <iomanip>
#include <iostream>

using namespace std;
using namespace boost;

namespace avg {
    
thread_specific_ptr<ThreadProfiler*> ThreadProfiler::s_pInstance;

ThreadProfiler* ThreadProfiler::get() 
{
    if (s_pInstance.get() == 0) {
        s_pInstance.reset(new (ThreadProfiler*));
        *s_pInstance = new ThreadProfiler();
    }
    return *s_pInstance;
}

void ThreadProfiler::kill()
{
    delete *s_pInstance;
    s_pInstance.reset();
}

ThreadProfiler::ThreadProfiler()
    : m_sName(""),
      m_LogCategory(Logger::category::PROFILE)
{
    m_bRunning = false;
    ScopeTimer::enableTimers(Logger::get()->shouldLog(m_LogCategory,
            Logger::severity::INFO));
}

ThreadProfiler::~ThreadProfiler() 
{
}

void ThreadProfiler::setLogCategory(category_t category)
{
    AVG_ASSERT(!m_bRunning);
    m_LogCategory = category;
}

void ThreadProfiler::start()
{
    m_bRunning = true;
}

void ThreadProfiler::restart()
{
    ZoneVector::iterator it;
    for (it = m_Zones.begin(); it != m_Zones.end(); ++it) {
        (*it)->restart();
    }
}

void ThreadProfiler::startZone(const ProfilingZoneID& zoneID)
{
    ZoneMap::iterator it = m_ZoneMap.find(&zoneID);
    // Duplicated code to avoid instantiating a new smart pointer when it's not
    // necessary.
    if (it == m_ZoneMap.end()) {
        ProfilingZonePtr pZone = addZone(zoneID);
        pZone->start();
        m_ActiveZones.push_back(pZone);
    } else {
        ProfilingZonePtr& pZone = it->second;
        pZone->start();
        m_ActiveZones.push_back(pZone);
    }
}

void ThreadProfiler::stopZone(const ProfilingZoneID& zoneID)
{
    ZoneMap::iterator it = m_ZoneMap.find(&zoneID);
    ProfilingZonePtr& pZone = it->second;
    pZone->stop();
    m_ActiveZones.pop_back();
}

void ThreadProfiler::dumpStatistics()
{
    if (!m_Zones.empty()) {
        AVG_TRACE(m_LogCategory, Logger::severity::INFO, "Thread " << m_sName);
        AVG_TRACE(m_LogCategory, Logger::severity::INFO,
                "Zone name                          Avg. time");
        AVG_TRACE(m_LogCategory, Logger::severity::INFO,
                "---------                          ---------");

        ZoneVector::iterator it;
        for (it = m_Zones.begin(); it != m_Zones.end(); ++it) {
            AVG_TRACE(m_LogCategory, Logger::severity::INFO,
                    std::setw(35) << std::left 
                    << ((*it)->getIndentString()+(*it)->getName())
                    << std::setw(9) << std::right << (*it)->getAvgUSecs());
        }
        AVG_TRACE(m_LogCategory, Logger::severity::INFO, "");
    }
}

void ThreadProfiler::reset()
{
    ZoneVector::iterator it;
    for (it = m_Zones.begin(); it != m_Zones.end(); ++it) {
        (*it)->reset();
    }
}

int ThreadProfiler::getNumZones()
{
    return m_Zones.size();
}

const std::string& ThreadProfiler::getName() const
{
    return m_sName;
}

void ThreadProfiler::setName(const std::string& sName)
{
    m_sName = sName;
}


ProfilingZonePtr ThreadProfiler::addZone(const ProfilingZoneID& zoneID)
{
    ProfilingZonePtr pZone(new ProfilingZone(zoneID));
    m_ZoneMap[&zoneID] = pZone;
    ZoneVector::iterator it;
    int parentIndent = -2;
    if (m_ActiveZones.empty()) {
        it = m_Zones.end();
    } else {
        ProfilingZonePtr pActiveZone = m_ActiveZones.back();
        bool bParentFound = false;
        for (it = m_Zones.begin(); it != m_Zones.end(); ++it) 
        {
            if (pActiveZone == *it) {
                bParentFound = true;
                break;
            }
        }
        AVG_ASSERT(bParentFound);
        parentIndent = pActiveZone->getIndentLevel();
        ++it;
        for (; it != m_Zones.end() && (*it)->getIndentLevel() > parentIndent; ++it) {};
    }
    m_Zones.insert(it, pZone);
    pZone->setIndentLevel(parentIndent+2);
    return pZone;
}

}