Skip to content

Commit 49d23a3

Browse files
ofrobotsMylesBorins
authored andcommitted
deps: V8: backport 14ac02c from upstream
Original commit message: [cpu-profiler] Clear code entries when no observers are present. Performed manual testing as well by making 20 CPU profile recordings of loading http://meduza.io page. Without the patch the page renderer memory size grows beyond 300MB. With the patch it remains below 200MB. BUG=v8:6623 Change-Id: Ifce541b84bb2aaaa5175520f8dd49dbc0cb5dd20 Reviewed-on: https://chromium-review.googlesource.com/798020 Commit-Queue: Alexei Filippov <[email protected]> Reviewed-by: Yang Guo <[email protected]> Cr-Commit-Position: refs/heads/master@{#49914} Ref: v8/v8@14ac02c PR-URL: #17512 Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent f5a1e6c commit 49d23a3

File tree

4 files changed

+45
-16
lines changed

4 files changed

+45
-16
lines changed

common.gypi

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
# Reset this number to 0 on major V8 upgrades.
2929
# Increment by one for each non-official patch applied to deps/v8.
30-
'v8_embedder_string': '-node.11',
30+
'v8_embedder_string': '-node.12',
3131

3232
# Enable disassembler for `--print-code` v8 options
3333
'v8_enable_disassembler': 1,

deps/v8/src/profiler/profiler-listener.cc

+14-14
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ namespace internal {
1616
ProfilerListener::ProfilerListener(Isolate* isolate)
1717
: function_and_resource_names_(isolate->heap()) {}
1818

19-
ProfilerListener::~ProfilerListener() {
20-
for (auto code_entry : code_entries_) {
21-
delete code_entry;
22-
}
23-
}
19+
ProfilerListener::~ProfilerListener() = default;
2420

2521
void ProfilerListener::CallbackEvent(Name* name, Address entry_point) {
2622
CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
@@ -286,19 +282,23 @@ CodeEntry* ProfilerListener::NewCodeEntry(
286282
CodeEventListener::LogEventsAndTags tag, const char* name,
287283
const char* name_prefix, const char* resource_name, int line_number,
288284
int column_number, JITLineInfoTable* line_info, Address instruction_start) {
289-
CodeEntry* code_entry =
290-
new CodeEntry(tag, name, name_prefix, resource_name, line_number,
291-
column_number, line_info, instruction_start);
292-
code_entries_.push_back(code_entry);
293-
return code_entry;
285+
std::unique_ptr<CodeEntry> code_entry = base::make_unique<CodeEntry>(
286+
tag, name, name_prefix, resource_name, line_number, column_number,
287+
line_info, instruction_start);
288+
CodeEntry* raw_code_entry = code_entry.get();
289+
code_entries_.push_back(std::move(code_entry));
290+
return raw_code_entry;
294291
}
295292

296293
void ProfilerListener::AddObserver(CodeEventObserver* observer) {
297294
base::LockGuard<base::Mutex> guard(&mutex_);
298-
if (std::find(observers_.begin(), observers_.end(), observer) !=
299-
observers_.end())
300-
return;
301-
observers_.push_back(observer);
295+
if (observers_.empty()) {
296+
code_entries_.clear();
297+
}
298+
if (std::find(observers_.begin(), observers_.end(), observer) ==
299+
observers_.end()) {
300+
observers_.push_back(observer);
301+
}
302302
}
303303

304304
void ProfilerListener::RemoveObserver(CodeEventObserver* observer) {

deps/v8/src/profiler/profiler-listener.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class ProfilerListener : public CodeEventListener {
7474
const char* GetFunctionName(const char* name) {
7575
return function_and_resource_names_.GetFunctionName(name);
7676
}
77+
size_t entries_count_for_test() const { return code_entries_.size(); }
7778

7879
private:
7980
void RecordInliningInfo(CodeEntry* entry, AbstractCode* abstract_code);
@@ -87,7 +88,7 @@ class ProfilerListener : public CodeEventListener {
8788
}
8889

8990
StringsStorage function_and_resource_names_;
90-
std::vector<CodeEntry*> code_entries_;
91+
std::vector<std::unique_ptr<CodeEntry>> code_entries_;
9192
std::vector<CodeEventObserver*> observers_;
9293
base::Mutex mutex_;
9394

deps/v8/test/cctest/test-cpu-profiler.cc

+28
Original file line numberDiff line numberDiff line change
@@ -2191,3 +2191,31 @@ TEST(TracingCpuProfiler) {
21912191

21922192
i::V8::SetPlatformForTesting(old_platform);
21932193
}
2194+
2195+
TEST(CodeEntriesMemoryLeak) {
2196+
v8::HandleScope scope(CcTest::isolate());
2197+
v8::Local<v8::Context> env = CcTest::NewContext(PROFILER_EXTENSION);
2198+
v8::Context::Scope context_scope(env);
2199+
2200+
std::string source = "function start() {}\n";
2201+
for (int i = 0; i < 1000; ++i) {
2202+
source += "function foo" + std::to_string(i) + "() { return " +
2203+
std::to_string(i) +
2204+
"; }\n"
2205+
"foo" +
2206+
std::to_string(i) + "();\n";
2207+
}
2208+
CompileRun(source.c_str());
2209+
v8::Local<v8::Function> function = GetFunction(env, "start");
2210+
2211+
ProfilerHelper helper(env);
2212+
2213+
for (int j = 0; j < 100; ++j) {
2214+
v8::CpuProfile* profile = helper.Run(function, nullptr, 0);
2215+
profile->Delete();
2216+
}
2217+
ProfilerListener* profiler_listener =
2218+
CcTest::i_isolate()->logger()->profiler_listener();
2219+
2220+
CHECK_GE(10000ul, profiler_listener->entries_count_for_test());
2221+
}

0 commit comments

Comments
 (0)