summaryrefslogtreecommitdiffstats
path: root/src/debug-profiler.cpp
blob: 90f18a289014d3f67785f9834dbb233966221238 (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
/***************************************************************************
                          debug-profiler.h  -  description
                             -------------------
    begin                : Sat May 28 2005
    copyright            : (C) 2005 by Martin Witte
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program 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 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "include/debug-profiler.h"

#include <tqstringlist.h>

#include <sys/resource.h>

TDE_EXPORT TimeProfiler global_time_profiler;
TDE_EXPORT MemProfiler  global_mem_profiler;

Profiler::Profiler()
{
}


Profiler::~Profiler()
{
    m_tmpStartVal = 0;
}

void Profiler::stopInternalCounter()
{
    long long counter = getCounter();
    long long diff = counter - m_tmpStartVal;
    m_internalCounter += diff;
}

void Profiler::startInternalCounter() {
    m_tmpStartVal = getCounter();
}

void  Profiler::startProfile(const TQString &descr)
{
    stopInternalCounter();

    if (m_ProfileData.contains(descr)) {
        profile_data &d = m_ProfileData[descr];
        d.startCounter = m_internalCounter;
    } else {
        m_ProfileData.insert(descr, profile_data(m_internalCounter));
    }

    startInternalCounter();
}


void Profiler::stopProfile (const TQString &descr)
{
    stopInternalCounter();

    if (!descr.isNull() && m_ProfileData.contains(descr)) {
        profile_data &d = m_ProfileData[descr];
        long long diff = m_internalCounter - d.startCounter;
        d.accumulatedCounter += diff;
        if (d.maxCounter < diff)
            d.maxCounter = diff;
        if (d.minCounter > diff)
            d.minCounter = diff;
        d.callCounter++;
    }

    startInternalCounter();
}


void Profiler::printData ()
{
    stopInternalCounter();

    TQStringList keys=m_ProfileData.keys();
    keys.sort();
    TQValueListIterator<TQString> it  = keys.begin();
    TQValueListIterator<TQString> end = keys.end();
    for (; it != end; ++it) {
        int l = (*it).length();
        l = (((l-1) / 25) + 1) * 25;
        if (l < 50) l = 50;
        const profile_data &d = m_ProfileData[*it];
        printf(("%-"+TQString::number(l)+"s: total: %3.8f (%9lli)  avg: %3.8f  min: %3.8f  max: %3.8f\n").ascii(),
               (*it).ascii(),
               (double)d.accumulatedCounter / 1.666e9,
               d.callCounter,
               (double)d.accumulatedCounter / (double)d.callCounter / 1.666e9,
               (double)d.minCounter / 1.666e9,
               (double)d.maxCounter / 1.666e9);
    }

    startInternalCounter();
}


long long MemProfiler::getCounter() const
{
    struct rusage usg;
    if (getrusage(RUSAGE_SELF, &usg) == 0) {
        return usg.ru_idrss + usg.ru_isrss;
    } else {
        return 0;
    }
}


BlockProfiler::BlockProfiler(const TQString &descr)
    : m_Description(descr)
{
    global_mem_profiler.startProfile(m_Description);
    global_time_profiler.startProfile(m_Description);
}

BlockProfiler::~BlockProfiler()
{
    global_time_profiler.stopProfile(m_Description);
    global_mem_profiler.stopProfile(m_Description);
}

void BlockProfiler::stop()
{
    global_time_profiler.stopProfile(m_Description);
    global_mem_profiler.stopProfile(m_Description);
    m_Description = TQString();
}