jdebug.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #define TIMING
  17. typedef __int64 cycle_t;
  18. __int64 jlib_decl cycle_to_nanosec(cycle_t cycles);
  19. cycle_t jlib_decl nanosec_to_cycle(__int64 cycles);
  20. cycle_t jlib_decl get_cycles_now(); // equivalent to getTSC when available
  21. double jlib_decl getCycleToNanoScale();
  22. void jlib_decl display_time(const char * title, cycle_t diff);
  23. #if defined(_WIN32) && ! defined (_AMD64_)
  24. #pragma warning(push)
  25. #pragma warning(disable:4035)
  26. inline cycle_t getTSC() { __asm { __asm _emit 0x0f __asm _emit 0x31 } }
  27. #pragma warning(pop)
  28. #elif !defined(_WIN32)
  29. inline volatile __int64 getTSC()
  30. {
  31. cycle_t x;
  32. unsigned a, d;
  33. __asm__ volatile("rdtsc" : "=a" (a), "=d" (d));
  34. return ((cycle_t)a)|(((cycle_t)d) << 32);
  35. }
  36. #else
  37. #include <intrin.h>
  38. inline cycle_t getTSC() { return __rdtsc(); }
  39. #endif
  40. struct HardwareInfo
  41. {
  42. unsigned numCPUs;
  43. unsigned CPUSpeed; // In MHz
  44. unsigned totalMemory; // In MB
  45. unsigned primDiskSize; // In GB
  46. unsigned primFreeSize; // In GB
  47. unsigned secDiskSize; // In GB
  48. unsigned secFreeSize; // In GB
  49. unsigned NICSpeed; //
  50. };
  51. interface ITimeReportInfo
  52. {
  53. virtual void report(const char *name, const __int64 totaltime, const __int64 maxtime, const unsigned count) = 0;
  54. };
  55. class StringBuffer;
  56. class MemoryBuffer;
  57. struct ITimeReporter : public IInterface
  58. {
  59. virtual void addTiming(const char *title, __int64 time) = 0;
  60. virtual void addTiming(const char *title, const __int64 totaltime, const __int64 maxtime, const unsigned count) = 0;
  61. virtual unsigned numSections() = 0;
  62. virtual __int64 getTime(unsigned idx) = 0;
  63. virtual __int64 getMaxTime(unsigned idx) = 0;
  64. virtual unsigned getCount(unsigned idx) = 0;
  65. virtual StringBuffer &getSection(unsigned idx, StringBuffer &s) = 0;
  66. virtual StringBuffer &getTimings(StringBuffer &s) = 0;
  67. virtual void printTimings() = 0;
  68. virtual void reset() = 0;
  69. virtual void mergeInto(ITimeReporter &other) = 0;
  70. virtual void merge(ITimeReporter &other)= 0;
  71. virtual void report(ITimeReportInfo &cb) = 0;
  72. virtual void serialize(MemoryBuffer &mb) = 0;
  73. };
  74. extern jlib_decl cycle_t oneSecInCycles;
  75. class CCycleTimer
  76. {
  77. cycle_t start_time;
  78. public:
  79. CCycleTimer()
  80. {
  81. reset();
  82. }
  83. inline void reset()
  84. {
  85. start_time = get_cycles_now();
  86. }
  87. inline cycle_t elapsedCycles()
  88. {
  89. return get_cycles_now() - start_time;
  90. }
  91. inline unsigned elapsedMs()
  92. {
  93. return static_cast<unsigned>(cycle_to_nanosec(elapsedCycles())/1000000);
  94. }
  95. };
  96. inline cycle_t queryOneSecCycles() { return oneSecInCycles; }
  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