File: profiling.h

package info (click to toggle)
indigo 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 28,256 kB
  • sloc: ansic: 309,316; cpp: 137,636; cs: 9,118; asm: 8,011; java: 7,195; sql: 6,697; xml: 4,352; python: 3,426; sh: 207; php: 56; makefile: 49
file content (156 lines) | stat: -rw-r--r-- 4,514 bytes parent folder | download | duplicates (3)
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
/****************************************************************************
 * Copyright (C) 2009-2015 EPAM Systems
 * 
 * This file is part of Indigo toolkit.
 * 
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 3 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 * 
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 ***************************************************************************/

#ifndef __profiling_h__
#define __profiling_h__

#include "base_c/nano.h"
#include "base_cpp/os_sync_wrapper.h"
#include "base_cpp/array.h"
#include "base_cpp/obj_array.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;

}

#ifdef _WIN32
#pragma warning(pop)
#endif

#endif // __profiling_h__