summaryrefslogtreecommitdiffstats
path: root/src/include/debug-profiler.h
blob: 1e46111f92e7ed2a720661ccaed6065e663ba4ce (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
/***************************************************************************
                          debug-profiler.h  -  description
                             -------------------
    begin                : Sat May 28 2005
    copyright            : (C) 2005 by Martin Witte
    email                : witte@kawo1.rwth-aachen.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef KRADIO_DEBUG_PROFILER_H
#define KRADIO_DEBUG_PROFILER_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <tqstring.h>
#include <tqmap.h>
#include <kdemacros.h>

#if (defined __i386__) || (defined __x86_64__)
static __inline__ unsigned long long int rdtsc()
{
    unsigned int a, d;
    asm volatile("rdtsc" : "=a" (a), "=d" (d));
    return ((unsigned long long)a) | (((unsigned long long)d) << 32);
}
#else
static __inline__ unsigned long long int rdtsc()
{
    return 0UL;
}
#endif

class KDE_EXPORT Profiler
{
public:
    Profiler();
    virtual ~Profiler();

    void startProfile(const TQString &descr);
    void stopProfile (const TQString &descr);

    void printData();

protected:

    virtual long long getCounter() const = 0;

    void stopInternalCounter();
    void startInternalCounter();

    long long m_internalCounter;
    long long m_tmpStartVal;

    struct profile_data
    {
        profile_data(long long start = 0) :
            startCounter(start), accumulatedCounter(0), callCounter(0),
            minCounter(0x7FFFFFFFFFFFFFFFll), maxCounter(0) {}
        long long startCounter;
        long long accumulatedCounter;
        long long callCounter;
        long long minCounter;
        long long maxCounter;
    };

    TQMap<TQString, profile_data> m_ProfileData;
};


class KDE_EXPORT TimeProfiler : public Profiler
{
protected:
    long long getCounter() const { return rdtsc(); }
};


class KDE_EXPORT MemProfiler : public Profiler
{
protected:
    long long getCounter() const;
};


extern TimeProfiler global_time_profiler;
extern MemProfiler  global_mem_profiler;



class KDE_EXPORT BlockProfiler
{
public:
    BlockProfiler(const TQString &descr);
    ~BlockProfiler();

    void stop();

protected:
    TQString m_Description;
};



#endif