Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(527)

Side by Side Diff: base/trace_event/heap_profiler_heap_dump_writer_unittest.cc

Issue 1911643002: Add configurable limit to allocations in heap profiler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed naming in protocol.json Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/trace_event/heap_profiler_heap_dump_writer.h" 5 #include "base/trace_event/heap_profiler_heap_dump_writer.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <set> 10 #include <set>
11 #include <string> 11 #include <string>
12 12
13 #include "base/json/json_reader.h" 13 #include "base/json/json_reader.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/trace_event/heap_profiler_allocation_context.h" 16 #include "base/trace_event/heap_profiler_allocation_context.h"
17 #include "base/trace_event/heap_profiler_stack_frame_deduplicator.h" 17 #include "base/trace_event/heap_profiler_stack_frame_deduplicator.h"
18 #include "base/trace_event/heap_profiler_type_name_deduplicator.h" 18 #include "base/trace_event/heap_profiler_type_name_deduplicator.h"
19 #include "base/trace_event/memory_dump_session_state.h"
19 #include "base/trace_event/trace_event_argument.h" 20 #include "base/trace_event/trace_event_argument.h"
20 #include "base/values.h" 21 #include "base/values.h"
21 #include "testing/gtest/include/gtest/gtest.h" 22 #include "testing/gtest/include/gtest/gtest.h"
22 23
23 namespace { 24 namespace {
24 25
25 // Define all strings once, because the deduplicator requires pointer equality, 26 // Define all strings once, because the deduplicator requires pointer equality,
26 // and string interning is unreliable. 27 // and string interning is unreliable.
27 const char kBrowserMain[] = "BrowserMain"; 28 const char kBrowserMain[] = "BrowserMain";
28 const char kRendererMain[] = "RendererMain"; 29 const char kRendererMain[] = "RendererMain";
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 // 207 //
207 // | | CrWidget <- BrMain | Init <- RenMain | Sum | 208 // | | CrWidget <- BrMain | Init <- RenMain | Sum |
208 // +--------+--------------------+-----------------+-------------+ 209 // +--------+--------------------+-----------------+-------------+
209 // | | size count | size count | size count | 210 // | | size count | size count | size count |
210 // | int | 10 5 | 0 0 | 10 5 | 211 // | int | 10 5 | 0 0 | 10 5 |
211 // | bool | 18 18 | 30 30 | 48 48 | 212 // | bool | 18 18 | 30 30 | 48 48 |
212 // | string | 0 0 | 19 4 | 19 4 | 213 // | string | 0 0 | 19 4 | 19 4 |
213 // +--------+--------------------+-----------------+-------------+ 214 // +--------+--------------------+-----------------+-------------+
214 // | Sum | 28 23 | 49 34 | 77 57 | 215 // | Sum | 28 23 | 49 34 | 77 57 |
215 216
216 auto sf_deduplicator = WrapUnique(new StackFrameDeduplicator); 217 auto stack_frame_deduplicator = WrapUnique(new StackFrameDeduplicator);
217 auto tn_deduplicator = WrapUnique(new TypeNameDeduplicator); 218 auto type_name_deduplicator = WrapUnique(new TypeNameDeduplicator);
218 HeapDumpWriter writer(sf_deduplicator.get(), tn_deduplicator.get()); 219 HeapDumpWriter writer(stack_frame_deduplicator.get(),
220 type_name_deduplicator.get(),
221 10u);
219 const std::set<Entry>& dump = writer.Summarize(metrics_by_context); 222 const std::set<Entry>& dump = writer.Summarize(metrics_by_context);
220 223
221 // Get the indices of the backtraces and types by adding them again to the 224 // Get the indices of the backtraces and types by adding them again to the
222 // deduplicator. Because they were added before, the same number is returned. 225 // deduplicator. Because they were added before, the same number is returned.
223 StackFrame bt0[] = {kRendererMain, kInitialize}; 226 StackFrame bt0[] = {kRendererMain, kInitialize};
224 StackFrame bt1[] = {kBrowserMain, kCreateWidget}; 227 StackFrame bt1[] = {kBrowserMain, kCreateWidget};
225 int bt_renderer_main = sf_deduplicator->Insert(bt0, bt0 + 1); 228 int bt_renderer_main = stack_frame_deduplicator->Insert(bt0, bt0 + 1);
226 int bt_browser_main = sf_deduplicator->Insert(bt1, bt1 + 1); 229 int bt_browser_main = stack_frame_deduplicator->Insert(bt1, bt1 + 1);
227 int bt_renderer_main_initialize = sf_deduplicator->Insert(bt0, bt0 + 2); 230 int bt_renderer_main_initialize =
228 int bt_browser_main_create_widget = sf_deduplicator->Insert(bt1, bt1 + 2); 231 stack_frame_deduplicator->Insert(bt0, bt0 + 2);
229 int type_id_int = tn_deduplicator->Insert(kInt); 232 int bt_browser_main_create_widget =
230 int type_id_bool = tn_deduplicator->Insert(kBool); 233 stack_frame_deduplicator->Insert(bt1, bt1 + 2);
231 int type_id_string = tn_deduplicator->Insert(kString); 234 int type_id_int = type_name_deduplicator->Insert(kInt);
235 int type_id_bool = type_name_deduplicator->Insert(kBool);
236 int type_id_string = type_name_deduplicator->Insert(kString);
232 237
233 // Full heap should have size 77. 238 // Full heap should have size 77.
234 AssertSizeAndCountEq(dump, -1, -1, {77, 57}); 239 AssertSizeAndCountEq(dump, -1, -1, {77, 57});
235 240
236 // 49 bytes in 34 chunks were allocated in RendererMain and children. Also 241 // 49 bytes in 34 chunks were allocated in RendererMain and children. Also
237 // check the type breakdown. 242 // check the type breakdown.
238 AssertSizeAndCountEq(dump, bt_renderer_main, -1, {49, 34}); 243 AssertSizeAndCountEq(dump, bt_renderer_main, -1, {49, 34});
239 AssertSizeAndCountEq(dump, bt_renderer_main, type_id_bool, {30, 30}); 244 AssertSizeAndCountEq(dump, bt_renderer_main, type_id_bool, {30, 30});
240 AssertSizeAndCountEq(dump, bt_renderer_main, type_id_string, {19, 4}); 245 AssertSizeAndCountEq(dump, bt_renderer_main, type_id_string, {19, 4});
241 246
(...skipping 29 matching lines...) Expand all
271 ctx.backtrace.frames[0] = kBrowserMain; 276 ctx.backtrace.frames[0] = kBrowserMain;
272 ctx.backtrace.frames[1] = kCreateWidget; 277 ctx.backtrace.frames[1] = kCreateWidget;
273 278
274 // 0.5 KiB and 1 chunk in BrowserMain -> CreateWidget itself. 279 // 0.5 KiB and 1 chunk in BrowserMain -> CreateWidget itself.
275 metrics_by_context[ctx] = {512, 1}; 280 metrics_by_context[ctx] = {512, 1};
276 281
277 // 1 MiB and 1 chunk in BrowserMain -> CreateWidget -> GetBitmap. 282 // 1 MiB and 1 chunk in BrowserMain -> CreateWidget -> GetBitmap.
278 ctx.backtrace.frames[2] = kGetBitmap; 283 ctx.backtrace.frames[2] = kGetBitmap;
279 metrics_by_context[ctx] = {1024 * 1024, 1}; 284 metrics_by_context[ctx] = {1024 * 1024, 1};
280 285
281 // 0.5 KiB and 1 chunk in BrowserMain -> CreateWidget -> Initialize. 286 // 400B and 1 chunk in BrowserMain -> CreateWidget -> Initialize.
282 ctx.backtrace.frames[2] = kInitialize; 287 ctx.backtrace.frames[2] = kInitialize;
283 metrics_by_context[ctx] = {512, 1}; 288 metrics_by_context[ctx] = {400, 1};
284 289
285 auto sf_deduplicator = WrapUnique(new StackFrameDeduplicator); 290 auto stack_frame_deduplicator = WrapUnique(new StackFrameDeduplicator);
286 auto tn_deduplicator = WrapUnique(new TypeNameDeduplicator); 291 auto type_name_deduplicator = WrapUnique(new TypeNameDeduplicator);
287 HeapDumpWriter writer(sf_deduplicator.get(), tn_deduplicator.get()); 292 HeapDumpWriter writer(stack_frame_deduplicator.get(),
293 type_name_deduplicator.get(),
294 512u);
288 const std::set<Entry>& dump = writer.Summarize(metrics_by_context); 295 const std::set<Entry>& dump = writer.Summarize(metrics_by_context);
289 296
290 // Get the indices of the backtraces and types by adding them again to the 297 // Get the indices of the backtraces and types by adding them again to the
291 // deduplicator. Because they were added before, the same number is returned. 298 // deduplicator. Because they were added before, the same number is returned.
292 StackFrame bt0[] = {kBrowserMain, kCreateWidget, kGetBitmap}; 299 StackFrame bt0[] = {kBrowserMain, kCreateWidget, kGetBitmap};
293 StackFrame bt1[] = {kBrowserMain, kCreateWidget, kInitialize}; 300 StackFrame bt1[] = {kBrowserMain, kCreateWidget, kInitialize};
294 int bt_browser_main = sf_deduplicator->Insert(bt0, bt0 + 1); 301 int bt_browser_main = stack_frame_deduplicator->Insert(bt0, bt0 + 1);
295 int bt_create_widget = sf_deduplicator->Insert(bt0, bt0 + 2); 302 int bt_create_widget = stack_frame_deduplicator->Insert(bt0, bt0 + 2);
296 int bt_get_bitmap = sf_deduplicator->Insert(bt0, bt0 + 3); 303 int bt_get_bitmap = stack_frame_deduplicator->Insert(bt0, bt0 + 3);
297 int bt_initialize = sf_deduplicator->Insert(bt1, bt1 + 3); 304 int bt_initialize = stack_frame_deduplicator->Insert(bt1, bt1 + 3);
298 305
299 // Full heap should have size of 1 MiB + 1 KiB and 3 chunks. 306 // Full heap should have size of 1 MiB + .9 KiB and 3 chunks.
300 AssertSizeAndCountEq(dump, -1, -1 /* No type specified */, 307 AssertSizeAndCountEq(dump, -1, -1 /* No type specified */,
301 {1024 * 1024 + 512 + 512, 3}); 308 {1024 * 1024 + 512 + 400, 3});
302 309
303 // |GetBitmap| allocated 1 MiB and 1 chunk. 310 // |GetBitmap| allocated 1 MiB and 1 chunk.
304 AssertSizeAndCountEq(dump, bt_get_bitmap, -1, {1024 * 1024, 1}); 311 AssertSizeAndCountEq(dump, bt_get_bitmap, -1, {1024 * 1024, 1});
305 312
306 // Because |GetBitmap| was dumped, all of its parent nodes should have been 313 // Because |GetBitmap| was dumped, all of its parent nodes should have been
307 // dumped too. |CreateWidget| has 1 MiB in |GetBitmap|, 512 bytes in 314 // dumped too. |CreateWidget| has 1 MiB in |GetBitmap|, 400 bytes in
308 // |Initialize|, and 512 bytes of its own and each in 1 chunk. 315 // |Initialize|, and 512 bytes of its own and each in 1 chunk.
309 AssertSizeAndCountEq(dump, bt_create_widget, -1, 316 AssertSizeAndCountEq(dump, bt_create_widget, -1,
310 {1024 * 1024 + 512 + 512, 3}); 317 {1024 * 1024 + 400 + 512, 3});
311 AssertSizeAndCountEq(dump, bt_browser_main, -1, {1024 * 1024 + 512 + 512, 3}); 318 AssertSizeAndCountEq(dump, bt_browser_main, -1, {1024 * 1024 + 400 + 512, 3});
312 319
313 // Initialize was not significant, it should not have been dumped. 320 // Initialize was not significant, it should not have been dumped.
314 AssertNotDumped(dump, bt_initialize, -1); 321 AssertNotDumped(dump, bt_initialize, -1);
315 } 322 }
316 323
317 } // namespace internal 324 } // namespace internal
318 } // namespace trace_event 325 } // namespace trace_event
319 } // namespace base 326 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698