Commit fa5b221e authored by mtrofin's avatar mtrofin Committed by Commit bot

[wasm] tracing for wasm module instances

Debug-time tracing for module instances, to aid diagnosing
potential bugs.

BUG=

Review-Url: https://codereview.chromium.org/2390393002
Cr-Commit-Position: refs/heads/master@{#39965}
parent c938f0df
...@@ -1035,6 +1035,10 @@ DEFINE_BOOL(trace_regexp_parser, false, "trace regexp parsing") ...@@ -1035,6 +1035,10 @@ DEFINE_BOOL(trace_regexp_parser, false, "trace regexp parsing")
// Debugger // Debugger
DEFINE_BOOL(print_break_location, false, "print source location on debug break") DEFINE_BOOL(print_break_location, false, "print source location on debug break")
// wasm instance management
DEFINE_BOOL(trace_wasm_instances, false,
"trace creation and collection of wasm instances")
// //
// Logging and profiling flags // Logging and profiling flags
// //
......
...@@ -28,6 +28,16 @@ namespace v8 { ...@@ -28,6 +28,16 @@ namespace v8 {
namespace internal { namespace internal {
namespace wasm { namespace wasm {
#define TRACE(...) \
do { \
if (FLAG_trace_wasm_instances) PrintF(__VA_ARGS__); \
} while (false)
#define TRACE_CHAIN(instance) \
do { \
instance->PrintInstancesChain(); \
} while (false)
namespace { namespace {
static const int kPlaceholderMarker = 1000000000; static const int kPlaceholderMarker = 1000000000;
...@@ -728,6 +738,7 @@ void PatchDirectCalls(Handle<FixedArray> old_functions, ...@@ -728,6 +738,7 @@ void PatchDirectCalls(Handle<FixedArray> old_functions,
static void ResetCompiledModule(Isolate* isolate, JSObject* owner, static void ResetCompiledModule(Isolate* isolate, JSObject* owner,
WasmCompiledModule* compiled_module) { WasmCompiledModule* compiled_module) {
TRACE("Resetting %d\n", compiled_module->instance_id());
Object* undefined = *isolate->factory()->undefined_value(); Object* undefined = *isolate->factory()->undefined_value();
uint32_t old_mem_size = compiled_module->has_heap() uint32_t old_mem_size = compiled_module->has_heap()
? compiled_module->mem_size() ? compiled_module->mem_size()
...@@ -781,6 +792,7 @@ static void InstanceFinalizer(const v8::WeakCallbackInfo<void>& data) { ...@@ -781,6 +792,7 @@ static void InstanceFinalizer(const v8::WeakCallbackInfo<void>& data) {
JSObject* owner = *p; JSObject* owner = *p;
WasmCompiledModule* compiled_module = WasmCompiledModule* compiled_module =
WasmCompiledModule::cast(owner->GetInternalField(kWasmCompiledModule)); WasmCompiledModule::cast(owner->GetInternalField(kWasmCompiledModule));
TRACE("Finalizing %d {\n", compiled_module->instance_id());
Isolate* isolate = reinterpret_cast<Isolate*>(data.GetIsolate()); Isolate* isolate = reinterpret_cast<Isolate*>(data.GetIsolate());
DCHECK(compiled_module->has_weak_module_object()); DCHECK(compiled_module->has_weak_module_object());
WeakCell* weak_module_obj = compiled_module->ptr_to_weak_module_object(); WeakCell* weak_module_obj = compiled_module->ptr_to_weak_module_object();
...@@ -792,6 +804,11 @@ static void InstanceFinalizer(const v8::WeakCallbackInfo<void>& data) { ...@@ -792,6 +804,11 @@ static void InstanceFinalizer(const v8::WeakCallbackInfo<void>& data) {
JSObject* module_obj = JSObject::cast(weak_module_obj->value()); JSObject* module_obj = JSObject::cast(weak_module_obj->value());
WasmCompiledModule* current_template = WasmCompiledModule* current_template =
WasmCompiledModule::cast(module_obj->GetInternalField(0)); WasmCompiledModule::cast(module_obj->GetInternalField(0));
TRACE("chain before {\n");
TRACE_CHAIN(current_template);
TRACE("}\n");
DCHECK(!current_template->has_weak_prev_instance()); DCHECK(!current_template->has_weak_prev_instance());
WeakCell* next = compiled_module->ptr_to_weak_next_instance(); WeakCell* next = compiled_module->ptr_to_weak_next_instance();
WeakCell* prev = compiled_module->ptr_to_weak_prev_instance(); WeakCell* prev = compiled_module->ptr_to_weak_prev_instance();
...@@ -829,9 +846,13 @@ static void InstanceFinalizer(const v8::WeakCallbackInfo<void>& data) { ...@@ -829,9 +846,13 @@ static void InstanceFinalizer(const v8::WeakCallbackInfo<void>& data) {
} }
} }
} }
TRACE("chain after {\n");
TRACE_CHAIN(WasmCompiledModule::cast(module_obj->GetInternalField(0)));
TRACE("}\n");
} }
compiled_module->reset_weak_owning_instance(); compiled_module->reset_weak_owning_instance();
GlobalHandles::Destroy(reinterpret_cast<Object**>(p)); GlobalHandles::Destroy(reinterpret_cast<Object**>(p));
TRACE("}\n");
} }
Handle<FixedArray> SetupIndirectFunctionTable( Handle<FixedArray> SetupIndirectFunctionTable(
...@@ -1186,6 +1207,7 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, ...@@ -1186,6 +1207,7 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate,
// this will be a cleared. We'll link the instances chain last. // this will be a cleared. We'll link the instances chain last.
MaybeHandle<WeakCell> link_to_original; MaybeHandle<WeakCell> link_to_original;
TRACE("Starting new module instantiation\n");
{ {
Handle<WasmCompiledModule> original( Handle<WasmCompiledModule> original(
WasmCompiledModule::cast(module_object->GetInternalField(0)), isolate); WasmCompiledModule::cast(module_object->GetInternalField(0)), isolate);
...@@ -1200,6 +1222,7 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, ...@@ -1200,6 +1222,7 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate,
// There is already an owner, clone everything. // There is already an owner, clone everything.
owner = Handle<JSObject>(JSObject::cast(tmp->value()), isolate); owner = Handle<JSObject>(JSObject::cast(tmp->value()), isolate);
// Insert the latest clone in front. // Insert the latest clone in front.
TRACE("Cloning from %d\n", original->instance_id());
compiled_module = WasmCompiledModule::Clone(isolate, original); compiled_module = WasmCompiledModule::Clone(isolate, original);
// Replace the strong reference to point to the new instance here. // Replace the strong reference to point to the new instance here.
// This allows any of the other instances, including the original, // This allows any of the other instances, including the original,
...@@ -1233,6 +1256,7 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, ...@@ -1233,6 +1256,7 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate,
} else { } else {
// There was no owner, so we can reuse the original. // There was no owner, so we can reuse the original.
compiled_module = original; compiled_module = original;
TRACE("Reusing existing instance %d\n", compiled_module->instance_id());
} }
compiled_module->set_code_table(code_table); compiled_module->set_code_table(code_table);
} }
...@@ -1490,8 +1514,7 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, ...@@ -1490,8 +1514,7 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate,
DCHECK(wasm::IsWasmObject(*instance)); DCHECK(wasm::IsWasmObject(*instance));
if (compiled_module->has_weak_module_object()) { {
instance->SetInternalField(kWasmCompiledModule, *compiled_module);
Handle<WeakCell> link_to_owner = factory->NewWeakCell(instance); Handle<WeakCell> link_to_owner = factory->NewWeakCell(instance);
Handle<Object> global_handle = isolate->global_handles()->Create(*instance); Handle<Object> global_handle = isolate->global_handles()->Create(*instance);
...@@ -1507,15 +1530,23 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, ...@@ -1507,15 +1530,23 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate,
compiled_module->set_weak_next_instance(next); compiled_module->set_weak_next_instance(next);
original->set_weak_prev_instance(link_to_clone); original->set_weak_prev_instance(link_to_clone);
} }
compiled_module->set_weak_owning_instance(link_to_owner);
instance->SetInternalField(kWasmCompiledModule, *compiled_module);
GlobalHandles::MakeWeak(global_handle.location(), GlobalHandles::MakeWeak(global_handle.location(),
global_handle.location(), &InstanceFinalizer, global_handle.location(), &InstanceFinalizer,
v8::WeakCallbackType::kFinalizer); v8::WeakCallbackType::kFinalizer);
} }
} }
TRACE("Finishing instance %d\n", compiled_module->instance_id());
TRACE_CHAIN(WasmCompiledModule::cast(module_object->GetInternalField(0)));
return instance; return instance;
} }
#if DEBUG
uint32_t WasmCompiledModule::instance_id_counter_ = 0;
#endif
Handle<WasmCompiledModule> WasmCompiledModule::New(Isolate* isolate, Handle<WasmCompiledModule> WasmCompiledModule::New(Isolate* isolate,
uint32_t min_memory_pages, uint32_t min_memory_pages,
uint32_t globals_size, uint32_t globals_size,
...@@ -1533,9 +1564,31 @@ Handle<WasmCompiledModule> WasmCompiledModule::New(Isolate* isolate, ...@@ -1533,9 +1564,31 @@ Handle<WasmCompiledModule> WasmCompiledModule::New(Isolate* isolate,
ret->set(kID_globals_size, Smi::FromInt(static_cast<int>(globals_size))); ret->set(kID_globals_size, Smi::FromInt(static_cast<int>(globals_size)));
ret->set(kID_export_memory, Smi::FromInt(static_cast<int>(export_memory))); ret->set(kID_export_memory, Smi::FromInt(static_cast<int>(export_memory)));
ret->set(kID_origin, Smi::FromInt(static_cast<int>(origin))); ret->set(kID_origin, Smi::FromInt(static_cast<int>(origin)));
WasmCompiledModule::cast(*ret)->Init();
return handle(WasmCompiledModule::cast(*ret)); return handle(WasmCompiledModule::cast(*ret));
} }
void WasmCompiledModule::Init() {
#if DEBUG
set(kID_instance_id, Smi::FromInt(instance_id_counter_++));
TRACE("New compiled module id: %d\n", instance_id());
#endif
}
void WasmCompiledModule::PrintInstancesChain() {
#if DEBUG
if (!FLAG_trace_wasm_instances) return;
for (WasmCompiledModule* current = this; current != nullptr;) {
PrintF("->%d", current->instance_id());
if (current->ptr_to_weak_next_instance() == nullptr) break;
CHECK(!current->ptr_to_weak_next_instance()->cleared());
current =
WasmCompiledModule::cast(current->ptr_to_weak_next_instance()->value());
}
PrintF("\n");
#endif
}
Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm, Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm,
uint32_t func_index) { uint32_t func_index) {
if (!wasm->IsUndefined(isolate)) { if (!wasm->IsUndefined(isolate)) {
......
...@@ -382,7 +382,7 @@ class WasmCompiledModule : public FixedArray { ...@@ -382,7 +382,7 @@ class WasmCompiledModule : public FixedArray {
return handle(TYPE::cast(weak_##NAME()->value())); \ return handle(TYPE::cast(weak_##NAME()->value())); \
} }
#define WCM_PROPERTY_TABLE(MACRO) \ #define CORE_WCM_PROPERTY_TABLE(MACRO) \
MACRO(OBJECT, FixedArray, code_table) \ MACRO(OBJECT, FixedArray, code_table) \
MACRO(OBJECT, FixedArray, import_data) \ MACRO(OBJECT, FixedArray, import_data) \
MACRO(OBJECT, FixedArray, exports) \ MACRO(OBJECT, FixedArray, exports) \
...@@ -402,6 +402,17 @@ class WasmCompiledModule : public FixedArray { ...@@ -402,6 +402,17 @@ class WasmCompiledModule : public FixedArray {
MACRO(WEAK_LINK, JSObject, owning_instance) \ MACRO(WEAK_LINK, JSObject, owning_instance) \
MACRO(WEAK_LINK, JSObject, module_object) MACRO(WEAK_LINK, JSObject, module_object)
#if DEBUG
#define DEBUG_ONLY_TABLE(MACRO) MACRO(SMALL_NUMBER, uint32_t, instance_id)
#else
#define DEBUG_ONLY_TABLE(IGNORE)
uint32_t instance_id() const { return -1; }
#endif
#define WCM_PROPERTY_TABLE(MACRO) \
CORE_WCM_PROPERTY_TABLE(MACRO) \
DEBUG_ONLY_TABLE(MACRO)
private: private:
enum PropertyIndices { enum PropertyIndices {
#define INDICES(IGNORE1, IGNORE2, NAME) kID_##NAME, #define INDICES(IGNORE1, IGNORE2, NAME) kID_##NAME,
...@@ -420,6 +431,10 @@ class WasmCompiledModule : public FixedArray { ...@@ -420,6 +431,10 @@ class WasmCompiledModule : public FixedArray {
Handle<WasmCompiledModule> module) { Handle<WasmCompiledModule> module) {
Handle<WasmCompiledModule> ret = Handle<WasmCompiledModule>::cast( Handle<WasmCompiledModule> ret = Handle<WasmCompiledModule>::cast(
isolate->factory()->CopyFixedArray(module)); isolate->factory()->CopyFixedArray(module));
ret->Init();
ret->reset_weak_owning_instance();
ret->reset_weak_next_instance();
ret->reset_weak_prev_instance();
return ret; return ret;
} }
...@@ -436,7 +451,14 @@ class WasmCompiledModule : public FixedArray { ...@@ -436,7 +451,14 @@ class WasmCompiledModule : public FixedArray {
WCM_PROPERTY_TABLE(DECLARATION) WCM_PROPERTY_TABLE(DECLARATION)
#undef DECLARATION #undef DECLARATION
void PrintInstancesChain();
private: private:
#if DEBUG
static uint32_t instance_id_counter_;
#endif
void Init();
DISALLOW_IMPLICIT_CONSTRUCTORS(WasmCompiledModule); DISALLOW_IMPLICIT_CONSTRUCTORS(WasmCompiledModule);
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment