2013-08-20 10:45:26 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef js_ProfilingStack_h
|
|
|
|
#define js_ProfilingStack_h
|
|
|
|
|
2017-05-26 02:37:28 +03:00
|
|
|
#include <algorithm>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2013-08-20 10:45:26 +04:00
|
|
|
#include "jsbytecode.h"
|
|
|
|
#include "jstypes.h"
|
2015-06-18 08:05:42 +03:00
|
|
|
#include "js/TypeDecls.h"
|
2013-08-20 10:45:26 +04:00
|
|
|
#include "js/Utility.h"
|
|
|
|
|
|
|
|
struct JSRuntime;
|
2016-04-13 12:03:44 +03:00
|
|
|
class JSTracer;
|
2013-08-20 10:45:26 +04:00
|
|
|
|
2017-05-26 02:51:31 +03:00
|
|
|
class PseudoStack;
|
|
|
|
|
2013-08-20 10:45:26 +04:00
|
|
|
namespace js {
|
|
|
|
|
|
|
|
// A call stack can be specified to the JS engine such that all JS entry/exits
|
|
|
|
// to functions push/pop an entry to/from the specified stack.
|
|
|
|
//
|
2017-01-25 01:08:15 +03:00
|
|
|
// For more detailed information, see vm/GeckoProfiler.h.
|
2013-08-20 10:45:26 +04:00
|
|
|
//
|
2017-03-11 08:10:36 +03:00
|
|
|
class ProfileEntry
|
2013-08-20 10:45:26 +04:00
|
|
|
{
|
2017-06-02 10:16:56 +03:00
|
|
|
// A ProfileEntry represents either a C++ profile entry or a JS one.
|
2014-05-29 02:44:41 +04:00
|
|
|
|
2017-05-18 10:17:46 +03:00
|
|
|
// Descriptive label for this entry. Must be a static string! Can be an
|
|
|
|
// empty string, but not a null pointer.
|
2017-06-02 10:16:56 +03:00
|
|
|
const char* label_;
|
2014-05-29 02:44:41 +04:00
|
|
|
|
2017-05-18 10:17:46 +03:00
|
|
|
// An additional descriptive string of this entry which is combined with
|
|
|
|
// |label_| in profiler output. Need not be (and usually isn't) static. Can
|
|
|
|
// be null.
|
2017-06-02 10:16:56 +03:00
|
|
|
const char* dynamicString_;
|
2017-03-23 02:37:33 +03:00
|
|
|
|
2014-05-29 02:44:41 +04:00
|
|
|
// Stack pointer for non-JS entries, the script pointer otherwise.
|
2017-06-02 10:16:56 +03:00
|
|
|
void* spOrScript;
|
2014-05-29 02:44:41 +04:00
|
|
|
|
|
|
|
// Line number for non-JS entries, the bytecode offset otherwise.
|
2017-06-02 10:16:56 +03:00
|
|
|
int32_t lineOrPcOffset;
|
2014-05-29 02:44:41 +04:00
|
|
|
|
2017-06-02 05:46:09 +03:00
|
|
|
// Bits 0..1 hold the Kind. Bits 2..3 are unused. Bits 4..12 hold the
|
|
|
|
// Category.
|
2017-06-02 10:16:56 +03:00
|
|
|
uint32_t kindAndCategory_;
|
2013-08-20 10:45:26 +04:00
|
|
|
|
2017-05-26 02:51:31 +03:00
|
|
|
static int32_t pcToOffset(JSScript* aScript, jsbytecode* aPc);
|
|
|
|
|
2013-08-20 10:45:26 +04:00
|
|
|
public:
|
2017-06-02 05:46:09 +03:00
|
|
|
enum class Kind : uint32_t {
|
|
|
|
// A normal C++ frame.
|
|
|
|
CPP_NORMAL = 0,
|
|
|
|
|
|
|
|
// A special C++ frame indicating the start of a run of JS pseudostack
|
|
|
|
// entries. CPP_MARKER_FOR_JS frames are ignored, except for the sp
|
|
|
|
// field.
|
|
|
|
CPP_MARKER_FOR_JS = 1,
|
|
|
|
|
|
|
|
// A normal JS frame.
|
|
|
|
JS_NORMAL = 2,
|
|
|
|
|
|
|
|
// An interpreter JS frame that has OSR-ed into baseline. JS_NORMAL
|
|
|
|
// frames can be converted to JS_OSR and back. JS_OSR frames are
|
|
|
|
// ignored.
|
|
|
|
JS_OSR = 3,
|
|
|
|
|
|
|
|
KIND_MASK = 0x3,
|
2014-05-29 02:44:41 +04:00
|
|
|
};
|
2014-04-11 19:58:55 +04:00
|
|
|
|
2016-03-22 12:17:20 +03:00
|
|
|
// Keep these in sync with devtools/client/performance/modules/categories.js
|
2015-01-26 01:22:08 +03:00
|
|
|
enum class Category : uint32_t {
|
2017-05-26 07:54:31 +03:00
|
|
|
OTHER = 1u << 4,
|
|
|
|
CSS = 1u << 5,
|
|
|
|
JS = 1u << 6,
|
|
|
|
GC = 1u << 7,
|
|
|
|
CC = 1u << 8,
|
|
|
|
NETWORK = 1u << 9,
|
|
|
|
GRAPHICS = 1u << 10,
|
|
|
|
STORAGE = 1u << 11,
|
|
|
|
EVENTS = 1u << 12,
|
2014-05-31 05:41:11 +04:00
|
|
|
|
|
|
|
FIRST = OTHER,
|
2017-06-02 05:46:09 +03:00
|
|
|
LAST = EVENTS,
|
|
|
|
|
|
|
|
CATEGORY_MASK = ~uint32_t(Kind::KIND_MASK),
|
2015-01-26 01:22:08 +03:00
|
|
|
};
|
2014-05-24 01:12:29 +04:00
|
|
|
|
2017-06-02 05:46:09 +03:00
|
|
|
static_assert((uint32_t(Category::FIRST) & uint32_t(Kind::KIND_MASK)) == 0,
|
|
|
|
"Category overlaps with Kind");
|
2015-09-14 12:57:00 +03:00
|
|
|
|
2017-06-02 10:16:56 +03:00
|
|
|
bool isCpp() const
|
2017-06-02 05:46:09 +03:00
|
|
|
{
|
|
|
|
Kind k = kind();
|
|
|
|
return k == Kind::CPP_NORMAL || k == Kind::CPP_MARKER_FOR_JS;
|
|
|
|
}
|
|
|
|
|
2017-06-02 10:16:56 +03:00
|
|
|
bool isJs() const
|
2017-06-02 05:46:09 +03:00
|
|
|
{
|
|
|
|
Kind k = kind();
|
|
|
|
return k == Kind::JS_NORMAL || k == Kind::JS_OSR;
|
|
|
|
}
|
2014-05-29 02:44:41 +04:00
|
|
|
|
2017-06-02 10:16:56 +03:00
|
|
|
void setLabel(const char* aLabel) { label_ = aLabel; }
|
|
|
|
const char* label() const { return label_; }
|
2014-05-29 02:44:41 +04:00
|
|
|
|
2017-06-02 10:16:56 +03:00
|
|
|
const char* dynamicString() const { return dynamicString_; }
|
2017-03-23 02:37:33 +03:00
|
|
|
|
2017-05-26 02:51:31 +03:00
|
|
|
void initCppFrame(const char* aLabel, const char* aDynamicString, void* sp, uint32_t aLine,
|
2017-06-02 10:16:56 +03:00
|
|
|
Kind aKind, Category aCategory)
|
2017-05-26 02:51:31 +03:00
|
|
|
{
|
|
|
|
label_ = aLabel;
|
|
|
|
dynamicString_ = aDynamicString;
|
|
|
|
spOrScript = sp;
|
2016-09-13 15:06:46 +03:00
|
|
|
lineOrPcOffset = static_cast<int32_t>(aLine);
|
2017-06-02 05:46:09 +03:00
|
|
|
kindAndCategory_ = uint32_t(aKind) | uint32_t(aCategory);
|
|
|
|
MOZ_ASSERT(isCpp());
|
2017-05-26 02:51:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void initJsFrame(const char* aLabel, const char* aDynamicString, JSScript* aScript,
|
2017-06-02 10:16:56 +03:00
|
|
|
jsbytecode* aPc)
|
2017-05-26 02:51:31 +03:00
|
|
|
{
|
|
|
|
label_ = aLabel;
|
|
|
|
dynamicString_ = aDynamicString;
|
|
|
|
spOrScript = aScript;
|
|
|
|
lineOrPcOffset = pcToOffset(aScript, aPc);
|
2017-06-02 05:46:09 +03:00
|
|
|
kindAndCategory_ = uint32_t(Kind::JS_NORMAL) | uint32_t(Category::JS);
|
|
|
|
MOZ_ASSERT(isJs());
|
2013-08-20 10:45:26 +04:00
|
|
|
}
|
|
|
|
|
2017-06-02 10:16:56 +03:00
|
|
|
void setKind(Kind aKind) {
|
2017-06-02 05:46:09 +03:00
|
|
|
kindAndCategory_ = uint32_t(aKind) | uint32_t(category());
|
2014-05-20 23:25:25 +04:00
|
|
|
}
|
2015-09-14 22:15:12 +03:00
|
|
|
|
2017-06-02 10:16:56 +03:00
|
|
|
Kind kind() const {
|
2017-06-02 05:46:09 +03:00
|
|
|
return Kind(kindAndCategory_ & uint32_t(Kind::KIND_MASK));
|
2015-09-14 22:15:12 +03:00
|
|
|
}
|
2014-05-27 05:51:50 +04:00
|
|
|
|
2017-06-02 10:16:56 +03:00
|
|
|
Category category() const {
|
2017-06-02 05:46:09 +03:00
|
|
|
return Category(kindAndCategory_ & uint32_t(Category::CATEGORY_MASK));
|
2015-01-16 04:11:22 +03:00
|
|
|
}
|
|
|
|
|
2017-06-02 10:16:56 +03:00
|
|
|
void* stackAddress() const {
|
2014-05-29 02:44:41 +04:00
|
|
|
MOZ_ASSERT(!isJs());
|
|
|
|
return spOrScript;
|
|
|
|
}
|
2017-05-26 07:54:31 +03:00
|
|
|
|
2017-06-02 10:16:56 +03:00
|
|
|
JS_PUBLIC_API(JSScript*) script() const;
|
2017-05-26 07:54:31 +03:00
|
|
|
|
2017-06-02 10:16:56 +03:00
|
|
|
uint32_t line() const {
|
2014-05-29 02:44:41 +04:00
|
|
|
MOZ_ASSERT(!isJs());
|
2016-09-13 15:06:46 +03:00
|
|
|
return static_cast<uint32_t>(lineOrPcOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note that the pointer returned might be invalid.
|
2017-06-02 10:16:56 +03:00
|
|
|
JSScript* rawScript() const {
|
2016-09-13 15:06:46 +03:00
|
|
|
MOZ_ASSERT(isJs());
|
|
|
|
return (JSScript*)spOrScript;
|
2014-05-29 02:44:41 +04:00
|
|
|
}
|
2013-08-20 10:45:26 +04:00
|
|
|
|
2017-01-25 01:08:15 +03:00
|
|
|
// We can't know the layout of JSScript, so look in vm/GeckoProfiler.cpp.
|
2017-06-02 10:16:56 +03:00
|
|
|
JS_FRIEND_API(jsbytecode*) pc() const;
|
|
|
|
void setPC(jsbytecode* pc);
|
2013-08-20 10:45:26 +04:00
|
|
|
|
2017-06-02 10:16:56 +03:00
|
|
|
void trace(JSTracer* trc);
|
2016-04-13 12:03:44 +03:00
|
|
|
|
2014-05-29 02:44:41 +04:00
|
|
|
// The offset of a pc into a script's code can actually be 0, so to
|
|
|
|
// signify a nullptr pc, use a -1 index. This is checked against in
|
|
|
|
// pc() and setPC() to set/get the right pc.
|
|
|
|
static const int32_t NullPCOffset = -1;
|
2013-08-20 10:45:26 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
JS_FRIEND_API(void)
|
2017-05-26 02:51:31 +03:00
|
|
|
SetContextProfilingStack(JSContext* cx, PseudoStack* pseudoStack);
|
2013-08-20 10:45:26 +04:00
|
|
|
|
|
|
|
JS_FRIEND_API(void)
|
2016-08-11 15:39:22 +03:00
|
|
|
EnableContextProfilingStack(JSContext* cx, bool enabled);
|
2013-08-20 10:45:26 +04:00
|
|
|
|
2014-03-03 23:36:08 +04:00
|
|
|
JS_FRIEND_API(void)
|
2016-08-11 15:39:22 +03:00
|
|
|
RegisterContextProfilingEventMarker(JSContext* cx, void (*fn)(const char*));
|
2014-03-03 23:36:08 +04:00
|
|
|
|
2013-08-20 10:45:26 +04:00
|
|
|
} // namespace js
|
|
|
|
|
2017-06-02 10:16:56 +03:00
|
|
|
// Each thread has its own PseudoStack. That thread modifies the PseudoStack,
|
|
|
|
// pushing and popping elements as necessary.
|
|
|
|
//
|
|
|
|
// The PseudoStack is also read periodically by the profiler's sampler thread.
|
|
|
|
// This happens only when the thread that owns the PseudoStack is suspended. So
|
|
|
|
// there are no genuine parallel accesses.
|
|
|
|
//
|
|
|
|
// However, it is possible for pushing/popping to be interrupted by a periodic
|
|
|
|
// sample. Because of this, we need pushing/popping to be effectively atomic.
|
|
|
|
//
|
|
|
|
// - When pushing a new entry, we increment the stack pointer -- making the new
|
|
|
|
// entry visible to the sampler thread -- only after the new entry has been
|
|
|
|
// fully written. The stack pointer is Atomic<> (with SequentiallyConsistent
|
|
|
|
// semantics) to ensure the incrementing is not reordered before the writes.
|
|
|
|
//
|
|
|
|
// - When popping an old entry, the only operation is the decrementing of the
|
|
|
|
// stack pointer, which is obviously atomic.
|
|
|
|
//
|
2017-05-26 02:37:28 +03:00
|
|
|
class PseudoStack
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PseudoStack()
|
|
|
|
: stackPointer(0)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~PseudoStack() {
|
|
|
|
// The label macros keep a reference to the PseudoStack to avoid a TLS
|
|
|
|
// access. If these are somehow not all cleared we will get a
|
|
|
|
// use-after-free so better to crash now.
|
|
|
|
MOZ_RELEASE_ASSERT(stackPointer == 0);
|
|
|
|
}
|
|
|
|
|
2017-05-26 02:51:31 +03:00
|
|
|
void pushCppFrame(const char* label, const char* dynamicString, void* sp, uint32_t line,
|
2017-06-02 05:46:09 +03:00
|
|
|
js::ProfileEntry::Kind kind, js::ProfileEntry::Category category) {
|
2017-05-26 02:51:31 +03:00
|
|
|
if (stackPointer < MaxEntries) {
|
2017-06-02 05:46:09 +03:00
|
|
|
entries[stackPointer].initCppFrame(label, dynamicString, sp, line, kind, category);
|
2017-05-26 02:37:28 +03:00
|
|
|
}
|
|
|
|
|
2017-05-26 02:51:31 +03:00
|
|
|
// This must happen at the end! The compiler will not reorder this
|
|
|
|
// update because stackPointer is Atomic.
|
|
|
|
stackPointer++;
|
|
|
|
}
|
2017-05-26 02:37:28 +03:00
|
|
|
|
2017-05-26 02:51:31 +03:00
|
|
|
void pushJsFrame(const char* label, const char* dynamicString, JSScript* script,
|
|
|
|
jsbytecode* pc) {
|
|
|
|
if (stackPointer < MaxEntries) {
|
|
|
|
entries[stackPointer].initJsFrame(label, dynamicString, script, pc);
|
|
|
|
}
|
2017-05-26 02:37:28 +03:00
|
|
|
|
|
|
|
// This must happen at the end! The compiler will not reorder this
|
|
|
|
// update because stackPointer is Atomic.
|
|
|
|
stackPointer++;
|
|
|
|
}
|
|
|
|
|
2017-05-26 02:51:31 +03:00
|
|
|
void pop() {
|
|
|
|
MOZ_ASSERT(stackPointer > 0);
|
|
|
|
stackPointer--;
|
2017-05-26 02:37:28 +03:00
|
|
|
}
|
|
|
|
|
2017-05-26 02:51:31 +03:00
|
|
|
uint32_t stackSize() const { return std::min(uint32_t(stackPointer), uint32_t(MaxEntries)); }
|
2017-05-26 02:37:28 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
// No copying.
|
|
|
|
PseudoStack(const PseudoStack&) = delete;
|
|
|
|
void operator=(const PseudoStack&) = delete;
|
|
|
|
|
|
|
|
public:
|
2017-05-26 02:51:31 +03:00
|
|
|
static const uint32_t MaxEntries = 1024;
|
|
|
|
|
2017-05-26 02:37:28 +03:00
|
|
|
// The stack entries.
|
2017-06-02 10:16:56 +03:00
|
|
|
js::ProfileEntry entries[MaxEntries];
|
2017-05-26 02:37:28 +03:00
|
|
|
|
2017-05-26 02:51:31 +03:00
|
|
|
// This may exceed MaxEntries, so instead use the stackSize() method to
|
|
|
|
// determine the number of valid samples in entries. When this is less
|
|
|
|
// than MaxEntries, it refers to the first free entry past the top of the
|
|
|
|
// in-use stack (i.e. entries[stackPointer - 1] is the top stack entry).
|
2017-06-02 10:16:56 +03:00
|
|
|
mozilla::Atomic<uint32_t, mozilla::SequentiallyConsistent> stackPointer;
|
2017-05-26 02:37:28 +03:00
|
|
|
};
|
|
|
|
|
2013-08-20 10:45:26 +04:00
|
|
|
#endif /* js_ProfilingStack_h */
|