jdebug.hpp 8.7 KB

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