michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * michael@0: * Tests the stack-based instrumentation profiler on a JSRuntime michael@0: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "jscntxt.h" michael@0: michael@0: #include "jsapi-tests/tests.h" michael@0: michael@0: static js::ProfileEntry pstack[10]; michael@0: static uint32_t psize = 0; michael@0: static uint32_t max_stack = 0; michael@0: michael@0: static void michael@0: reset(JSContext *cx) michael@0: { michael@0: psize = max_stack = 0; michael@0: memset(pstack, 0, sizeof(pstack)); michael@0: cx->runtime()->spsProfiler.stringsReset(); michael@0: cx->runtime()->spsProfiler.enableSlowAssertions(true); michael@0: js::EnableRuntimeProfilingStack(cx->runtime(), true); michael@0: } michael@0: michael@0: static const JSClass ptestClass = { michael@0: "Prof", 0, JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, michael@0: JS_StrictPropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub michael@0: }; michael@0: michael@0: static bool michael@0: test_fn(JSContext *cx, unsigned argc, jsval *vp) michael@0: { michael@0: max_stack = psize; michael@0: return true; michael@0: } michael@0: michael@0: static bool michael@0: test_fn2(JSContext *cx, unsigned argc, jsval *vp) michael@0: { michael@0: JS::RootedValue r(cx); michael@0: JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx)); michael@0: return JS_CallFunctionName(cx, global, "d", JS::HandleValueArray::empty(), &r); michael@0: } michael@0: michael@0: static bool michael@0: enable(JSContext *cx, unsigned argc, jsval *vp) michael@0: { michael@0: js::EnableRuntimeProfilingStack(cx->runtime(), true); michael@0: return true; michael@0: } michael@0: michael@0: static bool michael@0: disable(JSContext *cx, unsigned argc, jsval *vp) michael@0: { michael@0: js::EnableRuntimeProfilingStack(cx->runtime(), false); michael@0: return true; michael@0: } michael@0: michael@0: static bool michael@0: Prof(JSContext* cx, unsigned argc, jsval *vp) michael@0: { michael@0: JS::CallArgs args = JS::CallArgsFromVp(argc, vp); michael@0: JSObject *obj = JS_NewObjectForConstructor(cx, &ptestClass, args); michael@0: if (!obj) michael@0: return false; michael@0: args.rval().setObject(*obj); michael@0: return true; michael@0: } michael@0: michael@0: static const JSFunctionSpec ptestFunctions[] = { michael@0: JS_FS("test_fn", test_fn, 0, 0), michael@0: JS_FS("test_fn2", test_fn2, 0, 0), michael@0: JS_FS("enable", enable, 0, 0), michael@0: JS_FS("disable", disable, 0, 0), michael@0: JS_FS_END michael@0: }; michael@0: michael@0: static JSObject* michael@0: initialize(JSContext *cx) michael@0: { michael@0: js::SetRuntimeProfilingStack(cx->runtime(), pstack, &psize, 10); michael@0: JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx)); michael@0: return JS_InitClass(cx, global, js::NullPtr(), &ptestClass, Prof, 0, michael@0: nullptr, ptestFunctions, nullptr, nullptr); michael@0: } michael@0: michael@0: BEGIN_TEST(testProfileStrings_isCalledWithInterpreter) michael@0: { michael@0: CHECK(initialize(cx)); michael@0: michael@0: EXEC("function g() { var p = new Prof(); p.test_fn(); }"); michael@0: EXEC("function f() { g(); }"); michael@0: EXEC("function e() { f(); }"); michael@0: EXEC("function d() { e(); }"); michael@0: EXEC("function c() { d(); }"); michael@0: EXEC("function b() { c(); }"); michael@0: EXEC("function a() { b(); }"); michael@0: EXEC("function check() { var p = new Prof(); p.test_fn(); a(); }"); michael@0: EXEC("function check2() { var p = new Prof(); p.test_fn2(); }"); michael@0: michael@0: reset(cx); michael@0: { michael@0: JS::RootedValue rval(cx); michael@0: /* Make sure the stack resets and we have an entry for each stack */ michael@0: CHECK(JS_CallFunctionName(cx, global, "check", JS::HandleValueArray::empty(), michael@0: &rval)); michael@0: CHECK(psize == 0); michael@0: CHECK(max_stack >= 8); michael@0: CHECK(cx->runtime()->spsProfiler.stringsCount() == 8); michael@0: /* Make sure the stack resets and we added no new entries */ michael@0: max_stack = 0; michael@0: CHECK(JS_CallFunctionName(cx, global, "check", JS::HandleValueArray::empty(), michael@0: &rval)); michael@0: CHECK(psize == 0); michael@0: CHECK(max_stack >= 8); michael@0: CHECK(cx->runtime()->spsProfiler.stringsCount() == 8); michael@0: } michael@0: reset(cx); michael@0: { michael@0: JS::RootedValue rval(cx); michael@0: CHECK(JS_CallFunctionName(cx, global, "check2", JS::HandleValueArray::empty(), michael@0: &rval)); michael@0: CHECK(cx->runtime()->spsProfiler.stringsCount() == 5); michael@0: CHECK(max_stack >= 6); michael@0: CHECK(psize == 0); michael@0: } michael@0: js::EnableRuntimeProfilingStack(cx->runtime(), false); michael@0: js::SetRuntimeProfilingStack(cx->runtime(), pstack, &psize, 3); michael@0: reset(cx); michael@0: { michael@0: JS::RootedValue rval(cx); michael@0: pstack[3].setLabel((char*) 1234); michael@0: CHECK(JS_CallFunctionName(cx, global, "check", JS::HandleValueArray::empty(), michael@0: &rval)); michael@0: CHECK((size_t) pstack[3].label() == 1234); michael@0: CHECK(max_stack >= 8); michael@0: CHECK(psize == 0); michael@0: } michael@0: return true; michael@0: } michael@0: END_TEST(testProfileStrings_isCalledWithInterpreter) michael@0: michael@0: BEGIN_TEST(testProfileStrings_isCalledWithJIT) michael@0: { michael@0: CHECK(initialize(cx)); michael@0: JS::RuntimeOptionsRef(cx).setBaseline(true) michael@0: .setIon(true); michael@0: michael@0: EXEC("function g() { var p = new Prof(); p.test_fn(); }"); michael@0: EXEC("function f() { g(); }"); michael@0: EXEC("function e() { f(); }"); michael@0: EXEC("function d() { e(); }"); michael@0: EXEC("function c() { d(); }"); michael@0: EXEC("function b() { c(); }"); michael@0: EXEC("function a() { b(); }"); michael@0: EXEC("function check() { var p = new Prof(); p.test_fn(); a(); }"); michael@0: EXEC("function check2() { var p = new Prof(); p.test_fn2(); }"); michael@0: michael@0: reset(cx); michael@0: { michael@0: JS::RootedValue rval(cx); michael@0: /* Make sure the stack resets and we have an entry for each stack */ michael@0: CHECK(JS_CallFunctionName(cx, global, "check", JS::HandleValueArray::empty(), michael@0: &rval)); michael@0: CHECK(psize == 0); michael@0: CHECK(max_stack >= 8); michael@0: michael@0: /* Make sure the stack resets and we added no new entries */ michael@0: uint32_t cnt = cx->runtime()->spsProfiler.stringsCount(); michael@0: max_stack = 0; michael@0: CHECK(JS_CallFunctionName(cx, global, "check", JS::HandleValueArray::empty(), michael@0: &rval)); michael@0: CHECK(psize == 0); michael@0: CHECK(cx->runtime()->spsProfiler.stringsCount() == cnt); michael@0: CHECK(max_stack >= 8); michael@0: } michael@0: michael@0: js::EnableRuntimeProfilingStack(cx->runtime(), false); michael@0: js::SetRuntimeProfilingStack(cx->runtime(), pstack, &psize, 3); michael@0: reset(cx); michael@0: { michael@0: /* Limit the size of the stack and make sure we don't overflow */ michael@0: JS::RootedValue rval(cx); michael@0: pstack[3].setLabel((char*) 1234); michael@0: CHECK(JS_CallFunctionName(cx, global, "check", JS::HandleValueArray::empty(), michael@0: &rval)); michael@0: CHECK(psize == 0); michael@0: CHECK(max_stack >= 8); michael@0: CHECK((size_t) pstack[3].label() == 1234); michael@0: } michael@0: return true; michael@0: } michael@0: END_TEST(testProfileStrings_isCalledWithJIT) michael@0: michael@0: BEGIN_TEST(testProfileStrings_isCalledWhenError) michael@0: { michael@0: CHECK(initialize(cx)); michael@0: JS::RuntimeOptionsRef(cx).setBaseline(true) michael@0: .setIon(true); michael@0: michael@0: EXEC("function check2() { throw 'a'; }"); michael@0: michael@0: reset(cx); michael@0: JS::ContextOptionsRef(cx).setDontReportUncaught(true); michael@0: { michael@0: JS::RootedValue rval(cx); michael@0: /* Make sure the stack resets and we have an entry for each stack */ michael@0: bool ok = JS_CallFunctionName(cx, global, "check2", JS::HandleValueArray::empty(), michael@0: &rval); michael@0: CHECK(!ok); michael@0: CHECK(psize == 0); michael@0: CHECK(cx->runtime()->spsProfiler.stringsCount() == 1); michael@0: michael@0: JS_ClearPendingException(cx); michael@0: } michael@0: return true; michael@0: } michael@0: END_TEST(testProfileStrings_isCalledWhenError) michael@0: michael@0: BEGIN_TEST(testProfileStrings_worksWhenEnabledOnTheFly) michael@0: { michael@0: CHECK(initialize(cx)); michael@0: JS::RuntimeOptionsRef(cx).setBaseline(true) michael@0: .setIon(true); michael@0: michael@0: EXEC("function b(p) { p.test_fn(); }"); michael@0: EXEC("function a() { var p = new Prof(); p.enable(); b(p); }"); michael@0: reset(cx); michael@0: js::EnableRuntimeProfilingStack(cx->runtime(), false); michael@0: { michael@0: /* enable it in the middle of JS and make sure things check out */ michael@0: JS::RootedValue rval(cx); michael@0: JS_CallFunctionName(cx, global, "a", JS::HandleValueArray::empty(), &rval); michael@0: CHECK(psize == 0); michael@0: CHECK(max_stack >= 1); michael@0: CHECK(cx->runtime()->spsProfiler.stringsCount() == 1); michael@0: } michael@0: michael@0: EXEC("function d(p) { p.disable(); }"); michael@0: EXEC("function c() { var p = new Prof(); d(p); }"); michael@0: reset(cx); michael@0: { michael@0: /* now disable in the middle of js */ michael@0: JS::RootedValue rval(cx); michael@0: JS_CallFunctionName(cx, global, "c", JS::HandleValueArray::empty(), &rval); michael@0: CHECK(psize == 0); michael@0: } michael@0: michael@0: EXEC("function e() { var p = new Prof(); d(p); p.enable(); b(p); }"); michael@0: reset(cx); michael@0: { michael@0: /* now disable in the middle of js, but re-enable before final exit */ michael@0: JS::RootedValue rval(cx); michael@0: JS_CallFunctionName(cx, global, "e", JS::HandleValueArray::empty(), &rval); michael@0: CHECK(psize == 0); michael@0: CHECK(max_stack >= 3); michael@0: } michael@0: michael@0: EXEC("function h() { }"); michael@0: EXEC("function g(p) { p.disable(); for (var i = 0; i < 100; i++) i++; }"); michael@0: EXEC("function f() { g(new Prof()); }"); michael@0: reset(cx); michael@0: cx->runtime()->spsProfiler.enableSlowAssertions(false); michael@0: { michael@0: JS::RootedValue rval(cx); michael@0: /* disable, and make sure that if we try to re-enter the JIT the pop michael@0: * will still happen */ michael@0: JS_CallFunctionName(cx, global, "f", JS::HandleValueArray::empty(), &rval); michael@0: CHECK(psize == 0); michael@0: } michael@0: return true; michael@0: } michael@0: END_TEST(testProfileStrings_worksWhenEnabledOnTheFly)