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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sts=4 et sw=4 tw=99:
*
* Tests the stack-based instrumentation profiler on a JSRuntime
*/
/* 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/. */
#include "jscntxt.h"
#include "jsapi-tests/tests.h"
static js::ProfileEntry pstack[10];
static uint32_t psize = 0;
static uint32_t max_stack = 0;
static void
reset(JSContext* cx)
{
psize = max_stack = 0;
memset(pstack, 0, sizeof(pstack));
cx->spsProfiler.stringsReset();
cx->spsProfiler.enableSlowAssertions(true);
js::EnableContextProfilingStack(cx, true);
}
static const JSClass ptestClass = {
"Prof", 0
};
static bool
test_fn(JSContext* cx, unsigned argc, JS::Value* vp)
{
max_stack = psize;
return true;
}
static bool
test_fn2(JSContext* cx, unsigned argc, JS::Value* vp)
{
JS::RootedValue r(cx);
JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx));
return JS_CallFunctionName(cx, global, "d", JS::HandleValueArray::empty(), &r);
}
static bool
enable(JSContext* cx, unsigned argc, JS::Value* vp)
{
js::EnableContextProfilingStack(cx, true);
return true;
}
static bool
disable(JSContext* cx, unsigned argc, JS::Value* vp)
{
js::EnableContextProfilingStack(cx, false);
return true;
}
static bool
Prof(JSContext* cx, unsigned argc, JS::Value* vp)
{
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JSObject* obj = JS_NewObjectForConstructor(cx, &ptestClass, args);
if (!obj)
return false;
args.rval().setObject(*obj);
return true;
}
static const JSFunctionSpec ptestFunctions[] = {
JS_FS("test_fn", test_fn, 0, 0),
JS_FS("test_fn2", test_fn2, 0, 0),
JS_FS("enable", enable, 0, 0),
JS_FS("disable", disable, 0, 0),
JS_FS_END
};
static JSObject*
initialize(JSContext* cx)
{
js::SetContextProfilingStack(cx, pstack, &psize, 10);
JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx));
return JS_InitClass(cx, global, nullptr, &ptestClass, Prof, 0,
nullptr, ptestFunctions, nullptr, nullptr);
}
BEGIN_TEST(testProfileStrings_isCalledWithInterpreter)
{
CHECK(initialize(cx));
EXEC("function g() { var p = new Prof(); p.test_fn(); }");
EXEC("function f() { g(); }");
EXEC("function e() { f(); }");
EXEC("function d() { e(); }");
EXEC("function c() { d(); }");
EXEC("function b() { c(); }");
EXEC("function a() { b(); }");
EXEC("function check() { var p = new Prof(); p.test_fn(); a(); }");
EXEC("function check2() { var p = new Prof(); p.test_fn2(); }");
reset(cx);
{
JS::RootedValue rval(cx);
/* Make sure the stack resets and we have an entry for each stack */
CHECK(JS_CallFunctionName(cx, global, "check", JS::HandleValueArray::empty(),
&rval));
CHECK(psize == 0);
CHECK(max_stack >= 8);
CHECK(cx->runtime()->spsProfiler.stringsCount() == 8);
/* Make sure the stack resets and we added no new entries */
max_stack = 0;
CHECK(JS_CallFunctionName(cx, global, "check", JS::HandleValueArray::empty(),
&rval));
CHECK(psize == 0);
CHECK(max_stack >= 8);
CHECK(cx->runtime()->spsProfiler.stringsCount() == 8);
}
reset(cx);
{
JS::RootedValue rval(cx);
CHECK(JS_CallFunctionName(cx, global, "check2", JS::HandleValueArray::empty(),
&rval));
CHECK(cx->runtime()->spsProfiler.stringsCount() == 5);
CHECK(max_stack >= 6);
CHECK(psize == 0);
}
js::EnableContextProfilingStack(cx, false);
js::SetContextProfilingStack(cx, pstack, &psize, 3);
reset(cx);
{
JS::RootedValue rval(cx);
pstack[3].setLabel((char*) 1234);
CHECK(JS_CallFunctionName(cx, global, "check", JS::HandleValueArray::empty(),
&rval));
CHECK((size_t) pstack[3].label() == 1234);
CHECK(max_stack >= 8);
CHECK(psize == 0);
}
return true;
}
END_TEST(testProfileStrings_isCalledWithInterpreter)
BEGIN_TEST(testProfileStrings_isCalledWithJIT)
{
CHECK(initialize(cx));
JS::ContextOptionsRef(cx).setBaseline(true)
.setIon(true);
EXEC("function g() { var p = new Prof(); p.test_fn(); }");
EXEC("function f() { g(); }");
EXEC("function e() { f(); }");
EXEC("function d() { e(); }");
EXEC("function c() { d(); }");
EXEC("function b() { c(); }");
EXEC("function a() { b(); }");
EXEC("function check() { var p = new Prof(); p.test_fn(); a(); }");
EXEC("function check2() { var p = new Prof(); p.test_fn2(); }");
reset(cx);
{
JS::RootedValue rval(cx);
/* Make sure the stack resets and we have an entry for each stack */
CHECK(JS_CallFunctionName(cx, global, "check", JS::HandleValueArray::empty(),
&rval));
CHECK(psize == 0);
CHECK(max_stack >= 8);
/* Make sure the stack resets and we added no new entries */
uint32_t cnt = cx->runtime()->spsProfiler.stringsCount();
max_stack = 0;
CHECK(JS_CallFunctionName(cx, global, "check", JS::HandleValueArray::empty(),
&rval));
CHECK(psize == 0);
CHECK(cx->runtime()->spsProfiler.stringsCount() == cnt);
CHECK(max_stack >= 8);
}
js::EnableContextProfilingStack(cx, false);
js::SetContextProfilingStack(cx, pstack, &psize, 3);
reset(cx);
{
/* Limit the size of the stack and make sure we don't overflow */
JS::RootedValue rval(cx);
pstack[3].setLabel((char*) 1234);
CHECK(JS_CallFunctionName(cx, global, "check", JS::HandleValueArray::empty(),
&rval));
CHECK(psize == 0);
CHECK(max_stack >= 8);
CHECK((size_t) pstack[3].label() == 1234);
}
return true;
}
END_TEST(testProfileStrings_isCalledWithJIT)
BEGIN_TEST(testProfileStrings_isCalledWhenError)
{
CHECK(initialize(cx));
JS::ContextOptionsRef(cx).setBaseline(true)
.setIon(true);
EXEC("function check2() { throw 'a'; }");
reset(cx);
{
JS::RootedValue rval(cx);
/* Make sure the stack resets and we have an entry for each stack */
bool ok = JS_CallFunctionName(cx, global, "check2", JS::HandleValueArray::empty(),
&rval);
CHECK(!ok);
CHECK(psize == 0);
CHECK(cx->runtime()->spsProfiler.stringsCount() == 1);
JS_ClearPendingException(cx);
}
return true;
}
END_TEST(testProfileStrings_isCalledWhenError)
BEGIN_TEST(testProfileStrings_worksWhenEnabledOnTheFly)
{
CHECK(initialize(cx));
JS::ContextOptionsRef(cx).setBaseline(true)
.setIon(true);
EXEC("function b(p) { p.test_fn(); }");
EXEC("function a() { var p = new Prof(); p.enable(); b(p); }");
reset(cx);
js::EnableContextProfilingStack(cx, false);
{
/* enable it in the middle of JS and make sure things check out */
JS::RootedValue rval(cx);
JS_CallFunctionName(cx, global, "a", JS::HandleValueArray::empty(), &rval);
CHECK(psize == 0);
CHECK(max_stack >= 1);
CHECK(cx->runtime()->spsProfiler.stringsCount() == 1);
}
EXEC("function d(p) { p.disable(); }");
EXEC("function c() { var p = new Prof(); d(p); }");
reset(cx);
{
/* now disable in the middle of js */
JS::RootedValue rval(cx);
JS_CallFunctionName(cx, global, "c", JS::HandleValueArray::empty(), &rval);
CHECK(psize == 0);
}
EXEC("function e() { var p = new Prof(); d(p); p.enable(); b(p); }");
reset(cx);
{
/* now disable in the middle of js, but re-enable before final exit */
JS::RootedValue rval(cx);
JS_CallFunctionName(cx, global, "e", JS::HandleValueArray::empty(), &rval);
CHECK(psize == 0);
CHECK(max_stack >= 3);
}
EXEC("function h() { }");
EXEC("function g(p) { p.disable(); for (var i = 0; i < 100; i++) i++; }");
EXEC("function f() { g(new Prof()); }");
reset(cx);
cx->runtime()->spsProfiler.enableSlowAssertions(false);
{
JS::RootedValue rval(cx);
/* disable, and make sure that if we try to re-enter the JIT the pop
* will still happen */
JS_CallFunctionName(cx, global, "f", JS::HandleValueArray::empty(), &rval);
CHECK(psize == 0);
}
return true;
}
END_TEST(testProfileStrings_worksWhenEnabledOnTheFly)
|