| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* |
| 4 |
* Tests the stack-based instrumentation profiler on a JSRuntime |
| 5 |
*/ |
| 6 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 9 |
|
| 10 |
#include "mozilla/Atomics.h" |
| 11 |
|
| 12 |
#include "js/CallAndConstruct.h" |
| 13 |
#include "js/ContextOptions.h" |
| 14 |
#include "js/GlobalObject.h" |
| 15 |
#include "js/PropertySpec.h" |
| 16 |
#include "jsapi-tests/tests.h" |
| 17 |
#include "vm/JSContext.h" |
| 18 |
|
| 19 |
MOZ_RUNINIT static ProfilingStack profilingStack; |
| 20 |
static uint32_t peakStackPointer = 0; |
| 21 |
|
| 22 |
static void reset(JSContext* cx) { |
| 23 |
profilingStack.stackPointer = 0; |
| 24 |
cx->runtime()->geckoProfiler().stringsReset(); |
| 25 |
cx->runtime()->geckoProfiler().enableSlowAssertions(true); |
| 26 |
js::EnableContextProfilingStack(cx, true); |
| 27 |
} |
| 28 |
|
| 29 |
static const JSClass ptestClass = { |
| 30 |
"Prof", |
| 31 |
0, |
| 32 |
}; |
| 33 |
|
| 34 |
static bool test_fn(JSContext* cx, unsigned argc, JS::Value* vp) { |
| 35 |
peakStackPointer = profilingStack.stackPointer; |
| 36 |
return true; |
| 37 |
} |
| 38 |
|
| 39 |
static bool test_fn2(JSContext* cx, unsigned argc, JS::Value* vp) { |
| 40 |
JS::RootedValue r(cx); |
| 41 |
JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx)); |
| 42 |
return JS_CallFunctionName(cx, global, "d", JS::HandleValueArray::empty(), |
| 43 |
&r); |
| 44 |
} |
| 45 |
|
| 46 |
static bool enable(JSContext* cx, unsigned argc, JS::Value* vp) { |
| 47 |
js::EnableContextProfilingStack(cx, true); |
| 48 |
return true; |
| 49 |
} |
| 50 |
|
| 51 |
static bool disable(JSContext* cx, unsigned argc, JS::Value* vp) { |
| 52 |
js::EnableContextProfilingStack(cx, false); |
| 53 |
return true; |
| 54 |
} |
| 55 |
|
| 56 |
static bool Prof(JSContext* cx, unsigned argc, JS::Value* vp) { |
| 57 |
JS::CallArgs args = JS::CallArgsFromVp(argc, vp); |
| 58 |
JSObject* obj = JS_NewObjectForConstructor(cx, &ptestClass, args); |
| 59 |
if (!obj) { |
| 60 |
return false; |
| 61 |
} |
| 62 |
args.rval().setObject(*obj); |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
static const JSFunctionSpec ptestFunctions[] = { |
| 67 |
JS_FN("test_fn", test_fn, 0, 0), |
| 68 |
JS_FN("test_fn2", test_fn2, 0, 0), |
| 69 |
JS_FN("enable", enable, 0, 0), |
| 70 |
JS_FN("disable", disable, 0, 0), |
| 71 |
JS_FS_END, |
| 72 |
}; |
| 73 |
|
| 74 |
static JSObject* initialize(JSContext* cx) { |
| 75 |
js::SetContextProfilingStack(cx, &profilingStack); |
| 76 |
JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx)); |
| 77 |
return JS_InitClass(cx, global, nullptr, nullptr, "Prof", Prof, 0, nullptr, |
| 78 |
ptestFunctions, nullptr, nullptr); |
| 79 |
} |
| 80 |
|
| 81 |
BEGIN_TEST(testProfileStrings_isCalledWithInterpreter) { |
| 82 |
CHECK(initialize(cx)); |
| 83 |
|
| 84 |
EXEC("function g() { var p = new Prof(); p.test_fn(); }"); |
| 85 |
EXEC("function f() { g(); }"); |
| 86 |
EXEC("function e() { f(); }"); |
| 87 |
EXEC("function d() { e(); }"); |
| 88 |
EXEC("function c() { d(); }"); |
| 89 |
EXEC("function b() { c(); }"); |
| 90 |
EXEC("function a() { b(); }"); |
| 91 |
EXEC("function check() { var p = new Prof(); p.test_fn(); a(); }"); |
| 92 |
EXEC("function check2() { var p = new Prof(); p.test_fn2(); }"); |
| 93 |
|
| 94 |
reset(cx); |
| 95 |
{ |
| 96 |
JS::RootedValue rval(cx); |
| 97 |
/* Make sure the stack resets and we have an entry for each stack */ |
| 98 |
CHECK(JS_CallFunctionName(cx, global, "check", |
| 99 |
JS::HandleValueArray::empty(), &rval)); |
| 100 |
CHECK(profilingStack.stackPointer == 0); |
| 101 |
CHECK(peakStackPointer >= 8); |
| 102 |
CHECK(cx->runtime()->geckoProfiler().stringsCount() == 8); |
| 103 |
/* Make sure the stack resets and we added no new entries */ |
| 104 |
peakStackPointer = 0; |
| 105 |
CHECK(JS_CallFunctionName(cx, global, "check", |
| 106 |
JS::HandleValueArray::empty(), &rval)); |
| 107 |
CHECK(profilingStack.stackPointer == 0); |
| 108 |
CHECK(peakStackPointer >= 8); |
| 109 |
CHECK(cx->runtime()->geckoProfiler().stringsCount() == 8); |
| 110 |
} |
| 111 |
reset(cx); |
| 112 |
{ |
| 113 |
JS::RootedValue rval(cx); |
| 114 |
CHECK(JS_CallFunctionName(cx, global, "check2", |
| 115 |
JS::HandleValueArray::empty(), &rval)); |
| 116 |
CHECK(cx->runtime()->geckoProfiler().stringsCount() == 5); |
| 117 |
CHECK(peakStackPointer >= 6); |
| 118 |
CHECK(profilingStack.stackPointer == 0); |
| 119 |
} |
| 120 |
return true; |
| 121 |
} |
| 122 |
END_TEST(testProfileStrings_isCalledWithInterpreter) |
| 123 |
|
| 124 |
BEGIN_TEST(testProfileStrings_isCalledWithJIT) { |
| 125 |
CHECK(initialize(cx)); |
| 126 |
|
| 127 |
EXEC("function g() { var p = new Prof(); p.test_fn(); }"); |
| 128 |
EXEC("function f() { g(); }"); |
| 129 |
EXEC("function e() { f(); }"); |
| 130 |
EXEC("function d() { e(); }"); |
| 131 |
EXEC("function c() { d(); }"); |
| 132 |
EXEC("function b() { c(); }"); |
| 133 |
EXEC("function a() { b(); }"); |
| 134 |
EXEC("function check() { var p = new Prof(); p.test_fn(); a(); }"); |
| 135 |
EXEC("function check2() { var p = new Prof(); p.test_fn2(); }"); |
| 136 |
|
| 137 |
reset(cx); |
| 138 |
{ |
| 139 |
JS::RootedValue rval(cx); |
| 140 |
/* Make sure the stack resets and we have an entry for each stack */ |
| 141 |
CHECK(JS_CallFunctionName(cx, global, "check", |
| 142 |
JS::HandleValueArray::empty(), &rval)); |
| 143 |
CHECK(profilingStack.stackPointer == 0); |
| 144 |
CHECK(peakStackPointer >= 8); |
| 145 |
|
| 146 |
/* Make sure the stack resets and we added no new entries */ |
| 147 |
uint32_t cnt = cx->runtime()->geckoProfiler().stringsCount(); |
| 148 |
peakStackPointer = 0; |
| 149 |
CHECK(JS_CallFunctionName(cx, global, "check", |
| 150 |
JS::HandleValueArray::empty(), &rval)); |
| 151 |
CHECK(profilingStack.stackPointer == 0); |
| 152 |
CHECK(cx->runtime()->geckoProfiler().stringsCount() == cnt); |
| 153 |
CHECK(peakStackPointer >= 8); |
| 154 |
} |
| 155 |
|
| 156 |
return true; |
| 157 |
} |
| 158 |
END_TEST(testProfileStrings_isCalledWithJIT) |
| 159 |
|
| 160 |
BEGIN_TEST(testProfileStrings_isCalledWhenError) { |
| 161 |
CHECK(initialize(cx)); |
| 162 |
|
| 163 |
EXEC("function check2() { throw 'a'; }"); |
| 164 |
|
| 165 |
reset(cx); |
| 166 |
{ |
| 167 |
JS::RootedValue rval(cx); |
| 168 |
/* Make sure the stack resets and we have an entry for each stack */ |
| 169 |
bool ok = JS_CallFunctionName(cx, global, "check2", |
| 170 |
JS::HandleValueArray::empty(), &rval); |
| 171 |
CHECK(!ok); |
| 172 |
CHECK(profilingStack.stackPointer == 0); |
| 173 |
CHECK(cx->runtime()->geckoProfiler().stringsCount() == 1); |
| 174 |
|
| 175 |
JS_ClearPendingException(cx); |
| 176 |
} |
| 177 |
|
| 178 |
return true; |
| 179 |
} |
| 180 |
END_TEST(testProfileStrings_isCalledWhenError) |
| 181 |
|
| 182 |
BEGIN_TEST(testProfileStrings_worksWhenEnabledOnTheFly) { |
| 183 |
CHECK(initialize(cx)); |
| 184 |
|
| 185 |
EXEC("function b(p) { p.test_fn(); }"); |
| 186 |
EXEC("function a() { var p = new Prof(); p.enable(); b(p); }"); |
| 187 |
reset(cx); |
| 188 |
js::EnableContextProfilingStack(cx, false); |
| 189 |
{ |
| 190 |
/* enable it in the middle of JS and make sure things check out */ |
| 191 |
JS::RootedValue rval(cx); |
| 192 |
JS_CallFunctionName(cx, global, "a", JS::HandleValueArray::empty(), &rval); |
| 193 |
CHECK(profilingStack.stackPointer == 0); |
| 194 |
CHECK(peakStackPointer >= 1); |
| 195 |
CHECK(cx->runtime()->geckoProfiler().stringsCount() == 1); |
| 196 |
} |
| 197 |
|
| 198 |
EXEC("function d(p) { p.disable(); }"); |
| 199 |
EXEC("function c() { var p = new Prof(); d(p); }"); |
| 200 |
reset(cx); |
| 201 |
{ |
| 202 |
/* now disable in the middle of js */ |
| 203 |
JS::RootedValue rval(cx); |
| 204 |
JS_CallFunctionName(cx, global, "c", JS::HandleValueArray::empty(), &rval); |
| 205 |
CHECK(profilingStack.stackPointer == 0); |
| 206 |
} |
| 207 |
|
| 208 |
EXEC("function e() { var p = new Prof(); d(p); p.enable(); b(p); }"); |
| 209 |
reset(cx); |
| 210 |
{ |
| 211 |
/* now disable in the middle of js, but re-enable before final exit */ |
| 212 |
JS::RootedValue rval(cx); |
| 213 |
JS_CallFunctionName(cx, global, "e", JS::HandleValueArray::empty(), &rval); |
| 214 |
CHECK(profilingStack.stackPointer == 0); |
| 215 |
CHECK(peakStackPointer >= 3); |
| 216 |
} |
| 217 |
|
| 218 |
EXEC("function h() { }"); |
| 219 |
EXEC("function g(p) { p.disable(); for (var i = 0; i < 100; i++) i++; }"); |
| 220 |
EXEC("function f() { g(new Prof()); }"); |
| 221 |
reset(cx); |
| 222 |
cx->runtime()->geckoProfiler().enableSlowAssertions(false); |
| 223 |
{ |
| 224 |
JS::RootedValue rval(cx); |
| 225 |
/* disable, and make sure that if we try to re-enter the JIT the pop |
| 226 |
* will still happen */ |
| 227 |
JS_CallFunctionName(cx, global, "f", JS::HandleValueArray::empty(), &rval); |
| 228 |
CHECK(profilingStack.stackPointer == 0); |
| 229 |
} |
| 230 |
return true; |
| 231 |
} |
| 232 |
END_TEST(testProfileStrings_worksWhenEnabledOnTheFly) |