njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1 | //--------------------------------------------------------------------*/ |
| 2 | //--- Massif: a heap profiling tool. ms_main.c ---*/ |
| 3 | //--------------------------------------------------------------------*/ |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 4 | |
| 5 | /* |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 6 | This file is part of Massif, a Valgrind tool for profiling memory |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 7 | usage of programs. |
| 8 | |
sewardj | 0f157dd | 2013-10-18 14:27:36 +0000 | [diff] [blame] | 9 | Copyright (C) 2003-2013 Nicholas Nethercote |
njn | 2bc1012 | 2005-05-08 02:10:27 +0000 | [diff] [blame] | 10 | njn@valgrind.org |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 11 | |
| 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 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 30 | //--------------------------------------------------------------------------- |
| 31 | // XXX: |
| 32 | //--------------------------------------------------------------------------- |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 33 | // Todo -- nice, but less critical: |
njn | ab23b61 | 2007-11-27 00:53:56 +0000 | [diff] [blame] | 34 | // - do a graph-drawing test |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 35 | // - 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 |
philippe | 868bfa2 | 2015-03-07 19:20:12 +0000 | [diff] [blame] | 40 | // - do snapshots on some specific client requests |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 41 | // - "show me the extra allocations since the last snapshot" |
| 42 | // - "start/stop logging" (eg. quickly skip boring bits) |
| 43 | // - Add ability to draw multiple graphs, eg. heap-only, stack-only, total. |
| 44 | // Give each graph a title. (try to do it generically!) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 45 | // - make --show-below-main=no work |
njn | a15b666 | 2009-08-04 05:59:46 +0000 | [diff] [blame] | 46 | // - Options like --alloc-fn='operator new(unsigned, std::nothrow_t const&)' |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 47 | // don't work in a .valgrindrc file or in $VALGRIND_OPTS. |
| 48 | // m_commandline.c:add_args_from_string() needs to respect single quotes. |
njn | 3b677e5 | 2007-12-03 21:29:22 +0000 | [diff] [blame] | 49 | // - With --stack=yes, want to add a stack trace for detailed snapshots so |
| 50 | // it's clear where/why the peak is occurring. (Mattieu Castet) Also, |
| 51 | // possibly useful even with --stack=no? (Andi Yin) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 52 | // |
| 53 | // Performance: |
| 54 | // - To run the benchmarks: |
| 55 | // |
| 56 | // perl perf/vg_perf --tools=massif --reps=3 perf/{heap,tinycc} massif |
| 57 | // time valgrind --tool=massif --depth=100 konqueror |
| 58 | // |
| 59 | // The other benchmarks don't do much allocation, and so give similar speeds |
| 60 | // to Nulgrind. |
| 61 | // |
| 62 | // Timing results on 'nevermore' (njn's machine) as of r7013: |
| 63 | // |
| 64 | // heap 0.53s ma:12.4s (23.5x, -----) |
| 65 | // tinycc 0.46s ma: 4.9s (10.7x, -----) |
| 66 | // many-xpts 0.08s ma: 2.0s (25.0x, -----) |
| 67 | // konqueror 29.6s real 0:21.0s user |
| 68 | // |
njn | ab23b61 | 2007-11-27 00:53:56 +0000 | [diff] [blame] | 69 | // [Introduction of --time-unit=i as the default slowed things down by |
| 70 | // roughly 0--20%.] |
| 71 | // |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 72 | // - get_XCon accounts for about 9% of konqueror startup time. Try |
| 73 | // keeping XPt children sorted by 'ip' and use binary search in get_XCon. |
| 74 | // Requires factoring out binary search code from various places into a |
| 75 | // VG_(bsearch) function. |
| 76 | // |
| 77 | // Todo -- low priority: |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 78 | // - In each XPt, record both bytes and the number of allocations, and |
| 79 | // possibly the global number of allocations. |
njn | 0f85e02 | 2007-11-27 02:03:44 +0000 | [diff] [blame] | 80 | // - (Andy Lin) Give a stack trace on detailed snapshots? |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 81 | // - (Artur Wisz) add a feature to Massif to ignore any heap blocks larger |
| 82 | // than a certain size! Because: "linux's malloc allows to set a |
| 83 | // MMAP_THRESHOLD value, so we set it to 4096 - all blocks above that will |
| 84 | // be handled directly by the kernel, and are guaranteed to be returned to |
| 85 | // the system when freed. So we needed to profile only blocks below this |
| 86 | // limit." |
| 87 | // |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 88 | // File format working notes: |
| 89 | |
| 90 | #if 0 |
| 91 | desc: --heap-admin=foo |
| 92 | cmd: date |
| 93 | time_unit: ms |
| 94 | #----------- |
| 95 | snapshot=0 |
| 96 | #----------- |
| 97 | time=0 |
| 98 | mem_heap_B=0 |
| 99 | mem_heap_admin_B=0 |
| 100 | mem_stacks_B=0 |
| 101 | heap_tree=empty |
| 102 | #----------- |
| 103 | snapshot=1 |
| 104 | #----------- |
| 105 | time=353 |
| 106 | mem_heap_B=5 |
| 107 | mem_heap_admin_B=0 |
| 108 | mem_stacks_B=0 |
| 109 | heap_tree=detailed |
| 110 | n1: 5 (heap allocation functions) malloc/new/new[], --alloc-fns, etc. |
| 111 | n1: 5 0x27F6E0: _nl_normalize_codeset (in /lib/libc-2.3.5.so) |
| 112 | n1: 5 0x279DE6: _nl_load_locale_from_archive (in /lib/libc-2.3.5.so) |
| 113 | n1: 5 0x278E97: _nl_find_locale (in /lib/libc-2.3.5.so) |
| 114 | n1: 5 0x278871: setlocale (in /lib/libc-2.3.5.so) |
| 115 | n1: 5 0x8049821: (within /bin/date) |
| 116 | n0: 5 0x26ED5E: (below main) (in /lib/libc-2.3.5.so) |
| 117 | |
| 118 | |
| 119 | n_events: n time(ms) total(B) useful-heap(B) admin-heap(B) stacks(B) |
| 120 | t_events: B |
| 121 | n 0 0 0 0 0 |
| 122 | n 0 0 0 0 0 |
| 123 | t1: 5 <string...> |
| 124 | t1: 6 <string...> |
| 125 | |
| 126 | Ideas: |
| 127 | - each snapshot specifies an x-axis value and one or more y-axis values. |
| 128 | - can display the y-axis values separately if you like |
| 129 | - can completely separate connection between snapshots and trees. |
| 130 | |
| 131 | Challenges: |
| 132 | - how to specify and scale/abbreviate units on axes? |
| 133 | - how to combine multiple values into the y-axis? |
| 134 | |
| 135 | --------------------------------------------------------------------------------Command: date |
| 136 | Massif arguments: --heap-admin=foo |
| 137 | ms_print arguments: massif.out |
| 138 | -------------------------------------------------------------------------------- |
| 139 | KB |
| 140 | 6.472^ :# |
| 141 | | :# :: . . |
| 142 | ... |
| 143 | | ::@ :@ :@ :@:::# :: : :::: |
| 144 | 0 +-----------------------------------@---@---@-----@--@---#-------------->ms 0 713 |
| 145 | |
| 146 | Number of snapshots: 50 |
| 147 | Detailed snapshots: [2, 11, 13, 19, 25, 32 (peak)] |
| 148 | -------------------------------------------------------------------------------- n time(ms) total(B) useful-heap(B) admin-heap(B) stacks(B) |
| 149 | -------------------------------------------------------------------------------- 0 0 0 0 0 0 |
| 150 | 1 345 5 5 0 0 |
| 151 | 2 353 5 5 0 0 |
| 152 | 100.00% (5B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc. |
| 153 | ->100.00% (5B) 0x27F6E0: _nl_normalize_codeset (in /lib/libc-2.3.5.so) |
| 154 | #endif |
| 155 | |
| 156 | //--------------------------------------------------------------------------- |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 157 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 158 | #include "pub_tool_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 159 | #include "pub_tool_vki.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 160 | #include "pub_tool_aspacemgr.h" |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 161 | #include "pub_tool_debuginfo.h" |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 162 | #include "pub_tool_hashtable.h" |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 163 | #include "pub_tool_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 164 | #include "pub_tool_libcassert.h" |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 165 | #include "pub_tool_libcfile.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 166 | #include "pub_tool_libcprint.h" |
njn | f39e9a3 | 2005-06-12 02:43:17 +0000 | [diff] [blame] | 167 | #include "pub_tool_libcproc.h" |
njn | b506bd8 | 2005-06-21 04:01:51 +0000 | [diff] [blame] | 168 | #include "pub_tool_machine.h" |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 169 | #include "pub_tool_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 170 | #include "pub_tool_options.h" |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 171 | #include "pub_tool_replacemalloc.h" |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 172 | #include "pub_tool_stacktrace.h" |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 173 | #include "pub_tool_threadstate.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 174 | #include "pub_tool_tooliface.h" |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 175 | #include "pub_tool_xarray.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 176 | #include "pub_tool_clientstate.h" |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 177 | #include "pub_tool_gdbserver.h" |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 178 | |
florian | 1a046d5 | 2013-09-16 20:56:35 +0000 | [diff] [blame] | 179 | #include "pub_tool_clreq.h" // For {MALLOC,FREE}LIKE_BLOCK |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 180 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 181 | //------------------------------------------------------------*/ |
| 182 | //--- Overview of operation ---*/ |
| 183 | //------------------------------------------------------------*/ |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 184 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 185 | // The size of the stacks and heap is tracked. The heap is tracked in a lot |
| 186 | // of detail, enough to tell how many bytes each line of code is responsible |
| 187 | // for, more or less. The main data structure is a tree representing the |
| 188 | // call tree beneath all the allocation functions like malloc(). |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 189 | // (Alternatively, if --pages-as-heap=yes is specified, memory is tracked at |
| 190 | // the page level, and each page is treated much like a heap block. We use |
| 191 | // "heap" throughout below to cover this case because the concepts are all the |
| 192 | // same.) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 193 | // |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 194 | // "Snapshots" are recordings of the memory usage. There are two basic |
| 195 | // kinds: |
| 196 | // - Normal: these record the current time, total memory size, total heap |
| 197 | // size, heap admin size and stack size. |
| 198 | // - Detailed: these record those things in a normal snapshot, plus a very |
| 199 | // detailed XTree (see below) indicating how the heap is structured. |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 200 | // |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 201 | // Snapshots are taken every so often. There are two storage classes of |
| 202 | // snapshots: |
| 203 | // - Temporary: Massif does a temporary snapshot every so often. The idea |
| 204 | // is to always have a certain number of temporary snapshots around. So |
| 205 | // we take them frequently to begin with, but decreasingly often as the |
| 206 | // program continues to run. Also, we remove some old ones after a while. |
| 207 | // Overall it's a kind of exponential decay thing. Most of these are |
| 208 | // normal snapshots, a small fraction are detailed snapshots. |
| 209 | // - Permanent: Massif takes a permanent (detailed) snapshot in some |
| 210 | // circumstances. They are: |
| 211 | // - Peak snapshot: When the memory usage peak is reached, it takes a |
| 212 | // snapshot. It keeps this, unless the peak is subsequently exceeded, |
| 213 | // in which case it will overwrite the peak snapshot. |
| 214 | // - User-requested snapshots: These are done in response to client |
| 215 | // requests. They are always kept. |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 216 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 217 | // Used for printing things when clo_verbosity > 1. |
| 218 | #define VERB(verb, format, args...) \ |
| 219 | if (VG_(clo_verbosity) > verb) { \ |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 220 | VG_(dmsg)("Massif: " format, ##args); \ |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 221 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 222 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 223 | //------------------------------------------------------------// |
| 224 | //--- Statistics ---// |
| 225 | //------------------------------------------------------------// |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 226 | |
| 227 | // Konqueror startup, to give an idea of the numbers involved with a biggish |
| 228 | // program, with default depth: |
| 229 | // |
| 230 | // depth=3 depth=40 |
| 231 | // - 310,000 allocations |
| 232 | // - 300,000 frees |
| 233 | // - 15,000 XPts 800,000 XPts |
| 234 | // - 1,800 top-XPts |
| 235 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 236 | static UInt n_heap_allocs = 0; |
| 237 | static UInt n_heap_reallocs = 0; |
| 238 | static UInt n_heap_frees = 0; |
| 239 | static UInt n_ignored_heap_allocs = 0; |
| 240 | static UInt n_ignored_heap_frees = 0; |
| 241 | static UInt n_ignored_heap_reallocs = 0; |
| 242 | static UInt n_stack_allocs = 0; |
| 243 | static UInt n_stack_frees = 0; |
| 244 | static UInt n_xpts = 0; |
| 245 | static UInt n_xpt_init_expansions = 0; |
| 246 | static UInt n_xpt_later_expansions = 0; |
| 247 | static UInt n_sxpt_allocs = 0; |
| 248 | static UInt n_sxpt_frees = 0; |
| 249 | static UInt n_skipped_snapshots = 0; |
| 250 | static UInt n_real_snapshots = 0; |
| 251 | static UInt n_detailed_snapshots = 0; |
| 252 | static UInt n_peak_snapshots = 0; |
| 253 | static UInt n_cullings = 0; |
| 254 | static UInt n_XCon_redos = 0; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 255 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 256 | //------------------------------------------------------------// |
| 257 | //--- Globals ---// |
| 258 | //------------------------------------------------------------// |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 259 | |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 260 | // Number of guest instructions executed so far. Only used with |
| 261 | // --time-unit=i. |
| 262 | static Long guest_instrs_executed = 0; |
| 263 | |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 264 | static SizeT heap_szB = 0; // Live heap size |
| 265 | static SizeT heap_extra_szB = 0; // Live heap extra size -- slop + admin bytes |
| 266 | static SizeT stacks_szB = 0; // Live stacks size |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 267 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 268 | // This is the total size from the current peak snapshot, or 0 if no peak |
| 269 | // snapshot has been taken yet. |
| 270 | static SizeT peak_snapshot_total_szB = 0; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 271 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 272 | // Incremented every time memory is allocated/deallocated, by the |
| 273 | // allocated/deallocated amount; includes heap, heap-admin and stack |
| 274 | // memory. An alternative to milliseconds as a unit of program "time". |
| 275 | static ULong total_allocs_deallocs_szB = 0; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 276 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 277 | // When running with --heap=yes --pages-as-heap=no, we don't start taking |
| 278 | // snapshots until the first basic block is executed, rather than doing it in |
| 279 | // ms_post_clo_init (which is the obvious spot), for two reasons. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 280 | // - It lets us ignore stack events prior to that, because they're not |
| 281 | // really proper ones and just would screw things up. |
| 282 | // - Because there's still some core initialisation to do, and so there |
| 283 | // would be an artificial time gap between the first and second snapshots. |
| 284 | // |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 285 | // When running with --heap=yes --pages-as-heap=yes, snapshots start much |
| 286 | // earlier due to new_mem_startup so this isn't relevant. |
| 287 | // |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 288 | static Bool have_started_executing_code = False; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 289 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 290 | //------------------------------------------------------------// |
| 291 | //--- Alloc fns ---// |
| 292 | //------------------------------------------------------------// |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 293 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 294 | static XArray* alloc_fns; |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 295 | static XArray* ignore_fns; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 296 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 297 | static void init_alloc_fns(void) |
| 298 | { |
| 299 | // Create the list, and add the default elements. |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 300 | alloc_fns = VG_(newXA)(VG_(malloc), "ms.main.iaf.1", |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 301 | VG_(free), sizeof(HChar*)); |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 302 | #define DO(x) { const HChar* s = x; VG_(addToXA)(alloc_fns, &s); } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 303 | |
njn | 13118f8 | 2009-07-01 02:44:12 +0000 | [diff] [blame] | 304 | // Ordered roughly according to (presumed) frequency. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 305 | // Nb: The C++ "operator new*" ones are overloadable. We include them |
| 306 | // always anyway, because even if they're overloaded, it would be a |
| 307 | // prodigiously stupid overloading that caused them to not allocate |
| 308 | // memory. |
njn | 343a504 | 2009-07-01 05:05:38 +0000 | [diff] [blame] | 309 | // |
| 310 | // XXX: because we don't look at the first stack entry (unless it's a |
| 311 | // custom allocation) there's not much point to having all these alloc |
| 312 | // functions here -- they should never appear anywhere (I think?) other |
| 313 | // than the top stack entry. The only exceptions are those that in |
| 314 | // vg_replace_malloc.c are partly or fully implemented in terms of another |
| 315 | // alloc function: realloc (which uses malloc); valloc, |
| 316 | // malloc_zone_valloc, posix_memalign and memalign_common (which use |
| 317 | // memalign). |
| 318 | // |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 319 | DO("malloc" ); |
| 320 | DO("__builtin_new" ); |
| 321 | DO("operator new(unsigned)" ); |
| 322 | DO("operator new(unsigned long)" ); |
| 323 | DO("__builtin_vec_new" ); |
| 324 | DO("operator new[](unsigned)" ); |
| 325 | DO("operator new[](unsigned long)" ); |
| 326 | DO("calloc" ); |
| 327 | DO("realloc" ); |
| 328 | DO("memalign" ); |
njn | 13118f8 | 2009-07-01 02:44:12 +0000 | [diff] [blame] | 329 | DO("posix_memalign" ); |
| 330 | DO("valloc" ); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 331 | DO("operator new(unsigned, std::nothrow_t const&)" ); |
| 332 | DO("operator new[](unsigned, std::nothrow_t const&)" ); |
| 333 | DO("operator new(unsigned long, std::nothrow_t const&)" ); |
| 334 | DO("operator new[](unsigned long, std::nothrow_t const&)"); |
sewardj | 6e9de46 | 2011-06-28 07:25:29 +0000 | [diff] [blame] | 335 | #if defined(VGO_darwin) |
njn | 13118f8 | 2009-07-01 02:44:12 +0000 | [diff] [blame] | 336 | DO("malloc_zone_malloc" ); |
| 337 | DO("malloc_zone_calloc" ); |
| 338 | DO("malloc_zone_realloc" ); |
| 339 | DO("malloc_zone_memalign" ); |
| 340 | DO("malloc_zone_valloc" ); |
| 341 | #endif |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 342 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 343 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 344 | static void init_ignore_fns(void) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 345 | { |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 346 | // Create the (empty) list. |
| 347 | ignore_fns = VG_(newXA)(VG_(malloc), "ms.main.iif.1", |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 348 | VG_(free), sizeof(HChar*)); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // Determines if the named function is a member of the XArray. |
florian | 46cc045 | 2014-10-25 19:20:38 +0000 | [diff] [blame] | 352 | static Bool is_member_fn(const XArray* fns, const HChar* fnname) |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 353 | { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 354 | HChar** fn_ptr; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 355 | Int i; |
| 356 | |
| 357 | // Nb: It's a linear search through the list, because we're comparing |
| 358 | // strings rather than pointers to strings. |
| 359 | // Nb: This gets called a lot. It was an OSet, but they're quite slow to |
| 360 | // iterate through so it wasn't a good choice. |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 361 | for (i = 0; i < VG_(sizeXA)(fns); i++) { |
| 362 | fn_ptr = VG_(indexXA)(fns, i); |
| 363 | if (VG_STREQ(fnname, *fn_ptr)) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 364 | return True; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 365 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 366 | return False; |
| 367 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 368 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 369 | |
| 370 | //------------------------------------------------------------// |
| 371 | //--- Command line args ---// |
| 372 | //------------------------------------------------------------// |
| 373 | |
| 374 | #define MAX_DEPTH 200 |
| 375 | |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 376 | typedef enum { TimeI, TimeMS, TimeB } TimeUnit; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 377 | |
florian | e58e8a7 | 2012-10-20 19:57:16 +0000 | [diff] [blame] | 378 | static const HChar* TimeUnit_to_string(TimeUnit time_unit) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 379 | { |
| 380 | switch (time_unit) { |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 381 | case TimeI: return "i"; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 382 | case TimeMS: return "ms"; |
| 383 | case TimeB: return "B"; |
| 384 | default: tl_assert2(0, "TimeUnit_to_string: unrecognised TimeUnit"); |
| 385 | } |
| 386 | } |
| 387 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 388 | static Bool clo_heap = True; |
njn | 429afb4 | 2007-11-02 04:12:48 +0000 | [diff] [blame] | 389 | // clo_heap_admin is deliberately a word-sized type. At one point it was |
| 390 | // a UInt, but this caused problems on 64-bit machines when it was |
| 391 | // multiplied by a small negative number and then promoted to a |
| 392 | // word-sized type -- it ended up with a value of 4.2 billion. Sigh. |
njn | efc13c2 | 2009-02-23 06:44:51 +0000 | [diff] [blame] | 393 | static SSizeT clo_heap_admin = 8; |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 394 | static Bool clo_pages_as_heap = False; |
njn | 62721e9 | 2007-11-11 22:15:58 +0000 | [diff] [blame] | 395 | static Bool clo_stacks = False; |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 396 | static Int clo_depth = 30; |
njn | 62721e9 | 2007-11-11 22:15:58 +0000 | [diff] [blame] | 397 | static double clo_threshold = 1.0; // percentage |
| 398 | static double clo_peak_inaccuracy = 1.0; // percentage |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 399 | static Int clo_time_unit = TimeI; |
| 400 | static Int clo_detailed_freq = 10; |
| 401 | static Int clo_max_snapshots = 100; |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 402 | static const HChar* clo_massif_out_file = "massif.out.%p"; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 403 | |
| 404 | static XArray* args_for_massif; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 405 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 406 | static Bool ms_process_cmd_line_option(const HChar* arg) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 407 | { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 408 | const HChar* tmp_str; |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 409 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 410 | // Remember the arg for later use. |
| 411 | VG_(addToXA)(args_for_massif, &arg); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 412 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 413 | if VG_BOOL_CLO(arg, "--heap", clo_heap) {} |
| 414 | else if VG_BINT_CLO(arg, "--heap-admin", clo_heap_admin, 0, 1024) {} |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 415 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 416 | else if VG_BOOL_CLO(arg, "--stacks", clo_stacks) {} |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 417 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 418 | else if VG_BOOL_CLO(arg, "--pages-as-heap", clo_pages_as_heap) {} |
| 419 | |
| 420 | else if VG_BINT_CLO(arg, "--depth", clo_depth, 1, MAX_DEPTH) {} |
| 421 | |
| 422 | else if VG_STR_CLO(arg, "--alloc-fn", tmp_str) { |
| 423 | VG_(addToXA)(alloc_fns, &tmp_str); |
| 424 | } |
| 425 | else if VG_STR_CLO(arg, "--ignore-fn", tmp_str) { |
| 426 | VG_(addToXA)(ignore_fns, &tmp_str); |
| 427 | } |
| 428 | |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 429 | else if VG_DBL_CLO(arg, "--threshold", clo_threshold) { |
| 430 | if (clo_threshold < 0 || clo_threshold > 100) { |
| 431 | VG_(fmsg_bad_option)(arg, |
| 432 | "--threshold must be between 0.0 and 100.0\n"); |
| 433 | } |
| 434 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 435 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 436 | else if VG_DBL_CLO(arg, "--peak-inaccuracy", clo_peak_inaccuracy) {} |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 437 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 438 | else if VG_XACT_CLO(arg, "--time-unit=i", clo_time_unit, TimeI) {} |
| 439 | else if VG_XACT_CLO(arg, "--time-unit=ms", clo_time_unit, TimeMS) {} |
| 440 | else if VG_XACT_CLO(arg, "--time-unit=B", clo_time_unit, TimeB) {} |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 441 | |
njn | e006e6f | 2010-12-08 02:42:38 +0000 | [diff] [blame] | 442 | else if VG_BINT_CLO(arg, "--detailed-freq", clo_detailed_freq, 1, 1000000) {} |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 443 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 444 | else if VG_BINT_CLO(arg, "--max-snapshots", clo_max_snapshots, 10, 1000) {} |
| 445 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 446 | else if VG_STR_CLO(arg, "--massif-out-file", clo_massif_out_file) {} |
njn | f4c665f | 2007-11-20 07:55:40 +0000 | [diff] [blame] | 447 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 448 | else |
| 449 | return VG_(replacement_malloc_process_cmd_line_option)(arg); |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 450 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 451 | return True; |
| 452 | } |
| 453 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 454 | static void ms_print_usage(void) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 455 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 456 | VG_(printf)( |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 457 | " --heap=no|yes profile heap blocks [yes]\n" |
njn | a15b666 | 2009-08-04 05:59:46 +0000 | [diff] [blame] | 458 | " --heap-admin=<size> average admin bytes per heap block;\n" |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 459 | " ignored if --heap=no [8]\n" |
| 460 | " --stacks=no|yes profile stack(s) [no]\n" |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 461 | " --pages-as-heap=no|yes profile memory at the page level [no]\n" |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 462 | " --depth=<number> depth of contexts [30]\n" |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 463 | " --alloc-fn=<name> specify <name> as an alloc function [empty]\n" |
| 464 | " --ignore-fn=<name> ignore heap allocations within <name> [empty]\n" |
njn | 62721e9 | 2007-11-11 22:15:58 +0000 | [diff] [blame] | 465 | " --threshold=<m.n> significance threshold, as a percentage [1.0]\n" |
| 466 | " --peak-inaccuracy=<m.n> maximum peak inaccuracy, as a percentage [1.0]\n" |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 467 | " --time-unit=i|ms|B time unit: instructions executed, milliseconds\n" |
| 468 | " or heap bytes alloc'd/dealloc'd [i]\n" |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 469 | " --detailed-freq=<N> every Nth snapshot should be detailed [10]\n" |
| 470 | " --max-snapshots=<N> maximum number of snapshots recorded [100]\n" |
njn | 374a36d | 2007-11-23 01:41:32 +0000 | [diff] [blame] | 471 | " --massif-out-file=<file> output file name [massif.out.%%p]\n" |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 472 | ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 473 | } |
| 474 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 475 | static void ms_print_debug_usage(void) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 476 | { |
njn | 97db761 | 2009-08-04 02:32:55 +0000 | [diff] [blame] | 477 | VG_(printf)( |
| 478 | " (none)\n" |
| 479 | ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 480 | } |
| 481 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 482 | |
| 483 | //------------------------------------------------------------// |
| 484 | //--- XPts, XTrees and XCons ---// |
| 485 | //------------------------------------------------------------// |
| 486 | |
| 487 | // An XPt represents an "execution point", ie. a code address. Each XPt is |
| 488 | // part of a tree of XPts (an "execution tree", or "XTree"). The details of |
| 489 | // the heap are represented by a single XTree. |
| 490 | // |
| 491 | // The root of the tree is 'alloc_xpt', which represents all allocation |
| 492 | // functions, eg: |
| 493 | // - malloc/calloc/realloc/memalign/new/new[]; |
| 494 | // - user-specified allocation functions (using --alloc-fn); |
| 495 | // - custom allocation (MALLOCLIKE) points |
| 496 | // It's a bit of a fake XPt (ie. its 'ip' is zero), and is only used because |
| 497 | // it makes the code simpler. |
| 498 | // |
| 499 | // Any child of 'alloc_xpt' is called a "top-XPt". The XPts at the bottom |
| 500 | // of an XTree (leaf nodes) are "bottom-XPTs". |
| 501 | // |
| 502 | // Each path from a top-XPt to a bottom-XPt through an XTree gives an |
| 503 | // execution context ("XCon"), ie. a stack trace. (And sub-paths represent |
| 504 | // stack sub-traces.) The number of XCons in an XTree is equal to the |
| 505 | // number of bottom-XPTs in that XTree. |
| 506 | // |
| 507 | // alloc_xpt XTrees are bi-directional. |
| 508 | // | ^ |
| 509 | // v | |
| 510 | // > parent < Example: if child1() calls parent() and child2() |
| 511 | // / | \ also calls parent(), and parent() calls malloc(), |
| 512 | // | / \ | the XTree will look like this. |
| 513 | // | v v | |
| 514 | // child1 child2 |
| 515 | // |
njn | dbeb535 | 2007-12-04 03:15:23 +0000 | [diff] [blame] | 516 | // (Note that malformed stack traces can lead to difficulties. See the |
| 517 | // comment at the bottom of get_XCon.) |
| 518 | // |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 519 | // XTrees and XPts are mirrored by SXTrees and SXPts, where the 'S' is short |
| 520 | // for "saved". When the XTree is duplicated for a snapshot, we duplicate |
| 521 | // it as an SXTree, which is similar but omits some things it does not need, |
| 522 | // and aggregates up insignificant nodes. This is important as an SXTree is |
| 523 | // typically much smaller than an XTree. |
| 524 | |
| 525 | // XXX: make XPt and SXPt extensible arrays, to avoid having to do two |
| 526 | // allocations per Pt. |
| 527 | |
| 528 | typedef struct _XPt XPt; |
| 529 | struct _XPt { |
| 530 | Addr ip; // code address |
| 531 | |
| 532 | // Bottom-XPts: space for the precise context. |
| 533 | // Other XPts: space of all the descendent bottom-XPts. |
| 534 | // Nb: this value goes up and down as the program executes. |
| 535 | SizeT szB; |
| 536 | |
| 537 | XPt* parent; // pointer to parent XPt |
| 538 | |
| 539 | // Children. |
| 540 | // n_children and max_children are 32-bit integers. 16-bit integers |
| 541 | // are too small -- a very big program might have more than 65536 |
| 542 | // allocation points (ie. top-XPts) -- Konqueror starting up has 1800. |
| 543 | UInt n_children; // number of children |
| 544 | UInt max_children; // capacity of children array |
| 545 | XPt** children; // pointers to children XPts |
| 546 | }; |
| 547 | |
| 548 | typedef |
| 549 | enum { |
| 550 | SigSXPt, |
| 551 | InsigSXPt |
| 552 | } |
| 553 | SXPtTag; |
| 554 | |
| 555 | typedef struct _SXPt SXPt; |
| 556 | struct _SXPt { |
| 557 | SXPtTag tag; |
| 558 | SizeT szB; // memory size for the node, be it Sig or Insig |
| 559 | union { |
| 560 | // An SXPt representing a single significant code location. Much like |
| 561 | // an XPt, minus the fields that aren't necessary. |
| 562 | struct { |
| 563 | Addr ip; |
| 564 | UInt n_children; |
| 565 | SXPt** children; |
| 566 | } |
| 567 | Sig; |
| 568 | |
| 569 | // An SXPt representing one or more code locations, all below the |
| 570 | // significance threshold. |
| 571 | struct { |
| 572 | Int n_xpts; // number of aggregated XPts |
| 573 | } |
| 574 | Insig; |
| 575 | }; |
| 576 | }; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 577 | |
| 578 | // Fake XPt representing all allocation functions like malloc(). Acts as |
| 579 | // parent node to all top-XPts. |
| 580 | static XPt* alloc_xpt; |
| 581 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 582 | static XPt* new_XPt(Addr ip, XPt* parent) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 583 | { |
philippe | 6e4b713 | 2013-01-18 06:19:49 +0000 | [diff] [blame] | 584 | // XPts are never freed, so we can use VG_(perm_malloc) to allocate them. |
| 585 | // Note that we cannot use VG_(perm_malloc) for the 'children' array, because |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 586 | // that needs to be resizable. |
philippe | 6e4b713 | 2013-01-18 06:19:49 +0000 | [diff] [blame] | 587 | XPt* xpt = VG_(perm_malloc)(sizeof(XPt), vg_alignof(XPt)); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 588 | xpt->ip = ip; |
| 589 | xpt->szB = 0; |
| 590 | xpt->parent = parent; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 591 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 592 | // We don't initially allocate any space for children. We let that |
| 593 | // happen on demand. Many XPts (ie. all the bottom-XPts) don't have any |
| 594 | // children anyway. |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 595 | xpt->n_children = 0; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 596 | xpt->max_children = 0; |
| 597 | xpt->children = NULL; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 598 | |
| 599 | // Update statistics |
| 600 | n_xpts++; |
| 601 | |
| 602 | return xpt; |
| 603 | } |
| 604 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 605 | static void add_child_xpt(XPt* parent, XPt* child) |
| 606 | { |
| 607 | // Expand 'children' if necessary. |
| 608 | tl_assert(parent->n_children <= parent->max_children); |
| 609 | if (parent->n_children == parent->max_children) { |
njn | 84f32b2 | 2009-02-10 07:14:37 +0000 | [diff] [blame] | 610 | if (0 == parent->max_children) { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 611 | parent->max_children = 4; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 612 | parent->children = VG_(malloc)( "ms.main.acx.1", |
| 613 | parent->max_children * sizeof(XPt*) ); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 614 | n_xpt_init_expansions++; |
| 615 | } else { |
| 616 | parent->max_children *= 2; // Double size |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 617 | parent->children = VG_(realloc)( "ms.main.acx.2", |
| 618 | parent->children, |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 619 | parent->max_children * sizeof(XPt*) ); |
| 620 | n_xpt_later_expansions++; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | // Insert new child XPt in parent's children list. |
| 625 | parent->children[ parent->n_children++ ] = child; |
| 626 | } |
| 627 | |
| 628 | // Reverse comparison for a reverse sort -- biggest to smallest. |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 629 | static Int SXPt_revcmp_szB(const void* n1, const void* n2) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 630 | { |
florian | 3e79863 | 2012-11-24 19:41:54 +0000 | [diff] [blame] | 631 | const SXPt* sxpt1 = *(const SXPt *const *)n1; |
| 632 | const SXPt* sxpt2 = *(const SXPt *const *)n2; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 633 | return ( sxpt1->szB < sxpt2->szB ? 1 |
| 634 | : sxpt1->szB > sxpt2->szB ? -1 |
| 635 | : 0); |
| 636 | } |
| 637 | |
| 638 | //------------------------------------------------------------// |
| 639 | //--- XTree Operations ---// |
| 640 | //------------------------------------------------------------// |
| 641 | |
| 642 | // Duplicates an XTree as an SXTree. |
| 643 | static SXPt* dup_XTree(XPt* xpt, SizeT total_szB) |
| 644 | { |
| 645 | Int i, n_sig_children, n_insig_children, n_child_sxpts; |
njn | dbeb535 | 2007-12-04 03:15:23 +0000 | [diff] [blame] | 646 | SizeT sig_child_threshold_szB; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 647 | SXPt* sxpt; |
| 648 | |
| 649 | // Number of XPt children Action for SXPT |
| 650 | // ------------------ --------------- |
| 651 | // 0 sig, 0 insig alloc 0 children |
| 652 | // N sig, 0 insig alloc N children, dup all |
| 653 | // N sig, M insig alloc N+1, dup first N, aggregate remaining M |
| 654 | // 0 sig, M insig alloc 1, aggregate M |
| 655 | |
| 656 | // Work out how big a child must be to be significant. If the current |
| 657 | // total_szB is zero, then we set it to 1, which means everything will be |
| 658 | // judged insignificant -- this is sensible, as there's no point showing |
| 659 | // any detail for this case. Unless they used --threshold=0, in which |
| 660 | // case we show them everything because that's what they asked for. |
| 661 | // |
| 662 | // Nb: We do this once now, rather than once per child, because if we do |
| 663 | // that the cost of all the divisions adds up to something significant. |
njn | 84f32b2 | 2009-02-10 07:14:37 +0000 | [diff] [blame] | 664 | if (0 == total_szB && 0 != clo_threshold) { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 665 | sig_child_threshold_szB = 1; |
| 666 | } else { |
njn | 62721e9 | 2007-11-11 22:15:58 +0000 | [diff] [blame] | 667 | sig_child_threshold_szB = (SizeT)((total_szB * clo_threshold) / 100); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | // How many children are significant? And do we need an aggregate SXPt? |
| 671 | n_sig_children = 0; |
| 672 | for (i = 0; i < xpt->n_children; i++) { |
| 673 | if (xpt->children[i]->szB >= sig_child_threshold_szB) { |
| 674 | n_sig_children++; |
| 675 | } |
| 676 | } |
| 677 | n_insig_children = xpt->n_children - n_sig_children; |
| 678 | n_child_sxpts = n_sig_children + ( n_insig_children > 0 ? 1 : 0 ); |
| 679 | |
| 680 | // Duplicate the XPt. |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 681 | sxpt = VG_(malloc)("ms.main.dX.1", sizeof(SXPt)); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 682 | n_sxpt_allocs++; |
| 683 | sxpt->tag = SigSXPt; |
| 684 | sxpt->szB = xpt->szB; |
| 685 | sxpt->Sig.ip = xpt->ip; |
| 686 | sxpt->Sig.n_children = n_child_sxpts; |
| 687 | |
| 688 | // Create the SXPt's children. |
| 689 | if (n_child_sxpts > 0) { |
| 690 | Int j; |
njn | dbeb535 | 2007-12-04 03:15:23 +0000 | [diff] [blame] | 691 | SizeT sig_children_szB = 0, insig_children_szB = 0; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 692 | sxpt->Sig.children = VG_(malloc)("ms.main.dX.2", |
| 693 | n_child_sxpts * sizeof(SXPt*)); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 694 | |
njn | dbeb535 | 2007-12-04 03:15:23 +0000 | [diff] [blame] | 695 | // Duplicate the significant children. (Nb: sig_children_szB + |
| 696 | // insig_children_szB doesn't necessarily equal xpt->szB.) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 697 | j = 0; |
| 698 | for (i = 0; i < xpt->n_children; i++) { |
| 699 | if (xpt->children[i]->szB >= sig_child_threshold_szB) { |
| 700 | sxpt->Sig.children[j++] = dup_XTree(xpt->children[i], total_szB); |
njn | dbeb535 | 2007-12-04 03:15:23 +0000 | [diff] [blame] | 701 | sig_children_szB += xpt->children[i]->szB; |
| 702 | } else { |
| 703 | insig_children_szB += xpt->children[i]->szB; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | |
| 707 | // Create the SXPt for the insignificant children, if any, and put it |
| 708 | // in the last child entry. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 709 | if (n_insig_children > 0) { |
| 710 | // Nb: We 'n_sxpt_allocs' here because creating an Insig SXPt |
| 711 | // doesn't involve a call to dup_XTree(). |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 712 | SXPt* insig_sxpt = VG_(malloc)("ms.main.dX.3", sizeof(SXPt)); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 713 | n_sxpt_allocs++; |
| 714 | insig_sxpt->tag = InsigSXPt; |
| 715 | insig_sxpt->szB = insig_children_szB; |
| 716 | insig_sxpt->Insig.n_xpts = n_insig_children; |
| 717 | sxpt->Sig.children[n_sig_children] = insig_sxpt; |
| 718 | } |
| 719 | } else { |
| 720 | sxpt->Sig.children = NULL; |
| 721 | } |
| 722 | |
| 723 | return sxpt; |
| 724 | } |
| 725 | |
| 726 | static void free_SXTree(SXPt* sxpt) |
| 727 | { |
| 728 | Int i; |
| 729 | tl_assert(sxpt != NULL); |
| 730 | |
| 731 | switch (sxpt->tag) { |
| 732 | case SigSXPt: |
| 733 | // Free all children SXPts, then the children array. |
| 734 | for (i = 0; i < sxpt->Sig.n_children; i++) { |
| 735 | free_SXTree(sxpt->Sig.children[i]); |
| 736 | sxpt->Sig.children[i] = NULL; |
| 737 | } |
| 738 | VG_(free)(sxpt->Sig.children); sxpt->Sig.children = NULL; |
| 739 | break; |
| 740 | |
| 741 | case InsigSXPt: |
| 742 | break; |
| 743 | |
| 744 | default: tl_assert2(0, "free_SXTree: unknown SXPt tag"); |
| 745 | } |
| 746 | |
| 747 | // Free the SXPt itself. |
| 748 | VG_(free)(sxpt); sxpt = NULL; |
| 749 | n_sxpt_frees++; |
| 750 | } |
| 751 | |
| 752 | // Sanity checking: we periodically check the heap XTree with |
| 753 | // ms_expensive_sanity_check. |
| 754 | static void sanity_check_XTree(XPt* xpt, XPt* parent) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 755 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 756 | tl_assert(xpt != NULL); |
| 757 | |
| 758 | // Check back-pointer. |
| 759 | tl_assert2(xpt->parent == parent, |
| 760 | "xpt->parent = %p, parent = %p\n", xpt->parent, parent); |
| 761 | |
| 762 | // Check children counts look sane. |
| 763 | tl_assert(xpt->n_children <= xpt->max_children); |
| 764 | |
njn | dbeb535 | 2007-12-04 03:15:23 +0000 | [diff] [blame] | 765 | // Unfortunately, xpt's size is not necessarily equal to the sum of xpt's |
| 766 | // children's sizes. See comment at the bottom of get_XCon. |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 767 | } |
| 768 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 769 | // Sanity checking: we check SXTrees (which are in snapshots) after |
| 770 | // snapshots are created, before they are deleted, and before they are |
| 771 | // printed. |
| 772 | static void sanity_check_SXTree(SXPt* sxpt) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 773 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 774 | Int i; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 775 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 776 | tl_assert(sxpt != NULL); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 777 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 778 | // Check the sum of any children szBs equals the SXPt's szB. Check the |
| 779 | // children at the same time. |
| 780 | switch (sxpt->tag) { |
| 781 | case SigSXPt: { |
| 782 | if (sxpt->Sig.n_children > 0) { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 783 | for (i = 0; i < sxpt->Sig.n_children; i++) { |
| 784 | sanity_check_SXTree(sxpt->Sig.children[i]); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 785 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 786 | } |
| 787 | break; |
| 788 | } |
| 789 | case InsigSXPt: |
| 790 | break; // do nothing |
| 791 | |
| 792 | default: tl_assert2(0, "sanity_check_SXTree: unknown SXPt tag"); |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | |
| 797 | //------------------------------------------------------------// |
| 798 | //--- XCon Operations ---// |
| 799 | //------------------------------------------------------------// |
| 800 | |
| 801 | // This is the limit on the number of removed alloc-fns that can be in a |
| 802 | // single XCon. |
| 803 | #define MAX_OVERESTIMATE 50 |
| 804 | #define MAX_IPS (MAX_DEPTH + MAX_OVERESTIMATE) |
| 805 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 806 | // Determine if the given IP belongs to a function that should be ignored. |
| 807 | static Bool fn_should_be_ignored(Addr ip) |
| 808 | { |
florian | 46cc045 | 2014-10-25 19:20:38 +0000 | [diff] [blame] | 809 | const HChar *buf; |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 810 | return |
florian | 46cc045 | 2014-10-25 19:20:38 +0000 | [diff] [blame] | 811 | ( VG_(get_fnname)(ip, &buf) && is_member_fn(ignore_fns, buf) |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 812 | ? True : False ); |
| 813 | } |
| 814 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 815 | // Get the stack trace for an XCon, filtering out uninteresting entries: |
| 816 | // alloc-fns and entries above alloc-fns, and entries below main-or-below-main. |
| 817 | // Eg: alloc-fn1 / alloc-fn2 / a / b / main / (below main) / c |
| 818 | // becomes: a / b / main |
| 819 | // Nb: it's possible to end up with an empty trace, eg. if 'main' is marked |
| 820 | // as an alloc-fn. This is ok. |
| 821 | static |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 822 | Int get_IPs( ThreadId tid, Bool exclude_first_entry, Addr ips[]) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 823 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 824 | Int n_ips, i, n_alloc_fns_removed; |
| 825 | Int overestimate; |
| 826 | Bool redo; |
| 827 | |
| 828 | // We ask for a few more IPs than clo_depth suggests we need. Then we |
| 829 | // remove every entry that is an alloc-fn. Depending on the |
| 830 | // circumstances, we may need to redo it all, asking for more IPs. |
| 831 | // Details: |
| 832 | // - If the original stack trace is smaller than asked-for, redo=False |
| 833 | // - Else if after filtering we have >= clo_depth IPs, redo=False |
| 834 | // - Else redo=True |
| 835 | // In other words, to redo, we'd have to get a stack trace as big as we |
| 836 | // asked for and remove more than 'overestimate' alloc-fns. |
| 837 | |
| 838 | // Main loop. |
| 839 | redo = True; // Assume this to begin with. |
| 840 | for (overestimate = 3; redo; overestimate += 6) { |
| 841 | // This should never happen -- would require MAX_OVERESTIMATE |
| 842 | // alloc-fns to be removed from the stack trace. |
| 843 | if (overestimate > MAX_OVERESTIMATE) |
| 844 | VG_(tool_panic)("get_IPs: ips[] too small, inc. MAX_OVERESTIMATE?"); |
| 845 | |
| 846 | // Ask for more IPs than clo_depth suggests we need. |
sewardj | 39f3423 | 2007-11-09 23:02:28 +0000 | [diff] [blame] | 847 | n_ips = VG_(get_StackTrace)( tid, ips, clo_depth + overestimate, |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 848 | NULL/*array to dump SP values in*/, |
| 849 | NULL/*array to dump FP values in*/, |
| 850 | 0/*first_ip_delta*/ ); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 851 | tl_assert(n_ips > 0); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 852 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 853 | // If the original stack trace is smaller than asked-for, redo=False. |
| 854 | if (n_ips < clo_depth + overestimate) { redo = False; } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 855 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 856 | // Filter out alloc fns. If requested, we automatically remove the |
| 857 | // first entry (which presumably will be something like malloc or |
| 858 | // __builtin_new that we're sure to filter out) without looking at it, |
| 859 | // because VG_(get_fnname) is expensive. |
| 860 | n_alloc_fns_removed = ( exclude_first_entry ? 1 : 0 ); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 861 | for (i = n_alloc_fns_removed; i < n_ips; i++) { |
florian | 46cc045 | 2014-10-25 19:20:38 +0000 | [diff] [blame] | 862 | const HChar *buf; |
| 863 | if (VG_(get_fnname)(ips[i], &buf)) { |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 864 | if (is_member_fn(alloc_fns, buf)) { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 865 | n_alloc_fns_removed++; |
| 866 | } else { |
| 867 | break; |
| 868 | } |
| 869 | } |
| 870 | } |
| 871 | // Remove the alloc fns by shuffling the rest down over them. |
| 872 | n_ips -= n_alloc_fns_removed; |
| 873 | for (i = 0; i < n_ips; i++) { |
| 874 | ips[i] = ips[i + n_alloc_fns_removed]; |
| 875 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 876 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 877 | // If after filtering we have >= clo_depth IPs, redo=False |
| 878 | if (n_ips >= clo_depth) { |
| 879 | redo = False; |
| 880 | n_ips = clo_depth; // Ignore any IPs below --depth. |
| 881 | } |
| 882 | |
| 883 | if (redo) { |
| 884 | n_XCon_redos++; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 885 | } |
| 886 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 887 | return n_ips; |
| 888 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 889 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 890 | // Gets an XCon and puts it in the tree. Returns the XCon's bottom-XPt. |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 891 | // Unless the allocation should be ignored, in which case we return NULL. |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 892 | static XPt* get_XCon( ThreadId tid, Bool exclude_first_entry ) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 893 | { |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 894 | static Addr ips[MAX_IPS]; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 895 | Int i; |
| 896 | XPt* xpt = alloc_xpt; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 897 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 898 | // After this call, the IPs we want are in ips[0]..ips[n_ips-1]. |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 899 | Int n_ips = get_IPs(tid, exclude_first_entry, ips); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 900 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 901 | // Should we ignore this allocation? (Nb: n_ips can be zero, eg. if |
| 902 | // 'main' is marked as an alloc-fn.) |
| 903 | if (n_ips > 0 && fn_should_be_ignored(ips[0])) { |
| 904 | return NULL; |
| 905 | } |
| 906 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 907 | // Now do the search/insertion of the XCon. |
| 908 | for (i = 0; i < n_ips; i++) { |
| 909 | Addr ip = ips[i]; |
| 910 | Int ch; |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 911 | // Look for IP in xpt's children. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 912 | // Linear search, ugh -- about 10% of time for konqueror startup tried |
| 913 | // caching last result, only hit about 4% for konqueror. |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 914 | // Nb: this search hits about 98% of the time for konqueror |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 915 | for (ch = 0; True; ch++) { |
| 916 | if (ch == xpt->n_children) { |
| 917 | // IP not found in the children. |
| 918 | // Create and add new child XPt, then stop. |
| 919 | XPt* new_child_xpt = new_XPt(ip, xpt); |
| 920 | add_child_xpt(xpt, new_child_xpt); |
| 921 | xpt = new_child_xpt; |
| 922 | break; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 923 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 924 | } else if (ip == xpt->children[ch]->ip) { |
| 925 | // Found the IP in the children, stop. |
| 926 | xpt = xpt->children[ch]; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 927 | break; |
| 928 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 929 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 930 | } |
njn | dbeb535 | 2007-12-04 03:15:23 +0000 | [diff] [blame] | 931 | |
| 932 | // [Note: several comments refer to this comment. Do not delete it |
| 933 | // without updating them.] |
| 934 | // |
| 935 | // A complication... If all stack traces were well-formed, then the |
| 936 | // returned xpt would always be a bottom-XPt. As a consequence, an XPt's |
| 937 | // size would always be equal to the sum of its children's sizes, which |
| 938 | // is an excellent sanity check. |
| 939 | // |
| 940 | // Unfortunately, stack traces occasionally are malformed, ie. truncated. |
| 941 | // This allows a stack trace to be a sub-trace of another, eg. a/b/c is a |
| 942 | // sub-trace of a/b/c/d. So we can't assume this xpt is a bottom-XPt; |
| 943 | // nor can we do sanity check an XPt's size against its children's sizes. |
| 944 | // This is annoying, but must be dealt with. (Older versions of Massif |
| 945 | // had this assertion in, and it was reported to fail by real users a |
| 946 | // couple of times.) Even more annoyingly, I can't come up with a simple |
| 947 | // test case that exhibit such a malformed stack trace, so I can't |
| 948 | // regression test it. Sigh. |
| 949 | // |
| 950 | // However, we can print a warning, so that if it happens (unexpectedly) |
| 951 | // in existing regression tests we'll know. Also, it warns users that |
| 952 | // the output snapshots may not add up the way they might expect. |
| 953 | // |
| 954 | //tl_assert(0 == xpt->n_children); // Must be bottom-XPt |
| 955 | if (0 != xpt->n_children) { |
| 956 | static Int n_moans = 0; |
| 957 | if (n_moans < 3) { |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 958 | VG_(umsg)( |
| 959 | "Warning: Malformed stack trace detected. In Massif's output,\n"); |
| 960 | VG_(umsg)( |
| 961 | " the size of an entry's child entries may not sum up\n"); |
| 962 | VG_(umsg)( |
| 963 | " to the entry's size as they normally do.\n"); |
njn | dbeb535 | 2007-12-04 03:15:23 +0000 | [diff] [blame] | 964 | n_moans++; |
| 965 | if (3 == n_moans) |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 966 | VG_(umsg)( |
| 967 | " (And Massif now won't warn about this again.)\n"); |
njn | dbeb535 | 2007-12-04 03:15:23 +0000 | [diff] [blame] | 968 | } |
| 969 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 970 | return xpt; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 971 | } |
| 972 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 973 | // Update 'szB' of every XPt in the XCon, by percolating upwards. |
| 974 | static void update_XCon(XPt* xpt, SSizeT space_delta) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 975 | { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 976 | tl_assert(clo_heap); |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 977 | tl_assert(NULL != xpt); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 978 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 979 | if (0 == space_delta) |
| 980 | return; |
| 981 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 982 | while (xpt != alloc_xpt) { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 983 | if (space_delta < 0) tl_assert(xpt->szB >= -space_delta); |
| 984 | xpt->szB += space_delta; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 985 | xpt = xpt->parent; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 986 | } |
| 987 | if (space_delta < 0) tl_assert(alloc_xpt->szB >= -space_delta); |
| 988 | alloc_xpt->szB += space_delta; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 992 | //------------------------------------------------------------// |
| 993 | //--- Snapshots ---// |
| 994 | //------------------------------------------------------------// |
| 995 | |
| 996 | // Snapshots are done in a way so that we always have a reasonable number of |
| 997 | // them. We start by taking them quickly. Once we hit our limit, we cull |
| 998 | // some (eg. half), and start taking them more slowly. Once we hit the |
| 999 | // limit again, we again cull and then take them even more slowly, and so |
| 1000 | // on. |
| 1001 | |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 1002 | // Time is measured either in i or ms or bytes, depending on the --time-unit |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1003 | // option. It's a Long because it can exceed 32-bits reasonably easily, and |
| 1004 | // because we need to allow negative values to represent unset times. |
| 1005 | typedef Long Time; |
| 1006 | |
| 1007 | #define UNUSED_SNAPSHOT_TIME -333 // A conspicuous negative number. |
| 1008 | |
| 1009 | typedef |
| 1010 | enum { |
| 1011 | Normal = 77, |
| 1012 | Peak, |
| 1013 | Unused |
| 1014 | } |
| 1015 | SnapshotKind; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1016 | |
| 1017 | typedef |
| 1018 | struct { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1019 | SnapshotKind kind; |
| 1020 | Time time; |
| 1021 | SizeT heap_szB; |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1022 | SizeT heap_extra_szB;// Heap slop + admin bytes. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1023 | SizeT stacks_szB; |
| 1024 | SXPt* alloc_sxpt; // Heap XTree root, if a detailed snapshot, |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1025 | } // otherwise NULL. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1026 | Snapshot; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1027 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1028 | static UInt next_snapshot_i = 0; // Index of where next snapshot will go. |
| 1029 | static Snapshot* snapshots; // Array of snapshots. |
| 1030 | |
| 1031 | static Bool is_snapshot_in_use(Snapshot* snapshot) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1032 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1033 | if (Unused == snapshot->kind) { |
| 1034 | // If snapshot is unused, check all the fields are unset. |
| 1035 | tl_assert(snapshot->time == UNUSED_SNAPSHOT_TIME); |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1036 | tl_assert(snapshot->heap_extra_szB == 0); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1037 | tl_assert(snapshot->heap_szB == 0); |
| 1038 | tl_assert(snapshot->stacks_szB == 0); |
| 1039 | tl_assert(snapshot->alloc_sxpt == NULL); |
| 1040 | return False; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1041 | } else { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1042 | tl_assert(snapshot->time != UNUSED_SNAPSHOT_TIME); |
| 1043 | return True; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1047 | static Bool is_detailed_snapshot(Snapshot* snapshot) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1048 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1049 | return (snapshot->alloc_sxpt ? True : False); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1052 | static Bool is_uncullable_snapshot(Snapshot* snapshot) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1053 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1054 | return &snapshots[0] == snapshot // First snapshot |
| 1055 | || &snapshots[next_snapshot_i-1] == snapshot // Last snapshot |
| 1056 | || snapshot->kind == Peak; // Peak snapshot |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1059 | static void sanity_check_snapshot(Snapshot* snapshot) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1060 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1061 | if (snapshot->alloc_sxpt) { |
| 1062 | sanity_check_SXTree(snapshot->alloc_sxpt); |
| 1063 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1066 | // All the used entries should look used, all the unused ones should be clear. |
| 1067 | static void sanity_check_snapshots_array(void) |
| 1068 | { |
| 1069 | Int i; |
| 1070 | for (i = 0; i < next_snapshot_i; i++) { |
| 1071 | tl_assert( is_snapshot_in_use( & snapshots[i] )); |
| 1072 | } |
| 1073 | for ( ; i < clo_max_snapshots; i++) { |
| 1074 | tl_assert(!is_snapshot_in_use( & snapshots[i] )); |
| 1075 | } |
| 1076 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1077 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1078 | // This zeroes all the fields in the snapshot, but does not free the heap |
| 1079 | // XTree if present. It also does a sanity check unless asked not to; we |
| 1080 | // can't sanity check at startup when clearing the initial snapshots because |
| 1081 | // they're full of junk. |
| 1082 | static void clear_snapshot(Snapshot* snapshot, Bool do_sanity_check) |
| 1083 | { |
| 1084 | if (do_sanity_check) sanity_check_snapshot(snapshot); |
| 1085 | snapshot->kind = Unused; |
| 1086 | snapshot->time = UNUSED_SNAPSHOT_TIME; |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1087 | snapshot->heap_extra_szB = 0; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1088 | snapshot->heap_szB = 0; |
| 1089 | snapshot->stacks_szB = 0; |
| 1090 | snapshot->alloc_sxpt = NULL; |
| 1091 | } |
| 1092 | |
| 1093 | // This zeroes all the fields in the snapshot, and frees the heap XTree if |
| 1094 | // present. |
| 1095 | static void delete_snapshot(Snapshot* snapshot) |
| 1096 | { |
| 1097 | // Nb: if there's an XTree, we free it after calling clear_snapshot, |
| 1098 | // because clear_snapshot does a sanity check which includes checking the |
| 1099 | // XTree. |
| 1100 | SXPt* tmp_sxpt = snapshot->alloc_sxpt; |
| 1101 | clear_snapshot(snapshot, /*do_sanity_check*/True); |
| 1102 | if (tmp_sxpt) { |
| 1103 | free_SXTree(tmp_sxpt); |
| 1104 | } |
| 1105 | } |
| 1106 | |
florian | e58e8a7 | 2012-10-20 19:57:16 +0000 | [diff] [blame] | 1107 | static void VERB_snapshot(Int verbosity, const HChar* prefix, Int i) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1108 | { |
| 1109 | Snapshot* snapshot = &snapshots[i]; |
florian | e58e8a7 | 2012-10-20 19:57:16 +0000 | [diff] [blame] | 1110 | const HChar* suffix; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1111 | switch (snapshot->kind) { |
| 1112 | case Peak: suffix = "p"; break; |
| 1113 | case Normal: suffix = ( is_detailed_snapshot(snapshot) ? "d" : "." ); break; |
| 1114 | case Unused: suffix = "u"; break; |
| 1115 | default: |
| 1116 | tl_assert2(0, "VERB_snapshot: unknown snapshot kind: %d", snapshot->kind); |
| 1117 | } |
florian | 16eef85 | 2015-08-03 21:05:20 +0000 | [diff] [blame] | 1118 | VERB(verbosity, "%s S%s%3d (t:%lld, hp:%lu, ex:%lu, st:%lu)\n", |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1119 | prefix, suffix, i, |
| 1120 | snapshot->time, |
| 1121 | snapshot->heap_szB, |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1122 | snapshot->heap_extra_szB, |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1123 | snapshot->stacks_szB |
| 1124 | ); |
| 1125 | } |
| 1126 | |
| 1127 | // Cull half the snapshots; we choose those that represent the smallest |
| 1128 | // time-spans, because that gives us the most even distribution of snapshots |
| 1129 | // over time. (It's possible to lose interesting spikes, however.) |
| 1130 | // |
| 1131 | // Algorithm for N snapshots: We find the snapshot representing the smallest |
| 1132 | // timeframe, and remove it. We repeat this until (N/2) snapshots are gone. |
| 1133 | // We have to do this one snapshot at a time, rather than finding the (N/2) |
| 1134 | // smallest snapshots in one hit, because when a snapshot is removed, its |
| 1135 | // neighbours immediately cover greater timespans. So it's O(N^2), but N is |
| 1136 | // small, and it's not done very often. |
| 1137 | // |
| 1138 | // Once we're done, we return the new smallest interval between snapshots. |
| 1139 | // That becomes our minimum time interval. |
| 1140 | static UInt cull_snapshots(void) |
| 1141 | { |
| 1142 | Int i, jp, j, jn, min_timespan_i; |
| 1143 | Int n_deleted = 0; |
| 1144 | Time min_timespan; |
| 1145 | |
| 1146 | n_cullings++; |
| 1147 | |
| 1148 | // Sets j to the index of the first not-yet-removed snapshot at or after i |
| 1149 | #define FIND_SNAPSHOT(i, j) \ |
| 1150 | for (j = i; \ |
| 1151 | j < clo_max_snapshots && !is_snapshot_in_use(&snapshots[j]); \ |
| 1152 | j++) { } |
| 1153 | |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1154 | VERB(2, "Culling...\n"); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1155 | |
| 1156 | // First we remove enough snapshots by clearing them in-place. Once |
| 1157 | // that's done, we can slide the remaining ones down. |
| 1158 | for (i = 0; i < clo_max_snapshots/2; i++) { |
| 1159 | // Find the snapshot representing the smallest timespan. The timespan |
| 1160 | // for snapshot n = d(N-1,N)+d(N,N+1), where d(A,B) is the time between |
| 1161 | // snapshot A and B. We don't consider the first and last snapshots for |
| 1162 | // removal. |
| 1163 | Snapshot* min_snapshot; |
| 1164 | Int min_j; |
| 1165 | |
| 1166 | // Initial triple: (prev, curr, next) == (jp, j, jn) |
| 1167 | // Initial min_timespan is the first one. |
| 1168 | jp = 0; |
| 1169 | FIND_SNAPSHOT(1, j); |
| 1170 | FIND_SNAPSHOT(j+1, jn); |
| 1171 | min_timespan = 0x7fffffffffffffffLL; |
| 1172 | min_j = -1; |
| 1173 | while (jn < clo_max_snapshots) { |
| 1174 | Time timespan = snapshots[jn].time - snapshots[jp].time; |
| 1175 | tl_assert(timespan >= 0); |
| 1176 | // Nb: We never cull the peak snapshot. |
| 1177 | if (Peak != snapshots[j].kind && timespan < min_timespan) { |
| 1178 | min_timespan = timespan; |
| 1179 | min_j = j; |
| 1180 | } |
| 1181 | // Move on to next triple |
| 1182 | jp = j; |
| 1183 | j = jn; |
| 1184 | FIND_SNAPSHOT(jn+1, jn); |
| 1185 | } |
| 1186 | // We've found the least important snapshot, now delete it. First |
| 1187 | // print it if necessary. |
| 1188 | tl_assert(-1 != min_j); // Check we found a minimum. |
| 1189 | min_snapshot = & snapshots[ min_j ]; |
| 1190 | if (VG_(clo_verbosity) > 1) { |
florian | ef901ff | 2014-11-16 09:56:42 +0000 | [diff] [blame] | 1191 | HChar buf[64]; // large enough |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1192 | VG_(snprintf)(buf, 64, " %3d (t-span = %lld)", i, min_timespan); |
| 1193 | VERB_snapshot(2, buf, min_j); |
| 1194 | } |
| 1195 | delete_snapshot(min_snapshot); |
| 1196 | n_deleted++; |
| 1197 | } |
| 1198 | |
| 1199 | // Slide down the remaining snapshots over the removed ones. First set i |
| 1200 | // to point to the first empty slot, and j to the first full slot after |
| 1201 | // i. Then slide everything down. |
| 1202 | for (i = 0; is_snapshot_in_use( &snapshots[i] ); i++) { } |
| 1203 | for (j = i; !is_snapshot_in_use( &snapshots[j] ); j++) { } |
| 1204 | for ( ; j < clo_max_snapshots; j++) { |
| 1205 | if (is_snapshot_in_use( &snapshots[j] )) { |
| 1206 | snapshots[i++] = snapshots[j]; |
| 1207 | clear_snapshot(&snapshots[j], /*do_sanity_check*/True); |
| 1208 | } |
| 1209 | } |
| 1210 | next_snapshot_i = i; |
| 1211 | |
| 1212 | // Check snapshots array looks ok after changes. |
| 1213 | sanity_check_snapshots_array(); |
| 1214 | |
| 1215 | // Find the minimum timespan remaining; that will be our new minimum |
| 1216 | // time interval. Note that above we were finding timespans by measuring |
| 1217 | // two intervals around a snapshot that was under consideration for |
| 1218 | // deletion. Here we only measure single intervals because all the |
| 1219 | // deletions have occurred. |
| 1220 | // |
| 1221 | // But we have to be careful -- some snapshots (eg. snapshot 0, and the |
| 1222 | // peak snapshot) are uncullable. If two uncullable snapshots end up |
| 1223 | // next to each other, they'll never be culled (assuming the peak doesn't |
| 1224 | // change), and the time gap between them will not change. However, the |
| 1225 | // time between the remaining cullable snapshots will grow ever larger. |
| 1226 | // This means that the min_timespan found will always be that between the |
| 1227 | // two uncullable snapshots, and it will be much smaller than it should |
| 1228 | // be. To avoid this problem, when computing the minimum timespan, we |
| 1229 | // ignore any timespans between two uncullable snapshots. |
| 1230 | tl_assert(next_snapshot_i > 1); |
| 1231 | min_timespan = 0x7fffffffffffffffLL; |
| 1232 | min_timespan_i = -1; |
| 1233 | for (i = 1; i < next_snapshot_i; i++) { |
| 1234 | if (is_uncullable_snapshot(&snapshots[i]) && |
| 1235 | is_uncullable_snapshot(&snapshots[i-1])) |
| 1236 | { |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1237 | VERB(2, "(Ignoring interval %d--%d when computing minimum)\n", i-1, i); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1238 | } else { |
| 1239 | Time timespan = snapshots[i].time - snapshots[i-1].time; |
| 1240 | tl_assert(timespan >= 0); |
| 1241 | if (timespan < min_timespan) { |
| 1242 | min_timespan = timespan; |
| 1243 | min_timespan_i = i; |
| 1244 | } |
| 1245 | } |
| 1246 | } |
| 1247 | tl_assert(-1 != min_timespan_i); // Check we found a minimum. |
| 1248 | |
| 1249 | // Print remaining snapshots, if necessary. |
| 1250 | if (VG_(clo_verbosity) > 1) { |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1251 | VERB(2, "Finished culling (%3d of %3d deleted)\n", |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1252 | n_deleted, clo_max_snapshots); |
| 1253 | for (i = 0; i < next_snapshot_i; i++) { |
| 1254 | VERB_snapshot(2, " post-cull", i); |
| 1255 | } |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1256 | VERB(2, "New time interval = %lld (between snapshots %d and %d)\n", |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1257 | min_timespan, min_timespan_i-1, min_timespan_i); |
| 1258 | } |
| 1259 | |
| 1260 | return min_timespan; |
| 1261 | } |
| 1262 | |
| 1263 | static Time get_time(void) |
| 1264 | { |
| 1265 | // Get current time, in whatever time unit we're using. |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 1266 | if (clo_time_unit == TimeI) { |
| 1267 | return guest_instrs_executed; |
| 1268 | } else if (clo_time_unit == TimeMS) { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1269 | // Some stuff happens between the millisecond timer being initialised |
| 1270 | // to zero and us taking our first snapshot. We determine that time |
| 1271 | // gap so we can subtract it from all subsequent times so that our |
| 1272 | // first snapshot is considered to be at t = 0ms. Unfortunately, a |
| 1273 | // bunch of symbols get read after the first snapshot is taken but |
| 1274 | // before the second one (which is triggered by the first allocation), |
| 1275 | // so when the time-unit is 'ms' we always have a big gap between the |
| 1276 | // first two snapshots. But at least users won't have to wonder why |
| 1277 | // the first snapshot isn't at t=0. |
| 1278 | static Bool is_first_get_time = True; |
| 1279 | static Time start_time_ms; |
| 1280 | if (is_first_get_time) { |
| 1281 | start_time_ms = VG_(read_millisecond_timer)(); |
| 1282 | is_first_get_time = False; |
| 1283 | return 0; |
| 1284 | } else { |
| 1285 | return VG_(read_millisecond_timer)() - start_time_ms; |
| 1286 | } |
| 1287 | } else if (clo_time_unit == TimeB) { |
| 1288 | return total_allocs_deallocs_szB; |
| 1289 | } else { |
| 1290 | tl_assert2(0, "bad --time-unit value"); |
| 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | // Take a snapshot, and only that -- decisions on whether to take a |
| 1295 | // snapshot, or what kind of snapshot, are made elsewhere. |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1296 | // Nb: we call the arg "my_time" because "time" shadows a global declaration |
| 1297 | // in /usr/include/time.h on Darwin. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1298 | static void |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1299 | take_snapshot(Snapshot* snapshot, SnapshotKind kind, Time my_time, |
njn | efc13c2 | 2009-02-23 06:44:51 +0000 | [diff] [blame] | 1300 | Bool is_detailed) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1301 | { |
| 1302 | tl_assert(!is_snapshot_in_use(snapshot)); |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1303 | if (!clo_pages_as_heap) { |
| 1304 | tl_assert(have_started_executing_code); |
| 1305 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1306 | |
| 1307 | // Heap and heap admin. |
| 1308 | if (clo_heap) { |
| 1309 | snapshot->heap_szB = heap_szB; |
| 1310 | if (is_detailed) { |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1311 | SizeT total_szB = heap_szB + heap_extra_szB + stacks_szB; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1312 | snapshot->alloc_sxpt = dup_XTree(alloc_xpt, total_szB); |
| 1313 | tl_assert( alloc_xpt->szB == heap_szB); |
| 1314 | tl_assert(snapshot->alloc_sxpt->szB == heap_szB); |
| 1315 | } |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1316 | snapshot->heap_extra_szB = heap_extra_szB; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | // Stack(s). |
| 1320 | if (clo_stacks) { |
| 1321 | snapshot->stacks_szB = stacks_szB; |
| 1322 | } |
| 1323 | |
| 1324 | // Rest of snapshot. |
| 1325 | snapshot->kind = kind; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1326 | snapshot->time = my_time; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1327 | sanity_check_snapshot(snapshot); |
| 1328 | |
| 1329 | // Update stats. |
| 1330 | if (Peak == kind) n_peak_snapshots++; |
| 1331 | if (is_detailed) n_detailed_snapshots++; |
| 1332 | n_real_snapshots++; |
| 1333 | } |
| 1334 | |
| 1335 | |
| 1336 | // Take a snapshot, if it's time, or if we've hit a peak. |
| 1337 | static void |
florian | e58e8a7 | 2012-10-20 19:57:16 +0000 | [diff] [blame] | 1338 | maybe_take_snapshot(SnapshotKind kind, const HChar* what) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1339 | { |
| 1340 | // 'min_time_interval' is the minimum time interval between snapshots. |
| 1341 | // If we try to take a snapshot and less than this much time has passed, |
| 1342 | // we don't take it. It gets larger as the program runs longer. It's |
| 1343 | // initialised to zero so that we begin by taking snapshots as quickly as |
| 1344 | // possible. |
| 1345 | static Time min_time_interval = 0; |
| 1346 | // Zero allows startup snapshot. |
| 1347 | static Time earliest_possible_time_of_next_snapshot = 0; |
| 1348 | static Int n_snapshots_since_last_detailed = 0; |
| 1349 | static Int n_skipped_snapshots_since_last_snapshot = 0; |
| 1350 | |
| 1351 | Snapshot* snapshot; |
| 1352 | Bool is_detailed; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1353 | // Nb: we call this variable "my_time" because "time" shadows a global |
| 1354 | // declaration in /usr/include/time.h on Darwin. |
| 1355 | Time my_time = get_time(); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1356 | |
| 1357 | switch (kind) { |
| 1358 | case Normal: |
| 1359 | // Only do a snapshot if it's time. |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1360 | if (my_time < earliest_possible_time_of_next_snapshot) { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1361 | n_skipped_snapshots++; |
| 1362 | n_skipped_snapshots_since_last_snapshot++; |
| 1363 | return; |
| 1364 | } |
| 1365 | is_detailed = (clo_detailed_freq-1 == n_snapshots_since_last_detailed); |
| 1366 | break; |
| 1367 | |
| 1368 | case Peak: { |
| 1369 | // Because we're about to do a deallocation, we're coming down from a |
| 1370 | // local peak. If it is (a) actually a global peak, and (b) a certain |
| 1371 | // amount bigger than the previous peak, then we take a peak snapshot. |
| 1372 | // By not taking a snapshot for every peak, we save a lot of effort -- |
| 1373 | // because many peaks remain peak only for a short time. |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1374 | SizeT total_szB = heap_szB + heap_extra_szB + stacks_szB; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1375 | SizeT excess_szB_for_new_peak = |
njn | 62721e9 | 2007-11-11 22:15:58 +0000 | [diff] [blame] | 1376 | (SizeT)((peak_snapshot_total_szB * clo_peak_inaccuracy) / 100); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1377 | if (total_szB <= peak_snapshot_total_szB + excess_szB_for_new_peak) { |
| 1378 | return; |
| 1379 | } |
| 1380 | is_detailed = True; |
| 1381 | break; |
| 1382 | } |
| 1383 | |
| 1384 | default: |
| 1385 | tl_assert2(0, "maybe_take_snapshot: unrecognised snapshot kind"); |
| 1386 | } |
| 1387 | |
| 1388 | // Take the snapshot. |
| 1389 | snapshot = & snapshots[next_snapshot_i]; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1390 | take_snapshot(snapshot, kind, my_time, is_detailed); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1391 | |
| 1392 | // Record if it was detailed. |
| 1393 | if (is_detailed) { |
| 1394 | n_snapshots_since_last_detailed = 0; |
| 1395 | } else { |
| 1396 | n_snapshots_since_last_detailed++; |
| 1397 | } |
| 1398 | |
| 1399 | // Update peak data, if it's a Peak snapshot. |
| 1400 | if (Peak == kind) { |
| 1401 | Int i, number_of_peaks_snapshots_found = 0; |
| 1402 | |
| 1403 | // Sanity check the size, then update our recorded peak. |
| 1404 | SizeT snapshot_total_szB = |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1405 | snapshot->heap_szB + snapshot->heap_extra_szB + snapshot->stacks_szB; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1406 | tl_assert2(snapshot_total_szB > peak_snapshot_total_szB, |
| 1407 | "%ld, %ld\n", snapshot_total_szB, peak_snapshot_total_szB); |
| 1408 | peak_snapshot_total_szB = snapshot_total_szB; |
| 1409 | |
| 1410 | // Find the old peak snapshot, if it exists, and mark it as normal. |
| 1411 | for (i = 0; i < next_snapshot_i; i++) { |
| 1412 | if (Peak == snapshots[i].kind) { |
| 1413 | snapshots[i].kind = Normal; |
| 1414 | number_of_peaks_snapshots_found++; |
| 1415 | } |
| 1416 | } |
| 1417 | tl_assert(number_of_peaks_snapshots_found <= 1); |
| 1418 | } |
| 1419 | |
| 1420 | // Finish up verbosity and stats stuff. |
| 1421 | if (n_skipped_snapshots_since_last_snapshot > 0) { |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1422 | VERB(2, " (skipped %d snapshot%s)\n", |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1423 | n_skipped_snapshots_since_last_snapshot, |
njn | 84f32b2 | 2009-02-10 07:14:37 +0000 | [diff] [blame] | 1424 | ( 1 == n_skipped_snapshots_since_last_snapshot ? "" : "s") ); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1425 | } |
| 1426 | VERB_snapshot(2, what, next_snapshot_i); |
| 1427 | n_skipped_snapshots_since_last_snapshot = 0; |
| 1428 | |
| 1429 | // Cull the entries, if our snapshot table is full. |
| 1430 | next_snapshot_i++; |
| 1431 | if (clo_max_snapshots == next_snapshot_i) { |
| 1432 | min_time_interval = cull_snapshots(); |
| 1433 | } |
| 1434 | |
| 1435 | // Work out the earliest time when the next snapshot can happen. |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1436 | earliest_possible_time_of_next_snapshot = my_time + min_time_interval; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | |
| 1440 | //------------------------------------------------------------// |
| 1441 | //--- Sanity checking ---// |
| 1442 | //------------------------------------------------------------// |
| 1443 | |
| 1444 | static Bool ms_cheap_sanity_check ( void ) |
| 1445 | { |
| 1446 | return True; // Nothing useful we can cheaply check. |
| 1447 | } |
| 1448 | |
| 1449 | static Bool ms_expensive_sanity_check ( void ) |
| 1450 | { |
| 1451 | sanity_check_XTree(alloc_xpt, /*parent*/NULL); |
| 1452 | sanity_check_snapshots_array(); |
| 1453 | return True; |
| 1454 | } |
| 1455 | |
| 1456 | |
| 1457 | //------------------------------------------------------------// |
| 1458 | //--- Heap management ---// |
| 1459 | //------------------------------------------------------------// |
| 1460 | |
| 1461 | // Metadata for heap blocks. Each one contains a pointer to a bottom-XPt, |
| 1462 | // which is a foothold into the XCon at which it was allocated. From |
| 1463 | // HP_Chunks, XPt 'space' fields are incremented (at allocation) and |
| 1464 | // decremented (at deallocation). |
| 1465 | // |
| 1466 | // Nb: first two fields must match core's VgHashNode. |
| 1467 | typedef |
| 1468 | struct _HP_Chunk { |
| 1469 | struct _HP_Chunk* next; |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1470 | Addr data; // Ptr to actual block |
| 1471 | SizeT req_szB; // Size requested |
| 1472 | SizeT slop_szB; // Extra bytes given above those requested |
| 1473 | XPt* where; // Where allocated; bottom-XPt |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1474 | } |
| 1475 | HP_Chunk; |
| 1476 | |
florian | 09a4c79 | 2014-10-18 10:58:05 +0000 | [diff] [blame] | 1477 | static VgHashTable *malloc_list = NULL; // HP_Chunks |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1478 | |
| 1479 | static void update_alloc_stats(SSizeT szB_delta) |
| 1480 | { |
| 1481 | // Update total_allocs_deallocs_szB. |
| 1482 | if (szB_delta < 0) szB_delta = -szB_delta; |
| 1483 | total_allocs_deallocs_szB += szB_delta; |
| 1484 | } |
| 1485 | |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1486 | static void update_heap_stats(SSizeT heap_szB_delta, Int heap_extra_szB_delta) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1487 | { |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1488 | if (heap_szB_delta < 0) |
| 1489 | tl_assert(heap_szB >= -heap_szB_delta); |
| 1490 | if (heap_extra_szB_delta < 0) |
| 1491 | tl_assert(heap_extra_szB >= -heap_extra_szB_delta); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1492 | |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1493 | heap_extra_szB += heap_extra_szB_delta; |
| 1494 | heap_szB += heap_szB_delta; |
| 1495 | |
| 1496 | update_alloc_stats(heap_szB_delta + heap_extra_szB_delta); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1497 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1498 | |
nethercote | 159dfef | 2004-09-13 13:27:30 +0000 | [diff] [blame] | 1499 | static |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1500 | void* record_block( ThreadId tid, void* p, SizeT req_szB, SizeT slop_szB, |
| 1501 | Bool exclude_first_entry, Bool maybe_snapshot ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1502 | { |
njn | f1c5def | 2005-08-11 02:17:07 +0000 | [diff] [blame] | 1503 | // Make new HP_Chunk node, add to malloc_list |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1504 | HP_Chunk* hc = VG_(malloc)("ms.main.rb.1", sizeof(HP_Chunk)); |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1505 | hc->req_szB = req_szB; |
| 1506 | hc->slop_szB = slop_szB; |
| 1507 | hc->data = (Addr)p; |
| 1508 | hc->where = NULL; |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 1509 | VG_(HT_add_node)(malloc_list, hc); |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 1510 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1511 | if (clo_heap) { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1512 | VERB(3, "<<< record_block (%lu, %lu)\n", req_szB, slop_szB); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1513 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1514 | hc->where = get_XCon( tid, exclude_first_entry ); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1515 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1516 | if (hc->where) { |
| 1517 | // Update statistics. |
| 1518 | n_heap_allocs++; |
| 1519 | |
| 1520 | // Update heap stats. |
| 1521 | update_heap_stats(req_szB, clo_heap_admin + slop_szB); |
| 1522 | |
| 1523 | // Update XTree. |
| 1524 | update_XCon(hc->where, req_szB); |
| 1525 | |
| 1526 | // Maybe take a snapshot. |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1527 | if (maybe_snapshot) { |
| 1528 | maybe_take_snapshot(Normal, " alloc"); |
| 1529 | } |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1530 | |
| 1531 | } else { |
| 1532 | // Ignored allocation. |
| 1533 | n_ignored_heap_allocs++; |
| 1534 | |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1535 | VERB(3, "(ignored)\n"); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1536 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1537 | |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1538 | VERB(3, ">>>\n"); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1539 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1540 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1541 | return p; |
| 1542 | } |
| 1543 | |
| 1544 | static __inline__ |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1545 | void* alloc_and_record_block ( ThreadId tid, SizeT req_szB, SizeT req_alignB, |
| 1546 | Bool is_zeroed ) |
| 1547 | { |
| 1548 | SizeT actual_szB, slop_szB; |
| 1549 | void* p; |
| 1550 | |
| 1551 | if ((SSizeT)req_szB < 0) return NULL; |
| 1552 | |
| 1553 | // Allocate and zero if necessary. |
| 1554 | p = VG_(cli_malloc)( req_alignB, req_szB ); |
| 1555 | if (!p) { |
| 1556 | return NULL; |
| 1557 | } |
| 1558 | if (is_zeroed) VG_(memset)(p, 0, req_szB); |
florian | e464e80 | 2014-09-11 20:15:23 +0000 | [diff] [blame] | 1559 | actual_szB = VG_(cli_malloc_usable_size)(p); |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1560 | tl_assert(actual_szB >= req_szB); |
| 1561 | slop_szB = actual_szB - req_szB; |
| 1562 | |
| 1563 | // Record block. |
| 1564 | record_block(tid, p, req_szB, slop_szB, /*exclude_first_entry*/True, |
| 1565 | /*maybe_snapshot*/True); |
| 1566 | |
| 1567 | return p; |
| 1568 | } |
| 1569 | |
| 1570 | static __inline__ |
| 1571 | void unrecord_block ( void* p, Bool maybe_snapshot ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1572 | { |
njn | f1c5def | 2005-08-11 02:17:07 +0000 | [diff] [blame] | 1573 | // Remove HP_Chunk from malloc_list |
njn | 7081c1a | 2009-05-19 01:53:30 +0000 | [diff] [blame] | 1574 | HP_Chunk* hc = VG_(HT_remove)(malloc_list, (UWord)p); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1575 | if (NULL == hc) { |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 1576 | return; // must have been a bogus free() |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1577 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1578 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1579 | if (clo_heap) { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1580 | VERB(3, "<<< unrecord_block\n"); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1581 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1582 | if (hc->where) { |
| 1583 | // Update statistics. |
| 1584 | n_heap_frees++; |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 1585 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1586 | // Maybe take a peak snapshot, since it's a deallocation. |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1587 | if (maybe_snapshot) { |
| 1588 | maybe_take_snapshot(Peak, "de-PEAK"); |
| 1589 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1590 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1591 | // Update heap stats. |
| 1592 | update_heap_stats(-hc->req_szB, -clo_heap_admin - hc->slop_szB); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1593 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1594 | // Update XTree. |
| 1595 | update_XCon(hc->where, -hc->req_szB); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1596 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1597 | // Maybe take a snapshot. |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1598 | if (maybe_snapshot) { |
| 1599 | maybe_take_snapshot(Normal, "dealloc"); |
| 1600 | } |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1601 | |
| 1602 | } else { |
| 1603 | n_ignored_heap_frees++; |
| 1604 | |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1605 | VERB(3, "(ignored)\n"); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1606 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1607 | |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1608 | VERB(3, ">>> (-%lu, -%lu)\n", hc->req_szB, hc->slop_szB); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | // Actually free the chunk, and the heap block (if necessary) |
| 1612 | VG_(free)( hc ); hc = NULL; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1615 | // Nb: --ignore-fn is tricky for realloc. If the block's original alloc was |
| 1616 | // ignored, but the realloc is not requested to be ignored, and we are |
| 1617 | // shrinking the block, then we have to ignore the realloc -- otherwise we |
| 1618 | // could end up with negative heap sizes. This isn't a danger if we are |
| 1619 | // growing such a block, but for consistency (it also simplifies things) we |
| 1620 | // ignore such reallocs as well. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1621 | static __inline__ |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1622 | void* realloc_block ( ThreadId tid, void* p_old, SizeT new_req_szB ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1623 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1624 | HP_Chunk* hc; |
| 1625 | void* p_new; |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1626 | SizeT old_req_szB, old_slop_szB, new_slop_szB, new_actual_szB; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1627 | XPt *old_where, *new_where; |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1628 | Bool is_ignored = False; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1629 | |
| 1630 | // Remove the old block |
| 1631 | hc = VG_(HT_remove)(malloc_list, (UWord)p_old); |
| 1632 | if (hc == NULL) { |
| 1633 | return NULL; // must have been a bogus realloc() |
| 1634 | } |
| 1635 | |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1636 | old_req_szB = hc->req_szB; |
| 1637 | old_slop_szB = hc->slop_szB; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1638 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1639 | tl_assert(!clo_pages_as_heap); // Shouldn't be here if --pages-as-heap=yes. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1640 | if (clo_heap) { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1641 | VERB(3, "<<< realloc_block (%lu)\n", new_req_szB); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1642 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1643 | if (hc->where) { |
| 1644 | // Update statistics. |
| 1645 | n_heap_reallocs++; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1646 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1647 | // Maybe take a peak snapshot, if it's (effectively) a deallocation. |
| 1648 | if (new_req_szB < old_req_szB) { |
| 1649 | maybe_take_snapshot(Peak, "re-PEAK"); |
| 1650 | } |
| 1651 | } else { |
| 1652 | // The original malloc was ignored, so we have to ignore the |
| 1653 | // realloc as well. |
| 1654 | is_ignored = True; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1655 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | // Actually do the allocation, if necessary. |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1659 | if (new_req_szB <= old_req_szB + old_slop_szB) { |
| 1660 | // New size is smaller or same; block not moved. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1661 | p_new = p_old; |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1662 | new_slop_szB = old_slop_szB + (old_req_szB - new_req_szB); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1663 | |
| 1664 | } else { |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1665 | // New size is bigger; make new block, copy shared contents, free old. |
| 1666 | p_new = VG_(cli_malloc)(VG_(clo_alignment), new_req_szB); |
| 1667 | if (!p_new) { |
| 1668 | // Nb: if realloc fails, NULL is returned but the old block is not |
| 1669 | // touched. What an awful function. |
| 1670 | return NULL; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1671 | } |
njn | dfa408c | 2011-08-08 01:58:50 +0000 | [diff] [blame] | 1672 | VG_(memcpy)(p_new, p_old, old_req_szB + old_slop_szB); |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1673 | VG_(cli_free)(p_old); |
florian | e464e80 | 2014-09-11 20:15:23 +0000 | [diff] [blame] | 1674 | new_actual_szB = VG_(cli_malloc_usable_size)(p_new); |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1675 | tl_assert(new_actual_szB >= new_req_szB); |
| 1676 | new_slop_szB = new_actual_szB - new_req_szB; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | if (p_new) { |
| 1680 | // Update HP_Chunk. |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1681 | hc->data = (Addr)p_new; |
| 1682 | hc->req_szB = new_req_szB; |
| 1683 | hc->slop_szB = new_slop_szB; |
| 1684 | old_where = hc->where; |
| 1685 | hc->where = NULL; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1686 | |
| 1687 | // Update XTree. |
| 1688 | if (clo_heap) { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1689 | new_where = get_XCon( tid, /*exclude_first_entry*/True); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1690 | if (!is_ignored && new_where) { |
| 1691 | hc->where = new_where; |
| 1692 | update_XCon(old_where, -old_req_szB); |
| 1693 | update_XCon(new_where, new_req_szB); |
| 1694 | } else { |
| 1695 | // The realloc itself is ignored. |
| 1696 | is_ignored = True; |
| 1697 | |
| 1698 | // Update statistics. |
| 1699 | n_ignored_heap_reallocs++; |
| 1700 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | // Now insert the new hc (with a possibly new 'data' field) into |
| 1705 | // malloc_list. If this realloc() did not increase the memory size, we |
| 1706 | // will have removed and then re-added hc unnecessarily. But that's ok |
| 1707 | // because shrinking a block with realloc() is (presumably) much rarer |
| 1708 | // than growing it, and this way simplifies the growing case. |
| 1709 | VG_(HT_add_node)(malloc_list, hc); |
| 1710 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1711 | if (clo_heap) { |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1712 | if (!is_ignored) { |
| 1713 | // Update heap stats. |
| 1714 | update_heap_stats(new_req_szB - old_req_szB, |
| 1715 | new_slop_szB - old_slop_szB); |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1716 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1717 | // Maybe take a snapshot. |
| 1718 | maybe_take_snapshot(Normal, "realloc"); |
| 1719 | } else { |
| 1720 | |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1721 | VERB(3, "(ignored)\n"); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1722 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1723 | |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1724 | VERB(3, ">>> (%ld, %ld)\n", |
florian | 16eef85 | 2015-08-03 21:05:20 +0000 | [diff] [blame] | 1725 | (SSizeT)(new_req_szB - old_req_szB), |
| 1726 | (SSizeT)(new_slop_szB - old_slop_szB)); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | return p_new; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1732 | |
| 1733 | //------------------------------------------------------------// |
| 1734 | //--- malloc() et al replacement wrappers ---// |
| 1735 | //------------------------------------------------------------// |
| 1736 | |
| 1737 | static void* ms_malloc ( ThreadId tid, SizeT szB ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1738 | { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1739 | return alloc_and_record_block( tid, szB, VG_(clo_alignment), /*is_zeroed*/False ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1742 | static void* ms___builtin_new ( ThreadId tid, SizeT szB ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1743 | { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1744 | return alloc_and_record_block( tid, szB, VG_(clo_alignment), /*is_zeroed*/False ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1747 | static void* ms___builtin_vec_new ( ThreadId tid, SizeT szB ) |
fitzhardinge | 51f3ff1 | 2004-03-04 22:42:03 +0000 | [diff] [blame] | 1748 | { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1749 | return alloc_and_record_block( tid, szB, VG_(clo_alignment), /*is_zeroed*/False ); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | static void* ms_calloc ( ThreadId tid, SizeT m, SizeT szB ) |
| 1753 | { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1754 | return alloc_and_record_block( tid, m*szB, VG_(clo_alignment), /*is_zeroed*/True ); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | static void *ms_memalign ( ThreadId tid, SizeT alignB, SizeT szB ) |
| 1758 | { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1759 | return alloc_and_record_block( tid, szB, alignB, False ); |
fitzhardinge | 51f3ff1 | 2004-03-04 22:42:03 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
njn | efc13c2 | 2009-02-23 06:44:51 +0000 | [diff] [blame] | 1762 | static void ms_free ( ThreadId tid __attribute__((unused)), void* p ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1763 | { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1764 | unrecord_block(p, /*maybe_snapshot*/True); |
| 1765 | VG_(cli_free)(p); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1768 | static void ms___builtin_delete ( ThreadId tid, void* p ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1769 | { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1770 | unrecord_block(p, /*maybe_snapshot*/True); |
| 1771 | VG_(cli_free)(p); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1774 | static void ms___builtin_vec_delete ( ThreadId tid, void* p ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1775 | { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1776 | unrecord_block(p, /*maybe_snapshot*/True); |
| 1777 | VG_(cli_free)(p); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1780 | static void* ms_realloc ( ThreadId tid, void* p_old, SizeT new_szB ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1781 | { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1782 | return realloc_block(tid, p_old, new_szB); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1783 | } |
| 1784 | |
njn | efc13c2 | 2009-02-23 06:44:51 +0000 | [diff] [blame] | 1785 | static SizeT ms_malloc_usable_size ( ThreadId tid, void* p ) |
njn | 8b140de | 2009-02-17 04:31:18 +0000 | [diff] [blame] | 1786 | { |
| 1787 | HP_Chunk* hc = VG_(HT_lookup)( malloc_list, (UWord)p ); |
| 1788 | |
| 1789 | return ( hc ? hc->req_szB + hc->slop_szB : 0 ); |
| 1790 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1791 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1792 | //------------------------------------------------------------// |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1793 | //--- Page handling ---// |
| 1794 | //------------------------------------------------------------// |
| 1795 | |
| 1796 | static |
| 1797 | void ms_record_page_mem ( Addr a, SizeT len ) |
| 1798 | { |
| 1799 | ThreadId tid = VG_(get_running_tid)(); |
| 1800 | Addr end; |
| 1801 | tl_assert(VG_IS_PAGE_ALIGNED(len)); |
| 1802 | tl_assert(len >= VKI_PAGE_SIZE); |
| 1803 | // Record the first N-1 pages as blocks, but don't do any snapshots. |
| 1804 | for (end = a + len - VKI_PAGE_SIZE; a < end; a += VKI_PAGE_SIZE) { |
| 1805 | record_block( tid, (void*)a, VKI_PAGE_SIZE, /*slop_szB*/0, |
| 1806 | /*exclude_first_entry*/False, /*maybe_snapshot*/False ); |
| 1807 | } |
| 1808 | // Record the last page as a block, and maybe do a snapshot afterwards. |
| 1809 | record_block( tid, (void*)a, VKI_PAGE_SIZE, /*slop_szB*/0, |
| 1810 | /*exclude_first_entry*/False, /*maybe_snapshot*/True ); |
| 1811 | } |
| 1812 | |
| 1813 | static |
| 1814 | void ms_unrecord_page_mem( Addr a, SizeT len ) |
| 1815 | { |
| 1816 | Addr end; |
| 1817 | tl_assert(VG_IS_PAGE_ALIGNED(len)); |
| 1818 | tl_assert(len >= VKI_PAGE_SIZE); |
| 1819 | for (end = a + len - VKI_PAGE_SIZE; a < end; a += VKI_PAGE_SIZE) { |
| 1820 | unrecord_block((void*)a, /*maybe_snapshot*/False); |
| 1821 | } |
| 1822 | unrecord_block((void*)a, /*maybe_snapshot*/True); |
| 1823 | } |
| 1824 | |
| 1825 | //------------------------------------------------------------// |
| 1826 | |
| 1827 | static |
| 1828 | void ms_new_mem_mmap ( Addr a, SizeT len, |
| 1829 | Bool rr, Bool ww, Bool xx, ULong di_handle ) |
| 1830 | { |
| 1831 | tl_assert(VG_IS_PAGE_ALIGNED(len)); |
| 1832 | ms_record_page_mem(a, len); |
| 1833 | } |
| 1834 | |
| 1835 | static |
| 1836 | void ms_new_mem_startup( Addr a, SizeT len, |
| 1837 | Bool rr, Bool ww, Bool xx, ULong di_handle ) |
| 1838 | { |
| 1839 | // startup maps are always be page-sized, except the trampoline page is |
| 1840 | // marked by the core as only being the size of the trampoline itself, |
| 1841 | // which is something like 57 bytes. Round it up to page size. |
| 1842 | len = VG_PGROUNDUP(len); |
| 1843 | ms_record_page_mem(a, len); |
| 1844 | } |
| 1845 | |
| 1846 | static |
| 1847 | void ms_new_mem_brk ( Addr a, SizeT len, ThreadId tid ) |
| 1848 | { |
philippe | e6a26cc | 2012-05-01 20:02:30 +0000 | [diff] [blame] | 1849 | // brk limit is not necessarily aligned on a page boundary. |
| 1850 | // If new memory being brk-ed implies to allocate a new page, |
| 1851 | // then call ms_record_page_mem with page aligned parameters |
| 1852 | // otherwise just ignore. |
| 1853 | Addr old_bottom_page = VG_PGROUNDDN(a - 1); |
| 1854 | Addr new_top_page = VG_PGROUNDDN(a + len - 1); |
| 1855 | if (old_bottom_page != new_top_page) |
| 1856 | ms_record_page_mem(VG_PGROUNDDN(a), |
| 1857 | (new_top_page - old_bottom_page)); |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | static |
| 1861 | void ms_copy_mem_remap( Addr from, Addr to, SizeT len) |
| 1862 | { |
| 1863 | tl_assert(VG_IS_PAGE_ALIGNED(len)); |
| 1864 | ms_unrecord_page_mem(from, len); |
| 1865 | ms_record_page_mem(to, len); |
| 1866 | } |
| 1867 | |
| 1868 | static |
| 1869 | void ms_die_mem_munmap( Addr a, SizeT len ) |
| 1870 | { |
| 1871 | tl_assert(VG_IS_PAGE_ALIGNED(len)); |
| 1872 | ms_unrecord_page_mem(a, len); |
| 1873 | } |
| 1874 | |
| 1875 | static |
| 1876 | void ms_die_mem_brk( Addr a, SizeT len ) |
| 1877 | { |
philippe | e6a26cc | 2012-05-01 20:02:30 +0000 | [diff] [blame] | 1878 | // Call ms_unrecord_page_mem only if one or more pages are de-allocated. |
| 1879 | // See ms_new_mem_brk for more details. |
| 1880 | Addr new_bottom_page = VG_PGROUNDDN(a - 1); |
| 1881 | Addr old_top_page = VG_PGROUNDDN(a + len - 1); |
| 1882 | if (old_top_page != new_bottom_page) |
| 1883 | ms_unrecord_page_mem(VG_PGROUNDDN(a), |
| 1884 | (old_top_page - new_bottom_page)); |
| 1885 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | //------------------------------------------------------------// |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1889 | //--- Stacks ---// |
| 1890 | //------------------------------------------------------------// |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1891 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1892 | // We really want the inlining to occur... |
| 1893 | #define INLINE inline __attribute__((always_inline)) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1894 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1895 | static void update_stack_stats(SSizeT stack_szB_delta) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1896 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1897 | if (stack_szB_delta < 0) tl_assert(stacks_szB >= -stack_szB_delta); |
| 1898 | stacks_szB += stack_szB_delta; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1899 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1900 | update_alloc_stats(stack_szB_delta); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
florian | e58e8a7 | 2012-10-20 19:57:16 +0000 | [diff] [blame] | 1903 | static INLINE void new_mem_stack_2(SizeT len, const HChar* what) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1904 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1905 | if (have_started_executing_code) { |
florian | 16eef85 | 2015-08-03 21:05:20 +0000 | [diff] [blame] | 1906 | VERB(3, "<<< new_mem_stack (%lu)\n", len); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1907 | n_stack_allocs++; |
| 1908 | update_stack_stats(len); |
| 1909 | maybe_take_snapshot(Normal, what); |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1910 | VERB(3, ">>>\n"); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1911 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
florian | e58e8a7 | 2012-10-20 19:57:16 +0000 | [diff] [blame] | 1914 | static INLINE void die_mem_stack_2(SizeT len, const HChar* what) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1915 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1916 | if (have_started_executing_code) { |
florian | 16eef85 | 2015-08-03 21:05:20 +0000 | [diff] [blame] | 1917 | VERB(3, "<<< die_mem_stack (-%lu)\n", len); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1918 | n_stack_frees++; |
| 1919 | maybe_take_snapshot(Peak, "stkPEAK"); |
| 1920 | update_stack_stats(-len); |
| 1921 | maybe_take_snapshot(Normal, what); |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1922 | VERB(3, ">>>\n"); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1923 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1926 | static void new_mem_stack(Addr a, SizeT len) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1927 | { |
njn | efc13c2 | 2009-02-23 06:44:51 +0000 | [diff] [blame] | 1928 | new_mem_stack_2(len, "stk-new"); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1929 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1930 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1931 | static void die_mem_stack(Addr a, SizeT len) |
| 1932 | { |
njn | efc13c2 | 2009-02-23 06:44:51 +0000 | [diff] [blame] | 1933 | die_mem_stack_2(len, "stk-die"); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1934 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1935 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1936 | static void new_mem_stack_signal(Addr a, SizeT len, ThreadId tid) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1937 | { |
njn | efc13c2 | 2009-02-23 06:44:51 +0000 | [diff] [blame] | 1938 | new_mem_stack_2(len, "sig-new"); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 1941 | static void die_mem_stack_signal(Addr a, SizeT len) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1942 | { |
njn | efc13c2 | 2009-02-23 06:44:51 +0000 | [diff] [blame] | 1943 | die_mem_stack_2(len, "sig-die"); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1946 | |
| 1947 | //------------------------------------------------------------// |
| 1948 | //--- Client Requests ---// |
| 1949 | //------------------------------------------------------------// |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1950 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1951 | static void print_monitor_help ( void ) |
| 1952 | { |
| 1953 | VG_(gdb_printf) ("\n"); |
| 1954 | VG_(gdb_printf) ("massif monitor commands:\n"); |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1955 | VG_(gdb_printf) (" snapshot [<filename>]\n"); |
| 1956 | VG_(gdb_printf) (" detailed_snapshot [<filename>]\n"); |
philippe | 868bfa2 | 2015-03-07 19:20:12 +0000 | [diff] [blame] | 1957 | VG_(gdb_printf) (" takes a snapshot (or a detailed snapshot)\n"); |
| 1958 | VG_(gdb_printf) (" and saves it in <filename>\n"); |
| 1959 | VG_(gdb_printf) (" default <filename> is massif.vgdb.out\n"); |
| 1960 | VG_(gdb_printf) (" all_snapshots [<filename>]\n"); |
| 1961 | VG_(gdb_printf) (" saves all snapshot(s) taken so far in <filename>\n"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1962 | VG_(gdb_printf) (" default <filename> is massif.vgdb.out\n"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1963 | VG_(gdb_printf) ("\n"); |
| 1964 | } |
| 1965 | |
| 1966 | |
| 1967 | /* Forward declaration. |
| 1968 | return True if request recognised, False otherwise */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1969 | static Bool handle_gdb_monitor_command (ThreadId tid, HChar *req); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1970 | static Bool ms_handle_client_request ( ThreadId tid, UWord* argv, UWord* ret ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1971 | { |
| 1972 | switch (argv[0]) { |
| 1973 | case VG_USERREQ__MALLOCLIKE_BLOCK: { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1974 | void* p = (void*)argv[1]; |
| 1975 | SizeT szB = argv[2]; |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1976 | record_block( tid, p, szB, /*slop_szB*/0, /*exclude_first_entry*/False, |
| 1977 | /*maybe_snapshot*/True ); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1978 | *ret = 0; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1979 | return True; |
| 1980 | } |
bart | 9134738 | 2011-03-25 20:07:25 +0000 | [diff] [blame] | 1981 | case VG_USERREQ__RESIZEINPLACE_BLOCK: { |
| 1982 | void* p = (void*)argv[1]; |
| 1983 | SizeT newSizeB = argv[3]; |
| 1984 | |
| 1985 | unrecord_block(p, /*maybe_snapshot*/True); |
| 1986 | record_block(tid, p, newSizeB, /*slop_szB*/0, |
| 1987 | /*exclude_first_entry*/False, /*maybe_snapshot*/True); |
| 1988 | return True; |
| 1989 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1990 | case VG_USERREQ__FREELIKE_BLOCK: { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1991 | void* p = (void*)argv[1]; |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1992 | unrecord_block(p, /*maybe_snapshot*/True); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1993 | *ret = 0; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1994 | return True; |
| 1995 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1996 | case VG_USERREQ__GDB_MONITOR_COMMAND: { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1997 | Bool handled = handle_gdb_monitor_command (tid, (HChar*)argv[1]); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1998 | if (handled) |
| 1999 | *ret = 1; |
| 2000 | else |
| 2001 | *ret = 0; |
| 2002 | return handled; |
| 2003 | } |
| 2004 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2005 | default: |
| 2006 | *ret = 0; |
| 2007 | return False; |
| 2008 | } |
| 2009 | } |
| 2010 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2011 | //------------------------------------------------------------// |
| 2012 | //--- Instrumentation ---// |
| 2013 | //------------------------------------------------------------// |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2014 | |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 2015 | static void add_counter_update(IRSB* sbOut, Int n) |
| 2016 | { |
| 2017 | #if defined(VG_BIGENDIAN) |
| 2018 | # define END Iend_BE |
| 2019 | #elif defined(VG_LITTLEENDIAN) |
| 2020 | # define END Iend_LE |
| 2021 | #else |
| 2022 | # error "Unknown endianness" |
| 2023 | #endif |
| 2024 | // Add code to increment 'guest_instrs_executed' by 'n', like this: |
| 2025 | // WrTmp(t1, Load64(&guest_instrs_executed)) |
| 2026 | // WrTmp(t2, Add64(RdTmp(t1), Const(n))) |
| 2027 | // Store(&guest_instrs_executed, t2) |
| 2028 | IRTemp t1 = newIRTemp(sbOut->tyenv, Ity_I64); |
| 2029 | IRTemp t2 = newIRTemp(sbOut->tyenv, Ity_I64); |
| 2030 | IRExpr* counter_addr = mkIRExpr_HWord( (HWord)&guest_instrs_executed ); |
| 2031 | |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 2032 | IRStmt* st1 = IRStmt_WrTmp(t1, IRExpr_Load(END, Ity_I64, counter_addr)); |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 2033 | IRStmt* st2 = |
| 2034 | IRStmt_WrTmp(t2, |
| 2035 | IRExpr_Binop(Iop_Add64, IRExpr_RdTmp(t1), |
| 2036 | IRExpr_Const(IRConst_U64(n)))); |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 2037 | IRStmt* st3 = IRStmt_Store(END, counter_addr, IRExpr_RdTmp(t2)); |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 2038 | |
| 2039 | addStmtToIRSB( sbOut, st1 ); |
| 2040 | addStmtToIRSB( sbOut, st2 ); |
| 2041 | addStmtToIRSB( sbOut, st3 ); |
| 2042 | } |
| 2043 | |
| 2044 | static IRSB* ms_instrument2( IRSB* sbIn ) |
| 2045 | { |
| 2046 | Int i, n = 0; |
| 2047 | IRSB* sbOut; |
| 2048 | |
| 2049 | // We increment the instruction count in two places: |
| 2050 | // - just before any Ist_Exit statements; |
| 2051 | // - just before the IRSB's end. |
| 2052 | // In the former case, we zero 'n' and then continue instrumenting. |
| 2053 | |
| 2054 | sbOut = deepCopyIRSBExceptStmts(sbIn); |
| 2055 | |
| 2056 | for (i = 0; i < sbIn->stmts_used; i++) { |
| 2057 | IRStmt* st = sbIn->stmts[i]; |
| 2058 | |
| 2059 | if (!st || st->tag == Ist_NoOp) continue; |
| 2060 | |
| 2061 | if (st->tag == Ist_IMark) { |
| 2062 | n++; |
| 2063 | } else if (st->tag == Ist_Exit) { |
| 2064 | if (n > 0) { |
| 2065 | // Add an increment before the Exit statement, then reset 'n'. |
| 2066 | add_counter_update(sbOut, n); |
| 2067 | n = 0; |
| 2068 | } |
| 2069 | } |
| 2070 | addStmtToIRSB( sbOut, st ); |
| 2071 | } |
| 2072 | |
| 2073 | if (n > 0) { |
| 2074 | // Add an increment before the SB end. |
| 2075 | add_counter_update(sbOut, n); |
| 2076 | } |
| 2077 | return sbOut; |
| 2078 | } |
| 2079 | |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 2080 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 2081 | IRSB* ms_instrument ( VgCallbackClosure* closure, |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 2082 | IRSB* sbIn, |
florian | 3c0c947 | 2014-09-24 12:06:55 +0000 | [diff] [blame] | 2083 | const VexGuestLayout* layout, |
| 2084 | const VexGuestExtents* vge, |
| 2085 | const VexArchInfo* archinfo_host, |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 2086 | IRType gWordTy, IRType hWordTy ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2087 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2088 | if (! have_started_executing_code) { |
| 2089 | // Do an initial sample to guarantee that we have at least one. |
| 2090 | // We use 'maybe_take_snapshot' instead of 'take_snapshot' to ensure |
| 2091 | // 'maybe_take_snapshot's internal static variables are initialised. |
| 2092 | have_started_executing_code = True; |
| 2093 | maybe_take_snapshot(Normal, "startup"); |
| 2094 | } |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 2095 | |
| 2096 | if (clo_time_unit == TimeI) { return ms_instrument2(sbIn); } |
| 2097 | else if (clo_time_unit == TimeMS) { return sbIn; } |
| 2098 | else if (clo_time_unit == TimeB) { return sbIn; } |
| 2099 | else { tl_assert2(0, "bad --time-unit value"); } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2100 | } |
| 2101 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2102 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2103 | //------------------------------------------------------------// |
| 2104 | //--- Writing snapshots ---// |
| 2105 | //------------------------------------------------------------// |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2106 | |
florian | 12d2eb5 | 2014-10-30 22:17:56 +0000 | [diff] [blame] | 2107 | #define FP(format, args...) ({ VG_(fprintf)(fp, format, ##args); }) |
njn | b11e5aa | 2008-01-20 22:30:52 +0000 | [diff] [blame] | 2108 | |
florian | 12d2eb5 | 2014-10-30 22:17:56 +0000 | [diff] [blame] | 2109 | static void pp_snapshot_SXPt(VgFile *fp, SXPt* sxpt, Int depth, |
| 2110 | HChar* depth_str, Int depth_str_len, |
| 2111 | SizeT snapshot_heap_szB, SizeT snapshot_total_szB) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2112 | { |
njn | 466ed6e | 2009-02-18 05:14:44 +0000 | [diff] [blame] | 2113 | Int i, j, n_insig_children_sxpts; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2114 | SXPt* child = NULL; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2115 | |
sewardj | f330ae8 | 2008-05-13 10:57:29 +0000 | [diff] [blame] | 2116 | // Used for printing function names. Is made static to keep it out |
| 2117 | // of the stack frame -- this function is recursive. Obviously this |
| 2118 | // now means its contents are trashed across the recursive call. |
florian | 770a8d2 | 2014-11-03 22:43:42 +0000 | [diff] [blame] | 2119 | const HChar* ip_desc; |
sewardj | f330ae8 | 2008-05-13 10:57:29 +0000 | [diff] [blame] | 2120 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2121 | switch (sxpt->tag) { |
| 2122 | case SigSXPt: |
| 2123 | // Print the SXPt itself. |
njn | 84f32b2 | 2009-02-10 07:14:37 +0000 | [diff] [blame] | 2124 | if (0 == depth) { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 2125 | if (clo_heap) { |
| 2126 | ip_desc = |
| 2127 | ( clo_pages_as_heap |
| 2128 | ? "(page allocation syscalls) mmap/mremap/brk, --alloc-fns, etc." |
| 2129 | : "(heap allocation functions) malloc/new/new[], --alloc-fns, etc." |
| 2130 | ); |
| 2131 | } else { |
| 2132 | // XXX: --alloc-fns? |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 2133 | |
florian | ef901ff | 2014-11-16 09:56:42 +0000 | [diff] [blame] | 2134 | // Nick thinks this case cannot happen. ip_desc would be |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 2135 | // conceptually uninitialised here. Therefore: |
| 2136 | tl_assert2(0, "pp_snapshot_SXPt: unexpected"); |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 2137 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2138 | } else { |
| 2139 | // If it's main-or-below-main, we (if appropriate) ignore everything |
| 2140 | // below it by pretending it has no children. |
njn | 6882443 | 2009-02-10 06:48:00 +0000 | [diff] [blame] | 2141 | if ( ! VG_(clo_show_below_main) ) { |
| 2142 | Vg_FnNameKind kind = VG_(get_fnname_kind_from_IP)(sxpt->Sig.ip); |
| 2143 | if (Vg_FnNameMain == kind || Vg_FnNameBelowMain == kind) { |
| 2144 | sxpt->Sig.n_children = 0; |
| 2145 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2146 | } |
njn | 6882443 | 2009-02-10 06:48:00 +0000 | [diff] [blame] | 2147 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2148 | // We need the -1 to get the line number right, But I'm not sure why. |
florian | 770a8d2 | 2014-11-03 22:43:42 +0000 | [diff] [blame] | 2149 | ip_desc = VG_(describe_IP)(sxpt->Sig.ip-1, NULL); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2150 | } |
njn | 466ed6e | 2009-02-18 05:14:44 +0000 | [diff] [blame] | 2151 | |
| 2152 | // Do the non-ip_desc part first... |
florian | 16eef85 | 2015-08-03 21:05:20 +0000 | [diff] [blame] | 2153 | FP("%sn%u: %lu ", depth_str, sxpt->Sig.n_children, sxpt->szB); |
njn | 466ed6e | 2009-02-18 05:14:44 +0000 | [diff] [blame] | 2154 | |
| 2155 | // For ip_descs beginning with "0xABCD...:" addresses, we first |
| 2156 | // measure the length of the "0xabcd: " address at the start of the |
| 2157 | // ip_desc. |
| 2158 | j = 0; |
| 2159 | if ('0' == ip_desc[0] && 'x' == ip_desc[1]) { |
| 2160 | j = 2; |
| 2161 | while (True) { |
| 2162 | if (ip_desc[j]) { |
| 2163 | if (':' == ip_desc[j]) break; |
| 2164 | j++; |
| 2165 | } else { |
| 2166 | tl_assert2(0, "ip_desc has unexpected form: %s\n", ip_desc); |
| 2167 | } |
| 2168 | } |
| 2169 | } |
florian | 770a8d2 | 2014-11-03 22:43:42 +0000 | [diff] [blame] | 2170 | // It used to be that ip_desc was truncated at the end. |
| 2171 | // But there does not seem to be a good reason for that. Besides, |
| 2172 | // the string was truncated at the right, which is less than ideal. |
| 2173 | // Truncation at the beginning of the string would have been preferable. |
| 2174 | // Think several nested namespaces in C++.... |
| 2175 | // Anyhow, we spit out the full-length string now. |
florian | ef901ff | 2014-11-16 09:56:42 +0000 | [diff] [blame] | 2176 | FP("%s\n", ip_desc); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2177 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2178 | // Indent. |
| 2179 | tl_assert(depth+1 < depth_str_len-1); // -1 for end NUL char |
| 2180 | depth_str[depth+0] = ' '; |
| 2181 | depth_str[depth+1] = '\0'; |
| 2182 | |
| 2183 | // Sort SXPt's children by szB (reverse order: biggest to smallest). |
| 2184 | // Nb: we sort them here, rather than earlier (eg. in dup_XTree), for |
| 2185 | // two reasons. First, if we do it during dup_XTree, it can get |
| 2186 | // expensive (eg. 15% of execution time for konqueror |
| 2187 | // startup/shutdown). Second, this way we get the Insig SXPt (if one |
| 2188 | // is present) in its sorted position, not at the end. |
| 2189 | VG_(ssort)(sxpt->Sig.children, sxpt->Sig.n_children, sizeof(SXPt*), |
| 2190 | SXPt_revcmp_szB); |
| 2191 | |
| 2192 | // Print the SXPt's children. They should already be in sorted order. |
| 2193 | n_insig_children_sxpts = 0; |
| 2194 | for (i = 0; i < sxpt->Sig.n_children; i++) { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2195 | child = sxpt->Sig.children[i]; |
| 2196 | |
| 2197 | if (InsigSXPt == child->tag) |
| 2198 | n_insig_children_sxpts++; |
| 2199 | |
florian | 770a8d2 | 2014-11-03 22:43:42 +0000 | [diff] [blame] | 2200 | // Ok, print the child. NB: contents of ip_desc will be |
sewardj | f330ae8 | 2008-05-13 10:57:29 +0000 | [diff] [blame] | 2201 | // trashed by this recursive call. Doesn't matter currently, |
| 2202 | // but worth noting. |
florian | 12d2eb5 | 2014-10-30 22:17:56 +0000 | [diff] [blame] | 2203 | pp_snapshot_SXPt(fp, child, depth+1, depth_str, depth_str_len, |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2204 | snapshot_heap_szB, snapshot_total_szB); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2205 | } |
njn | dbeb535 | 2007-12-04 03:15:23 +0000 | [diff] [blame] | 2206 | |
| 2207 | // Unindent. |
| 2208 | depth_str[depth+0] = '\0'; |
| 2209 | depth_str[depth+1] = '\0'; |
| 2210 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2211 | // There should be 0 or 1 Insig children SXPts. |
| 2212 | tl_assert(n_insig_children_sxpts <= 1); |
| 2213 | break; |
| 2214 | |
| 2215 | case InsigSXPt: { |
florian | e58e8a7 | 2012-10-20 19:57:16 +0000 | [diff] [blame] | 2216 | const HChar* s = ( 1 == sxpt->Insig.n_xpts ? "," : "s, all" ); |
florian | 227a1ec | 2014-12-12 19:32:10 +0000 | [diff] [blame] | 2217 | FP("%sn0: %lu in %d place%s below massif's threshold (%.2f%%)\n", |
| 2218 | depth_str, sxpt->szB, sxpt->Insig.n_xpts, s, clo_threshold); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2219 | break; |
| 2220 | } |
| 2221 | |
| 2222 | default: |
| 2223 | tl_assert2(0, "pp_snapshot_SXPt: unrecognised SXPt tag"); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2224 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2225 | } |
| 2226 | |
florian | 12d2eb5 | 2014-10-30 22:17:56 +0000 | [diff] [blame] | 2227 | static void pp_snapshot(VgFile *fp, Snapshot* snapshot, Int snapshot_n) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2228 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2229 | sanity_check_snapshot(snapshot); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2230 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2231 | FP("#-----------\n"); |
| 2232 | FP("snapshot=%d\n", snapshot_n); |
| 2233 | FP("#-----------\n"); |
| 2234 | FP("time=%lld\n", snapshot->time); |
| 2235 | FP("mem_heap_B=%lu\n", snapshot->heap_szB); |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 2236 | FP("mem_heap_extra_B=%lu\n", snapshot->heap_extra_szB); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2237 | FP("mem_stacks_B=%lu\n", snapshot->stacks_szB); |
| 2238 | |
| 2239 | if (is_detailed_snapshot(snapshot)) { |
| 2240 | // Detailed snapshot -- print heap tree. |
| 2241 | Int depth_str_len = clo_depth + 3; |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2242 | HChar* depth_str = VG_(malloc)("ms.main.pps.1", |
| 2243 | sizeof(HChar) * depth_str_len); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2244 | SizeT snapshot_total_szB = |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 2245 | snapshot->heap_szB + snapshot->heap_extra_szB + snapshot->stacks_szB; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2246 | depth_str[0] = '\0'; // Initialise depth_str to "". |
| 2247 | |
| 2248 | FP("heap_tree=%s\n", ( Peak == snapshot->kind ? "peak" : "detailed" )); |
florian | 12d2eb5 | 2014-10-30 22:17:56 +0000 | [diff] [blame] | 2249 | pp_snapshot_SXPt(fp, snapshot->alloc_sxpt, 0, depth_str, |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2250 | depth_str_len, snapshot->heap_szB, |
| 2251 | snapshot_total_szB); |
| 2252 | |
| 2253 | VG_(free)(depth_str); |
| 2254 | |
| 2255 | } else { |
| 2256 | FP("heap_tree=empty\n"); |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 2257 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2258 | } |
| 2259 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2260 | static void write_snapshots_to_file(const HChar* massif_out_file, |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2261 | Snapshot snapshots_array[], |
| 2262 | Int nr_elements) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2263 | { |
florian | 12d2eb5 | 2014-10-30 22:17:56 +0000 | [diff] [blame] | 2264 | Int i; |
| 2265 | VgFile *fp; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2266 | |
florian | 12d2eb5 | 2014-10-30 22:17:56 +0000 | [diff] [blame] | 2267 | fp = VG_(fopen)(massif_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY, |
| 2268 | VKI_S_IRUSR|VKI_S_IWUSR); |
| 2269 | if (fp == NULL) { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2270 | // If the file can't be opened for whatever reason (conflict |
| 2271 | // between multiple cachegrinded processes?), give up now. |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 2272 | VG_(umsg)("error: can't open output file '%s'\n", massif_out_file ); |
| 2273 | VG_(umsg)(" ... so profiling results will be missing.\n"); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2274 | return; |
| 2275 | } |
| 2276 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2277 | // Print massif-specific options that were used. |
| 2278 | // XXX: is it worth having a "desc:" line? Could just call it "options:" |
| 2279 | // -- this file format isn't as generic as Cachegrind's, so the |
| 2280 | // implied genericity of "desc:" is bogus. |
| 2281 | FP("desc:"); |
| 2282 | for (i = 0; i < VG_(sizeXA)(args_for_massif); i++) { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2283 | HChar* arg = *(HChar**)VG_(indexXA)(args_for_massif, i); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2284 | FP(" %s", arg); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2285 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2286 | if (0 == i) FP(" (none)"); |
| 2287 | FP("\n"); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2288 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2289 | // Print "cmd:" line. |
| 2290 | FP("cmd: "); |
florian | b16609b | 2014-08-20 21:04:14 +0000 | [diff] [blame] | 2291 | FP("%s", VG_(args_the_exename)); |
| 2292 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) { |
| 2293 | HChar* arg = * (HChar**) VG_(indexXA)( VG_(args_for_client), i ); |
florian | 5980e9d | 2014-10-28 20:28:32 +0000 | [diff] [blame] | 2294 | FP(" %s", arg); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2295 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2296 | FP("\n"); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2297 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2298 | FP("time_unit: %s\n", TimeUnit_to_string(clo_time_unit)); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2299 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2300 | for (i = 0; i < nr_elements; i++) { |
| 2301 | Snapshot* snapshot = & snapshots_array[i]; |
florian | 12d2eb5 | 2014-10-30 22:17:56 +0000 | [diff] [blame] | 2302 | pp_snapshot(fp, snapshot, i); // Detailed snapshot! |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2303 | } |
florian | 12d2eb5 | 2014-10-30 22:17:56 +0000 | [diff] [blame] | 2304 | VG_(fclose) (fp); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2305 | } |
| 2306 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2307 | static void write_snapshots_array_to_file(void) |
| 2308 | { |
| 2309 | // Setup output filename. Nb: it's important to do this now, ie. as late |
| 2310 | // as possible. If we do it at start-up and the program forks and the |
| 2311 | // output file format string contains a %p (pid) specifier, both the |
| 2312 | // parent and child will incorrectly write to the same file; this |
| 2313 | // happened in 3.3.0. |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2314 | HChar* massif_out_file = |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2315 | VG_(expand_file_name)("--massif-out-file", clo_massif_out_file); |
| 2316 | write_snapshots_to_file (massif_out_file, snapshots, next_snapshot_i); |
| 2317 | VG_(free)(massif_out_file); |
| 2318 | } |
| 2319 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2320 | static void handle_snapshot_monitor_command (const HChar *filename, |
| 2321 | Bool detailed) |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2322 | { |
| 2323 | Snapshot snapshot; |
| 2324 | |
philippe | 27c9f0d | 2012-09-24 21:50:16 +0000 | [diff] [blame] | 2325 | if (!clo_pages_as_heap && !have_started_executing_code) { |
| 2326 | // See comments of variable have_started_executing_code. |
| 2327 | VG_(gdb_printf) |
| 2328 | ("error: cannot take snapshot before execution has started\n"); |
| 2329 | return; |
| 2330 | } |
| 2331 | |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2332 | clear_snapshot(&snapshot, /* do_sanity_check */ False); |
| 2333 | take_snapshot(&snapshot, Normal, get_time(), detailed); |
philippe | 27c9f0d | 2012-09-24 21:50:16 +0000 | [diff] [blame] | 2334 | write_snapshots_to_file ((filename == NULL) ? |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2335 | "massif.vgdb.out" : filename, |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2336 | &snapshot, |
| 2337 | 1); |
| 2338 | delete_snapshot(&snapshot); |
| 2339 | } |
| 2340 | |
philippe | 868bfa2 | 2015-03-07 19:20:12 +0000 | [diff] [blame] | 2341 | static void handle_all_snapshots_monitor_command (const HChar *filename) |
| 2342 | { |
| 2343 | if (!clo_pages_as_heap && !have_started_executing_code) { |
| 2344 | // See comments of variable have_started_executing_code. |
| 2345 | VG_(gdb_printf) |
| 2346 | ("error: cannot take snapshot before execution has started\n"); |
| 2347 | return; |
| 2348 | } |
| 2349 | |
| 2350 | write_snapshots_to_file ((filename == NULL) ? |
| 2351 | "massif.vgdb.out" : filename, |
| 2352 | snapshots, next_snapshot_i); |
| 2353 | } |
| 2354 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2355 | static Bool handle_gdb_monitor_command (ThreadId tid, HChar *req) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2356 | { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2357 | HChar* wcmd; |
dejanj | c29761f | 2014-03-19 11:58:35 +0000 | [diff] [blame] | 2358 | HChar s[VG_(strlen(req)) + 1]; /* copy for strtok_r */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2359 | HChar *ssaveptr; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2360 | |
| 2361 | VG_(strcpy) (s, req); |
| 2362 | |
| 2363 | wcmd = VG_(strtok_r) (s, " ", &ssaveptr); |
philippe | 868bfa2 | 2015-03-07 19:20:12 +0000 | [diff] [blame] | 2364 | switch (VG_(keyword_id) ("help snapshot detailed_snapshot all_snapshots", |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2365 | wcmd, kwd_report_duplicated_matches)) { |
| 2366 | case -2: /* multiple matches */ |
| 2367 | return True; |
| 2368 | case -1: /* not found */ |
| 2369 | return False; |
| 2370 | case 0: /* help */ |
| 2371 | print_monitor_help(); |
| 2372 | return True; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2373 | case 1: { /* snapshot */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2374 | HChar* filename; |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2375 | filename = VG_(strtok_r) (NULL, " ", &ssaveptr); |
| 2376 | handle_snapshot_monitor_command (filename, False /* detailed */); |
| 2377 | return True; |
| 2378 | } |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 2379 | case 2: { /* detailed_snapshot */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2380 | HChar* filename; |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 2381 | filename = VG_(strtok_r) (NULL, " ", &ssaveptr); |
| 2382 | handle_snapshot_monitor_command (filename, True /* detailed */); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2383 | return True; |
| 2384 | } |
philippe | 868bfa2 | 2015-03-07 19:20:12 +0000 | [diff] [blame] | 2385 | case 3: { /* all_snapshots */ |
| 2386 | HChar* filename; |
| 2387 | filename = VG_(strtok_r) (NULL, " ", &ssaveptr); |
| 2388 | handle_all_snapshots_monitor_command (filename); |
| 2389 | return True; |
| 2390 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 2391 | default: |
| 2392 | tl_assert(0); |
| 2393 | return False; |
| 2394 | } |
| 2395 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2396 | |
philippe | f066315 | 2013-12-19 19:10:20 +0000 | [diff] [blame] | 2397 | static void ms_print_stats (void) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2398 | { |
philippe | f066315 | 2013-12-19 19:10:20 +0000 | [diff] [blame] | 2399 | #define STATS(format, args...) \ |
| 2400 | VG_(dmsg)("Massif: " format, ##args) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2401 | |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 2402 | STATS("heap allocs: %u\n", n_heap_allocs); |
| 2403 | STATS("heap reallocs: %u\n", n_heap_reallocs); |
| 2404 | STATS("heap frees: %u\n", n_heap_frees); |
| 2405 | STATS("ignored heap allocs: %u\n", n_ignored_heap_allocs); |
| 2406 | STATS("ignored heap frees: %u\n", n_ignored_heap_frees); |
| 2407 | STATS("ignored heap reallocs: %u\n", n_ignored_heap_reallocs); |
| 2408 | STATS("stack allocs: %u\n", n_stack_allocs); |
| 2409 | STATS("stack frees: %u\n", n_stack_frees); |
| 2410 | STATS("XPts: %u\n", n_xpts); |
florian | 16eef85 | 2015-08-03 21:05:20 +0000 | [diff] [blame] | 2411 | STATS("top-XPts: %u (%u%%)\n", |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2412 | alloc_xpt->n_children, |
| 2413 | ( n_xpts ? alloc_xpt->n_children * 100 / n_xpts : 0)); |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 2414 | STATS("XPt init expansions: %u\n", n_xpt_init_expansions); |
| 2415 | STATS("XPt later expansions: %u\n", n_xpt_later_expansions); |
| 2416 | STATS("SXPt allocs: %u\n", n_sxpt_allocs); |
| 2417 | STATS("SXPt frees: %u\n", n_sxpt_frees); |
| 2418 | STATS("skipped snapshots: %u\n", n_skipped_snapshots); |
| 2419 | STATS("real snapshots: %u\n", n_real_snapshots); |
| 2420 | STATS("detailed snapshots: %u\n", n_detailed_snapshots); |
| 2421 | STATS("peak snapshots: %u\n", n_peak_snapshots); |
| 2422 | STATS("cullings: %u\n", n_cullings); |
| 2423 | STATS("XCon redos: %u\n", n_XCon_redos); |
philippe | f066315 | 2013-12-19 19:10:20 +0000 | [diff] [blame] | 2424 | #undef STATS |
| 2425 | } |
| 2426 | |
| 2427 | //------------------------------------------------------------// |
| 2428 | //--- Finalisation ---// |
| 2429 | //------------------------------------------------------------// |
| 2430 | |
| 2431 | static void ms_fini(Int exit_status) |
| 2432 | { |
| 2433 | // Output. |
| 2434 | write_snapshots_array_to_file(); |
| 2435 | |
| 2436 | // Stats |
| 2437 | tl_assert(n_xpts > 0); // always have alloc_xpt |
| 2438 | |
| 2439 | if (VG_(clo_stats)) |
| 2440 | ms_print_stats(); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2443 | |
| 2444 | //------------------------------------------------------------// |
| 2445 | //--- Initialisation ---// |
| 2446 | //------------------------------------------------------------// |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2447 | |
| 2448 | static void ms_post_clo_init(void) |
| 2449 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2450 | Int i; |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2451 | HChar* LD_PRELOAD_val; |
| 2452 | HChar* s; |
| 2453 | HChar* s2; |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2454 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2455 | // Check options. |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 2456 | if (clo_pages_as_heap) { |
| 2457 | if (clo_stacks) { |
florian | 5b99e66 | 2014-11-29 14:41:32 +0000 | [diff] [blame] | 2458 | VG_(fmsg_bad_option)("--pages-as-heap=yes", |
| 2459 | "Cannot be used together with --stacks=yes"); |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 2460 | } |
| 2461 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2462 | if (!clo_heap) { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 2463 | clo_pages_as_heap = False; |
| 2464 | } |
| 2465 | |
| 2466 | // If --pages-as-heap=yes we don't want malloc replacement to occur. So we |
| 2467 | // disable vgpreload_massif-$PLATFORM.so by removing it from LD_PRELOAD (or |
| 2468 | // platform-equivalent). We replace it entirely with spaces because then |
| 2469 | // the linker doesn't complain (it does complain if we just change the name |
| 2470 | // to a bogus file). This is a bit of a hack, but LD_PRELOAD is setup well |
| 2471 | // before tool initialisation, so this seems the best way to do it. |
| 2472 | if (clo_pages_as_heap) { |
| 2473 | clo_heap_admin = 0; // No heap admin on pages. |
| 2474 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2475 | LD_PRELOAD_val = VG_(getenv)( VG_(LD_PRELOAD_var_name) ); |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 2476 | tl_assert(LD_PRELOAD_val); |
| 2477 | |
| 2478 | // Make sure the vgpreload_core-$PLATFORM entry is there, for sanity. |
| 2479 | s2 = VG_(strstr)(LD_PRELOAD_val, "vgpreload_core"); |
| 2480 | tl_assert(s2); |
| 2481 | |
| 2482 | // Now find the vgpreload_massif-$PLATFORM entry. |
| 2483 | s2 = VG_(strstr)(LD_PRELOAD_val, "vgpreload_massif"); |
| 2484 | tl_assert(s2); |
| 2485 | |
| 2486 | // Blank out everything to the previous ':', which must be there because |
| 2487 | // of the preceding vgpreload_core-$PLATFORM entry. |
| 2488 | for (s = s2; *s != ':'; s--) { |
| 2489 | *s = ' '; |
| 2490 | } |
| 2491 | |
| 2492 | // Blank out everything to the end of the entry, which will be '\0' if |
| 2493 | // LD_PRELOAD was empty before Valgrind started, or ':' otherwise. |
| 2494 | for (s = s2; *s != ':' && *s != '\0'; s++) { |
| 2495 | *s = ' '; |
| 2496 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2499 | // Print alloc-fns and ignore-fns, if necessary. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2500 | if (VG_(clo_verbosity) > 1) { |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 2501 | VERB(1, "alloc-fns:\n"); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2502 | for (i = 0; i < VG_(sizeXA)(alloc_fns); i++) { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2503 | HChar** fn_ptr = VG_(indexXA)(alloc_fns, i); |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 2504 | VERB(1, " %s\n", *fn_ptr); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 2507 | VERB(1, "ignore-fns:\n"); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2508 | if (0 == VG_(sizeXA)(ignore_fns)) { |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 2509 | VERB(1, " <empty>\n"); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2510 | } |
| 2511 | for (i = 0; i < VG_(sizeXA)(ignore_fns); i++) { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2512 | HChar** fn_ptr = VG_(indexXA)(ignore_fns, i); |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 2513 | VERB(1, " %d: %s\n", i, *fn_ptr); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2514 | } |
| 2515 | } |
| 2516 | |
| 2517 | // Events to track. |
| 2518 | if (clo_stacks) { |
| 2519 | VG_(track_new_mem_stack) ( new_mem_stack ); |
| 2520 | VG_(track_die_mem_stack) ( die_mem_stack ); |
| 2521 | VG_(track_new_mem_stack_signal) ( new_mem_stack_signal ); |
| 2522 | VG_(track_die_mem_stack_signal) ( die_mem_stack_signal ); |
| 2523 | } |
| 2524 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 2525 | if (clo_pages_as_heap) { |
| 2526 | VG_(track_new_mem_startup) ( ms_new_mem_startup ); |
| 2527 | VG_(track_new_mem_brk) ( ms_new_mem_brk ); |
| 2528 | VG_(track_new_mem_mmap) ( ms_new_mem_mmap ); |
| 2529 | |
| 2530 | VG_(track_copy_mem_remap) ( ms_copy_mem_remap ); |
| 2531 | |
| 2532 | VG_(track_die_mem_brk) ( ms_die_mem_brk ); |
| 2533 | VG_(track_die_mem_munmap) ( ms_die_mem_munmap ); |
| 2534 | } |
| 2535 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2536 | // Initialise snapshot array, and sanity-check it. |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2537 | snapshots = VG_(malloc)("ms.main.mpoci.1", |
| 2538 | sizeof(Snapshot) * clo_max_snapshots); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2539 | // We don't want to do snapshot sanity checks here, because they're |
| 2540 | // currently uninitialised. |
| 2541 | for (i = 0; i < clo_max_snapshots; i++) { |
| 2542 | clear_snapshot( & snapshots[i], /*do_sanity_check*/False ); |
| 2543 | } |
| 2544 | sanity_check_snapshots_array(); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
tom | 151a639 | 2005-11-11 12:30:36 +0000 | [diff] [blame] | 2547 | static void ms_pre_clo_init(void) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2548 | { |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2549 | VG_(details_name) ("Massif"); |
| 2550 | VG_(details_version) (NULL); |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 2551 | VG_(details_description) ("a heap profiler"); |
njn | 9a0cba4 | 2007-04-15 22:15:57 +0000 | [diff] [blame] | 2552 | VG_(details_copyright_author)( |
sewardj | 0f157dd | 2013-10-18 14:27:36 +0000 | [diff] [blame] | 2553 | "Copyright (C) 2003-2013, and GNU GPL'd, by Nicholas Nethercote"); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2554 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
| 2555 | |
sewardj | 1e0fff6 | 2011-01-10 15:01:03 +0000 | [diff] [blame] | 2556 | VG_(details_avg_translation_sizeB) ( 330 ); |
| 2557 | |
sewardj | 8d47a61 | 2015-02-05 12:59:46 +0000 | [diff] [blame] | 2558 | VG_(clo_vex_control).iropt_register_updates_default |
| 2559 | = VG_(clo_px_file_backed) |
philippe | 5b240c2 | 2012-08-14 22:28:31 +0000 | [diff] [blame] | 2560 | = VexRegUpdSpAtMemAccess; // overridable by the user. |
| 2561 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2562 | // Basic functions. |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2563 | VG_(basic_tool_funcs) (ms_post_clo_init, |
| 2564 | ms_instrument, |
| 2565 | ms_fini); |
| 2566 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2567 | // Needs. |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2568 | VG_(needs_libc_freeres)(); |
| 2569 | VG_(needs_command_line_options)(ms_process_cmd_line_option, |
| 2570 | ms_print_usage, |
| 2571 | ms_print_debug_usage); |
| 2572 | VG_(needs_client_requests) (ms_handle_client_request); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2573 | VG_(needs_sanity_checks) (ms_cheap_sanity_check, |
| 2574 | ms_expensive_sanity_check); |
philippe | f066315 | 2013-12-19 19:10:20 +0000 | [diff] [blame] | 2575 | VG_(needs_print_stats) (ms_print_stats); |
njn | fc51f8d | 2005-06-21 03:20:17 +0000 | [diff] [blame] | 2576 | VG_(needs_malloc_replacement) (ms_malloc, |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2577 | ms___builtin_new, |
| 2578 | ms___builtin_vec_new, |
| 2579 | ms_memalign, |
| 2580 | ms_calloc, |
| 2581 | ms_free, |
| 2582 | ms___builtin_delete, |
| 2583 | ms___builtin_vec_delete, |
| 2584 | ms_realloc, |
njn | 8b140de | 2009-02-17 04:31:18 +0000 | [diff] [blame] | 2585 | ms_malloc_usable_size, |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2586 | 0 ); |
| 2587 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2588 | // HP_Chunks. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2589 | malloc_list = VG_(HT_construct)( "Massif's malloc list" ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2590 | |
| 2591 | // Dummy node at top of the context structure. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2592 | alloc_xpt = new_XPt(/*ip*/0, /*parent*/NULL); |
| 2593 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2594 | // Initialise alloc_fns and ignore_fns. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2595 | init_alloc_fns(); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2596 | init_ignore_fns(); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2597 | |
| 2598 | // Initialise args_for_massif. |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2599 | args_for_massif = VG_(newXA)(VG_(malloc), "ms.main.mprci.1", |
| 2600 | VG_(free), sizeof(HChar*)); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2601 | } |
| 2602 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2603 | VG_DETERMINE_INTERFACE_VERSION(ms_pre_clo_init) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2604 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2605 | //--------------------------------------------------------------------// |
| 2606 | //--- end ---// |
| 2607 | //--------------------------------------------------------------------// |