Bug 1832850 - Part 5: Move the allocateObject definition into gc/Nursery.h r=jandem
[gecko.git] / js / src / jsapi-tests / testProfileStrings.cpp
blob030b31ce8f3adf07f445d0e9cce391ca6c95e0fe
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:
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/. */
10 #include "mozilla/Atomics.h"
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"
19 static ProfilingStack profilingStack;
20 static uint32_t peakStackPointer = 0;
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);
29 static const JSClass ptestClass = {"Prof", 0};
31 static bool test_fn(JSContext* cx, unsigned argc, JS::Value* vp) {
32 peakStackPointer = profilingStack.stackPointer;
33 return true;
36 static bool test_fn2(JSContext* cx, unsigned argc, JS::Value* vp) {
37 JS::RootedValue r(cx);
38 JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx));
39 return JS_CallFunctionName(cx, global, "d", JS::HandleValueArray::empty(),
40 &r);
43 static bool enable(JSContext* cx, unsigned argc, JS::Value* vp) {
44 js::EnableContextProfilingStack(cx, true);
45 return true;
48 static bool disable(JSContext* cx, unsigned argc, JS::Value* vp) {
49 js::EnableContextProfilingStack(cx, false);
50 return true;
53 static bool Prof(JSContext* cx, unsigned argc, JS::Value* vp) {
54 JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
55 JSObject* obj = JS_NewObjectForConstructor(cx, &ptestClass, args);
56 if (!obj) {
57 return false;
59 args.rval().setObject(*obj);
60 return true;
63 static const JSFunctionSpec ptestFunctions[] = {
64 JS_FN("test_fn", test_fn, 0, 0), JS_FN("test_fn2", test_fn2, 0, 0),
65 JS_FN("enable", enable, 0, 0), JS_FN("disable", disable, 0, 0), JS_FS_END};
67 static JSObject* initialize(JSContext* cx) {
68 js::SetContextProfilingStack(cx, &profilingStack);
69 JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx));
70 return JS_InitClass(cx, global, nullptr, nullptr, "Prof", Prof, 0, nullptr,
71 ptestFunctions, nullptr, nullptr);
74 BEGIN_TEST(testProfileStrings_isCalledWithInterpreter) {
75 CHECK(initialize(cx));
77 EXEC("function g() { var p = new Prof(); p.test_fn(); }");
78 EXEC("function f() { g(); }");
79 EXEC("function e() { f(); }");
80 EXEC("function d() { e(); }");
81 EXEC("function c() { d(); }");
82 EXEC("function b() { c(); }");
83 EXEC("function a() { b(); }");
84 EXEC("function check() { var p = new Prof(); p.test_fn(); a(); }");
85 EXEC("function check2() { var p = new Prof(); p.test_fn2(); }");
87 reset(cx);
89 JS::RootedValue rval(cx);
90 /* Make sure the stack resets and we have an entry for each stack */
91 CHECK(JS_CallFunctionName(cx, global, "check",
92 JS::HandleValueArray::empty(), &rval));
93 CHECK(profilingStack.stackPointer == 0);
94 CHECK(peakStackPointer >= 8);
95 CHECK(cx->runtime()->geckoProfiler().stringsCount() == 8);
96 /* Make sure the stack resets and we added no new entries */
97 peakStackPointer = 0;
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);
104 reset(cx);
106 JS::RootedValue rval(cx);
107 CHECK(JS_CallFunctionName(cx, global, "check2",
108 JS::HandleValueArray::empty(), &rval));
109 CHECK(cx->runtime()->geckoProfiler().stringsCount() == 5);
110 CHECK(peakStackPointer >= 6);
111 CHECK(profilingStack.stackPointer == 0);
113 return true;
115 END_TEST(testProfileStrings_isCalledWithInterpreter)
117 BEGIN_TEST(testProfileStrings_isCalledWithJIT) {
118 CHECK(initialize(cx));
120 EXEC("function g() { var p = new Prof(); p.test_fn(); }");
121 EXEC("function f() { g(); }");
122 EXEC("function e() { f(); }");
123 EXEC("function d() { e(); }");
124 EXEC("function c() { d(); }");
125 EXEC("function b() { c(); }");
126 EXEC("function a() { b(); }");
127 EXEC("function check() { var p = new Prof(); p.test_fn(); a(); }");
128 EXEC("function check2() { var p = new Prof(); p.test_fn2(); }");
130 reset(cx);
132 JS::RootedValue rval(cx);
133 /* Make sure the stack resets and we have an entry for each stack */
134 CHECK(JS_CallFunctionName(cx, global, "check",
135 JS::HandleValueArray::empty(), &rval));
136 CHECK(profilingStack.stackPointer == 0);
137 CHECK(peakStackPointer >= 8);
139 /* Make sure the stack resets and we added no new entries */
140 uint32_t cnt = cx->runtime()->geckoProfiler().stringsCount();
141 peakStackPointer = 0;
142 CHECK(JS_CallFunctionName(cx, global, "check",
143 JS::HandleValueArray::empty(), &rval));
144 CHECK(profilingStack.stackPointer == 0);
145 CHECK(cx->runtime()->geckoProfiler().stringsCount() == cnt);
146 CHECK(peakStackPointer >= 8);
149 return true;
151 END_TEST(testProfileStrings_isCalledWithJIT)
153 BEGIN_TEST(testProfileStrings_isCalledWhenError) {
154 CHECK(initialize(cx));
156 EXEC("function check2() { throw 'a'; }");
158 reset(cx);
160 JS::RootedValue rval(cx);
161 /* Make sure the stack resets and we have an entry for each stack */
162 bool ok = JS_CallFunctionName(cx, global, "check2",
163 JS::HandleValueArray::empty(), &rval);
164 CHECK(!ok);
165 CHECK(profilingStack.stackPointer == 0);
166 CHECK(cx->runtime()->geckoProfiler().stringsCount() == 1);
168 JS_ClearPendingException(cx);
171 return true;
173 END_TEST(testProfileStrings_isCalledWhenError)
175 BEGIN_TEST(testProfileStrings_worksWhenEnabledOnTheFly) {
176 CHECK(initialize(cx));
178 EXEC("function b(p) { p.test_fn(); }");
179 EXEC("function a() { var p = new Prof(); p.enable(); b(p); }");
180 reset(cx);
181 js::EnableContextProfilingStack(cx, false);
183 /* enable it in the middle of JS and make sure things check out */
184 JS::RootedValue rval(cx);
185 JS_CallFunctionName(cx, global, "a", JS::HandleValueArray::empty(), &rval);
186 CHECK(profilingStack.stackPointer == 0);
187 CHECK(peakStackPointer >= 1);
188 CHECK(cx->runtime()->geckoProfiler().stringsCount() == 1);
191 EXEC("function d(p) { p.disable(); }");
192 EXEC("function c() { var p = new Prof(); d(p); }");
193 reset(cx);
195 /* now disable in the middle of js */
196 JS::RootedValue rval(cx);
197 JS_CallFunctionName(cx, global, "c", JS::HandleValueArray::empty(), &rval);
198 CHECK(profilingStack.stackPointer == 0);
201 EXEC("function e() { var p = new Prof(); d(p); p.enable(); b(p); }");
202 reset(cx);
204 /* now disable in the middle of js, but re-enable before final exit */
205 JS::RootedValue rval(cx);
206 JS_CallFunctionName(cx, global, "e", JS::HandleValueArray::empty(), &rval);
207 CHECK(profilingStack.stackPointer == 0);
208 CHECK(peakStackPointer >= 3);
211 EXEC("function h() { }");
212 EXEC("function g(p) { p.disable(); for (var i = 0; i < 100; i++) i++; }");
213 EXEC("function f() { g(new Prof()); }");
214 reset(cx);
215 cx->runtime()->geckoProfiler().enableSlowAssertions(false);
217 JS::RootedValue rval(cx);
218 /* disable, and make sure that if we try to re-enter the JIT the pop
219 * will still happen */
220 JS_CallFunctionName(cx, global, "f", JS::HandleValueArray::empty(), &rval);
221 CHECK(profilingStack.stackPointer == 0);
223 return true;
225 END_TEST(testProfileStrings_worksWhenEnabledOnTheFly)