1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-17 18:55:07 +00:00

LibJS: Put zombie cell tracking code behind a compile-time flag

Since this is a debug-only feature, let's not have it impact GC marking
performance when you don't need it.
This commit is contained in:
Andreas Kling 2021-10-02 16:35:55 +02:00
parent f290c59dd8
commit 6a1b82df2b
13 changed files with 76 additions and 8 deletions

View file

@ -24,12 +24,18 @@ public:
bool is_marked() const { return m_mark; } bool is_marked() const { return m_mark; }
void set_marked(bool b) { m_mark = b; } void set_marked(bool b) { m_mark = b; }
virtual void did_become_zombie() { } #ifdef JS_TRACK_ZOMBIE_CELLS
virtual void did_become_zombie()
{
}
#endif
enum class State { enum class State {
Live, Live,
Dead, Dead,
#ifdef JS_TRACK_ZOMBIE_CELLS
Zombie, Zombie,
#endif
}; };
State state() const { return m_state; } State state() const { return m_state; }

View file

@ -184,11 +184,13 @@ public:
return; return;
dbgln_if(HEAP_DEBUG, " ! {}", &cell); dbgln_if(HEAP_DEBUG, " ! {}", &cell);
#ifdef JS_TRACK_ZOMBIE_CELLS
if (cell.state() == Cell::State::Zombie) { if (cell.state() == Cell::State::Zombie) {
dbgln("BUG! Marking a zombie cell, {} @ {:p}", cell.class_name(), &cell); dbgln("BUG! Marking a zombie cell, {} @ {:p}", cell.class_name(), &cell);
cell.vm().dump_backtrace(); cell.vm().dump_backtrace();
VERIFY_NOT_REACHED(); VERIFY_NOT_REACHED();
} }
#endif
cell.set_marked(true); cell.set_marked(true);
cell.visit_edges(*this); cell.visit_edges(*this);
@ -230,12 +232,16 @@ void Heap::sweep_dead_cells(bool print_report, const Core::ElapsedTimer& measure
dbgln_if(HEAP_DEBUG, " ~ {}", cell); dbgln_if(HEAP_DEBUG, " ~ {}", cell);
if (should_store_swept_cells) if (should_store_swept_cells)
swept_cells.append(cell); swept_cells.append(cell);
#ifdef JS_TRACK_ZOMBIE_CELLS
if (m_zombify_dead_cells) { if (m_zombify_dead_cells) {
cell->set_state(Cell::State::Zombie); cell->set_state(Cell::State::Zombie);
cell->did_become_zombie(); cell->did_become_zombie();
} else { } else {
#endif
block.deallocate(cell); block.deallocate(cell);
#ifdef JS_TRACK_ZOMBIE_CELLS
} }
#endif
++collected_cells; ++collected_cells;
collected_cell_bytes += block.cell_size(); collected_cell_bytes += block.cell_size();
} else { } else {

View file

@ -62,7 +62,12 @@ public:
bool should_collect_on_every_allocation() const { return m_should_collect_on_every_allocation; } bool should_collect_on_every_allocation() const { return m_should_collect_on_every_allocation; }
void set_should_collect_on_every_allocation(bool b) { m_should_collect_on_every_allocation = b; } void set_should_collect_on_every_allocation(bool b) { m_should_collect_on_every_allocation = b; }
void set_zombify_dead_cells(bool b) { m_zombify_dead_cells = b; } #ifdef JS_TRACK_ZOMBIE_CELLS
void set_zombify_dead_cells(bool b)
{
m_zombify_dead_cells = b;
}
#endif
void did_create_handle(Badge<HandleImpl>, HandleImpl&); void did_create_handle(Badge<HandleImpl>, HandleImpl&);
void did_destroy_handle(Badge<HandleImpl>, HandleImpl&); void did_destroy_handle(Badge<HandleImpl>, HandleImpl&);
@ -122,7 +127,10 @@ private:
bool m_should_gc_when_deferral_ends { false }; bool m_should_gc_when_deferral_ends { false };
bool m_collecting_garbage { false }; bool m_collecting_garbage { false };
#ifdef JS_TRACK_ZOMBIE_CELLS
bool m_zombify_dead_cells { false }; bool m_zombify_dead_cells { false };
#endif
}; };
} }

View file

@ -34,7 +34,13 @@ public:
private: private:
virtual void visit_edges(Visitor& visitor) override; virtual void visit_edges(Visitor& visitor) override;
virtual void did_become_zombie() override { deregister(); }
#ifdef JS_TRACK_ZOMBIE_CELLS
virtual void did_become_zombie() override
{
deregister();
}
#endif
FunctionObject* m_cleanup_callback { nullptr }; FunctionObject* m_cleanup_callback { nullptr };

View file

@ -238,9 +238,11 @@ FLATTEN void Shape::add_property_without_transition(PropertyName const& property
add_property_without_transition(property_name.to_string_or_symbol(), attributes); add_property_without_transition(property_name.to_string_or_symbol(), attributes);
} }
#ifdef JS_TRACK_ZOMBIE_CELLS
void Shape::did_become_zombie() void Shape::did_become_zombie()
{ {
revoke_weak_ptrs(); revoke_weak_ptrs();
} }
#endif
} }

