Compare commits

...

2 Commits

Author SHA1 Message Date
Jarred Sumner
0a0d3ba850 Use profiled call 2023-07-04 03:47:56 -07:00
Jarred Sumner
1d8431a92c Create readfile-not-found.mjs 2023-07-04 03:47:31 -07:00
11 changed files with 159 additions and 43 deletions

View File

@@ -0,0 +1,17 @@
import { bench, run } from "./runner.mjs";
import { readFileSync, existsSync } from "node:fs";
import { readFile } from "node:fs/promises";
bench(`readFileSync(/tmp/404-not-found)`, () => {
try {
readFileSync("/tmp/404-not-found");
} catch (e) {}
});
bench(`readFile(/tmp/404-not-found)`, async () => {
try {
await readFile("/tmp/404-not-found");
} catch (e) {}
});
await run();

View File

@@ -1,4 +1,5 @@
#include "root.h"
#include "headers-handwritten.h"
#include "JavaScriptCore/JavaScript.h"
#include "wtf/FileSystem.h"
@@ -24,8 +25,11 @@
#include "JavaScriptCore/DeferTermination.h"
#include "JavaScriptCore/SamplingProfiler.h"
#include "JavaScriptCore/VMTrapsInlines.h"
#include "JavaScriptCore/JSPromise.h"
#include "JavaScriptCore/InspectorProtocolObjects.h"
#if ENABLE(REMOTE_INSPECTOR)
#include "JavaScriptCore/InspectorScriptProfilerAgent.h"
#include "JavaScriptCore/RemoteInspectorServer.h"
#endif
@@ -33,6 +37,70 @@
using namespace JSC;
using namespace WTF;
using namespace Inspector;
static Ref<Protocol::ScriptProfiler::Samples> buildSamples(JSC::JSGlobalObject* glboalObject, VM& vm, Vector<SamplingProfiler::StackTrace>&& samplingProfilerStackTraces)
{
auto stackTraces = JSON::ArrayOf<Protocol::ScriptProfiler::StackTrace>::create();
for (SamplingProfiler::StackTrace& stackTrace : samplingProfilerStackTraces) {
auto frames = JSON::ArrayOf<Protocol::ScriptProfiler::StackFrame>::create();
for (SamplingProfiler::StackFrame& stackFrame : stackTrace.frames) {
if (!JSC::Options::showPrivateScriptsInStackTraces()) {
if (stackFrame.frameType == SamplingProfiler::FrameType::Executable) {
if (stackFrame.executable->implementationVisibility() != ImplementationVisibility::Public) {
// Skip internal frames.
continue;
}
}
}
ZigStackFrame remappedFrame[2];
remappedFrame[0].position.line = stackFrame.functionStartLine();
remappedFrame[0].position.column_start = stackFrame.functionStartColumn();
if (stackFrame.hasExpressionInfo()) {
remappedFrame[1].position.line = stackFrame.lineNumber();
remappedFrame[1].position.column_start = stackFrame.columnNumber();
}
if (!stackFrame.url().isEmpty()) {
remappedFrame[0].source_url = Bun::toString(stackFrame.url());
if (stackFrame.hasExpressionInfo())
remappedFrame[1].source_url = Bun::toString(stackFrame.url());
}
Bun__remapStackFramePositions(glboalObject, remappedFrame, 1 + stackFrame.hasExpressionInfo());
auto frameObject = Protocol::ScriptProfiler::StackFrame::create()
.setSourceID(String::number(stackFrame.sourceID()))
.setName(stackFrame.displayName(vm))
.setLine(remappedFrame[0].position.line)
.setColumn(remappedFrame[0].position.column_start)
.setUrl(stackFrame.url())
.release();
if (stackFrame.hasExpressionInfo()) {
Ref<Protocol::ScriptProfiler::ExpressionLocation> expressionLocation = Protocol::ScriptProfiler::ExpressionLocation::create()
.setLine(remappedFrame[1].position.line)
.setColumn(remappedFrame[1].position.column_start)
.release();
frameObject->setExpressionLocation(WTFMove(expressionLocation));
}
frames->addItem(WTFMove(frameObject));
}
Ref<Protocol::ScriptProfiler::StackTrace> inspectorStackTrace = Protocol::ScriptProfiler::StackTrace::create()
.setTimestamp(stackTrace.timestamp.seconds())
.setStackFrames(WTFMove(frames))
.release();
stackTraces->addItem(WTFMove(inspectorStackTrace));
}
return Protocol::ScriptProfiler::Samples::create()
.setStackTraces(WTFMove(stackTraces))
.release();
}
JSC_DECLARE_HOST_FUNCTION(functionStartRemoteDebugger);
JSC_DEFINE_HOST_FUNCTION(functionStartRemoteDebugger, (JSGlobalObject * globalObject, CallFrame* callFrame))
@@ -452,6 +520,37 @@ JSC_DEFINE_HOST_FUNCTION(functionSetTimeZone, (JSGlobalObject * globalObject, Ca
return JSValue::encode(jsString(vm, timeZoneString));
}
static EncodedJSValue createResultObject(JSGlobalObject* globalObject)
{
auto& vm = globalObject->vm();
JSC::SamplingProfiler& samplingProfiler = *vm.samplingProfiler();
StringPrintStream topFunctions;
samplingProfiler.reportTopFunctions(topFunctions);
StringPrintStream byteCodes;
samplingProfiler.reportTopBytecodes(byteCodes);
Locker locker { samplingProfiler.getLock() };
samplingProfiler.pause();
JSValue stackTraces = JSONParse(globalObject, buildSamples(globalObject, vm, samplingProfiler.releaseStackTraces())->toJSONString());
locker.unlockEarly();
samplingProfiler.shutdown();
samplingProfiler.clearData();
JSObject* result = constructEmptyObject(globalObject, globalObject->objectPrototype(), 3);
result->putDirect(vm, Identifier::fromString(vm, "functions"_s), jsString(vm, topFunctions.toString()));
result->putDirect(vm, Identifier::fromString(vm, "bytecodes"_s), jsString(vm, byteCodes.toString()));
result->putDirect(vm, Identifier::fromString(vm, "stackTraces"_s), stackTraces);
return JSValue::encode(result);
}
JSC_DEFINE_HOST_FUNCTION(functionFulfillProfilerRequest, (JSGlobalObject * globalObject, CallFrame* callFrame))
{
return createResultObject(globalObject);
}
JSC_DEFINE_HOST_FUNCTION(functionRunProfiler, (JSGlobalObject * globalObject, CallFrame* callFrame))
{
JSC::VM& vm = globalObject->vm();
@@ -477,31 +576,31 @@ JSC_DEFINE_HOST_FUNCTION(functionRunProfiler, (JSGlobalObject * globalObject, Ca
samplingProfiler.noticeCurrentThreadAsJSCExecutionThread();
samplingProfiler.start();
JSC::call(globalObject, function, callData, JSC::jsUndefined(), args);
samplingProfiler.pause();
JSValue result = JSC::profiledCall(globalObject, JSC::ProfilingReason::API, function, callData, JSC::jsUndefined(), args);
if (throwScope.exception()) {
samplingProfiler.shutdown();
samplingProfiler.clearData();
return JSValue::encode(JSValue {});
}
StringPrintStream topFunctions;
samplingProfiler.reportTopFunctions(topFunctions);
if (auto* promise = jsDynamicCast<JSC::JSPromise*>(result)) {
auto* fulfill = JSC::JSFunction::create(vm, globalObject, 0, "fulfill"_s, functionFulfillProfilerRequest, ImplementationVisibility::Public);
RETURN_IF_EXCEPTION(throwScope, {});
auto afterOngoingPromiseCapability = JSC::JSPromise::createNewPromiseCapability(globalObject, globalObject->promiseConstructor());
StringPrintStream byteCodes;
samplingProfiler.reportTopBytecodes(byteCodes);
auto data = JSC::JSPromise::convertCapabilityToDeferredData(globalObject, afterOngoingPromiseCapability);
RETURN_IF_EXCEPTION(throwScope, {});
JSValue stackTraces = JSONParse(globalObject, samplingProfiler.stackTracesAsJSON());
promise->performPromiseThen(globalObject, fulfill, fulfill, afterOngoingPromiseCapability);
samplingProfiler.shutdown();
samplingProfiler.clearData();
return JSValue::encode(data.promise);
}
JSObject* result = constructEmptyObject(globalObject, globalObject->objectPrototype(), 3);
result->putDirect(vm, Identifier::fromString(vm, "functions"_s), jsString(vm, topFunctions.toString()));
result->putDirect(vm, Identifier::fromString(vm, "bytecodes"_s), jsString(vm, byteCodes.toString()));
result->putDirect(vm, Identifier::fromString(vm, "stackTraces"_s), stackTraces);
samplingProfiler.pause();
return JSValue::encode(result);
return createResultObject(globalObject);
}
JSC_DECLARE_HOST_FUNCTION(functionGenerateHeapSnapshotForDebugging);

View File

@@ -393,7 +393,7 @@ extern "C" EncodedJSValue JSBundlerPlugin__runSetupFunction(
arguments.append(JSValue::decode(encodedConfig));
auto* lexicalGlobalObject = jsCast<JSFunction*>(JSValue::decode(encodedSetupFunction))->globalObject();
auto result = JSC::call(lexicalGlobalObject, setupFunction, callData, plugin, arguments);
auto result = JSC::profiledCall(lexicalGlobalObject, ProfilingReason::Other, setupFunction, callData, plugin, arguments);
if (UNLIKELY(scope.exception())) {
auto exception = scope.exception();
scope.clearException();

View File

@@ -1,6 +1,6 @@
// AUTO-GENERATED FILE. DO NOT EDIT.
// Generated by 'make generate-sink' at 2023-07-02T16:19:51.440Z
// Generated by 'make generate-sink' at 2023-07-04T09:59:44.299Z
// To regenerate this file, run:
//
// make generate-sink
@@ -802,7 +802,7 @@ void JSReadableArrayBufferSinkController::detach()
JSC::MarkedArgumentBuffer arguments;
arguments.append(readableStream);
arguments.append(jsUndefined());
JSC::call(globalObject, onClose, callData, JSC::jsUndefined(), arguments);
JSC::profiledCall(globalObject, JSC::ProfilingReason::Other, onClose, callData, JSC::jsUndefined(), arguments);
}
m_weakReadableStream.clear();
@@ -1056,7 +1056,7 @@ void JSReadableFileSinkController::detach()
JSC::MarkedArgumentBuffer arguments;
arguments.append(readableStream);
arguments.append(jsUndefined());
JSC::call(globalObject, onClose, callData, JSC::jsUndefined(), arguments);
JSC::profiledCall(globalObject, JSC::ProfilingReason::Other, onClose, callData, JSC::jsUndefined(), arguments);
}
m_weakReadableStream.clear();
@@ -1310,7 +1310,7 @@ void JSReadableHTTPResponseSinkController::detach()
JSC::MarkedArgumentBuffer arguments;
arguments.append(readableStream);
arguments.append(jsUndefined());
JSC::call(globalObject, onClose, callData, JSC::jsUndefined(), arguments);
JSC::profiledCall(globalObject, JSC::ProfilingReason::Other, onClose, callData, JSC::jsUndefined(), arguments);
}
m_weakReadableStream.clear();
@@ -1564,7 +1564,7 @@ void JSReadableHTTPSResponseSinkController::detach()
JSC::MarkedArgumentBuffer arguments;
arguments.append(readableStream);
arguments.append(jsUndefined());
JSC::call(globalObject, onClose, callData, JSC::jsUndefined(), arguments);
JSC::profiledCall(globalObject, JSC::ProfilingReason::Other, onClose, callData, JSC::jsUndefined(), arguments);
}
m_weakReadableStream.clear();
@@ -1835,7 +1835,7 @@ extern "C" void ArrayBufferSink__onReady(JSC__JSValue controllerValue, JSC__JSVa
arguments.append(JSC::JSValue::decode(amt));
arguments.append(JSC::JSValue::decode(offset));
JSC::call(globalObject, function, callData, JSC::jsUndefined(), arguments);
JSC::profiledCall(globalObject, JSC::ProfilingReason::Other, function, callData, JSC::jsUndefined(), arguments);
}
extern "C" void ArrayBufferSink__onStart(JSC__JSValue controllerValue)
@@ -1859,7 +1859,7 @@ extern "C" void ArrayBufferSink__onClose(JSC__JSValue controllerValue, JSC__JSVa
arguments.append(readableStream ? readableStream : JSC::jsUndefined());
arguments.append(JSC::JSValue::decode(reason));
JSC::call(globalObject, function, callData, JSC::jsUndefined(), arguments);
JSC::profiledCall(globalObject, JSC::ProfilingReason::Other, function, callData, JSC::jsUndefined(), arguments);
}
extern "C" JSC__JSValue FileSink__createObject(JSC__JSGlobalObject* arg0, void* sinkPtr)
@@ -1921,7 +1921,7 @@ extern "C" void FileSink__onReady(JSC__JSValue controllerValue, JSC__JSValue amt
arguments.append(JSC::JSValue::decode(amt));
arguments.append(JSC::JSValue::decode(offset));
JSC::call(globalObject, function, callData, JSC::jsUndefined(), arguments);
JSC::profiledCall(globalObject, JSC::ProfilingReason::Other, function, callData, JSC::jsUndefined(), arguments);
}
extern "C" void FileSink__onStart(JSC__JSValue controllerValue)
@@ -1945,7 +1945,7 @@ extern "C" void FileSink__onClose(JSC__JSValue controllerValue, JSC__JSValue rea
arguments.append(readableStream ? readableStream : JSC::jsUndefined());
arguments.append(JSC::JSValue::decode(reason));
JSC::call(globalObject, function, callData, JSC::jsUndefined(), arguments);
JSC::profiledCall(globalObject, JSC::ProfilingReason::Other, function, callData, JSC::jsUndefined(), arguments);
}
extern "C" JSC__JSValue HTTPResponseSink__createObject(JSC__JSGlobalObject* arg0, void* sinkPtr)
@@ -2007,7 +2007,7 @@ extern "C" void HTTPResponseSink__onReady(JSC__JSValue controllerValue, JSC__JSV
arguments.append(JSC::JSValue::decode(amt));
arguments.append(JSC::JSValue::decode(offset));
JSC::call(globalObject, function, callData, JSC::jsUndefined(), arguments);
JSC::profiledCall(globalObject, JSC::ProfilingReason::Other, function, callData, JSC::jsUndefined(), arguments);
}
extern "C" void HTTPResponseSink__onStart(JSC__JSValue controllerValue)
@@ -2031,7 +2031,7 @@ extern "C" void HTTPResponseSink__onClose(JSC__JSValue controllerValue, JSC__JSV
arguments.append(readableStream ? readableStream : JSC::jsUndefined());
arguments.append(JSC::JSValue::decode(reason));
JSC::call(globalObject, function, callData, JSC::jsUndefined(), arguments);
JSC::profiledCall(globalObject, JSC::ProfilingReason::Other, function, callData, JSC::jsUndefined(), arguments);
}
extern "C" JSC__JSValue HTTPSResponseSink__createObject(JSC__JSGlobalObject* arg0, void* sinkPtr)
@@ -2093,7 +2093,7 @@ extern "C" void HTTPSResponseSink__onReady(JSC__JSValue controllerValue, JSC__JS
arguments.append(JSC::JSValue::decode(amt));
arguments.append(JSC::JSValue::decode(offset));
JSC::call(globalObject, function, callData, JSC::jsUndefined(), arguments);
JSC::profiledCall(globalObject, JSC::ProfilingReason::Other, function, callData, JSC::jsUndefined(), arguments);
}
extern "C" void HTTPSResponseSink__onStart(JSC__JSValue controllerValue)
@@ -2117,5 +2117,5 @@ extern "C" void HTTPSResponseSink__onClose(JSC__JSValue controllerValue, JSC__JS
arguments.append(readableStream ? readableStream : JSC::jsUndefined());
arguments.append(JSC::JSValue::decode(reason));
JSC::call(globalObject, function, callData, JSC::jsUndefined(), arguments);
JSC::profiledCall(globalObject, JSC::ProfilingReason::Other, function, callData, JSC::jsUndefined(), arguments);
}

View File

@@ -1,6 +1,6 @@
// AUTO-GENERATED FILE. DO NOT EDIT.
// Generated by 'make generate-sink' at 2023-07-02T16:19:51.438Z
// Generated by 'make generate-sink' at 2023-07-04T09:59:44.298Z
//
#pragma once

View File

@@ -1,4 +1,4 @@
// Automatically generated from src/bun.js/bindings/JSSink.cpp using /home/cirospaciari/Repos/bun/src/bun.js/WebKit/Source/JavaScriptCore/create_hash_table. DO NOT EDIT!
// Automatically generated from src/bun.js/bindings/JSSink.cpp using /Users/jarred/Code/bun/src/bun.js/WebKit/Source/JavaScriptCore/create_hash_table. DO NOT EDIT!

View File

@@ -239,9 +239,9 @@ extern "C" void JSCInitialize(const char* envp[], size_t envc, void (*onCrash)(c
JSC::Options::useJITCage() = false;
JSC::Options::useShadowRealm() = true;
JSC::Options::useResizableArrayBuffer() = true;
#ifdef BUN_DEBUG
JSC::Options::showPrivateScriptsInStackTraces() = true;
#endif
// #ifdef BUN_DEBUG
// JSC::Options::showPrivateScriptsInStackTraces() = true;
// #endif
JSC::Options::useSetMethods() = true;
/*
@@ -2606,7 +2606,7 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionPerformMicrotask, (JSGlobalObject * globalObj
break;
}
JSC::call(globalObject, job, callData, jsUndefined(), arguments, exceptionPtr);
JSC::profiledCall(globalObject, ProfilingReason::Microtask, job, callData, jsUndefined(), arguments, exceptionPtr);
if (auto* exception = exceptionPtr.get()) {
Bun__reportUnhandledError(globalObject, JSValue::encode(exception));
@@ -2657,7 +2657,7 @@ JSC_DEFINE_HOST_FUNCTION(jsFunctionPerformMicrotaskVariadic, (JSGlobalObject * g
thisValue = callframe->argument(2);
}
JSC::call(globalObject, job, callData, thisValue, arguments, exceptionPtr);
JSC::profiledCall(globalObject, ProfilingReason::Microtask, job, callData, thisValue, arguments, exceptionPtr);
if (auto* exception = exceptionPtr.get()) {
Bun__reportUnhandledError(globalObject, JSValue::encode(exception));

View File

@@ -250,7 +250,7 @@ static void handlePromise(PromiseType* promise, JSC__JSGlobalObject* globalObjec
arguments.append(jsUndefined());
arguments.append(JSValue::decode(ctx));
ASSERT(!arguments.hasOverflowed());
JSC::call(globalThis, performPromiseThenFunction, callData, jsUndefined(), arguments);
JSC::profiledCall(globalThis, ProfilingReason::Other, performPromiseThenFunction, callData, jsUndefined(), arguments);
} else {
promise->then(globalThis, resolverFunction, rejecterFunction);
}
@@ -1763,7 +1763,7 @@ JSC__JSValue JSObjectCallAsFunctionReturnValue(JSContextRef ctx, JSObjectRef obj
return JSC::JSValue::encode(JSC::JSValue());
NakedPtr<JSC::Exception> returnedException = nullptr;
auto result = JSC::call(globalObject, jsObject, callData, jsThisObject, argList, returnedException);
auto result = JSC::profiledCall(globalObject, ProfilingReason::Other, jsObject, callData, jsThisObject, argList, returnedException);
if (returnedException.get()) {
return JSC::JSValue::encode(JSC::JSValue(returnedException.get()));
@@ -1805,7 +1805,7 @@ JSC__JSValue JSObjectCallAsFunctionReturnValueHoldingAPILock(JSContextRef ctx, J
return JSC::JSValue::encode(JSC::JSValue());
NakedPtr<JSC::Exception> returnedException = nullptr;
auto result = JSC::call(globalObject, jsObject, callData, jsThisObject, argList, returnedException);
auto result = JSC::profiledCall(globalObject, ProfilingReason::Other, jsObject, callData, jsThisObject, argList, returnedException);
if (returnedException.get()) {
return JSC::JSValue::encode(JSC::JSValue(returnedException.get()));

View File

@@ -1867,7 +1867,7 @@ extern "C" napi_status napi_call_function(napi_env env, napi_value recv_napi,
if (thisValue.isEmpty()) {
thisValue = JSC::jsUndefined();
}
JSC::JSValue result = JSC::call(globalObject, funcValue, callData, thisValue, args);
JSC::JSValue result = JSC::profiledCall(globalObject, ProfilingReason::Other, funcValue, callData, thisValue, args);
if (result_ptr) {
*result_ptr = toNapi(result);

View File

@@ -227,7 +227,7 @@ void EventEmitter::innerInvokeEventListeners(const Identifier& eventType, Simple
continue;
WTF::NakedPtr<JSC::Exception> exceptionPtr;
JSC::call(lexicalGlobalObject, jsFunction, callData, thisValue, arguments, exceptionPtr);
JSC::profiledCall(lexicalGlobalObject, ProfilingReason::Other, jsFunction, callData, thisValue, arguments, exceptionPtr);
auto* exception = exceptionPtr.get();
if (UNLIKELY(exception)) {

View File

@@ -640,7 +640,7 @@ void JS${controllerName}::detach() {
JSC::MarkedArgumentBuffer arguments;
arguments.append(readableStream);
arguments.append(jsUndefined());
JSC::call(globalObject, onClose, callData, JSC::jsUndefined(), arguments);
JSC::profiledCall(globalObject, JSC::ProfilingReason::Other, onClose, callData, JSC::jsUndefined(), arguments);
}
m_weakReadableStream.clear();
@@ -912,7 +912,7 @@ extern "C" void ${name}__onReady(JSC__JSValue controllerValue, JSC__JSValue amt,
arguments.append(JSC::JSValue::decode(amt));
arguments.append(JSC::JSValue::decode(offset));
JSC::call(globalObject, function, callData, JSC::jsUndefined(), arguments);
JSC::profiledCall(globalObject, JSC::ProfilingReason::Other, function, callData, JSC::jsUndefined(), arguments);
}
extern "C" void ${name}__onStart(JSC__JSValue controllerValue)
@@ -937,7 +937,7 @@ extern "C" void ${name}__onClose(JSC__JSValue controllerValue, JSC__JSValue reas
arguments.append(readableStream ? readableStream : JSC::jsUndefined());
arguments.append(JSC::JSValue::decode(reason));
JSC::call(globalObject, function, callData, JSC::jsUndefined(), arguments);
JSC::profiledCall(globalObject, JSC::ProfilingReason::Other, function, callData, JSC::jsUndefined(), arguments);
}
`;