ProfilingStack.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef js_ProfilingStack_h
  6. #define js_ProfilingStack_h
  7. #include "jsbytecode.h"
  8. #include "jstypes.h"
  9. #include "js/TypeDecls.h"
  10. #include "js/Utility.h"
  11. struct JSRuntime;
  12. class JSTracer;
  13. namespace js {
  14. // A call stack can be specified to the JS engine such that all JS entry/exits
  15. // to functions push/pop an entry to/from the specified stack.
  16. //
  17. // For more detailed information, see vm/SPSProfiler.h.
  18. //
  19. class ProfileEntry
  20. {
  21. // All fields are marked volatile to prevent the compiler from re-ordering
  22. // instructions. Namely this sequence:
  23. //
  24. // entry[size] = ...;
  25. // size++;
  26. //
  27. // If the size modification were somehow reordered before the stores, then
  28. // if a sample were taken it would be examining bogus information.
  29. //
  30. // A ProfileEntry represents both a C++ profile entry and a JS one.
  31. // Descriptive string of this entry.
  32. const char * volatile string;
  33. // Stack pointer for non-JS entries, the script pointer otherwise.
  34. void * volatile spOrScript;
  35. // Line number for non-JS entries, the bytecode offset otherwise.
  36. int32_t volatile lineOrPcOffset;
  37. // General purpose storage describing this frame.
  38. uint32_t volatile flags_;
  39. public:
  40. // These traits are bit masks. Make sure they're powers of 2.
  41. enum Flags : uint32_t {
  42. // Indicate whether a profile entry represents a CPP frame. If not set,
  43. // a JS frame is assumed by default. You're not allowed to publicly
  44. // change the frame type. Instead, initialize the ProfileEntry as either
  45. // a JS or CPP frame with `initJsFrame` or `initCppFrame` respectively.
  46. IS_CPP_ENTRY = 0x01,
  47. // Indicate that copying the frame label is not necessary when taking a
  48. // sample of the pseudostack.
  49. FRAME_LABEL_COPY = 0x02,
  50. // This ProfileEntry is a dummy entry indicating the start of a run
  51. // of JS pseudostack entries.
  52. BEGIN_PSEUDO_JS = 0x04,
  53. // This flag is used to indicate that an interpreter JS entry has OSR-ed
  54. // into baseline.
  55. OSR = 0x08,
  56. // Union of all flags.
  57. ALL = IS_CPP_ENTRY|FRAME_LABEL_COPY|BEGIN_PSEUDO_JS|OSR,
  58. // Mask for removing all flags except the category information.
  59. CATEGORY_MASK = ~ALL
  60. };
  61. // Keep these in sync with devtools/client/performance/modules/categories.js
  62. enum class Category : uint32_t {
  63. OTHER = 0x10,
  64. CSS = 0x20,
  65. JS = 0x40,
  66. GC = 0x80,
  67. CC = 0x100,
  68. NETWORK = 0x200,
  69. GRAPHICS = 0x400,
  70. STORAGE = 0x800,
  71. EVENTS = 0x1000,
  72. FIRST = OTHER,
  73. LAST = EVENTS
  74. };
  75. static_assert((static_cast<int>(Category::FIRST) & Flags::ALL) == 0,
  76. "The category bitflags should not intersect with the other flags!");
  77. // All of these methods are marked with the 'volatile' keyword because SPS's
  78. // representation of the stack is stored such that all ProfileEntry
  79. // instances are volatile. These methods would not be available unless they
  80. // were marked as volatile as well.
  81. bool isCpp() const volatile { return hasFlag(IS_CPP_ENTRY); }
  82. bool isJs() const volatile { return !isCpp(); }
  83. bool isCopyLabel() const volatile { return hasFlag(FRAME_LABEL_COPY); }
  84. void setLabel(const char* aString) volatile { string = aString; }
  85. const char* label() const volatile { return string; }
  86. void initJsFrame(JSScript* aScript, jsbytecode* aPc) volatile {
  87. flags_ = 0;
  88. spOrScript = aScript;
  89. setPC(aPc);
  90. }
  91. void initCppFrame(void* aSp, uint32_t aLine) volatile {
  92. flags_ = IS_CPP_ENTRY;
  93. spOrScript = aSp;
  94. lineOrPcOffset = static_cast<int32_t>(aLine);
  95. }
  96. void setFlag(uint32_t flag) volatile {
  97. MOZ_ASSERT(flag != IS_CPP_ENTRY);
  98. flags_ |= flag;
  99. }
  100. void unsetFlag(uint32_t flag) volatile {
  101. MOZ_ASSERT(flag != IS_CPP_ENTRY);
  102. flags_ &= ~flag;
  103. }
  104. bool hasFlag(uint32_t flag) const volatile {
  105. return bool(flags_ & flag);
  106. }
  107. uint32_t flags() const volatile {
  108. return flags_;
  109. }
  110. uint32_t category() const volatile {
  111. return flags_ & CATEGORY_MASK;
  112. }
  113. void setCategory(Category c) volatile {
  114. MOZ_ASSERT(c >= Category::FIRST);
  115. MOZ_ASSERT(c <= Category::LAST);
  116. flags_ &= ~CATEGORY_MASK;
  117. setFlag(static_cast<uint32_t>(c));
  118. }
  119. void setOSR() volatile {
  120. MOZ_ASSERT(isJs());
  121. setFlag(OSR);
  122. }
  123. void unsetOSR() volatile {
  124. MOZ_ASSERT(isJs());
  125. unsetFlag(OSR);
  126. }
  127. bool isOSR() const volatile {
  128. return hasFlag(OSR);
  129. }
  130. void* stackAddress() const volatile {
  131. MOZ_ASSERT(!isJs());
  132. return spOrScript;
  133. }
  134. JS_PUBLIC_API(JSScript*) script() const volatile;
  135. uint32_t line() const volatile {
  136. MOZ_ASSERT(!isJs());
  137. return static_cast<uint32_t>(lineOrPcOffset);
  138. }
  139. // Note that the pointer returned might be invalid.
  140. JSScript* rawScript() const volatile {
  141. MOZ_ASSERT(isJs());
  142. return (JSScript*)spOrScript;
  143. }
  144. // We can't know the layout of JSScript, so look in vm/SPSProfiler.cpp.
  145. JS_FRIEND_API(jsbytecode*) pc() const volatile;
  146. JS_FRIEND_API(void) setPC(jsbytecode* pc) volatile;
  147. void trace(JSTracer* trc);
  148. // The offset of a pc into a script's code can actually be 0, so to
  149. // signify a nullptr pc, use a -1 index. This is checked against in
  150. // pc() and setPC() to set/get the right pc.
  151. static const int32_t NullPCOffset = -1;
  152. static size_t offsetOfLabel() { return offsetof(ProfileEntry, string); }
  153. static size_t offsetOfSpOrScript() { return offsetof(ProfileEntry, spOrScript); }
  154. static size_t offsetOfLineOrPcOffset() { return offsetof(ProfileEntry, lineOrPcOffset); }
  155. static size_t offsetOfFlags() { return offsetof(ProfileEntry, flags_); }
  156. };
  157. JS_FRIEND_API(void)
  158. SetContextProfilingStack(JSContext* cx, ProfileEntry* stack, uint32_t* size,
  159. uint32_t max);
  160. JS_FRIEND_API(void)
  161. EnableContextProfilingStack(JSContext* cx, bool enabled);
  162. JS_FRIEND_API(void)
  163. RegisterContextProfilingEventMarker(JSContext* cx, void (*fn)(const char*));
  164. JS_FRIEND_API(jsbytecode*)
  165. ProfilingGetPC(JSContext* cx, JSScript* script, void* ip);
  166. } // namespace js
  167. #endif /* js_ProfilingStack_h */