blob: 9e60043e4fc210ae99a28c0a9b45fc4f36567d27 [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
sewardj03f8d3f2012-08-05 15:46:46 +00009 Copyright (C) 2003-2012 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",
florian19f91bb2012-11-10 22:29:54 +0000313 VG_(free), sizeof(HChar*));
florian6bd9dc12012-11-23 16:17:43 +0000314 #define DO(x) { const HChar* 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",
florian19f91bb2012-11-10 22:29:54 +0000360 VG_(free), sizeof(HChar*));
njnf6b00762009-04-17 04:26:41 +0000361}
362
363// Determines if the named function is a member of the XArray.
florian6bd9dc12012-11-23 16:17:43 +0000364static Bool is_member_fn(XArray* fns, const HChar* fnname)
njnf6b00762009-04-17 04:26:41 +0000365{
florian19f91bb2012-11-10 22:29:54 +0000366 HChar** 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
floriane58e8a72012-10-20 19:57:16 +0000390static const HChar* TimeUnit_to_string(TimeUnit time_unit)
njn734b8052007-11-01 04:40:37 +0000391{
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;
florian19f91bb2012-11-10 22:29:54 +0000414static const HChar* 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
florian19f91bb2012-11-10 22:29:54 +0000418static Bool ms_process_cmd_line_option(const HChar* arg)
nethercotec9f36922004-02-14 16:40:02 +0000419{
florian19f91bb2012-11-10 22:29:54 +0000420 const HChar* tmp_str;
njn83df0b62009-02-25 01:01:05 +0000421
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
njn734b8052007-11-01 04:40:37 +0000594static XPt* new_XPt(Addr ip, XPt* parent)
nethercotec9f36922004-02-14 16:40:02 +0000595{
philippe6e4b7132013-01-18 06:19:49 +0000596 // XPts are never freed, so we can use VG_(perm_malloc) to allocate them.
597 // Note that we cannot use VG_(perm_malloc) for the 'children' array, because
njn734b8052007-11-01 04:40:37 +0000598 // that needs to be resizable.
philippe6e4b7132013-01-18 06:19:49 +0000599 XPt* xpt = VG_(perm_malloc)(sizeof(XPt), vg_alignof(XPt));
njn734b8052007-11-01 04:40:37 +0000600 xpt->ip = ip;
601 xpt->szB = 0;
602 xpt->parent = parent;
nethercotec9f36922004-02-14 16:40:02 +0000603
njn734b8052007-11-01 04:40:37 +0000604 // We don't initially allocate any space for children. We let that
605 // happen on demand. Many XPts (ie. all the bottom-XPts) don't have any
606 // children anyway.
nethercotec9f36922004-02-14 16:40:02 +0000607 xpt->n_children = 0;
njn734b8052007-11-01 04:40:37 +0000608 xpt->max_children = 0;
609 xpt->children = NULL;
nethercotec9f36922004-02-14 16:40:02 +0000610
611 // Update statistics
612 n_xpts++;
613
614 return xpt;
615}
616
njn734b8052007-11-01 04:40:37 +0000617static void add_child_xpt(XPt* parent, XPt* child)
618{
619 // Expand 'children' if necessary.
620 tl_assert(parent->n_children <= parent->max_children);
621 if (parent->n_children == parent->max_children) {
njn84f32b22009-02-10 07:14:37 +0000622 if (0 == parent->max_children) {
njn734b8052007-11-01 04:40:37 +0000623 parent->max_children = 4;
sewardj9c606bd2008-09-18 18:12:50 +0000624 parent->children = VG_(malloc)( "ms.main.acx.1",
625 parent->max_children * sizeof(XPt*) );
njn734b8052007-11-01 04:40:37 +0000626 n_xpt_init_expansions++;
627 } else {
628 parent->max_children *= 2; // Double size
sewardj9c606bd2008-09-18 18:12:50 +0000629 parent->children = VG_(realloc)( "ms.main.acx.2",
630 parent->children,
njn734b8052007-11-01 04:40:37 +0000631 parent->max_children * sizeof(XPt*) );
632 n_xpt_later_expansions++;
633 }
634 }
635
636 // Insert new child XPt in parent's children list.
637 parent->children[ parent->n_children++ ] = child;
638}
639
640// Reverse comparison for a reverse sort -- biggest to smallest.
florian6bd9dc12012-11-23 16:17:43 +0000641static Int SXPt_revcmp_szB(const void* n1, const void* n2)
njn734b8052007-11-01 04:40:37 +0000642{
florian3e798632012-11-24 19:41:54 +0000643 const SXPt* sxpt1 = *(const SXPt *const *)n1;
644 const SXPt* sxpt2 = *(const SXPt *const *)n2;
njn734b8052007-11-01 04:40:37 +0000645 return ( sxpt1->szB < sxpt2->szB ? 1
646 : sxpt1->szB > sxpt2->szB ? -1
647 : 0);
648}
649
650//------------------------------------------------------------//
651//--- XTree Operations ---//
652//------------------------------------------------------------//
653
654// Duplicates an XTree as an SXTree.
655static SXPt* dup_XTree(XPt* xpt, SizeT total_szB)
656{
657 Int i, n_sig_children, n_insig_children, n_child_sxpts;
njndbeb5352007-12-04 03:15:23 +0000658 SizeT sig_child_threshold_szB;
njn734b8052007-11-01 04:40:37 +0000659 SXPt* sxpt;
660
661 // Number of XPt children Action for SXPT
662 // ------------------ ---------------
663 // 0 sig, 0 insig alloc 0 children
664 // N sig, 0 insig alloc N children, dup all
665 // N sig, M insig alloc N+1, dup first N, aggregate remaining M
666 // 0 sig, M insig alloc 1, aggregate M
667
668 // Work out how big a child must be to be significant. If the current
669 // total_szB is zero, then we set it to 1, which means everything will be
670 // judged insignificant -- this is sensible, as there's no point showing
671 // any detail for this case. Unless they used --threshold=0, in which
672 // case we show them everything because that's what they asked for.
673 //
674 // Nb: We do this once now, rather than once per child, because if we do
675 // that the cost of all the divisions adds up to something significant.
njn84f32b22009-02-10 07:14:37 +0000676 if (0 == total_szB && 0 != clo_threshold) {
njn734b8052007-11-01 04:40:37 +0000677 sig_child_threshold_szB = 1;
678 } else {
njn62721e92007-11-11 22:15:58 +0000679 sig_child_threshold_szB = (SizeT)((total_szB * clo_threshold) / 100);
njn734b8052007-11-01 04:40:37 +0000680 }
681
682 // How many children are significant? And do we need an aggregate SXPt?
683 n_sig_children = 0;
684 for (i = 0; i < xpt->n_children; i++) {
685 if (xpt->children[i]->szB >= sig_child_threshold_szB) {
686 n_sig_children++;
687 }
688 }
689 n_insig_children = xpt->n_children - n_sig_children;
690 n_child_sxpts = n_sig_children + ( n_insig_children > 0 ? 1 : 0 );
691
692 // Duplicate the XPt.
sewardj9c606bd2008-09-18 18:12:50 +0000693 sxpt = VG_(malloc)("ms.main.dX.1", sizeof(SXPt));
njn734b8052007-11-01 04:40:37 +0000694 n_sxpt_allocs++;
695 sxpt->tag = SigSXPt;
696 sxpt->szB = xpt->szB;
697 sxpt->Sig.ip = xpt->ip;
698 sxpt->Sig.n_children = n_child_sxpts;
699
700 // Create the SXPt's children.
701 if (n_child_sxpts > 0) {
702 Int j;
njndbeb5352007-12-04 03:15:23 +0000703 SizeT sig_children_szB = 0, insig_children_szB = 0;
sewardj9c606bd2008-09-18 18:12:50 +0000704 sxpt->Sig.children = VG_(malloc)("ms.main.dX.2",
705 n_child_sxpts * sizeof(SXPt*));
njn734b8052007-11-01 04:40:37 +0000706
njndbeb5352007-12-04 03:15:23 +0000707 // Duplicate the significant children. (Nb: sig_children_szB +
708 // insig_children_szB doesn't necessarily equal xpt->szB.)
njn734b8052007-11-01 04:40:37 +0000709 j = 0;
710 for (i = 0; i < xpt->n_children; i++) {
711 if (xpt->children[i]->szB >= sig_child_threshold_szB) {
712 sxpt->Sig.children[j++] = dup_XTree(xpt->children[i], total_szB);
njndbeb5352007-12-04 03:15:23 +0000713 sig_children_szB += xpt->children[i]->szB;
714 } else {
715 insig_children_szB += xpt->children[i]->szB;
njn734b8052007-11-01 04:40:37 +0000716 }
717 }
718
719 // Create the SXPt for the insignificant children, if any, and put it
720 // in the last child entry.
njn734b8052007-11-01 04:40:37 +0000721 if (n_insig_children > 0) {
722 // Nb: We 'n_sxpt_allocs' here because creating an Insig SXPt
723 // doesn't involve a call to dup_XTree().
sewardj9c606bd2008-09-18 18:12:50 +0000724 SXPt* insig_sxpt = VG_(malloc)("ms.main.dX.3", sizeof(SXPt));
njn734b8052007-11-01 04:40:37 +0000725 n_sxpt_allocs++;
726 insig_sxpt->tag = InsigSXPt;
727 insig_sxpt->szB = insig_children_szB;
728 insig_sxpt->Insig.n_xpts = n_insig_children;
729 sxpt->Sig.children[n_sig_children] = insig_sxpt;
730 }
731 } else {
732 sxpt->Sig.children = NULL;
733 }
734
735 return sxpt;
736}
737
738static void free_SXTree(SXPt* sxpt)
739{
740 Int i;
741 tl_assert(sxpt != NULL);
742
743 switch (sxpt->tag) {
744 case SigSXPt:
745 // Free all children SXPts, then the children array.
746 for (i = 0; i < sxpt->Sig.n_children; i++) {
747 free_SXTree(sxpt->Sig.children[i]);
748 sxpt->Sig.children[i] = NULL;
749 }
750 VG_(free)(sxpt->Sig.children); sxpt->Sig.children = NULL;
751 break;
752
753 case InsigSXPt:
754 break;
755
756 default: tl_assert2(0, "free_SXTree: unknown SXPt tag");
757 }
758
759 // Free the SXPt itself.
760 VG_(free)(sxpt); sxpt = NULL;
761 n_sxpt_frees++;
762}
763
764// Sanity checking: we periodically check the heap XTree with
765// ms_expensive_sanity_check.
766static void sanity_check_XTree(XPt* xpt, XPt* parent)
nethercotec9f36922004-02-14 16:40:02 +0000767{
njn734b8052007-11-01 04:40:37 +0000768 tl_assert(xpt != NULL);
769
770 // Check back-pointer.
771 tl_assert2(xpt->parent == parent,
772 "xpt->parent = %p, parent = %p\n", xpt->parent, parent);
773
774 // Check children counts look sane.
775 tl_assert(xpt->n_children <= xpt->max_children);
776
njndbeb5352007-12-04 03:15:23 +0000777 // Unfortunately, xpt's size is not necessarily equal to the sum of xpt's
778 // children's sizes. See comment at the bottom of get_XCon.
nethercotec9f36922004-02-14 16:40:02 +0000779}
780
njn734b8052007-11-01 04:40:37 +0000781// Sanity checking: we check SXTrees (which are in snapshots) after
782// snapshots are created, before they are deleted, and before they are
783// printed.
784static void sanity_check_SXTree(SXPt* sxpt)
nethercotec9f36922004-02-14 16:40:02 +0000785{
njn734b8052007-11-01 04:40:37 +0000786 Int i;
nethercotec9f36922004-02-14 16:40:02 +0000787
njn734b8052007-11-01 04:40:37 +0000788 tl_assert(sxpt != NULL);
nethercotec9f36922004-02-14 16:40:02 +0000789
njn734b8052007-11-01 04:40:37 +0000790 // Check the sum of any children szBs equals the SXPt's szB. Check the
791 // children at the same time.
792 switch (sxpt->tag) {
793 case SigSXPt: {
794 if (sxpt->Sig.n_children > 0) {
njn734b8052007-11-01 04:40:37 +0000795 for (i = 0; i < sxpt->Sig.n_children; i++) {
796 sanity_check_SXTree(sxpt->Sig.children[i]);
njn734b8052007-11-01 04:40:37 +0000797 }
njn734b8052007-11-01 04:40:37 +0000798 }
799 break;
800 }
801 case InsigSXPt:
802 break; // do nothing
803
804 default: tl_assert2(0, "sanity_check_SXTree: unknown SXPt tag");
805 }
806}
807
808
809//------------------------------------------------------------//
810//--- XCon Operations ---//
811//------------------------------------------------------------//
812
813// This is the limit on the number of removed alloc-fns that can be in a
814// single XCon.
815#define MAX_OVERESTIMATE 50
816#define MAX_IPS (MAX_DEPTH + MAX_OVERESTIMATE)
817
njnb11e5aa2008-01-20 22:30:52 +0000818// This is used for various buffers which can hold function names/IP
819// description. Some C++ names can get really long so 1024 isn't big
820// enough.
821#define BUF_LEN 2048
822
njnf6b00762009-04-17 04:26:41 +0000823// Determine if the given IP belongs to a function that should be ignored.
824static Bool fn_should_be_ignored(Addr ip)
825{
florian19f91bb2012-11-10 22:29:54 +0000826 static HChar buf[BUF_LEN];
njnf6b00762009-04-17 04:26:41 +0000827 return
828 ( VG_(get_fnname)(ip, buf, BUF_LEN) && is_member_fn(ignore_fns, buf)
829 ? True : False );
830}
831
njn734b8052007-11-01 04:40:37 +0000832// Get the stack trace for an XCon, filtering out uninteresting entries:
833// alloc-fns and entries above alloc-fns, and entries below main-or-below-main.
834// Eg: alloc-fn1 / alloc-fn2 / a / b / main / (below main) / c
835// becomes: a / b / main
836// Nb: it's possible to end up with an empty trace, eg. if 'main' is marked
837// as an alloc-fn. This is ok.
838static
njne323a6b2010-07-01 02:35:03 +0000839Int get_IPs( ThreadId tid, Bool exclude_first_entry, Addr ips[])
njn734b8052007-11-01 04:40:37 +0000840{
florian19f91bb2012-11-10 22:29:54 +0000841 static HChar buf[BUF_LEN];
njn734b8052007-11-01 04:40:37 +0000842 Int n_ips, i, n_alloc_fns_removed;
843 Int overestimate;
844 Bool redo;
845
846 // We ask for a few more IPs than clo_depth suggests we need. Then we
847 // remove every entry that is an alloc-fn. Depending on the
848 // circumstances, we may need to redo it all, asking for more IPs.
849 // Details:
850 // - If the original stack trace is smaller than asked-for, redo=False
851 // - Else if after filtering we have >= clo_depth IPs, redo=False
852 // - Else redo=True
853 // In other words, to redo, we'd have to get a stack trace as big as we
854 // asked for and remove more than 'overestimate' alloc-fns.
855
856 // Main loop.
857 redo = True; // Assume this to begin with.
858 for (overestimate = 3; redo; overestimate += 6) {
859 // This should never happen -- would require MAX_OVERESTIMATE
860 // alloc-fns to be removed from the stack trace.
861 if (overestimate > MAX_OVERESTIMATE)
862 VG_(tool_panic)("get_IPs: ips[] too small, inc. MAX_OVERESTIMATE?");
863
864 // Ask for more IPs than clo_depth suggests we need.
sewardj39f34232007-11-09 23:02:28 +0000865 n_ips = VG_(get_StackTrace)( tid, ips, clo_depth + overestimate,
sewardjb8b79ad2008-03-03 01:35:41 +0000866 NULL/*array to dump SP values in*/,
867 NULL/*array to dump FP values in*/,
868 0/*first_ip_delta*/ );
njn734b8052007-11-01 04:40:37 +0000869 tl_assert(n_ips > 0);
nethercotec9f36922004-02-14 16:40:02 +0000870
njn734b8052007-11-01 04:40:37 +0000871 // If the original stack trace is smaller than asked-for, redo=False.
872 if (n_ips < clo_depth + overestimate) { redo = False; }
nethercotec9f36922004-02-14 16:40:02 +0000873
njne323a6b2010-07-01 02:35:03 +0000874 // Filter out alloc fns. If requested, we automatically remove the
875 // first entry (which presumably will be something like malloc or
876 // __builtin_new that we're sure to filter out) without looking at it,
877 // because VG_(get_fnname) is expensive.
878 n_alloc_fns_removed = ( exclude_first_entry ? 1 : 0 );
njn734b8052007-11-01 04:40:37 +0000879 for (i = n_alloc_fns_removed; i < n_ips; i++) {
880 if (VG_(get_fnname)(ips[i], buf, BUF_LEN)) {
njnf6b00762009-04-17 04:26:41 +0000881 if (is_member_fn(alloc_fns, buf)) {
njn734b8052007-11-01 04:40:37 +0000882 n_alloc_fns_removed++;
883 } else {
884 break;
885 }
886 }
887 }
888 // Remove the alloc fns by shuffling the rest down over them.
889 n_ips -= n_alloc_fns_removed;
890 for (i = 0; i < n_ips; i++) {
891 ips[i] = ips[i + n_alloc_fns_removed];
892 }
nethercotec9f36922004-02-14 16:40:02 +0000893
njn734b8052007-11-01 04:40:37 +0000894 // If after filtering we have >= clo_depth IPs, redo=False
895 if (n_ips >= clo_depth) {
896 redo = False;
897 n_ips = clo_depth; // Ignore any IPs below --depth.
898 }
899
900 if (redo) {
901 n_XCon_redos++;
nethercotec9f36922004-02-14 16:40:02 +0000902 }
903 }
njn734b8052007-11-01 04:40:37 +0000904 return n_ips;
905}
nethercotec9f36922004-02-14 16:40:02 +0000906
njn734b8052007-11-01 04:40:37 +0000907// Gets an XCon and puts it in the tree. Returns the XCon's bottom-XPt.
njnf6b00762009-04-17 04:26:41 +0000908// Unless the allocation should be ignored, in which case we return NULL.
njne323a6b2010-07-01 02:35:03 +0000909static XPt* get_XCon( ThreadId tid, Bool exclude_first_entry )
njn734b8052007-11-01 04:40:37 +0000910{
njnf6b00762009-04-17 04:26:41 +0000911 static Addr ips[MAX_IPS];
njn734b8052007-11-01 04:40:37 +0000912 Int i;
913 XPt* xpt = alloc_xpt;
nethercotec9f36922004-02-14 16:40:02 +0000914
njn734b8052007-11-01 04:40:37 +0000915 // After this call, the IPs we want are in ips[0]..ips[n_ips-1].
njne323a6b2010-07-01 02:35:03 +0000916 Int n_ips = get_IPs(tid, exclude_first_entry, ips);
njn734b8052007-11-01 04:40:37 +0000917
njnf6b00762009-04-17 04:26:41 +0000918 // Should we ignore this allocation? (Nb: n_ips can be zero, eg. if
919 // 'main' is marked as an alloc-fn.)
920 if (n_ips > 0 && fn_should_be_ignored(ips[0])) {
921 return NULL;
922 }
923
njn734b8052007-11-01 04:40:37 +0000924 // Now do the search/insertion of the XCon.
925 for (i = 0; i < n_ips; i++) {
926 Addr ip = ips[i];
927 Int ch;
njnd01fef72005-03-25 23:35:48 +0000928 // Look for IP in xpt's children.
njn734b8052007-11-01 04:40:37 +0000929 // Linear search, ugh -- about 10% of time for konqueror startup tried
930 // caching last result, only hit about 4% for konqueror.
nethercotec9f36922004-02-14 16:40:02 +0000931 // Nb: this search hits about 98% of the time for konqueror
njn734b8052007-11-01 04:40:37 +0000932 for (ch = 0; True; ch++) {
933 if (ch == xpt->n_children) {
934 // IP not found in the children.
935 // Create and add new child XPt, then stop.
936 XPt* new_child_xpt = new_XPt(ip, xpt);
937 add_child_xpt(xpt, new_child_xpt);
938 xpt = new_child_xpt;
939 break;
nethercotec9f36922004-02-14 16:40:02 +0000940
njn734b8052007-11-01 04:40:37 +0000941 } else if (ip == xpt->children[ch]->ip) {
942 // Found the IP in the children, stop.
943 xpt = xpt->children[ch];
nethercotec9f36922004-02-14 16:40:02 +0000944 break;
945 }
nethercotec9f36922004-02-14 16:40:02 +0000946 }
nethercotec9f36922004-02-14 16:40:02 +0000947 }
njndbeb5352007-12-04 03:15:23 +0000948
949 // [Note: several comments refer to this comment. Do not delete it
950 // without updating them.]
951 //
952 // A complication... If all stack traces were well-formed, then the
953 // returned xpt would always be a bottom-XPt. As a consequence, an XPt's
954 // size would always be equal to the sum of its children's sizes, which
955 // is an excellent sanity check.
956 //
957 // Unfortunately, stack traces occasionally are malformed, ie. truncated.
958 // This allows a stack trace to be a sub-trace of another, eg. a/b/c is a
959 // sub-trace of a/b/c/d. So we can't assume this xpt is a bottom-XPt;
960 // nor can we do sanity check an XPt's size against its children's sizes.
961 // This is annoying, but must be dealt with. (Older versions of Massif
962 // had this assertion in, and it was reported to fail by real users a
963 // couple of times.) Even more annoyingly, I can't come up with a simple
964 // test case that exhibit such a malformed stack trace, so I can't
965 // regression test it. Sigh.
966 //
967 // However, we can print a warning, so that if it happens (unexpectedly)
968 // in existing regression tests we'll know. Also, it warns users that
969 // the output snapshots may not add up the way they might expect.
970 //
971 //tl_assert(0 == xpt->n_children); // Must be bottom-XPt
972 if (0 != xpt->n_children) {
973 static Int n_moans = 0;
974 if (n_moans < 3) {
sewardj5ea0f392009-07-15 14:51:34 +0000975 VG_(umsg)(
976 "Warning: Malformed stack trace detected. In Massif's output,\n");
977 VG_(umsg)(
978 " the size of an entry's child entries may not sum up\n");
979 VG_(umsg)(
980 " to the entry's size as they normally do.\n");
njndbeb5352007-12-04 03:15:23 +0000981 n_moans++;
982 if (3 == n_moans)
sewardj5ea0f392009-07-15 14:51:34 +0000983 VG_(umsg)(
984 " (And Massif now won't warn about this again.)\n");
njndbeb5352007-12-04 03:15:23 +0000985 }
986 }
njn734b8052007-11-01 04:40:37 +0000987 return xpt;
nethercotec9f36922004-02-14 16:40:02 +0000988}
989
njn734b8052007-11-01 04:40:37 +0000990// Update 'szB' of every XPt in the XCon, by percolating upwards.
991static void update_XCon(XPt* xpt, SSizeT space_delta)
nethercotec9f36922004-02-14 16:40:02 +0000992{
njne323a6b2010-07-01 02:35:03 +0000993 tl_assert(clo_heap);
njnca82cc02004-11-22 17:18:48 +0000994 tl_assert(NULL != xpt);
nethercotec9f36922004-02-14 16:40:02 +0000995
njn734b8052007-11-01 04:40:37 +0000996 if (0 == space_delta)
997 return;
998
nethercotec9f36922004-02-14 16:40:02 +0000999 while (xpt != alloc_xpt) {
njn734b8052007-11-01 04:40:37 +00001000 if (space_delta < 0) tl_assert(xpt->szB >= -space_delta);
1001 xpt->szB += space_delta;
nethercotec9f36922004-02-14 16:40:02 +00001002 xpt = xpt->parent;
njn734b8052007-11-01 04:40:37 +00001003 }
1004 if (space_delta < 0) tl_assert(alloc_xpt->szB >= -space_delta);
1005 alloc_xpt->szB += space_delta;
nethercotec9f36922004-02-14 16:40:02 +00001006}
1007
1008
njn734b8052007-11-01 04:40:37 +00001009//------------------------------------------------------------//
1010//--- Snapshots ---//
1011//------------------------------------------------------------//
1012
1013// Snapshots are done in a way so that we always have a reasonable number of
1014// them. We start by taking them quickly. Once we hit our limit, we cull
1015// some (eg. half), and start taking them more slowly. Once we hit the
1016// limit again, we again cull and then take them even more slowly, and so
1017// on.
1018
njn1a2741a2007-11-26 21:59:04 +00001019// Time is measured either in i or ms or bytes, depending on the --time-unit
njn734b8052007-11-01 04:40:37 +00001020// option. It's a Long because it can exceed 32-bits reasonably easily, and
1021// because we need to allow negative values to represent unset times.
1022typedef Long Time;
1023
1024#define UNUSED_SNAPSHOT_TIME -333 // A conspicuous negative number.
1025
1026typedef
1027 enum {
1028 Normal = 77,
1029 Peak,
1030 Unused
1031 }
1032 SnapshotKind;
nethercotec9f36922004-02-14 16:40:02 +00001033
1034typedef
1035 struct {
njn734b8052007-11-01 04:40:37 +00001036 SnapshotKind kind;
1037 Time time;
1038 SizeT heap_szB;
njn32397c02007-11-10 04:08:08 +00001039 SizeT heap_extra_szB;// Heap slop + admin bytes.
njn734b8052007-11-01 04:40:37 +00001040 SizeT stacks_szB;
1041 SXPt* alloc_sxpt; // Heap XTree root, if a detailed snapshot,
njn32397c02007-11-10 04:08:08 +00001042 } // otherwise NULL.
njn734b8052007-11-01 04:40:37 +00001043 Snapshot;
nethercotec9f36922004-02-14 16:40:02 +00001044
njn734b8052007-11-01 04:40:37 +00001045static UInt next_snapshot_i = 0; // Index of where next snapshot will go.
1046static Snapshot* snapshots; // Array of snapshots.
1047
1048static Bool is_snapshot_in_use(Snapshot* snapshot)
nethercotec9f36922004-02-14 16:40:02 +00001049{
njn734b8052007-11-01 04:40:37 +00001050 if (Unused == snapshot->kind) {
1051 // If snapshot is unused, check all the fields are unset.
1052 tl_assert(snapshot->time == UNUSED_SNAPSHOT_TIME);
njn32397c02007-11-10 04:08:08 +00001053 tl_assert(snapshot->heap_extra_szB == 0);
njn734b8052007-11-01 04:40:37 +00001054 tl_assert(snapshot->heap_szB == 0);
1055 tl_assert(snapshot->stacks_szB == 0);
1056 tl_assert(snapshot->alloc_sxpt == NULL);
1057 return False;
nethercotec9f36922004-02-14 16:40:02 +00001058 } else {
njn734b8052007-11-01 04:40:37 +00001059 tl_assert(snapshot->time != UNUSED_SNAPSHOT_TIME);
1060 return True;
nethercotec9f36922004-02-14 16:40:02 +00001061 }
1062}
1063
njn734b8052007-11-01 04:40:37 +00001064static Bool is_detailed_snapshot(Snapshot* snapshot)
nethercotec9f36922004-02-14 16:40:02 +00001065{
njn734b8052007-11-01 04:40:37 +00001066 return (snapshot->alloc_sxpt ? True : False);
nethercotec9f36922004-02-14 16:40:02 +00001067}
1068
njn734b8052007-11-01 04:40:37 +00001069static Bool is_uncullable_snapshot(Snapshot* snapshot)
nethercotec9f36922004-02-14 16:40:02 +00001070{
njn734b8052007-11-01 04:40:37 +00001071 return &snapshots[0] == snapshot // First snapshot
1072 || &snapshots[next_snapshot_i-1] == snapshot // Last snapshot
1073 || snapshot->kind == Peak; // Peak snapshot
nethercotec9f36922004-02-14 16:40:02 +00001074}
1075
njn734b8052007-11-01 04:40:37 +00001076static void sanity_check_snapshot(Snapshot* snapshot)
nethercotec9f36922004-02-14 16:40:02 +00001077{
njn734b8052007-11-01 04:40:37 +00001078 if (snapshot->alloc_sxpt) {
1079 sanity_check_SXTree(snapshot->alloc_sxpt);
1080 }
nethercotec9f36922004-02-14 16:40:02 +00001081}
1082
njn734b8052007-11-01 04:40:37 +00001083// All the used entries should look used, all the unused ones should be clear.
1084static void sanity_check_snapshots_array(void)
1085{
1086 Int i;
1087 for (i = 0; i < next_snapshot_i; i++) {
1088 tl_assert( is_snapshot_in_use( & snapshots[i] ));
1089 }
1090 for ( ; i < clo_max_snapshots; i++) {
1091 tl_assert(!is_snapshot_in_use( & snapshots[i] ));
1092 }
1093}
nethercotec9f36922004-02-14 16:40:02 +00001094
njn734b8052007-11-01 04:40:37 +00001095// This zeroes all the fields in the snapshot, but does not free the heap
1096// XTree if present. It also does a sanity check unless asked not to; we
1097// can't sanity check at startup when clearing the initial snapshots because
1098// they're full of junk.
1099static void clear_snapshot(Snapshot* snapshot, Bool do_sanity_check)
1100{
1101 if (do_sanity_check) sanity_check_snapshot(snapshot);
1102 snapshot->kind = Unused;
1103 snapshot->time = UNUSED_SNAPSHOT_TIME;
njn32397c02007-11-10 04:08:08 +00001104 snapshot->heap_extra_szB = 0;
njn734b8052007-11-01 04:40:37 +00001105 snapshot->heap_szB = 0;
1106 snapshot->stacks_szB = 0;
1107 snapshot->alloc_sxpt = NULL;
1108}
1109
1110// This zeroes all the fields in the snapshot, and frees the heap XTree if
1111// present.
1112static void delete_snapshot(Snapshot* snapshot)
1113{
1114 // Nb: if there's an XTree, we free it after calling clear_snapshot,
1115 // because clear_snapshot does a sanity check which includes checking the
1116 // XTree.
1117 SXPt* tmp_sxpt = snapshot->alloc_sxpt;
1118 clear_snapshot(snapshot, /*do_sanity_check*/True);
1119 if (tmp_sxpt) {
1120 free_SXTree(tmp_sxpt);
1121 }
1122}
1123
floriane58e8a72012-10-20 19:57:16 +00001124static void VERB_snapshot(Int verbosity, const HChar* prefix, Int i)
njn734b8052007-11-01 04:40:37 +00001125{
1126 Snapshot* snapshot = &snapshots[i];
floriane58e8a72012-10-20 19:57:16 +00001127 const HChar* suffix;
njn734b8052007-11-01 04:40:37 +00001128 switch (snapshot->kind) {
1129 case Peak: suffix = "p"; break;
1130 case Normal: suffix = ( is_detailed_snapshot(snapshot) ? "d" : "." ); break;
1131 case Unused: suffix = "u"; break;
1132 default:
1133 tl_assert2(0, "VERB_snapshot: unknown snapshot kind: %d", snapshot->kind);
1134 }
sewardj5ea0f392009-07-15 14:51:34 +00001135 VERB(verbosity, "%s S%s%3d (t:%lld, hp:%ld, ex:%ld, st:%ld)\n",
njn734b8052007-11-01 04:40:37 +00001136 prefix, suffix, i,
1137 snapshot->time,
1138 snapshot->heap_szB,
njn32397c02007-11-10 04:08:08 +00001139 snapshot->heap_extra_szB,
njn734b8052007-11-01 04:40:37 +00001140 snapshot->stacks_szB
1141 );
1142}
1143
1144// Cull half the snapshots; we choose those that represent the smallest
1145// time-spans, because that gives us the most even distribution of snapshots
1146// over time. (It's possible to lose interesting spikes, however.)
1147//
1148// Algorithm for N snapshots: We find the snapshot representing the smallest
1149// timeframe, and remove it. We repeat this until (N/2) snapshots are gone.
1150// We have to do this one snapshot at a time, rather than finding the (N/2)
1151// smallest snapshots in one hit, because when a snapshot is removed, its
1152// neighbours immediately cover greater timespans. So it's O(N^2), but N is
1153// small, and it's not done very often.
1154//
1155// Once we're done, we return the new smallest interval between snapshots.
1156// That becomes our minimum time interval.
1157static UInt cull_snapshots(void)
1158{
1159 Int i, jp, j, jn, min_timespan_i;
1160 Int n_deleted = 0;
1161 Time min_timespan;
1162
1163 n_cullings++;
1164
1165 // Sets j to the index of the first not-yet-removed snapshot at or after i
1166 #define FIND_SNAPSHOT(i, j) \
1167 for (j = i; \
1168 j < clo_max_snapshots && !is_snapshot_in_use(&snapshots[j]); \
1169 j++) { }
1170
sewardj5ea0f392009-07-15 14:51:34 +00001171 VERB(2, "Culling...\n");
njn734b8052007-11-01 04:40:37 +00001172
1173 // First we remove enough snapshots by clearing them in-place. Once
1174 // that's done, we can slide the remaining ones down.
1175 for (i = 0; i < clo_max_snapshots/2; i++) {
1176 // Find the snapshot representing the smallest timespan. The timespan
1177 // for snapshot n = d(N-1,N)+d(N,N+1), where d(A,B) is the time between
1178 // snapshot A and B. We don't consider the first and last snapshots for
1179 // removal.
1180 Snapshot* min_snapshot;
1181 Int min_j;
1182
1183 // Initial triple: (prev, curr, next) == (jp, j, jn)
1184 // Initial min_timespan is the first one.
1185 jp = 0;
1186 FIND_SNAPSHOT(1, j);
1187 FIND_SNAPSHOT(j+1, jn);
1188 min_timespan = 0x7fffffffffffffffLL;
1189 min_j = -1;
1190 while (jn < clo_max_snapshots) {
1191 Time timespan = snapshots[jn].time - snapshots[jp].time;
1192 tl_assert(timespan >= 0);
1193 // Nb: We never cull the peak snapshot.
1194 if (Peak != snapshots[j].kind && timespan < min_timespan) {
1195 min_timespan = timespan;
1196 min_j = j;
1197 }
1198 // Move on to next triple
1199 jp = j;
1200 j = jn;
1201 FIND_SNAPSHOT(jn+1, jn);
1202 }
1203 // We've found the least important snapshot, now delete it. First
1204 // print it if necessary.
1205 tl_assert(-1 != min_j); // Check we found a minimum.
1206 min_snapshot = & snapshots[ min_j ];
1207 if (VG_(clo_verbosity) > 1) {
floriane58e8a72012-10-20 19:57:16 +00001208 HChar buf[64];
njn734b8052007-11-01 04:40:37 +00001209 VG_(snprintf)(buf, 64, " %3d (t-span = %lld)", i, min_timespan);
1210 VERB_snapshot(2, buf, min_j);
1211 }
1212 delete_snapshot(min_snapshot);
1213 n_deleted++;
1214 }
1215
1216 // Slide down the remaining snapshots over the removed ones. First set i
1217 // to point to the first empty slot, and j to the first full slot after
1218 // i. Then slide everything down.
1219 for (i = 0; is_snapshot_in_use( &snapshots[i] ); i++) { }
1220 for (j = i; !is_snapshot_in_use( &snapshots[j] ); j++) { }
1221 for ( ; j < clo_max_snapshots; j++) {
1222 if (is_snapshot_in_use( &snapshots[j] )) {
1223 snapshots[i++] = snapshots[j];
1224 clear_snapshot(&snapshots[j], /*do_sanity_check*/True);
1225 }
1226 }
1227 next_snapshot_i = i;
1228
1229 // Check snapshots array looks ok after changes.
1230 sanity_check_snapshots_array();
1231
1232 // Find the minimum timespan remaining; that will be our new minimum
1233 // time interval. Note that above we were finding timespans by measuring
1234 // two intervals around a snapshot that was under consideration for
1235 // deletion. Here we only measure single intervals because all the
1236 // deletions have occurred.
1237 //
1238 // But we have to be careful -- some snapshots (eg. snapshot 0, and the
1239 // peak snapshot) are uncullable. If two uncullable snapshots end up
1240 // next to each other, they'll never be culled (assuming the peak doesn't
1241 // change), and the time gap between them will not change. However, the
1242 // time between the remaining cullable snapshots will grow ever larger.
1243 // This means that the min_timespan found will always be that between the
1244 // two uncullable snapshots, and it will be much smaller than it should
1245 // be. To avoid this problem, when computing the minimum timespan, we
1246 // ignore any timespans between two uncullable snapshots.
1247 tl_assert(next_snapshot_i > 1);
1248 min_timespan = 0x7fffffffffffffffLL;
1249 min_timespan_i = -1;
1250 for (i = 1; i < next_snapshot_i; i++) {
1251 if (is_uncullable_snapshot(&snapshots[i]) &&
1252 is_uncullable_snapshot(&snapshots[i-1]))
1253 {
sewardj5ea0f392009-07-15 14:51:34 +00001254 VERB(2, "(Ignoring interval %d--%d when computing minimum)\n", i-1, i);
njn734b8052007-11-01 04:40:37 +00001255 } else {
1256 Time timespan = snapshots[i].time - snapshots[i-1].time;
1257 tl_assert(timespan >= 0);
1258 if (timespan < min_timespan) {
1259 min_timespan = timespan;
1260 min_timespan_i = i;
1261 }
1262 }
1263 }
1264 tl_assert(-1 != min_timespan_i); // Check we found a minimum.
1265
1266 // Print remaining snapshots, if necessary.
1267 if (VG_(clo_verbosity) > 1) {
sewardj5ea0f392009-07-15 14:51:34 +00001268 VERB(2, "Finished culling (%3d of %3d deleted)\n",
njn734b8052007-11-01 04:40:37 +00001269 n_deleted, clo_max_snapshots);
1270 for (i = 0; i < next_snapshot_i; i++) {
1271 VERB_snapshot(2, " post-cull", i);
1272 }
sewardj5ea0f392009-07-15 14:51:34 +00001273 VERB(2, "New time interval = %lld (between snapshots %d and %d)\n",
njn734b8052007-11-01 04:40:37 +00001274 min_timespan, min_timespan_i-1, min_timespan_i);
1275 }
1276
1277 return min_timespan;
1278}
1279
1280static Time get_time(void)
1281{
1282 // Get current time, in whatever time unit we're using.
njn1a2741a2007-11-26 21:59:04 +00001283 if (clo_time_unit == TimeI) {
1284 return guest_instrs_executed;
1285 } else if (clo_time_unit == TimeMS) {
njn734b8052007-11-01 04:40:37 +00001286 // Some stuff happens between the millisecond timer being initialised
1287 // to zero and us taking our first snapshot. We determine that time
1288 // gap so we can subtract it from all subsequent times so that our
1289 // first snapshot is considered to be at t = 0ms. Unfortunately, a
1290 // bunch of symbols get read after the first snapshot is taken but
1291 // before the second one (which is triggered by the first allocation),
1292 // so when the time-unit is 'ms' we always have a big gap between the
1293 // first two snapshots. But at least users won't have to wonder why
1294 // the first snapshot isn't at t=0.
1295 static Bool is_first_get_time = True;
1296 static Time start_time_ms;
1297 if (is_first_get_time) {
1298 start_time_ms = VG_(read_millisecond_timer)();
1299 is_first_get_time = False;
1300 return 0;
1301 } else {
1302 return VG_(read_millisecond_timer)() - start_time_ms;
1303 }
1304 } else if (clo_time_unit == TimeB) {
1305 return total_allocs_deallocs_szB;
1306 } else {
1307 tl_assert2(0, "bad --time-unit value");
1308 }
1309}
1310
1311// Take a snapshot, and only that -- decisions on whether to take a
1312// snapshot, or what kind of snapshot, are made elsewhere.
njnf76d27a2009-05-28 01:53:07 +00001313// Nb: we call the arg "my_time" because "time" shadows a global declaration
1314// in /usr/include/time.h on Darwin.
njn734b8052007-11-01 04:40:37 +00001315static void
njnf76d27a2009-05-28 01:53:07 +00001316take_snapshot(Snapshot* snapshot, SnapshotKind kind, Time my_time,
njnefc13c22009-02-23 06:44:51 +00001317 Bool is_detailed)
njn734b8052007-11-01 04:40:37 +00001318{
1319 tl_assert(!is_snapshot_in_use(snapshot));
njne323a6b2010-07-01 02:35:03 +00001320 if (!clo_pages_as_heap) {
1321 tl_assert(have_started_executing_code);
1322 }
njn734b8052007-11-01 04:40:37 +00001323
1324 // Heap and heap admin.
1325 if (clo_heap) {
1326 snapshot->heap_szB = heap_szB;
1327 if (is_detailed) {
njn32397c02007-11-10 04:08:08 +00001328 SizeT total_szB = heap_szB + heap_extra_szB + stacks_szB;
njn734b8052007-11-01 04:40:37 +00001329 snapshot->alloc_sxpt = dup_XTree(alloc_xpt, total_szB);
1330 tl_assert( alloc_xpt->szB == heap_szB);
1331 tl_assert(snapshot->alloc_sxpt->szB == heap_szB);
1332 }
njn32397c02007-11-10 04:08:08 +00001333 snapshot->heap_extra_szB = heap_extra_szB;
njn734b8052007-11-01 04:40:37 +00001334 }
1335
1336 // Stack(s).
1337 if (clo_stacks) {
1338 snapshot->stacks_szB = stacks_szB;
1339 }
1340
1341 // Rest of snapshot.
1342 snapshot->kind = kind;
njnf76d27a2009-05-28 01:53:07 +00001343 snapshot->time = my_time;
njn734b8052007-11-01 04:40:37 +00001344 sanity_check_snapshot(snapshot);
1345
1346 // Update stats.
1347 if (Peak == kind) n_peak_snapshots++;
1348 if (is_detailed) n_detailed_snapshots++;
1349 n_real_snapshots++;
1350}
1351
1352
1353// Take a snapshot, if it's time, or if we've hit a peak.
1354static void
floriane58e8a72012-10-20 19:57:16 +00001355maybe_take_snapshot(SnapshotKind kind, const HChar* what)
njn734b8052007-11-01 04:40:37 +00001356{
1357 // 'min_time_interval' is the minimum time interval between snapshots.
1358 // If we try to take a snapshot and less than this much time has passed,
1359 // we don't take it. It gets larger as the program runs longer. It's
1360 // initialised to zero so that we begin by taking snapshots as quickly as
1361 // possible.
1362 static Time min_time_interval = 0;
1363 // Zero allows startup snapshot.
1364 static Time earliest_possible_time_of_next_snapshot = 0;
1365 static Int n_snapshots_since_last_detailed = 0;
1366 static Int n_skipped_snapshots_since_last_snapshot = 0;
1367
1368 Snapshot* snapshot;
1369 Bool is_detailed;
njnf76d27a2009-05-28 01:53:07 +00001370 // Nb: we call this variable "my_time" because "time" shadows a global
1371 // declaration in /usr/include/time.h on Darwin.
1372 Time my_time = get_time();
njn734b8052007-11-01 04:40:37 +00001373
1374 switch (kind) {
1375 case Normal:
1376 // Only do a snapshot if it's time.
njnf76d27a2009-05-28 01:53:07 +00001377 if (my_time < earliest_possible_time_of_next_snapshot) {
njn734b8052007-11-01 04:40:37 +00001378 n_skipped_snapshots++;
1379 n_skipped_snapshots_since_last_snapshot++;
1380 return;
1381 }
1382 is_detailed = (clo_detailed_freq-1 == n_snapshots_since_last_detailed);
1383 break;
1384
1385 case Peak: {
1386 // Because we're about to do a deallocation, we're coming down from a
1387 // local peak. If it is (a) actually a global peak, and (b) a certain
1388 // amount bigger than the previous peak, then we take a peak snapshot.
1389 // By not taking a snapshot for every peak, we save a lot of effort --
1390 // because many peaks remain peak only for a short time.
njn32397c02007-11-10 04:08:08 +00001391 SizeT total_szB = heap_szB + heap_extra_szB + stacks_szB;
njn734b8052007-11-01 04:40:37 +00001392 SizeT excess_szB_for_new_peak =
njn62721e92007-11-11 22:15:58 +00001393 (SizeT)((peak_snapshot_total_szB * clo_peak_inaccuracy) / 100);
njn734b8052007-11-01 04:40:37 +00001394 if (total_szB <= peak_snapshot_total_szB + excess_szB_for_new_peak) {
1395 return;
1396 }
1397 is_detailed = True;
1398 break;
1399 }
1400
1401 default:
1402 tl_assert2(0, "maybe_take_snapshot: unrecognised snapshot kind");
1403 }
1404
1405 // Take the snapshot.
1406 snapshot = & snapshots[next_snapshot_i];
njnf76d27a2009-05-28 01:53:07 +00001407 take_snapshot(snapshot, kind, my_time, is_detailed);
njn734b8052007-11-01 04:40:37 +00001408
1409 // Record if it was detailed.
1410 if (is_detailed) {
1411 n_snapshots_since_last_detailed = 0;
1412 } else {
1413 n_snapshots_since_last_detailed++;
1414 }
1415
1416 // Update peak data, if it's a Peak snapshot.
1417 if (Peak == kind) {
1418 Int i, number_of_peaks_snapshots_found = 0;
1419
1420 // Sanity check the size, then update our recorded peak.
1421 SizeT snapshot_total_szB =
njn32397c02007-11-10 04:08:08 +00001422 snapshot->heap_szB + snapshot->heap_extra_szB + snapshot->stacks_szB;
njn734b8052007-11-01 04:40:37 +00001423 tl_assert2(snapshot_total_szB > peak_snapshot_total_szB,
1424 "%ld, %ld\n", snapshot_total_szB, peak_snapshot_total_szB);
1425 peak_snapshot_total_szB = snapshot_total_szB;
1426
1427 // Find the old peak snapshot, if it exists, and mark it as normal.
1428 for (i = 0; i < next_snapshot_i; i++) {
1429 if (Peak == snapshots[i].kind) {
1430 snapshots[i].kind = Normal;
1431 number_of_peaks_snapshots_found++;
1432 }
1433 }
1434 tl_assert(number_of_peaks_snapshots_found <= 1);
1435 }
1436
1437 // Finish up verbosity and stats stuff.
1438 if (n_skipped_snapshots_since_last_snapshot > 0) {
sewardj5ea0f392009-07-15 14:51:34 +00001439 VERB(2, " (skipped %d snapshot%s)\n",
njn734b8052007-11-01 04:40:37 +00001440 n_skipped_snapshots_since_last_snapshot,
njn84f32b22009-02-10 07:14:37 +00001441 ( 1 == n_skipped_snapshots_since_last_snapshot ? "" : "s") );
njn734b8052007-11-01 04:40:37 +00001442 }
1443 VERB_snapshot(2, what, next_snapshot_i);
1444 n_skipped_snapshots_since_last_snapshot = 0;
1445
1446 // Cull the entries, if our snapshot table is full.
1447 next_snapshot_i++;
1448 if (clo_max_snapshots == next_snapshot_i) {
1449 min_time_interval = cull_snapshots();
1450 }
1451
1452 // Work out the earliest time when the next snapshot can happen.
njnf76d27a2009-05-28 01:53:07 +00001453 earliest_possible_time_of_next_snapshot = my_time + min_time_interval;
njn734b8052007-11-01 04:40:37 +00001454}
1455
1456
1457//------------------------------------------------------------//
1458//--- Sanity checking ---//
1459//------------------------------------------------------------//
1460
1461static Bool ms_cheap_sanity_check ( void )
1462{
1463 return True; // Nothing useful we can cheaply check.
1464}
1465
1466static Bool ms_expensive_sanity_check ( void )
1467{
1468 sanity_check_XTree(alloc_xpt, /*parent*/NULL);
1469 sanity_check_snapshots_array();
1470 return True;
1471}
1472
1473
1474//------------------------------------------------------------//
1475//--- Heap management ---//
1476//------------------------------------------------------------//
1477
1478// Metadata for heap blocks. Each one contains a pointer to a bottom-XPt,
1479// which is a foothold into the XCon at which it was allocated. From
1480// HP_Chunks, XPt 'space' fields are incremented (at allocation) and
1481// decremented (at deallocation).
1482//
1483// Nb: first two fields must match core's VgHashNode.
1484typedef
1485 struct _HP_Chunk {
1486 struct _HP_Chunk* next;
njn32397c02007-11-10 04:08:08 +00001487 Addr data; // Ptr to actual block
1488 SizeT req_szB; // Size requested
1489 SizeT slop_szB; // Extra bytes given above those requested
1490 XPt* where; // Where allocated; bottom-XPt
njn734b8052007-11-01 04:40:37 +00001491 }
1492 HP_Chunk;
1493
1494static VgHashTable malloc_list = NULL; // HP_Chunks
1495
1496static void update_alloc_stats(SSizeT szB_delta)
1497{
1498 // Update total_allocs_deallocs_szB.
1499 if (szB_delta < 0) szB_delta = -szB_delta;
1500 total_allocs_deallocs_szB += szB_delta;
1501}
1502
njn32397c02007-11-10 04:08:08 +00001503static void update_heap_stats(SSizeT heap_szB_delta, Int heap_extra_szB_delta)
njn734b8052007-11-01 04:40:37 +00001504{
njn32397c02007-11-10 04:08:08 +00001505 if (heap_szB_delta < 0)
1506 tl_assert(heap_szB >= -heap_szB_delta);
1507 if (heap_extra_szB_delta < 0)
1508 tl_assert(heap_extra_szB >= -heap_extra_szB_delta);
njn734b8052007-11-01 04:40:37 +00001509
njn32397c02007-11-10 04:08:08 +00001510 heap_extra_szB += heap_extra_szB_delta;
1511 heap_szB += heap_szB_delta;
1512
1513 update_alloc_stats(heap_szB_delta + heap_extra_szB_delta);
njn734b8052007-11-01 04:40:37 +00001514}
nethercotec9f36922004-02-14 16:40:02 +00001515
nethercote159dfef2004-09-13 13:27:30 +00001516static
njne323a6b2010-07-01 02:35:03 +00001517void* record_block( ThreadId tid, void* p, SizeT req_szB, SizeT slop_szB,
1518 Bool exclude_first_entry, Bool maybe_snapshot )
nethercotec9f36922004-02-14 16:40:02 +00001519{
njnf1c5def2005-08-11 02:17:07 +00001520 // Make new HP_Chunk node, add to malloc_list
njne323a6b2010-07-01 02:35:03 +00001521 HP_Chunk* hc = VG_(malloc)("ms.main.rb.1", sizeof(HP_Chunk));
njn32397c02007-11-10 04:08:08 +00001522 hc->req_szB = req_szB;
1523 hc->slop_szB = slop_szB;
1524 hc->data = (Addr)p;
1525 hc->where = NULL;
njn246a9d22005-08-14 06:24:20 +00001526 VG_(HT_add_node)(malloc_list, hc);
nethercote57e36b32004-07-10 14:56:28 +00001527
njn734b8052007-11-01 04:40:37 +00001528 if (clo_heap) {
njne323a6b2010-07-01 02:35:03 +00001529 VERB(3, "<<< record_block (%lu, %lu)\n", req_szB, slop_szB);
njn734b8052007-11-01 04:40:37 +00001530
njne323a6b2010-07-01 02:35:03 +00001531 hc->where = get_XCon( tid, exclude_first_entry );
njn734b8052007-11-01 04:40:37 +00001532
njnf6b00762009-04-17 04:26:41 +00001533 if (hc->where) {
1534 // Update statistics.
1535 n_heap_allocs++;
1536
1537 // Update heap stats.
1538 update_heap_stats(req_szB, clo_heap_admin + slop_szB);
1539
1540 // Update XTree.
1541 update_XCon(hc->where, req_szB);
1542
1543 // Maybe take a snapshot.
njne323a6b2010-07-01 02:35:03 +00001544 if (maybe_snapshot) {
1545 maybe_take_snapshot(Normal, " alloc");
1546 }
njnf6b00762009-04-17 04:26:41 +00001547
1548 } else {
1549 // Ignored allocation.
1550 n_ignored_heap_allocs++;
1551
sewardj5ea0f392009-07-15 14:51:34 +00001552 VERB(3, "(ignored)\n");
njnf6b00762009-04-17 04:26:41 +00001553 }
njn734b8052007-11-01 04:40:37 +00001554
sewardj5ea0f392009-07-15 14:51:34 +00001555 VERB(3, ">>>\n");
njn734b8052007-11-01 04:40:37 +00001556 }
nethercotec9f36922004-02-14 16:40:02 +00001557
nethercotec9f36922004-02-14 16:40:02 +00001558 return p;
1559}
1560
1561static __inline__
njne323a6b2010-07-01 02:35:03 +00001562void* alloc_and_record_block ( ThreadId tid, SizeT req_szB, SizeT req_alignB,
1563 Bool is_zeroed )
1564{
1565 SizeT actual_szB, slop_szB;
1566 void* p;
1567
1568 if ((SSizeT)req_szB < 0) return NULL;
1569
1570 // Allocate and zero if necessary.
1571 p = VG_(cli_malloc)( req_alignB, req_szB );
1572 if (!p) {
1573 return NULL;
1574 }
1575 if (is_zeroed) VG_(memset)(p, 0, req_szB);
1576 actual_szB = VG_(malloc_usable_size)(p);
1577 tl_assert(actual_szB >= req_szB);
1578 slop_szB = actual_szB - req_szB;
1579
1580 // Record block.
1581 record_block(tid, p, req_szB, slop_szB, /*exclude_first_entry*/True,
1582 /*maybe_snapshot*/True);
1583
1584 return p;
1585}
1586
1587static __inline__
1588void unrecord_block ( void* p, Bool maybe_snapshot )
nethercotec9f36922004-02-14 16:40:02 +00001589{
njnf1c5def2005-08-11 02:17:07 +00001590 // Remove HP_Chunk from malloc_list
njn7081c1a2009-05-19 01:53:30 +00001591 HP_Chunk* hc = VG_(HT_remove)(malloc_list, (UWord)p);
njn734b8052007-11-01 04:40:37 +00001592 if (NULL == hc) {
njn5cc5d7e2005-08-11 02:09:25 +00001593 return; // must have been a bogus free()
njn734b8052007-11-01 04:40:37 +00001594 }
nethercotec9f36922004-02-14 16:40:02 +00001595
njn734b8052007-11-01 04:40:37 +00001596 if (clo_heap) {
njne323a6b2010-07-01 02:35:03 +00001597 VERB(3, "<<< unrecord_block\n");
nethercotec9f36922004-02-14 16:40:02 +00001598
njnf6b00762009-04-17 04:26:41 +00001599 if (hc->where) {
1600 // Update statistics.
1601 n_heap_frees++;
nethercote57e36b32004-07-10 14:56:28 +00001602
njnf6b00762009-04-17 04:26:41 +00001603 // Maybe take a peak snapshot, since it's a deallocation.
njne323a6b2010-07-01 02:35:03 +00001604 if (maybe_snapshot) {
1605 maybe_take_snapshot(Peak, "de-PEAK");
1606 }
njn734b8052007-11-01 04:40:37 +00001607
njnf6b00762009-04-17 04:26:41 +00001608 // Update heap stats.
1609 update_heap_stats(-hc->req_szB, -clo_heap_admin - hc->slop_szB);
njn734b8052007-11-01 04:40:37 +00001610
njnf6b00762009-04-17 04:26:41 +00001611 // Update XTree.
1612 update_XCon(hc->where, -hc->req_szB);
njn734b8052007-11-01 04:40:37 +00001613
njnf6b00762009-04-17 04:26:41 +00001614 // Maybe take a snapshot.
njne323a6b2010-07-01 02:35:03 +00001615 if (maybe_snapshot) {
1616 maybe_take_snapshot(Normal, "dealloc");
1617 }
njnf6b00762009-04-17 04:26:41 +00001618
1619 } else {
1620 n_ignored_heap_frees++;
1621
sewardj5ea0f392009-07-15 14:51:34 +00001622 VERB(3, "(ignored)\n");
njnf6b00762009-04-17 04:26:41 +00001623 }
njn734b8052007-11-01 04:40:37 +00001624
sewardj5ea0f392009-07-15 14:51:34 +00001625 VERB(3, ">>> (-%lu, -%lu)\n", hc->req_szB, hc->slop_szB);
njn734b8052007-11-01 04:40:37 +00001626 }
1627
1628 // Actually free the chunk, and the heap block (if necessary)
1629 VG_(free)( hc ); hc = NULL;
nethercotec9f36922004-02-14 16:40:02 +00001630}
1631
njnf6b00762009-04-17 04:26:41 +00001632// Nb: --ignore-fn is tricky for realloc. If the block's original alloc was
1633// ignored, but the realloc is not requested to be ignored, and we are
1634// shrinking the block, then we have to ignore the realloc -- otherwise we
1635// could end up with negative heap sizes. This isn't a danger if we are
1636// growing such a block, but for consistency (it also simplifies things) we
1637// ignore such reallocs as well.
njn734b8052007-11-01 04:40:37 +00001638static __inline__
njne323a6b2010-07-01 02:35:03 +00001639void* realloc_block ( ThreadId tid, void* p_old, SizeT new_req_szB )
nethercotec9f36922004-02-14 16:40:02 +00001640{
njn734b8052007-11-01 04:40:37 +00001641 HP_Chunk* hc;
1642 void* p_new;
njn32397c02007-11-10 04:08:08 +00001643 SizeT old_req_szB, old_slop_szB, new_slop_szB, new_actual_szB;
njn734b8052007-11-01 04:40:37 +00001644 XPt *old_where, *new_where;
njnf6b00762009-04-17 04:26:41 +00001645 Bool is_ignored = False;
njn734b8052007-11-01 04:40:37 +00001646
1647 // Remove the old block
1648 hc = VG_(HT_remove)(malloc_list, (UWord)p_old);
1649 if (hc == NULL) {
1650 return NULL; // must have been a bogus realloc()
1651 }
1652
njn32397c02007-11-10 04:08:08 +00001653 old_req_szB = hc->req_szB;
1654 old_slop_szB = hc->slop_szB;
njn734b8052007-11-01 04:40:37 +00001655
njne323a6b2010-07-01 02:35:03 +00001656 tl_assert(!clo_pages_as_heap); // Shouldn't be here if --pages-as-heap=yes.
njn734b8052007-11-01 04:40:37 +00001657 if (clo_heap) {
njne323a6b2010-07-01 02:35:03 +00001658 VERB(3, "<<< realloc_block (%lu)\n", new_req_szB);
njn734b8052007-11-01 04:40:37 +00001659
njnf6b00762009-04-17 04:26:41 +00001660 if (hc->where) {
1661 // Update statistics.
1662 n_heap_reallocs++;
njn734b8052007-11-01 04:40:37 +00001663
njnf6b00762009-04-17 04:26:41 +00001664 // Maybe take a peak snapshot, if it's (effectively) a deallocation.
1665 if (new_req_szB < old_req_szB) {
1666 maybe_take_snapshot(Peak, "re-PEAK");
1667 }
1668 } else {
1669 // The original malloc was ignored, so we have to ignore the
1670 // realloc as well.
1671 is_ignored = True;
njn734b8052007-11-01 04:40:37 +00001672 }
njn734b8052007-11-01 04:40:37 +00001673 }
1674
1675 // Actually do the allocation, if necessary.
njn32397c02007-11-10 04:08:08 +00001676 if (new_req_szB <= old_req_szB + old_slop_szB) {
1677 // New size is smaller or same; block not moved.
njn734b8052007-11-01 04:40:37 +00001678 p_new = p_old;
njn32397c02007-11-10 04:08:08 +00001679 new_slop_szB = old_slop_szB + (old_req_szB - new_req_szB);
njn734b8052007-11-01 04:40:37 +00001680
1681 } else {
njn32397c02007-11-10 04:08:08 +00001682 // New size is bigger; make new block, copy shared contents, free old.
1683 p_new = VG_(cli_malloc)(VG_(clo_alignment), new_req_szB);
1684 if (!p_new) {
1685 // Nb: if realloc fails, NULL is returned but the old block is not
1686 // touched. What an awful function.
1687 return NULL;
njn734b8052007-11-01 04:40:37 +00001688 }
njndfa408c2011-08-08 01:58:50 +00001689 VG_(memcpy)(p_new, p_old, old_req_szB + old_slop_szB);
njn32397c02007-11-10 04:08:08 +00001690 VG_(cli_free)(p_old);
1691 new_actual_szB = VG_(malloc_usable_size)(p_new);
1692 tl_assert(new_actual_szB >= new_req_szB);
1693 new_slop_szB = new_actual_szB - new_req_szB;
njn734b8052007-11-01 04:40:37 +00001694 }
1695
1696 if (p_new) {
1697 // Update HP_Chunk.
njn32397c02007-11-10 04:08:08 +00001698 hc->data = (Addr)p_new;
1699 hc->req_szB = new_req_szB;
1700 hc->slop_szB = new_slop_szB;
1701 old_where = hc->where;
1702 hc->where = NULL;
njn734b8052007-11-01 04:40:37 +00001703
1704 // Update XTree.
1705 if (clo_heap) {
njne323a6b2010-07-01 02:35:03 +00001706 new_where = get_XCon( tid, /*exclude_first_entry*/True);
njnf6b00762009-04-17 04:26:41 +00001707 if (!is_ignored && new_where) {
1708 hc->where = new_where;
1709 update_XCon(old_where, -old_req_szB);
1710 update_XCon(new_where, new_req_szB);
1711 } else {
1712 // The realloc itself is ignored.
1713 is_ignored = True;
1714
1715 // Update statistics.
1716 n_ignored_heap_reallocs++;
1717 }
njn734b8052007-11-01 04:40:37 +00001718 }
1719 }
1720
1721 // Now insert the new hc (with a possibly new 'data' field) into
1722 // malloc_list. If this realloc() did not increase the memory size, we
1723 // will have removed and then re-added hc unnecessarily. But that's ok
1724 // because shrinking a block with realloc() is (presumably) much rarer
1725 // than growing it, and this way simplifies the growing case.
1726 VG_(HT_add_node)(malloc_list, hc);
1727
njn734b8052007-11-01 04:40:37 +00001728 if (clo_heap) {
njnf6b00762009-04-17 04:26:41 +00001729 if (!is_ignored) {
1730 // Update heap stats.
1731 update_heap_stats(new_req_szB - old_req_szB,
1732 new_slop_szB - old_slop_szB);
njn32397c02007-11-10 04:08:08 +00001733
njnf6b00762009-04-17 04:26:41 +00001734 // Maybe take a snapshot.
1735 maybe_take_snapshot(Normal, "realloc");
1736 } else {
1737
sewardj5ea0f392009-07-15 14:51:34 +00001738 VERB(3, "(ignored)\n");
njnf6b00762009-04-17 04:26:41 +00001739 }
njn734b8052007-11-01 04:40:37 +00001740
sewardj5ea0f392009-07-15 14:51:34 +00001741 VERB(3, ">>> (%ld, %ld)\n",
njn32397c02007-11-10 04:08:08 +00001742 new_req_szB - old_req_szB, new_slop_szB - old_slop_szB);
njn734b8052007-11-01 04:40:37 +00001743 }
1744
1745 return p_new;
nethercotec9f36922004-02-14 16:40:02 +00001746}
1747
njn734b8052007-11-01 04:40:37 +00001748
1749//------------------------------------------------------------//
1750//--- malloc() et al replacement wrappers ---//
1751//------------------------------------------------------------//
1752
1753static void* ms_malloc ( ThreadId tid, SizeT szB )
nethercotec9f36922004-02-14 16:40:02 +00001754{
njne323a6b2010-07-01 02:35:03 +00001755 return alloc_and_record_block( tid, szB, VG_(clo_alignment), /*is_zeroed*/False );
nethercotec9f36922004-02-14 16:40:02 +00001756}
1757
njn734b8052007-11-01 04:40:37 +00001758static void* ms___builtin_new ( ThreadId tid, SizeT szB )
nethercotec9f36922004-02-14 16:40:02 +00001759{
njne323a6b2010-07-01 02:35:03 +00001760 return alloc_and_record_block( tid, szB, VG_(clo_alignment), /*is_zeroed*/False );
nethercotec9f36922004-02-14 16:40:02 +00001761}
1762
njn734b8052007-11-01 04:40:37 +00001763static void* ms___builtin_vec_new ( ThreadId tid, SizeT szB )
fitzhardinge51f3ff12004-03-04 22:42:03 +00001764{
njne323a6b2010-07-01 02:35:03 +00001765 return alloc_and_record_block( tid, szB, VG_(clo_alignment), /*is_zeroed*/False );
njn734b8052007-11-01 04:40:37 +00001766}
1767
1768static void* ms_calloc ( ThreadId tid, SizeT m, SizeT szB )
1769{
njne323a6b2010-07-01 02:35:03 +00001770 return alloc_and_record_block( tid, m*szB, VG_(clo_alignment), /*is_zeroed*/True );
njn734b8052007-11-01 04:40:37 +00001771}
1772
1773static void *ms_memalign ( ThreadId tid, SizeT alignB, SizeT szB )
1774{
njne323a6b2010-07-01 02:35:03 +00001775 return alloc_and_record_block( tid, szB, alignB, False );
fitzhardinge51f3ff12004-03-04 22:42:03 +00001776}
1777
njnefc13c22009-02-23 06:44:51 +00001778static void ms_free ( ThreadId tid __attribute__((unused)), void* p )
nethercotec9f36922004-02-14 16:40:02 +00001779{
njne323a6b2010-07-01 02:35:03 +00001780 unrecord_block(p, /*maybe_snapshot*/True);
1781 VG_(cli_free)(p);
nethercotec9f36922004-02-14 16:40:02 +00001782}
1783
njn51d827b2005-05-09 01:02:08 +00001784static void ms___builtin_delete ( ThreadId tid, void* p )
nethercotec9f36922004-02-14 16:40:02 +00001785{
njne323a6b2010-07-01 02:35:03 +00001786 unrecord_block(p, /*maybe_snapshot*/True);
1787 VG_(cli_free)(p);
nethercotec9f36922004-02-14 16:40:02 +00001788}
1789
njn51d827b2005-05-09 01:02:08 +00001790static void ms___builtin_vec_delete ( ThreadId tid, void* p )
nethercotec9f36922004-02-14 16:40:02 +00001791{
njne323a6b2010-07-01 02:35:03 +00001792 unrecord_block(p, /*maybe_snapshot*/True);
1793 VG_(cli_free)(p);
nethercotec9f36922004-02-14 16:40:02 +00001794}
1795
njn734b8052007-11-01 04:40:37 +00001796static void* ms_realloc ( ThreadId tid, void* p_old, SizeT new_szB )
nethercotec9f36922004-02-14 16:40:02 +00001797{
njne323a6b2010-07-01 02:35:03 +00001798 return realloc_block(tid, p_old, new_szB);
nethercotec9f36922004-02-14 16:40:02 +00001799}
1800
njnefc13c22009-02-23 06:44:51 +00001801static SizeT ms_malloc_usable_size ( ThreadId tid, void* p )
njn8b140de2009-02-17 04:31:18 +00001802{
1803 HP_Chunk* hc = VG_(HT_lookup)( malloc_list, (UWord)p );
1804
1805 return ( hc ? hc->req_szB + hc->slop_szB : 0 );
1806}
nethercotec9f36922004-02-14 16:40:02 +00001807
njn734b8052007-11-01 04:40:37 +00001808//------------------------------------------------------------//
njne323a6b2010-07-01 02:35:03 +00001809//--- Page handling ---//
1810//------------------------------------------------------------//
1811
1812static
1813void ms_record_page_mem ( Addr a, SizeT len )
1814{
1815 ThreadId tid = VG_(get_running_tid)();
1816 Addr end;
1817 tl_assert(VG_IS_PAGE_ALIGNED(len));
1818 tl_assert(len >= VKI_PAGE_SIZE);
1819 // Record the first N-1 pages as blocks, but don't do any snapshots.
1820 for (end = a + len - VKI_PAGE_SIZE; a < end; a += VKI_PAGE_SIZE) {
1821 record_block( tid, (void*)a, VKI_PAGE_SIZE, /*slop_szB*/0,
1822 /*exclude_first_entry*/False, /*maybe_snapshot*/False );
1823 }
1824 // Record the last page as a block, and maybe do a snapshot afterwards.
1825 record_block( tid, (void*)a, VKI_PAGE_SIZE, /*slop_szB*/0,
1826 /*exclude_first_entry*/False, /*maybe_snapshot*/True );
1827}
1828
1829static
1830void ms_unrecord_page_mem( Addr a, SizeT len )
1831{
1832 Addr end;
1833 tl_assert(VG_IS_PAGE_ALIGNED(len));
1834 tl_assert(len >= VKI_PAGE_SIZE);
1835 for (end = a + len - VKI_PAGE_SIZE; a < end; a += VKI_PAGE_SIZE) {
1836 unrecord_block((void*)a, /*maybe_snapshot*/False);
1837 }
1838 unrecord_block((void*)a, /*maybe_snapshot*/True);
1839}
1840
1841//------------------------------------------------------------//
1842
1843static
1844void ms_new_mem_mmap ( Addr a, SizeT len,
1845 Bool rr, Bool ww, Bool xx, ULong di_handle )
1846{
1847 tl_assert(VG_IS_PAGE_ALIGNED(len));
1848 ms_record_page_mem(a, len);
1849}
1850
1851static
1852void ms_new_mem_startup( Addr a, SizeT len,
1853 Bool rr, Bool ww, Bool xx, ULong di_handle )
1854{
1855 // startup maps are always be page-sized, except the trampoline page is
1856 // marked by the core as only being the size of the trampoline itself,
1857 // which is something like 57 bytes. Round it up to page size.
1858 len = VG_PGROUNDUP(len);
1859 ms_record_page_mem(a, len);
1860}
1861
1862static
1863void ms_new_mem_brk ( Addr a, SizeT len, ThreadId tid )
1864{
philippee6a26cc2012-05-01 20:02:30 +00001865 // brk limit is not necessarily aligned on a page boundary.
1866 // If new memory being brk-ed implies to allocate a new page,
1867 // then call ms_record_page_mem with page aligned parameters
1868 // otherwise just ignore.
1869 Addr old_bottom_page = VG_PGROUNDDN(a - 1);
1870 Addr new_top_page = VG_PGROUNDDN(a + len - 1);
1871 if (old_bottom_page != new_top_page)
1872 ms_record_page_mem(VG_PGROUNDDN(a),
1873 (new_top_page - old_bottom_page));
njne323a6b2010-07-01 02:35:03 +00001874}
1875
1876static
1877void ms_copy_mem_remap( Addr from, Addr to, SizeT len)
1878{
1879 tl_assert(VG_IS_PAGE_ALIGNED(len));
1880 ms_unrecord_page_mem(from, len);
1881 ms_record_page_mem(to, len);
1882}
1883
1884static
1885void ms_die_mem_munmap( Addr a, SizeT len )
1886{
1887 tl_assert(VG_IS_PAGE_ALIGNED(len));
1888 ms_unrecord_page_mem(a, len);
1889}
1890
1891static
1892void ms_die_mem_brk( Addr a, SizeT len )
1893{
philippee6a26cc2012-05-01 20:02:30 +00001894 // Call ms_unrecord_page_mem only if one or more pages are de-allocated.
1895 // See ms_new_mem_brk for more details.
1896 Addr new_bottom_page = VG_PGROUNDDN(a - 1);
1897 Addr old_top_page = VG_PGROUNDDN(a + len - 1);
1898 if (old_top_page != new_bottom_page)
1899 ms_unrecord_page_mem(VG_PGROUNDDN(a),
1900 (old_top_page - new_bottom_page));
1901
njne323a6b2010-07-01 02:35:03 +00001902}
1903
1904//------------------------------------------------------------//
njn734b8052007-11-01 04:40:37 +00001905//--- Stacks ---//
1906//------------------------------------------------------------//
nethercotec9f36922004-02-14 16:40:02 +00001907
njn734b8052007-11-01 04:40:37 +00001908// We really want the inlining to occur...
1909#define INLINE inline __attribute__((always_inline))
nethercotec9f36922004-02-14 16:40:02 +00001910
njn734b8052007-11-01 04:40:37 +00001911static void update_stack_stats(SSizeT stack_szB_delta)
nethercotec9f36922004-02-14 16:40:02 +00001912{
njn734b8052007-11-01 04:40:37 +00001913 if (stack_szB_delta < 0) tl_assert(stacks_szB >= -stack_szB_delta);
1914 stacks_szB += stack_szB_delta;
nethercotec9f36922004-02-14 16:40:02 +00001915
njn734b8052007-11-01 04:40:37 +00001916 update_alloc_stats(stack_szB_delta);
nethercotec9f36922004-02-14 16:40:02 +00001917}
1918
floriane58e8a72012-10-20 19:57:16 +00001919static INLINE void new_mem_stack_2(SizeT len, const HChar* what)
nethercotec9f36922004-02-14 16:40:02 +00001920{
njn734b8052007-11-01 04:40:37 +00001921 if (have_started_executing_code) {
sewardj5ea0f392009-07-15 14:51:34 +00001922 VERB(3, "<<< new_mem_stack (%ld)\n", len);
njn734b8052007-11-01 04:40:37 +00001923 n_stack_allocs++;
1924 update_stack_stats(len);
1925 maybe_take_snapshot(Normal, what);
sewardj5ea0f392009-07-15 14:51:34 +00001926 VERB(3, ">>>\n");
nethercotec9f36922004-02-14 16:40:02 +00001927 }
nethercotec9f36922004-02-14 16:40:02 +00001928}
1929
floriane58e8a72012-10-20 19:57:16 +00001930static INLINE void die_mem_stack_2(SizeT len, const HChar* what)
nethercotec9f36922004-02-14 16:40:02 +00001931{
njn734b8052007-11-01 04:40:37 +00001932 if (have_started_executing_code) {
sewardj5ea0f392009-07-15 14:51:34 +00001933 VERB(3, "<<< die_mem_stack (%ld)\n", -len);
njn734b8052007-11-01 04:40:37 +00001934 n_stack_frees++;
1935 maybe_take_snapshot(Peak, "stkPEAK");
1936 update_stack_stats(-len);
1937 maybe_take_snapshot(Normal, what);
sewardj5ea0f392009-07-15 14:51:34 +00001938 VERB(3, ">>>\n");
nethercotec9f36922004-02-14 16:40:02 +00001939 }
nethercotec9f36922004-02-14 16:40:02 +00001940}
1941
njn734b8052007-11-01 04:40:37 +00001942static void new_mem_stack(Addr a, SizeT len)
nethercotec9f36922004-02-14 16:40:02 +00001943{
njnefc13c22009-02-23 06:44:51 +00001944 new_mem_stack_2(len, "stk-new");
njn734b8052007-11-01 04:40:37 +00001945}
nethercotec9f36922004-02-14 16:40:02 +00001946
njn734b8052007-11-01 04:40:37 +00001947static void die_mem_stack(Addr a, SizeT len)
1948{
njnefc13c22009-02-23 06:44:51 +00001949 die_mem_stack_2(len, "stk-die");
njn734b8052007-11-01 04:40:37 +00001950}
nethercotec9f36922004-02-14 16:40:02 +00001951
sewardj7cf4e6b2008-05-01 20:24:26 +00001952static void new_mem_stack_signal(Addr a, SizeT len, ThreadId tid)
nethercotec9f36922004-02-14 16:40:02 +00001953{
njnefc13c22009-02-23 06:44:51 +00001954 new_mem_stack_2(len, "sig-new");
nethercotec9f36922004-02-14 16:40:02 +00001955}
1956
nethercote8b5f40c2004-11-02 13:29:50 +00001957static void die_mem_stack_signal(Addr a, SizeT len)
nethercotec9f36922004-02-14 16:40:02 +00001958{
njnefc13c22009-02-23 06:44:51 +00001959 die_mem_stack_2(len, "sig-die");
nethercotec9f36922004-02-14 16:40:02 +00001960}
1961
njn734b8052007-11-01 04:40:37 +00001962
1963//------------------------------------------------------------//
1964//--- Client Requests ---//
1965//------------------------------------------------------------//
nethercotec9f36922004-02-14 16:40:02 +00001966
sewardj3b290482011-05-06 21:02:55 +00001967static void print_monitor_help ( void )
1968{
1969 VG_(gdb_printf) ("\n");
1970 VG_(gdb_printf) ("massif monitor commands:\n");
sewardj30b3eca2011-06-28 08:20:39 +00001971 VG_(gdb_printf) (" snapshot [<filename>]\n");
1972 VG_(gdb_printf) (" detailed_snapshot [<filename>]\n");
sewardjd142f992011-05-17 17:15:07 +00001973 VG_(gdb_printf) (" takes a snapshot (or a detailed snapshot)\n");
1974 VG_(gdb_printf) (" and saves it in <filename>\n");
sewardj3b290482011-05-06 21:02:55 +00001975 VG_(gdb_printf) (" default <filename> is massif.vgdb.out\n");
sewardj3b290482011-05-06 21:02:55 +00001976 VG_(gdb_printf) ("\n");
1977}
1978
1979
1980/* Forward declaration.
1981 return True if request recognised, False otherwise */
florian19f91bb2012-11-10 22:29:54 +00001982static Bool handle_gdb_monitor_command (ThreadId tid, HChar *req);
njn51d827b2005-05-09 01:02:08 +00001983static Bool ms_handle_client_request ( ThreadId tid, UWord* argv, UWord* ret )
nethercotec9f36922004-02-14 16:40:02 +00001984{
1985 switch (argv[0]) {
1986 case VG_USERREQ__MALLOCLIKE_BLOCK: {
njn734b8052007-11-01 04:40:37 +00001987 void* p = (void*)argv[1];
1988 SizeT szB = argv[2];
njne323a6b2010-07-01 02:35:03 +00001989 record_block( tid, p, szB, /*slop_szB*/0, /*exclude_first_entry*/False,
1990 /*maybe_snapshot*/True );
njn734b8052007-11-01 04:40:37 +00001991 *ret = 0;
nethercotec9f36922004-02-14 16:40:02 +00001992 return True;
1993 }
bart91347382011-03-25 20:07:25 +00001994 case VG_USERREQ__RESIZEINPLACE_BLOCK: {
1995 void* p = (void*)argv[1];
1996 SizeT newSizeB = argv[3];
1997
1998 unrecord_block(p, /*maybe_snapshot*/True);
1999 record_block(tid, p, newSizeB, /*slop_szB*/0,
2000 /*exclude_first_entry*/False, /*maybe_snapshot*/True);
2001 return True;
2002 }
nethercotec9f36922004-02-14 16:40:02 +00002003 case VG_USERREQ__FREELIKE_BLOCK: {
njn734b8052007-11-01 04:40:37 +00002004 void* p = (void*)argv[1];
njne323a6b2010-07-01 02:35:03 +00002005 unrecord_block(p, /*maybe_snapshot*/True);
njn734b8052007-11-01 04:40:37 +00002006 *ret = 0;
nethercotec9f36922004-02-14 16:40:02 +00002007 return True;
2008 }
sewardj3b290482011-05-06 21:02:55 +00002009 case VG_USERREQ__GDB_MONITOR_COMMAND: {
florian19f91bb2012-11-10 22:29:54 +00002010 Bool handled = handle_gdb_monitor_command (tid, (HChar*)argv[1]);
sewardj3b290482011-05-06 21:02:55 +00002011 if (handled)
2012 *ret = 1;
2013 else
2014 *ret = 0;
2015 return handled;
2016 }
2017
nethercotec9f36922004-02-14 16:40:02 +00002018 default:
2019 *ret = 0;
2020 return False;
2021 }
2022}
2023
njn734b8052007-11-01 04:40:37 +00002024//------------------------------------------------------------//
2025//--- Instrumentation ---//
2026//------------------------------------------------------------//
nethercotec9f36922004-02-14 16:40:02 +00002027
njn1a2741a2007-11-26 21:59:04 +00002028static void add_counter_update(IRSB* sbOut, Int n)
2029{
2030 #if defined(VG_BIGENDIAN)
2031 # define END Iend_BE
2032 #elif defined(VG_LITTLEENDIAN)
2033 # define END Iend_LE
2034 #else
2035 # error "Unknown endianness"
2036 #endif
2037 // Add code to increment 'guest_instrs_executed' by 'n', like this:
2038 // WrTmp(t1, Load64(&guest_instrs_executed))
2039 // WrTmp(t2, Add64(RdTmp(t1), Const(n)))
2040 // Store(&guest_instrs_executed, t2)
2041 IRTemp t1 = newIRTemp(sbOut->tyenv, Ity_I64);
2042 IRTemp t2 = newIRTemp(sbOut->tyenv, Ity_I64);
2043 IRExpr* counter_addr = mkIRExpr_HWord( (HWord)&guest_instrs_executed );
2044
sewardjdb5907d2009-11-26 17:20:21 +00002045 IRStmt* st1 = IRStmt_WrTmp(t1, IRExpr_Load(END, Ity_I64, counter_addr));
njn1a2741a2007-11-26 21:59:04 +00002046 IRStmt* st2 =
2047 IRStmt_WrTmp(t2,
2048 IRExpr_Binop(Iop_Add64, IRExpr_RdTmp(t1),
2049 IRExpr_Const(IRConst_U64(n))));
sewardjdb5907d2009-11-26 17:20:21 +00002050 IRStmt* st3 = IRStmt_Store(END, counter_addr, IRExpr_RdTmp(t2));
njn1a2741a2007-11-26 21:59:04 +00002051
2052 addStmtToIRSB( sbOut, st1 );
2053 addStmtToIRSB( sbOut, st2 );
2054 addStmtToIRSB( sbOut, st3 );
2055}
2056
2057static IRSB* ms_instrument2( IRSB* sbIn )
2058{
2059 Int i, n = 0;
2060 IRSB* sbOut;
2061
2062 // We increment the instruction count in two places:
2063 // - just before any Ist_Exit statements;
2064 // - just before the IRSB's end.
2065 // In the former case, we zero 'n' and then continue instrumenting.
2066
2067 sbOut = deepCopyIRSBExceptStmts(sbIn);
2068
2069 for (i = 0; i < sbIn->stmts_used; i++) {
2070 IRStmt* st = sbIn->stmts[i];
2071
2072 if (!st || st->tag == Ist_NoOp) continue;
2073
2074 if (st->tag == Ist_IMark) {
2075 n++;
2076 } else if (st->tag == Ist_Exit) {
2077 if (n > 0) {
2078 // Add an increment before the Exit statement, then reset 'n'.
2079 add_counter_update(sbOut, n);
2080 n = 0;
2081 }
2082 }
2083 addStmtToIRSB( sbOut, st );
2084 }
2085
2086 if (n > 0) {
2087 // Add an increment before the SB end.
2088 add_counter_update(sbOut, n);
2089 }
2090 return sbOut;
2091}
2092
sewardj4ba057c2005-10-18 12:04:18 +00002093static
sewardj0b9d74a2006-12-24 02:24:11 +00002094IRSB* ms_instrument ( VgCallbackClosure* closure,
njn1a2741a2007-11-26 21:59:04 +00002095 IRSB* sbIn,
njn734b8052007-11-01 04:40:37 +00002096 VexGuestLayout* layout,
sewardj461df9c2006-01-17 02:06:39 +00002097 VexGuestExtents* vge,
florianca503be2012-10-07 21:59:42 +00002098 VexArchInfo* archinfo_host,
sewardj4ba057c2005-10-18 12:04:18 +00002099 IRType gWordTy, IRType hWordTy )
nethercotec9f36922004-02-14 16:40:02 +00002100{
njn734b8052007-11-01 04:40:37 +00002101 if (! have_started_executing_code) {
2102 // Do an initial sample to guarantee that we have at least one.
2103 // We use 'maybe_take_snapshot' instead of 'take_snapshot' to ensure
2104 // 'maybe_take_snapshot's internal static variables are initialised.
2105 have_started_executing_code = True;
2106 maybe_take_snapshot(Normal, "startup");
2107 }
njn1a2741a2007-11-26 21:59:04 +00002108
2109 if (clo_time_unit == TimeI) { return ms_instrument2(sbIn); }
2110 else if (clo_time_unit == TimeMS) { return sbIn; }
2111 else if (clo_time_unit == TimeB) { return sbIn; }
2112 else { tl_assert2(0, "bad --time-unit value"); }
nethercotec9f36922004-02-14 16:40:02 +00002113}
2114
nethercotec9f36922004-02-14 16:40:02 +00002115
njn734b8052007-11-01 04:40:37 +00002116//------------------------------------------------------------//
2117//--- Writing snapshots ---//
2118//------------------------------------------------------------//
nethercotec9f36922004-02-14 16:40:02 +00002119
floriandbb35842012-10-27 18:39:11 +00002120HChar FP_buf[BUF_LEN];
nethercotec9f36922004-02-14 16:40:02 +00002121
njn734b8052007-11-01 04:40:37 +00002122// XXX: implement f{,n}printf in m_libcprint.c eventually, and use it here.
2123// Then change Cachegrind to use it too.
2124#define FP(format, args...) ({ \
njnb11e5aa2008-01-20 22:30:52 +00002125 VG_(snprintf)(FP_buf, BUF_LEN, format, ##args); \
2126 FP_buf[BUF_LEN-1] = '\0'; /* Make sure the string is terminated. */ \
2127 VG_(write)(fd, (void*)FP_buf, VG_(strlen)(FP_buf)); \
2128})
2129
nethercotec9f36922004-02-14 16:40:02 +00002130// Nb: uses a static buffer, each call trashes the last string returned.
florian19f91bb2012-11-10 22:29:54 +00002131static const HChar* make_perc(double x)
nethercotec9f36922004-02-14 16:40:02 +00002132{
floriandbb35842012-10-27 18:39:11 +00002133 static HChar mbuf[32];
njn734b8052007-11-01 04:40:37 +00002134
njne9bbd362011-01-27 23:07:56 +00002135 VG_(percentify)((ULong)(x * 100), 10000, 2, 6, mbuf);
njn734b8052007-11-01 04:40:37 +00002136 // XXX: this is bogus if the denominator was zero -- resulting string is
2137 // something like "0 --%")
2138 if (' ' == mbuf[0]) mbuf[0] = '0';
nethercotec9f36922004-02-14 16:40:02 +00002139 return mbuf;
2140}
2141
florian19f91bb2012-11-10 22:29:54 +00002142static void pp_snapshot_SXPt(Int fd, SXPt* sxpt, Int depth, HChar* depth_str,
njn734b8052007-11-01 04:40:37 +00002143 Int depth_str_len,
2144 SizeT snapshot_heap_szB, SizeT snapshot_total_szB)
nethercotec9f36922004-02-14 16:40:02 +00002145{
njn466ed6e2009-02-18 05:14:44 +00002146 Int i, j, n_insig_children_sxpts;
njn734b8052007-11-01 04:40:37 +00002147 SXPt* child = NULL;
nethercotec9f36922004-02-14 16:40:02 +00002148
sewardjf330ae82008-05-13 10:57:29 +00002149 // Used for printing function names. Is made static to keep it out
2150 // of the stack frame -- this function is recursive. Obviously this
2151 // now means its contents are trashed across the recursive call.
floriane58e8a72012-10-20 19:57:16 +00002152 static HChar ip_desc_array[BUF_LEN];
florian6bd9dc12012-11-23 16:17:43 +00002153 const HChar* ip_desc = ip_desc_array;
sewardjf330ae82008-05-13 10:57:29 +00002154
njn734b8052007-11-01 04:40:37 +00002155 switch (sxpt->tag) {
2156 case SigSXPt:
2157 // Print the SXPt itself.
njn84f32b22009-02-10 07:14:37 +00002158 if (0 == depth) {
njne323a6b2010-07-01 02:35:03 +00002159 if (clo_heap) {
2160 ip_desc =
2161 ( clo_pages_as_heap
2162 ? "(page allocation syscalls) mmap/mremap/brk, --alloc-fns, etc."
2163 : "(heap allocation functions) malloc/new/new[], --alloc-fns, etc."
2164 );
2165 } else {
2166 // XXX: --alloc-fns?
florian6bd9dc12012-11-23 16:17:43 +00002167
2168 // Nick thinks this case cannot happen. ip_desc_array would be
2169 // conceptually uninitialised here. Therefore:
2170 tl_assert2(0, "pp_snapshot_SXPt: unexpected");
njne323a6b2010-07-01 02:35:03 +00002171 }
njn734b8052007-11-01 04:40:37 +00002172 } else {
2173 // If it's main-or-below-main, we (if appropriate) ignore everything
2174 // below it by pretending it has no children.
njn68824432009-02-10 06:48:00 +00002175 if ( ! VG_(clo_show_below_main) ) {
2176 Vg_FnNameKind kind = VG_(get_fnname_kind_from_IP)(sxpt->Sig.ip);
2177 if (Vg_FnNameMain == kind || Vg_FnNameBelowMain == kind) {
2178 sxpt->Sig.n_children = 0;
2179 }
njn734b8052007-11-01 04:40:37 +00002180 }
njn68824432009-02-10 06:48:00 +00002181
njn734b8052007-11-01 04:40:37 +00002182 // We need the -1 to get the line number right, But I'm not sure why.
florian6bd9dc12012-11-23 16:17:43 +00002183 ip_desc = VG_(describe_IP)(sxpt->Sig.ip-1, ip_desc_array, BUF_LEN);
njn734b8052007-11-01 04:40:37 +00002184 }
njn466ed6e2009-02-18 05:14:44 +00002185
2186 // Do the non-ip_desc part first...
2187 FP("%sn%d: %lu ", depth_str, sxpt->Sig.n_children, sxpt->szB);
2188
2189 // For ip_descs beginning with "0xABCD...:" addresses, we first
2190 // measure the length of the "0xabcd: " address at the start of the
2191 // ip_desc.
2192 j = 0;
2193 if ('0' == ip_desc[0] && 'x' == ip_desc[1]) {
2194 j = 2;
2195 while (True) {
2196 if (ip_desc[j]) {
2197 if (':' == ip_desc[j]) break;
2198 j++;
2199 } else {
2200 tl_assert2(0, "ip_desc has unexpected form: %s\n", ip_desc);
2201 }
2202 }
2203 }
2204 // Nb: We treat this specially (ie. we don't use FP) so that if the
2205 // ip_desc is too long (eg. due to a long C++ function name), it'll
2206 // get truncated, but the '\n' is still there so its a valid file.
2207 // (At one point we were truncating without adding the '\n', which
2208 // caused bug #155929.)
2209 //
2210 // Also, we account for the length of the address in ip_desc when
2211 // truncating. (The longest address we could have is 18 chars: "0x"
2212 // plus 16 address digits.) This ensures that the truncated function
2213 // name always has the same length, which makes truncation
2214 // deterministic and thus makes testing easier.
2215 tl_assert(j <= 18);
2216 VG_(snprintf)(FP_buf, BUF_LEN, "%s\n", ip_desc);
2217 FP_buf[BUF_LEN-18+j-5] = '.'; // "..." at the end make the
2218 FP_buf[BUF_LEN-18+j-4] = '.'; // truncation more obvious.
2219 FP_buf[BUF_LEN-18+j-3] = '.';
2220 FP_buf[BUF_LEN-18+j-2] = '\n'; // The last char is '\n'.
2221 FP_buf[BUF_LEN-18+j-1] = '\0'; // The string is terminated.
2222 VG_(write)(fd, (void*)FP_buf, VG_(strlen)(FP_buf));
nethercotec9f36922004-02-14 16:40:02 +00002223
njn734b8052007-11-01 04:40:37 +00002224 // Indent.
2225 tl_assert(depth+1 < depth_str_len-1); // -1 for end NUL char
2226 depth_str[depth+0] = ' ';
2227 depth_str[depth+1] = '\0';
2228
2229 // Sort SXPt's children by szB (reverse order: biggest to smallest).
2230 // Nb: we sort them here, rather than earlier (eg. in dup_XTree), for
2231 // two reasons. First, if we do it during dup_XTree, it can get
2232 // expensive (eg. 15% of execution time for konqueror
2233 // startup/shutdown). Second, this way we get the Insig SXPt (if one
2234 // is present) in its sorted position, not at the end.
2235 VG_(ssort)(sxpt->Sig.children, sxpt->Sig.n_children, sizeof(SXPt*),
2236 SXPt_revcmp_szB);
2237
2238 // Print the SXPt's children. They should already be in sorted order.
2239 n_insig_children_sxpts = 0;
2240 for (i = 0; i < sxpt->Sig.n_children; i++) {
njn734b8052007-11-01 04:40:37 +00002241 child = sxpt->Sig.children[i];
2242
2243 if (InsigSXPt == child->tag)
2244 n_insig_children_sxpts++;
2245
sewardjf330ae82008-05-13 10:57:29 +00002246 // Ok, print the child. NB: contents of ip_desc_array will be
2247 // trashed by this recursive call. Doesn't matter currently,
2248 // but worth noting.
njn734b8052007-11-01 04:40:37 +00002249 pp_snapshot_SXPt(fd, child, depth+1, depth_str, depth_str_len,
2250 snapshot_heap_szB, snapshot_total_szB);
njn734b8052007-11-01 04:40:37 +00002251 }
njndbeb5352007-12-04 03:15:23 +00002252
2253 // Unindent.
2254 depth_str[depth+0] = '\0';
2255 depth_str[depth+1] = '\0';
2256
njn734b8052007-11-01 04:40:37 +00002257 // There should be 0 or 1 Insig children SXPts.
2258 tl_assert(n_insig_children_sxpts <= 1);
2259 break;
2260
2261 case InsigSXPt: {
floriane58e8a72012-10-20 19:57:16 +00002262 const HChar* s = ( 1 == sxpt->Insig.n_xpts ? "," : "s, all" );
njn734b8052007-11-01 04:40:37 +00002263 FP("%sn0: %lu in %d place%s below massif's threshold (%s)\n",
2264 depth_str, sxpt->szB, sxpt->Insig.n_xpts, s,
njne9bbd362011-01-27 23:07:56 +00002265 make_perc(clo_threshold));
njn734b8052007-11-01 04:40:37 +00002266 break;
2267 }
2268
2269 default:
2270 tl_assert2(0, "pp_snapshot_SXPt: unrecognised SXPt tag");
nethercotec9f36922004-02-14 16:40:02 +00002271 }
nethercotec9f36922004-02-14 16:40:02 +00002272}
2273
njn734b8052007-11-01 04:40:37 +00002274static void pp_snapshot(Int fd, Snapshot* snapshot, Int snapshot_n)
nethercotec9f36922004-02-14 16:40:02 +00002275{
njn734b8052007-11-01 04:40:37 +00002276 sanity_check_snapshot(snapshot);
nethercotec9f36922004-02-14 16:40:02 +00002277
njn734b8052007-11-01 04:40:37 +00002278 FP("#-----------\n");
2279 FP("snapshot=%d\n", snapshot_n);
2280 FP("#-----------\n");
2281 FP("time=%lld\n", snapshot->time);
2282 FP("mem_heap_B=%lu\n", snapshot->heap_szB);
njn32397c02007-11-10 04:08:08 +00002283 FP("mem_heap_extra_B=%lu\n", snapshot->heap_extra_szB);
njn734b8052007-11-01 04:40:37 +00002284 FP("mem_stacks_B=%lu\n", snapshot->stacks_szB);
2285
2286 if (is_detailed_snapshot(snapshot)) {
2287 // Detailed snapshot -- print heap tree.
2288 Int depth_str_len = clo_depth + 3;
florian19f91bb2012-11-10 22:29:54 +00002289 HChar* depth_str = VG_(malloc)("ms.main.pps.1",
2290 sizeof(HChar) * depth_str_len);
njn734b8052007-11-01 04:40:37 +00002291 SizeT snapshot_total_szB =
njn32397c02007-11-10 04:08:08 +00002292 snapshot->heap_szB + snapshot->heap_extra_szB + snapshot->stacks_szB;
njn734b8052007-11-01 04:40:37 +00002293 depth_str[0] = '\0'; // Initialise depth_str to "".
2294
2295 FP("heap_tree=%s\n", ( Peak == snapshot->kind ? "peak" : "detailed" ));
2296 pp_snapshot_SXPt(fd, snapshot->alloc_sxpt, 0, depth_str,
2297 depth_str_len, snapshot->heap_szB,
2298 snapshot_total_szB);
2299
2300 VG_(free)(depth_str);
2301
2302 } else {
2303 FP("heap_tree=empty\n");
nethercote43a15ce2004-08-30 19:15:12 +00002304 }
nethercotec9f36922004-02-14 16:40:02 +00002305}
2306
florian19f91bb2012-11-10 22:29:54 +00002307static void write_snapshots_to_file(const HChar* massif_out_file,
sewardj3b290482011-05-06 21:02:55 +00002308 Snapshot snapshots_array[],
2309 Int nr_elements)
nethercotec9f36922004-02-14 16:40:02 +00002310{
njn734b8052007-11-01 04:40:37 +00002311 Int i, fd;
sewardj92645592005-07-23 09:18:34 +00002312 SysRes sres;
nethercotec9f36922004-02-14 16:40:02 +00002313
njn734b8052007-11-01 04:40:37 +00002314 sres = VG_(open)(massif_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
2315 VKI_S_IRUSR|VKI_S_IWUSR);
njncda2f0f2009-05-18 02:12:08 +00002316 if (sr_isError(sres)) {
njn734b8052007-11-01 04:40:37 +00002317 // If the file can't be opened for whatever reason (conflict
2318 // between multiple cachegrinded processes?), give up now.
sewardj5ea0f392009-07-15 14:51:34 +00002319 VG_(umsg)("error: can't open output file '%s'\n", massif_out_file );
2320 VG_(umsg)(" ... so profiling results will be missing.\n");
nethercotec9f36922004-02-14 16:40:02 +00002321 return;
sewardj92645592005-07-23 09:18:34 +00002322 } else {
njncda2f0f2009-05-18 02:12:08 +00002323 fd = sr_Res(sres);
nethercotec9f36922004-02-14 16:40:02 +00002324 }
2325
njn734b8052007-11-01 04:40:37 +00002326 // Print massif-specific options that were used.
2327 // XXX: is it worth having a "desc:" line? Could just call it "options:"
2328 // -- this file format isn't as generic as Cachegrind's, so the
2329 // implied genericity of "desc:" is bogus.
2330 FP("desc:");
2331 for (i = 0; i < VG_(sizeXA)(args_for_massif); i++) {
florian19f91bb2012-11-10 22:29:54 +00002332 HChar* arg = *(HChar**)VG_(indexXA)(args_for_massif, i);
njn734b8052007-11-01 04:40:37 +00002333 FP(" %s", arg);
nethercotec9f36922004-02-14 16:40:02 +00002334 }
njn734b8052007-11-01 04:40:37 +00002335 if (0 == i) FP(" (none)");
2336 FP("\n");
nethercotec9f36922004-02-14 16:40:02 +00002337
njn734b8052007-11-01 04:40:37 +00002338 // Print "cmd:" line.
2339 FP("cmd: ");
sewardj45f4e7c2005-09-27 19:20:21 +00002340 if (VG_(args_the_exename)) {
njn734b8052007-11-01 04:40:37 +00002341 FP("%s", VG_(args_the_exename));
2342 for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) {
2343 HChar* arg = * (HChar**) VG_(indexXA)( VG_(args_for_client), i );
2344 if (arg)
2345 FP(" %s", arg);
2346 }
2347 } else {
2348 FP(" ???");
sewardj45f4e7c2005-09-27 19:20:21 +00002349 }
njn734b8052007-11-01 04:40:37 +00002350 FP("\n");
nethercotec9f36922004-02-14 16:40:02 +00002351
njn734b8052007-11-01 04:40:37 +00002352 FP("time_unit: %s\n", TimeUnit_to_string(clo_time_unit));
nethercotec9f36922004-02-14 16:40:02 +00002353
sewardj3b290482011-05-06 21:02:55 +00002354 for (i = 0; i < nr_elements; i++) {
2355 Snapshot* snapshot = & snapshots_array[i];
njn734b8052007-11-01 04:40:37 +00002356 pp_snapshot(fd, snapshot, i); // Detailed snapshot!
nethercotec9f36922004-02-14 16:40:02 +00002357 }
sewardj3b290482011-05-06 21:02:55 +00002358 VG_(close) (fd);
nethercotec9f36922004-02-14 16:40:02 +00002359}
2360
sewardj3b290482011-05-06 21:02:55 +00002361static void write_snapshots_array_to_file(void)
2362{
2363 // Setup output filename. Nb: it's important to do this now, ie. as late
2364 // as possible. If we do it at start-up and the program forks and the
2365 // output file format string contains a %p (pid) specifier, both the
2366 // parent and child will incorrectly write to the same file; this
2367 // happened in 3.3.0.
florian19f91bb2012-11-10 22:29:54 +00002368 HChar* massif_out_file =
sewardj3b290482011-05-06 21:02:55 +00002369 VG_(expand_file_name)("--massif-out-file", clo_massif_out_file);
2370 write_snapshots_to_file (massif_out_file, snapshots, next_snapshot_i);
2371 VG_(free)(massif_out_file);
2372}
2373
florian19f91bb2012-11-10 22:29:54 +00002374static void handle_snapshot_monitor_command (const HChar *filename,
2375 Bool detailed)
sewardjd142f992011-05-17 17:15:07 +00002376{
2377 Snapshot snapshot;
2378
philippe27c9f0d2012-09-24 21:50:16 +00002379 if (!clo_pages_as_heap && !have_started_executing_code) {
2380 // See comments of variable have_started_executing_code.
2381 VG_(gdb_printf)
2382 ("error: cannot take snapshot before execution has started\n");
2383 return;
2384 }
2385
sewardjd142f992011-05-17 17:15:07 +00002386 clear_snapshot(&snapshot, /* do_sanity_check */ False);
2387 take_snapshot(&snapshot, Normal, get_time(), detailed);
philippe27c9f0d2012-09-24 21:50:16 +00002388 write_snapshots_to_file ((filename == NULL) ?
florian19f91bb2012-11-10 22:29:54 +00002389 "massif.vgdb.out" : filename,
sewardjd142f992011-05-17 17:15:07 +00002390 &snapshot,
2391 1);
2392 delete_snapshot(&snapshot);
2393}
2394
florian19f91bb2012-11-10 22:29:54 +00002395static Bool handle_gdb_monitor_command (ThreadId tid, HChar *req)
sewardj3b290482011-05-06 21:02:55 +00002396{
florian19f91bb2012-11-10 22:29:54 +00002397 HChar* wcmd;
2398 HChar s[VG_(strlen(req))]; /* copy for strtok_r */
2399 HChar *ssaveptr;
sewardj3b290482011-05-06 21:02:55 +00002400
2401 VG_(strcpy) (s, req);
2402
2403 wcmd = VG_(strtok_r) (s, " ", &ssaveptr);
sewardj30b3eca2011-06-28 08:20:39 +00002404 switch (VG_(keyword_id) ("help snapshot detailed_snapshot",
sewardj3b290482011-05-06 21:02:55 +00002405 wcmd, kwd_report_duplicated_matches)) {
2406 case -2: /* multiple matches */
2407 return True;
2408 case -1: /* not found */
2409 return False;
2410 case 0: /* help */
2411 print_monitor_help();
2412 return True;
sewardj30b3eca2011-06-28 08:20:39 +00002413 case 1: { /* snapshot */
florian19f91bb2012-11-10 22:29:54 +00002414 HChar* filename;
sewardjd142f992011-05-17 17:15:07 +00002415 filename = VG_(strtok_r) (NULL, " ", &ssaveptr);
2416 handle_snapshot_monitor_command (filename, False /* detailed */);
2417 return True;
2418 }
sewardj30b3eca2011-06-28 08:20:39 +00002419 case 2: { /* detailed_snapshot */
florian19f91bb2012-11-10 22:29:54 +00002420 HChar* filename;
sewardjd142f992011-05-17 17:15:07 +00002421 filename = VG_(strtok_r) (NULL, " ", &ssaveptr);
2422 handle_snapshot_monitor_command (filename, True /* detailed */);
sewardj3b290482011-05-06 21:02:55 +00002423 return True;
2424 }
2425 default:
2426 tl_assert(0);
2427 return False;
2428 }
2429}
njn734b8052007-11-01 04:40:37 +00002430
2431//------------------------------------------------------------//
2432//--- Finalisation ---//
2433//------------------------------------------------------------//
2434
njn51d827b2005-05-09 01:02:08 +00002435static void ms_fini(Int exit_status)
nethercotec9f36922004-02-14 16:40:02 +00002436{
njn734b8052007-11-01 04:40:37 +00002437 // Output.
sewardj3b290482011-05-06 21:02:55 +00002438 write_snapshots_array_to_file();
nethercotec9f36922004-02-14 16:40:02 +00002439
njn734b8052007-11-01 04:40:37 +00002440 // Stats
2441 tl_assert(n_xpts > 0); // always have alloc_xpt
sewardj2d9e8742009-08-07 15:46:56 +00002442 STATS("heap allocs: %u\n", n_heap_allocs);
2443 STATS("heap reallocs: %u\n", n_heap_reallocs);
2444 STATS("heap frees: %u\n", n_heap_frees);
2445 STATS("ignored heap allocs: %u\n", n_ignored_heap_allocs);
2446 STATS("ignored heap frees: %u\n", n_ignored_heap_frees);
2447 STATS("ignored heap reallocs: %u\n", n_ignored_heap_reallocs);
2448 STATS("stack allocs: %u\n", n_stack_allocs);
2449 STATS("stack frees: %u\n", n_stack_frees);
2450 STATS("XPts: %u\n", n_xpts);
2451 STATS("top-XPts: %u (%d%%)\n",
njn734b8052007-11-01 04:40:37 +00002452 alloc_xpt->n_children,
2453 ( n_xpts ? alloc_xpt->n_children * 100 / n_xpts : 0));
sewardj2d9e8742009-08-07 15:46:56 +00002454 STATS("XPt init expansions: %u\n", n_xpt_init_expansions);
2455 STATS("XPt later expansions: %u\n", n_xpt_later_expansions);
2456 STATS("SXPt allocs: %u\n", n_sxpt_allocs);
2457 STATS("SXPt frees: %u\n", n_sxpt_frees);
2458 STATS("skipped snapshots: %u\n", n_skipped_snapshots);
2459 STATS("real snapshots: %u\n", n_real_snapshots);
2460 STATS("detailed snapshots: %u\n", n_detailed_snapshots);
2461 STATS("peak snapshots: %u\n", n_peak_snapshots);
2462 STATS("cullings: %u\n", n_cullings);
2463 STATS("XCon redos: %u\n", n_XCon_redos);
nethercotec9f36922004-02-14 16:40:02 +00002464}
2465
njn734b8052007-11-01 04:40:37 +00002466
2467//------------------------------------------------------------//
2468//--- Initialisation ---//
2469//------------------------------------------------------------//
njn51d827b2005-05-09 01:02:08 +00002470
2471static void ms_post_clo_init(void)
2472{
njn734b8052007-11-01 04:40:37 +00002473 Int i;
florian19f91bb2012-11-10 22:29:54 +00002474 HChar* LD_PRELOAD_val;
2475 HChar* s;
2476 HChar* s2;
njn51d827b2005-05-09 01:02:08 +00002477
njn734b8052007-11-01 04:40:37 +00002478 // Check options.
njne323a6b2010-07-01 02:35:03 +00002479 if (clo_pages_as_heap) {
2480 if (clo_stacks) {
njnb1cc5d62010-07-06 04:05:23 +00002481 VG_(fmsg_bad_option)(
2482 "--pages-as-heap=yes together with --stacks=yes", "");
njne323a6b2010-07-01 02:35:03 +00002483 }
2484 }
njn734b8052007-11-01 04:40:37 +00002485 if (!clo_heap) {
njne323a6b2010-07-01 02:35:03 +00002486 clo_pages_as_heap = False;
2487 }
2488
2489 // If --pages-as-heap=yes we don't want malloc replacement to occur. So we
2490 // disable vgpreload_massif-$PLATFORM.so by removing it from LD_PRELOAD (or
2491 // platform-equivalent). We replace it entirely with spaces because then
2492 // the linker doesn't complain (it does complain if we just change the name
2493 // to a bogus file). This is a bit of a hack, but LD_PRELOAD is setup well
2494 // before tool initialisation, so this seems the best way to do it.
2495 if (clo_pages_as_heap) {
2496 clo_heap_admin = 0; // No heap admin on pages.
2497
florian19f91bb2012-11-10 22:29:54 +00002498 LD_PRELOAD_val = VG_(getenv)( VG_(LD_PRELOAD_var_name) );
njne323a6b2010-07-01 02:35:03 +00002499 tl_assert(LD_PRELOAD_val);
2500
2501 // Make sure the vgpreload_core-$PLATFORM entry is there, for sanity.
2502 s2 = VG_(strstr)(LD_PRELOAD_val, "vgpreload_core");
2503 tl_assert(s2);
2504
2505 // Now find the vgpreload_massif-$PLATFORM entry.
2506 s2 = VG_(strstr)(LD_PRELOAD_val, "vgpreload_massif");
2507 tl_assert(s2);
2508
2509 // Blank out everything to the previous ':', which must be there because
2510 // of the preceding vgpreload_core-$PLATFORM entry.
2511 for (s = s2; *s != ':'; s--) {
2512 *s = ' ';
2513 }
2514
2515 // Blank out everything to the end of the entry, which will be '\0' if
2516 // LD_PRELOAD was empty before Valgrind started, or ':' otherwise.
2517 for (s = s2; *s != ':' && *s != '\0'; s++) {
2518 *s = ' ';
2519 }
njn734b8052007-11-01 04:40:37 +00002520 }
2521
njnf6b00762009-04-17 04:26:41 +00002522 // Print alloc-fns and ignore-fns, if necessary.
njn734b8052007-11-01 04:40:37 +00002523 if (VG_(clo_verbosity) > 1) {
sewardj5ea0f392009-07-15 14:51:34 +00002524 VERB(1, "alloc-fns:\n");
njn734b8052007-11-01 04:40:37 +00002525 for (i = 0; i < VG_(sizeXA)(alloc_fns); i++) {
florian19f91bb2012-11-10 22:29:54 +00002526 HChar** fn_ptr = VG_(indexXA)(alloc_fns, i);
sewardj5ea0f392009-07-15 14:51:34 +00002527 VERB(1, " %s\n", *fn_ptr);
njnf6b00762009-04-17 04:26:41 +00002528 }
2529
sewardj5ea0f392009-07-15 14:51:34 +00002530 VERB(1, "ignore-fns:\n");
njnf6b00762009-04-17 04:26:41 +00002531 if (0 == VG_(sizeXA)(ignore_fns)) {
sewardj5ea0f392009-07-15 14:51:34 +00002532 VERB(1, " <empty>\n");
njnf6b00762009-04-17 04:26:41 +00002533 }
2534 for (i = 0; i < VG_(sizeXA)(ignore_fns); i++) {
florian19f91bb2012-11-10 22:29:54 +00002535 HChar** fn_ptr = VG_(indexXA)(ignore_fns, i);
sewardj5ea0f392009-07-15 14:51:34 +00002536 VERB(1, " %d: %s\n", i, *fn_ptr);
njn734b8052007-11-01 04:40:37 +00002537 }
2538 }
2539
2540 // Events to track.
2541 if (clo_stacks) {
2542 VG_(track_new_mem_stack) ( new_mem_stack );
2543 VG_(track_die_mem_stack) ( die_mem_stack );
2544 VG_(track_new_mem_stack_signal) ( new_mem_stack_signal );
2545 VG_(track_die_mem_stack_signal) ( die_mem_stack_signal );
2546 }
2547
njne323a6b2010-07-01 02:35:03 +00002548 if (clo_pages_as_heap) {
2549 VG_(track_new_mem_startup) ( ms_new_mem_startup );
2550 VG_(track_new_mem_brk) ( ms_new_mem_brk );
2551 VG_(track_new_mem_mmap) ( ms_new_mem_mmap );
2552
2553 VG_(track_copy_mem_remap) ( ms_copy_mem_remap );
2554
2555 VG_(track_die_mem_brk) ( ms_die_mem_brk );
2556 VG_(track_die_mem_munmap) ( ms_die_mem_munmap );
2557 }
2558
njn734b8052007-11-01 04:40:37 +00002559 // Initialise snapshot array, and sanity-check it.
sewardj9c606bd2008-09-18 18:12:50 +00002560 snapshots = VG_(malloc)("ms.main.mpoci.1",
2561 sizeof(Snapshot) * clo_max_snapshots);
njn734b8052007-11-01 04:40:37 +00002562 // We don't want to do snapshot sanity checks here, because they're
2563 // currently uninitialised.
2564 for (i = 0; i < clo_max_snapshots; i++) {
2565 clear_snapshot( & snapshots[i], /*do_sanity_check*/False );
2566 }
2567 sanity_check_snapshots_array();
njn51d827b2005-05-09 01:02:08 +00002568}
2569
tom151a6392005-11-11 12:30:36 +00002570static void ms_pre_clo_init(void)
njn734b8052007-11-01 04:40:37 +00002571{
njn51d827b2005-05-09 01:02:08 +00002572 VG_(details_name) ("Massif");
2573 VG_(details_version) (NULL);
njn1a2741a2007-11-26 21:59:04 +00002574 VG_(details_description) ("a heap profiler");
njn9a0cba42007-04-15 22:15:57 +00002575 VG_(details_copyright_author)(
sewardj03f8d3f2012-08-05 15:46:46 +00002576 "Copyright (C) 2003-2012, and GNU GPL'd, by Nicholas Nethercote");
njn51d827b2005-05-09 01:02:08 +00002577 VG_(details_bug_reports_to) (VG_BUGS_TO);
2578
sewardj1e0fff62011-01-10 15:01:03 +00002579 VG_(details_avg_translation_sizeB) ( 330 );
2580
philippe5b240c22012-08-14 22:28:31 +00002581 VG_(clo_vex_control).iropt_register_updates
2582 = VexRegUpdSpAtMemAccess; // overridable by the user.
2583
njnf6b00762009-04-17 04:26:41 +00002584 // Basic functions.
njn51d827b2005-05-09 01:02:08 +00002585 VG_(basic_tool_funcs) (ms_post_clo_init,
2586 ms_instrument,
2587 ms_fini);
2588
njnf6b00762009-04-17 04:26:41 +00002589 // Needs.
njn51d827b2005-05-09 01:02:08 +00002590 VG_(needs_libc_freeres)();
2591 VG_(needs_command_line_options)(ms_process_cmd_line_option,
2592 ms_print_usage,
2593 ms_print_debug_usage);
2594 VG_(needs_client_requests) (ms_handle_client_request);
njn734b8052007-11-01 04:40:37 +00002595 VG_(needs_sanity_checks) (ms_cheap_sanity_check,
2596 ms_expensive_sanity_check);
njnfc51f8d2005-06-21 03:20:17 +00002597 VG_(needs_malloc_replacement) (ms_malloc,
njn51d827b2005-05-09 01:02:08 +00002598 ms___builtin_new,
2599 ms___builtin_vec_new,
2600 ms_memalign,
2601 ms_calloc,
2602 ms_free,
2603 ms___builtin_delete,
2604 ms___builtin_vec_delete,
2605 ms_realloc,
njn8b140de2009-02-17 04:31:18 +00002606 ms_malloc_usable_size,
njn51d827b2005-05-09 01:02:08 +00002607 0 );
2608
njnf6b00762009-04-17 04:26:41 +00002609 // HP_Chunks.
njn734b8052007-11-01 04:40:37 +00002610 malloc_list = VG_(HT_construct)( "Massif's malloc list" );
njn51d827b2005-05-09 01:02:08 +00002611
2612 // Dummy node at top of the context structure.
njn734b8052007-11-01 04:40:37 +00002613 alloc_xpt = new_XPt(/*ip*/0, /*parent*/NULL);
2614
njnf6b00762009-04-17 04:26:41 +00002615 // Initialise alloc_fns and ignore_fns.
njn734b8052007-11-01 04:40:37 +00002616 init_alloc_fns();
njnf6b00762009-04-17 04:26:41 +00002617 init_ignore_fns();
njn734b8052007-11-01 04:40:37 +00002618
2619 // Initialise args_for_massif.
sewardj9c606bd2008-09-18 18:12:50 +00002620 args_for_massif = VG_(newXA)(VG_(malloc), "ms.main.mprci.1",
2621 VG_(free), sizeof(HChar*));
njn51d827b2005-05-09 01:02:08 +00002622}
2623
sewardj45f4e7c2005-09-27 19:20:21 +00002624VG_DETERMINE_INTERFACE_VERSION(ms_pre_clo_init)
nethercotec9f36922004-02-14 16:40:02 +00002625
njn734b8052007-11-01 04:40:37 +00002626//--------------------------------------------------------------------//
2627//--- end ---//
2628//--------------------------------------------------------------------//