summaryrefslogtreecommitdiff
path: root/src/third_party/mozjs-60/extract/js/src/vm/GeckoProfiler-inl.h
blob: de640c8e2bbdee5999d1f6c6e3deebe2ae1517e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* -*- 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 vm_GeckoProfiler_inl_h
#define vm_GeckoProfiler_inl_h

#include "vm/GeckoProfiler.h"

#include "vm/JSContext.h"
#include "vm/Runtime.h"

namespace js {

inline void
GeckoProfilerThread::updatePC(JSContext* cx, JSScript* script, jsbytecode* pc)
{
    if (!cx->runtime()->geckoProfiler().enabled())
        return;

    uint32_t sp = pseudoStack_->stackPointer;
    if (sp - 1 < PseudoStack::MaxEntries) {
        MOZ_ASSERT(sp > 0);
        MOZ_ASSERT(pseudoStack_->entries[sp - 1].rawScript() == script);
        pseudoStack_->entries[sp - 1].setPC(pc);
    }
}

/*
 * This class is used to suppress profiler sampling during
 * critical sections where stack state is not valid.
 */
class MOZ_RAII AutoSuppressProfilerSampling
{
  public:
    explicit AutoSuppressProfilerSampling(JSContext* cx MOZ_GUARD_OBJECT_NOTIFIER_PARAM);

    ~AutoSuppressProfilerSampling();

  private:
    JSContext* cx_;
    bool previouslyEnabled_;
    JSRuntime::AutoProhibitActiveContextChange prohibitContextChange_;
    MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};

MOZ_ALWAYS_INLINE
GeckoProfilerEntryMarker::GeckoProfilerEntryMarker(JSContext* cx,
                                                   JSScript* script
                                                   MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
  : profiler_(&cx->geckoProfiler())
{
    MOZ_GUARD_OBJECT_NOTIFIER_INIT;
    if (MOZ_LIKELY(!profiler_->installed())) {
        profiler_ = nullptr;
        return;
    }
#ifdef DEBUG
    spBefore_ = profiler_->stackPointer();
#endif

    // We want to push a CPP frame so the profiler can correctly order JS and native stacks.
    // Only the sp value is important.
    profiler_->pseudoStack_->pushCppFrame(
        /* label = */ "", /* dynamicString = */ nullptr, /* sp = */ this, /* line = */ 0,
        ProfileEntry::Kind::CPP_MARKER_FOR_JS, ProfileEntry::Category::OTHER);

    profiler_->pseudoStack_->pushJsFrame(
        "js::RunScript", /* dynamicString = */ nullptr, script, script->code());
}

MOZ_ALWAYS_INLINE
GeckoProfilerEntryMarker::~GeckoProfilerEntryMarker()
{
    if (MOZ_LIKELY(profiler_ == nullptr))
        return;

    profiler_->pseudoStack_->pop();    // the JS frame
    profiler_->pseudoStack_->pop();    // the BEGIN_PSEUDO_JS frame
    MOZ_ASSERT(spBefore_ == profiler_->stackPointer());
}

MOZ_ALWAYS_INLINE
AutoGeckoProfilerEntry::AutoGeckoProfilerEntry(JSContext* cx, const char* label,
                                               ProfileEntry::Category category
                                               MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
  : profiler_(&cx->geckoProfiler())
{
    MOZ_GUARD_OBJECT_NOTIFIER_INIT;
    if (MOZ_LIKELY(!profiler_->installed())) {
        profiler_ = nullptr;
        return;
    }
#ifdef DEBUG
    spBefore_ = profiler_->stackPointer();
#endif
    profiler_->pseudoStack_->pushCppFrame(label,
                                          /* dynamicString = */ nullptr,
                                          /* sp = */ this,
                                          /* line = */ 0,
                                          ProfileEntry::Kind::CPP_NORMAL,
                                          category);
}

MOZ_ALWAYS_INLINE
AutoGeckoProfilerEntry::~AutoGeckoProfilerEntry()
{
    if (MOZ_LIKELY(!profiler_))
        return;

    profiler_->pseudoStack_->pop();
    MOZ_ASSERT(spBefore_ == profiler_->stackPointer());
}

} // namespace js

#endif // vm_GeckoProfiler_inl_h