jdebug.hpp 8.3 KB

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