jdebug.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 jlib_decl TimeSection
  77. {
  78. public:
  79. TimeSection(const char * _title);
  80. ~TimeSection();
  81. protected:
  82. const char * title;
  83. cycle_t start_time;
  84. };
  85. class jlib_decl MTimeSection
  86. {
  87. public:
  88. MTimeSection(ITimeReporter *_master, const char * _title);
  89. ~MTimeSection();
  90. protected:
  91. const char * title;
  92. cycle_t start_time;
  93. ITimeReporter *master;
  94. };
  95. #if defined(TIMING)
  96. extern jlib_decl ITimeReporter *defaultTimer; // MORE - this appears to be always exactly the same as timer. Should delete one or other of them?
  97. extern jlib_decl ITimeReporter *timer;
  98. extern jlib_decl ITimeReporter *createStdTimeReporter();
  99. extern jlib_decl ITimeReporter *createStdTimeReporter(MemoryBuffer &mb);
  100. #define TIME_SECTION(title) TimeSection glue(_timer,__LINE__)(title);
  101. #define MTIME_SECTION(master,title) MTimeSection glue(mtimer,__LINE__)(master, title);
  102. #else
  103. #define TIME_SECTION(title)
  104. #define MTIME_SECTION(master,title)
  105. #endif
  106. extern jlib_decl unsigned usTick();
  107. class jlib_decl HiresTimer
  108. {
  109. public:
  110. inline HiresTimer()
  111. {
  112. start=usTick();
  113. }
  114. inline void reset()
  115. {
  116. start=usTick();
  117. }
  118. inline double get()
  119. {
  120. return (double)(usTick()-start)/1000000;
  121. }
  122. private:
  123. unsigned start;
  124. };
  125. //===========================================================================
  126. #ifndef USING_MPATROL
  127. #ifdef _DEBUG
  128. #define LEAK_CHECK
  129. #endif
  130. #endif
  131. #ifdef LEAK_CHECK
  132. #ifdef _WIN32
  133. #include <stdio.h>
  134. #define _CRTDBG_MAP_ALLOC
  135. #include <crtdbg.h>
  136. #define CHECK_FOR_LEAKS(title) LeakChecker checker##__LINE__(title)
  137. class jlib_decl LeakChecker
  138. {
  139. public:
  140. LeakChecker(const char * _title);
  141. ~LeakChecker();
  142. protected:
  143. const char * title;
  144. _CrtMemState oldMemState;
  145. };
  146. extern jlib_decl void logLeaks (const char *logFile); // logFile may be NULL, leaks are logged to only stderr in this case
  147. extern jlib_decl void logLeaks (FILE *logHandle); // only use predefined file handles like stderr and stdout
  148. #else
  149. #define CHECK_FOR_LEAKS(title)
  150. #define logLeaks(name)
  151. #endif
  152. #else
  153. #define CHECK_FOR_LEAKS(title)
  154. #define logLeaks(name)
  155. #endif
  156. #if !defined(USING_MPATROL) && defined(_DEBUG) && (defined(_WIN32) || defined(WIN32))
  157. void jlib_decl enableMemLeakChecking(bool enable);
  158. #else
  159. #define enableMemLeakChecking(enable)
  160. #endif
  161. // Hook to be called by the performance monitor, takes stats for processor, virtual memory, disk, and thread usage
  162. class IPerfMonHook : public IInterface
  163. {
  164. public:
  165. virtual void processPerfStats(unsigned processorUsage, unsigned memoryUsage, unsigned memoryTotal, unsigned __int64 fistDiskUsage, unsigned __int64 firstDiskTotal, unsigned __int64 secondDiskUsage, unsigned __int64 secondDiskTotal, unsigned threadCount) = 0;
  166. virtual StringBuffer &extraLogging(StringBuffer &extra) = 0; // for extra periodic logging
  167. };
  168. enum
  169. {
  170. //do nothing modes:
  171. PerfMonSilent = 0x00,
  172. //individual components:
  173. PerfMonProcMem = 0x01,
  174. PerfMonPackets = 0x02,
  175. PerfMonDiskUsage = 0x04,
  176. //default and full modes:
  177. PerfMonExtended = 0x08,
  178. #ifdef _WIN32
  179. PerfMonStandard = PerfMonProcMem
  180. #else
  181. PerfMonStandard = PerfMonProcMem|PerfMonExtended
  182. #endif
  183. };
  184. interface IUserMetric : public IInterface
  185. {
  186. virtual unsigned __int64 queryCount() const = 0;
  187. virtual const char *queryName() const = 0;
  188. virtual const char *queryMatchString() const = 0;
  189. virtual void inc() = 0;
  190. virtual void reset() = 0;
  191. };
  192. extern jlib_decl IUserMetric * createUserMetric(const char *name, const char *matchString);
  193. typedef unsigned PerfMonMode;
  194. void jlib_decl getSystemTraceInfo(StringBuffer &str, PerfMonMode mode = PerfMonProcMem);
  195. void jlib_decl startPerformanceMonitor(unsigned interval, PerfMonMode traceMode = PerfMonStandard, IPerfMonHook * hook = 0);
  196. void jlib_decl stopPerformanceMonitor();
  197. void jlib_decl setPerformanceMonitorPrimaryFileSystem(char const * fs); // for monitoring disk1, defaults to C: (win) or / (linux)
  198. void jlib_decl setPerformanceMonitorSecondaryFileSystem(char const * fs); // for monitoring disk2, no default
  199. unsigned jlib_decl getLatestCPUUsage();
  200. __int64 jlib_decl getTotalMem();
  201. unsigned jlib_decl setAllocHook(bool on); // bwd compat returns unsigned
  202. #if defined(__GLIBC) && !defined(_DEBUG)
  203. #if __GLIBC_PREREQ(2, 14)
  204. #define USE_JLIB_ALLOC_HOOK extern void jlib_decl jlib_init_hook(); void (* volatile __malloc_initialize_hook) (void) = jlib_init_hook;
  205. #else
  206. #define USE_JLIB_ALLOC_HOOK extern void jlib_decl jlib_init_hook(); void (* __malloc_initialize_hook) (void) = jlib_init_hook;
  207. #endif
  208. #else
  209. #define USE_JLIB_ALLOC_HOOK
  210. #endif
  211. extern jlib_decl void getHardwareInfo(HardwareInfo &hdwInfo, const char *primDiskPath = NULL, const char *secDiskPath = NULL);
  212. extern jlib_decl void getCpuInfo(unsigned &numCPUs, unsigned &CPUSpeed);
  213. extern jlib_decl unsigned getAffinityCpus();
  214. extern jlib_decl void printProcMap(const char *fn, bool printbody, bool printsummary, StringBuffer *lnout, MemoryBuffer *mb, bool useprintf);
  215. extern jlib_decl void PrintMemoryReport(bool full=true);
  216. extern jlib_decl void printAllocationSummary();
  217. #endif