jdebug.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef JDEBUG_HPP
  14. #define JDEBUG_HPP
  15. #include "jiface.hpp"
  16. #include "jstats.h"
  17. #define TIMING
  18. __int64 jlib_decl cycle_to_nanosec(cycle_t cycles);
  19. __int64 jlib_decl cycle_to_microsec(cycle_t cycles);
  20. __int64 jlib_decl cycle_to_millisec(cycle_t cycles);
  21. cycle_t jlib_decl nanosec_to_cycle(__int64 cycles);
  22. double jlib_decl getCycleToNanoScale();
  23. void jlib_decl display_time(const char * title, cycle_t diff);
  24. // X86 / X86_64
  25. #if defined(_ARCH_X86_64_) || defined(_ARCH_X86_)
  26. #define HAS_GOOD_CYCLE_COUNTER
  27. #if defined(_WIN32) && defined (_ARCH_X86_)
  28. #pragma warning(push)
  29. #pragma warning(disable:4035)
  30. inline cycle_t getTSC() { __asm { __asm _emit 0x0f __asm _emit 0x31 } }
  31. #pragma warning(pop)
  32. #elif !defined(_WIN32)
  33. inline volatile __int64 getTSC()
  34. {
  35. cycle_t x;
  36. unsigned a, d;
  37. __asm__ volatile("rdtsc" : "=a" (a), "=d" (d));
  38. return ((cycle_t)a)|(((cycle_t)d) << 32);
  39. }
  40. #else
  41. #include <intrin.h>
  42. inline cycle_t getTSC() { return __rdtsc(); }
  43. #endif // WIN32
  44. #else
  45. // ARMFIX: cycle-count is not always available in user mode
  46. // http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0338g/Bihbeabc.html
  47. // http://neocontra.blogspot.co.uk/2013/05/user-mode-performance-counters-for.html
  48. inline cycle_t getTSC() { return 0; }
  49. #endif // X86
  50. #if defined(INLINE_GET_CYCLES_NOW) && defined(HAS_GOOD_CYCLE_COUNTER)
  51. inline cycle_t get_cycles_now() { return getTSC(); }
  52. #else
  53. cycle_t jlib_decl get_cycles_now(); // equivalent to getTSC when available
  54. #endif
  55. struct HardwareInfo
  56. {
  57. unsigned numCPUs;
  58. unsigned CPUSpeed; // In MHz
  59. unsigned totalMemory; // In MB
  60. unsigned primDiskSize; // In GB
  61. unsigned primFreeSize; // In GB
  62. unsigned secDiskSize; // In GB
  63. unsigned secFreeSize; // In GB
  64. unsigned NICSpeed; //
  65. };
  66. struct UserSystemTime_t
  67. {
  68. public:
  69. UserSystemTime_t() : user(0), system(0) {}
  70. unsigned user;
  71. unsigned system;
  72. };
  73. interface ITimeReportInfo
  74. {
  75. virtual void report(const char * scope, const char * description, const __int64 totaltime, const __int64 maxtime, const unsigned count) = 0;
  76. };
  77. class StringBuffer;
  78. class MemoryBuffer;
  79. struct ITimeReporter : public IInterface
  80. {
  81. virtual void addTiming(const char * scope, cycle_t cycles) = 0;
  82. virtual void mergeTiming(const char * scope, cycle_t totalcycles, cycle_t maxcycles, const unsigned count) = 0;
  83. virtual unsigned numSections() = 0;
  84. virtual __int64 getTime(unsigned idx) = 0;
  85. virtual __int64 getMaxTime(unsigned idx) = 0;
  86. virtual unsigned getCount(unsigned idx) = 0;
  87. virtual StatisticKind getTimerType(unsigned idx) = 0;
  88. virtual StatisticScopeType getScopeType(unsigned idx) = 0;
  89. virtual StringBuffer &getScope(unsigned idx, StringBuffer &s) = 0;
  90. virtual StringBuffer &getTimings(StringBuffer &s) = 0;
  91. virtual void printTimings() = 0;
  92. virtual void reset() = 0;
  93. virtual void mergeInto(ITimeReporter &other) = 0;
  94. virtual void merge(ITimeReporter &other)= 0;
  95. virtual void report(ITimeReportInfo &cb) = 0;
  96. virtual void serialize(MemoryBuffer &mb) = 0;
  97. };
  98. extern jlib_decl cycle_t oneSecInCycles;
  99. class CCycleTimer
  100. {
  101. cycle_t start_time;
  102. public:
  103. CCycleTimer()
  104. {
  105. reset();
  106. }
  107. inline void reset()
  108. {
  109. start_time = get_cycles_now();
  110. }
  111. inline cycle_t elapsedCycles()
  112. {
  113. return get_cycles_now() - start_time;
  114. }
  115. inline unsigned elapsedMs()
  116. {
  117. return static_cast<unsigned>(cycle_to_millisec(elapsedCycles()));
  118. }
  119. };
  120. inline cycle_t queryOneSecCycles() { return oneSecInCycles; }
  121. class jlib_decl TimeSection
  122. {
  123. public:
  124. TimeSection(const char * _title);
  125. ~TimeSection();
  126. protected:
  127. const char * title;
  128. cycle_t start_time;
  129. };
  130. class jlib_decl MTimeSection
  131. {
  132. public:
  133. MTimeSection(ITimeReporter *_master, const char * scope);
  134. ~MTimeSection();
  135. protected:
  136. const char * scope;
  137. const char * title;
  138. cycle_t start_time;
  139. ITimeReporter *master;
  140. };
  141. #if defined(TIMING)
  142. extern jlib_decl ITimeReporter * queryActiveTimer();
  143. extern jlib_decl ITimeReporter *createStdTimeReporter();
  144. extern jlib_decl ITimeReporter *createStdTimeReporter(MemoryBuffer &mb);
  145. #define TIME_SECTION(title) TimeSection glue(_timer,__LINE__)(title);
  146. #define MTIME_SECTION(master,title) MTimeSection glue(mtimer,__LINE__)(master, title);
  147. #else
  148. #define TIME_SECTION(title)
  149. #define MTIME_SECTION(master,title)
  150. #endif
  151. extern jlib_decl unsigned usTick();
  152. class jlib_decl HiresTimer
  153. {
  154. public:
  155. inline HiresTimer()
  156. {
  157. start=usTick();
  158. }
  159. inline void reset()
  160. {
  161. start=usTick();
  162. }
  163. inline double get()
  164. {
  165. return (double)(usTick()-start)/1000000;
  166. }
  167. private:
  168. unsigned start;
  169. };
  170. //===========================================================================
  171. #ifndef USING_MPATROL
  172. #ifdef _DEBUG
  173. #define LEAK_CHECK
  174. #endif
  175. #endif
  176. #ifdef LEAK_CHECK
  177. #ifdef _WIN32
  178. #include <stdio.h>
  179. #define _CRTDBG_MAP_ALLOC
  180. #include <crtdbg.h>
  181. #define CHECK_FOR_LEAKS(title) LeakChecker checker##__LINE__(title)
  182. class jlib_decl LeakChecker
  183. {
  184. public:
  185. LeakChecker(const char * _title);
  186. ~LeakChecker();
  187. protected:
  188. const char * title;
  189. _CrtMemState oldMemState;
  190. };
  191. extern jlib_decl void logLeaks (const char *logFile); // logFile may be NULL, leaks are logged to only stderr in this case
  192. extern jlib_decl void logLeaks (FILE *logHandle); // only use predefined file handles like stderr and stdout
  193. #else
  194. #define CHECK_FOR_LEAKS(title)
  195. #define logLeaks(name)
  196. #endif
  197. #else
  198. #define CHECK_FOR_LEAKS(title)
  199. #define logLeaks(name)
  200. #endif
  201. #if !defined(USING_MPATROL) && defined(_DEBUG) && (defined(_WIN32) || defined(WIN32))
  202. void jlib_decl enableMemLeakChecking(bool enable);
  203. #else
  204. #define enableMemLeakChecking(enable)
  205. #endif
  206. // Hook to be called by the performance monitor, takes stats for processor, virtual memory, disk, and thread usage
  207. interface IPerfMonHook : extends IInterface
  208. {
  209. public:
  210. virtual void processPerfStats(unsigned processorUsage, unsigned memoryUsage, unsigned memoryTotal, unsigned __int64 fistDiskUsage, unsigned __int64 firstDiskTotal, unsigned __int64 secondDiskUsage, unsigned __int64 secondDiskTotal, unsigned threadCount) = 0;
  211. virtual StringBuffer &extraLogging(StringBuffer &extra) = 0; // for extra periodic logging
  212. virtual void log(int level, const char *msg) = 0;
  213. };
  214. enum
  215. {
  216. //do nothing modes:
  217. PerfMonSilent = 0x00,
  218. //individual components:
  219. PerfMonProcMem = 0x01,
  220. PerfMonPackets = 0x02,
  221. PerfMonDiskUsage = 0x04,
  222. //default and full modes:
  223. PerfMonExtended = 0x08,
  224. #ifdef _WIN32
  225. PerfMonStandard = PerfMonProcMem
  226. #else
  227. PerfMonStandard = PerfMonProcMem|PerfMonExtended
  228. #endif
  229. };
  230. interface IUserMetric : extends IInterface
  231. {
  232. virtual unsigned __int64 queryCount() const = 0;
  233. virtual const char *queryName() const = 0;
  234. virtual const char *queryMatchString() const = 0;
  235. virtual void inc() = 0;
  236. virtual void reset() = 0;
  237. };
  238. extern jlib_decl IUserMetric * createUserMetric(const char *name, const char *matchString);
  239. typedef unsigned PerfMonMode;
  240. void jlib_decl getSystemTraceInfo(StringBuffer &str, PerfMonMode mode = PerfMonProcMem);
  241. void jlib_decl startPerformanceMonitor(unsigned interval, PerfMonMode traceMode = PerfMonStandard, IPerfMonHook * hook = NULL);
  242. void jlib_decl stopPerformanceMonitor();
  243. void jlib_decl setPerformanceMonitorPrimaryFileSystem(char const * fs); // for monitoring disk1, defaults to C: (win) or / (linux)
  244. void jlib_decl setPerformanceMonitorSecondaryFileSystem(char const * fs); // for monitoring disk2, no default
  245. unsigned jlib_decl getLatestCPUUsage();
  246. __int64 jlib_decl getTotalMem();
  247. unsigned jlib_decl setAllocHook(bool on); // bwd compat returns unsigned
  248. #if defined(__GLIBC) && !defined(_DEBUG)
  249. #if __GLIBC_PREREQ(2, 14)
  250. #define USE_JLIB_ALLOC_HOOK extern void jlib_decl jlib_init_hook(); void (* volatile __malloc_initialize_hook) (void) = jlib_init_hook;
  251. #else
  252. #define USE_JLIB_ALLOC_HOOK extern void jlib_decl jlib_init_hook(); void (* __malloc_initialize_hook) (void) = jlib_init_hook;
  253. #endif
  254. #else
  255. #define USE_JLIB_ALLOC_HOOK
  256. #endif
  257. extern jlib_decl void getHardwareInfo(HardwareInfo &hdwInfo, const char *primDiskPath = NULL, const char *secDiskPath = NULL);
  258. extern jlib_decl void getProcessTime(UserSystemTime_t & time);
  259. extern jlib_decl memsize_t getMapInfo(const char *type);
  260. extern jlib_decl void getCpuInfo(unsigned &numCPUs, unsigned &CPUSpeed);
  261. extern jlib_decl void getPeakMemUsage(memsize_t &peakVm,memsize_t &peakResident);
  262. extern jlib_decl unsigned getAffinityCpus();
  263. extern jlib_decl void printProcMap(const char *fn, bool printbody, bool printsummary, StringBuffer *lnout, MemoryBuffer *mb, bool useprintf);
  264. extern jlib_decl void PrintMemoryReport(bool full=true);
  265. extern jlib_decl void printAllocationSummary();
  266. extern jlib_decl bool areTransparentHugePagesEnabled();
  267. extern jlib_decl memsize_t getHugePageSize();
  268. #endif