File: profiling.h

package info (click to toggle)
indigo 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 48,936 kB
  • sloc: ansic: 332,816; cpp: 169,470; python: 20,033; java: 13,701; cs: 9,979; asm: 8,475; sql: 6,743; xml: 6,354; javascript: 1,245; sh: 555; php: 506; makefile: 54
file content (165 lines) | stat: -rw-r--r-- 7,612 bytes parent folder | download
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
/****************************************************************************
 * Copyright (C) from 2009 to Present EPAM Systems.
 *
 * This file is part of Indigo toolkit.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ***************************************************************************/

#ifndef __profiling_h__
#define __profiling_h__

#include "base_c/nano.h"
#include "base_cpp/array.h"
#include "base_cpp/obj_array.h"
#include "base_cpp/os_sync_wrapper.h"

#ifdef _WIN32
#pragma warning(push)
#pragma warning(disable : 4251)
#endif

#define _PROF_GET_NAME_INDEX(var_name, name)                                                                                                                   \
    static int var_name##_name_index;                                                                                                                          \
    if (var_name##_name_index == 0)                                                                                                                            \
    {                                                                                                                                                          \
        indigo::OsLocker locker(indigo::_profiling_global_lock);                                                                                               \
        if (var_name##_name_index == 0)                                                                                                                        \
        {                                                                                                                                                      \
            indigo::ProfilingSystem& inst = indigo::ProfilingSystem::getInstance();                                                                            \
            var_name##_name_index = inst.getNameIndex(name);                                                                                                   \
        }                                                                                                                                                      \
    }

#define profTimerStart(var_name, name)                                                                                                                         \
    _PROF_GET_NAME_INDEX(var_name, name)                                                                                                                       \
    indigo::_ProfilingTimer var_name##_timer(var_name##_name_index)

#define profTimerStop(var_name) var_name##_timer.stop()

#define profTimerGetTime(var_name) var_name##_timer.getTime()

#define profTimerGetTimeSec(var_name) var_name##_timer.getTimeSec()

#define profIncTimer(name, dt)                                                                                                                                 \
    do                                                                                                                                                         \
    {                                                                                                                                                          \
        _PROF_GET_NAME_INDEX(var_name, name)                                                                                                                   \
        indigo::ProfilingSystem& inst = indigo::ProfilingSystem::getInstance();                                                                                \
        inst.addTimer(var_name##_name_index, dt);                                                                                                              \
    } while (false)

#define profIncCounter(name, count)                                                                                                                            \
    do                                                                                                                                                         \
    {                                                                                                                                                          \
        _PROF_GET_NAME_INDEX(var_name, name)                                                                                                                   \
        indigo::ProfilingSystem& inst = indigo::ProfilingSystem::getInstance();                                                                                \
        inst.addCounter(var_name##_name_index, count);                                                                                                         \
    } while (false)

#define profTimersReset() indigo::ProfilingSystem::getInstance().reset(false)
#define profTimersResetSession() indigo::ProfilingSystem::getInstance().reset(true)

#define profGetStatistics(output, all) indigo::ProfilingSystem::getInstance().getStatistics(output, all)

namespace indigo
{
    class Output;

    class DLLEXPORT ProfilingSystem
    {
    public:
        static ProfilingSystem& getInstance();

        static int getNameIndex(const char* name, bool add_if_not_exists = true);

        void addTimer(int name_index, qword dt);
        void addCounter(int name_index, int value);
        void reset(bool all);
        void getStatistics(Output& output, bool get_all);

        bool hasLabel(const char* name);
        float getLabelExecTime(const char* name, bool total = false);
        qword getLabelValue(const char* name, bool total = false);
        qword getLabelCallCount(const char* name, bool total = false);

        DECL_ERROR;

    private:
        struct Record
        {
            enum
            {
                TYPE_TIMER,
                TYPE_COUNTER
            };

            struct Data
            {
                qword count, value, max_value;
                double square_sum;

                Data();

                void reset();

                void add(qword value);
            };

            Data current, total;

            int type;

            void reset(bool all);
        };

        static int _recordsCmp(int idx1, int idx2, void* context);

        void _printTimerData(const Record::Data& data, Output& output);
        void _printCounterData(const Record::Data& data, Output& output);

        bool _hasLabelIndex(int name_index);
        void _ensureRecordExistanceLocked(int name_index);

        ObjArray<Record> _records;
        Array<int> _sorted_records;
        OsLock _lock;

        static ObjArray<Array<char>> _names;
    };

    // This class shouldn't be used explicitly
    class DLLEXPORT _ProfilingTimer
    {
    public:
        _ProfilingTimer(int name_index);
        ~_ProfilingTimer();

        qword stop();
        qword getTime() const;
        float getTimeSec() const;

    private:
        int _name_index;
        qword _start_time, _dt;
    };

    extern DLLEXPORT OsLock _profiling_global_lock;

} // namespace indigo

#ifdef _WIN32
#pragma warning(pop)
#endif

#endif // __profiling_h__