View file

@ -88,7 +88,10 @@ public:
private: private:
virtual const char* class_name() const override { return "Shape"; } virtual const char* class_name() const override { return "Shape"; }
virtual void visit_edges(Visitor&) override; virtual void visit_edges(Visitor&) override;
#ifdef JS_TRACK_ZOMBIE_CELLS
virtual void did_become_zombie() override; virtual void did_become_zombie() override;
#endif
Shape* get_or_prune_cached_forward_transition(TransitionKey const&); Shape* get_or_prune_cached_forward_transition(TransitionKey const&);
Shape* get_or_prune_cached_prototype_transition(Object* prototype); Shape* get_or_prune_cached_prototype_transition(Object* prototype);

View file

@ -30,7 +30,13 @@ public:
virtual void remove_swept_cells(Badge<Heap>, Span<Cell*>) override; virtual void remove_swept_cells(Badge<Heap>, Span<Cell*>) override;
private: private:
virtual void did_become_zombie() override { deregister(); } #ifdef JS_TRACK_ZOMBIE_CELLS
virtual void did_become_zombie() override
{
deregister();
}
#endif
void visit_edges(Visitor&) override; void visit_edges(Visitor&) override;
HashMap<Cell*, Value> m_values; // This stores Cell pointers instead of Object pointers to aide with sweeping HashMap<Cell*, Value> m_values; // This stores Cell pointers instead of Object pointers to aide with sweeping

View file

@ -31,7 +31,13 @@ public:
private: private:
virtual void visit_edges(Visitor&) override; virtual void visit_edges(Visitor&) override;
virtual void did_become_zombie() override { deregister(); }
#ifdef JS_TRACK_ZOMBIE_CELLS
virtual void did_become_zombie() override
{
deregister();
}
#endif
Object* m_value { nullptr }; Object* m_value { nullptr };
u32 m_last_execution_generation { 0 }; u32 m_last_execution_generation { 0 };

View file

@ -30,7 +30,12 @@ public:
virtual void remove_swept_cells(Badge<Heap>, Span<Cell*>) override; virtual void remove_swept_cells(Badge<Heap>, Span<Cell*>) override;
private: private:
virtual void did_become_zombie() override { deregister(); } #ifdef JS_TRACK_ZOMBIE_CELLS
virtual void did_become_zombie() override
{
deregister();
}
#endif
HashTable<Cell*> m_values; // This stores Cell pointers instead of Object pointers to aide with sweeping HashTable<Cell*> m_values; // This stores Cell pointers instead of Object pointers to aide with sweeping
}; };

View file

@ -113,7 +113,9 @@ static consteval size_t __testjs_last() { return (AK::Detail::IntegralConstant<s
static constexpr auto TOP_LEVEL_TEST_NAME = "__$$TOP_LEVEL$$__"; static constexpr auto TOP_LEVEL_TEST_NAME = "__$$TOP_LEVEL$$__";
extern RefPtr<JS::VM> g_vm; extern RefPtr<JS::VM> g_vm;
extern bool g_collect_on_every_allocation; extern bool g_collect_on_every_allocation;
#ifdef JS_TRACK_ZOMBIE_CELLS
extern bool g_zombify_dead_cells; extern bool g_zombify_dead_cells;
#endif
extern bool g_run_bytecode; extern bool g_run_bytecode;
extern bool g_dump_bytecode; extern bool g_dump_bytecode;
extern String g_currently_running_test; extern String g_currently_running_test;
@ -284,7 +286,10 @@ inline JSFileResult TestRunner::run_file_test(const String& test_path)
JS::VM::InterpreterExecutionScope scope(*interpreter); JS::VM::InterpreterExecutionScope scope(*interpreter);
interpreter->heap().set_should_collect_on_every_allocation(g_collect_on_every_allocation); interpreter->heap().set_should_collect_on_every_allocation(g_collect_on_every_allocation);
#ifdef JS_TRACK_ZOMBIE_CELLS
interpreter->heap().set_zombify_dead_cells(g_zombify_dead_cells); interpreter->heap().set_zombify_dead_cells(g_zombify_dead_cells);
#endif
if (g_run_file) { if (g_run_file) {
auto result = g_run_file(test_path, *interpreter); auto result = g_run_file(test_path, *interpreter);

View file

@ -18,7 +18,9 @@ namespace JS {
RefPtr<::JS::VM> g_vm; RefPtr<::JS::VM> g_vm;
bool g_collect_on_every_allocation = false; bool g_collect_on_every_allocation = false;
#ifdef JS_TRACK_ZOMBIE_CELLS
bool g_zombify_dead_cells = false; bool g_zombify_dead_cells = false;
#endif
bool g_run_bytecode = false; bool g_run_bytecode = false;
bool g_dump_bytecode = false; bool g_dump_bytecode = false;
String g_currently_running_test; String g_currently_running_test;
@ -110,7 +112,9 @@ int main(int argc, char** argv)
}); });
args_parser.add_option(print_json, "Show results as JSON", "json", 'j'); args_parser.add_option(print_json, "Show results as JSON", "json", 'j');
args_parser.add_option(g_collect_on_every_allocation, "Collect garbage after every allocation", "collect-often", 'g'); args_parser.add_option(g_collect_on_every_allocation, "Collect garbage after every allocation", "collect-often", 'g');
#ifdef JS_TRACK_ZOMBIE_CELLS
args_parser.add_option(g_zombify_dead_cells, "Zombify dead cells (to catch missing GC marks)", "zombify-dead-cells", 'z'); args_parser.add_option(g_zombify_dead_cells, "Zombify dead cells (to catch missing GC marks)", "zombify-dead-cells", 'z');
#endif
args_parser.add_option(g_run_bytecode, "Use the bytecode interpreter", "run-bytecode", 'b'); args_parser.add_option(g_run_bytecode, "Use the bytecode interpreter", "run-bytecode", 'b');
args_parser.add_option(g_dump_bytecode, "Dump the bytecode", "dump-bytecode", 'd'); args_parser.add_option(g_dump_bytecode, "Dump the bytecode", "dump-bytecode", 'd');
args_parser.add_option(test_glob, "Only run tests matching the given glob", "filter", 'f', "glob"); args_parser.add_option(test_glob, "Only run tests matching the given glob", "filter", 'f', "glob");

View file

@ -25,7 +25,12 @@ protected:
{ {
} }
virtual void did_become_zombie() override { revoke_weak_ptrs(); } #ifdef JS_TRACK_ZOMBIE_CELLS
virtual void did_become_zombie() override
{
revoke_weak_ptrs();
}
#endif
}; };
} }

