blob: 74b350aee77b613de9c6b01aa7c3446e42ce9907 [file] [log] [blame]
njn734b8052007-11-01 04:40:37 +00001//--------------------------------------------------------------------*/
2//--- Massif: a heap profiling tool. ms_main.c ---*/
3//--------------------------------------------------------------------*/
nethercotec9f36922004-02-14 16:40:02 +00004
5/*
nethercote996901a2004-08-03 13:29:09 +00006 This file is part of Massif, a Valgrind tool for profiling memory
nethercotec9f36922004-02-14 16:40:02 +00007 usage of programs.
8
sewardjec062e82011-10-23 07:32:08 +00009 Copyright (C) 2003-2011 Nicholas Nethercote
njn2bc10122005-05-08 02:10:27 +000010 njn@valgrind.org
nethercotec9f36922004-02-14 16:40:02 +000011
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful, but
18 WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 02111-1307, USA.
26
27 The GNU General Public License is contained in the file COPYING.
28*/
29
njn734b8052007-11-01 04:40:37 +000030//---------------------------------------------------------------------------
31// XXX:
32//---------------------------------------------------------------------------
njn734b8052007-11-01 04:40:37 +000033// Todo -- nice, but less critical:
njnab23b612007-11-27 00:53:56 +000034// - do a graph-drawing test
njn734b8052007-11-01 04:40:37 +000035// - make file format more generic. Obstacles:
36// - unit prefixes are not generic
37// - preset column widths for stats are not generic
38// - preset column headers are not generic
39// - "Massif arguments:" line is not generic
40// - do snapshots on client requests
41// - (Michael Meeks): have an interactive way to request a dump
42// (callgrind_control-style)
43// - "profile now"
44// - "show me the extra allocations since the last snapshot"
45// - "start/stop logging" (eg. quickly skip boring bits)
46// - Add ability to draw multiple graphs, eg. heap-only, stack-only, total.
47// Give each graph a title. (try to do it generically!)
48// - allow truncation of long fnnames if the exact line number is
49// identified? [hmm, could make getting the name of alloc-fns more
50// difficult] [could dump full names to file, truncate in ms_print]
51// - make --show-below-main=no work
njna15b6662009-08-04 05:59:46 +000052// - Options like --alloc-fn='operator new(unsigned, std::nothrow_t const&)'
njn1a2741a2007-11-26 21:59:04 +000053// don't work in a .valgrindrc file or in $VALGRIND_OPTS.
54// m_commandline.c:add_args_from_string() needs to respect single quotes.
njn3b677e52007-12-03 21:29:22 +000055// - With --stack=yes, want to add a stack trace for detailed snapshots so
56// it's clear where/why the peak is occurring. (Mattieu Castet) Also,
57// possibly useful even with --stack=no? (Andi Yin)
njn734b8052007-11-01 04:40:37 +000058//
59// Performance:
60// - To run the benchmarks:
61//
62// perl perf/vg_perf --tools=massif --reps=3 perf/{heap,tinycc} massif
63// time valgrind --tool=massif --depth=100 konqueror
64//
65// The other benchmarks don't do much allocation, and so give similar speeds
66// to Nulgrind.
67//
68// Timing results on 'nevermore' (njn's machine) as of r7013:
69//
70// heap 0.53s ma:12.4s (23.5x, -----)
71// tinycc 0.46s ma: 4.9s (10.7x, -----)
72// many-xpts 0.08s ma: 2.0s (25.0x, -----)
73// konqueror 29.6s real 0:21.0s user
74//
njnab23b612007-11-27 00:53:56 +000075// [Introduction of --time-unit=i as the default slowed things down by
76// roughly 0--20%.]
77//
njn734b8052007-11-01 04:40:37 +000078// - get_XCon accounts for about 9% of konqueror startup time. Try
79// keeping XPt children sorted by 'ip' and use binary search in get_XCon.
80// Requires factoring out binary search code from various places into a
81// VG_(bsearch) function.
82//
83// Todo -- low priority:
njn734b8052007-11-01 04:40:37 +000084// - In each XPt, record both bytes and the number of allocations, and
85// possibly the global number of allocations.
njn0f85e022007-11-27 02:03:44 +000086// - (Andy Lin) Give a stack trace on detailed snapshots?
njn734b8052007-11-01 04:40:37 +000087// - (Artur Wisz) add a feature to Massif to ignore any heap blocks larger
88// than a certain size! Because: "linux's malloc allows to set a
89// MMAP_THRESHOLD value, so we set it to 4096 - all blocks above that will
90// be handled directly by the kernel, and are guaranteed to be returned to
91// the system when freed. So we needed to profile only blocks below this
92// limit."
93//
njn734b8052007-11-01 04:40:37 +000094// File format working notes:
95
96#if 0
97desc: --heap-admin=foo
98cmd: date
99time_unit: ms
100#-----------
101snapshot=0
102#-----------
103time=0
104mem_heap_B=0
105mem_heap_admin_B=0
106mem_stacks_B=0
107heap_tree=empty
108#-----------
109snapshot=1
110#-----------
111time=353
112mem_heap_B=5
113mem_heap_admin_B=0
114mem_stacks_B=0
115heap_tree=detailed
116n1: 5 (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
117 n1: 5 0x27F6E0: _nl_normalize_codeset (in /lib/libc-2.3.5.so)
118 n1: 5 0x279DE6: _nl_load_locale_from_archive (in /lib/libc-2.3.5.so)
119 n1: 5 0x278E97: _nl_find_locale (in /lib/libc-2.3.5.so)
120 n1: 5 0x278871: setlocale (in /lib/libc-2.3.5.so)
121 n1: 5 0x8049821: (within /bin/date)
122 n0: 5 0x26ED5E: (below main) (in /lib/libc-2.3.5.so)
123
124
125n_events: n time(ms) total(B) useful-heap(B) admin-heap(B) stacks(B)
126t_events: B
127n 0 0 0 0 0
128n 0 0 0 0 0
129t1: 5 <string...>
130 t1: 6 <string...>
131
132Ideas:
133- each snapshot specifies an x-axis value and one or more y-axis values.
134- can display the y-axis values separately if you like
135- can completely separate connection between snapshots and trees.
136
137Challenges:
138- how to specify and scale/abbreviate units on axes?
139- how to combine multiple values into the y-axis?
140
141--------------------------------------------------------------------------------Command: date
142Massif arguments: --heap-admin=foo
143ms_print arguments: massif.out
144--------------------------------------------------------------------------------
145 KB
1466.472^ :#
147 | :# :: . .
148 ...
149 | ::@ :@ :@ :@:::# :: : ::::
150 0 +-----------------------------------@---@---@-----@--@---#-------------->ms 0 713
151
152Number of snapshots: 50
153 Detailed snapshots: [2, 11, 13, 19, 25, 32 (peak)]
154-------------------------------------------------------------------------------- n time(ms) total(B) useful-heap(B) admin-heap(B) stacks(B)
155-------------------------------------------------------------------------------- 0 0 0 0 0 0
156 1 345 5 5 0 0
157 2 353 5 5 0 0
158100.00% (5B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
159->100.00% (5B) 0x27F6E0: _nl_normalize_codeset (in /lib/libc-2.3.5.so)
160#endif
161
162//---------------------------------------------------------------------------
nethercotec9f36922004-02-14 16:40:02 +0000163
njnc7561b92005-06-19 01:24:32 +0000164#include "pub_tool_basics.h"
sewardj4cfea4f2006-10-14 19:26:10 +0000165#include "pub_tool_vki.h"
sewardj45f4e7c2005-09-27 19:20:21 +0000166#include "pub_tool_aspacemgr.h"
njnea27e462005-05-31 02:38:09 +0000167#include "pub_tool_debuginfo.h"
njn81c00df2005-05-14 21:28:43 +0000168#include "pub_tool_hashtable.h"
njn97405b22005-06-02 03:39:33 +0000169#include "pub_tool_libcbase.h"
njn132bfcc2005-06-04 19:16:06 +0000170#include "pub_tool_libcassert.h"
njneb8896b2005-06-04 20:03:55 +0000171#include "pub_tool_libcfile.h"
njn36a20fa2005-06-03 03:08:39 +0000172#include "pub_tool_libcprint.h"
njnf39e9a32005-06-12 02:43:17 +0000173#include "pub_tool_libcproc.h"
njnb506bd82005-06-21 04:01:51 +0000174#include "pub_tool_machine.h"
njn717cde52005-05-10 02:47:21 +0000175#include "pub_tool_mallocfree.h"
njn20242342005-05-16 23:31:24 +0000176#include "pub_tool_options.h"
njn717cde52005-05-10 02:47:21 +0000177#include "pub_tool_replacemalloc.h"
njnd01fef72005-03-25 23:35:48 +0000178#include "pub_tool_stacktrace.h"
njne323a6b2010-07-01 02:35:03 +0000179#include "pub_tool_threadstate.h"
njn43b9a8a2005-05-10 04:37:01 +0000180#include "pub_tool_tooliface.h"
sewardj14c7cc52007-02-25 15:08:24 +0000181#include "pub_tool_xarray.h"
sewardj45f4e7c2005-09-27 19:20:21 +0000182#include "pub_tool_clientstate.h"
sewardj3b290482011-05-06 21:02:55 +0000183#include "pub_tool_gdbserver.h"
nethercotec9f36922004-02-14 16:40:02 +0000184
185#include "valgrind.h" // For {MALLOC,FREE}LIKE_BLOCK
186
njn734b8052007-11-01 04:40:37 +0000187//------------------------------------------------------------*/
188//--- Overview of operation ---*/
189//------------------------------------------------------------*/
nethercotec9f36922004-02-14 16:40:02 +0000190
njn734b8052007-11-01 04:40:37 +0000191// The size of the stacks and heap is tracked. The heap is tracked in a lot
192// of detail, enough to tell how many bytes each line of code is responsible
193// for, more or less. The main data structure is a tree representing the
194// call tree beneath all the allocation functions like malloc().
njne323a6b2010-07-01 02:35:03 +0000195// (Alternatively, if --pages-as-heap=yes is specified, memory is tracked at
196// the page level, and each page is treated much like a heap block. We use
197// "heap" throughout below to cover this case because the concepts are all the
198// same.)
nethercotec9f36922004-02-14 16:40:02 +0000199//
njn734b8052007-11-01 04:40:37 +0000200// "Snapshots" are recordings of the memory usage. There are two basic
201// kinds:
202// - Normal: these record the current time, total memory size, total heap
203// size, heap admin size and stack size.
204// - Detailed: these record those things in a normal snapshot, plus a very
205// detailed XTree (see below) indicating how the heap is structured.
nethercotec9f36922004-02-14 16:40:02 +0000206//
njn734b8052007-11-01 04:40:37 +0000207// Snapshots are taken every so often. There are two storage classes of
208// snapshots:
209// - Temporary: Massif does a temporary snapshot every so often. The idea
210// is to always have a certain number of temporary snapshots around. So
211// we take them frequently to begin with, but decreasingly often as the
212// program continues to run. Also, we remove some old ones after a while.
213// Overall it's a kind of exponential decay thing. Most of these are
214// normal snapshots, a small fraction are detailed snapshots.
215// - Permanent: Massif takes a permanent (detailed) snapshot in some
216// circumstances. They are:
217// - Peak snapshot: When the memory usage peak is reached, it takes a
218// snapshot. It keeps this, unless the peak is subsequently exceeded,
219// in which case it will overwrite the peak snapshot.
220// - User-requested snapshots: These are done in response to client
221// requests. They are always kept.
nethercotec9f36922004-02-14 16:40:02 +0000222
njn734b8052007-11-01 04:40:37 +0000223// Used for printing things when clo_verbosity > 1.
224#define VERB(verb, format, args...) \
225 if (VG_(clo_verbosity) > verb) { \
sewardj5ea0f392009-07-15 14:51:34 +0000226 VG_(dmsg)("Massif: " format, ##args); \
nethercotec9f36922004-02-14 16:40:02 +0000227 }
nethercotec9f36922004-02-14 16:40:02 +0000228
sewardj2d9e8742009-08-07 15:46:56 +0000229// Used for printing stats when clo_stats == True.
230#define STATS(format, args...) \
231 if (VG_(clo_stats)) { \
232 VG_(dmsg)("Massif: " format, ##args); \
233 }
234
njn734b8052007-11-01 04:40:37 +0000235//------------------------------------------------------------//
236//--- Statistics ---//
237//------------------------------------------------------------//
nethercotec9f36922004-02-14 16:40:02 +0000238
239// Konqueror startup, to give an idea of the numbers involved with a biggish
240// program, with default depth:
241//
242// depth=3 depth=40
243// - 310,000 allocations
244// - 300,000 frees
245// - 15,000 XPts 800,000 XPts
246// - 1,800 top-XPts
247
njnf6b00762009-04-17 04:26:41 +0000248static UInt n_heap_allocs = 0;
249static UInt n_heap_reallocs = 0;
250static UInt n_heap_frees = 0;
251static UInt n_ignored_heap_allocs = 0;
252static UInt n_ignored_heap_frees = 0;
253static UInt n_ignored_heap_reallocs = 0;
254static UInt n_stack_allocs = 0;
255static UInt n_stack_frees = 0;
256static UInt n_xpts = 0;
257static UInt n_xpt_init_expansions = 0;
258static UInt n_xpt_later_expansions = 0;
259static UInt n_sxpt_allocs = 0;
260static UInt n_sxpt_frees = 0;
261static UInt n_skipped_snapshots = 0;
262static UInt n_real_snapshots = 0;
263static UInt n_detailed_snapshots = 0;
264static UInt n_peak_snapshots = 0;
265static UInt n_cullings = 0;
266static UInt n_XCon_redos = 0;
nethercotec9f36922004-02-14 16:40:02 +0000267
njn734b8052007-11-01 04:40:37 +0000268//------------------------------------------------------------//
269//--- Globals ---//
270//------------------------------------------------------------//
nethercotec9f36922004-02-14 16:40:02 +0000271
njn1a2741a2007-11-26 21:59:04 +0000272// Number of guest instructions executed so far. Only used with
273// --time-unit=i.
274static Long guest_instrs_executed = 0;
275
njn32397c02007-11-10 04:08:08 +0000276static SizeT heap_szB = 0; // Live heap size
277static SizeT heap_extra_szB = 0; // Live heap extra size -- slop + admin bytes
278static SizeT stacks_szB = 0; // Live stacks size
nethercotec9f36922004-02-14 16:40:02 +0000279
njn734b8052007-11-01 04:40:37 +0000280// This is the total size from the current peak snapshot, or 0 if no peak
281// snapshot has been taken yet.
282static SizeT peak_snapshot_total_szB = 0;
nethercotec9f36922004-02-14 16:40:02 +0000283
njn734b8052007-11-01 04:40:37 +0000284// Incremented every time memory is allocated/deallocated, by the
285// allocated/deallocated amount; includes heap, heap-admin and stack
286// memory. An alternative to milliseconds as a unit of program "time".
287static ULong total_allocs_deallocs_szB = 0;
nethercotec9f36922004-02-14 16:40:02 +0000288
njne323a6b2010-07-01 02:35:03 +0000289// When running with --heap=yes --pages-as-heap=no, we don't start taking
290// snapshots until the first basic block is executed, rather than doing it in
291// ms_post_clo_init (which is the obvious spot), for two reasons.
njn734b8052007-11-01 04:40:37 +0000292// - It lets us ignore stack events prior to that, because they're not
293// really proper ones and just would screw things up.
294// - Because there's still some core initialisation to do, and so there
295// would be an artificial time gap between the first and second snapshots.
296//
njne323a6b2010-07-01 02:35:03 +0000297// When running with --heap=yes --pages-as-heap=yes, snapshots start much
298// earlier due to new_mem_startup so this isn't relevant.
299//
njn734b8052007-11-01 04:40:37 +0000300static Bool have_started_executing_code = False;
nethercotec9f36922004-02-14 16:40:02 +0000301
njn734b8052007-11-01 04:40:37 +0000302//------------------------------------------------------------//
303//--- Alloc fns ---//
304//------------------------------------------------------------//
nethercotec9f36922004-02-14 16:40:02 +0000305
njn734b8052007-11-01 04:40:37 +0000306static XArray* alloc_fns;
njnf6b00762009-04-17 04:26:41 +0000307static XArray* ignore_fns;
nethercotec9f36922004-02-14 16:40:02 +0000308
njn734b8052007-11-01 04:40:37 +0000309static void init_alloc_fns(void)
310{
311 // Create the list, and add the default elements.
sewardj9c606bd2008-09-18 18:12:50 +0000312 alloc_fns = VG_(newXA)(VG_(malloc), "ms.main.iaf.1",
313 VG_(free), sizeof(Char*));
njn734b8052007-11-01 04:40:37 +0000314 #define DO(x) { Char* s = x; VG_(addToXA)(alloc_fns, &s); }
nethercotec9f36922004-02-14 16:40:02 +0000315
njn13118f82009-07-01 02:44:12 +0000316 // Ordered roughly according to (presumed) frequency.
njn734b8052007-11-01 04:40:37 +0000317 // Nb: The C++ "operator new*" ones are overloadable. We include them
318 // always anyway, because even if they're overloaded, it would be a
319 // prodigiously stupid overloading that caused them to not allocate
320 // memory.
njn343a5042009-07-01 05:05:38 +0000321 //
322 // XXX: because we don't look at the first stack entry (unless it's a
323 // custom allocation) there's not much point to having all these alloc
324 // functions here -- they should never appear anywhere (I think?) other
325 // than the top stack entry. The only exceptions are those that in
326 // vg_replace_malloc.c are partly or fully implemented in terms of another
327 // alloc function: realloc (which uses malloc); valloc,
328 // malloc_zone_valloc, posix_memalign and memalign_common (which use
329 // memalign).
330 //
njn734b8052007-11-01 04:40:37 +0000331 DO("malloc" );
332 DO("__builtin_new" );
333 DO("operator new(unsigned)" );
334 DO("operator new(unsigned long)" );
335 DO("__builtin_vec_new" );
336 DO("operator new[](unsigned)" );
337 DO("operator new[](unsigned long)" );
338 DO("calloc" );
339 DO("realloc" );
340 DO("memalign" );
njn13118f82009-07-01 02:44:12 +0000341 DO("posix_memalign" );
342 DO("valloc" );
njn734b8052007-11-01 04:40:37 +0000343 DO("operator new(unsigned, std::nothrow_t const&)" );
344 DO("operator new[](unsigned, std::nothrow_t const&)" );
345 DO("operator new(unsigned long, std::nothrow_t const&)" );
346 DO("operator new[](unsigned long, std::nothrow_t const&)");
sewardj6e9de462011-06-28 07:25:29 +0000347#if defined(VGO_darwin)
njn13118f82009-07-01 02:44:12 +0000348 DO("malloc_zone_malloc" );
349 DO("malloc_zone_calloc" );
350 DO("malloc_zone_realloc" );
351 DO("malloc_zone_memalign" );
352 DO("malloc_zone_valloc" );
353#endif
njn734b8052007-11-01 04:40:37 +0000354}
nethercotec9f36922004-02-14 16:40:02 +0000355
njnf6b00762009-04-17 04:26:41 +0000356static void init_ignore_fns(void)
njn734b8052007-11-01 04:40:37 +0000357{
njnf6b00762009-04-17 04:26:41 +0000358 // Create the (empty) list.
359 ignore_fns = VG_(newXA)(VG_(malloc), "ms.main.iif.1",
360 VG_(free), sizeof(Char*));
361}
362
363// Determines if the named function is a member of the XArray.
364static Bool is_member_fn(XArray* fns, Char* fnname)
365{
366 Char** fn_ptr;
njn734b8052007-11-01 04:40:37 +0000367 Int i;
368
369 // Nb: It's a linear search through the list, because we're comparing
370 // strings rather than pointers to strings.
371 // Nb: This gets called a lot. It was an OSet, but they're quite slow to
372 // iterate through so it wasn't a good choice.
njnf6b00762009-04-17 04:26:41 +0000373 for (i = 0; i < VG_(sizeXA)(fns); i++) {
374 fn_ptr = VG_(indexXA)(fns, i);
375 if (VG_STREQ(fnname, *fn_ptr))
njn734b8052007-11-01 04:40:37 +0000376 return True;
nethercotec9f36922004-02-14 16:40:02 +0000377 }
njn734b8052007-11-01 04:40:37 +0000378 return False;
379}
nethercotec9f36922004-02-14 16:40:02 +0000380
njn734b8052007-11-01 04:40:37 +0000381
382//------------------------------------------------------------//
383//--- Command line args ---//
384//------------------------------------------------------------//
385
386#define MAX_DEPTH 200
387
njn1a2741a2007-11-26 21:59:04 +0000388typedef enum { TimeI, TimeMS, TimeB } TimeUnit;
njn734b8052007-11-01 04:40:37 +0000389
390static Char* TimeUnit_to_string(TimeUnit time_unit)
391{
392 switch (time_unit) {
njn1a2741a2007-11-26 21:59:04 +0000393 case TimeI: return "i";
njn734b8052007-11-01 04:40:37 +0000394 case TimeMS: return "ms";
395 case TimeB: return "B";
396 default: tl_assert2(0, "TimeUnit_to_string: unrecognised TimeUnit");
397 }
398}
399
njne323a6b2010-07-01 02:35:03 +0000400static Bool clo_heap = True;
njn429afb42007-11-02 04:12:48 +0000401 // clo_heap_admin is deliberately a word-sized type. At one point it was
402 // a UInt, but this caused problems on 64-bit machines when it was
403 // multiplied by a small negative number and then promoted to a
404 // word-sized type -- it ended up with a value of 4.2 billion. Sigh.
njnefc13c22009-02-23 06:44:51 +0000405static SSizeT clo_heap_admin = 8;
njne323a6b2010-07-01 02:35:03 +0000406static Bool clo_pages_as_heap = False;
njn62721e92007-11-11 22:15:58 +0000407static Bool clo_stacks = False;
njn83df0b62009-02-25 01:01:05 +0000408static Int clo_depth = 30;
njn62721e92007-11-11 22:15:58 +0000409static double clo_threshold = 1.0; // percentage
410static double clo_peak_inaccuracy = 1.0; // percentage
njn83df0b62009-02-25 01:01:05 +0000411static Int clo_time_unit = TimeI;
412static Int clo_detailed_freq = 10;
413static Int clo_max_snapshots = 100;
njnf4c665f2007-11-20 07:55:40 +0000414static Char* clo_massif_out_file = "massif.out.%p";
njn734b8052007-11-01 04:40:37 +0000415
416static XArray* args_for_massif;
nethercotec9f36922004-02-14 16:40:02 +0000417
njn51d827b2005-05-09 01:02:08 +0000418static Bool ms_process_cmd_line_option(Char* arg)
nethercotec9f36922004-02-14 16:40:02 +0000419{
njn83df0b62009-02-25 01:01:05 +0000420 Char* tmp_str;
421
njn734b8052007-11-01 04:40:37 +0000422 // Remember the arg for later use.
423 VG_(addToXA)(args_for_massif, &arg);
nethercotec9f36922004-02-14 16:40:02 +0000424
njne323a6b2010-07-01 02:35:03 +0000425 if VG_BOOL_CLO(arg, "--heap", clo_heap) {}
426 else if VG_BINT_CLO(arg, "--heap-admin", clo_heap_admin, 0, 1024) {}
njn734b8052007-11-01 04:40:37 +0000427
njne323a6b2010-07-01 02:35:03 +0000428 else if VG_BOOL_CLO(arg, "--stacks", clo_stacks) {}
njn734b8052007-11-01 04:40:37 +0000429
njne323a6b2010-07-01 02:35:03 +0000430 else if VG_BOOL_CLO(arg, "--pages-as-heap", clo_pages_as_heap) {}
431
432 else if VG_BINT_CLO(arg, "--depth", clo_depth, 1, MAX_DEPTH) {}
433
434 else if VG_STR_CLO(arg, "--alloc-fn", tmp_str) {
435 VG_(addToXA)(alloc_fns, &tmp_str);
436 }
437 else if VG_STR_CLO(arg, "--ignore-fn", tmp_str) {
438 VG_(addToXA)(ignore_fns, &tmp_str);
439 }
440
njnb1cc5d62010-07-06 04:05:23 +0000441 else if VG_DBL_CLO(arg, "--threshold", clo_threshold) {
442 if (clo_threshold < 0 || clo_threshold > 100) {
443 VG_(fmsg_bad_option)(arg,
444 "--threshold must be between 0.0 and 100.0\n");
445 }
446 }
njn734b8052007-11-01 04:40:37 +0000447
njn83df0b62009-02-25 01:01:05 +0000448 else if VG_DBL_CLO(arg, "--peak-inaccuracy", clo_peak_inaccuracy) {}
njn734b8052007-11-01 04:40:37 +0000449
njne323a6b2010-07-01 02:35:03 +0000450 else if VG_XACT_CLO(arg, "--time-unit=i", clo_time_unit, TimeI) {}
451 else if VG_XACT_CLO(arg, "--time-unit=ms", clo_time_unit, TimeMS) {}
452 else if VG_XACT_CLO(arg, "--time-unit=B", clo_time_unit, TimeB) {}
njn734b8052007-11-01 04:40:37 +0000453
njne006e6f2010-12-08 02:42:38 +0000454 else if VG_BINT_CLO(arg, "--detailed-freq", clo_detailed_freq, 1, 1000000) {}
nethercotec9f36922004-02-14 16:40:02 +0000455
njne323a6b2010-07-01 02:35:03 +0000456 else if VG_BINT_CLO(arg, "--max-snapshots", clo_max_snapshots, 10, 1000) {}
457
njn83df0b62009-02-25 01:01:05 +0000458 else if VG_STR_CLO(arg, "--massif-out-file", clo_massif_out_file) {}
njnf4c665f2007-11-20 07:55:40 +0000459
nethercotec9f36922004-02-14 16:40:02 +0000460 else
461 return VG_(replacement_malloc_process_cmd_line_option)(arg);
nethercote27fec902004-06-16 21:26:32 +0000462
nethercotec9f36922004-02-14 16:40:02 +0000463 return True;
464}
465
njn51d827b2005-05-09 01:02:08 +0000466static void ms_print_usage(void)
nethercotec9f36922004-02-14 16:40:02 +0000467{
njn734b8052007-11-01 04:40:37 +0000468 VG_(printf)(
nethercotec9f36922004-02-14 16:40:02 +0000469" --heap=no|yes profile heap blocks [yes]\n"
njna15b6662009-08-04 05:59:46 +0000470" --heap-admin=<size> average admin bytes per heap block;\n"
njn734b8052007-11-01 04:40:37 +0000471" ignored if --heap=no [8]\n"
472" --stacks=no|yes profile stack(s) [no]\n"
njne323a6b2010-07-01 02:35:03 +0000473" --pages-as-heap=no|yes profile memory at the page level [no]\n"
njn734b8052007-11-01 04:40:37 +0000474" --depth=<number> depth of contexts [30]\n"
njnf6b00762009-04-17 04:26:41 +0000475" --alloc-fn=<name> specify <name> as an alloc function [empty]\n"
476" --ignore-fn=<name> ignore heap allocations within <name> [empty]\n"
njn62721e92007-11-11 22:15:58 +0000477" --threshold=<m.n> significance threshold, as a percentage [1.0]\n"
478" --peak-inaccuracy=<m.n> maximum peak inaccuracy, as a percentage [1.0]\n"
njn1a2741a2007-11-26 21:59:04 +0000479" --time-unit=i|ms|B time unit: instructions executed, milliseconds\n"
480" or heap bytes alloc'd/dealloc'd [i]\n"
njn734b8052007-11-01 04:40:37 +0000481" --detailed-freq=<N> every Nth snapshot should be detailed [10]\n"
482" --max-snapshots=<N> maximum number of snapshots recorded [100]\n"
njn374a36d2007-11-23 01:41:32 +0000483" --massif-out-file=<file> output file name [massif.out.%%p]\n"
nethercotec9f36922004-02-14 16:40:02 +0000484 );
nethercotec9f36922004-02-14 16:40:02 +0000485}
486
njn51d827b2005-05-09 01:02:08 +0000487static void ms_print_debug_usage(void)
nethercotec9f36922004-02-14 16:40:02 +0000488{
njn97db7612009-08-04 02:32:55 +0000489 VG_(printf)(
490" (none)\n"
491 );
nethercotec9f36922004-02-14 16:40:02 +0000492}
493
njn734b8052007-11-01 04:40:37 +0000494
495//------------------------------------------------------------//
496//--- XPts, XTrees and XCons ---//
497//------------------------------------------------------------//
498
499// An XPt represents an "execution point", ie. a code address. Each XPt is
500// part of a tree of XPts (an "execution tree", or "XTree"). The details of
501// the heap are represented by a single XTree.
502//
503// The root of the tree is 'alloc_xpt', which represents all allocation
504// functions, eg:
505// - malloc/calloc/realloc/memalign/new/new[];
506// - user-specified allocation functions (using --alloc-fn);
507// - custom allocation (MALLOCLIKE) points
508// It's a bit of a fake XPt (ie. its 'ip' is zero), and is only used because
509// it makes the code simpler.
510//
511// Any child of 'alloc_xpt' is called a "top-XPt". The XPts at the bottom
512// of an XTree (leaf nodes) are "bottom-XPTs".
513//
514// Each path from a top-XPt to a bottom-XPt through an XTree gives an
515// execution context ("XCon"), ie. a stack trace. (And sub-paths represent
516// stack sub-traces.) The number of XCons in an XTree is equal to the
517// number of bottom-XPTs in that XTree.
518//
519// alloc_xpt XTrees are bi-directional.
520// | ^
521// v |
522// > parent < Example: if child1() calls parent() and child2()
523// / | \ also calls parent(), and parent() calls malloc(),
524// | / \ | the XTree will look like this.
525// | v v |
526// child1 child2
527//
njndbeb5352007-12-04 03:15:23 +0000528// (Note that malformed stack traces can lead to difficulties. See the
529// comment at the bottom of get_XCon.)
530//
njn734b8052007-11-01 04:40:37 +0000531// XTrees and XPts are mirrored by SXTrees and SXPts, where the 'S' is short
532// for "saved". When the XTree is duplicated for a snapshot, we duplicate
533// it as an SXTree, which is similar but omits some things it does not need,
534// and aggregates up insignificant nodes. This is important as an SXTree is
535// typically much smaller than an XTree.
536
537// XXX: make XPt and SXPt extensible arrays, to avoid having to do two
538// allocations per Pt.
539
540typedef struct _XPt XPt;
541struct _XPt {
542 Addr ip; // code address
543
544 // Bottom-XPts: space for the precise context.
545 // Other XPts: space of all the descendent bottom-XPts.
546 // Nb: this value goes up and down as the program executes.
547 SizeT szB;
548
549 XPt* parent; // pointer to parent XPt
550
551 // Children.
552 // n_children and max_children are 32-bit integers. 16-bit integers
553 // are too small -- a very big program might have more than 65536
554 // allocation points (ie. top-XPts) -- Konqueror starting up has 1800.
555 UInt n_children; // number of children
556 UInt max_children; // capacity of children array
557 XPt** children; // pointers to children XPts
558};
559
560typedef
561 enum {
562 SigSXPt,
563 InsigSXPt
564 }
565 SXPtTag;
566
567typedef struct _SXPt SXPt;
568struct _SXPt {
569 SXPtTag tag;
570 SizeT szB; // memory size for the node, be it Sig or Insig
571 union {
572 // An SXPt representing a single significant code location. Much like
573 // an XPt, minus the fields that aren't necessary.
574 struct {
575 Addr ip;
576 UInt n_children;
577 SXPt** children;
578 }
579 Sig;
580
581 // An SXPt representing one or more code locations, all below the
582 // significance threshold.
583 struct {
584 Int n_xpts; // number of aggregated XPts
585 }
586 Insig;
587 };
588};
nethercotec9f36922004-02-14 16:40:02 +0000589
590// Fake XPt representing all allocation functions like malloc(). Acts as
591// parent node to all top-XPts.
592static XPt* alloc_xpt;
593
594// Cheap allocation for blocks that never need to be freed. Saves about 10%
595// for Konqueror startup with --depth=40.
nethercote7ac7f7b2004-11-02 12:36:02 +0000596static void* perm_malloc(SizeT n_bytes)
nethercotec9f36922004-02-14 16:40:02 +0000597{
598 static Addr hp = 0; // current heap pointer
599 static Addr hp_lim = 0; // maximum usable byte in current block
600
601 #define SUPERBLOCK_SIZE (1 << 20) // 1 MB
602
603 if (hp + n_bytes > hp_lim) {
sewardj45f4e7c2005-09-27 19:20:21 +0000604 hp = (Addr)VG_(am_shadow_alloc)(SUPERBLOCK_SIZE);
njn84f32b22009-02-10 07:14:37 +0000605 if (0 == hp)
njn734b8052007-11-01 04:40:37 +0000606 VG_(out_of_memory_NORETURN)( "massif:perm_malloc",
sewardj45f4e7c2005-09-27 19:20:21 +0000607 SUPERBLOCK_SIZE);
nethercotec9f36922004-02-14 16:40:02 +0000608 hp_lim = hp + SUPERBLOCK_SIZE - 1;
609 }
610
611 hp += n_bytes;
612
613 return (void*)(hp - n_bytes);
614}
615
njn734b8052007-11-01 04:40:37 +0000616static XPt* new_XPt(Addr ip, XPt* parent)
nethercotec9f36922004-02-14 16:40:02 +0000617{
njn734b8052007-11-01 04:40:37 +0000618 // XPts are never freed, so we can use perm_malloc to allocate them.
619 // Note that we cannot use perm_malloc for the 'children' array, because
620 // that needs to be resizable.
621 XPt* xpt = perm_malloc(sizeof(XPt));
622 xpt->ip = ip;
623 xpt->szB = 0;
624 xpt->parent = parent;
nethercotec9f36922004-02-14 16:40:02 +0000625
njn734b8052007-11-01 04:40:37 +0000626 // We don't initially allocate any space for children. We let that
627 // happen on demand. Many XPts (ie. all the bottom-XPts) don't have any
628 // children anyway.
nethercotec9f36922004-02-14 16:40:02 +0000629 xpt->n_children = 0;
njn734b8052007-11-01 04:40:37 +0000630 xpt->max_children = 0;
631 xpt->children = NULL;
nethercotec9f36922004-02-14 16:40:02 +0000632
633 // Update statistics
634 n_xpts++;
635
636 return xpt;
637}
638
njn734b8052007-11-01 04:40:37 +0000639static void add_child_xpt(XPt* parent, XPt* child)
640{
641 // Expand 'children' if necessary.
642 tl_assert(parent->n_children <= parent->max_children);
643 if (parent->n_children == parent->max_children) {
njn84f32b22009-02-10 07:14:37 +0000644 if (0 == parent->max_children) {
njn734b8052007-11-01 04:40:37 +0000645 parent->max_children = 4;
sewardj9c606bd2008-09-18 18:12:50 +0000646 parent->children = VG_(malloc)( "ms.main.acx.1",
647 parent->max_children * sizeof(XPt*) );
njn734b8052007-11-01 04:40:37 +0000648 n_xpt_init_expansions++;
649 } else {
650 parent->max_children *= 2; // Double size
sewardj9c606bd2008-09-18 18:12:50 +0000651 parent->children = VG_(realloc)( "ms.main.acx.2",
652 parent->children,
njn734b8052007-11-01 04:40:37 +0000653 parent->max_children * sizeof(XPt*) );
654 n_xpt_later_expansions++;
655 }
656 }
657
658 // Insert new child XPt in parent's children list.
659 parent->children[ parent->n_children++ ] = child;
660}
661
662// Reverse comparison for a reverse sort -- biggest to smallest.
663static Int SXPt_revcmp_szB(void* n1, void* n2)
664{
665 SXPt* sxpt1 = *(SXPt**)n1;
666 SXPt* sxpt2 = *(SXPt**)n2;
667 return ( sxpt1->szB < sxpt2->szB ? 1
668 : sxpt1->szB > sxpt2->szB ? -1
669 : 0);
670}
671
672//------------------------------------------------------------//
673//--- XTree Operations ---//
674//------------------------------------------------------------//
675
676// Duplicates an XTree as an SXTree.
677static SXPt* dup_XTree(XPt* xpt, SizeT total_szB)
678{
679 Int i, n_sig_children, n_insig_children, n_child_sxpts;
njndbeb5352007-12-04 03:15:23 +0000680 SizeT sig_child_threshold_szB;
njn734b8052007-11-01 04:40:37 +0000681 SXPt* sxpt;
682
683 // Number of XPt children Action for SXPT
684 // ------------------ ---------------
685 // 0 sig, 0 insig alloc 0 children
686 // N sig, 0 insig alloc N children, dup all
687 // N sig, M insig alloc N+1, dup first N, aggregate remaining M
688 // 0 sig, M insig alloc 1, aggregate M
689
690 // Work out how big a child must be to be significant. If the current
691 // total_szB is zero, then we set it to 1, which means everything will be
692 // judged insignificant -- this is sensible, as there's no point showing
693 // any detail for this case. Unless they used --threshold=0, in which
694 // case we show them everything because that's what they asked for.
695 //
696 // Nb: We do this once now, rather than once per child, because if we do
697 // that the cost of all the divisions adds up to something significant.
njn84f32b22009-02-10 07:14:37 +0000698 if (0 == total_szB && 0 != clo_threshold) {
njn734b8052007-11-01 04:40:37 +0000699 sig_child_threshold_szB = 1;
700 } else {
njn62721e92007-11-11 22:15:58 +0000701 sig_child_threshold_szB = (SizeT)((total_szB * clo_threshold) / 100);
njn734b8052007-11-01 04:40:37 +0000702 }
703
704 // How many children are significant? And do we need an aggregate SXPt?
705 n_sig_children = 0;
706 for (i = 0; i < xpt->n_children; i++) {
707 if (xpt->children[i]->szB >= sig_child_threshold_szB) {
708 n_sig_children++;
709 }
710 }
711 n_insig_children = xpt->n_children - n_sig_children;
712 n_child_sxpts = n_sig_children + ( n_insig_children > 0 ? 1 : 0 );
713
714 // Duplicate the XPt.
sewardj9c606bd2008-09-18 18:12:50 +0000715 sxpt = VG_(malloc)("ms.main.dX.1", sizeof(SXPt));
njn734b8052007-11-01 04:40:37 +0000716 n_sxpt_allocs++;
717 sxpt->tag = SigSXPt;
718 sxpt->szB = xpt->szB;
719 sxpt->Sig.ip = xpt->ip;
720 sxpt->Sig.n_children = n_child_sxpts;
721
722 // Create the SXPt's children.
723 if (n_child_sxpts > 0) {
724 Int j;
njndbeb5352007-12-04 03:15:23 +0000725 SizeT sig_children_szB = 0, insig_children_szB = 0;
sewardj9c606bd2008-09-18 18:12:50 +0000726 sxpt->Sig.children = VG_(malloc)("ms.main.dX.2",
727 n_child_sxpts * sizeof(SXPt*));
njn734b8052007-11-01 04:40:37 +0000728
njndbeb5352007-12-04 03:15:23 +0000729 // Duplicate the significant children. (Nb: sig_children_szB +
730 // insig_children_szB doesn't necessarily equal xpt->szB.)
njn734b8052007-11-01 04:40:37 +0000731 j = 0;
732 for (i = 0; i < xpt->n_children; i++) {
733 if (xpt->children[i]->szB >= sig_child_threshold_szB) {
734 sxpt->Sig.children[j++] = dup_XTree(xpt->children[i], total_szB);
njndbeb5352007-12-04 03:15:23 +0000735 sig_children_szB += xpt->children[i]->szB;
736 } else {
737 insig_children_szB += xpt->children[i]->szB;
njn734b8052007-11-01 04:40:37 +0000738 }
739 }
740
741 // Create the SXPt for the insignificant children, if any, and put it
742 // in the last child entry.
njn734b8052007-11-01 04:40:37 +0000743 if (n_insig_children > 0) {
744 // Nb: We 'n_sxpt_allocs' here because creating an Insig SXPt
745 // doesn't involve a call to dup_XTree().
sewardj9c606bd2008-09-18 18:12:50 +0000746 SXPt* insig_sxpt = VG_(malloc)("ms.main.dX.3", sizeof(SXPt));
njn734b8052007-11-01 04:40:37 +0000747 n_sxpt_allocs++;
748 insig_sxpt->tag = InsigSXPt;
749 insig_sxpt->szB = insig_children_szB;
750 insig_sxpt->Insig.n_xpts = n_insig_children;
751 sxpt->Sig.children[n_sig_children] = insig_sxpt;
752 }
753 } else {
754 sxpt->Sig.children = NULL;
755 }
756
757 return sxpt;
758}
759
760static void free_SXTree(SXPt* sxpt)
761{
762 Int i;
763 tl_assert(sxpt != NULL);
764
765 switch (sxpt->tag) {
766 case SigSXPt:
767 // Free all children SXPts, then the children array.
768 for (i = 0; i < sxpt->Sig.n_children; i++) {
769 free_SXTree(sxpt->Sig.children[i]);
770 sxpt->Sig.children[i] = NULL;
771 }
772 VG_(free)(sxpt->Sig.children); sxpt->Sig.children = NULL;
773 break;
774
775 case InsigSXPt:
776 break;
777
778 default: tl_assert2(0, "free_SXTree: unknown SXPt tag");
779 }
780
781 // Free the SXPt itself.
782 VG_(free)(sxpt); sxpt = NULL;
783 n_sxpt_frees++;
784}
785
786// Sanity checking: we periodically check the heap XTree with
787// ms_expensive_sanity_check.
788static void sanity_check_XTree(XPt* xpt, XPt* parent)
nethercotec9f36922004-02-14 16:40:02 +0000789{
njn734b8052007-11-01 04:40:37 +0000790 tl_assert(xpt != NULL);
791
792 // Check back-pointer.
793 tl_assert2(xpt->parent == parent,
794 "xpt->parent = %p, parent = %p\n", xpt->parent, parent);
795
796 // Check children counts look sane.
797 tl_assert(xpt->n_children <= xpt->max_children);
798
njndbeb5352007-12-04 03:15:23 +0000799 // Unfortunately, xpt's size is not necessarily equal to the sum of xpt's
800 // children's sizes. See comment at the bottom of get_XCon.
nethercotec9f36922004-02-14 16:40:02 +0000801}
802
njn734b8052007-11-01 04:40:37 +0000803// Sanity checking: we check SXTrees (which are in snapshots) after
804// snapshots are created, before they are deleted, and before they are
805// printed.
806static void sanity_check_SXTree(SXPt* sxpt)
nethercotec9f36922004-02-14 16:40:02 +0000807{
njn734b8052007-11-01 04:40:37 +0000808 Int i;
nethercotec9f36922004-02-14 16:40:02 +0000809
njn734b8052007-11-01 04:40:37 +0000810 tl_assert(sxpt != NULL);
nethercotec9f36922004-02-14 16:40:02 +0000811
njn734b8052007-11-01 04:40:37 +0000812 // Check the sum of any children szBs equals the SXPt's szB. Check the
813 // children at the same time.
814 switch (sxpt->tag) {
815 case SigSXPt: {
816 if (sxpt->Sig.n_children > 0) {
njn734b8052007-11-01 04:40:37 +0000817 for (i = 0; i < sxpt->Sig.n_children; i++) {
818 sanity_check_SXTree(sxpt->Sig.children[i]);
njn734b8052007-11-01 04:40:37 +0000819 }
njn734b8052007-11-01 04:40:37 +0000820 }
821 break;
822 }
823 case InsigSXPt:
824 break; // do nothing
825
826 default: tl_assert2(0, "sanity_check_SXTree: unknown SXPt tag");
827 }
828}
829
830
831//------------------------------------------------------------//
832//--- XCon Operations ---//
833//------------------------------------------------------------//
834
835// This is the limit on the number of removed alloc-fns that can be in a
836// single XCon.
837#define MAX_OVERESTIMATE 50
838#define MAX_IPS (MAX_DEPTH + MAX_OVERESTIMATE)
839
njnb11e5aa2008-01-20 22:30:52 +0000840// This is used for various buffers which can hold function names/IP
841// description. Some C++ names can get really long so 1024 isn't big
842// enough.
843#define BUF_LEN 2048
844
njnf6b00762009-04-17 04:26:41 +0000845// Determine if the given IP belongs to a function that should be ignored.
846static Bool fn_should_be_ignored(Addr ip)
847{
848 static Char buf[BUF_LEN];
849 return
850 ( VG_(get_fnname)(ip, buf, BUF_LEN) && is_member_fn(ignore_fns, buf)
851 ? True : False );
852}
853
njn734b8052007-11-01 04:40:37 +0000854// Get the stack trace for an XCon, filtering out uninteresting entries:
855// alloc-fns and entries above alloc-fns, and entries below main-or-below-main.
856// Eg: alloc-fn1 / alloc-fn2 / a / b / main / (below main) / c
857// becomes: a / b / main
858// Nb: it's possible to end up with an empty trace, eg. if 'main' is marked
859// as an alloc-fn. This is ok.
860static
njne323a6b2010-07-01 02:35:03 +0000861Int get_IPs( ThreadId tid, Bool exclude_first_entry, Addr ips[])
njn734b8052007-11-01 04:40:37 +0000862{
njnf6b00762009-04-17 04:26:41 +0000863 static Char buf[BUF_LEN];
njn734b8052007-11-01 04:40:37 +0000864 Int n_ips, i, n_alloc_fns_removed;
865 Int overestimate;
866 Bool redo;
867
868 // We ask for a few more IPs than clo_depth suggests we need. Then we
869 // remove every entry that is an alloc-fn. Depending on the
870 // circumstances, we may need to redo it all, asking for more IPs.
871 // Details:
872 // - If the original stack trace is smaller than asked-for, redo=False
873 // - Else if after filtering we have >= clo_depth IPs, redo=False
874 // - Else redo=True
875 // In other words, to redo, we'd have to get a stack trace as big as we
876 // asked for and remove more than 'overestimate' alloc-fns.
877
878 // Main loop.
879 redo = True; // Assume this to begin with.
880 for (overestimate = 3; redo; overestimate += 6) {
881 // This should never happen -- would require MAX_OVERESTIMATE
882 // alloc-fns to be removed from the stack trace.
883 if (overestimate > MAX_OVERESTIMATE)
884 VG_(tool_panic)("get_IPs: ips[] too small, inc. MAX_OVERESTIMATE?");
885
886 // Ask for more IPs than clo_depth suggests we need.
sewardj39f34232007-11-09 23:02:28 +0000887 n_ips = VG_(get_StackTrace)( tid, ips, clo_depth + overestimate,
sewardjb8b79ad2008-03-03 01:35:41 +0000888 NULL/*array to dump SP values in*/,
889 NULL/*array to dump FP values in*/,
890 0/*first_ip_delta*/ );
njn734b8052007-11-01 04:40:37 +0000891 tl_assert(n_ips > 0);
nethercotec9f36922004-02-14 16:40:02 +0000892
njn734b8052007-11-01 04:40:37 +0000893 // If the original stack trace is smaller than asked-for, redo=False.
894 if (n_ips < clo_depth + overestimate) { redo = False; }
nethercotec9f36922004-02-14 16:40:02 +0000895
njne323a6b2010-07-01 02:35:03 +0000896 // Filter out alloc fns. If requested, we automatically remove the
897 // first entry (which presumably will be something like malloc or
898 // __builtin_new that we're sure to filter out) without looking at it,
899 // because VG_(get_fnname) is expensive.
900 n_alloc_fns_removed = ( exclude_first_entry ? 1 : 0 );
njn734b8052007-11-01 04:40:37 +0000901 for (i = n_alloc_fns_removed; i < n_ips; i++) {
902 if (VG_(get_fnname)(ips[i], buf, BUF_LEN)) {
njnf6b00762009-04-17 04:26:41 +0000903 if (is_member_fn(alloc_fns, buf)) {
njn734b8052007-11-01 04:40:37 +0000904 n_alloc_fns_removed++;
905 } else {
906 break;
907 }
908 }
909 }
910 // Remove the alloc fns by shuffling the rest down over them.
911 n_ips -= n_alloc_fns_removed;
912 for (i = 0; i < n_ips; i++) {
913 ips[i] = ips[i + n_alloc_fns_removed];
914 }
nethercotec9f36922004-02-14 16:40:02 +0000915
njn734b8052007-11-01 04:40:37 +0000916 // If after filtering we have >= clo_depth IPs, redo=False
917 if (n_ips >= clo_depth) {
918 redo = False;
919 n_ips = clo_depth; // Ignore any IPs below --depth.
920 }
921
922 if (redo) {
923 n_XCon_redos++;
nethercotec9f36922004-02-14 16:40:02 +0000924 }
925 }
njn734b8052007-11-01 04:40:37 +0000926 return n_ips;
927}
nethercotec9f36922004-02-14 16:40:02 +0000928
njn734b8052007-11-01 04:40:37 +0000929// Gets an XCon and puts it in the tree. Returns the XCon's bottom-XPt.
njnf6b00762009-04-17 04:26:41 +0000930// Unless the allocation should be ignored, in which case we return NULL.
njne323a6b2010-07-01 02:35:03 +0000931static XPt* get_XCon( ThreadId tid, Bool exclude_first_entry )
njn734b8052007-11-01 04:40:37 +0000932{
njnf6b00762009-04-17 04:26:41 +0000933 static Addr ips[MAX_IPS];
njn734b8052007-11-01 04:40:37 +0000934 Int i;
935 XPt* xpt = alloc_xpt;
nethercotec9f36922004-02-14 16:40:02 +0000936
njn734b8052007-11-01 04:40:37 +0000937 // After this call, the IPs we want are in ips[0]..ips[n_ips-1].
njne323a6b2010-07-01 02:35:03 +0000938 Int n_ips = get_IPs(tid, exclude_first_entry, ips);
njn734b8052007-11-01 04:40:37 +0000939
njnf6b00762009-04-17 04:26:41 +0000940 // Should we ignore this allocation? (Nb: n_ips can be zero, eg. if
941 // 'main' is marked as an alloc-fn.)
942 if (n_ips > 0 && fn_should_be_ignored(ips[0])) {
943 return NULL;
944 }
945
njn734b8052007-11-01 04:40:37 +0000946 // Now do the search/insertion of the XCon.
947 for (i = 0; i < n_ips; i++) {
948 Addr ip = ips[i];
949 Int ch;
njnd01fef72005-03-25 23:35:48 +0000950 // Look for IP in xpt's children.
njn734b8052007-11-01 04:40:37 +0000951 // Linear search, ugh -- about 10% of time for konqueror startup tried
952 // caching last result, only hit about 4% for konqueror.
nethercotec9f36922004-02-14 16:40:02 +0000953 // Nb: this search hits about 98% of the time for konqueror
njn734b8052007-11-01 04:40:37 +0000954 for (ch = 0; True; ch++) {
955 if (ch == xpt->n_children) {
956 // IP not found in the children.
957 // Create and add new child XPt, then stop.
958 XPt* new_child_xpt = new_XPt(ip, xpt);
959 add_child_xpt(xpt, new_child_xpt);
960 xpt = new_child_xpt;
961 break;
nethercotec9f36922004-02-14 16:40:02 +0000962
njn734b8052007-11-01 04:40:37 +0000963 } else if (ip == xpt->children[ch]->ip) {
964 // Found the IP in the children, stop.
965 xpt = xpt->children[ch];
nethercotec9f36922004-02-14 16:40:02 +0000966 break;
967 }
nethercotec9f36922004-02-14 16:40:02 +0000968 }
nethercotec9f36922004-02-14 16:40:02 +0000969 }
njndbeb5352007-12-04 03:15:23 +0000970
971 // [Note: several comments refer to this comment. Do not delete it
972 // without updating them.]
973 //
974 // A complication... If all stack traces were well-formed, then the
975 // returned xpt would always be a bottom-XPt. As a consequence, an XPt's
976 // size would always be equal to the sum of its children's sizes, which
977 // is an excellent sanity check.
978 //
979 // Unfortunately, stack traces occasionally are malformed, ie. truncated.
980 // This allows a stack trace to be a sub-trace of another, eg. a/b/c is a
981 // sub-trace of a/b/c/d. So we can't assume this xpt is a bottom-XPt;
982 // nor can we do sanity check an XPt's size against its children's sizes.
983 // This is annoying, but must be dealt with. (Older versions of Massif
984 // had this assertion in, and it was reported to fail by real users a
985 // couple of times.) Even more annoyingly, I can't come up with a simple
986 // test case that exhibit such a malformed stack trace, so I can't
987 // regression test it. Sigh.
988 //
989 // However, we can print a warning, so that if it happens (unexpectedly)
990 // in existing regression tests we'll know. Also, it warns users that
991 // the output snapshots may not add up the way they might expect.
992 //
993 //tl_assert(0 == xpt->n_children); // Must be bottom-XPt
994 if (0 != xpt->n_children) {
995 static Int n_moans = 0;
996 if (n_moans < 3) {
sewardj5ea0f392009-07-15 14:51:34 +0000997 VG_(umsg)(
998 "Warning: Malformed stack trace detected. In Massif's output,\n");
999 VG_(umsg)(
1000 " the size of an entry's child entries may not sum up\n");
1001 VG_(umsg)(
1002 " to the entry's size as they normally do.\n");
njndbeb5352007-12-04 03:15:23 +00001003 n_moans++;
1004 if (3 == n_moans)
sewardj5ea0f392009-07-15 14:51:34 +00001005 VG_(umsg)(
1006 " (And Massif now won't warn about this again.)\n");
njndbeb5352007-12-04 03:15:23 +00001007 }
1008 }
njn734b8052007-11-01 04:40:37 +00001009 return xpt;
nethercotec9f36922004-02-14 16:40:02 +00001010}
1011
njn734b8052007-11-01 04:40:37 +00001012// Update 'szB' of every XPt in the XCon, by percolating upwards.
1013static void update_XCon(XPt* xpt, SSizeT space_delta)
nethercotec9f36922004-02-14 16:40:02 +00001014{
njne323a6b2010-07-01 02:35:03 +00001015 tl_assert(clo_heap);
njnca82cc02004-11-22 17:18:48 +00001016 tl_assert(NULL != xpt);
nethercotec9f36922004-02-14 16:40:02 +00001017
njn734b8052007-11-01 04:40:37 +00001018 if (0 == space_delta)
1019 return;
1020
nethercotec9f36922004-02-14 16:40:02 +00001021 while (xpt != alloc_xpt) {
njn734b8052007-11-01 04:40:37 +00001022 if (space_delta < 0) tl_assert(xpt->szB >= -space_delta);
1023 xpt->szB += space_delta;
nethercotec9f36922004-02-14 16:40:02 +00001024 xpt = xpt->parent;
njn734b8052007-11-01 04:40:37 +00001025 }
1026 if (space_delta < 0) tl_assert(alloc_xpt->szB >= -space_delta);
1027 alloc_xpt->szB += space_delta;
nethercotec9f36922004-02-14 16:40:02 +00001028}
1029
1030
njn734b8052007-11-01 04:40:37 +00001031//------------------------------------------------------------//
1032//--- Snapshots ---//
1033//------------------------------------------------------------//
1034
1035// Snapshots are done in a way so that we always have a reasonable number of
1036// them. We start by taking them quickly. Once we hit our limit, we cull
1037// some (eg. half), and start taking them more slowly. Once we hit the
1038// limit again, we again cull and then take them even more slowly, and so
1039// on.
1040
njn1a2741a2007-11-26 21:59:04 +00001041// Time is measured either in i or ms or bytes, depending on the --time-unit
njn734b8052007-11-01 04:40:37 +00001042// option. It's a Long because it can exceed 32-bits reasonably easily, and
1043// because we need to allow negative values to represent unset times.
1044typedef Long Time;
1045
1046#define UNUSED_SNAPSHOT_TIME -333 // A conspicuous negative number.
1047
1048typedef
1049 enum {
1050 Normal = 77,
1051 Peak,
1052 Unused
1053 }
1054 SnapshotKind;
nethercotec9f36922004-02-14 16:40:02 +00001055
1056typedef
1057 struct {
njn734b8052007-11-01 04:40:37 +00001058 SnapshotKind kind;
1059 Time time;
1060 SizeT heap_szB;
njn32397c02007-11-10 04:08:08 +00001061 SizeT heap_extra_szB;// Heap slop + admin bytes.
njn734b8052007-11-01 04:40:37 +00001062 SizeT stacks_szB;
1063 SXPt* alloc_sxpt; // Heap XTree root, if a detailed snapshot,
njn32397c02007-11-10 04:08:08 +00001064 } // otherwise NULL.
njn734b8052007-11-01 04:40:37 +00001065 Snapshot;
nethercotec9f36922004-02-14 16:40:02 +00001066
njn734b8052007-11-01 04:40:37 +00001067static UInt next_snapshot_i = 0; // Index of where next snapshot will go.
1068static Snapshot* snapshots; // Array of snapshots.
1069
1070static Bool is_snapshot_in_use(Snapshot* snapshot)
nethercotec9f36922004-02-14 16:40:02 +00001071{
njn734b8052007-11-01 04:40:37 +00001072 if (Unused == snapshot->kind) {
1073 // If snapshot is unused, check all the fields are unset.
1074 tl_assert(snapshot->time == UNUSED_SNAPSHOT_TIME);
njn32397c02007-11-10 04:08:08 +00001075 tl_assert(snapshot->heap_extra_szB == 0);
njn734b8052007-11-01 04:40:37 +00001076 tl_assert(snapshot->heap_szB == 0);
1077 tl_assert(snapshot->stacks_szB == 0);
1078 tl_assert(snapshot->alloc_sxpt == NULL);
1079 return False;
nethercotec9f36922004-02-14 16:40:02 +00001080 } else {
njn734b8052007-11-01 04:40:37 +00001081 tl_assert(snapshot->time != UNUSED_SNAPSHOT_TIME);
1082 return True;
nethercotec9f36922004-02-14 16:40:02 +00001083 }
1084}
1085
njn734b8052007-11-01 04:40:37 +00001086static Bool is_detailed_snapshot(Snapshot* snapshot)
nethercotec9f36922004-02-14 16:40:02 +00001087{
njn734b8052007-11-01 04:40:37 +00001088 return (snapshot->alloc_sxpt ? True : False);
nethercotec9f36922004-02-14 16:40:02 +00001089}
1090
njn734b8052007-11-01 04:40:37 +00001091static Bool is_uncullable_snapshot(Snapshot* snapshot)
nethercotec9f36922004-02-14 16:40:02 +00001092{
njn734b8052007-11-01 04:40:37 +00001093 return &snapshots[0] == snapshot // First snapshot
1094 || &snapshots[next_snapshot_i-1] == snapshot // Last snapshot
1095 || snapshot->kind == Peak; // Peak snapshot
nethercotec9f36922004-02-14 16:40:02 +00001096}
1097
njn734b8052007-11-01 04:40:37 +00001098static void sanity_check_snapshot(Snapshot* snapshot)
nethercotec9f36922004-02-14 16:40:02 +00001099{
njn734b8052007-11-01 04:40:37 +00001100 if (snapshot->alloc_sxpt) {
1101 sanity_check_SXTree(snapshot->alloc_sxpt);
1102 }
nethercotec9f36922004-02-14 16:40:02 +00001103}
1104
njn734b8052007-11-01 04:40:37 +00001105// All the used entries should look used, all the unused ones should be clear.
1106static void sanity_check_snapshots_array(void)
1107{
1108 Int i;
1109 for (i = 0; i < next_snapshot_i; i++) {
1110 tl_assert( is_snapshot_in_use( & snapshots[i] ));
1111 }
1112 for ( ; i < clo_max_snapshots; i++) {
1113 tl_assert(!is_snapshot_in_use( & snapshots[i] ));
1114 }
1115}
nethercotec9f36922004-02-14 16:40:02 +00001116
njn734b8052007-11-01 04:40:37 +00001117// This zeroes all the fields in the snapshot, but does not free the heap
1118// XTree if present. It also does a sanity check unless asked not to; we
1119// can't sanity check at startup when clearing the initial snapshots because
1120// they're full of junk.
1121static void clear_snapshot(Snapshot* snapshot, Bool do_sanity_check)
1122{
1123 if (do_sanity_check) sanity_check_snapshot(snapshot);
1124 snapshot->kind = Unused;
1125 snapshot->time = UNUSED_SNAPSHOT_TIME;
njn32397c02007-11-10 04:08:08 +00001126 snapshot->heap_extra_szB = 0;
njn734b8052007-11-01 04:40:37 +00001127 snapshot->heap_szB = 0;
1128 snapshot->stacks_szB = 0;
1129 snapshot->alloc_sxpt = NULL;
1130}
1131
1132// This zeroes all the fields in the snapshot, and frees the heap XTree if
1133// present.
1134static void delete_snapshot(Snapshot* snapshot)
1135{
1136 // Nb: if there's an XTree, we free it after calling clear_snapshot,
1137 // because clear_snapshot does a sanity check which includes checking the
1138 // XTree.
1139 SXPt* tmp_sxpt = snapshot->alloc_sxpt;
1140 clear_snapshot(snapshot, /*do_sanity_check*/True);
1141 if (tmp_sxpt) {
1142 free_SXTree(tmp_sxpt);
1143 }
1144}
1145
1146static void VERB_snapshot(Int verbosity, Char* prefix, Int i)
1147{
1148 Snapshot* snapshot = &snapshots[i];
1149 Char* suffix;
1150 switch (snapshot->kind) {
1151 case Peak: suffix = "p"; break;
1152 case Normal: suffix = ( is_detailed_snapshot(snapshot) ? "d" : "." ); break;
1153 case Unused: suffix = "u"; break;
1154 default:
1155 tl_assert2(0, "VERB_snapshot: unknown snapshot kind: %d", snapshot->kind);
1156 }
sewardj5ea0f392009-07-15 14:51:34 +00001157 VERB(verbosity, "%s S%s%3d (t:%lld, hp:%ld, ex:%ld, st:%ld)\n",
njn734b8052007-11-01 04:40:37 +00001158 prefix, suffix, i,
1159 snapshot->time,
1160 snapshot->heap_szB,
njn32397c02007-11-10 04:08:08 +00001161 snapshot->heap_extra_szB,
njn734b8052007-11-01 04:40:37 +00001162 snapshot->stacks_szB
1163 );
1164}
1165
1166// Cull half the snapshots; we choose those that represent the smallest
1167// time-spans, because that gives us the most even distribution of snapshots
1168// over time. (It's possible to lose interesting spikes, however.)
1169//
1170// Algorithm for N snapshots: We find the snapshot representing the smallest
1171// timeframe, and remove it. We repeat this until (N/2) snapshots are gone.
1172// We have to do this one snapshot at a time, rather than finding the (N/2)
1173// smallest snapshots in one hit, because when a snapshot is removed, its
1174// neighbours immediately cover greater timespans. So it's O(N^2), but N is
1175// small, and it's not done very often.
1176//
1177// Once we're done, we return the new smallest interval between snapshots.
1178// That becomes our minimum time interval.
1179static UInt cull_snapshots(void)
1180{
1181 Int i, jp, j, jn, min_timespan_i;
1182 Int n_deleted = 0;
1183 Time min_timespan;
1184
1185 n_cullings++;
1186
1187 // Sets j to the index of the first not-yet-removed snapshot at or after i
1188 #define FIND_SNAPSHOT(i, j) \
1189 for (j = i; \
1190 j < clo_max_snapshots && !is_snapshot_in_use(&snapshots[j]); \
1191 j++) { }
1192
sewardj5ea0f392009-07-15 14:51:34 +00001193 VERB(2, "Culling...\n");
njn734b8052007-11-01 04:40:37 +00001194
1195 // First we remove enough snapshots by clearing them in-place. Once
1196 // that's done, we can slide the remaining ones down.
1197 for (i = 0; i < clo_max_snapshots/2; i++) {
1198 // Find the snapshot representing the smallest timespan. The timespan
1199 // for snapshot n = d(N-1,N)+d(N,N+1), where d(A,B) is the time between
1200 // snapshot A and B. We don't consider the first and last snapshots for
1201 // removal.
1202 Snapshot* min_snapshot;
1203 Int min_j;
1204
1205 // Initial triple: (prev, curr, next) == (jp, j, jn)
1206 // Initial min_timespan is the first one.
1207 jp = 0;
1208 FIND_SNAPSHOT(1, j);
1209 FIND_SNAPSHOT(j+1, jn);
1210 min_timespan = 0x7fffffffffffffffLL;
1211 min_j = -1;
1212 while (jn < clo_max_snapshots) {
1213 Time timespan = snapshots[jn].time - snapshots[jp].time;
1214 tl_assert(timespan >= 0);
1215 // Nb: We never cull the peak snapshot.
1216 if (Peak != snapshots[j].kind && timespan < min_timespan) {
1217 min_timespan = timespan;
1218 min_j = j;
1219 }
1220 // Move on to next triple
1221 jp = j;
1222 j = jn;
1223 FIND_SNAPSHOT(jn+1, jn);
1224 }
1225 // We've found the least important snapshot, now delete it. First
1226 // print it if necessary.
1227 tl_assert(-1 != min_j); // Check we found a minimum.
1228 min_snapshot = & snapshots[ min_j ];
1229 if (VG_(clo_verbosity) > 1) {
1230 Char buf[64];
1231 VG_(snprintf)(buf, 64, " %3d (t-span = %lld)", i, min_timespan);
1232 VERB_snapshot(2, buf, min_j);
1233 }
1234 delete_snapshot(min_snapshot);
1235 n_deleted++;
1236 }
1237
1238 // Slide down the remaining snapshots over the removed ones. First set i
1239 // to point to the first empty slot, and j to the first full slot after
1240 // i. Then slide everything down.
1241 for (i = 0; is_snapshot_in_use( &snapshots[i] ); i++) { }
1242 for (j = i; !is_snapshot_in_use( &snapshots[j] ); j++) { }
1243 for ( ; j < clo_max_snapshots; j++) {
1244 if (is_snapshot_in_use( &snapshots[j] )) {
1245 snapshots[i++] = snapshots[j];
1246 clear_snapshot(&snapshots[j], /*do_sanity_check*/True);
1247 }
1248 }
1249 next_snapshot_i = i;
1250
1251 // Check snapshots array looks ok after changes.
1252 sanity_check_snapshots_array();
1253
1254 // Find the minimum timespan remaining; that will be our new minimum
1255 // time interval. Note that above we were finding timespans by measuring
1256 // two intervals around a snapshot that was under consideration for
1257 // deletion. Here we only measure single intervals because all the
1258 // deletions have occurred.
1259 //
1260 // But we have to be careful -- some snapshots (eg. snapshot 0, and the
1261 // peak snapshot) are uncullable. If two uncullable snapshots end up
1262 // next to each other, they'll never be culled (assuming the peak doesn't
1263 // change), and the time gap between them will not change. However, the
1264 // time between the remaining cullable snapshots will grow ever larger.
1265 // This means that the min_timespan found will always be that between the
1266 // two uncullable snapshots, and it will be much smaller than it should
1267 // be. To avoid this problem, when computing the minimum timespan, we
1268 // ignore any timespans between two uncullable snapshots.
1269 tl_assert(next_snapshot_i > 1);
1270 min_timespan = 0x7fffffffffffffffLL;
1271 min_timespan_i = -1;
1272 for (i = 1; i < next_snapshot_i; i++) {
1273 if (is_uncullable_snapshot(&snapshots[i]) &&
1274 is_uncullable_snapshot(&snapshots[i-1]))
1275 {
sewardj5ea0f392009-07-15 14:51:34 +00001276 VERB(2, "(Ignoring interval %d--%d when computing minimum)\n", i-1, i);
njn734b8052007-11-01 04:40:37 +00001277 } else {
1278 Time timespan = snapshots[i].time - snapshots[i-1].time;
1279 tl_assert(timespan >= 0);
1280 if (timespan < min_timespan) {
1281 min_timespan = timespan;
1282 min_timespan_i = i;
1283 }
1284 }
1285 }
1286 tl_assert(-1 != min_timespan_i); // Check we found a minimum.
1287
1288 // Print remaining snapshots, if necessary.
1289 if (VG_(clo_verbosity) > 1) {
sewardj5ea0f392009-07-15 14:51:34 +00001290 VERB(2, "Finished culling (%3d of %3d deleted)\n",
njn734b8052007-11-01 04:40:37 +00001291 n_deleted, clo_max_snapshots);
1292 for (i = 0; i < next_snapshot_i; i++) {
1293 VERB_snapshot(2, " post-cull", i);
1294 }
sewardj5ea0f392009-07-15 14:51:34 +00001295 VERB(2, "New time interval = %lld (between snapshots %d and %d)\n",
njn734b8052007-11-01 04:40:37 +00001296 min_timespan, min_timespan_i-1, min_timespan_i);
1297 }
1298
1299 return min_timespan;
1300}
1301
1302static Time get_time(void)
1303{
1304 // Get current time, in whatever time unit we're using.
njn1a2741a2007-11-26 21:59:04 +00001305 if (clo_time_unit == TimeI) {
1306 return guest_instrs_executed;
1307 } else if (clo_time_unit == TimeMS) {
njn734b8052007-11-01 04:40:37 +00001308 // Some stuff happens between the millisecond timer being initialised
1309 // to zero and us taking our first snapshot. We determine that time
1310 // gap so we can subtract it from all subsequent times so that our
1311 // first snapshot is considered to be at t = 0ms. Unfortunately, a
1312 // bunch of symbols get read after the first snapshot is taken but
1313 // before the second one (which is triggered by the first allocation),
1314 // so when the time-unit is 'ms' we always have a big gap between the
1315 // first two snapshots. But at least users won't have to wonder why
1316 // the first snapshot isn't at t=0.
1317 static Bool is_first_get_time = True;
1318 static Time start_time_ms;
1319 if (is_first_get_time) {
1320 start_time_ms = VG_(read_millisecond_timer)();
1321 is_first_get_time = False;
1322 return 0;
1323 } else {
1324 return VG_(read_millisecond_timer)() - start_time_ms;
1325 }
1326 } else if (clo_time_unit == TimeB) {
1327 return total_allocs_deallocs_szB;
1328 } else {
1329 tl_assert2(0, "bad --time-unit value");
1330 }
1331}
1332
1333// Take a snapshot, and only that -- decisions on whether to take a
1334// snapshot, or what kind of snapshot, are made elsewhere.
njnf76d27a2009-05-28 01:53:07 +00001335// Nb: we call the arg "my_time" because "time" shadows a global declaration
1336// in /usr/include/time.h on Darwin.
njn734b8052007-11-01 04:40:37 +00001337static void
njnf76d27a2009-05-28 01:53:07 +00001338take_snapshot(Snapshot* snapshot, SnapshotKind kind, Time my_time,
njnefc13c22009-02-23 06:44:51 +00001339 Bool is_detailed)
njn734b8052007-11-01 04:40:37 +00001340{
1341 tl_assert(!is_snapshot_in_use(snapshot));
njne323a6b2010-07-01 02:35:03 +00001342 if (!clo_pages_as_heap) {
1343 tl_assert(have_started_executing_code);
1344 }
njn734b8052007-11-01 04:40:37 +00001345
1346 // Heap and heap admin.
1347 if (clo_heap) {
1348 snapshot->heap_szB = heap_szB;
1349 if (is_detailed) {
njn32397c02007-11-10 04:08:08 +00001350 SizeT total_szB = heap_szB + heap_extra_szB + stacks_szB;
njn734b8052007-11-01 04:40:37 +00001351 snapshot->alloc_sxpt = dup_XTree(alloc_xpt, total_szB);
1352 tl_assert( alloc_xpt->szB == heap_szB);
1353 tl_assert(snapshot->alloc_sxpt->szB == heap_szB);
1354 }
njn32397c02007-11-10 04:08:08 +00001355 snapshot->heap_extra_szB = heap_extra_szB;
njn734b8052007-11-01 04:40:37 +00001356 }
1357
1358 // Stack(s).
1359 if (clo_stacks) {
1360 snapshot->stacks_szB = stacks_szB;
1361 }
1362
1363 // Rest of snapshot.
1364 snapshot->kind = kind;
njnf76d27a2009-05-28 01:53:07 +00001365 snapshot->time = my_time;
njn734b8052007-11-01 04:40:37 +00001366 sanity_check_snapshot(snapshot);
1367
1368 // Update stats.
1369 if (Peak == kind) n_peak_snapshots++;
1370 if (is_detailed) n_detailed_snapshots++;
1371 n_real_snapshots++;
1372}
1373
1374
1375// Take a snapshot, if it's time, or if we've hit a peak.
1376static void
1377maybe_take_snapshot(SnapshotKind kind, Char* what)
1378{
1379 // 'min_time_interval' is the minimum time interval between snapshots.
1380 // If we try to take a snapshot and less than this much time has passed,
1381 // we don't take it. It gets larger as the program runs longer. It's
1382 // initialised to zero so that we begin by taking snapshots as quickly as
1383 // possible.
1384 static Time min_time_interval = 0;
1385 // Zero allows startup snapshot.
1386 static Time earliest_possible_time_of_next_snapshot = 0;
1387 static Int n_snapshots_since_last_detailed = 0;
1388 static Int n_skipped_snapshots_since_last_snapshot = 0;
1389
1390 Snapshot* snapshot;
1391 Bool is_detailed;
njnf76d27a2009-05-28 01:53:07 +00001392 // Nb: we call this variable "my_time" because "time" shadows a global
1393 // declaration in /usr/include/time.h on Darwin.
1394 Time my_time = get_time();
njn734b8052007-11-01 04:40:37 +00001395
1396 switch (kind) {
1397 case Normal:
1398 // Only do a snapshot if it's time.
njnf76d27a2009-05-28 01:53:07 +00001399 if (my_time < earliest_possible_time_of_next_snapshot) {
njn734b8052007-11-01 04:40:37 +00001400 n_skipped_snapshots++;
1401 n_skipped_snapshots_since_last_snapshot++;
1402 return;
1403 }
1404 is_detailed = (clo_detailed_freq-1 == n_snapshots_since_last_detailed);
1405 break;
1406
1407 case Peak: {
1408 // Because we're about to do a deallocation, we're coming down from a
1409 // local peak. If it is (a) actually a global peak, and (b) a certain
1410 // amount bigger than the previous peak, then we take a peak snapshot.
1411 // By not taking a snapshot for every peak, we save a lot of effort --
1412 // because many peaks remain peak only for a short time.
njn32397c02007-11-10 04:08:08 +00001413 SizeT total_szB = heap_szB + heap_extra_szB + stacks_szB;
njn734b8052007-11-01 04:40:37 +00001414 SizeT excess_szB_for_new_peak =
njn62721e92007-11-11 22:15:58 +00001415 (SizeT)((peak_snapshot_total_szB * clo_peak_inaccuracy) / 100);
njn734b8052007-11-01 04:40:37 +00001416 if (total_szB <= peak_snapshot_total_szB + excess_szB_for_new_peak) {
1417 return;
1418 }
1419 is_detailed = True;
1420 break;
1421 }
1422
1423 default:
1424 tl_assert2(0, "maybe_take_snapshot: unrecognised snapshot kind");
1425 }
1426
1427 // Take the snapshot.
1428 snapshot = & snapshots[next_snapshot_i];
njnf76d27a2009-05-28 01:53:07 +00001429 take_snapshot(snapshot, kind, my_time, is_detailed);
njn734b8052007-11-01 04:40:37 +00001430
1431 // Record if it was detailed.
1432 if (is_detailed) {
1433 n_snapshots_since_last_detailed = 0;
1434 } else {
1435 n_snapshots_since_last_detailed++;
1436 }
1437
1438 // Update peak data, if it's a Peak snapshot.
1439 if (Peak == kind) {
1440 Int i, number_of_peaks_snapshots_found = 0;
1441
1442 // Sanity check the size, then update our recorded peak.
1443 SizeT snapshot_total_szB =
njn32397c02007-11-10 04:08:08 +00001444 snapshot->heap_szB + snapshot->heap_extra_szB + snapshot->stacks_szB;
njn734b8052007-11-01 04:40:37 +00001445 tl_assert2(snapshot_total_szB > peak_snapshot_total_szB,
1446 "%ld, %ld\n", snapshot_total_szB, peak_snapshot_total_szB);
1447 peak_snapshot_total_szB = snapshot_total_szB;
1448
1449 // Find the old peak snapshot, if it exists, and mark it as normal.
1450 for (i = 0; i < next_snapshot_i; i++) {
1451 if (Peak == snapshots[i].kind) {
1452 snapshots[i].kind = Normal;
1453 number_of_peaks_snapshots_found++;
1454 }
1455 }
1456 tl_assert(number_of_peaks_snapshots_found <= 1);
1457 }
1458
1459 // Finish up verbosity and stats stuff.
1460 if (n_skipped_snapshots_since_last_snapshot > 0) {
sewardj5ea0f392009-07-15 14:51:34 +00001461 VERB(2, " (skipped %d snapshot%s)\n",
njn734b8052007-11-01 04:40:37 +00001462 n_skipped_snapshots_since_last_snapshot,
njn84f32b22009-02-10 07:14:37 +00001463 ( 1 == n_skipped_snapshots_since_last_snapshot ? "" : "s") );
njn734b8052007-11-01 04:40:37 +00001464 }
1465 VERB_snapshot(2, what, next_snapshot_i);
1466 n_skipped_snapshots_since_last_snapshot = 0;
1467
1468 // Cull the entries, if our snapshot table is full.
1469 next_snapshot_i++;
1470 if (clo_max_snapshots == next_snapshot_i) {
1471 min_time_interval = cull_snapshots();
1472 }
1473
1474 // Work out the earliest time when the next snapshot can happen.
njnf76d27a2009-05-28 01:53:07 +00001475 earliest_possible_time_of_next_snapshot = my_time + min_time_interval;
njn734b8052007-11-01 04:40:37 +00001476}
1477
1478
1479//------------------------------------------------------------//
1480//--- Sanity checking ---//
1481//------------------------------------------------------------//
1482
1483static Bool ms_cheap_sanity_check ( void )
1484{
1485 return True; // Nothing useful we can cheaply check.
1486}
1487
1488static Bool ms_expensive_sanity_check ( void )
1489{
1490 sanity_check_XTree(alloc_xpt, /*parent*/NULL);
1491 sanity_check_snapshots_array();
1492 return True;
1493}
1494
1495
1496//------------------------------------------------------------//
1497//--- Heap management ---//
1498//------------------------------------------------------------//
1499
1500// Metadata for heap blocks. Each one contains a pointer to a bottom-XPt,
1501// which is a foothold into the XCon at which it was allocated. From
1502// HP_Chunks, XPt 'space' fields are incremented (at allocation) and
1503// decremented (at deallocation).
1504//
1505// Nb: first two fields must match core's VgHashNode.
1506typedef
1507 struct _HP_Chunk {
1508 struct _HP_Chunk* next;
njn32397c02007-11-10 04:08:08 +00001509 Addr data; // Ptr to actual block
1510 SizeT req_szB; // Size requested
1511 SizeT slop_szB; // Extra bytes given above those requested
1512 XPt* where; // Where allocated; bottom-XPt
njn734b8052007-11-01 04:40:37 +00001513 }
1514 HP_Chunk;
1515
1516static VgHashTable malloc_list = NULL; // HP_Chunks
1517
1518static void update_alloc_stats(SSizeT szB_delta)
1519{
1520 // Update total_allocs_deallocs_szB.
1521 if (szB_delta < 0) szB_delta = -szB_delta;
1522 total_allocs_deallocs_szB += szB_delta;
1523}
1524
njn32397c02007-11-10 04:08:08 +00001525static void update_heap_stats(SSizeT heap_szB_delta, Int heap_extra_szB_delta)
njn734b8052007-11-01 04:40:37 +00001526{
njn32397c02007-11-10 04:08:08 +00001527 if (heap_szB_delta < 0)
1528 tl_assert(heap_szB >= -heap_szB_delta);
1529 if (heap_extra_szB_delta < 0)
1530 tl_assert(heap_extra_szB >= -heap_extra_szB_delta);
njn734b8052007-11-01 04:40:37 +00001531
njn32397c02007-11-10 04:08:08 +00001532 heap_extra_szB += heap_extra_szB_delta;
1533 heap_szB += heap_szB_delta;
1534
1535 update_alloc_stats(heap_szB_delta + heap_extra_szB_delta);
njn734b8052007-11-01 04:40:37 +00001536}
nethercotec9f36922004-02-14 16:40:02 +00001537
nethercote159dfef2004-09-13 13:27:30 +00001538static
njne323a6b2010-07-01 02:35:03 +00001539void* record_block( ThreadId tid, void* p, SizeT req_szB, SizeT slop_szB,
1540 Bool exclude_first_entry, Bool maybe_snapshot )
nethercotec9f36922004-02-14 16:40:02 +00001541{
njnf1c5def2005-08-11 02:17:07 +00001542 // Make new HP_Chunk node, add to malloc_list
njne323a6b2010-07-01 02:35:03 +00001543 HP_Chunk* hc = VG_(malloc)("ms.main.rb.1", sizeof(HP_Chunk));
njn32397c02007-11-10 04:08:08 +00001544 hc->req_szB = req_szB;
1545 hc->slop_szB = slop_szB;
1546 hc->data = (Addr)p;
1547 hc->where = NULL;
njn246a9d22005-08-14 06:24:20 +00001548 VG_(HT_add_node)(malloc_list, hc);
nethercote57e36b32004-07-10 14:56:28 +00001549
njn734b8052007-11-01 04:40:37 +00001550 if (clo_heap) {
njne323a6b2010-07-01 02:35:03 +00001551 VERB(3, "<<< record_block (%lu, %lu)\n", req_szB, slop_szB);
njn734b8052007-11-01 04:40:37 +00001552
njne323a6b2010-07-01 02:35:03 +00001553 hc->where = get_XCon( tid, exclude_first_entry );
njn734b8052007-11-01 04:40:37 +00001554
njnf6b00762009-04-17 04:26:41 +00001555 if (hc->where) {
1556 // Update statistics.
1557 n_heap_allocs++;
1558
1559 // Update heap stats.
1560 update_heap_stats(req_szB, clo_heap_admin + slop_szB);
1561
1562 // Update XTree.
1563 update_XCon(hc->where, req_szB);
1564
1565 // Maybe take a snapshot.
njne323a6b2010-07-01 02:35:03 +00001566 if (maybe_snapshot) {
1567 maybe_take_snapshot(Normal, " alloc");
1568 }
njnf6b00762009-04-17 04:26:41 +00001569
1570 } else {
1571 // Ignored allocation.
1572 n_ignored_heap_allocs++;
1573
sewardj5ea0f392009-07-15 14:51:34 +00001574 VERB(3, "(ignored)\n");
njnf6b00762009-04-17 04:26:41 +00001575 }
njn734b8052007-11-01 04:40:37 +00001576
sewardj5ea0f392009-07-15 14:51:34 +00001577 VERB(3, ">>>\n");
njn734b8052007-11-01 04:40:37 +00001578 }
nethercotec9f36922004-02-14 16:40:02 +00001579
nethercotec9f36922004-02-14 16:40:02 +00001580 return p;
1581}
1582
1583static __inline__
njne323a6b2010-07-01 02:35:03 +00001584void* alloc_and_record_block ( ThreadId tid, SizeT req_szB, SizeT req_alignB,
1585 Bool is_zeroed )
1586{
1587 SizeT actual_szB, slop_szB;
1588 void* p;
1589
1590 if ((SSizeT)req_szB < 0) return NULL;
1591
1592 // Allocate and zero if necessary.
1593 p = VG_(cli_malloc)( req_alignB, req_szB );
1594 if (!p) {
1595 return NULL;
1596 }
1597 if (is_zeroed) VG_(memset)(p, 0, req_szB);
1598 actual_szB = VG_(malloc_usable_size)(p);
1599 tl_assert(actual_szB >= req_szB);
1600 slop_szB = actual_szB - req_szB;
1601
1602 // Record block.
1603 record_block(tid, p, req_szB, slop_szB, /*exclude_first_entry*/True,
1604 /*maybe_snapshot*/True);
1605
1606 return p;
1607}
1608
1609static __inline__
1610void unrecord_block ( void* p, Bool maybe_snapshot )
nethercotec9f36922004-02-14 16:40:02 +00001611{
njnf1c5def2005-08-11 02:17:07 +00001612 // Remove HP_Chunk from malloc_list
njn7081c1a2009-05-19 01:53:30 +00001613 HP_Chunk* hc = VG_(HT_remove)(malloc_list, (UWord)p);
njn734b8052007-11-01 04:40:37 +00001614 if (NULL == hc) {
njn5cc5d7e2005-08-11 02:09:25 +00001615 return; // must have been a bogus free()
njn734b8052007-11-01 04:40:37 +00001616 }
nethercotec9f36922004-02-14 16:40:02 +00001617
njn734b8052007-11-01 04:40:37 +00001618 if (clo_heap) {
njne323a6b2010-07-01 02:35:03 +00001619 VERB(3, "<<< unrecord_block\n");
nethercotec9f36922004-02-14 16:40:02 +00001620
njnf6b00762009-04-17 04:26:41 +00001621 if (hc->where) {
1622 // Update statistics.
1623 n_heap_frees++;
nethercote57e36b32004-07-10 14:56:28 +00001624
njnf6b00762009-04-17 04:26:41 +00001625 // Maybe take a peak snapshot, since it's a deallocation.
njne323a6b2010-07-01 02:35:03 +00001626 if (maybe_snapshot) {
1627 maybe_take_snapshot(Peak, "de-PEAK");
1628 }
njn734b8052007-11-01 04:40:37 +00001629
njnf6b00762009-04-17 04:26:41 +00001630 // Update heap stats.
1631 update_heap_stats(-hc->req_szB, -clo_heap_admin - hc->slop_szB);
njn734b8052007-11-01 04:40:37 +00001632
njnf6b00762009-04-17 04:26:41 +00001633 // Update XTree.
1634 update_XCon(hc->where, -hc->req_szB);
njn734b8052007-11-01 04:40:37 +00001635
njnf6b00762009-04-17 04:26:41 +00001636 // Maybe take a snapshot.
njne323a6b2010-07-01 02:35:03 +00001637 if (maybe_snapshot) {
1638 maybe_take_snapshot(Normal, "dealloc");
1639 }
njnf6b00762009-04-17 04:26:41 +00001640
1641 } else {
1642 n_ignored_heap_frees++;
1643
sewardj5ea0f392009-07-15 14:51:34 +00001644 VERB(3, "(ignored)\n");
njnf6b00762009-04-17 04:26:41 +00001645 }
njn734b8052007-11-01 04:40:37 +00001646
sewardj5ea0f392009-07-15 14:51:34 +00001647 VERB(3, ">>> (-%lu, -%lu)\n", hc->req_szB, hc->slop_szB);
njn734b8052007-11-01 04:40:37 +00001648 }
1649
1650 // Actually free the chunk, and the heap block (if necessary)
1651 VG_(free)( hc ); hc = NULL;
nethercotec9f36922004-02-14 16:40:02 +00001652}
1653
njnf6b00762009-04-17 04:26:41 +00001654// Nb: --ignore-fn is tricky for realloc. If the block's original alloc was
1655// ignored, but the realloc is not requested to be ignored, and we are
1656// shrinking the block, then we have to ignore the realloc -- otherwise we
1657// could end up with negative heap sizes. This isn't a danger if we are
1658// growing such a block, but for consistency (it also simplifies things) we
1659// ignore such reallocs as well.
njn734b8052007-11-01 04:40:37 +00001660static __inline__
njne323a6b2010-07-01 02:35:03 +00001661void* realloc_block ( ThreadId tid, void* p_old, SizeT new_req_szB )
nethercotec9f36922004-02-14 16:40:02 +00001662{
njn734b8052007-11-01 04:40:37 +00001663 HP_Chunk* hc;
1664 void* p_new;
njn32397c02007-11-10 04:08:08 +00001665 SizeT old_req_szB, old_slop_szB, new_slop_szB, new_actual_szB;
njn734b8052007-11-01 04:40:37 +00001666 XPt *old_where, *new_where;
njnf6b00762009-04-17 04:26:41 +00001667 Bool is_ignored = False;
njn734b8052007-11-01 04:40:37 +00001668
1669 // Remove the old block
1670 hc = VG_(HT_remove)(malloc_list, (UWord)p_old);
1671 if (hc == NULL) {
1672 return NULL; // must have been a bogus realloc()
1673 }
1674
njn32397c02007-11-10 04:08:08 +00001675 old_req_szB = hc->req_szB;
1676 old_slop_szB = hc->slop_szB;
njn734b8052007-11-01 04:40:37 +00001677
njne323a6b2010-07-01 02:35:03 +00001678 tl_assert(!clo_pages_as_heap); // Shouldn't be here if --pages-as-heap=yes.
njn734b8052007-11-01 04:40:37 +00001679 if (clo_heap) {
njne323a6b2010-07-01 02:35:03 +00001680 VERB(3, "<<< realloc_block (%lu)\n", new_req_szB);
njn734b8052007-11-01 04:40:37 +00001681
njnf6b00762009-04-17 04:26:41 +00001682 if (hc->where) {
1683 // Update statistics.
1684 n_heap_reallocs++;
njn734b8052007-11-01 04:40:37 +00001685
njnf6b00762009-04-17 04:26:41 +00001686 // Maybe take a peak snapshot, if it's (effectively) a deallocation.
1687 if (new_req_szB < old_req_szB) {
1688 maybe_take_snapshot(Peak, "re-PEAK");
1689 }
1690 } else {
1691 // The original malloc was ignored, so we have to ignore the
1692 // realloc as well.
1693 is_ignored = True;
njn734b8052007-11-01 04:40:37 +00001694 }
njn734b8052007-11-01 04:40:37 +00001695 }
1696
1697 // Actually do the allocation, if necessary.
njn32397c02007-11-10 04:08:08 +00001698 if (new_req_szB <= old_req_szB + old_slop_szB) {
1699 // New size is smaller or same; block not moved.
njn734b8052007-11-01 04:40:37 +00001700 p_new = p_old;
njn32397c02007-11-10 04:08:08 +00001701 new_slop_szB = old_slop_szB + (old_req_szB - new_req_szB);
njn734b8052007-11-01 04:40:37 +00001702
1703 } else {
njn32397c02007-11-10 04:08:08 +00001704 // New size is bigger; make new block, copy shared contents, free old.
1705 p_new = VG_(cli_malloc)(VG_(clo_alignment), new_req_szB);
1706 if (!p_new) {
1707 // Nb: if realloc fails, NULL is returned but the old block is not
1708 // touched. What an awful function.
1709 return NULL;
njn734b8052007-11-01 04:40:37 +00001710 }
njndfa408c2011-08-08 01:58:50 +00001711 VG_(memcpy)(p_new, p_old, old_req_szB + old_slop_szB);
njn32397c02007-11-10 04:08:08 +00001712 VG_(cli_free)(p_old);
1713 new_actual_szB = VG_(malloc_usable_size)(p_new);
1714 tl_assert(new_actual_szB >= new_req_szB);
1715 new_slop_szB = new_actual_szB - new_req_szB;
njn734b8052007-11-01 04:40:37 +00001716 }
1717
1718 if (p_new) {
1719 // Update HP_Chunk.
njn32397c02007-11-10 04:08:08 +00001720 hc->data = (Addr)p_new;
1721 hc->req_szB = new_req_szB;
1722 hc->slop_szB = new_slop_szB;
1723 old_where = hc->where;
1724 hc->where = NULL;
njn734b8052007-11-01 04:40:37 +00001725
1726 // Update XTree.
1727 if (clo_heap) {
njne323a6b2010-07-01 02:35:03 +00001728 new_where = get_XCon( tid, /*exclude_first_entry*/True);
njnf6b00762009-04-17 04:26:41 +00001729 if (!is_ignored && new_where) {
1730 hc->where = new_where;
1731 update_XCon(old_where, -old_req_szB);
1732 update_XCon(new_where, new_req_szB);
1733 } else {
1734 // The realloc itself is ignored.
1735 is_ignored = True;
1736
1737 // Update statistics.
1738 n_ignored_heap_reallocs++;
1739 }
njn734b8052007-11-01 04:40:37 +00001740 }
1741 }
1742
1743 // Now insert the new hc (with a possibly new 'data' field) into
1744 // malloc_list. If this realloc() did not increase the memory size, we
1745 // will have removed and then re-added hc unnecessarily. But that's ok
1746 // because shrinking a block with realloc() is (presumably) much rarer
1747 // than growing it, and this way simplifies the growing case.
1748 VG_(HT_add_node)(malloc_list, hc);
1749
njn734b8052007-11-01 04:40:37 +00001750 if (clo_heap) {
njnf6b00762009-04-17 04:26:41 +00001751 if (!is_ignored) {
1752 // Update heap stats.
1753 update_heap_stats(new_req_szB - old_req_szB,
1754 new_slop_szB - old_slop_szB);
njn32397c02007-11-10 04:08:08 +00001755
njnf6b00762009-04-17 04:26:41 +00001756 // Maybe take a snapshot.
1757 maybe_take_snapshot(Normal, "realloc");
1758 } else {
1759
sewardj5ea0f392009-07-15 14:51:34 +00001760 VERB(3, "(ignored)\n");
njnf6b00762009-04-17 04:26:41 +00001761 }
njn734b8052007-11-01 04:40:37 +00001762
sewardj5ea0f392009-07-15 14:51:34 +00001763 VERB(3, ">>> (%ld, %ld)\n",
njn32397c02007-11-10 04:08:08 +00001764 new_req_szB - old_req_szB, new_slop_szB - old_slop_szB);
njn734b8052007-11-01 04:40:37 +00001765 }
1766
1767 return p_new;
nethercotec9f36922004-02-14 16:40:02 +00001768}
1769
njn734b8052007-11-01 04:40:37 +00001770
1771//------------------------------------------------------------//
1772//--- malloc() et al replacement wrappers ---//
1773//------------------------------------------------------------//
1774
1775static void* ms_malloc ( ThreadId tid, SizeT szB )
nethercotec9f36922004-02-14 16:40:02 +00001776{
njne323a6b2010-07-01 02:35:03 +00001777 return alloc_and_record_block( tid, szB, VG_(clo_alignment), /*is_zeroed*/False );
nethercotec9f36922004-02-14 16:40:02 +00001778}
1779
njn734b8052007-11-01 04:40:37 +00001780static void* ms___builtin_new ( ThreadId tid, SizeT szB )
nethercotec9f36922004-02-14 16:40:02 +00001781{
njne323a6b2010-07-01 02:35:03 +00001782 return alloc_and_record_block( tid, szB, VG_(clo_alignment), /*is_zeroed*/False );
nethercotec9f36922004-02-14 16:40:02 +00001783}
1784
njn734b8052007-11-01 04:40:37 +00001785static void* ms___builtin_vec_new ( ThreadId tid, SizeT szB )
fitzhardinge51f3ff12004-03-04 22:42:03 +00001786{
njne323a6b2010-07-01 02:35:03 +00001787 return alloc_and_record_block( tid, szB, VG_(clo_alignment), /*is_zeroed*/False );
njn734b8052007-11-01 04:40:37 +00001788}
1789
1790static void* ms_calloc ( ThreadId tid, SizeT m, SizeT szB )
1791{
njne323a6b2010-07-01 02:35:03 +00001792 return alloc_and_record_block( tid, m*szB, VG_(clo_alignment), /*is_zeroed*/True );
njn734b8052007-11-01 04:40:37 +00001793}
1794
1795static void *ms_memalign ( ThreadId tid, SizeT alignB, SizeT szB )
1796{
njne323a6b2010-07-01 02:35:03 +00001797 return alloc_and_record_block( tid, szB, alignB, False );
fitzhardinge51f3ff12004-03-04 22:42:03 +00001798}
1799
njnefc13c22009-02-23 06:44:51 +00001800static void ms_free ( ThreadId tid __attribute__((unused)), void* p )
nethercotec9f36922004-02-14 16:40:02 +00001801{
njne323a6b2010-07-01 02:35:03 +00001802 unrecord_block(p, /*maybe_snapshot*/True);
1803 VG_(cli_free)(p);
nethercotec9f36922004-02-14 16:40:02 +00001804}
1805
njn51d827b2005-05-09 01:02:08 +00001806static void ms___builtin_delete ( ThreadId tid, void* p )
nethercotec9f36922004-02-14 16:40:02 +00001807{
njne323a6b2010-07-01 02:35:03 +00001808 unrecord_block(p, /*maybe_snapshot*/True);
1809 VG_(cli_free)(p);
nethercotec9f36922004-02-14 16:40:02 +00001810}
1811
njn51d827b2005-05-09 01:02:08 +00001812static void ms___builtin_vec_delete ( ThreadId tid, void* p )
nethercotec9f36922004-02-14 16:40:02 +00001813{
njne323a6b2010-07-01 02:35:03 +00001814 unrecord_block(p, /*maybe_snapshot*/True);
1815 VG_(cli_free)(p);
nethercotec9f36922004-02-14 16:40:02 +00001816}
1817
njn734b8052007-11-01 04:40:37 +00001818static void* ms_realloc ( ThreadId tid, void* p_old, SizeT new_szB )
nethercotec9f36922004-02-14 16:40:02 +00001819{
njne323a6b2010-07-01 02:35:03 +00001820 return realloc_block(tid, p_old, new_szB);
nethercotec9f36922004-02-14 16:40:02 +00001821}
1822
njnefc13c22009-02-23 06:44:51 +00001823static SizeT ms_malloc_usable_size ( ThreadId tid, void* p )
njn8b140de2009-02-17 04:31:18 +00001824{
1825 HP_Chunk* hc = VG_(HT_lookup)( malloc_list, (UWord)p );
1826
1827 return ( hc ? hc->req_szB + hc->slop_szB : 0 );
1828}
nethercotec9f36922004-02-14 16:40:02 +00001829
njn734b8052007-11-01 04:40:37 +00001830//------------------------------------------------------------//
njne323a6b2010-07-01 02:35:03 +00001831//--- Page handling ---//
1832//------------------------------------------------------------//
1833
1834static
1835void ms_record_page_mem ( Addr a, SizeT len )
1836{
1837 ThreadId tid = VG_(get_running_tid)();
1838 Addr end;
1839 tl_assert(VG_IS_PAGE_ALIGNED(len));
1840 tl_assert(len >= VKI_PAGE_SIZE);
1841 // Record the first N-1 pages as blocks, but don't do any snapshots.
1842 for (end = a + len - VKI_PAGE_SIZE; a < end; a += VKI_PAGE_SIZE) {
1843 record_block( tid, (void*)a, VKI_PAGE_SIZE, /*slop_szB*/0,
1844 /*exclude_first_entry*/False, /*maybe_snapshot*/False );
1845 }
1846 // Record the last page as a block, and maybe do a snapshot afterwards.
1847 record_block( tid, (void*)a, VKI_PAGE_SIZE, /*slop_szB*/0,
1848 /*exclude_first_entry*/False, /*maybe_snapshot*/True );
1849}
1850
1851static
1852void ms_unrecord_page_mem( Addr a, SizeT len )
1853{
1854 Addr end;
1855 tl_assert(VG_IS_PAGE_ALIGNED(len));
1856 tl_assert(len >= VKI_PAGE_SIZE);
1857 for (end = a + len - VKI_PAGE_SIZE; a < end; a += VKI_PAGE_SIZE) {
1858 unrecord_block((void*)a, /*maybe_snapshot*/False);
1859 }
1860 unrecord_block((void*)a, /*maybe_snapshot*/True);
1861}
1862
1863//------------------------------------------------------------//
1864
1865static
1866void ms_new_mem_mmap ( Addr a, SizeT len,
1867 Bool rr, Bool ww, Bool xx, ULong di_handle )
1868{
1869 tl_assert(VG_IS_PAGE_ALIGNED(len));
1870 ms_record_page_mem(a, len);
1871}
1872
1873static
1874void ms_new_mem_startup( Addr a, SizeT len,
1875 Bool rr, Bool ww, Bool xx, ULong di_handle )
1876{
1877 // startup maps are always be page-sized, except the trampoline page is
1878 // marked by the core as only being the size of the trampoline itself,
1879 // which is something like 57 bytes. Round it up to page size.
1880 len = VG_PGROUNDUP(len);
1881 ms_record_page_mem(a, len);
1882}
1883
1884static
1885void ms_new_mem_brk ( Addr a, SizeT len, ThreadId tid )
1886{
philippee6a26cc2012-05-01 20:02:30 +00001887 // brk limit is not necessarily aligned on a page boundary.
1888 // If new memory being brk-ed implies to allocate a new page,
1889 // then call ms_record_page_mem with page aligned parameters
1890 // otherwise just ignore.
1891 Addr old_bottom_page = VG_PGROUNDDN(a - 1);
1892 Addr new_top_page = VG_PGROUNDDN(a + len - 1);
1893 if (old_bottom_page != new_top_page)
1894 ms_record_page_mem(VG_PGROUNDDN(a),
1895 (new_top_page - old_bottom_page));
njne323a6b2010-07-01 02:35:03 +00001896}
1897
1898static
1899void ms_copy_mem_remap( Addr from, Addr to, SizeT len)
1900{
1901 tl_assert(VG_IS_PAGE_ALIGNED(len));
1902 ms_unrecord_page_mem(from, len);
1903 ms_record_page_mem(to, len);
1904}
1905
1906static
1907void ms_die_mem_munmap( Addr a, SizeT len )
1908{
1909 tl_assert(VG_IS_PAGE_ALIGNED(len));
1910 ms_unrecord_page_mem(a, len);
1911}
1912
1913static
1914void ms_die_mem_brk( Addr a, SizeT len )
1915{
philippee6a26cc2012-05-01 20:02:30 +00001916 // Call ms_unrecord_page_mem only if one or more pages are de-allocated.
1917 // See ms_new_mem_brk for more details.
1918 Addr new_bottom_page = VG_PGROUNDDN(a - 1);
1919 Addr old_top_page = VG_PGROUNDDN(a + len - 1);
1920 if (old_top_page != new_bottom_page)
1921 ms_unrecord_page_mem(VG_PGROUNDDN(a),
1922 (old_top_page - new_bottom_page));
1923
njne323a6b2010-07-01 02:35:03 +00001924}
1925
1926//------------------------------------------------------------//
njn734b8052007-11-01 04:40:37 +00001927//--- Stacks ---//
1928//------------------------------------------------------------//
nethercotec9f36922004-02-14 16:40:02 +00001929
njn734b8052007-11-01 04:40:37 +00001930// We really want the inlining to occur...
1931#define INLINE inline __attribute__((always_inline))
nethercotec9f36922004-02-14 16:40:02 +00001932
njn734b8052007-11-01 04:40:37 +00001933static void update_stack_stats(SSizeT stack_szB_delta)
nethercotec9f36922004-02-14 16:40:02 +00001934{
njn734b8052007-11-01 04:40:37 +00001935 if (stack_szB_delta < 0) tl_assert(stacks_szB >= -stack_szB_delta);
1936 stacks_szB += stack_szB_delta;
nethercotec9f36922004-02-14 16:40:02 +00001937
njn734b8052007-11-01 04:40:37 +00001938 update_alloc_stats(stack_szB_delta);
nethercotec9f36922004-02-14 16:40:02 +00001939}
1940
njnefc13c22009-02-23 06:44:51 +00001941static INLINE void new_mem_stack_2(SizeT len, Char* what)
nethercotec9f36922004-02-14 16:40:02 +00001942{
njn734b8052007-11-01 04:40:37 +00001943 if (have_started_executing_code) {
sewardj5ea0f392009-07-15 14:51:34 +00001944 VERB(3, "<<< new_mem_stack (%ld)\n", len);
njn734b8052007-11-01 04:40:37 +00001945 n_stack_allocs++;
1946 update_stack_stats(len);
1947 maybe_take_snapshot(Normal, what);
sewardj5ea0f392009-07-15 14:51:34 +00001948 VERB(3, ">>>\n");
nethercotec9f36922004-02-14 16:40:02 +00001949 }
nethercotec9f36922004-02-14 16:40:02 +00001950}
1951
njnefc13c22009-02-23 06:44:51 +00001952static INLINE void die_mem_stack_2(SizeT len, Char* what)
nethercotec9f36922004-02-14 16:40:02 +00001953{
njn734b8052007-11-01 04:40:37 +00001954 if (have_started_executing_code) {
sewardj5ea0f392009-07-15 14:51:34 +00001955 VERB(3, "<<< die_mem_stack (%ld)\n", -len);
njn734b8052007-11-01 04:40:37 +00001956 n_stack_frees++;
1957 maybe_take_snapshot(Peak, "stkPEAK");
1958 update_stack_stats(-len);
1959 maybe_take_snapshot(Normal, what);
sewardj5ea0f392009-07-15 14:51:34 +00001960 VERB(3, ">>>\n");
nethercotec9f36922004-02-14 16:40:02 +00001961 }
nethercotec9f36922004-02-14 16:40:02 +00001962}
1963
njn734b8052007-11-01 04:40:37 +00001964static void new_mem_stack(Addr a, SizeT len)
nethercotec9f36922004-02-14 16:40:02 +00001965{
njnefc13c22009-02-23 06:44:51 +00001966 new_mem_stack_2(len, "stk-new");
njn734b8052007-11-01 04:40:37 +00001967}
nethercotec9f36922004-02-14 16:40:02 +00001968
njn734b8052007-11-01 04:40:37 +00001969static void die_mem_stack(Addr a, SizeT len)
1970{
njnefc13c22009-02-23 06:44:51 +00001971 die_mem_stack_2(len, "stk-die");
njn734b8052007-11-01 04:40:37 +00001972}
nethercotec9f36922004-02-14 16:40:02 +00001973
sewardj7cf4e6b2008-05-01 20:24:26 +00001974static void new_mem_stack_signal(Addr a, SizeT len, ThreadId tid)
nethercotec9f36922004-02-14 16:40:02 +00001975{
njnefc13c22009-02-23 06:44:51 +00001976 new_mem_stack_2(len, "sig-new");
nethercotec9f36922004-02-14 16:40:02 +00001977}
1978
nethercote8b5f40c2004-11-02 13:29:50 +00001979static void die_mem_stack_signal(Addr a, SizeT len)
nethercotec9f36922004-02-14 16:40:02 +00001980{
njnefc13c22009-02-23 06:44:51 +00001981 die_mem_stack_2(len, "sig-die");
nethercotec9f36922004-02-14 16:40:02 +00001982}
1983
njn734b8052007-11-01 04:40:37 +00001984
1985//------------------------------------------------------------//
1986//--- Client Requests ---//
1987//------------------------------------------------------------//
nethercotec9f36922004-02-14 16:40:02 +00001988
sewardj3b290482011-05-06 21:02:55 +00001989static void print_monitor_help ( void )
1990{
1991 VG_(gdb_printf) ("\n");
1992 VG_(gdb_printf) ("massif monitor commands:\n");
sewardj30b3eca2011-06-28 08:20:39 +00001993 VG_(gdb_printf) (" snapshot [<filename>]\n");
1994 VG_(gdb_printf) (" detailed_snapshot [<filename>]\n");
sewardjd142f992011-05-17 17:15:07 +00001995 VG_(gdb_printf) (" takes a snapshot (or a detailed snapshot)\n");
1996 VG_(gdb_printf) (" and saves it in <filename>\n");
sewardj3b290482011-05-06 21:02:55 +00001997 VG_(gdb_printf) (" default <filename> is massif.vgdb.out\n");
sewardj3b290482011-05-06 21:02:55 +00001998 VG_(gdb_printf) ("\n");
1999}
2000
2001
2002/* Forward declaration.
2003 return True if request recognised, False otherwise */
2004static Bool handle_gdb_monitor_command (ThreadId tid, Char *req);
njn51d827b2005-05-09 01:02:08 +00002005static Bool ms_handle_client_request ( ThreadId tid, UWord* argv, UWord* ret )
nethercotec9f36922004-02-14 16:40:02 +00002006{
2007 switch (argv[0]) {
2008 case VG_USERREQ__MALLOCLIKE_BLOCK: {
njn734b8052007-11-01 04:40:37 +00002009 void* p = (void*)argv[1];
2010 SizeT szB = argv[2];
njne323a6b2010-07-01 02:35:03 +00002011 record_block( tid, p, szB, /*slop_szB*/0, /*exclude_first_entry*/False,
2012 /*maybe_snapshot*/True );
njn734b8052007-11-01 04:40:37 +00002013 *ret = 0;
nethercotec9f36922004-02-14 16:40:02 +00002014 return True;
2015 }
bart91347382011-03-25 20:07:25 +00002016 case VG_USERREQ__RESIZEINPLACE_BLOCK: {
2017 void* p = (void*)argv[1];
2018 SizeT newSizeB = argv[3];
2019
2020 unrecord_block(p, /*maybe_snapshot*/True);
2021 record_block(tid, p, newSizeB, /*slop_szB*/0,
2022 /*exclude_first_entry*/False, /*maybe_snapshot*/True);
2023 return True;
2024 }
nethercotec9f36922004-02-14 16:40:02 +00002025 case VG_USERREQ__FREELIKE_BLOCK: {
njn734b8052007-11-01 04:40:37 +00002026 void* p = (void*)argv[1];
njne323a6b2010-07-01 02:35:03 +00002027 unrecord_block(p, /*maybe_snapshot*/True);
njn734b8052007-11-01 04:40:37 +00002028 *ret = 0;
nethercotec9f36922004-02-14 16:40:02 +00002029 return True;
2030 }
sewardj3b290482011-05-06 21:02:55 +00002031 case VG_USERREQ__GDB_MONITOR_COMMAND: {
2032 Bool handled = handle_gdb_monitor_command (tid, (Char*)argv[1]);
2033 if (handled)
2034 *ret = 1;
2035 else
2036 *ret = 0;
2037 return handled;
2038 }
2039
nethercotec9f36922004-02-14 16:40:02 +00002040 default:
2041 *ret = 0;
2042 return False;
2043 }
2044}
2045
njn734b8052007-11-01 04:40:37 +00002046//------------------------------------------------------------//
2047//--- Instrumentation ---//
2048//------------------------------------------------------------//
nethercotec9f36922004-02-14 16:40:02 +00002049
njn1a2741a2007-11-26 21:59:04 +00002050static void add_counter_update(IRSB* sbOut, Int n)
2051{
2052 #if defined(VG_BIGENDIAN)
2053 # define END Iend_BE
2054 #elif defined(VG_LITTLEENDIAN)
2055 # define END Iend_LE
2056 #else
2057 # error "Unknown endianness"
2058 #endif
2059 // Add code to increment 'guest_instrs_executed' by 'n', like this:
2060 // WrTmp(t1, Load64(&guest_instrs_executed))
2061 // WrTmp(t2, Add64(RdTmp(t1), Const(n)))
2062 // Store(&guest_instrs_executed, t2)
2063 IRTemp t1 = newIRTemp(sbOut->tyenv, Ity_I64);
2064 IRTemp t2 = newIRTemp(sbOut->tyenv, Ity_I64);
2065 IRExpr* counter_addr = mkIRExpr_HWord( (HWord)&guest_instrs_executed );
2066
sewardjdb5907d2009-11-26 17:20:21 +00002067 IRStmt* st1 = IRStmt_WrTmp(t1, IRExpr_Load(END, Ity_I64, counter_addr));
njn1a2741a2007-11-26 21:59:04 +00002068 IRStmt* st2 =
2069 IRStmt_WrTmp(t2,
2070 IRExpr_Binop(Iop_Add64, IRExpr_RdTmp(t1),
2071 IRExpr_Const(IRConst_U64(n))));
sewardjdb5907d2009-11-26 17:20:21 +00002072 IRStmt* st3 = IRStmt_Store(END, counter_addr, IRExpr_RdTmp(t2));
njn1a2741a2007-11-26 21:59:04 +00002073
2074 addStmtToIRSB( sbOut, st1 );
2075 addStmtToIRSB( sbOut, st2 );
2076 addStmtToIRSB( sbOut, st3 );
2077}
2078
2079static IRSB* ms_instrument2( IRSB* sbIn )
2080{
2081 Int i, n = 0;
2082 IRSB* sbOut;
2083
2084 // We increment the instruction count in two places:
2085 // - just before any Ist_Exit statements;
2086 // - just before the IRSB's end.
2087 // In the former case, we zero 'n' and then continue instrumenting.
2088
2089 sbOut = deepCopyIRSBExceptStmts(sbIn);
2090
2091 for (i = 0; i < sbIn->stmts_used; i++) {
2092 IRStmt* st = sbIn->stmts[i];
2093
2094 if (!st || st->tag == Ist_NoOp) continue;
2095
2096 if (st->tag == Ist_IMark) {
2097 n++;
2098 } else if (st->tag == Ist_Exit) {
2099 if (n > 0) {
2100 // Add an increment before the Exit statement, then reset 'n'.
2101 add_counter_update(sbOut, n);
2102 n = 0;
2103 }
2104 }
2105 addStmtToIRSB( sbOut, st );
2106 }
2107
2108 if (n > 0) {
2109 // Add an increment before the SB end.
2110 add_counter_update(sbOut, n);
2111 }
2112 return sbOut;
2113}
2114
sewardj4ba057c2005-10-18 12:04:18 +00002115static
sewardj0b9d74a2006-12-24 02:24:11 +00002116IRSB* ms_instrument ( VgCallbackClosure* closure,
njn1a2741a2007-11-26 21:59:04 +00002117 IRSB* sbIn,
njn734b8052007-11-01 04:40:37 +00002118 VexGuestLayout* layout,
sewardj461df9c2006-01-17 02:06:39 +00002119 VexGuestExtents* vge,
sewardj4ba057c2005-10-18 12:04:18 +00002120 IRType gWordTy, IRType hWordTy )
nethercotec9f36922004-02-14 16:40:02 +00002121{
njn734b8052007-11-01 04:40:37 +00002122 if (! have_started_executing_code) {
2123 // Do an initial sample to guarantee that we have at least one.
2124 // We use 'maybe_take_snapshot' instead of 'take_snapshot' to ensure
2125 // 'maybe_take_snapshot's internal static variables are initialised.
2126 have_started_executing_code = True;
2127 maybe_take_snapshot(Normal, "startup");
2128 }
njn1a2741a2007-11-26 21:59:04 +00002129
2130 if (clo_time_unit == TimeI) { return ms_instrument2(sbIn); }
2131 else if (clo_time_unit == TimeMS) { return sbIn; }
2132 else if (clo_time_unit == TimeB) { return sbIn; }
2133 else { tl_assert2(0, "bad --time-unit value"); }
nethercotec9f36922004-02-14 16:40:02 +00002134}
2135
nethercotec9f36922004-02-14 16:40:02 +00002136
njn734b8052007-11-01 04:40:37 +00002137//------------------------------------------------------------//
2138//--- Writing snapshots ---//
2139//------------------------------------------------------------//
nethercotec9f36922004-02-14 16:40:02 +00002140
njnb11e5aa2008-01-20 22:30:52 +00002141Char FP_buf[BUF_LEN];
nethercotec9f36922004-02-14 16:40:02 +00002142
njn734b8052007-11-01 04:40:37 +00002143// XXX: implement f{,n}printf in m_libcprint.c eventually, and use it here.
2144// Then change Cachegrind to use it too.
2145#define FP(format, args...) ({ \
njnb11e5aa2008-01-20 22:30:52 +00002146 VG_(snprintf)(FP_buf, BUF_LEN, format, ##args); \
2147 FP_buf[BUF_LEN-1] = '\0'; /* Make sure the string is terminated. */ \
2148 VG_(write)(fd, (void*)FP_buf, VG_(strlen)(FP_buf)); \
2149})
2150
nethercotec9f36922004-02-14 16:40:02 +00002151// Nb: uses a static buffer, each call trashes the last string returned.
njne9bbd362011-01-27 23:07:56 +00002152static Char* make_perc(double x)
nethercotec9f36922004-02-14 16:40:02 +00002153{
2154 static Char mbuf[32];
njn734b8052007-11-01 04:40:37 +00002155
njne9bbd362011-01-27 23:07:56 +00002156 VG_(percentify)((ULong)(x * 100), 10000, 2, 6, mbuf);
njn734b8052007-11-01 04:40:37 +00002157 // XXX: this is bogus if the denominator was zero -- resulting string is
2158 // something like "0 --%")
2159 if (' ' == mbuf[0]) mbuf[0] = '0';
nethercotec9f36922004-02-14 16:40:02 +00002160 return mbuf;
2161}
2162
njn734b8052007-11-01 04:40:37 +00002163static void pp_snapshot_SXPt(Int fd, SXPt* sxpt, Int depth, Char* depth_str,
2164 Int depth_str_len,
2165 SizeT snapshot_heap_szB, SizeT snapshot_total_szB)
nethercotec9f36922004-02-14 16:40:02 +00002166{
njn466ed6e2009-02-18 05:14:44 +00002167 Int i, j, n_insig_children_sxpts;
njn734b8052007-11-01 04:40:37 +00002168 SXPt* child = NULL;
nethercotec9f36922004-02-14 16:40:02 +00002169
sewardjf330ae82008-05-13 10:57:29 +00002170 // Used for printing function names. Is made static to keep it out
2171 // of the stack frame -- this function is recursive. Obviously this
2172 // now means its contents are trashed across the recursive call.
2173 static Char ip_desc_array[BUF_LEN];
2174 Char* ip_desc = ip_desc_array;
2175
njn734b8052007-11-01 04:40:37 +00002176 switch (sxpt->tag) {
2177 case SigSXPt:
2178 // Print the SXPt itself.
njn84f32b22009-02-10 07:14:37 +00002179 if (0 == depth) {
njne323a6b2010-07-01 02:35:03 +00002180 if (clo_heap) {
2181 ip_desc =
2182 ( clo_pages_as_heap
2183 ? "(page allocation syscalls) mmap/mremap/brk, --alloc-fns, etc."
2184 : "(heap allocation functions) malloc/new/new[], --alloc-fns, etc."
2185 );
2186 } else {
2187 // XXX: --alloc-fns?
2188 }
njn734b8052007-11-01 04:40:37 +00002189 } else {
2190 // If it's main-or-below-main, we (if appropriate) ignore everything
2191 // below it by pretending it has no children.
njn68824432009-02-10 06:48:00 +00002192 if ( ! VG_(clo_show_below_main) ) {
2193 Vg_FnNameKind kind = VG_(get_fnname_kind_from_IP)(sxpt->Sig.ip);
2194 if (Vg_FnNameMain == kind || Vg_FnNameBelowMain == kind) {
2195 sxpt->Sig.n_children = 0;
2196 }
njn734b8052007-11-01 04:40:37 +00002197 }
njn68824432009-02-10 06:48:00 +00002198
njn734b8052007-11-01 04:40:37 +00002199 // We need the -1 to get the line number right, But I'm not sure why.
2200 ip_desc = VG_(describe_IP)(sxpt->Sig.ip-1, ip_desc, BUF_LEN);
2201 }
njn466ed6e2009-02-18 05:14:44 +00002202
2203 // Do the non-ip_desc part first...
2204 FP("%sn%d: %lu ", depth_str, sxpt->Sig.n_children, sxpt->szB);
2205
2206 // For ip_descs beginning with "0xABCD...:" addresses, we first
2207 // measure the length of the "0xabcd: " address at the start of the
2208 // ip_desc.
2209 j = 0;
2210 if ('0' == ip_desc[0] && 'x' == ip_desc[1]) {
2211 j = 2;
2212 while (True) {
2213 if (ip_desc[j]) {
2214 if (':' == ip_desc[j]) break;
2215 j++;
2216 } else {
2217 tl_assert2(0, "ip_desc has unexpected form: %s\n", ip_desc);
2218 }
2219 }
2220 }
2221 // Nb: We treat this specially (ie. we don't use FP) so that if the
2222 // ip_desc is too long (eg. due to a long C++ function name), it'll
2223 // get truncated, but the '\n' is still there so its a valid file.
2224 // (At one point we were truncating without adding the '\n', which
2225 // caused bug #155929.)
2226 //
2227 // Also, we account for the length of the address in ip_desc when
2228 // truncating. (The longest address we could have is 18 chars: "0x"
2229 // plus 16 address digits.) This ensures that the truncated function
2230 // name always has the same length, which makes truncation
2231 // deterministic and thus makes testing easier.
2232 tl_assert(j <= 18);
2233 VG_(snprintf)(FP_buf, BUF_LEN, "%s\n", ip_desc);
2234 FP_buf[BUF_LEN-18+j-5] = '.'; // "..." at the end make the
2235 FP_buf[BUF_LEN-18+j-4] = '.'; // truncation more obvious.
2236 FP_buf[BUF_LEN-18+j-3] = '.';
2237 FP_buf[BUF_LEN-18+j-2] = '\n'; // The last char is '\n'.
2238 FP_buf[BUF_LEN-18+j-1] = '\0'; // The string is terminated.
2239 VG_(write)(fd, (void*)FP_buf, VG_(strlen)(FP_buf));
nethercotec9f36922004-02-14 16:40:02 +00002240
njn734b8052007-11-01 04:40:37 +00002241 // Indent.
2242 tl_assert(depth+1 < depth_str_len-1); // -1 for end NUL char
2243 depth_str[depth+0] = ' ';
2244 depth_str[depth+1] = '\0';
2245
2246 // Sort SXPt's children by szB (reverse order: biggest to smallest).
2247 // Nb: we sort them here, rather than earlier (eg. in dup_XTree), for
2248 // two reasons. First, if we do it during dup_XTree, it can get
2249 // expensive (eg. 15% of execution time for konqueror
2250 // startup/shutdown). Second, this way we get the Insig SXPt (if one
2251 // is present) in its sorted position, not at the end.
2252 VG_(ssort)(sxpt->Sig.children, sxpt->Sig.n_children, sizeof(SXPt*),
2253 SXPt_revcmp_szB);
2254
2255 // Print the SXPt's children. They should already be in sorted order.
2256 n_insig_children_sxpts = 0;
2257 for (i = 0; i < sxpt->Sig.n_children; i++) {
njn734b8052007-11-01 04:40:37 +00002258 child = sxpt->Sig.children[i];
2259
2260 if (InsigSXPt == child->tag)
2261 n_insig_children_sxpts++;
2262
sewardjf330ae82008-05-13 10:57:29 +00002263 // Ok, print the child. NB: contents of ip_desc_array will be
2264 // trashed by this recursive call. Doesn't matter currently,
2265 // but worth noting.
njn734b8052007-11-01 04:40:37 +00002266 pp_snapshot_SXPt(fd, child, depth+1, depth_str, depth_str_len,
2267 snapshot_heap_szB, snapshot_total_szB);
njn734b8052007-11-01 04:40:37 +00002268 }
njndbeb5352007-12-04 03:15:23 +00002269
2270 // Unindent.
2271 depth_str[depth+0] = '\0';
2272 depth_str[depth+1] = '\0';
2273
njn734b8052007-11-01 04:40:37 +00002274 // There should be 0 or 1 Insig children SXPts.
2275 tl_assert(n_insig_children_sxpts <= 1);
2276 break;
2277
2278 case InsigSXPt: {
njn84f32b22009-02-10 07:14:37 +00002279 Char* s = ( 1 == sxpt->Insig.n_xpts ? "," : "s, all" );
njn734b8052007-11-01 04:40:37 +00002280 FP("%sn0: %lu in %d place%s below massif's threshold (%s)\n",
2281 depth_str, sxpt->szB, sxpt->Insig.n_xpts, s,
njne9bbd362011-01-27 23:07:56 +00002282 make_perc(clo_threshold));
njn734b8052007-11-01 04:40:37 +00002283 break;
2284 }
2285
2286 default:
2287 tl_assert2(0, "pp_snapshot_SXPt: unrecognised SXPt tag");
nethercotec9f36922004-02-14 16:40:02 +00002288 }
nethercotec9f36922004-02-14 16:40:02 +00002289}
2290
njn734b8052007-11-01 04:40:37 +00002291static void pp_snapshot(Int fd, Snapshot* snapshot, Int snapshot_n)
nethercotec9f36922004-02-14 16:40:02 +00002292{
njn734b8052007-11-01 04:40:37 +00002293 sanity_check_snapshot(snapshot);
nethercotec9f36922004-02-14 16:40:02 +00002294
njn734b8052007-11-01 04:40:37 +00002295 FP("#-----------\n");
2296 FP("snapshot=%d\n", snapshot_n);
2297 FP("#-----------\n");
2298 FP("time=%lld\n", snapshot->time);
2299 FP("mem_heap_B=%lu\n", snapshot->heap_szB);
njn32397c02007-11-10 04:08:08 +00002300 FP("mem_heap_extra_B=%lu\n", snapshot->heap_extra_szB);
njn734b8052007-11-01 04:40:37 +00002301 FP("mem_stacks_B=%lu\n", snapshot->stacks_szB);
2302
2303 if (is_detailed_snapshot(snapshot)) {
2304 // Detailed snapshot -- print heap tree.
2305 Int depth_str_len = clo_depth + 3;
sewardj9c606bd2008-09-18 18:12:50 +00002306 Char* depth_str = VG_(malloc)("ms.main.pps.1",
2307 sizeof(Char) * depth_str_len);
njn734b8052007-11-01 04:40:37 +00002308 SizeT snapshot_total_szB =
njn32397c02007-11-10 04:08:08 +00002309 snapshot->heap_szB + snapshot->heap_extra_szB + snapshot->stacks_szB;
njn734b8052007-11-01 04:40:37 +00002310 depth_str[0] = '\0'; // Initialise depth_str to "".
2311
2312 FP("heap_tree=%s\n", ( Peak == snapshot->kind ? "peak" : "detailed" ));
2313 pp_snapshot_SXPt(fd, snapshot->alloc_sxpt, 0, depth_str,
2314 depth_str_len, snapshot->heap_szB,
2315 snapshot_total_szB);
2316
2317 VG_(free)(depth_str);
2318
2319 } else {
2320 FP("heap_tree=empty\n");
nethercote43a15ce2004-08-30 19:15:12 +00002321 }
nethercotec9f36922004-02-14 16:40:02 +00002322}
2323
sewardj3b290482011-05-06 21:02:55 +00002324static void write_snapshots_to_file(Char* massif_out_file,
2325 Snapshot snapshots_array[],
2326 Int nr_elements)
nethercotec9f36922004-02-14 16:40:02 +00002327{
njn734b8052007-11-01 04:40:37 +00002328 Int i, fd;
sewardj92645592005-07-23 09:18:34 +00002329 SysRes sres;
nethercotec9f36922004-02-14 16:40:02 +00002330
njn734b8052007-11-01 04:40:37 +00002331 sres = VG_(open)(massif_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
2332 VKI_S_IRUSR|VKI_S_IWUSR);
njncda2f0f2009-05-18 02:12:08 +00002333 if (sr_isError(sres)) {
njn734b8052007-11-01 04:40:37 +00002334 // If the file can't be opened for whatever reason (conflict
2335 // between multiple cachegrinded processes?), give up now.
sewardj5ea0f392009-07-15 14:51:34 +00002336 VG_(umsg)("error: can't open output file '%s'\n", massif_out_file );
2337 VG_(umsg)(" ... so profiling results will be missing.\n");
nethercotec9f36922004-02-14 16:40:02 +00002338 return;
sewardj92645592005-07-23 09:18:34 +00002339 } else {
njncda2f0f2009-05-18 02:12:08 +00002340 fd = sr_Res(sres);
nethercotec9f36922004-02-14 16:40:02 +00002341 }
2342
njn734b8052007-11-01 04:40:37 +00002343 // Print massif-specific options that were used.
2344 // XXX: is it worth having a "desc:" line? Could just call it "options:"
2345 // -- this file format isn't as generic as Cachegrind's, so the
2346 // implied genericity of "desc:" is bogus.
2347 FP("desc:");
2348 for (i = 0; i < VG_(sizeXA)(args_for_massif); i++) {
2349 Char* arg = *(Char**)VG_(indexXA)(args_for_massif, i);
2350 FP(" %s", arg);
nethercotec9f36922004-02-14 16:40:02 +00002351 }
njn734b8052007-11-01 04:40:37 +00002352 if (0 == i) FP(" (none)");
2353 FP("\n");
nethercotec9f36922004-02-14 16:40:02 +00002354
njn734b8052007-11-01 04:40:37 +00002355 // Print "cmd:" line.
2356 FP("cmd: ");
sewardj45f4e7c2005-09-27 19:20:21 +00002357 if (VG_(args_the_exename)) {
njn734b8052007-11-01 04:40:37 +00002358 FP("%s", VG_(args_the_exename));
2359 for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) {
2360 HChar* arg = * (HChar**) VG_(indexXA)( VG_(args_for_client), i );
2361 if (arg)
2362 FP(" %s", arg);
2363 }
2364 } else {
2365 FP(" ???");
sewardj45f4e7c2005-09-27 19:20:21 +00002366 }
njn734b8052007-11-01 04:40:37 +00002367 FP("\n");
nethercotec9f36922004-02-14 16:40:02 +00002368
njn734b8052007-11-01 04:40:37 +00002369 FP("time_unit: %s\n", TimeUnit_to_string(clo_time_unit));
nethercotec9f36922004-02-14 16:40:02 +00002370
sewardj3b290482011-05-06 21:02:55 +00002371 for (i = 0; i < nr_elements; i++) {
2372 Snapshot* snapshot = & snapshots_array[i];
njn734b8052007-11-01 04:40:37 +00002373 pp_snapshot(fd, snapshot, i); // Detailed snapshot!
nethercotec9f36922004-02-14 16:40:02 +00002374 }
sewardj3b290482011-05-06 21:02:55 +00002375 VG_(close) (fd);
nethercotec9f36922004-02-14 16:40:02 +00002376}
2377
sewardj3b290482011-05-06 21:02:55 +00002378static void write_snapshots_array_to_file(void)
2379{
2380 // Setup output filename. Nb: it's important to do this now, ie. as late
2381 // as possible. If we do it at start-up and the program forks and the
2382 // output file format string contains a %p (pid) specifier, both the
2383 // parent and child will incorrectly write to the same file; this
2384 // happened in 3.3.0.
2385 Char* massif_out_file =
2386 VG_(expand_file_name)("--massif-out-file", clo_massif_out_file);
2387 write_snapshots_to_file (massif_out_file, snapshots, next_snapshot_i);
2388 VG_(free)(massif_out_file);
2389}
2390
bart6b254372011-05-18 16:04:46 +00002391static void handle_snapshot_monitor_command (Char *filename, Bool detailed)
sewardjd142f992011-05-17 17:15:07 +00002392{
2393 Snapshot snapshot;
2394
2395 clear_snapshot(&snapshot, /* do_sanity_check */ False);
2396 take_snapshot(&snapshot, Normal, get_time(), detailed);
2397 write_snapshots_to_file ((filename == NULL) ? (Char*) "massif.vgdb.out" : filename,
2398 &snapshot,
2399 1);
2400 delete_snapshot(&snapshot);
2401}
2402
sewardj3b290482011-05-06 21:02:55 +00002403static Bool handle_gdb_monitor_command (ThreadId tid, Char *req)
2404{
2405 Char* wcmd;
2406 Char s[VG_(strlen(req))]; /* copy for strtok_r */
2407 Char *ssaveptr;
2408
2409 VG_(strcpy) (s, req);
2410
2411 wcmd = VG_(strtok_r) (s, " ", &ssaveptr);
sewardj30b3eca2011-06-28 08:20:39 +00002412 switch (VG_(keyword_id) ("help snapshot detailed_snapshot",
sewardj3b290482011-05-06 21:02:55 +00002413 wcmd, kwd_report_duplicated_matches)) {
2414 case -2: /* multiple matches */
2415 return True;
2416 case -1: /* not found */
2417 return False;
2418 case 0: /* help */
2419 print_monitor_help();
2420 return True;
sewardj30b3eca2011-06-28 08:20:39 +00002421 case 1: { /* snapshot */
sewardjd142f992011-05-17 17:15:07 +00002422 Char* filename;
2423 filename = VG_(strtok_r) (NULL, " ", &ssaveptr);
2424 handle_snapshot_monitor_command (filename, False /* detailed */);
2425 return True;
2426 }
sewardj30b3eca2011-06-28 08:20:39 +00002427 case 2: { /* detailed_snapshot */
sewardjd142f992011-05-17 17:15:07 +00002428 Char* filename;
2429 filename = VG_(strtok_r) (NULL, " ", &ssaveptr);
2430 handle_snapshot_monitor_command (filename, True /* detailed */);
sewardj3b290482011-05-06 21:02:55 +00002431 return True;
2432 }
2433 default:
2434 tl_assert(0);
2435 return False;
2436 }
2437}
njn734b8052007-11-01 04:40:37 +00002438
2439//------------------------------------------------------------//
2440//--- Finalisation ---//
2441//------------------------------------------------------------//
2442
njn51d827b2005-05-09 01:02:08 +00002443static void ms_fini(Int exit_status)
nethercotec9f36922004-02-14 16:40:02 +00002444{
njn734b8052007-11-01 04:40:37 +00002445 // Output.
sewardj3b290482011-05-06 21:02:55 +00002446 write_snapshots_array_to_file();
nethercotec9f36922004-02-14 16:40:02 +00002447
njn734b8052007-11-01 04:40:37 +00002448 // Stats
2449 tl_assert(n_xpts > 0); // always have alloc_xpt
sewardj2d9e8742009-08-07 15:46:56 +00002450 STATS("heap allocs: %u\n", n_heap_allocs);
2451 STATS("heap reallocs: %u\n", n_heap_reallocs);
2452 STATS("heap frees: %u\n", n_heap_frees);
2453 STATS("ignored heap allocs: %u\n", n_ignored_heap_allocs);
2454 STATS("ignored heap frees: %u\n", n_ignored_heap_frees);
2455 STATS("ignored heap reallocs: %u\n", n_ignored_heap_reallocs);
2456 STATS("stack allocs: %u\n", n_stack_allocs);
2457 STATS("stack frees: %u\n", n_stack_frees);
2458 STATS("XPts: %u\n", n_xpts);
2459 STATS("top-XPts: %u (%d%%)\n",
njn734b8052007-11-01 04:40:37 +00002460 alloc_xpt->n_children,
2461 ( n_xpts ? alloc_xpt->n_children * 100 / n_xpts : 0));
sewardj2d9e8742009-08-07 15:46:56 +00002462 STATS("XPt init expansions: %u\n", n_xpt_init_expansions);
2463 STATS("XPt later expansions: %u\n", n_xpt_later_expansions);
2464 STATS("SXPt allocs: %u\n", n_sxpt_allocs);
2465 STATS("SXPt frees: %u\n", n_sxpt_frees);
2466 STATS("skipped snapshots: %u\n", n_skipped_snapshots);
2467 STATS("real snapshots: %u\n", n_real_snapshots);
2468 STATS("detailed snapshots: %u\n", n_detailed_snapshots);
2469 STATS("peak snapshots: %u\n", n_peak_snapshots);
2470 STATS("cullings: %u\n", n_cullings);
2471 STATS("XCon redos: %u\n", n_XCon_redos);
nethercotec9f36922004-02-14 16:40:02 +00002472}
2473
njn734b8052007-11-01 04:40:37 +00002474
2475//------------------------------------------------------------//
2476//--- Initialisation ---//
2477//------------------------------------------------------------//
njn51d827b2005-05-09 01:02:08 +00002478
2479static void ms_post_clo_init(void)
2480{
njn734b8052007-11-01 04:40:37 +00002481 Int i;
njne323a6b2010-07-01 02:35:03 +00002482 Char* LD_PRELOAD_val;
2483 Char* s;
2484 Char* s2;
njn51d827b2005-05-09 01:02:08 +00002485
njn734b8052007-11-01 04:40:37 +00002486 // Check options.
njne323a6b2010-07-01 02:35:03 +00002487 if (clo_pages_as_heap) {
2488 if (clo_stacks) {
njnb1cc5d62010-07-06 04:05:23 +00002489 VG_(fmsg_bad_option)(
2490 "--pages-as-heap=yes together with --stacks=yes", "");
njne323a6b2010-07-01 02:35:03 +00002491 }
2492 }
njn734b8052007-11-01 04:40:37 +00002493 if (!clo_heap) {
njne323a6b2010-07-01 02:35:03 +00002494 clo_pages_as_heap = False;
2495 }
2496
2497 // If --pages-as-heap=yes we don't want malloc replacement to occur. So we
2498 // disable vgpreload_massif-$PLATFORM.so by removing it from LD_PRELOAD (or
2499 // platform-equivalent). We replace it entirely with spaces because then
2500 // the linker doesn't complain (it does complain if we just change the name
2501 // to a bogus file). This is a bit of a hack, but LD_PRELOAD is setup well
2502 // before tool initialisation, so this seems the best way to do it.
2503 if (clo_pages_as_heap) {
2504 clo_heap_admin = 0; // No heap admin on pages.
2505
2506 LD_PRELOAD_val = VG_(getenv)( (Char*)VG_(LD_PRELOAD_var_name) );
2507 tl_assert(LD_PRELOAD_val);
2508
2509 // Make sure the vgpreload_core-$PLATFORM entry is there, for sanity.
2510 s2 = VG_(strstr)(LD_PRELOAD_val, "vgpreload_core");
2511 tl_assert(s2);
2512
2513 // Now find the vgpreload_massif-$PLATFORM entry.
2514 s2 = VG_(strstr)(LD_PRELOAD_val, "vgpreload_massif");
2515 tl_assert(s2);
2516
2517 // Blank out everything to the previous ':', which must be there because
2518 // of the preceding vgpreload_core-$PLATFORM entry.
2519 for (s = s2; *s != ':'; s--) {
2520 *s = ' ';
2521 }
2522
2523 // Blank out everything to the end of the entry, which will be '\0' if
2524 // LD_PRELOAD was empty before Valgrind started, or ':' otherwise.
2525 for (s = s2; *s != ':' && *s != '\0'; s++) {
2526 *s = ' ';
2527 }
njn734b8052007-11-01 04:40:37 +00002528 }
2529
njnf6b00762009-04-17 04:26:41 +00002530 // Print alloc-fns and ignore-fns, if necessary.
njn734b8052007-11-01 04:40:37 +00002531 if (VG_(clo_verbosity) > 1) {
sewardj5ea0f392009-07-15 14:51:34 +00002532 VERB(1, "alloc-fns:\n");
njn734b8052007-11-01 04:40:37 +00002533 for (i = 0; i < VG_(sizeXA)(alloc_fns); i++) {
njnf6b00762009-04-17 04:26:41 +00002534 Char** fn_ptr = VG_(indexXA)(alloc_fns, i);
sewardj5ea0f392009-07-15 14:51:34 +00002535 VERB(1, " %s\n", *fn_ptr);
njnf6b00762009-04-17 04:26:41 +00002536 }
2537
sewardj5ea0f392009-07-15 14:51:34 +00002538 VERB(1, "ignore-fns:\n");
njnf6b00762009-04-17 04:26:41 +00002539 if (0 == VG_(sizeXA)(ignore_fns)) {
sewardj5ea0f392009-07-15 14:51:34 +00002540 VERB(1, " <empty>\n");
njnf6b00762009-04-17 04:26:41 +00002541 }
2542 for (i = 0; i < VG_(sizeXA)(ignore_fns); i++) {
2543 Char** fn_ptr = VG_(indexXA)(ignore_fns, i);
sewardj5ea0f392009-07-15 14:51:34 +00002544 VERB(1, " %d: %s\n", i, *fn_ptr);
njn734b8052007-11-01 04:40:37 +00002545 }
2546 }
2547
2548 // Events to track.
2549 if (clo_stacks) {
2550 VG_(track_new_mem_stack) ( new_mem_stack );
2551 VG_(track_die_mem_stack) ( die_mem_stack );
2552 VG_(track_new_mem_stack_signal) ( new_mem_stack_signal );
2553 VG_(track_die_mem_stack_signal) ( die_mem_stack_signal );
2554 }
2555
njne323a6b2010-07-01 02:35:03 +00002556 if (clo_pages_as_heap) {
2557 VG_(track_new_mem_startup) ( ms_new_mem_startup );
2558 VG_(track_new_mem_brk) ( ms_new_mem_brk );
2559 VG_(track_new_mem_mmap) ( ms_new_mem_mmap );
2560
2561 VG_(track_copy_mem_remap) ( ms_copy_mem_remap );
2562
2563 VG_(track_die_mem_brk) ( ms_die_mem_brk );
2564 VG_(track_die_mem_munmap) ( ms_die_mem_munmap );
2565 }
2566
njn734b8052007-11-01 04:40:37 +00002567 // Initialise snapshot array, and sanity-check it.
sewardj9c606bd2008-09-18 18:12:50 +00002568 snapshots = VG_(malloc)("ms.main.mpoci.1",
2569 sizeof(Snapshot) * clo_max_snapshots);
njn734b8052007-11-01 04:40:37 +00002570 // We don't want to do snapshot sanity checks here, because they're
2571 // currently uninitialised.
2572 for (i = 0; i < clo_max_snapshots; i++) {
2573 clear_snapshot( & snapshots[i], /*do_sanity_check*/False );
2574 }
2575 sanity_check_snapshots_array();
njn51d827b2005-05-09 01:02:08 +00002576}
2577
tom151a6392005-11-11 12:30:36 +00002578static void ms_pre_clo_init(void)
njn734b8052007-11-01 04:40:37 +00002579{
njn51d827b2005-05-09 01:02:08 +00002580 VG_(details_name) ("Massif");
2581 VG_(details_version) (NULL);
njn1a2741a2007-11-26 21:59:04 +00002582 VG_(details_description) ("a heap profiler");
njn9a0cba42007-04-15 22:15:57 +00002583 VG_(details_copyright_author)(
sewardjec062e82011-10-23 07:32:08 +00002584 "Copyright (C) 2003-2011, and GNU GPL'd, by Nicholas Nethercote");
njn51d827b2005-05-09 01:02:08 +00002585 VG_(details_bug_reports_to) (VG_BUGS_TO);
2586
sewardj1e0fff62011-01-10 15:01:03 +00002587 VG_(details_avg_translation_sizeB) ( 330 );
2588
njnf6b00762009-04-17 04:26:41 +00002589 // Basic functions.
njn51d827b2005-05-09 01:02:08 +00002590 VG_(basic_tool_funcs) (ms_post_clo_init,
2591 ms_instrument,
2592 ms_fini);
2593
njnf6b00762009-04-17 04:26:41 +00002594 // Needs.
njn51d827b2005-05-09 01:02:08 +00002595 VG_(needs_libc_freeres)();
2596 VG_(needs_command_line_options)(ms_process_cmd_line_option,
2597 ms_print_usage,
2598 ms_print_debug_usage);
2599 VG_(needs_client_requests) (ms_handle_client_request);
njn734b8052007-11-01 04:40:37 +00002600 VG_(needs_sanity_checks) (ms_cheap_sanity_check,
2601 ms_expensive_sanity_check);
njnfc51f8d2005-06-21 03:20:17 +00002602 VG_(needs_malloc_replacement) (ms_malloc,
njn51d827b2005-05-09 01:02:08 +00002603 ms___builtin_new,
2604 ms___builtin_vec_new,
2605 ms_memalign,
2606 ms_calloc,
2607 ms_free,
2608 ms___builtin_delete,
2609 ms___builtin_vec_delete,
2610 ms_realloc,
njn8b140de2009-02-17 04:31:18 +00002611 ms_malloc_usable_size,
njn51d827b2005-05-09 01:02:08 +00002612 0 );
2613
njnf6b00762009-04-17 04:26:41 +00002614 // HP_Chunks.
njn734b8052007-11-01 04:40:37 +00002615 malloc_list = VG_(HT_construct)( "Massif's malloc list" );
njn51d827b2005-05-09 01:02:08 +00002616
2617 // Dummy node at top of the context structure.
njn734b8052007-11-01 04:40:37 +00002618 alloc_xpt = new_XPt(/*ip*/0, /*parent*/NULL);
2619
njnf6b00762009-04-17 04:26:41 +00002620 // Initialise alloc_fns and ignore_fns.
njn734b8052007-11-01 04:40:37 +00002621 init_alloc_fns();
njnf6b00762009-04-17 04:26:41 +00002622 init_ignore_fns();
njn734b8052007-11-01 04:40:37 +00002623
2624 // Initialise args_for_massif.
sewardj9c606bd2008-09-18 18:12:50 +00002625 args_for_massif = VG_(newXA)(VG_(malloc), "ms.main.mprci.1",
2626 VG_(free), sizeof(HChar*));
njn51d827b2005-05-09 01:02:08 +00002627}
2628
sewardj45f4e7c2005-09-27 19:20:21 +00002629VG_DETERMINE_INTERFACE_VERSION(ms_pre_clo_init)
nethercotec9f36922004-02-14 16:40:02 +00002630
njn734b8052007-11-01 04:40:37 +00002631//--------------------------------------------------------------------//
2632//--- end ---//
2633//--------------------------------------------------------------------//