jdebug.hpp 11 KB

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