View file

@ -1113,7 +1113,6 @@ public:
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
bool gc_on_every_allocation = false; bool gc_on_every_allocation = false;
bool zombify_dead_cells = false;
bool disable_syntax_highlight = false; bool disable_syntax_highlight = false;
Vector<String> script_paths; Vector<String> script_paths;
@ -1126,7 +1125,10 @@ int main(int argc, char** argv)
args_parser.add_option(s_as_module, "Treat as module", "as-module", 'm'); args_parser.add_option(s_as_module, "Treat as module", "as-module", 'm');
args_parser.add_option(s_print_last_result, "Print last result", "print-last-result", 'l'); args_parser.add_option(s_print_last_result, "Print last result", "print-last-result", 'l');
args_parser.add_option(gc_on_every_allocation, "GC on every allocation", "gc-on-every-allocation", 'g'); args_parser.add_option(gc_on_every_allocation, "GC on every allocation", "gc-on-every-allocation", 'g');
#ifdef JS_TRACK_ZOMBIE_CELLS
bool zombify_dead_cells = false;
args_parser.add_option(zombify_dead_cells, "Zombify dead cells (to catch missing GC marks)", "zombify-dead-cells", 'z'); args_parser.add_option(zombify_dead_cells, "Zombify dead cells (to catch missing GC marks)", "zombify-dead-cells", 'z');
#endif
args_parser.add_option(disable_syntax_highlight, "Disable live syntax highlighting", "no-syntax-highlight", 's'); args_parser.add_option(disable_syntax_highlight, "Disable live syntax highlighting", "no-syntax-highlight", 's');
args_parser.add_positional_argument(script_paths, "Path to script files", "scripts", Core::ArgsParser::Required::No); args_parser.add_positional_argument(script_paths, "Path to script files", "scripts", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv); args_parser.parse(argc, argv);
@ -1167,7 +1169,9 @@ int main(int argc, char** argv)
ReplConsoleClient console_client(interpreter->global_object().console()); ReplConsoleClient console_client(interpreter->global_object().console());
interpreter->global_object().console().set_client(console_client); interpreter->global_object().console().set_client(console_client);
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation); interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
#ifdef JS_TRACK_ZOMBIE_CELLS
interpreter->heap().set_zombify_dead_cells(zombify_dead_cells); interpreter->heap().set_zombify_dead_cells(zombify_dead_cells);
#endif
interpreter->vm().set_underscore_is_last_value(true); interpreter->vm().set_underscore_is_last_value(true);
s_editor = Line::Editor::construct(); s_editor = Line::Editor::construct();
@ -1378,7 +1382,9 @@ int main(int argc, char** argv)
ReplConsoleClient console_client(interpreter->global_object().console()); ReplConsoleClient console_client(interpreter->global_object().console());
interpreter->global_object().console().set_client(console_client); interpreter->global_object().console().set_client(console_client);
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation); interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
#ifdef JS_TRACK_ZOMBIE_CELLS
interpreter->heap().set_zombify_dead_cells(zombify_dead_cells); interpreter->heap().set_zombify_dead_cells(zombify_dead_cells);
#endif
signal(SIGINT, [](int) { signal(SIGINT, [](int) {
sigint_handler(); sigint_handler();