jdebug.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. // X86 / X86_64
  24. #if defined(_ARCH_X86_64_) || defined(_ARCH_X86_)
  25. #if defined(_WIN32) && defined (_ARCH_X86_)
  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 // WIN32
  42. #else
  43. // ARMFIX: cycle-count is not always available in user mode
  44. // http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0338g/Bihbeabc.html
  45. // http://neocontra.blogspot.co.uk/2013/05/user-mode-performance-counters-for.html
  46. inline cycle_t getTSC() { return 0; }
  47. #endif // X86
  48. struct HardwareInfo
  49. {
  50. unsigned numCPUs;
  51. unsigned CPUSpeed; // In MHz
  52. unsigned totalMemory; // In MB
  53. unsigned primDiskSize; // In GB
  54. unsigned primFreeSize; // In GB
  55. unsigned secDiskSize; // In GB
  56. unsigned secFreeSize; // In GB
  57. unsigned NICSpeed; //
  58. };
  59. struct UserSystemTime_t
  60. {
  61. public:
  62. UserSystemTime_t() : user(0), system(0) {}
  63. unsigned user;
  64. unsigned system;
  65. };
  66. interface ITimeReportInfo
  67. {
  68. virtual void report(const char * scope, const char * description, const __int64 totaltime, const __int64 maxtime, const unsigned count) = 0;
  69. };
  70. class StringBuffer;
  71. class MemoryBuffer;
  72. struct ITimeReporter : public IInterface
  73. {
  74. virtual void addTiming(const char * scope, const char *desc, unsigned __int64 cycles) = 0;
  75. virtual void mergeTiming(const char * scope, const char *desc, const __int64 totalcycles, const __int64 maxcycles, const unsigned count) = 0;
  76. virtual unsigned numSections() = 0;
  77. virtual __int64 getTime(unsigned idx) = 0;
  78. virtual __int64 getMaxTime(unsigned idx) = 0;
  79. virtual unsigned getCount(unsigned idx) = 0;
  80. virtual StringBuffer &getScope(unsigned idx, StringBuffer &s) = 0;
  81. virtual StringBuffer &getDescription(unsigned idx, StringBuffer &s) = 0;
  82. virtual StringBuffer &getTimings(StringBuffer &s) = 0;
  83. virtual void printTimings() = 0;
  84. virtual void reset() = 0;
  85. virtual void mergeInto(ITimeReporter &other) = 0;
  86. virtual void merge(ITimeReporter &other)= 0;
  87. virtual void report(ITimeReportInfo &cb) = 0;
  88. virtual void serialize(MemoryBuffer &mb) = 0;
  89. };
  90. extern jlib_decl cycle_t oneSecInCycles;
  91. class CCycleTimer
  92. {
  93. cycle_t start_time;
  94. public:
  95. CCycleTimer()
  96. {
  97. reset();
  98. }
  99. inline void reset()
  100. {
  101. start_time = get_cycles_now();
  102. }
  103. inline cycle_t elapsedCycles()
  104. {
  105. return get_cycles_now() - start_time;
  106. }
  107. inline unsigned elapsedMs()
  108. {
  109. return static_cast<unsigned>(cycle_to_nanosec(elapsedCycles())/1000000);
  110. }
  111. };
  112. inline cycle_t queryOneSecCycles() { return oneSecInCycles; }
  113. class jlib_decl TimeSection
  114. {
  115. public:
  116. TimeSection(const char * _title);
  117. ~TimeSection();
  118. protected:
  119. const char * title;
  120. cycle_t start_time;
  121. };
  122. class jlib_decl MTimeSection
  123. {
  124. public:
  125. MTimeSection(ITimeReporter *_master, const char * scope, const char * _title);
  126. ~MTimeSection();
  127. protected:
  128. const char * scope;
  129. const char * title;
  130. cycle_t start_time;
  131. ITimeReporter *master;
  132. };
  133. #if defined(TIMING)
  134. extern jlib_decl ITimeReporter * queryActiveTimer();
  135. extern jlib_decl ITimeReporter *createStdTimeReporter();
  136. extern jlib_decl ITimeReporter *createStdTimeReporter(MemoryBuffer &mb);
  137. #define TIME_SECTION(title) TimeSection glue(_timer,__LINE__)(title);
  138. #define MTIME_SECTION(master,title) MTimeSection glue(mtimer,__LINE__)(master, "workunit;" title, title);
  139. #else
  140. #define TIME_SECTION(title)
  141. #define MTIME_SECTION(master,title)
  142. #endif
  143. extern jlib_decl unsigned usTick();
  144. class jlib_decl HiresTimer
  145. {
  146. public:
  147. inline HiresTimer()
  148. {
  149. start=usTick();
  150. }
  151. inline void reset()
  152. {
  153. start=usTick();
  154. }
  155. inline double get()
  156. {
  157. return (double)(usTick()-start)/1000000;
  158. }
  159. private:
  160. unsigned start;
  161. };
  162. //===========================================================================
  163. #ifndef USING_MPATROL
  164. #ifdef _DEBUG
  165. #define LEAK_CHECK
  166. #endif
  167. #endif
  168. #ifdef LEAK_CHECK
  169. #ifdef _WIN32
  170. #include <stdio.h>
  171. #define _CRTDBG_MAP_ALLOC
  172. #include <crtdbg.h>
  173. #define CHECK_FOR_LEAKS(title) LeakChecker checker##__LINE__(title)
  174. class jlib_decl LeakChecker
  175. {
  176. public:
  177. LeakChecker(const char * _title);
  178. ~LeakChecker();
  179. protected:
  180. const char * title;
  181. _CrtMemState oldMemState;
  182. };
  183. extern jlib_decl void logLeaks (const char *logFile); // logFile may be NULL, leaks are logged to only stderr in this case
  184. extern jlib_decl void logLeaks (FILE *logHandle); // only use predefined file handles like stderr and stdout
  185. #else
  186. #define CHECK_FOR_LEAKS(title)
  187. #define logLeaks(name)
  188. #endif
  189. #else
  190. #define CHECK_FOR_LEAKS(title)
  191. #define logLeaks(name)
  192. #endif
  193. #if !defined(USING_MPATROL) && defined(_DEBUG) && (defined(_WIN32) || defined(WIN32))
  194. void jlib_decl enableMemLeakChecking(bool enable);
  195. #else
  196. #define enableMemLeakChecking(enable)
  197. #endif
  198. // Hook to be called by the performance monitor, takes stats for processor, virtual memory, disk, and thread usage
  199. class IPerfMonHook : public IInterface
  200. {
  201. public:
  202. virtual void processPerfStats(unsigned processorUsage, unsigned memoryUsage, unsigned memoryTotal, unsigned __int64 fistDiskUsage, unsigned __int64 firstDiskTotal, unsigned __int64 secondDiskUsage, unsigned __int64 secondDiskTotal, unsigned threadCount) = 0;
  203. virtual StringBuffer &extraLogging(StringBuffer &extra) = 0; // for extra periodic logging
  204. };
  205. enum
  206. {
  207. //do nothing modes:
  208. PerfMonSilent = 0x00,
  209. //individual components:
  210. PerfMonProcMem = 0x01,
  211. PerfMonPackets = 0x02,
  212. PerfMonDiskUsage = 0x04,
  213. //default and full modes:
  214. PerfMonExtended = 0x08,
  215. #ifdef _WIN32
  216. PerfMonStandard = PerfMonProcMem
  217. #else
  218. PerfMonStandard = PerfMonProcMem|PerfMonExtended
  219. #endif
  220. };
  221. interface IUserMetric : public IInterface
  222. {
  223. virtual unsigned __int64 queryCount() const = 0;
  224. virtual const char *queryName() const = 0;
  225. virtual const char *queryMatchString() const = 0;
  226. virtual void inc() = 0;
  227. virtual void reset() = 0;
  228. };
  229. extern jlib_decl IUserMetric * createUserMetric(const char *name, const char *matchString);
  230. typedef unsigned PerfMonMode;
  231. void jlib_decl getSystemTraceInfo(StringBuffer &str, PerfMonMode mode = PerfMonProcMem);
  232. void jlib_decl startPerformanceMonitor(unsigned interval, PerfMonMode traceMode = PerfMonStandard, IPerfMonHook * hook = 0);
  233. void jlib_decl stopPerformanceMonitor();
  234. void jlib_decl setPerformanceMonitorPrimaryFileSystem(char const * fs); // for monitoring disk1, defaults to C: (win) or / (linux)
  235. void jlib_decl setPerformanceMonitorSecondaryFileSystem(char const * fs); // for monitoring disk2, no default
  236. unsigned jlib_decl getLatestCPUUsage();
  237. __int64 jlib_decl getTotalMem();
  238. unsigned jlib_decl setAllocHook(bool on); // bwd compat returns unsigned
  239. #if defined(__GLIBC) && !defined(_DEBUG)
  240. #if __GLIBC_PREREQ(2, 14)
  241. #define USE_JLIB_ALLOC_HOOK extern void jlib_decl jlib_init_hook(); void (* volatile __malloc_initialize_hook) (void) = jlib_init_hook;
  242. #else
  243. #define USE_JLIB_ALLOC_HOOK extern void jlib_decl jlib_init_hook(); void (* __malloc_initialize_hook) (void) = jlib_init_hook;
  244. #endif
  245. #else
  246. #define USE_JLIB_ALLOC_HOOK
  247. #endif
  248. extern jlib_decl void getHardwareInfo(HardwareInfo &hdwInfo, const char *primDiskPath = NULL, const char *secDiskPath = NULL);
  249. extern jlib_decl void getProcessTime(UserSystemTime_t & time);
  250. extern jlib_decl memsize_t getMapInfo(const char *type);
  251. extern jlib_decl void getCpuInfo(unsigned &numCPUs, unsigned &CPUSpeed);
  252. extern jlib_decl void getPeakMemUsage(memsize_t &peakVm,memsize_t &peakResident);
  253. extern jlib_decl unsigned getAffinityCpus();
  254. extern jlib_decl void printProcMap(const char *fn, bool printbody, bool printsummary, StringBuffer *lnout, MemoryBuffer *mb, bool useprintf);
  255. extern jlib_decl void PrintMemoryReport(bool full=true);
  256. extern jlib_decl void printAllocationSummary();
  257. #endif