jdebug.hpp 9.7 KB

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