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 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 9 | Copyright (C) 2003-2017 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 | // Todo -- low priority: |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 73 | // - In each XPt, record both bytes and the number of allocations, and |
| 74 | // possibly the global number of allocations. |
njn | 0f85e02 | 2007-11-27 02:03:44 +0000 | [diff] [blame] | 75 | // - (Andy Lin) Give a stack trace on detailed snapshots? |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 76 | // - (Artur Wisz) add a feature to Massif to ignore any heap blocks larger |
| 77 | // than a certain size! Because: "linux's malloc allows to set a |
| 78 | // MMAP_THRESHOLD value, so we set it to 4096 - all blocks above that will |
| 79 | // be handled directly by the kernel, and are guaranteed to be returned to |
| 80 | // the system when freed. So we needed to profile only blocks below this |
| 81 | // limit." |
| 82 | // |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 83 | // File format working notes: |
| 84 | |
| 85 | #if 0 |
| 86 | desc: --heap-admin=foo |
| 87 | cmd: date |
| 88 | time_unit: ms |
| 89 | #----------- |
| 90 | snapshot=0 |
| 91 | #----------- |
| 92 | time=0 |
| 93 | mem_heap_B=0 |
| 94 | mem_heap_admin_B=0 |
| 95 | mem_stacks_B=0 |
| 96 | heap_tree=empty |
| 97 | #----------- |
| 98 | snapshot=1 |
| 99 | #----------- |
| 100 | time=353 |
| 101 | mem_heap_B=5 |
| 102 | mem_heap_admin_B=0 |
| 103 | mem_stacks_B=0 |
| 104 | heap_tree=detailed |
| 105 | n1: 5 (heap allocation functions) malloc/new/new[], --alloc-fns, etc. |
| 106 | n1: 5 0x27F6E0: _nl_normalize_codeset (in /lib/libc-2.3.5.so) |
| 107 | n1: 5 0x279DE6: _nl_load_locale_from_archive (in /lib/libc-2.3.5.so) |
| 108 | n1: 5 0x278E97: _nl_find_locale (in /lib/libc-2.3.5.so) |
| 109 | n1: 5 0x278871: setlocale (in /lib/libc-2.3.5.so) |
| 110 | n1: 5 0x8049821: (within /bin/date) |
| 111 | n0: 5 0x26ED5E: (below main) (in /lib/libc-2.3.5.so) |
| 112 | |
| 113 | |
| 114 | n_events: n time(ms) total(B) useful-heap(B) admin-heap(B) stacks(B) |
| 115 | t_events: B |
| 116 | n 0 0 0 0 0 |
| 117 | n 0 0 0 0 0 |
| 118 | t1: 5 <string...> |
| 119 | t1: 6 <string...> |
| 120 | |
| 121 | Ideas: |
| 122 | - each snapshot specifies an x-axis value and one or more y-axis values. |
| 123 | - can display the y-axis values separately if you like |
| 124 | - can completely separate connection between snapshots and trees. |
| 125 | |
| 126 | Challenges: |
| 127 | - how to specify and scale/abbreviate units on axes? |
| 128 | - how to combine multiple values into the y-axis? |
| 129 | |
| 130 | --------------------------------------------------------------------------------Command: date |
| 131 | Massif arguments: --heap-admin=foo |
| 132 | ms_print arguments: massif.out |
| 133 | -------------------------------------------------------------------------------- |
| 134 | KB |
| 135 | 6.472^ :# |
| 136 | | :# :: . . |
| 137 | ... |
| 138 | | ::@ :@ :@ :@:::# :: : :::: |
| 139 | 0 +-----------------------------------@---@---@-----@--@---#-------------->ms 0 713 |
| 140 | |
| 141 | Number of snapshots: 50 |
| 142 | Detailed snapshots: [2, 11, 13, 19, 25, 32 (peak)] |
| 143 | -------------------------------------------------------------------------------- n time(ms) total(B) useful-heap(B) admin-heap(B) stacks(B) |
| 144 | -------------------------------------------------------------------------------- 0 0 0 0 0 0 |
| 145 | 1 345 5 5 0 0 |
| 146 | 2 353 5 5 0 0 |
| 147 | 100.00% (5B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc. |
| 148 | ->100.00% (5B) 0x27F6E0: _nl_normalize_codeset (in /lib/libc-2.3.5.so) |
| 149 | #endif |
| 150 | |
| 151 | //--------------------------------------------------------------------------- |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 152 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 153 | #include "pub_tool_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 154 | #include "pub_tool_vki.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 155 | #include "pub_tool_aspacemgr.h" |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 156 | #include "pub_tool_debuginfo.h" |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 157 | #include "pub_tool_hashtable.h" |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 158 | #include "pub_tool_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 159 | #include "pub_tool_libcassert.h" |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 160 | #include "pub_tool_libcfile.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 161 | #include "pub_tool_libcprint.h" |
njn | f39e9a3 | 2005-06-12 02:43:17 +0000 | [diff] [blame] | 162 | #include "pub_tool_libcproc.h" |
njn | b506bd8 | 2005-06-21 04:01:51 +0000 | [diff] [blame] | 163 | #include "pub_tool_machine.h" |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 164 | #include "pub_tool_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 165 | #include "pub_tool_options.h" |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 166 | #include "pub_tool_poolalloc.h" |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 167 | #include "pub_tool_replacemalloc.h" |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 168 | #include "pub_tool_stacktrace.h" |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 169 | #include "pub_tool_threadstate.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 170 | #include "pub_tool_tooliface.h" |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 171 | #include "pub_tool_xarray.h" |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 172 | #include "pub_tool_xtree.h" |
| 173 | #include "pub_tool_xtmemory.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 174 | #include "pub_tool_clientstate.h" |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 175 | #include "pub_tool_gdbserver.h" |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 176 | |
florian | 1a046d5 | 2013-09-16 20:56:35 +0000 | [diff] [blame] | 177 | #include "pub_tool_clreq.h" // For {MALLOC,FREE}LIKE_BLOCK |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 178 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 179 | //------------------------------------------------------------*/ |
| 180 | //--- Overview of operation ---*/ |
| 181 | //------------------------------------------------------------*/ |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 182 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 183 | // The size of the stacks and heap is tracked. The heap is tracked in a lot |
| 184 | // of detail, enough to tell how many bytes each line of code is responsible |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 185 | // for, more or less. The main data structure is an xtree maintaining the |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 186 | // call tree beneath all the allocation functions like malloc(). |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 187 | // (Alternatively, if --pages-as-heap=yes is specified, memory is tracked at |
| 188 | // the page level, and each page is treated much like a heap block. We use |
| 189 | // "heap" throughout below to cover this case because the concepts are all the |
| 190 | // same.) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 191 | // |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 192 | // "Snapshots" are recordings of the memory usage. There are two basic |
| 193 | // kinds: |
| 194 | // - Normal: these record the current time, total memory size, total heap |
| 195 | // size, heap admin size and stack size. |
| 196 | // - Detailed: these record those things in a normal snapshot, plus a very |
| 197 | // detailed XTree (see below) indicating how the heap is structured. |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 198 | // |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 199 | // Snapshots are taken every so often. There are two storage classes of |
| 200 | // snapshots: |
| 201 | // - Temporary: Massif does a temporary snapshot every so often. The idea |
| 202 | // is to always have a certain number of temporary snapshots around. So |
| 203 | // we take them frequently to begin with, but decreasingly often as the |
| 204 | // program continues to run. Also, we remove some old ones after a while. |
| 205 | // Overall it's a kind of exponential decay thing. Most of these are |
| 206 | // normal snapshots, a small fraction are detailed snapshots. |
| 207 | // - Permanent: Massif takes a permanent (detailed) snapshot in some |
| 208 | // circumstances. They are: |
| 209 | // - Peak snapshot: When the memory usage peak is reached, it takes a |
| 210 | // snapshot. It keeps this, unless the peak is subsequently exceeded, |
| 211 | // in which case it will overwrite the peak snapshot. |
| 212 | // - User-requested snapshots: These are done in response to client |
| 213 | // requests. They are always kept. |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 214 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 215 | // Used for printing things when clo_verbosity > 1. |
| 216 | #define VERB(verb, format, args...) \ |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 217 | if (UNLIKELY(VG_(clo_verbosity) > verb)) { \ |
| 218 | VG_(dmsg)("Massif: " format, ##args); \ |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 219 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 220 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 221 | //------------------------------------------------------------// |
| 222 | //--- Statistics ---// |
| 223 | //------------------------------------------------------------// |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 224 | |
| 225 | // Konqueror startup, to give an idea of the numbers involved with a biggish |
| 226 | // program, with default depth: |
| 227 | // |
| 228 | // depth=3 depth=40 |
| 229 | // - 310,000 allocations |
| 230 | // - 300,000 frees |
| 231 | // - 15,000 XPts 800,000 XPts |
| 232 | // - 1,800 top-XPts |
| 233 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 234 | static UInt n_heap_allocs = 0; |
| 235 | static UInt n_heap_reallocs = 0; |
| 236 | static UInt n_heap_frees = 0; |
| 237 | static UInt n_ignored_heap_allocs = 0; |
| 238 | static UInt n_ignored_heap_frees = 0; |
| 239 | static UInt n_ignored_heap_reallocs = 0; |
| 240 | static UInt n_stack_allocs = 0; |
| 241 | static UInt n_stack_frees = 0; |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 242 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 243 | static UInt n_skipped_snapshots = 0; |
| 244 | static UInt n_real_snapshots = 0; |
| 245 | static UInt n_detailed_snapshots = 0; |
| 246 | static UInt n_peak_snapshots = 0; |
| 247 | static UInt n_cullings = 0; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 248 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 249 | //------------------------------------------------------------// |
| 250 | //--- Globals ---// |
| 251 | //------------------------------------------------------------// |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 252 | |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 253 | // Number of guest instructions executed so far. Only used with |
| 254 | // --time-unit=i. |
| 255 | static Long guest_instrs_executed = 0; |
| 256 | |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 257 | static SizeT heap_szB = 0; // Live heap size |
| 258 | static SizeT heap_extra_szB = 0; // Live heap extra size -- slop + admin bytes |
| 259 | static SizeT stacks_szB = 0; // Live stacks size |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 260 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 261 | // This is the total size from the current peak snapshot, or 0 if no peak |
| 262 | // snapshot has been taken yet. |
| 263 | static SizeT peak_snapshot_total_szB = 0; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 264 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 265 | // Incremented every time memory is allocated/deallocated, by the |
| 266 | // allocated/deallocated amount; includes heap, heap-admin and stack |
| 267 | // memory. An alternative to milliseconds as a unit of program "time". |
| 268 | static ULong total_allocs_deallocs_szB = 0; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 269 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 270 | // When running with --heap=yes --pages-as-heap=no, we don't start taking |
| 271 | // snapshots until the first basic block is executed, rather than doing it in |
| 272 | // ms_post_clo_init (which is the obvious spot), for two reasons. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 273 | // - It lets us ignore stack events prior to that, because they're not |
| 274 | // really proper ones and just would screw things up. |
| 275 | // - Because there's still some core initialisation to do, and so there |
| 276 | // would be an artificial time gap between the first and second snapshots. |
| 277 | // |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 278 | // When running with --heap=yes --pages-as-heap=yes, snapshots start much |
| 279 | // earlier due to new_mem_startup so this isn't relevant. |
| 280 | // |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 281 | static Bool have_started_executing_code = False; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 282 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 283 | //------------------------------------------------------------// |
| 284 | //--- Alloc fns ---// |
| 285 | //------------------------------------------------------------// |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 286 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 287 | static XArray* alloc_fns; |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 288 | static XArray* ignore_fns; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 289 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 290 | static void init_alloc_fns(void) |
| 291 | { |
| 292 | // Create the list, and add the default elements. |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 293 | alloc_fns = VG_(newXA)(VG_(malloc), "ms.main.iaf.1", |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 294 | VG_(free), sizeof(HChar*)); |
florian | 6bd9dc1 | 2012-11-23 16:17:43 +0000 | [diff] [blame] | 295 | #define DO(x) { const HChar* s = x; VG_(addToXA)(alloc_fns, &s); } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 296 | |
njn | 13118f8 | 2009-07-01 02:44:12 +0000 | [diff] [blame] | 297 | // Ordered roughly according to (presumed) frequency. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 298 | // Nb: The C++ "operator new*" ones are overloadable. We include them |
| 299 | // always anyway, because even if they're overloaded, it would be a |
| 300 | // prodigiously stupid overloading that caused them to not allocate |
| 301 | // memory. |
njn | 343a504 | 2009-07-01 05:05:38 +0000 | [diff] [blame] | 302 | // |
| 303 | // XXX: because we don't look at the first stack entry (unless it's a |
| 304 | // custom allocation) there's not much point to having all these alloc |
| 305 | // functions here -- they should never appear anywhere (I think?) other |
| 306 | // than the top stack entry. The only exceptions are those that in |
| 307 | // vg_replace_malloc.c are partly or fully implemented in terms of another |
| 308 | // alloc function: realloc (which uses malloc); valloc, |
| 309 | // malloc_zone_valloc, posix_memalign and memalign_common (which use |
| 310 | // memalign). |
| 311 | // |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 312 | DO("malloc" ); |
| 313 | DO("__builtin_new" ); |
| 314 | DO("operator new(unsigned)" ); |
| 315 | DO("operator new(unsigned long)" ); |
| 316 | DO("__builtin_vec_new" ); |
| 317 | DO("operator new[](unsigned)" ); |
| 318 | DO("operator new[](unsigned long)" ); |
| 319 | DO("calloc" ); |
| 320 | DO("realloc" ); |
| 321 | DO("memalign" ); |
njn | 13118f8 | 2009-07-01 02:44:12 +0000 | [diff] [blame] | 322 | DO("posix_memalign" ); |
| 323 | DO("valloc" ); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 324 | DO("operator new(unsigned, std::nothrow_t const&)" ); |
| 325 | DO("operator new[](unsigned, std::nothrow_t const&)" ); |
| 326 | DO("operator new(unsigned long, std::nothrow_t const&)" ); |
| 327 | DO("operator new[](unsigned long, std::nothrow_t const&)"); |
sewardj | 6e9de46 | 2011-06-28 07:25:29 +0000 | [diff] [blame] | 328 | #if defined(VGO_darwin) |
njn | 13118f8 | 2009-07-01 02:44:12 +0000 | [diff] [blame] | 329 | DO("malloc_zone_malloc" ); |
| 330 | DO("malloc_zone_calloc" ); |
| 331 | DO("malloc_zone_realloc" ); |
| 332 | DO("malloc_zone_memalign" ); |
| 333 | DO("malloc_zone_valloc" ); |
| 334 | #endif |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 335 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 336 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 337 | static void init_ignore_fns(void) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 338 | { |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 339 | // Create the (empty) list. |
| 340 | ignore_fns = VG_(newXA)(VG_(malloc), "ms.main.iif.1", |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 341 | VG_(free), sizeof(HChar*)); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 342 | } |
| 343 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 344 | //------------------------------------------------------------// |
| 345 | //--- Command line args ---// |
| 346 | //------------------------------------------------------------// |
| 347 | |
| 348 | #define MAX_DEPTH 200 |
| 349 | |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 350 | typedef enum { TimeI, TimeMS, TimeB } TimeUnit; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 351 | |
florian | e58e8a7 | 2012-10-20 19:57:16 +0000 | [diff] [blame] | 352 | static const HChar* TimeUnit_to_string(TimeUnit time_unit) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 353 | { |
| 354 | switch (time_unit) { |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 355 | case TimeI: return "i"; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 356 | case TimeMS: return "ms"; |
| 357 | case TimeB: return "B"; |
| 358 | default: tl_assert2(0, "TimeUnit_to_string: unrecognised TimeUnit"); |
| 359 | } |
| 360 | } |
| 361 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 362 | static Bool clo_heap = True; |
njn | 429afb4 | 2007-11-02 04:12:48 +0000 | [diff] [blame] | 363 | // clo_heap_admin is deliberately a word-sized type. At one point it was |
| 364 | // a UInt, but this caused problems on 64-bit machines when it was |
| 365 | // multiplied by a small negative number and then promoted to a |
| 366 | // 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] | 367 | static SSizeT clo_heap_admin = 8; |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 368 | static Bool clo_pages_as_heap = False; |
njn | 62721e9 | 2007-11-11 22:15:58 +0000 | [diff] [blame] | 369 | static Bool clo_stacks = False; |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 370 | static Int clo_depth = 30; |
njn | 62721e9 | 2007-11-11 22:15:58 +0000 | [diff] [blame] | 371 | static double clo_threshold = 1.0; // percentage |
| 372 | static double clo_peak_inaccuracy = 1.0; // percentage |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 373 | static Int clo_time_unit = TimeI; |
| 374 | static Int clo_detailed_freq = 10; |
| 375 | static Int clo_max_snapshots = 100; |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 376 | static const HChar* clo_massif_out_file = "massif.out.%p"; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 377 | |
| 378 | static XArray* args_for_massif; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 379 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 380 | static Bool ms_process_cmd_line_option(const HChar* arg) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 381 | { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 382 | const HChar* tmp_str; |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 383 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 384 | // Remember the arg for later use. |
| 385 | VG_(addToXA)(args_for_massif, &arg); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 386 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 387 | if VG_BOOL_CLO(arg, "--heap", clo_heap) {} |
| 388 | else if VG_BINT_CLO(arg, "--heap-admin", clo_heap_admin, 0, 1024) {} |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 389 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 390 | else if VG_BOOL_CLO(arg, "--stacks", clo_stacks) {} |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 391 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 392 | else if VG_BOOL_CLO(arg, "--pages-as-heap", clo_pages_as_heap) {} |
| 393 | |
| 394 | else if VG_BINT_CLO(arg, "--depth", clo_depth, 1, MAX_DEPTH) {} |
| 395 | |
| 396 | else if VG_STR_CLO(arg, "--alloc-fn", tmp_str) { |
| 397 | VG_(addToXA)(alloc_fns, &tmp_str); |
| 398 | } |
| 399 | else if VG_STR_CLO(arg, "--ignore-fn", tmp_str) { |
| 400 | VG_(addToXA)(ignore_fns, &tmp_str); |
| 401 | } |
| 402 | |
njn | b1cc5d6 | 2010-07-06 04:05:23 +0000 | [diff] [blame] | 403 | else if VG_DBL_CLO(arg, "--threshold", clo_threshold) { |
| 404 | if (clo_threshold < 0 || clo_threshold > 100) { |
| 405 | VG_(fmsg_bad_option)(arg, |
| 406 | "--threshold must be between 0.0 and 100.0\n"); |
| 407 | } |
| 408 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 409 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 410 | else if VG_DBL_CLO(arg, "--peak-inaccuracy", clo_peak_inaccuracy) {} |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 411 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 412 | else if VG_XACT_CLO(arg, "--time-unit=i", clo_time_unit, TimeI) {} |
| 413 | else if VG_XACT_CLO(arg, "--time-unit=ms", clo_time_unit, TimeMS) {} |
| 414 | else if VG_XACT_CLO(arg, "--time-unit=B", clo_time_unit, TimeB) {} |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 415 | |
njn | e006e6f | 2010-12-08 02:42:38 +0000 | [diff] [blame] | 416 | else if VG_BINT_CLO(arg, "--detailed-freq", clo_detailed_freq, 1, 1000000) {} |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 417 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 418 | else if VG_BINT_CLO(arg, "--max-snapshots", clo_max_snapshots, 10, 1000) {} |
| 419 | |
njn | 83df0b6 | 2009-02-25 01:01:05 +0000 | [diff] [blame] | 420 | else if VG_STR_CLO(arg, "--massif-out-file", clo_massif_out_file) {} |
njn | f4c665f | 2007-11-20 07:55:40 +0000 | [diff] [blame] | 421 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 422 | else |
| 423 | return VG_(replacement_malloc_process_cmd_line_option)(arg); |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 424 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 425 | return True; |
| 426 | } |
| 427 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 428 | static void ms_print_usage(void) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 429 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 430 | VG_(printf)( |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 431 | " --heap=no|yes profile heap blocks [yes]\n" |
njn | a15b666 | 2009-08-04 05:59:46 +0000 | [diff] [blame] | 432 | " --heap-admin=<size> average admin bytes per heap block;\n" |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 433 | " ignored if --heap=no [8]\n" |
| 434 | " --stacks=no|yes profile stack(s) [no]\n" |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 435 | " --pages-as-heap=no|yes profile memory at the page level [no]\n" |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 436 | " --depth=<number> depth of contexts [30]\n" |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 437 | " --alloc-fn=<name> specify <name> as an alloc function [empty]\n" |
| 438 | " --ignore-fn=<name> ignore heap allocations within <name> [empty]\n" |
njn | 62721e9 | 2007-11-11 22:15:58 +0000 | [diff] [blame] | 439 | " --threshold=<m.n> significance threshold, as a percentage [1.0]\n" |
| 440 | " --peak-inaccuracy=<m.n> maximum peak inaccuracy, as a percentage [1.0]\n" |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 441 | " --time-unit=i|ms|B time unit: instructions executed, milliseconds\n" |
| 442 | " or heap bytes alloc'd/dealloc'd [i]\n" |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 443 | " --detailed-freq=<N> every Nth snapshot should be detailed [10]\n" |
| 444 | " --max-snapshots=<N> maximum number of snapshots recorded [100]\n" |
njn | 374a36d | 2007-11-23 01:41:32 +0000 | [diff] [blame] | 445 | " --massif-out-file=<file> output file name [massif.out.%%p]\n" |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 446 | ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 447 | } |
| 448 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 449 | static void ms_print_debug_usage(void) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 450 | { |
njn | 97db761 | 2009-08-04 02:32:55 +0000 | [diff] [blame] | 451 | VG_(printf)( |
| 452 | " (none)\n" |
| 453 | ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 454 | } |
| 455 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 456 | |
| 457 | //------------------------------------------------------------// |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 458 | //--- XTrees ---// |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 459 | //------------------------------------------------------------// |
| 460 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 461 | // The details of the heap are represented by a single XTree. |
| 462 | // This XTree maintains the nr of allocated bytes for each |
| 463 | // stacktrace/execontext. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 464 | // |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 465 | // The root of the Xtree will be output as a top node 'alloc functions', |
| 466 | // which represents all allocation functions, eg: |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 467 | // - malloc/calloc/realloc/memalign/new/new[]; |
| 468 | // - user-specified allocation functions (using --alloc-fn); |
| 469 | // - custom allocation (MALLOCLIKE) points |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 470 | static XTree* heap_xt; |
| 471 | /* heap_xt contains a SizeT: the nr of allocated bytes by this execontext. */ |
| 472 | static void init_szB(void* value) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 473 | { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 474 | *((SizeT*)value) = 0; |
| 475 | } |
| 476 | static void add_szB(void* to, const void* value) |
| 477 | { |
| 478 | *((SizeT*)to) += *((const SizeT*)value); |
| 479 | } |
| 480 | static void sub_szB(void* from, const void* value) |
| 481 | { |
| 482 | *((SizeT*)from) -= *((const SizeT*)value); |
| 483 | } |
| 484 | static ULong alloc_szB(const void* value) |
| 485 | { |
| 486 | return (ULong)*((const SizeT*)value); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 487 | } |
| 488 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 489 | |
| 490 | //------------------------------------------------------------// |
| 491 | //--- XTree Operations ---// |
| 492 | //------------------------------------------------------------// |
| 493 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 494 | // This is the limit on the number of filtered alloc-fns that can be in a |
| 495 | // single stacktrace. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 496 | #define MAX_OVERESTIMATE 50 |
| 497 | #define MAX_IPS (MAX_DEPTH + MAX_OVERESTIMATE) |
| 498 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 499 | // filtering out uninteresting entries: |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 500 | // alloc-fns and entries above alloc-fns, and entries below main-or-below-main. |
| 501 | // Eg: alloc-fn1 / alloc-fn2 / a / b / main / (below main) / c |
| 502 | // becomes: a / b / main |
| 503 | // Nb: it's possible to end up with an empty trace, eg. if 'main' is marked |
| 504 | // as an alloc-fn. This is ok. |
| 505 | static |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 506 | void filter_IPs (Addr* ips, Int n_ips, |
| 507 | UInt* top, UInt* n_ips_sel) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 508 | { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 509 | Int i; |
| 510 | Bool top_has_fnname; |
| 511 | const HChar *fnname; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 512 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 513 | *top = 0; |
| 514 | *n_ips_sel = n_ips; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 515 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 516 | // Advance *top as long as we find alloc functions |
| 517 | // PW Nov 2016 xtree work: |
| 518 | // old massif code was doing something really strange(?buggy): |
| 519 | // 'sliding' a bunch of functions without names by removing an |
| 520 | // alloc function 'inside' a stacktrace e.g. |
| 521 | // 0x1 0x2 0x3 alloc func1 main |
| 522 | // becomes 0x1 0x2 0x3 func1 main |
| 523 | for (i = *top; i < n_ips; i++) { |
| 524 | top_has_fnname = VG_(get_fnname)(ips[*top], &fnname); |
| 525 | if (top_has_fnname && VG_(strIsMemberXA)(alloc_fns, fnname)) { |
| 526 | VERB(4, "filtering alloc fn %s\n", fnname); |
| 527 | (*top)++; |
| 528 | (*n_ips_sel)--; |
| 529 | } else { |
| 530 | break; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 533 | |
| 534 | // filter the whole stacktrace if this allocation has to be ignored. |
| 535 | if (*n_ips_sel > 0 |
| 536 | && top_has_fnname |
| 537 | && VG_(strIsMemberXA)(ignore_fns, fnname)) { |
| 538 | VERB(4, "ignored allocation from fn %s\n", fnname); |
| 539 | *top = n_ips; |
| 540 | *n_ips_sel = 0; |
| 541 | } |
| 542 | |
| 543 | |
| 544 | if (!VG_(clo_show_below_main) && *n_ips_sel > 0 ) { |
| 545 | Int mbm = VG_(XT_offset_main_or_below_main)(ips, n_ips); |
| 546 | |
| 547 | if (mbm < *top) { |
| 548 | // Special case: the first main (or below main) function is an |
| 549 | // alloc function. |
| 550 | *n_ips_sel = 1; |
| 551 | VERB(4, "main/below main: keeping 1 fn\n"); |
| 552 | } else { |
| 553 | *n_ips_sel -= n_ips - mbm - 1; |
| 554 | VERB(4, "main/below main: filtering %d\n", n_ips - mbm - 1); |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | // filter the frames if we have more than clo_depth |
| 559 | if (*n_ips_sel > clo_depth) { |
| 560 | VERB(4, "filtering IPs above clo_depth\n"); |
| 561 | *n_ips_sel = clo_depth; |
| 562 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 563 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 564 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 565 | // Capture a stacktrace, and make an ec of it, without the first entry |
| 566 | // if exclude_first_entry is True. |
| 567 | static ExeContext* make_ec(ThreadId tid, Bool exclude_first_entry) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 568 | { |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 569 | static Addr ips[MAX_IPS]; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 570 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 571 | // After this call, the IPs we want are in ips[0]..ips[n_ips-1]. |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 572 | Int n_ips = VG_(get_StackTrace)( tid, ips, clo_depth + MAX_OVERESTIMATE, |
| 573 | NULL/*array to dump SP values in*/, |
| 574 | NULL/*array to dump FP values in*/, |
| 575 | 0/*first_ip_delta*/ ); |
| 576 | if (exclude_first_entry && n_ips > 0) { |
| 577 | const HChar *fnname; |
| 578 | VERB(4, "removing top fn %s from stacktrace\n", |
| 579 | VG_(get_fnname)(ips[0], &fnname) ? fnname : "???"); |
| 580 | return VG_(make_ExeContext_from_StackTrace)(ips+1, n_ips-1); |
| 581 | } else |
| 582 | return VG_(make_ExeContext_from_StackTrace)(ips, n_ips); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 585 | // Create (or update) in heap_xt an xec corresponding to the stacktrace of tid. |
| 586 | // req_szB is added to the xec (unless ec is fully filtered). |
| 587 | // Returns the correspding XTree xec. |
| 588 | // exclude_first_entry is an optimisation: if True, automatically removes |
| 589 | // the top level IP from the stacktrace. Should be set to True if it is known |
| 590 | // that this is an alloc fn. The top function presumably will be something like |
| 591 | // malloc or __builtin_new that we're sure to filter out). |
| 592 | static Xecu add_heap_xt( ThreadId tid, SizeT req_szB, Bool exclude_first_entry) |
| 593 | { |
| 594 | ExeContext *ec = make_ec(tid, exclude_first_entry); |
| 595 | |
| 596 | if (UNLIKELY(VG_(clo_xtree_memory) == Vg_XTMemory_Full)) |
| 597 | VG_(XTMemory_Full_alloc)(req_szB, ec); |
| 598 | return VG_(XT_add_to_ec) (heap_xt, ec, &req_szB); |
| 599 | } |
| 600 | |
| 601 | // Substract req_szB from the heap_xt where. |
| 602 | static void sub_heap_xt(Xecu where, SizeT req_szB, Bool exclude_first_entry) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 603 | { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 604 | tl_assert(clo_heap); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 605 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 606 | if (0 == req_szB) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 607 | return; |
| 608 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 609 | VG_(XT_sub_from_xecu) (heap_xt, where, &req_szB); |
| 610 | if (UNLIKELY(VG_(clo_xtree_memory) == Vg_XTMemory_Full)) { |
| 611 | ExeContext *ec_free = make_ec(VG_(get_running_tid)(), |
| 612 | exclude_first_entry); |
| 613 | VG_(XTMemory_Full_free)(req_szB, |
| 614 | VG_(XT_get_ec_from_xecu)(heap_xt, where), |
| 615 | ec_free); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 616 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 620 | //------------------------------------------------------------// |
| 621 | //--- Snapshots ---// |
| 622 | //------------------------------------------------------------// |
| 623 | |
| 624 | // Snapshots are done in a way so that we always have a reasonable number of |
| 625 | // them. We start by taking them quickly. Once we hit our limit, we cull |
| 626 | // some (eg. half), and start taking them more slowly. Once we hit the |
| 627 | // limit again, we again cull and then take them even more slowly, and so |
| 628 | // on. |
| 629 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 630 | #define UNUSED_SNAPSHOT_TIME -333 // A conspicuous negative number. |
| 631 | |
| 632 | typedef |
| 633 | enum { |
| 634 | Normal = 77, |
| 635 | Peak, |
| 636 | Unused |
| 637 | } |
| 638 | SnapshotKind; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 639 | |
| 640 | typedef |
| 641 | struct { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 642 | SnapshotKind kind; |
| 643 | Time time; |
| 644 | SizeT heap_szB; |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 645 | SizeT heap_extra_szB;// Heap slop + admin bytes. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 646 | SizeT stacks_szB; |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 647 | XTree* xt; // Snapshot of heap_xt, if a detailed snapshot, |
| 648 | } // otherwise NULL. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 649 | Snapshot; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 650 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 651 | static UInt next_snapshot_i = 0; // Index of where next snapshot will go. |
| 652 | static Snapshot* snapshots; // Array of snapshots. |
| 653 | |
| 654 | static Bool is_snapshot_in_use(Snapshot* snapshot) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 655 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 656 | if (Unused == snapshot->kind) { |
| 657 | // If snapshot is unused, check all the fields are unset. |
| 658 | tl_assert(snapshot->time == UNUSED_SNAPSHOT_TIME); |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 659 | tl_assert(snapshot->heap_extra_szB == 0); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 660 | tl_assert(snapshot->heap_szB == 0); |
| 661 | tl_assert(snapshot->stacks_szB == 0); |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 662 | tl_assert(snapshot->xt == NULL); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 663 | return False; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 664 | } else { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 665 | tl_assert(snapshot->time != UNUSED_SNAPSHOT_TIME); |
| 666 | return True; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 670 | static Bool is_detailed_snapshot(Snapshot* snapshot) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 671 | { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 672 | return (snapshot->xt ? True : False); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 673 | } |
| 674 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 675 | static Bool is_uncullable_snapshot(Snapshot* snapshot) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 676 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 677 | return &snapshots[0] == snapshot // First snapshot |
| 678 | || &snapshots[next_snapshot_i-1] == snapshot // Last snapshot |
| 679 | || snapshot->kind == Peak; // Peak snapshot |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 680 | } |
| 681 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 682 | static void sanity_check_snapshot(Snapshot* snapshot) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 683 | { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 684 | // Not much we can sanity check. |
| 685 | tl_assert(snapshot->xt == NULL || snapshot->kind != Unused); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 686 | } |
| 687 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 688 | // All the used entries should look used, all the unused ones should be clear. |
| 689 | static void sanity_check_snapshots_array(void) |
| 690 | { |
| 691 | Int i; |
| 692 | for (i = 0; i < next_snapshot_i; i++) { |
| 693 | tl_assert( is_snapshot_in_use( & snapshots[i] )); |
| 694 | } |
| 695 | for ( ; i < clo_max_snapshots; i++) { |
| 696 | tl_assert(!is_snapshot_in_use( & snapshots[i] )); |
| 697 | } |
| 698 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 699 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 700 | // This zeroes all the fields in the snapshot, but does not free the xt |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 701 | // XTree if present. It also does a sanity check unless asked not to; we |
| 702 | // can't sanity check at startup when clearing the initial snapshots because |
| 703 | // they're full of junk. |
| 704 | static void clear_snapshot(Snapshot* snapshot, Bool do_sanity_check) |
| 705 | { |
| 706 | if (do_sanity_check) sanity_check_snapshot(snapshot); |
| 707 | snapshot->kind = Unused; |
| 708 | snapshot->time = UNUSED_SNAPSHOT_TIME; |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 709 | snapshot->heap_extra_szB = 0; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 710 | snapshot->heap_szB = 0; |
| 711 | snapshot->stacks_szB = 0; |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 712 | snapshot->xt = NULL; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 715 | // This zeroes all the fields in the snapshot, and frees the heap XTree xt if |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 716 | // present. |
| 717 | static void delete_snapshot(Snapshot* snapshot) |
| 718 | { |
| 719 | // Nb: if there's an XTree, we free it after calling clear_snapshot, |
| 720 | // because clear_snapshot does a sanity check which includes checking the |
| 721 | // XTree. |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 722 | XTree* tmp_xt = snapshot->xt; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 723 | clear_snapshot(snapshot, /*do_sanity_check*/True); |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 724 | if (tmp_xt) { |
| 725 | VG_(XT_delete)(tmp_xt); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 726 | } |
| 727 | } |
| 728 | |
florian | e58e8a7 | 2012-10-20 19:57:16 +0000 | [diff] [blame] | 729 | static void VERB_snapshot(Int verbosity, const HChar* prefix, Int i) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 730 | { |
| 731 | Snapshot* snapshot = &snapshots[i]; |
florian | e58e8a7 | 2012-10-20 19:57:16 +0000 | [diff] [blame] | 732 | const HChar* suffix; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 733 | switch (snapshot->kind) { |
| 734 | case Peak: suffix = "p"; break; |
| 735 | case Normal: suffix = ( is_detailed_snapshot(snapshot) ? "d" : "." ); break; |
| 736 | case Unused: suffix = "u"; break; |
| 737 | default: |
| 738 | tl_assert2(0, "VERB_snapshot: unknown snapshot kind: %d", snapshot->kind); |
| 739 | } |
florian | 16eef85 | 2015-08-03 21:05:20 +0000 | [diff] [blame] | 740 | 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] | 741 | prefix, suffix, i, |
| 742 | snapshot->time, |
| 743 | snapshot->heap_szB, |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 744 | snapshot->heap_extra_szB, |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 745 | snapshot->stacks_szB |
| 746 | ); |
| 747 | } |
| 748 | |
| 749 | // Cull half the snapshots; we choose those that represent the smallest |
| 750 | // time-spans, because that gives us the most even distribution of snapshots |
| 751 | // over time. (It's possible to lose interesting spikes, however.) |
| 752 | // |
| 753 | // Algorithm for N snapshots: We find the snapshot representing the smallest |
| 754 | // timeframe, and remove it. We repeat this until (N/2) snapshots are gone. |
| 755 | // We have to do this one snapshot at a time, rather than finding the (N/2) |
| 756 | // smallest snapshots in one hit, because when a snapshot is removed, its |
| 757 | // neighbours immediately cover greater timespans. So it's O(N^2), but N is |
| 758 | // small, and it's not done very often. |
| 759 | // |
| 760 | // Once we're done, we return the new smallest interval between snapshots. |
| 761 | // That becomes our minimum time interval. |
| 762 | static UInt cull_snapshots(void) |
| 763 | { |
| 764 | Int i, jp, j, jn, min_timespan_i; |
| 765 | Int n_deleted = 0; |
| 766 | Time min_timespan; |
| 767 | |
| 768 | n_cullings++; |
| 769 | |
| 770 | // Sets j to the index of the first not-yet-removed snapshot at or after i |
| 771 | #define FIND_SNAPSHOT(i, j) \ |
| 772 | for (j = i; \ |
| 773 | j < clo_max_snapshots && !is_snapshot_in_use(&snapshots[j]); \ |
| 774 | j++) { } |
| 775 | |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 776 | VERB(2, "Culling...\n"); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 777 | |
| 778 | // First we remove enough snapshots by clearing them in-place. Once |
| 779 | // that's done, we can slide the remaining ones down. |
| 780 | for (i = 0; i < clo_max_snapshots/2; i++) { |
| 781 | // Find the snapshot representing the smallest timespan. The timespan |
| 782 | // for snapshot n = d(N-1,N)+d(N,N+1), where d(A,B) is the time between |
| 783 | // snapshot A and B. We don't consider the first and last snapshots for |
| 784 | // removal. |
| 785 | Snapshot* min_snapshot; |
| 786 | Int min_j; |
| 787 | |
| 788 | // Initial triple: (prev, curr, next) == (jp, j, jn) |
| 789 | // Initial min_timespan is the first one. |
| 790 | jp = 0; |
| 791 | FIND_SNAPSHOT(1, j); |
| 792 | FIND_SNAPSHOT(j+1, jn); |
| 793 | min_timespan = 0x7fffffffffffffffLL; |
| 794 | min_j = -1; |
| 795 | while (jn < clo_max_snapshots) { |
| 796 | Time timespan = snapshots[jn].time - snapshots[jp].time; |
| 797 | tl_assert(timespan >= 0); |
| 798 | // Nb: We never cull the peak snapshot. |
| 799 | if (Peak != snapshots[j].kind && timespan < min_timespan) { |
| 800 | min_timespan = timespan; |
| 801 | min_j = j; |
| 802 | } |
| 803 | // Move on to next triple |
| 804 | jp = j; |
| 805 | j = jn; |
| 806 | FIND_SNAPSHOT(jn+1, jn); |
| 807 | } |
| 808 | // We've found the least important snapshot, now delete it. First |
| 809 | // print it if necessary. |
| 810 | tl_assert(-1 != min_j); // Check we found a minimum. |
| 811 | min_snapshot = & snapshots[ min_j ]; |
| 812 | if (VG_(clo_verbosity) > 1) { |
florian | ef901ff | 2014-11-16 09:56:42 +0000 | [diff] [blame] | 813 | HChar buf[64]; // large enough |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 814 | VG_(snprintf)(buf, 64, " %3d (t-span = %lld)", i, min_timespan); |
| 815 | VERB_snapshot(2, buf, min_j); |
| 816 | } |
| 817 | delete_snapshot(min_snapshot); |
| 818 | n_deleted++; |
| 819 | } |
| 820 | |
| 821 | // Slide down the remaining snapshots over the removed ones. First set i |
| 822 | // to point to the first empty slot, and j to the first full slot after |
| 823 | // i. Then slide everything down. |
| 824 | for (i = 0; is_snapshot_in_use( &snapshots[i] ); i++) { } |
| 825 | for (j = i; !is_snapshot_in_use( &snapshots[j] ); j++) { } |
| 826 | for ( ; j < clo_max_snapshots; j++) { |
| 827 | if (is_snapshot_in_use( &snapshots[j] )) { |
| 828 | snapshots[i++] = snapshots[j]; |
| 829 | clear_snapshot(&snapshots[j], /*do_sanity_check*/True); |
| 830 | } |
| 831 | } |
| 832 | next_snapshot_i = i; |
| 833 | |
| 834 | // Check snapshots array looks ok after changes. |
| 835 | sanity_check_snapshots_array(); |
| 836 | |
| 837 | // Find the minimum timespan remaining; that will be our new minimum |
| 838 | // time interval. Note that above we were finding timespans by measuring |
| 839 | // two intervals around a snapshot that was under consideration for |
| 840 | // deletion. Here we only measure single intervals because all the |
| 841 | // deletions have occurred. |
| 842 | // |
| 843 | // But we have to be careful -- some snapshots (eg. snapshot 0, and the |
| 844 | // peak snapshot) are uncullable. If two uncullable snapshots end up |
| 845 | // next to each other, they'll never be culled (assuming the peak doesn't |
| 846 | // change), and the time gap between them will not change. However, the |
| 847 | // time between the remaining cullable snapshots will grow ever larger. |
| 848 | // This means that the min_timespan found will always be that between the |
| 849 | // two uncullable snapshots, and it will be much smaller than it should |
| 850 | // be. To avoid this problem, when computing the minimum timespan, we |
| 851 | // ignore any timespans between two uncullable snapshots. |
| 852 | tl_assert(next_snapshot_i > 1); |
| 853 | min_timespan = 0x7fffffffffffffffLL; |
| 854 | min_timespan_i = -1; |
| 855 | for (i = 1; i < next_snapshot_i; i++) { |
| 856 | if (is_uncullable_snapshot(&snapshots[i]) && |
| 857 | is_uncullable_snapshot(&snapshots[i-1])) |
| 858 | { |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 859 | VERB(2, "(Ignoring interval %d--%d when computing minimum)\n", i-1, i); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 860 | } else { |
| 861 | Time timespan = snapshots[i].time - snapshots[i-1].time; |
| 862 | tl_assert(timespan >= 0); |
| 863 | if (timespan < min_timespan) { |
| 864 | min_timespan = timespan; |
| 865 | min_timespan_i = i; |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | tl_assert(-1 != min_timespan_i); // Check we found a minimum. |
| 870 | |
| 871 | // Print remaining snapshots, if necessary. |
| 872 | if (VG_(clo_verbosity) > 1) { |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 873 | VERB(2, "Finished culling (%3d of %3d deleted)\n", |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 874 | n_deleted, clo_max_snapshots); |
| 875 | for (i = 0; i < next_snapshot_i; i++) { |
| 876 | VERB_snapshot(2, " post-cull", i); |
| 877 | } |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 878 | VERB(2, "New time interval = %lld (between snapshots %d and %d)\n", |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 879 | min_timespan, min_timespan_i-1, min_timespan_i); |
| 880 | } |
| 881 | |
| 882 | return min_timespan; |
| 883 | } |
| 884 | |
| 885 | static Time get_time(void) |
| 886 | { |
| 887 | // Get current time, in whatever time unit we're using. |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 888 | if (clo_time_unit == TimeI) { |
| 889 | return guest_instrs_executed; |
| 890 | } else if (clo_time_unit == TimeMS) { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 891 | // Some stuff happens between the millisecond timer being initialised |
| 892 | // to zero and us taking our first snapshot. We determine that time |
| 893 | // gap so we can subtract it from all subsequent times so that our |
| 894 | // first snapshot is considered to be at t = 0ms. Unfortunately, a |
| 895 | // bunch of symbols get read after the first snapshot is taken but |
| 896 | // before the second one (which is triggered by the first allocation), |
| 897 | // so when the time-unit is 'ms' we always have a big gap between the |
| 898 | // first two snapshots. But at least users won't have to wonder why |
| 899 | // the first snapshot isn't at t=0. |
| 900 | static Bool is_first_get_time = True; |
| 901 | static Time start_time_ms; |
| 902 | if (is_first_get_time) { |
| 903 | start_time_ms = VG_(read_millisecond_timer)(); |
| 904 | is_first_get_time = False; |
| 905 | return 0; |
| 906 | } else { |
| 907 | return VG_(read_millisecond_timer)() - start_time_ms; |
| 908 | } |
| 909 | } else if (clo_time_unit == TimeB) { |
| 910 | return total_allocs_deallocs_szB; |
| 911 | } else { |
| 912 | tl_assert2(0, "bad --time-unit value"); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | // Take a snapshot, and only that -- decisions on whether to take a |
| 917 | // snapshot, or what kind of snapshot, are made elsewhere. |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 918 | // Nb: we call the arg "my_time" because "time" shadows a global declaration |
| 919 | // in /usr/include/time.h on Darwin. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 920 | static void |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 921 | take_snapshot(Snapshot* snapshot, SnapshotKind kind, Time my_time, |
njn | efc13c2 | 2009-02-23 06:44:51 +0000 | [diff] [blame] | 922 | Bool is_detailed) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 923 | { |
| 924 | tl_assert(!is_snapshot_in_use(snapshot)); |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 925 | if (!clo_pages_as_heap) { |
| 926 | tl_assert(have_started_executing_code); |
| 927 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 928 | |
| 929 | // Heap and heap admin. |
| 930 | if (clo_heap) { |
| 931 | snapshot->heap_szB = heap_szB; |
| 932 | if (is_detailed) { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 933 | snapshot->xt = VG_(XT_snapshot)(heap_xt); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 934 | } |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 935 | snapshot->heap_extra_szB = heap_extra_szB; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | // Stack(s). |
| 939 | if (clo_stacks) { |
| 940 | snapshot->stacks_szB = stacks_szB; |
| 941 | } |
| 942 | |
| 943 | // Rest of snapshot. |
| 944 | snapshot->kind = kind; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 945 | snapshot->time = my_time; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 946 | sanity_check_snapshot(snapshot); |
| 947 | |
| 948 | // Update stats. |
| 949 | if (Peak == kind) n_peak_snapshots++; |
| 950 | if (is_detailed) n_detailed_snapshots++; |
| 951 | n_real_snapshots++; |
| 952 | } |
| 953 | |
| 954 | |
| 955 | // Take a snapshot, if it's time, or if we've hit a peak. |
| 956 | static void |
florian | e58e8a7 | 2012-10-20 19:57:16 +0000 | [diff] [blame] | 957 | maybe_take_snapshot(SnapshotKind kind, const HChar* what) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 958 | { |
| 959 | // 'min_time_interval' is the minimum time interval between snapshots. |
| 960 | // If we try to take a snapshot and less than this much time has passed, |
| 961 | // we don't take it. It gets larger as the program runs longer. It's |
| 962 | // initialised to zero so that we begin by taking snapshots as quickly as |
| 963 | // possible. |
| 964 | static Time min_time_interval = 0; |
| 965 | // Zero allows startup snapshot. |
| 966 | static Time earliest_possible_time_of_next_snapshot = 0; |
| 967 | static Int n_snapshots_since_last_detailed = 0; |
| 968 | static Int n_skipped_snapshots_since_last_snapshot = 0; |
| 969 | |
| 970 | Snapshot* snapshot; |
| 971 | Bool is_detailed; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 972 | // Nb: we call this variable "my_time" because "time" shadows a global |
| 973 | // declaration in /usr/include/time.h on Darwin. |
| 974 | Time my_time = get_time(); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 975 | |
| 976 | switch (kind) { |
| 977 | case Normal: |
| 978 | // Only do a snapshot if it's time. |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 979 | if (my_time < earliest_possible_time_of_next_snapshot) { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 980 | n_skipped_snapshots++; |
| 981 | n_skipped_snapshots_since_last_snapshot++; |
| 982 | return; |
| 983 | } |
| 984 | is_detailed = (clo_detailed_freq-1 == n_snapshots_since_last_detailed); |
| 985 | break; |
| 986 | |
| 987 | case Peak: { |
| 988 | // Because we're about to do a deallocation, we're coming down from a |
| 989 | // local peak. If it is (a) actually a global peak, and (b) a certain |
| 990 | // amount bigger than the previous peak, then we take a peak snapshot. |
| 991 | // By not taking a snapshot for every peak, we save a lot of effort -- |
| 992 | // because many peaks remain peak only for a short time. |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 993 | SizeT total_szB = heap_szB + heap_extra_szB + stacks_szB; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 994 | SizeT excess_szB_for_new_peak = |
njn | 62721e9 | 2007-11-11 22:15:58 +0000 | [diff] [blame] | 995 | (SizeT)((peak_snapshot_total_szB * clo_peak_inaccuracy) / 100); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 996 | if (total_szB <= peak_snapshot_total_szB + excess_szB_for_new_peak) { |
| 997 | return; |
| 998 | } |
| 999 | is_detailed = True; |
| 1000 | break; |
| 1001 | } |
| 1002 | |
| 1003 | default: |
| 1004 | tl_assert2(0, "maybe_take_snapshot: unrecognised snapshot kind"); |
| 1005 | } |
| 1006 | |
| 1007 | // Take the snapshot. |
| 1008 | snapshot = & snapshots[next_snapshot_i]; |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1009 | take_snapshot(snapshot, kind, my_time, is_detailed); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1010 | |
| 1011 | // Record if it was detailed. |
| 1012 | if (is_detailed) { |
| 1013 | n_snapshots_since_last_detailed = 0; |
| 1014 | } else { |
| 1015 | n_snapshots_since_last_detailed++; |
| 1016 | } |
| 1017 | |
| 1018 | // Update peak data, if it's a Peak snapshot. |
| 1019 | if (Peak == kind) { |
| 1020 | Int i, number_of_peaks_snapshots_found = 0; |
| 1021 | |
| 1022 | // Sanity check the size, then update our recorded peak. |
| 1023 | SizeT snapshot_total_szB = |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1024 | snapshot->heap_szB + snapshot->heap_extra_szB + snapshot->stacks_szB; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1025 | tl_assert2(snapshot_total_szB > peak_snapshot_total_szB, |
| 1026 | "%ld, %ld\n", snapshot_total_szB, peak_snapshot_total_szB); |
| 1027 | peak_snapshot_total_szB = snapshot_total_szB; |
| 1028 | |
| 1029 | // Find the old peak snapshot, if it exists, and mark it as normal. |
| 1030 | for (i = 0; i < next_snapshot_i; i++) { |
| 1031 | if (Peak == snapshots[i].kind) { |
| 1032 | snapshots[i].kind = Normal; |
| 1033 | number_of_peaks_snapshots_found++; |
| 1034 | } |
| 1035 | } |
| 1036 | tl_assert(number_of_peaks_snapshots_found <= 1); |
| 1037 | } |
| 1038 | |
| 1039 | // Finish up verbosity and stats stuff. |
| 1040 | if (n_skipped_snapshots_since_last_snapshot > 0) { |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1041 | VERB(2, " (skipped %d snapshot%s)\n", |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1042 | n_skipped_snapshots_since_last_snapshot, |
njn | 84f32b2 | 2009-02-10 07:14:37 +0000 | [diff] [blame] | 1043 | ( 1 == n_skipped_snapshots_since_last_snapshot ? "" : "s") ); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1044 | } |
| 1045 | VERB_snapshot(2, what, next_snapshot_i); |
| 1046 | n_skipped_snapshots_since_last_snapshot = 0; |
| 1047 | |
| 1048 | // Cull the entries, if our snapshot table is full. |
| 1049 | next_snapshot_i++; |
| 1050 | if (clo_max_snapshots == next_snapshot_i) { |
| 1051 | min_time_interval = cull_snapshots(); |
| 1052 | } |
| 1053 | |
| 1054 | // Work out the earliest time when the next snapshot can happen. |
njn | f76d27a | 2009-05-28 01:53:07 +0000 | [diff] [blame] | 1055 | earliest_possible_time_of_next_snapshot = my_time + min_time_interval; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
| 1058 | |
| 1059 | //------------------------------------------------------------// |
| 1060 | //--- Sanity checking ---// |
| 1061 | //------------------------------------------------------------// |
| 1062 | |
| 1063 | static Bool ms_cheap_sanity_check ( void ) |
| 1064 | { |
| 1065 | return True; // Nothing useful we can cheaply check. |
| 1066 | } |
| 1067 | |
| 1068 | static Bool ms_expensive_sanity_check ( void ) |
| 1069 | { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1070 | tl_assert(heap_xt); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1071 | sanity_check_snapshots_array(); |
| 1072 | return True; |
| 1073 | } |
| 1074 | |
| 1075 | |
| 1076 | //------------------------------------------------------------// |
| 1077 | //--- Heap management ---// |
| 1078 | //------------------------------------------------------------// |
| 1079 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1080 | // Metadata for heap blocks. Each one contains an Xecu, |
| 1081 | // which identifies the XTree ec at which it was allocated. From |
| 1082 | // HP_Chunks, XTree ec 'space' field is incremented (at allocation) and |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1083 | // decremented (at deallocation). |
| 1084 | // |
| 1085 | // Nb: first two fields must match core's VgHashNode. |
| 1086 | typedef |
| 1087 | struct _HP_Chunk { |
| 1088 | struct _HP_Chunk* next; |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1089 | Addr data; // Ptr to actual block |
| 1090 | SizeT req_szB; // Size requested |
| 1091 | SizeT slop_szB; // Extra bytes given above those requested |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1092 | Xecu where; // Where allocated; XTree xecu from heap_xt |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1093 | } |
| 1094 | HP_Chunk; |
| 1095 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1096 | /* Pool allocator for HP_Chunk. */ |
| 1097 | static PoolAlloc *HP_chunk_poolalloc = NULL; |
| 1098 | |
florian | 09a4c79 | 2014-10-18 10:58:05 +0000 | [diff] [blame] | 1099 | static VgHashTable *malloc_list = NULL; // HP_Chunks |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1100 | |
| 1101 | static void update_alloc_stats(SSizeT szB_delta) |
| 1102 | { |
| 1103 | // Update total_allocs_deallocs_szB. |
| 1104 | if (szB_delta < 0) szB_delta = -szB_delta; |
| 1105 | total_allocs_deallocs_szB += szB_delta; |
| 1106 | } |
| 1107 | |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1108 | 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] | 1109 | { |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1110 | if (heap_szB_delta < 0) |
| 1111 | tl_assert(heap_szB >= -heap_szB_delta); |
| 1112 | if (heap_extra_szB_delta < 0) |
| 1113 | tl_assert(heap_extra_szB >= -heap_extra_szB_delta); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1114 | |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1115 | heap_extra_szB += heap_extra_szB_delta; |
| 1116 | heap_szB += heap_szB_delta; |
| 1117 | |
| 1118 | update_alloc_stats(heap_szB_delta + heap_extra_szB_delta); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1119 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1120 | |
nethercote | 159dfef | 2004-09-13 13:27:30 +0000 | [diff] [blame] | 1121 | static |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1122 | void* record_block( ThreadId tid, void* p, SizeT req_szB, SizeT slop_szB, |
| 1123 | Bool exclude_first_entry, Bool maybe_snapshot ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1124 | { |
njn | f1c5def | 2005-08-11 02:17:07 +0000 | [diff] [blame] | 1125 | // Make new HP_Chunk node, add to malloc_list |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1126 | HP_Chunk* hc = VG_(allocEltPA)(HP_chunk_poolalloc); |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1127 | hc->req_szB = req_szB; |
| 1128 | hc->slop_szB = slop_szB; |
| 1129 | hc->data = (Addr)p; |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1130 | hc->where = 0; |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 1131 | VG_(HT_add_node)(malloc_list, hc); |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 1132 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1133 | if (clo_heap) { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1134 | VERB(3, "<<< record_block (%lu, %lu)\n", req_szB, slop_szB); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1135 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1136 | hc->where = add_heap_xt( tid, req_szB, exclude_first_entry); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1137 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1138 | if (VG_(XT_n_ips_sel)(heap_xt, hc->where) > 0) { |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1139 | // Update statistics. |
| 1140 | n_heap_allocs++; |
| 1141 | |
| 1142 | // Update heap stats. |
| 1143 | update_heap_stats(req_szB, clo_heap_admin + slop_szB); |
| 1144 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1145 | // Maybe take a snapshot. |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1146 | if (maybe_snapshot) { |
| 1147 | maybe_take_snapshot(Normal, " alloc"); |
| 1148 | } |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1149 | |
| 1150 | } else { |
| 1151 | // Ignored allocation. |
| 1152 | n_ignored_heap_allocs++; |
| 1153 | |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1154 | VERB(3, "(ignored)\n"); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1155 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1156 | |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1157 | VERB(3, ">>>\n"); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1158 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1159 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1160 | return p; |
| 1161 | } |
| 1162 | |
| 1163 | static __inline__ |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1164 | void* alloc_and_record_block ( ThreadId tid, SizeT req_szB, SizeT req_alignB, |
| 1165 | Bool is_zeroed ) |
| 1166 | { |
| 1167 | SizeT actual_szB, slop_szB; |
| 1168 | void* p; |
| 1169 | |
| 1170 | if ((SSizeT)req_szB < 0) return NULL; |
| 1171 | |
| 1172 | // Allocate and zero if necessary. |
| 1173 | p = VG_(cli_malloc)( req_alignB, req_szB ); |
| 1174 | if (!p) { |
| 1175 | return NULL; |
| 1176 | } |
| 1177 | if (is_zeroed) VG_(memset)(p, 0, req_szB); |
florian | e464e80 | 2014-09-11 20:15:23 +0000 | [diff] [blame] | 1178 | actual_szB = VG_(cli_malloc_usable_size)(p); |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1179 | tl_assert(actual_szB >= req_szB); |
| 1180 | slop_szB = actual_szB - req_szB; |
| 1181 | |
| 1182 | // Record block. |
| 1183 | record_block(tid, p, req_szB, slop_szB, /*exclude_first_entry*/True, |
| 1184 | /*maybe_snapshot*/True); |
| 1185 | |
| 1186 | return p; |
| 1187 | } |
| 1188 | |
| 1189 | static __inline__ |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1190 | void unrecord_block ( void* p, Bool maybe_snapshot, Bool exclude_first_entry ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1191 | { |
njn | f1c5def | 2005-08-11 02:17:07 +0000 | [diff] [blame] | 1192 | // Remove HP_Chunk from malloc_list |
njn | 7081c1a | 2009-05-19 01:53:30 +0000 | [diff] [blame] | 1193 | HP_Chunk* hc = VG_(HT_remove)(malloc_list, (UWord)p); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1194 | if (NULL == hc) { |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 1195 | return; // must have been a bogus free() |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1196 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1197 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1198 | if (clo_heap) { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1199 | VERB(3, "<<< unrecord_block\n"); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1200 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1201 | if (VG_(XT_n_ips_sel)(heap_xt, hc->where) > 0) { |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1202 | // Update statistics. |
| 1203 | n_heap_frees++; |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 1204 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1205 | // Maybe take a peak snapshot, since it's a deallocation. |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1206 | if (maybe_snapshot) { |
| 1207 | maybe_take_snapshot(Peak, "de-PEAK"); |
| 1208 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1209 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1210 | // Update heap stats. |
| 1211 | update_heap_stats(-hc->req_szB, -clo_heap_admin - hc->slop_szB); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1212 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1213 | // Update XTree. |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1214 | sub_heap_xt(hc->where, hc->req_szB, exclude_first_entry); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1215 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1216 | // Maybe take a snapshot. |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1217 | if (maybe_snapshot) { |
| 1218 | maybe_take_snapshot(Normal, "dealloc"); |
| 1219 | } |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1220 | |
| 1221 | } else { |
| 1222 | n_ignored_heap_frees++; |
| 1223 | |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1224 | VERB(3, "(ignored)\n"); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1225 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1226 | |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1227 | VERB(3, ">>> (-%lu, -%lu)\n", hc->req_szB, hc->slop_szB); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | // Actually free the chunk, and the heap block (if necessary) |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1231 | VG_(freeEltPA) (HP_chunk_poolalloc, hc); hc = NULL; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1234 | // Nb: --ignore-fn is tricky for realloc. If the block's original alloc was |
| 1235 | // ignored, but the realloc is not requested to be ignored, and we are |
| 1236 | // shrinking the block, then we have to ignore the realloc -- otherwise we |
| 1237 | // could end up with negative heap sizes. This isn't a danger if we are |
| 1238 | // growing such a block, but for consistency (it also simplifies things) we |
| 1239 | // ignore such reallocs as well. |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1240 | // PW Nov 2016 xtree work: why can't we just consider that a realloc of an |
| 1241 | // ignored alloc is just a new alloc (i.e. do not remove the old sz from the |
| 1242 | // stats). Then everything would be fine, and a non ignored realloc would be |
| 1243 | // counted properly. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1244 | static __inline__ |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1245 | void* realloc_block ( ThreadId tid, void* p_old, SizeT new_req_szB ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1246 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1247 | HP_Chunk* hc; |
| 1248 | void* p_new; |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1249 | SizeT old_req_szB, old_slop_szB, new_slop_szB, new_actual_szB; |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1250 | Xecu old_where; |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1251 | Bool is_ignored = False; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1252 | |
| 1253 | // Remove the old block |
| 1254 | hc = VG_(HT_remove)(malloc_list, (UWord)p_old); |
| 1255 | if (hc == NULL) { |
| 1256 | return NULL; // must have been a bogus realloc() |
| 1257 | } |
| 1258 | |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1259 | old_req_szB = hc->req_szB; |
| 1260 | old_slop_szB = hc->slop_szB; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1261 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1262 | 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] | 1263 | if (clo_heap) { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1264 | VERB(3, "<<< realloc_block (%lu)\n", new_req_szB); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1265 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1266 | if (VG_(XT_n_ips_sel)(heap_xt, hc->where) > 0) { |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1267 | // Update statistics. |
| 1268 | n_heap_reallocs++; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1269 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1270 | // Maybe take a peak snapshot, if it's (effectively) a deallocation. |
| 1271 | if (new_req_szB < old_req_szB) { |
| 1272 | maybe_take_snapshot(Peak, "re-PEAK"); |
| 1273 | } |
| 1274 | } else { |
| 1275 | // The original malloc was ignored, so we have to ignore the |
| 1276 | // realloc as well. |
| 1277 | is_ignored = True; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1278 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | // Actually do the allocation, if necessary. |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1282 | if (new_req_szB <= old_req_szB + old_slop_szB) { |
| 1283 | // New size is smaller or same; block not moved. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1284 | p_new = p_old; |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1285 | new_slop_szB = old_slop_szB + (old_req_szB - new_req_szB); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1286 | |
| 1287 | } else { |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1288 | // New size is bigger; make new block, copy shared contents, free old. |
| 1289 | p_new = VG_(cli_malloc)(VG_(clo_alignment), new_req_szB); |
| 1290 | if (!p_new) { |
| 1291 | // Nb: if realloc fails, NULL is returned but the old block is not |
| 1292 | // touched. What an awful function. |
| 1293 | return NULL; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1294 | } |
njn | dfa408c | 2011-08-08 01:58:50 +0000 | [diff] [blame] | 1295 | VG_(memcpy)(p_new, p_old, old_req_szB + old_slop_szB); |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1296 | VG_(cli_free)(p_old); |
florian | e464e80 | 2014-09-11 20:15:23 +0000 | [diff] [blame] | 1297 | new_actual_szB = VG_(cli_malloc_usable_size)(p_new); |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1298 | tl_assert(new_actual_szB >= new_req_szB); |
| 1299 | new_slop_szB = new_actual_szB - new_req_szB; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | if (p_new) { |
| 1303 | // Update HP_Chunk. |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1304 | hc->data = (Addr)p_new; |
| 1305 | hc->req_szB = new_req_szB; |
| 1306 | hc->slop_szB = new_slop_szB; |
| 1307 | old_where = hc->where; |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1308 | hc->where = 0; |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1309 | |
| 1310 | // Update XTree. |
| 1311 | if (clo_heap) { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1312 | hc->where = add_heap_xt( tid, new_req_szB, |
| 1313 | /*exclude_first_entry*/True); |
| 1314 | if (!is_ignored && VG_(XT_n_ips_sel)(heap_xt, hc->where) > 0) { |
| 1315 | sub_heap_xt(old_where, old_req_szB, /*exclude_first_entry*/True); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1316 | } else { |
| 1317 | // The realloc itself is ignored. |
| 1318 | is_ignored = True; |
| 1319 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1320 | /* XTREE??? hack to have something compatible with pre |
| 1321 | m_xtree massif: if the previous alloc/realloc was |
| 1322 | ignored, and this one is not ignored, then keep the |
| 1323 | previous where, to continue marking this memory as |
| 1324 | ignored. */ |
| 1325 | if (VG_(XT_n_ips_sel)(heap_xt, hc->where) > 0 |
| 1326 | && VG_(XT_n_ips_sel)(heap_xt, old_where) == 0) |
| 1327 | hc->where = old_where; |
| 1328 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1329 | // Update statistics. |
| 1330 | n_ignored_heap_reallocs++; |
| 1331 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | // Now insert the new hc (with a possibly new 'data' field) into |
| 1336 | // malloc_list. If this realloc() did not increase the memory size, we |
| 1337 | // will have removed and then re-added hc unnecessarily. But that's ok |
| 1338 | // because shrinking a block with realloc() is (presumably) much rarer |
| 1339 | // than growing it, and this way simplifies the growing case. |
| 1340 | VG_(HT_add_node)(malloc_list, hc); |
| 1341 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1342 | if (clo_heap) { |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1343 | if (!is_ignored) { |
| 1344 | // Update heap stats. |
| 1345 | update_heap_stats(new_req_szB - old_req_szB, |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1346 | new_slop_szB - old_slop_szB); |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1347 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1348 | // Maybe take a snapshot. |
| 1349 | maybe_take_snapshot(Normal, "realloc"); |
| 1350 | } else { |
| 1351 | |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1352 | VERB(3, "(ignored)\n"); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 1353 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1354 | |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1355 | VERB(3, ">>> (%ld, %ld)\n", |
florian | 16eef85 | 2015-08-03 21:05:20 +0000 | [diff] [blame] | 1356 | (SSizeT)(new_req_szB - old_req_szB), |
| 1357 | (SSizeT)(new_slop_szB - old_slop_szB)); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | return p_new; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1363 | |
| 1364 | //------------------------------------------------------------// |
| 1365 | //--- malloc() et al replacement wrappers ---// |
| 1366 | //------------------------------------------------------------// |
| 1367 | |
| 1368 | static void* ms_malloc ( ThreadId tid, SizeT szB ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1369 | { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1370 | return alloc_and_record_block( tid, szB, VG_(clo_alignment), /*is_zeroed*/False ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1373 | static void* ms___builtin_new ( ThreadId tid, SizeT szB ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1374 | { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1375 | return alloc_and_record_block( tid, szB, VG_(clo_alignment), /*is_zeroed*/False ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1378 | static void* ms___builtin_vec_new ( ThreadId tid, SizeT szB ) |
fitzhardinge | 51f3ff1 | 2004-03-04 22:42:03 +0000 | [diff] [blame] | 1379 | { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1380 | return alloc_and_record_block( tid, szB, VG_(clo_alignment), /*is_zeroed*/False ); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | static void* ms_calloc ( ThreadId tid, SizeT m, SizeT szB ) |
| 1384 | { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1385 | 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] | 1386 | } |
| 1387 | |
| 1388 | static void *ms_memalign ( ThreadId tid, SizeT alignB, SizeT szB ) |
| 1389 | { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1390 | return alloc_and_record_block( tid, szB, alignB, False ); |
fitzhardinge | 51f3ff1 | 2004-03-04 22:42:03 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
njn | efc13c2 | 2009-02-23 06:44:51 +0000 | [diff] [blame] | 1393 | static void ms_free ( ThreadId tid __attribute__((unused)), void* p ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1394 | { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1395 | unrecord_block(p, /*maybe_snapshot*/True, /*exclude_first_entry*/True); |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1396 | VG_(cli_free)(p); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1399 | static void ms___builtin_delete ( ThreadId tid, void* p ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1400 | { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1401 | unrecord_block(p, /*maybe_snapshot*/True, /*exclude_first_entry*/True); |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1402 | VG_(cli_free)(p); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1405 | static void ms___builtin_vec_delete ( ThreadId tid, void* p ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1406 | { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1407 | unrecord_block(p, /*maybe_snapshot*/True, /*exclude_first_entry*/True); |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1408 | VG_(cli_free)(p); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1411 | static void* ms_realloc ( ThreadId tid, void* p_old, SizeT new_szB ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1412 | { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1413 | return realloc_block(tid, p_old, new_szB); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
njn | efc13c2 | 2009-02-23 06:44:51 +0000 | [diff] [blame] | 1416 | static SizeT ms_malloc_usable_size ( ThreadId tid, void* p ) |
njn | 8b140de | 2009-02-17 04:31:18 +0000 | [diff] [blame] | 1417 | { |
| 1418 | HP_Chunk* hc = VG_(HT_lookup)( malloc_list, (UWord)p ); |
| 1419 | |
| 1420 | return ( hc ? hc->req_szB + hc->slop_szB : 0 ); |
| 1421 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1422 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1423 | //------------------------------------------------------------// |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1424 | //--- Page handling ---// |
| 1425 | //------------------------------------------------------------// |
| 1426 | |
| 1427 | static |
| 1428 | void ms_record_page_mem ( Addr a, SizeT len ) |
| 1429 | { |
| 1430 | ThreadId tid = VG_(get_running_tid)(); |
| 1431 | Addr end; |
| 1432 | tl_assert(VG_IS_PAGE_ALIGNED(len)); |
| 1433 | tl_assert(len >= VKI_PAGE_SIZE); |
| 1434 | // Record the first N-1 pages as blocks, but don't do any snapshots. |
| 1435 | for (end = a + len - VKI_PAGE_SIZE; a < end; a += VKI_PAGE_SIZE) { |
| 1436 | record_block( tid, (void*)a, VKI_PAGE_SIZE, /*slop_szB*/0, |
| 1437 | /*exclude_first_entry*/False, /*maybe_snapshot*/False ); |
| 1438 | } |
| 1439 | // Record the last page as a block, and maybe do a snapshot afterwards. |
| 1440 | record_block( tid, (void*)a, VKI_PAGE_SIZE, /*slop_szB*/0, |
| 1441 | /*exclude_first_entry*/False, /*maybe_snapshot*/True ); |
| 1442 | } |
| 1443 | |
| 1444 | static |
| 1445 | void ms_unrecord_page_mem( Addr a, SizeT len ) |
| 1446 | { |
| 1447 | Addr end; |
| 1448 | tl_assert(VG_IS_PAGE_ALIGNED(len)); |
| 1449 | tl_assert(len >= VKI_PAGE_SIZE); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 1450 | // Unrecord the first page. This might be the peak, so do a snapshot. |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1451 | unrecord_block((void*)a, /*maybe_snapshot*/True, |
| 1452 | /*exclude_first_entry*/False); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 1453 | a += VKI_PAGE_SIZE; |
| 1454 | // Then unrecord the remaining pages, but without snapshots. |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1455 | for (end = a + len - VKI_PAGE_SIZE; a < end; a += VKI_PAGE_SIZE) { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1456 | unrecord_block((void*)a, /*maybe_snapshot*/False, |
| 1457 | /*exclude_first_entry*/False); |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1458 | } |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | //------------------------------------------------------------// |
| 1462 | |
| 1463 | static |
| 1464 | void ms_new_mem_mmap ( Addr a, SizeT len, |
| 1465 | Bool rr, Bool ww, Bool xx, ULong di_handle ) |
| 1466 | { |
| 1467 | tl_assert(VG_IS_PAGE_ALIGNED(len)); |
| 1468 | ms_record_page_mem(a, len); |
| 1469 | } |
| 1470 | |
| 1471 | static |
| 1472 | void ms_new_mem_startup( Addr a, SizeT len, |
| 1473 | Bool rr, Bool ww, Bool xx, ULong di_handle ) |
| 1474 | { |
| 1475 | // startup maps are always be page-sized, except the trampoline page is |
| 1476 | // marked by the core as only being the size of the trampoline itself, |
| 1477 | // which is something like 57 bytes. Round it up to page size. |
| 1478 | len = VG_PGROUNDUP(len); |
| 1479 | ms_record_page_mem(a, len); |
| 1480 | } |
| 1481 | |
| 1482 | static |
| 1483 | void ms_new_mem_brk ( Addr a, SizeT len, ThreadId tid ) |
| 1484 | { |
philippe | e6a26cc | 2012-05-01 20:02:30 +0000 | [diff] [blame] | 1485 | // brk limit is not necessarily aligned on a page boundary. |
| 1486 | // If new memory being brk-ed implies to allocate a new page, |
| 1487 | // then call ms_record_page_mem with page aligned parameters |
| 1488 | // otherwise just ignore. |
| 1489 | Addr old_bottom_page = VG_PGROUNDDN(a - 1); |
| 1490 | Addr new_top_page = VG_PGROUNDDN(a + len - 1); |
| 1491 | if (old_bottom_page != new_top_page) |
| 1492 | ms_record_page_mem(VG_PGROUNDDN(a), |
| 1493 | (new_top_page - old_bottom_page)); |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | static |
| 1497 | void ms_copy_mem_remap( Addr from, Addr to, SizeT len) |
| 1498 | { |
| 1499 | tl_assert(VG_IS_PAGE_ALIGNED(len)); |
| 1500 | ms_unrecord_page_mem(from, len); |
| 1501 | ms_record_page_mem(to, len); |
| 1502 | } |
| 1503 | |
| 1504 | static |
| 1505 | void ms_die_mem_munmap( Addr a, SizeT len ) |
| 1506 | { |
| 1507 | tl_assert(VG_IS_PAGE_ALIGNED(len)); |
| 1508 | ms_unrecord_page_mem(a, len); |
| 1509 | } |
| 1510 | |
| 1511 | static |
| 1512 | void ms_die_mem_brk( Addr a, SizeT len ) |
| 1513 | { |
philippe | e6a26cc | 2012-05-01 20:02:30 +0000 | [diff] [blame] | 1514 | // Call ms_unrecord_page_mem only if one or more pages are de-allocated. |
| 1515 | // See ms_new_mem_brk for more details. |
| 1516 | Addr new_bottom_page = VG_PGROUNDDN(a - 1); |
| 1517 | Addr old_top_page = VG_PGROUNDDN(a + len - 1); |
| 1518 | if (old_top_page != new_bottom_page) |
| 1519 | ms_unrecord_page_mem(VG_PGROUNDDN(a), |
| 1520 | (old_top_page - new_bottom_page)); |
| 1521 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | //------------------------------------------------------------// |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1525 | //--- Stacks ---// |
| 1526 | //------------------------------------------------------------// |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1527 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1528 | // We really want the inlining to occur... |
| 1529 | #define INLINE inline __attribute__((always_inline)) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1530 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1531 | static void update_stack_stats(SSizeT stack_szB_delta) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1532 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1533 | if (stack_szB_delta < 0) tl_assert(stacks_szB >= -stack_szB_delta); |
| 1534 | stacks_szB += stack_szB_delta; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1535 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1536 | update_alloc_stats(stack_szB_delta); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
florian | e58e8a7 | 2012-10-20 19:57:16 +0000 | [diff] [blame] | 1539 | static INLINE void new_mem_stack_2(SizeT len, const HChar* what) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1540 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1541 | if (have_started_executing_code) { |
florian | 16eef85 | 2015-08-03 21:05:20 +0000 | [diff] [blame] | 1542 | VERB(3, "<<< new_mem_stack (%lu)\n", len); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1543 | n_stack_allocs++; |
| 1544 | update_stack_stats(len); |
| 1545 | maybe_take_snapshot(Normal, what); |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1546 | VERB(3, ">>>\n"); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1547 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
florian | e58e8a7 | 2012-10-20 19:57:16 +0000 | [diff] [blame] | 1550 | static INLINE void die_mem_stack_2(SizeT len, const HChar* what) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1551 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1552 | if (have_started_executing_code) { |
florian | 16eef85 | 2015-08-03 21:05:20 +0000 | [diff] [blame] | 1553 | VERB(3, "<<< die_mem_stack (-%lu)\n", len); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1554 | n_stack_frees++; |
| 1555 | maybe_take_snapshot(Peak, "stkPEAK"); |
| 1556 | update_stack_stats(-len); |
| 1557 | maybe_take_snapshot(Normal, what); |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 1558 | VERB(3, ">>>\n"); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1559 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1562 | static void new_mem_stack(Addr a, SizeT len) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1563 | { |
njn | efc13c2 | 2009-02-23 06:44:51 +0000 | [diff] [blame] | 1564 | new_mem_stack_2(len, "stk-new"); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1565 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1566 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1567 | static void die_mem_stack(Addr a, SizeT len) |
| 1568 | { |
njn | efc13c2 | 2009-02-23 06:44:51 +0000 | [diff] [blame] | 1569 | die_mem_stack_2(len, "stk-die"); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1570 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1571 | |
sewardj | 7cf4e6b | 2008-05-01 20:24:26 +0000 | [diff] [blame] | 1572 | static void new_mem_stack_signal(Addr a, SizeT len, ThreadId tid) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1573 | { |
njn | efc13c2 | 2009-02-23 06:44:51 +0000 | [diff] [blame] | 1574 | new_mem_stack_2(len, "sig-new"); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1575 | } |
| 1576 | |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 1577 | static void die_mem_stack_signal(Addr a, SizeT len) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1578 | { |
njn | efc13c2 | 2009-02-23 06:44:51 +0000 | [diff] [blame] | 1579 | die_mem_stack_2(len, "sig-die"); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1582 | |
| 1583 | //------------------------------------------------------------// |
| 1584 | //--- Client Requests ---// |
| 1585 | //------------------------------------------------------------// |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1586 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1587 | static void print_monitor_help ( void ) |
| 1588 | { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1589 | VG_(gdb_printf) ( |
| 1590 | "\n" |
| 1591 | "massif monitor commands:\n" |
| 1592 | " snapshot [<filename>]\n" |
| 1593 | " detailed_snapshot [<filename>]\n" |
| 1594 | " takes a snapshot (or a detailed snapshot)\n" |
| 1595 | " and saves it in <filename>\n" |
| 1596 | " default <filename> is massif.vgdb.out\n" |
| 1597 | " all_snapshots [<filename>]\n" |
| 1598 | " saves all snapshot(s) taken so far in <filename>\n" |
| 1599 | " default <filename> is massif.vgdb.out\n" |
| 1600 | " xtmemory [<filename>]\n" |
| 1601 | " dump xtree memory profile in <filename> (default xtmemory.kcg)\n" |
| 1602 | "\n"); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | |
| 1606 | /* Forward declaration. |
| 1607 | return True if request recognised, False otherwise */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1608 | static Bool handle_gdb_monitor_command (ThreadId tid, HChar *req); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1609 | static Bool ms_handle_client_request ( ThreadId tid, UWord* argv, UWord* ret ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1610 | { |
| 1611 | switch (argv[0]) { |
| 1612 | case VG_USERREQ__MALLOCLIKE_BLOCK: { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1613 | void* p = (void*)argv[1]; |
| 1614 | SizeT szB = argv[2]; |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1615 | record_block( tid, p, szB, /*slop_szB*/0, /*exclude_first_entry*/False, |
| 1616 | /*maybe_snapshot*/True ); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1617 | *ret = 0; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1618 | return True; |
| 1619 | } |
bart | 9134738 | 2011-03-25 20:07:25 +0000 | [diff] [blame] | 1620 | case VG_USERREQ__RESIZEINPLACE_BLOCK: { |
| 1621 | void* p = (void*)argv[1]; |
| 1622 | SizeT newSizeB = argv[3]; |
| 1623 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1624 | unrecord_block(p, /*maybe_snapshot*/True, /*exclude_first_entry*/False); |
bart | 9134738 | 2011-03-25 20:07:25 +0000 | [diff] [blame] | 1625 | record_block(tid, p, newSizeB, /*slop_szB*/0, |
| 1626 | /*exclude_first_entry*/False, /*maybe_snapshot*/True); |
| 1627 | return True; |
| 1628 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1629 | case VG_USERREQ__FREELIKE_BLOCK: { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1630 | void* p = (void*)argv[1]; |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1631 | unrecord_block(p, /*maybe_snapshot*/True, /*exclude_first_entry*/False); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1632 | *ret = 0; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1633 | return True; |
| 1634 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1635 | case VG_USERREQ__GDB_MONITOR_COMMAND: { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1636 | Bool handled = handle_gdb_monitor_command (tid, (HChar*)argv[1]); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1637 | if (handled) |
| 1638 | *ret = 1; |
| 1639 | else |
| 1640 | *ret = 0; |
| 1641 | return handled; |
| 1642 | } |
| 1643 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1644 | default: |
| 1645 | *ret = 0; |
| 1646 | return False; |
| 1647 | } |
| 1648 | } |
| 1649 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1650 | //------------------------------------------------------------// |
| 1651 | //--- Instrumentation ---// |
| 1652 | //------------------------------------------------------------// |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1653 | |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 1654 | static void add_counter_update(IRSB* sbOut, Int n) |
| 1655 | { |
| 1656 | #if defined(VG_BIGENDIAN) |
| 1657 | # define END Iend_BE |
| 1658 | #elif defined(VG_LITTLEENDIAN) |
| 1659 | # define END Iend_LE |
| 1660 | #else |
| 1661 | # error "Unknown endianness" |
| 1662 | #endif |
| 1663 | // Add code to increment 'guest_instrs_executed' by 'n', like this: |
| 1664 | // WrTmp(t1, Load64(&guest_instrs_executed)) |
| 1665 | // WrTmp(t2, Add64(RdTmp(t1), Const(n))) |
| 1666 | // Store(&guest_instrs_executed, t2) |
| 1667 | IRTemp t1 = newIRTemp(sbOut->tyenv, Ity_I64); |
| 1668 | IRTemp t2 = newIRTemp(sbOut->tyenv, Ity_I64); |
| 1669 | IRExpr* counter_addr = mkIRExpr_HWord( (HWord)&guest_instrs_executed ); |
| 1670 | |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 1671 | IRStmt* st1 = IRStmt_WrTmp(t1, IRExpr_Load(END, Ity_I64, counter_addr)); |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 1672 | IRStmt* st2 = |
| 1673 | IRStmt_WrTmp(t2, |
| 1674 | IRExpr_Binop(Iop_Add64, IRExpr_RdTmp(t1), |
| 1675 | IRExpr_Const(IRConst_U64(n)))); |
sewardj | db5907d | 2009-11-26 17:20:21 +0000 | [diff] [blame] | 1676 | IRStmt* st3 = IRStmt_Store(END, counter_addr, IRExpr_RdTmp(t2)); |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 1677 | |
| 1678 | addStmtToIRSB( sbOut, st1 ); |
| 1679 | addStmtToIRSB( sbOut, st2 ); |
| 1680 | addStmtToIRSB( sbOut, st3 ); |
| 1681 | } |
| 1682 | |
| 1683 | static IRSB* ms_instrument2( IRSB* sbIn ) |
| 1684 | { |
| 1685 | Int i, n = 0; |
| 1686 | IRSB* sbOut; |
| 1687 | |
| 1688 | // We increment the instruction count in two places: |
| 1689 | // - just before any Ist_Exit statements; |
| 1690 | // - just before the IRSB's end. |
| 1691 | // In the former case, we zero 'n' and then continue instrumenting. |
| 1692 | |
| 1693 | sbOut = deepCopyIRSBExceptStmts(sbIn); |
| 1694 | |
| 1695 | for (i = 0; i < sbIn->stmts_used; i++) { |
| 1696 | IRStmt* st = sbIn->stmts[i]; |
| 1697 | |
| 1698 | if (!st || st->tag == Ist_NoOp) continue; |
| 1699 | |
| 1700 | if (st->tag == Ist_IMark) { |
| 1701 | n++; |
| 1702 | } else if (st->tag == Ist_Exit) { |
| 1703 | if (n > 0) { |
| 1704 | // Add an increment before the Exit statement, then reset 'n'. |
| 1705 | add_counter_update(sbOut, n); |
| 1706 | n = 0; |
| 1707 | } |
| 1708 | } |
| 1709 | addStmtToIRSB( sbOut, st ); |
| 1710 | } |
| 1711 | |
| 1712 | if (n > 0) { |
| 1713 | // Add an increment before the SB end. |
| 1714 | add_counter_update(sbOut, n); |
| 1715 | } |
| 1716 | return sbOut; |
| 1717 | } |
| 1718 | |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 1719 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1720 | IRSB* ms_instrument ( VgCallbackClosure* closure, |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 1721 | IRSB* sbIn, |
florian | 3c0c947 | 2014-09-24 12:06:55 +0000 | [diff] [blame] | 1722 | const VexGuestLayout* layout, |
| 1723 | const VexGuestExtents* vge, |
| 1724 | const VexArchInfo* archinfo_host, |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 1725 | IRType gWordTy, IRType hWordTy ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1726 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1727 | if (! have_started_executing_code) { |
| 1728 | // Do an initial sample to guarantee that we have at least one. |
| 1729 | // We use 'maybe_take_snapshot' instead of 'take_snapshot' to ensure |
| 1730 | // 'maybe_take_snapshot's internal static variables are initialised. |
| 1731 | have_started_executing_code = True; |
| 1732 | maybe_take_snapshot(Normal, "startup"); |
| 1733 | } |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 1734 | |
| 1735 | if (clo_time_unit == TimeI) { return ms_instrument2(sbIn); } |
| 1736 | else if (clo_time_unit == TimeMS) { return sbIn; } |
| 1737 | else if (clo_time_unit == TimeB) { return sbIn; } |
| 1738 | else { tl_assert2(0, "bad --time-unit value"); } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1741 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1742 | //------------------------------------------------------------// |
| 1743 | //--- Writing snapshots ---// |
| 1744 | //------------------------------------------------------------// |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1745 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1746 | static void pp_snapshot(MsFile *fp, Snapshot* snapshot, Int snapshot_n) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1747 | { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1748 | const Massif_Header header = (Massif_Header) { |
| 1749 | .snapshot_n = snapshot_n, |
| 1750 | .time = snapshot->time, |
| 1751 | .sz_B = snapshot->heap_szB, |
| 1752 | .extra_B = snapshot->heap_extra_szB, |
| 1753 | .stacks_B = snapshot->stacks_szB, |
| 1754 | .detailed = is_detailed_snapshot(snapshot), |
| 1755 | .peak = Peak == snapshot->kind, |
| 1756 | .top_node_desc = clo_pages_as_heap ? |
| 1757 | "(page allocation syscalls) mmap/mremap/brk, --alloc-fns, etc." |
| 1758 | : "(heap allocation functions) malloc/new/new[], --alloc-fns, etc.", |
| 1759 | .sig_threshold = clo_threshold |
| 1760 | }; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1761 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1762 | sanity_check_snapshot(snapshot); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1763 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1764 | VG_(XT_massif_print)(fp, snapshot->xt, &header, alloc_szB); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1765 | } |
| 1766 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1767 | static void write_snapshots_to_file(const HChar* massif_out_file, |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1768 | Snapshot snapshots_array[], |
| 1769 | Int nr_elements) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1770 | { |
florian | 12d2eb5 | 2014-10-30 22:17:56 +0000 | [diff] [blame] | 1771 | Int i; |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1772 | MsFile *fp; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1773 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1774 | fp = VG_(XT_massif_open)(massif_out_file, |
| 1775 | NULL, |
| 1776 | args_for_massif, |
| 1777 | TimeUnit_to_string(clo_time_unit)); |
| 1778 | if (fp == NULL) |
| 1779 | return; // Error reported by VG_(XT_massif_open) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1780 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1781 | for (i = 0; i < nr_elements; i++) { |
| 1782 | Snapshot* snapshot = & snapshots_array[i]; |
florian | 12d2eb5 | 2014-10-30 22:17:56 +0000 | [diff] [blame] | 1783 | pp_snapshot(fp, snapshot, i); // Detailed snapshot! |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1784 | } |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1785 | VG_(XT_massif_close) (fp); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1786 | } |
| 1787 | |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1788 | static void write_snapshots_array_to_file(void) |
| 1789 | { |
| 1790 | // Setup output filename. Nb: it's important to do this now, ie. as late |
| 1791 | // as possible. If we do it at start-up and the program forks and the |
| 1792 | // output file format string contains a %p (pid) specifier, both the |
| 1793 | // parent and child will incorrectly write to the same file; this |
| 1794 | // happened in 3.3.0. |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1795 | HChar* massif_out_file = |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1796 | VG_(expand_file_name)("--massif-out-file", clo_massif_out_file); |
| 1797 | write_snapshots_to_file (massif_out_file, snapshots, next_snapshot_i); |
| 1798 | VG_(free)(massif_out_file); |
| 1799 | } |
| 1800 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1801 | static void handle_snapshot_monitor_command (const HChar *filename, |
| 1802 | Bool detailed) |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1803 | { |
| 1804 | Snapshot snapshot; |
| 1805 | |
philippe | 27c9f0d | 2012-09-24 21:50:16 +0000 | [diff] [blame] | 1806 | if (!clo_pages_as_heap && !have_started_executing_code) { |
| 1807 | // See comments of variable have_started_executing_code. |
| 1808 | VG_(gdb_printf) |
| 1809 | ("error: cannot take snapshot before execution has started\n"); |
| 1810 | return; |
| 1811 | } |
| 1812 | |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1813 | clear_snapshot(&snapshot, /* do_sanity_check */ False); |
| 1814 | take_snapshot(&snapshot, Normal, get_time(), detailed); |
philippe | 27c9f0d | 2012-09-24 21:50:16 +0000 | [diff] [blame] | 1815 | write_snapshots_to_file ((filename == NULL) ? |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1816 | "massif.vgdb.out" : filename, |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1817 | &snapshot, |
| 1818 | 1); |
| 1819 | delete_snapshot(&snapshot); |
| 1820 | } |
| 1821 | |
philippe | 868bfa2 | 2015-03-07 19:20:12 +0000 | [diff] [blame] | 1822 | static void handle_all_snapshots_monitor_command (const HChar *filename) |
| 1823 | { |
| 1824 | if (!clo_pages_as_heap && !have_started_executing_code) { |
| 1825 | // See comments of variable have_started_executing_code. |
| 1826 | VG_(gdb_printf) |
| 1827 | ("error: cannot take snapshot before execution has started\n"); |
| 1828 | return; |
| 1829 | } |
| 1830 | |
| 1831 | write_snapshots_to_file ((filename == NULL) ? |
| 1832 | "massif.vgdb.out" : filename, |
| 1833 | snapshots, next_snapshot_i); |
| 1834 | } |
| 1835 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1836 | static void xtmemory_report_next_block(XT_Allocs* xta, ExeContext** ec_alloc) |
| 1837 | { |
| 1838 | const HP_Chunk* hc = VG_(HT_Next)(malloc_list); |
| 1839 | if (hc) { |
| 1840 | xta->nbytes = hc->req_szB; |
| 1841 | xta->nblocks = 1; |
| 1842 | *ec_alloc = VG_(XT_get_ec_from_xecu)(heap_xt, hc->where); |
| 1843 | } else |
| 1844 | xta->nblocks = 0; |
| 1845 | } |
| 1846 | static void ms_xtmemory_report ( const HChar* filename, Bool fini ) |
| 1847 | { |
| 1848 | // Make xtmemory_report_next_block ready to be called. |
| 1849 | VG_(HT_ResetIter)(malloc_list); |
| 1850 | VG_(XTMemory_report)(filename, fini, xtmemory_report_next_block, |
| 1851 | VG_(XT_filter_maybe_below_main)); |
| 1852 | /* As massif already filters one top function, use as filter |
| 1853 | VG_(XT_filter_maybe_below_main). */ |
| 1854 | } |
| 1855 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1856 | static Bool handle_gdb_monitor_command (ThreadId tid, HChar *req) |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1857 | { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1858 | HChar* wcmd; |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 1859 | HChar s[VG_(strlen)(req) + 1]; /* copy for strtok_r */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1860 | HChar *ssaveptr; |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1861 | |
| 1862 | VG_(strcpy) (s, req); |
| 1863 | |
| 1864 | wcmd = VG_(strtok_r) (s, " ", &ssaveptr); |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1865 | switch (VG_(keyword_id) ("help snapshot detailed_snapshot all_snapshots" |
| 1866 | " xtmemory", |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1867 | wcmd, kwd_report_duplicated_matches)) { |
| 1868 | case -2: /* multiple matches */ |
| 1869 | return True; |
| 1870 | case -1: /* not found */ |
| 1871 | return False; |
| 1872 | case 0: /* help */ |
| 1873 | print_monitor_help(); |
| 1874 | return True; |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1875 | case 1: { /* snapshot */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1876 | HChar* filename; |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1877 | filename = VG_(strtok_r) (NULL, " ", &ssaveptr); |
| 1878 | handle_snapshot_monitor_command (filename, False /* detailed */); |
| 1879 | return True; |
| 1880 | } |
sewardj | 30b3eca | 2011-06-28 08:20:39 +0000 | [diff] [blame] | 1881 | case 2: { /* detailed_snapshot */ |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1882 | HChar* filename; |
sewardj | d142f99 | 2011-05-17 17:15:07 +0000 | [diff] [blame] | 1883 | filename = VG_(strtok_r) (NULL, " ", &ssaveptr); |
| 1884 | handle_snapshot_monitor_command (filename, True /* detailed */); |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1885 | return True; |
| 1886 | } |
philippe | 868bfa2 | 2015-03-07 19:20:12 +0000 | [diff] [blame] | 1887 | case 3: { /* all_snapshots */ |
| 1888 | HChar* filename; |
| 1889 | filename = VG_(strtok_r) (NULL, " ", &ssaveptr); |
| 1890 | handle_all_snapshots_monitor_command (filename); |
| 1891 | return True; |
| 1892 | } |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1893 | case 4: { /* xtmemory */ |
| 1894 | HChar* filename; |
| 1895 | filename = VG_(strtok_r) (NULL, " ", &ssaveptr); |
| 1896 | ms_xtmemory_report (filename, False); |
| 1897 | return True; |
| 1898 | } |
sewardj | 3b29048 | 2011-05-06 21:02:55 +0000 | [diff] [blame] | 1899 | default: |
| 1900 | tl_assert(0); |
| 1901 | return False; |
| 1902 | } |
| 1903 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1904 | |
philippe | f066315 | 2013-12-19 19:10:20 +0000 | [diff] [blame] | 1905 | static void ms_print_stats (void) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1906 | { |
philippe | f066315 | 2013-12-19 19:10:20 +0000 | [diff] [blame] | 1907 | #define STATS(format, args...) \ |
| 1908 | VG_(dmsg)("Massif: " format, ##args) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1909 | |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 1910 | STATS("heap allocs: %u\n", n_heap_allocs); |
| 1911 | STATS("heap reallocs: %u\n", n_heap_reallocs); |
| 1912 | STATS("heap frees: %u\n", n_heap_frees); |
| 1913 | STATS("ignored heap allocs: %u\n", n_ignored_heap_allocs); |
| 1914 | STATS("ignored heap frees: %u\n", n_ignored_heap_frees); |
| 1915 | STATS("ignored heap reallocs: %u\n", n_ignored_heap_reallocs); |
| 1916 | STATS("stack allocs: %u\n", n_stack_allocs); |
sewardj | 2d9e874 | 2009-08-07 15:46:56 +0000 | [diff] [blame] | 1917 | STATS("skipped snapshots: %u\n", n_skipped_snapshots); |
| 1918 | STATS("real snapshots: %u\n", n_real_snapshots); |
| 1919 | STATS("detailed snapshots: %u\n", n_detailed_snapshots); |
| 1920 | STATS("peak snapshots: %u\n", n_peak_snapshots); |
| 1921 | STATS("cullings: %u\n", n_cullings); |
philippe | f066315 | 2013-12-19 19:10:20 +0000 | [diff] [blame] | 1922 | #undef STATS |
| 1923 | } |
| 1924 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1925 | |
philippe | f066315 | 2013-12-19 19:10:20 +0000 | [diff] [blame] | 1926 | //------------------------------------------------------------// |
| 1927 | //--- Finalisation ---// |
| 1928 | //------------------------------------------------------------// |
| 1929 | |
| 1930 | static void ms_fini(Int exit_status) |
| 1931 | { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1932 | ms_xtmemory_report(VG_(clo_xtree_memory_file), True); |
| 1933 | |
philippe | f066315 | 2013-12-19 19:10:20 +0000 | [diff] [blame] | 1934 | // Output. |
| 1935 | write_snapshots_array_to_file(); |
| 1936 | |
philippe | f066315 | 2013-12-19 19:10:20 +0000 | [diff] [blame] | 1937 | if (VG_(clo_stats)) |
| 1938 | ms_print_stats(); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1941 | |
| 1942 | //------------------------------------------------------------// |
| 1943 | //--- Initialisation ---// |
| 1944 | //------------------------------------------------------------// |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1945 | |
| 1946 | static void ms_post_clo_init(void) |
| 1947 | { |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1948 | Int i; |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1949 | HChar* LD_PRELOAD_val; |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1950 | |
| 1951 | /* We will record execontext up to clo_depth + overestimate and |
| 1952 | we will store this as ec => we need to increase the backtrace size |
| 1953 | if smaller than what we will store. */ |
| 1954 | if (VG_(clo_backtrace_size) < clo_depth + MAX_OVERESTIMATE) |
| 1955 | VG_(clo_backtrace_size) = clo_depth + MAX_OVERESTIMATE; |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1956 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1957 | // Check options. |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1958 | if (clo_pages_as_heap) { |
| 1959 | if (clo_stacks) { |
florian | 5b99e66 | 2014-11-29 14:41:32 +0000 | [diff] [blame] | 1960 | VG_(fmsg_bad_option)("--pages-as-heap=yes", |
| 1961 | "Cannot be used together with --stacks=yes"); |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1962 | } |
| 1963 | } |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 1964 | if (!clo_heap) { |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1965 | clo_pages_as_heap = False; |
| 1966 | } |
| 1967 | |
| 1968 | // If --pages-as-heap=yes we don't want malloc replacement to occur. So we |
| 1969 | // disable vgpreload_massif-$PLATFORM.so by removing it from LD_PRELOAD (or |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1970 | // platform-equivalent). This is a bit of a hack, but LD_PRELOAD is setup |
| 1971 | // well before tool initialisation, so this seems the best way to do it. |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1972 | if (clo_pages_as_heap) { |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1973 | HChar* s1; |
| 1974 | HChar* s2; |
| 1975 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1976 | clo_heap_admin = 0; // No heap admin on pages. |
| 1977 | |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 1978 | LD_PRELOAD_val = VG_(getenv)( VG_(LD_PRELOAD_var_name) ); |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1979 | tl_assert(LD_PRELOAD_val); |
| 1980 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1981 | VERB(2, "clo_pages_as_heap orig LD_PRELOAD '%s'\n", LD_PRELOAD_val); |
| 1982 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1983 | // Make sure the vgpreload_core-$PLATFORM entry is there, for sanity. |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1984 | s1 = VG_(strstr)(LD_PRELOAD_val, "vgpreload_core"); |
| 1985 | tl_assert(s1); |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1986 | |
| 1987 | // Now find the vgpreload_massif-$PLATFORM entry. |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1988 | s1 = VG_(strstr)(LD_PRELOAD_val, "vgpreload_massif"); |
| 1989 | tl_assert(s1); |
| 1990 | s2 = s1; |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1991 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1992 | // Position s1 on the previous ':', which must be there because |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1993 | // of the preceding vgpreload_core-$PLATFORM entry. |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1994 | for (; *s1 != ':'; s1--) |
| 1995 | ; |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 1996 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 1997 | // Position s2 on the next ':' or \0 |
| 1998 | for (; *s2 != ':' && *s2 != '\0'; s2++) |
| 1999 | ; |
| 2000 | |
| 2001 | // Move all characters from s2 to s1 |
| 2002 | while ((*s1++ = *s2++)) |
| 2003 | ; |
| 2004 | |
| 2005 | VERB(2, "clo_pages_as_heap cleaned LD_PRELOAD '%s'\n", LD_PRELOAD_val); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2006 | } |
| 2007 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2008 | // Print alloc-fns and ignore-fns, if necessary. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2009 | if (VG_(clo_verbosity) > 1) { |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 2010 | VERB(1, "alloc-fns:\n"); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2011 | for (i = 0; i < VG_(sizeXA)(alloc_fns); i++) { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2012 | HChar** fn_ptr = VG_(indexXA)(alloc_fns, i); |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 2013 | VERB(1, " %s\n", *fn_ptr); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 2016 | VERB(1, "ignore-fns:\n"); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2017 | if (0 == VG_(sizeXA)(ignore_fns)) { |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 2018 | VERB(1, " <empty>\n"); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2019 | } |
| 2020 | for (i = 0; i < VG_(sizeXA)(ignore_fns); i++) { |
florian | 19f91bb | 2012-11-10 22:29:54 +0000 | [diff] [blame] | 2021 | HChar** fn_ptr = VG_(indexXA)(ignore_fns, i); |
sewardj | 5ea0f39 | 2009-07-15 14:51:34 +0000 | [diff] [blame] | 2022 | VERB(1, " %d: %s\n", i, *fn_ptr); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2023 | } |
| 2024 | } |
| 2025 | |
| 2026 | // Events to track. |
| 2027 | if (clo_stacks) { |
| 2028 | VG_(track_new_mem_stack) ( new_mem_stack ); |
| 2029 | VG_(track_die_mem_stack) ( die_mem_stack ); |
| 2030 | VG_(track_new_mem_stack_signal) ( new_mem_stack_signal ); |
| 2031 | VG_(track_die_mem_stack_signal) ( die_mem_stack_signal ); |
| 2032 | } |
| 2033 | |
njn | e323a6b | 2010-07-01 02:35:03 +0000 | [diff] [blame] | 2034 | if (clo_pages_as_heap) { |
| 2035 | VG_(track_new_mem_startup) ( ms_new_mem_startup ); |
| 2036 | VG_(track_new_mem_brk) ( ms_new_mem_brk ); |
| 2037 | VG_(track_new_mem_mmap) ( ms_new_mem_mmap ); |
| 2038 | |
| 2039 | VG_(track_copy_mem_remap) ( ms_copy_mem_remap ); |
| 2040 | |
| 2041 | VG_(track_die_mem_brk) ( ms_die_mem_brk ); |
| 2042 | VG_(track_die_mem_munmap) ( ms_die_mem_munmap ); |
| 2043 | } |
| 2044 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2045 | // Initialise snapshot array, and sanity-check it. |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2046 | snapshots = VG_(malloc)("ms.main.mpoci.1", |
| 2047 | sizeof(Snapshot) * clo_max_snapshots); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2048 | // We don't want to do snapshot sanity checks here, because they're |
| 2049 | // currently uninitialised. |
| 2050 | for (i = 0; i < clo_max_snapshots; i++) { |
| 2051 | clear_snapshot( & snapshots[i], /*do_sanity_check*/False ); |
| 2052 | } |
| 2053 | sanity_check_snapshots_array(); |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 2054 | |
| 2055 | if (VG_(clo_xtree_memory) == Vg_XTMemory_Full) |
| 2056 | // Activate full xtree memory profiling. |
| 2057 | // As massif already filters one top function, use as filter |
| 2058 | // VG_(XT_filter_maybe_below_main). |
| 2059 | VG_(XTMemory_Full_init)(VG_(XT_filter_maybe_below_main)); |
| 2060 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
tom | 151a639 | 2005-11-11 12:30:36 +0000 | [diff] [blame] | 2063 | static void ms_pre_clo_init(void) |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2064 | { |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2065 | VG_(details_name) ("Massif"); |
| 2066 | VG_(details_version) (NULL); |
njn | 1a2741a | 2007-11-26 21:59:04 +0000 | [diff] [blame] | 2067 | VG_(details_description) ("a heap profiler"); |
njn | 9a0cba4 | 2007-04-15 22:15:57 +0000 | [diff] [blame] | 2068 | VG_(details_copyright_author)( |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 2069 | "Copyright (C) 2003-2017, and GNU GPL'd, by Nicholas Nethercote"); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2070 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
| 2071 | |
sewardj | 1e0fff6 | 2011-01-10 15:01:03 +0000 | [diff] [blame] | 2072 | VG_(details_avg_translation_sizeB) ( 330 ); |
| 2073 | |
sewardj | 8d47a61 | 2015-02-05 12:59:46 +0000 | [diff] [blame] | 2074 | VG_(clo_vex_control).iropt_register_updates_default |
| 2075 | = VG_(clo_px_file_backed) |
philippe | 5b240c2 | 2012-08-14 22:28:31 +0000 | [diff] [blame] | 2076 | = VexRegUpdSpAtMemAccess; // overridable by the user. |
| 2077 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2078 | // Basic functions. |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2079 | VG_(basic_tool_funcs) (ms_post_clo_init, |
| 2080 | ms_instrument, |
| 2081 | ms_fini); |
| 2082 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2083 | // Needs. |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2084 | VG_(needs_libc_freeres)(); |
Elliott Hughes | a0664b9 | 2017-04-18 17:46:52 -0700 | [diff] [blame] | 2085 | VG_(needs_cxx_freeres)(); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2086 | VG_(needs_command_line_options)(ms_process_cmd_line_option, |
| 2087 | ms_print_usage, |
| 2088 | ms_print_debug_usage); |
| 2089 | VG_(needs_client_requests) (ms_handle_client_request); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2090 | VG_(needs_sanity_checks) (ms_cheap_sanity_check, |
| 2091 | ms_expensive_sanity_check); |
philippe | f066315 | 2013-12-19 19:10:20 +0000 | [diff] [blame] | 2092 | VG_(needs_print_stats) (ms_print_stats); |
njn | fc51f8d | 2005-06-21 03:20:17 +0000 | [diff] [blame] | 2093 | VG_(needs_malloc_replacement) (ms_malloc, |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2094 | ms___builtin_new, |
| 2095 | ms___builtin_vec_new, |
| 2096 | ms_memalign, |
| 2097 | ms_calloc, |
| 2098 | ms_free, |
| 2099 | ms___builtin_delete, |
| 2100 | ms___builtin_vec_delete, |
| 2101 | ms_realloc, |
njn | 8b140de | 2009-02-17 04:31:18 +0000 | [diff] [blame] | 2102 | ms_malloc_usable_size, |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2103 | 0 ); |
| 2104 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2105 | // HP_Chunks. |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 2106 | HP_chunk_poolalloc = VG_(newPA) |
| 2107 | (sizeof(HP_Chunk), |
| 2108 | 1000, |
| 2109 | VG_(malloc), |
| 2110 | "massif MC_Chunk pool", |
| 2111 | VG_(free)); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2112 | malloc_list = VG_(HT_construct)( "Massif's malloc list" ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2113 | |
Elliott Hughes | ed39800 | 2017-06-21 14:41:24 -0700 | [diff] [blame^] | 2114 | // Heap XTree |
| 2115 | heap_xt = VG_(XT_create)(VG_(malloc), |
| 2116 | "ms.xtrees", |
| 2117 | VG_(free), |
| 2118 | sizeof(SizeT), |
| 2119 | init_szB, add_szB, sub_szB, |
| 2120 | filter_IPs); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2121 | |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2122 | // Initialise alloc_fns and ignore_fns. |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2123 | init_alloc_fns(); |
njn | f6b0076 | 2009-04-17 04:26:41 +0000 | [diff] [blame] | 2124 | init_ignore_fns(); |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2125 | |
| 2126 | // Initialise args_for_massif. |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 2127 | args_for_massif = VG_(newXA)(VG_(malloc), "ms.main.mprci.1", |
| 2128 | VG_(free), sizeof(HChar*)); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 2131 | VG_DETERMINE_INTERFACE_VERSION(ms_pre_clo_init) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 2132 | |
njn | 734b805 | 2007-11-01 04:40:37 +0000 | [diff] [blame] | 2133 | //--------------------------------------------------------------------// |
| 2134 | //--- end ---// |
| 2135 | //--------------------------------------------------------------------// |