jdebug.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*##############################################################################
  2. Copyright (C) 2011 HPCC Systems.
  3. All rights reserved. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ############################################################################## */
  14. #ifndef JDEBUG_HPP
  15. #define JDEBUG_HPP
  16. #include "jexpdef.hpp"
  17. #include "jiface.hpp"
  18. #define TIMING
  19. typedef __int64 cycle_t;
  20. __int64 jlib_decl cycle_to_nanosec(cycle_t cycles);
  21. cycle_t jlib_decl nanosec_to_cycle(__int64 cycles);
  22. cycle_t jlib_decl get_cycles_now(); // equivalent to getTSC when available
  23. double jlib_decl getCycleToNanoScale();
  24. void jlib_decl display_time(const char * title, cycle_t diff);
  25. #if defined(_WIN32) && ! defined (_AMD64_)
  26. #pragma warning(push)
  27. #pragma warning(disable:4035)
  28. inline cycle_t getTSC() { __asm { __asm _emit 0x0f __asm _emit 0x31 } }
  29. #pragma warning(pop)
  30. #elif !defined(_WIN32)
  31. inline volatile __int64 getTSC()
  32. {
  33. cycle_t x;
  34. unsigned a, d;
  35. __asm__ volatile("rdtsc" : "=a" (a), "=d" (d));
  36. return ((cycle_t)a)|(((cycle_t)d) << 32);
  37. }
  38. #else
  39. #include <intrin.h>
  40. inline cycle_t getTSC() { return __rdtsc(); }
  41. #endif
  42. struct HardwareInfo
  43. {
  44. unsigned numCPUs;
  45. unsigned CPUSpeed; // In MHz
  46. unsigned totalMemory; // In MB
  47. unsigned primDiskSize; // In GB
  48. unsigned primFreeSize; // In GB
  49. unsigned secDiskSize; // In GB
  50. unsigned secFreeSize; // In GB
  51. unsigned NICSpeed; //
  52. };
  53. interface ITimeReportInfo
  54. {
  55. virtual void report(const char *name, const __int64 totaltime, const __int64 maxtime, const unsigned count) = 0;
  56. };
  57. class StringBuffer;
  58. class MemoryBuffer;
  59. struct ITimeReporter : public IInterface
  60. {
  61. virtual void addTiming(const char *title, __int64 time) = 0;
  62. virtual void addTiming(const char *title, const __int64 totaltime, const __int64 maxtime, const unsigned count) = 0;
  63. virtual unsigned numSections() = 0;
  64. virtual __int64 getTime(unsigned idx) = 0;
  65. virtual __int64 getMaxTime(unsigned idx) = 0;
  66. virtual unsigned getCount(unsigned idx) = 0;
  67. virtual StringBuffer &getSection(unsigned idx, StringBuffer &s) = 0;
  68. virtual StringBuffer &getTimings(StringBuffer &s) = 0;
  69. virtual void printTimings() = 0;
  70. virtual void reset() = 0;
  71. virtual void mergeInto(ITimeReporter &other) = 0;
  72. virtual void merge(ITimeReporter &other)= 0;
  73. virtual void report(ITimeReportInfo &cb) = 0;
  74. virtual void serialize(MemoryBuffer &mb) = 0;
  75. };
  76. class CCycleTimer
  77. {
  78. cycle_t start_time;
  79. public:
  80. CCycleTimer()
  81. {
  82. reset();
  83. }
  84. inline void reset()
  85. {
  86. start_time = get_cycles_now();
  87. }
  88. inline cycle_t elapsedCycles()
  89. {
  90. return get_cycles_now() - start_time;
  91. }
  92. inline unsigned elapsedMs()
  93. {
  94. return cycle_to_nanosec(elapsedCycles())/1000000;
  95. }
  96. };
  97. class jlib_decl TimeSection
  98. {
  99. public:
  100. TimeSection(const char * _title);
  101. ~TimeSection();
  102. protected:
  103. const char * title;
  104. cycle_t start_time;
  105. };
  106. class jlib_decl MTimeSection
  107. {
  108. public:
  109. MTimeSection(ITimeReporter *_master, const char * _title);
  110. ~MTimeSection();
  111. protected:
  112. const char * title;
  113. cycle_t start_time;
  114. ITimeReporter *master;
  115. };
  116. #if defined(TIMING)
  117. extern jlib_decl ITimeReporter *defaultTimer; // MORE - this appears to be always exactly the same as timer. Should delete one or other of them?
  118. extern jlib_decl ITimeReporter *timer;
  119. extern jlib_decl ITimeReporter *createStdTimeReporter();
  120. extern jlib_decl ITimeReporter *createStdTimeReporter(MemoryBuffer &mb);
  121. #define TIME_SECTION(title) TimeSection glue(_timer,__LINE__)(title);
  122. #define MTIME_SECTION(master,title) MTimeSection glue(mtimer,__LINE__)(master, title);
  123. #else
  124. #define TIME_SECTION(title)
  125. #define MTIME_SECTION(master,title)
  126. #endif
  127. extern jlib_decl unsigned usTick();
  128. class jlib_decl HiresTimer
  129. {
  130. public:
  131. inline HiresTimer()
  132. {
  133. start=usTick();
  134. }
  135. inline void reset()
  136. {
  137. start=usTick();
  138. }
  139. inline double get()
  140. {
  141. return (double)(usTick()-start)/1000000;
  142. }
  143. private:
  144. unsigned start;
  145. };
  146. //===========================================================================
  147. #ifndef USING_MPATROL
  148. #ifdef _DEBUG
  149. #define LEAK_CHECK
  150. #endif
  151. #endif
  152. #ifdef LEAK_CHECK
  153. #ifdef _WIN32
  154. #include <stdio.h>
  155. #define _CRTDBG_MAP_ALLOC
  156. #include <crtdbg.h>
  157. #define CHECK_FOR_LEAKS(title) LeakChecker checker##__LINE__(title)
  158. class jlib_decl LeakChecker
  159. {
  160. public:
  161. LeakChecker(const char * _title);
  162. ~LeakChecker();
  163. protected:
  164. const char * title;
  165. _CrtMemState oldMemState;
  166. };
  167. extern jlib_decl void logLeaks (const char *logFile); // logFile may be NULL, leaks are logged to only stderr in this case
  168. extern jlib_decl void logLeaks (FILE *logHandle); // only use predefined file handles like stderr and stdout
  169. #else
  170. #define CHECK_FOR_LEAKS(title)
  171. #define logLeaks(name)
  172. #endif
  173. #else
  174. #define CHECK_FOR_LEAKS(title)
  175. #define logLeaks(name)
  176. #endif
  177. #if !defined(USING_MPATROL) && defined(_DEBUG) && (defined(_WIN32) || defined(WIN32))
  178. void jlib_decl enableMemLeakChecking(bool enable);
  179. #else
  180. #define enableMemLeakChecking(enable)
  181. #endif
  182. // Hook to be called by the performance monitor, takes stats for processor, virtual memory, disk, and thread usage
  183. class IPerfMonHook : public IInterface
  184. {
  185. public:
  186. virtual void processPerfStats(unsigned processorUsage, unsigned memoryUsage, unsigned memoryTotal, unsigned __int64 fistDiskUsage, unsigned __int64 firstDiskTotal, unsigned __int64 secondDiskUsage, unsigned __int64 secondDiskTotal, unsigned threadCount) = 0;
  187. virtual StringBuffer &extraLogging(StringBuffer &extra) = 0; // for extra periodic logging
  188. };
  189. enum
  190. {
  191. //do nothing modes:
  192. PerfMonSilent = 0x00,
  193. //individual components:
  194. PerfMonProcMem = 0x01,
  195. PerfMonPackets = 0x02,
  196. PerfMonDiskUsage = 0x04,
  197. //default and full modes:
  198. PerfMonExtended = 0x08,
  199. #ifdef _WIN32
  200. PerfMonStandard = PerfMonProcMem
  201. #else
  202. PerfMonStandard = PerfMonProcMem|PerfMonExtended
  203. #endif
  204. };
  205. interface IUserMetric : public IInterface
  206. {
  207. virtual unsigned __int64 queryCount() const = 0;
  208. virtual const char *queryName() const = 0;
  209. virtual const char *queryMatchString() const = 0;
  210. virtual void inc() = 0;
  211. virtual void reset() = 0;
  212. };
  213. extern jlib_decl IUserMetric * createUserMetric(const char *name, const char *matchString);
  214. typedef unsigned PerfMonMode;
  215. void jlib_decl getSystemTraceInfo(StringBuffer &str, PerfMonMode mode = PerfMonProcMem);
  216. void jlib_decl startPerformanceMonitor(unsigned interval, PerfMonMode traceMode = PerfMonStandard, IPerfMonHook * hook = 0);
  217. void jlib_decl stopPerformanceMonitor();
  218. void jlib_decl setPerformanceMonitorPrimaryFileSystem(char const * fs); // for monitoring disk1, defaults to C: (win) or / (linux)
  219. void jlib_decl setPerformanceMonitorSecondaryFileSystem(char const * fs); // for monitoring disk2, no default
  220. unsigned jlib_decl getLatestCPUUsage();
  221. __int64 jlib_decl getTotalMem();
  222. unsigned jlib_decl setAllocHook(bool on); // bwd compat returns unsigned
  223. #if defined(__GLIBC) && !defined(_DEBUG)
  224. #if __GLIBC_PREREQ(2, 14)
  225. #define USE_JLIB_ALLOC_HOOK extern void jlib_decl jlib_init_hook(); void (* volatile __malloc_initialize_hook) (void) = jlib_init_hook;
  226. #else
  227. #define USE_JLIB_ALLOC_HOOK extern void jlib_decl jlib_init_hook(); void (* __malloc_initialize_hook) (void) = jlib_init_hook;
  228. #endif
  229. #else
  230. #define USE_JLIB_ALLOC_HOOK
  231. #endif
  232. extern jlib_decl void getHardwareInfo(HardwareInfo &hdwInfo, const char *primDiskPath = NULL, const char *secDiskPath = NULL);
  233. extern jlib_decl memsize_t getMapInfo(const char *type);
  234. extern jlib_decl void getCpuInfo(unsigned &numCPUs, unsigned &CPUSpeed);
  235. extern jlib_decl unsigned getAffinityCpus();
  236. extern jlib_decl void printProcMap(const char *fn, bool printbody, bool printsummary, StringBuffer *lnout, MemoryBuffer *mb, bool useprintf);
  237. extern jlib_decl void PrintMemoryReport(bool full=true);
  238. extern jlib_decl void printAllocationSummary();
  239. #endif