jdebug.hpp 9.3 KB

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