nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 3 | /*--- Massif: a heap profiling tool. ms_main.c ---*/ |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 7 | This file is part of Massif, a Valgrind tool for profiling memory |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 8 | usage of programs. |
| 9 | |
sewardj | 9ebd6e0 | 2007-01-08 06:01:59 +0000 | [diff] [blame] | 10 | Copyright (C) 2003-2007 Nicholas Nethercote |
njn | 2bc1012 | 2005-05-08 02:10:27 +0000 | [diff] [blame] | 11 | njn@valgrind.org |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
| 28 | The GNU General Public License is contained in the file COPYING. |
| 29 | */ |
| 30 | |
| 31 | // Memory profiler. Produces a graph, gives lots of information about |
| 32 | // allocation contexts, in terms of space.time values (ie. area under the |
| 33 | // graph). Allocation context information is hierarchical, and can thus |
| 34 | // be inspected step-wise to an appropriate depth. See comments on data |
| 35 | // structures below for more info on how things work. |
| 36 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 37 | #include "pub_tool_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 38 | #include "pub_tool_vki.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 39 | #include "pub_tool_aspacemgr.h" |
njn | ea27e46 | 2005-05-31 02:38:09 +0000 | [diff] [blame] | 40 | #include "pub_tool_debuginfo.h" |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 41 | #include "pub_tool_hashtable.h" |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 42 | #include "pub_tool_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 43 | #include "pub_tool_libcassert.h" |
njn | eb8896b | 2005-06-04 20:03:55 +0000 | [diff] [blame] | 44 | #include "pub_tool_libcfile.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 45 | #include "pub_tool_libcprint.h" |
njn | f39e9a3 | 2005-06-12 02:43:17 +0000 | [diff] [blame] | 46 | #include "pub_tool_libcproc.h" |
njn | b506bd8 | 2005-06-21 04:01:51 +0000 | [diff] [blame] | 47 | #include "pub_tool_machine.h" |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 48 | #include "pub_tool_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 49 | #include "pub_tool_options.h" |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 50 | #include "pub_tool_replacemalloc.h" |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 51 | #include "pub_tool_stacktrace.h" |
njn | 43b9a8a | 2005-05-10 04:37:01 +0000 | [diff] [blame] | 52 | #include "pub_tool_tooliface.h" |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 53 | #include "pub_tool_xarray.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 54 | #include "pub_tool_clientstate.h" |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 55 | |
| 56 | #include "valgrind.h" // For {MALLOC,FREE}LIKE_BLOCK |
| 57 | |
| 58 | /*------------------------------------------------------------*/ |
| 59 | /*--- Overview of operation ---*/ |
| 60 | /*------------------------------------------------------------*/ |
| 61 | |
| 62 | // Heap blocks are tracked, and the amount of space allocated by various |
| 63 | // contexts (ie. lines of code, more or less) is also tracked. |
| 64 | // Periodically, a census is taken, and the amount of space used, at that |
| 65 | // point, by the most significant (highly allocating) contexts is recorded. |
| 66 | // Census start off frequently, but are scaled back as the program goes on, |
| 67 | // so that there are always a good number of them. At the end, overall |
| 68 | // spacetimes for different contexts (of differing levels of precision) is |
| 69 | // calculated, the graph is printed, and the text giving spacetimes for the |
| 70 | // increasingly precise contexts is given. |
| 71 | // |
| 72 | // Measures the following: |
| 73 | // - heap blocks |
| 74 | // - heap admin bytes |
| 75 | // - stack(s) |
| 76 | // - code (code segments loaded at startup, and loaded with mmap) |
| 77 | // - data (data segments loaded at startup, and loaded/created with mmap, |
| 78 | // and brk()d segments) |
| 79 | |
| 80 | /*------------------------------------------------------------*/ |
| 81 | /*--- Main types ---*/ |
| 82 | /*------------------------------------------------------------*/ |
| 83 | |
| 84 | // An XPt represents an "execution point", ie. a code address. Each XPt is |
| 85 | // part of a tree of XPts (an "execution tree", or "XTree"). Each |
| 86 | // top-to-bottom path through an XTree gives an execution context ("XCon"), |
| 87 | // and is equivalent to a traditional Valgrind ExeContext. |
| 88 | // |
| 89 | // The XPt at the top of an XTree (but below "alloc_xpt") is called a |
| 90 | // "top-XPt". The XPts are the bottom of an XTree (leaf nodes) are |
| 91 | // "bottom-XPTs". The number of XCons in an XTree is equal to the number of |
| 92 | // bottom-XPTs in that XTree. |
| 93 | // |
| 94 | // All XCons have the same top-XPt, "alloc_xpt", which represents all |
| 95 | // allocation functions like malloc(). It's a bit of a fake XPt, though, |
| 96 | // and is only used because it makes some of the code simpler. |
| 97 | // |
| 98 | // XTrees are bi-directional. |
| 99 | // |
| 100 | // > parent < Example: if child1() calls parent() and child2() |
| 101 | // / | \ also calls parent(), and parent() calls malloc(), |
| 102 | // | / \ | the XTree will look like this. |
| 103 | // | v v | |
| 104 | // child1 child2 |
| 105 | |
| 106 | typedef struct _XPt XPt; |
| 107 | |
| 108 | struct _XPt { |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 109 | Addr ip; // code address |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 110 | |
| 111 | // Bottom-XPts: space for the precise context. |
| 112 | // Other XPts: space of all the descendent bottom-XPts. |
| 113 | // Nb: this value goes up and down as the program executes. |
| 114 | UInt curr_space; |
| 115 | |
| 116 | // An approximate space.time calculation used along the way for selecting |
| 117 | // which contexts to include at each census point. |
| 118 | // !!! top-XPTs only !!! |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 119 | ULong approx_ST; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 120 | |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 121 | // exact_ST_dbld is an exact space.time calculation done at the end, and |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 122 | // used in the results. |
| 123 | // Note that it is *doubled*, to avoid rounding errors. |
| 124 | // !!! not used for 'alloc_xpt' !!! |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 125 | ULong exact_ST_dbld; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 126 | |
| 127 | // n_children and max_children are integers; a very big program might |
| 128 | // have more than 65536 allocation points (Konqueror startup has 1800). |
| 129 | XPt* parent; // pointer to parent XPt |
| 130 | UInt n_children; // number of children |
| 131 | UInt max_children; // capacity of children array |
| 132 | XPt** children; // pointers to children XPts |
| 133 | }; |
| 134 | |
| 135 | // Each census snapshots the most significant XTrees, each XTree having a |
| 136 | // top-XPt as its root. The 'curr_space' element for each XPt is recorded |
| 137 | // in the snapshot. The snapshot contains all the XTree's XPts, not in a |
| 138 | // tree structure, but flattened into an array. This flat snapshot is used |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 139 | // at the end for computing exact_ST_dbld for each XPt. |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 140 | // |
| 141 | // Graph resolution, x-axis: no point having more than about 200 census |
| 142 | // x-points; you can't see them on the graph. Therefore: |
| 143 | // |
| 144 | // - do a census every 1 ms for first 200 --> 200, all (200 ms) |
| 145 | // - halve (drop half of them) --> 100, every 2nd (200 ms) |
| 146 | // - do a census every 2 ms for next 200 --> 200, every 2nd (400 ms) |
| 147 | // - halve --> 100, every 4th (400 ms) |
| 148 | // - do a census every 4 ms for next 400 --> 200, every 4th (800 ms) |
| 149 | // - etc. |
| 150 | // |
| 151 | // This isn't exactly right, because we actually drop (N/2)-1 when halving, |
| 152 | // but it shows the basic idea. |
| 153 | |
| 154 | #define MAX_N_CENSI 200 // Keep it even, for simplicity |
| 155 | |
| 156 | // Graph resolution, y-axis: hp2ps only draws the 19 biggest (in space-time) |
| 157 | // bands, rest get lumped into OTHERS. I only print the top N |
| 158 | // (cumulative-so-far space-time) at each point. N should be a bit bigger |
| 159 | // than 19 in case the cumulative space-time doesn't fit with the eventual |
| 160 | // space-time computed by hp2ps (but it should be close if the samples are |
| 161 | // evenly spread, since hp2ps does an approximate per-band space-time |
| 162 | // calculation that just sums the totals; ie. it assumes all samples are |
| 163 | // the same distance apart). |
| 164 | |
| 165 | #define MAX_SNAPSHOTS 32 |
| 166 | |
| 167 | typedef |
| 168 | struct { |
| 169 | XPt* xpt; |
| 170 | UInt space; |
| 171 | } |
| 172 | XPtSnapshot; |
| 173 | |
| 174 | // An XTree snapshot is stored as an array of of XPt snapshots. |
| 175 | typedef XPtSnapshot* XTreeSnapshot; |
| 176 | |
| 177 | typedef |
| 178 | struct { |
| 179 | Int ms_time; // Int: must allow -1 |
| 180 | XTreeSnapshot xtree_snapshots[MAX_SNAPSHOTS+1]; // +1 for zero-termination |
| 181 | UInt others_space; |
| 182 | UInt heap_admin_space; |
| 183 | UInt stacks_space; |
| 184 | } |
| 185 | Census; |
| 186 | |
| 187 | // Metadata for heap blocks. Each one contains a pointer to a bottom-XPt, |
| 188 | // which is a foothold into the XCon at which it was allocated. From |
| 189 | // HP_Chunks, XPt 'space' fields are incremented (at allocation) and |
| 190 | // decremented (at deallocation). |
| 191 | // |
| 192 | // Nb: first two fields must match core's VgHashNode. |
| 193 | typedef |
| 194 | struct _HP_Chunk { |
| 195 | struct _HP_Chunk* next; |
| 196 | Addr data; // Ptr to actual block |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 197 | SizeT size; // Size requested |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 198 | XPt* where; // Where allocated; bottom-XPt |
| 199 | } |
| 200 | HP_Chunk; |
| 201 | |
| 202 | /*------------------------------------------------------------*/ |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 203 | /*--- Statistics ---*/ |
| 204 | /*------------------------------------------------------------*/ |
| 205 | |
| 206 | // Konqueror startup, to give an idea of the numbers involved with a biggish |
| 207 | // program, with default depth: |
| 208 | // |
| 209 | // depth=3 depth=40 |
| 210 | // - 310,000 allocations |
| 211 | // - 300,000 frees |
| 212 | // - 15,000 XPts 800,000 XPts |
| 213 | // - 1,800 top-XPts |
| 214 | |
| 215 | static UInt n_xpts = 0; |
| 216 | static UInt n_bot_xpts = 0; |
| 217 | static UInt n_allocs = 0; |
| 218 | static UInt n_zero_allocs = 0; |
| 219 | static UInt n_frees = 0; |
| 220 | static UInt n_children_reallocs = 0; |
| 221 | static UInt n_snapshot_frees = 0; |
| 222 | |
| 223 | static UInt n_halvings = 0; |
| 224 | static UInt n_real_censi = 0; |
| 225 | static UInt n_fake_censi = 0; |
| 226 | static UInt n_attempted_censi = 0; |
| 227 | |
| 228 | /*------------------------------------------------------------*/ |
| 229 | /*--- Globals ---*/ |
| 230 | /*------------------------------------------------------------*/ |
| 231 | |
| 232 | #define FILENAME_LEN 256 |
| 233 | |
| 234 | #define SPRINTF(zz_buf, fmt, args...) \ |
| 235 | do { Int len = VG_(sprintf)(zz_buf, fmt, ## args); \ |
| 236 | VG_(write)(fd, (void*)zz_buf, len); \ |
| 237 | } while (0) |
| 238 | |
| 239 | #define BUF_LEN 1024 // general purpose |
| 240 | static Char buf [BUF_LEN]; |
| 241 | static Char buf2[BUF_LEN]; |
| 242 | static Char buf3[BUF_LEN]; |
| 243 | |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 244 | static SizeT sigstacks_space = 0; // Current signal stacks space sum |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 245 | |
| 246 | static VgHashTable malloc_list = NULL; // HP_Chunks |
| 247 | |
| 248 | static UInt n_heap_blocks = 0; |
| 249 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 250 | // Current directory at startup. |
njn | 57ca7ab | 2005-06-21 23:44:58 +0000 | [diff] [blame] | 251 | static Char base_dir[VKI_PATH_MAX]; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 252 | |
njn | a3e991b | 2007-03-26 23:51:29 +0000 | [diff] [blame] | 253 | #define MAX_ALLOC_FNS 128 // includes the builtin ones |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 254 | |
nethercote | c746918 | 2004-05-11 09:21:08 +0000 | [diff] [blame] | 255 | // First few filled in, rest should be zeroed. Zero-terminated vector. |
njn | a3e991b | 2007-03-26 23:51:29 +0000 | [diff] [blame] | 256 | static UInt n_alloc_fns = 10; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 257 | static Char* alloc_fns[MAX_ALLOC_FNS] = { |
| 258 | "malloc", |
| 259 | "operator new(unsigned)", |
| 260 | "operator new[](unsigned)", |
nethercote | eb479cb | 2004-05-11 16:37:17 +0000 | [diff] [blame] | 261 | "operator new(unsigned, std::nothrow_t const&)", |
| 262 | "operator new[](unsigned, std::nothrow_t const&)", |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 263 | "__builtin_new", |
| 264 | "__builtin_vec_new", |
| 265 | "calloc", |
| 266 | "realloc", |
fitzhardinge | 51f3ff1 | 2004-03-04 22:42:03 +0000 | [diff] [blame] | 267 | "memalign", |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
| 270 | |
| 271 | /*------------------------------------------------------------*/ |
| 272 | /*--- Command line args ---*/ |
| 273 | /*------------------------------------------------------------*/ |
| 274 | |
| 275 | #define MAX_DEPTH 50 |
| 276 | |
| 277 | typedef |
| 278 | enum { |
| 279 | XText, XHTML, |
| 280 | } |
| 281 | XFormat; |
| 282 | |
| 283 | static Bool clo_heap = True; |
| 284 | static UInt clo_heap_admin = 8; |
| 285 | static Bool clo_stacks = True; |
| 286 | static Bool clo_depth = 3; |
| 287 | static XFormat clo_format = XText; |
| 288 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 289 | static Bool ms_process_cmd_line_option(Char* arg) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 290 | { |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 291 | VG_BOOL_CLO(arg, "--heap", clo_heap) |
| 292 | else VG_BOOL_CLO(arg, "--stacks", clo_stacks) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 293 | |
njn | 45270a2 | 2005-03-27 01:00:11 +0000 | [diff] [blame] | 294 | else VG_NUM_CLO (arg, "--heap-admin", clo_heap_admin) |
| 295 | else VG_BNUM_CLO(arg, "--depth", clo_depth, 1, MAX_DEPTH) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 296 | |
| 297 | else if (VG_CLO_STREQN(11, arg, "--alloc-fn=")) { |
| 298 | alloc_fns[n_alloc_fns] = & arg[11]; |
| 299 | n_alloc_fns++; |
| 300 | if (n_alloc_fns >= MAX_ALLOC_FNS) { |
| 301 | VG_(printf)("Too many alloc functions specified, sorry"); |
sewardj | 6893d65 | 2006-10-15 01:25:13 +0000 | [diff] [blame] | 302 | VG_(err_bad_option)(arg); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | else if (VG_CLO_STREQ(arg, "--format=text")) |
| 307 | clo_format = XText; |
| 308 | else if (VG_CLO_STREQ(arg, "--format=html")) |
| 309 | clo_format = XHTML; |
| 310 | |
| 311 | else |
| 312 | return VG_(replacement_malloc_process_cmd_line_option)(arg); |
nethercote | 27fec90 | 2004-06-16 21:26:32 +0000 | [diff] [blame] | 313 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 314 | return True; |
| 315 | } |
| 316 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 317 | static void ms_print_usage(void) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 318 | { |
| 319 | VG_(printf)( |
| 320 | " --heap=no|yes profile heap blocks [yes]\n" |
| 321 | " --heap-admin=<number> average admin bytes per heap block [8]\n" |
| 322 | " --stacks=no|yes profile stack(s) [yes]\n" |
| 323 | " --depth=<number> depth of contexts [3]\n" |
| 324 | " --alloc-fn=<name> specify <fn> as an alloc function [empty]\n" |
| 325 | " --format=text|html format of textual output [text]\n" |
| 326 | ); |
| 327 | VG_(replacement_malloc_print_usage)(); |
| 328 | } |
| 329 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 330 | static void ms_print_debug_usage(void) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 331 | { |
| 332 | VG_(replacement_malloc_print_debug_usage)(); |
| 333 | } |
| 334 | |
| 335 | /*------------------------------------------------------------*/ |
| 336 | /*--- Execution contexts ---*/ |
| 337 | /*------------------------------------------------------------*/ |
| 338 | |
| 339 | // Fake XPt representing all allocation functions like malloc(). Acts as |
| 340 | // parent node to all top-XPts. |
| 341 | static XPt* alloc_xpt; |
| 342 | |
| 343 | // Cheap allocation for blocks that never need to be freed. Saves about 10% |
| 344 | // for Konqueror startup with --depth=40. |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 345 | static void* perm_malloc(SizeT n_bytes) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 346 | { |
| 347 | static Addr hp = 0; // current heap pointer |
| 348 | static Addr hp_lim = 0; // maximum usable byte in current block |
| 349 | |
| 350 | #define SUPERBLOCK_SIZE (1 << 20) // 1 MB |
| 351 | |
| 352 | if (hp + n_bytes > hp_lim) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 353 | hp = (Addr)VG_(am_shadow_alloc)(SUPERBLOCK_SIZE); |
| 354 | if (hp == 0) |
| 355 | VG_(out_of_memory_NORETURN)( "massif:perm_malloc", |
| 356 | SUPERBLOCK_SIZE); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 357 | hp_lim = hp + SUPERBLOCK_SIZE - 1; |
| 358 | } |
| 359 | |
| 360 | hp += n_bytes; |
| 361 | |
| 362 | return (void*)(hp - n_bytes); |
| 363 | } |
| 364 | |
| 365 | |
| 366 | |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 367 | static XPt* new_XPt(Addr ip, XPt* parent, Bool is_bottom) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 368 | { |
| 369 | XPt* xpt = perm_malloc(sizeof(XPt)); |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 370 | xpt->ip = ip; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 371 | |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 372 | xpt->curr_space = 0; |
| 373 | xpt->approx_ST = 0; |
| 374 | xpt->exact_ST_dbld = 0; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 375 | |
| 376 | xpt->parent = parent; |
nethercote | fc01635 | 2004-04-27 09:51:51 +0000 | [diff] [blame] | 377 | |
| 378 | // Check parent is not a bottom-XPt |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 379 | tl_assert(parent == NULL || 0 != parent->max_children); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 380 | |
| 381 | xpt->n_children = 0; |
| 382 | |
| 383 | // If a bottom-XPt, don't allocate space for children. This can be 50% |
| 384 | // or more, although it tends to drop as --depth increases (eg. 10% for |
| 385 | // konqueror with --depth=20). |
| 386 | if ( is_bottom ) { |
| 387 | xpt->max_children = 0; |
| 388 | xpt->children = NULL; |
| 389 | n_bot_xpts++; |
| 390 | } else { |
| 391 | xpt->max_children = 4; |
| 392 | xpt->children = VG_(malloc)( xpt->max_children * sizeof(XPt*) ); |
| 393 | } |
| 394 | |
| 395 | // Update statistics |
| 396 | n_xpts++; |
| 397 | |
| 398 | return xpt; |
| 399 | } |
| 400 | |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 401 | static Bool is_alloc_fn(Addr ip) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 402 | { |
| 403 | Int i; |
| 404 | |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 405 | if ( VG_(get_fnname)(ip, buf, BUF_LEN) ) { |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 406 | for (i = 0; i < n_alloc_fns; i++) { |
| 407 | if (VG_STREQ(buf, alloc_fns[i])) |
| 408 | return True; |
| 409 | } |
| 410 | } |
| 411 | return False; |
| 412 | } |
| 413 | |
| 414 | // Returns an XCon, from the bottom-XPt. Nb: the XPt returned must be a |
| 415 | // bottom-XPt now and must always remain a bottom-XPt. We go to some effort |
| 416 | // to ensure this in certain cases. See comments below. |
| 417 | static XPt* get_XCon( ThreadId tid, Bool custom_malloc ) |
| 418 | { |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 419 | // Static to minimise stack size. +1 for added ~0 IP |
| 420 | static Addr ips[MAX_DEPTH + MAX_ALLOC_FNS + 1]; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 421 | |
| 422 | XPt* xpt = alloc_xpt; |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 423 | UInt n_ips, L, A, B, nC; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 424 | UInt overestimate; |
| 425 | Bool reached_bottom; |
| 426 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 427 | // Want at least clo_depth non-alloc-fn entries in the snapshot. |
| 428 | // However, because we have 1 or more (an unknown number, at this point) |
| 429 | // alloc-fns ignored, we overestimate the size needed for the stack |
| 430 | // snapshot. Then, if necessary, we repeatedly increase the size until |
| 431 | // it is enough. |
| 432 | overestimate = 2; |
| 433 | while (True) { |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 434 | n_ips = VG_(get_StackTrace)( tid, ips, clo_depth + overestimate ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 435 | |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 436 | // Now we add a dummy "unknown" IP at the end. This is only used if we |
| 437 | // run out of IPs before hitting clo_depth. It's done to ensure the |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 438 | // XPt we return is (now and forever) a bottom-XPt. If the returned XPt |
| 439 | // wasn't a bottom-XPt (now or later) it would cause problems later (eg. |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 440 | // the parent's approx_ST wouldn't be equal [or almost equal] to the |
| 441 | // total of the childrens' approx_STs). |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 442 | ips[ n_ips++ ] = ~((Addr)0); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 443 | |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 444 | // Skip over alloc functions in ips[]. |
| 445 | for (L = 0; is_alloc_fn(ips[L]) && L < n_ips; L++) { } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 446 | |
| 447 | // Must be at least one alloc function, unless client used |
| 448 | // MALLOCLIKE_BLOCK |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 449 | if (!custom_malloc) tl_assert(L > 0); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 450 | |
| 451 | // Should be at least one non-alloc function. If not, try again. |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 452 | if (L == n_ips) { |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 453 | overestimate += 2; |
| 454 | if (overestimate > MAX_ALLOC_FNS) |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 455 | VG_(tool_panic)("No stk snapshot big enough to find non-alloc fns"); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 456 | } else { |
| 457 | break; |
| 458 | } |
| 459 | } |
| 460 | A = L; |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 461 | B = n_ips - 1; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 462 | reached_bottom = False; |
| 463 | |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 464 | // By this point, the IPs we care about are in ips[A]..ips[B] |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 465 | |
| 466 | // Now do the search/insertion of the XCon. 'L' is the loop counter, |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 467 | // being the index into ips[]. |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 468 | while (True) { |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 469 | // Look for IP in xpt's children. |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 470 | // XXX: linear search, ugh -- about 10% of time for konqueror startup |
| 471 | // XXX: tried cacheing last result, only hit about 4% for konqueror |
| 472 | // Nb: this search hits about 98% of the time for konqueror |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 473 | |
| 474 | // If we've searched/added deep enough, or run out of EIPs, this is |
| 475 | // the bottom XPt. |
| 476 | if (L - A + 1 == clo_depth || L == B) |
| 477 | reached_bottom = True; |
| 478 | |
| 479 | nC = 0; |
| 480 | while (True) { |
| 481 | if (nC == xpt->n_children) { |
| 482 | // not found, insert new XPt |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 483 | tl_assert(xpt->max_children != 0); |
| 484 | tl_assert(xpt->n_children <= xpt->max_children); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 485 | // Expand 'children' if necessary |
| 486 | if (xpt->n_children == xpt->max_children) { |
| 487 | xpt->max_children *= 2; |
| 488 | xpt->children = VG_(realloc)( xpt->children, |
| 489 | xpt->max_children * sizeof(XPt*) ); |
| 490 | n_children_reallocs++; |
| 491 | } |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 492 | // Make new XPt for IP, insert in list |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 493 | xpt->children[ xpt->n_children++ ] = |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 494 | new_XPt(ips[L], xpt, reached_bottom); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 495 | break; |
| 496 | } |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 497 | if (ips[L] == xpt->children[nC]->ip) break; // found the IP |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 498 | nC++; // keep looking |
| 499 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 500 | |
| 501 | // Return found/built bottom-XPt. |
| 502 | if (reached_bottom) { |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 503 | tl_assert(0 == xpt->children[nC]->n_children); // Must be bottom-XPt |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 504 | return xpt->children[nC]; |
| 505 | } |
| 506 | |
| 507 | // Descend to next level in XTree, the newly found/built non-bottom-XPt |
| 508 | xpt = xpt->children[nC]; |
| 509 | L++; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | // Update 'curr_space' of every XPt in the XCon, by percolating upwards. |
| 514 | static void update_XCon(XPt* xpt, Int space_delta) |
| 515 | { |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 516 | tl_assert(True == clo_heap); |
| 517 | tl_assert(0 != space_delta); |
| 518 | tl_assert(NULL != xpt); |
| 519 | tl_assert(0 == xpt->n_children); // must be bottom-XPt |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 520 | |
| 521 | while (xpt != alloc_xpt) { |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 522 | if (space_delta < 0) tl_assert(xpt->curr_space >= -space_delta); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 523 | xpt->curr_space += space_delta; |
| 524 | xpt = xpt->parent; |
| 525 | } |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 526 | if (space_delta < 0) tl_assert(alloc_xpt->curr_space >= -space_delta); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 527 | alloc_xpt->curr_space += space_delta; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | // Actually want a reverse sort, biggest to smallest |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 531 | static Int XPt_cmp_approx_ST(void* n1, void* n2) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 532 | { |
| 533 | XPt* xpt1 = *(XPt**)n1; |
| 534 | XPt* xpt2 = *(XPt**)n2; |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 535 | return (xpt1->approx_ST < xpt2->approx_ST ? 1 : -1); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 536 | } |
| 537 | |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 538 | static Int XPt_cmp_exact_ST_dbld(void* n1, void* n2) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 539 | { |
| 540 | XPt* xpt1 = *(XPt**)n1; |
| 541 | XPt* xpt2 = *(XPt**)n2; |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 542 | return (xpt1->exact_ST_dbld < xpt2->exact_ST_dbld ? 1 : -1); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | |
| 546 | /*------------------------------------------------------------*/ |
| 547 | /*--- A generic Queue ---*/ |
| 548 | /*------------------------------------------------------------*/ |
| 549 | |
| 550 | typedef |
| 551 | struct { |
| 552 | UInt head; // Index of first entry |
| 553 | UInt tail; // Index of final+1 entry, ie. next free slot |
| 554 | UInt max_elems; |
| 555 | void** elems; |
| 556 | } |
| 557 | Queue; |
| 558 | |
| 559 | static Queue* construct_queue(UInt size) |
| 560 | { |
| 561 | UInt i; |
| 562 | Queue* q = VG_(malloc)(sizeof(Queue)); |
| 563 | q->head = 0; |
| 564 | q->tail = 0; |
| 565 | q->max_elems = size; |
| 566 | q->elems = VG_(malloc)(size * sizeof(void*)); |
| 567 | for (i = 0; i < size; i++) |
| 568 | q->elems[i] = NULL; |
| 569 | |
| 570 | return q; |
| 571 | } |
| 572 | |
| 573 | static void destruct_queue(Queue* q) |
| 574 | { |
| 575 | VG_(free)(q->elems); |
| 576 | VG_(free)(q); |
| 577 | } |
| 578 | |
| 579 | static void shuffle(Queue* dest_q, void** old_elems) |
| 580 | { |
| 581 | UInt i, j; |
| 582 | for (i = 0, j = dest_q->head; j < dest_q->tail; i++, j++) |
| 583 | dest_q->elems[i] = old_elems[j]; |
| 584 | dest_q->head = 0; |
| 585 | dest_q->tail = i; |
| 586 | for ( ; i < dest_q->max_elems; i++) |
| 587 | dest_q->elems[i] = NULL; // paranoia |
| 588 | } |
| 589 | |
| 590 | // Shuffles elements down. If not enough slots free, increase size. (We |
| 591 | // don't wait until we've completely run out of space, because there could |
| 592 | // be lots of shuffling just before that point which would be slow.) |
| 593 | static void adjust(Queue* q) |
| 594 | { |
| 595 | void** old_elems; |
| 596 | |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 597 | tl_assert(q->tail == q->max_elems); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 598 | if (q->head < 10) { |
| 599 | old_elems = q->elems; |
| 600 | q->max_elems *= 2; |
| 601 | q->elems = VG_(malloc)(q->max_elems * sizeof(void*)); |
| 602 | shuffle(q, old_elems); |
| 603 | VG_(free)(old_elems); |
| 604 | } else { |
| 605 | shuffle(q, q->elems); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | static void enqueue(Queue* q, void* elem) |
| 610 | { |
| 611 | if (q->tail == q->max_elems) |
| 612 | adjust(q); |
| 613 | q->elems[q->tail++] = elem; |
| 614 | } |
| 615 | |
| 616 | static Bool is_empty_queue(Queue* q) |
| 617 | { |
| 618 | return (q->head == q->tail); |
| 619 | } |
| 620 | |
| 621 | static void* dequeue(Queue* q) |
| 622 | { |
| 623 | if (is_empty_queue(q)) |
| 624 | return NULL; // Queue empty |
| 625 | else |
| 626 | return q->elems[q->head++]; |
| 627 | } |
| 628 | |
| 629 | /*------------------------------------------------------------*/ |
| 630 | /*--- malloc() et al replacement wrappers ---*/ |
| 631 | /*------------------------------------------------------------*/ |
| 632 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 633 | // Forward declaration |
| 634 | static void hp_census(void); |
| 635 | |
nethercote | 159dfef | 2004-09-13 13:27:30 +0000 | [diff] [blame] | 636 | static |
njn | 5773590 | 2004-11-25 18:04:54 +0000 | [diff] [blame] | 637 | void* new_block ( ThreadId tid, void* p, SizeT size, SizeT align, |
| 638 | Bool is_zeroed ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 639 | { |
| 640 | HP_Chunk* hc; |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 641 | Bool custom_alloc = (NULL == p); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 642 | if (size < 0) return NULL; |
| 643 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 644 | // Update statistics |
| 645 | n_allocs++; |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 646 | if (0 == size) n_zero_allocs++; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 647 | |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 648 | // Allocate and zero if necessary |
| 649 | if (!p) { |
| 650 | p = VG_(cli_malloc)( align, size ); |
| 651 | if (!p) { |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 652 | return NULL; |
| 653 | } |
| 654 | if (is_zeroed) VG_(memset)(p, 0, size); |
| 655 | } |
| 656 | |
njn | f1c5def | 2005-08-11 02:17:07 +0000 | [diff] [blame] | 657 | // Make new HP_Chunk node, add to malloc_list |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 658 | hc = VG_(malloc)(sizeof(HP_Chunk)); |
| 659 | hc->size = size; |
| 660 | hc->data = (Addr)p; |
| 661 | hc->where = NULL; // paranoia |
| 662 | if (clo_heap) { |
njn | 5773590 | 2004-11-25 18:04:54 +0000 | [diff] [blame] | 663 | hc->where = get_XCon( tid, custom_alloc ); |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 664 | if (0 != size) |
| 665 | update_XCon(hc->where, size); |
| 666 | } |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 667 | VG_(HT_add_node)(malloc_list, hc); |
njn | f1c5def | 2005-08-11 02:17:07 +0000 | [diff] [blame] | 668 | n_heap_blocks++; |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 669 | |
| 670 | // do a census! |
| 671 | hp_census(); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 672 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 673 | return p; |
| 674 | } |
| 675 | |
| 676 | static __inline__ |
| 677 | void die_block ( void* p, Bool custom_free ) |
| 678 | { |
njn | f1c5def | 2005-08-11 02:17:07 +0000 | [diff] [blame] | 679 | HP_Chunk* hc; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 680 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 681 | // Update statistics |
| 682 | n_frees++; |
| 683 | |
njn | f1c5def | 2005-08-11 02:17:07 +0000 | [diff] [blame] | 684 | // Remove HP_Chunk from malloc_list |
njn | 9a46324 | 2005-08-16 03:29:50 +0000 | [diff] [blame] | 685 | hc = VG_(HT_remove)(malloc_list, (UWord)p); |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 686 | if (NULL == hc) |
| 687 | return; // must have been a bogus free() |
| 688 | tl_assert(n_heap_blocks > 0); |
| 689 | n_heap_blocks--; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 690 | |
| 691 | if (clo_heap && hc->size != 0) |
| 692 | update_XCon(hc->where, -hc->size); |
| 693 | |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 694 | VG_(free)( hc ); |
| 695 | |
| 696 | // Actually free the heap block, if necessary |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 697 | if (!custom_free) |
| 698 | VG_(cli_free)( p ); |
| 699 | |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 700 | // do a census! |
| 701 | hp_census(); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 705 | static void* ms_malloc ( ThreadId tid, SizeT n ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 706 | { |
njn | 5773590 | 2004-11-25 18:04:54 +0000 | [diff] [blame] | 707 | return new_block( tid, NULL, n, VG_(clo_alignment), /*is_zeroed*/False ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 708 | } |
| 709 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 710 | static void* ms___builtin_new ( ThreadId tid, SizeT n ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 711 | { |
njn | 5773590 | 2004-11-25 18:04:54 +0000 | [diff] [blame] | 712 | return new_block( tid, NULL, n, VG_(clo_alignment), /*is_zeroed*/False ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 713 | } |
| 714 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 715 | static void* ms___builtin_vec_new ( ThreadId tid, SizeT n ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 716 | { |
njn | 5773590 | 2004-11-25 18:04:54 +0000 | [diff] [blame] | 717 | return new_block( tid, NULL, n, VG_(clo_alignment), /*is_zeroed*/False ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 718 | } |
| 719 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 720 | static void* ms_calloc ( ThreadId tid, SizeT m, SizeT size ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 721 | { |
njn | 5773590 | 2004-11-25 18:04:54 +0000 | [diff] [blame] | 722 | return new_block( tid, NULL, m*size, VG_(clo_alignment), /*is_zeroed*/True ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 723 | } |
| 724 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 725 | static void *ms_memalign ( ThreadId tid, SizeT align, SizeT n ) |
fitzhardinge | 51f3ff1 | 2004-03-04 22:42:03 +0000 | [diff] [blame] | 726 | { |
njn | 5773590 | 2004-11-25 18:04:54 +0000 | [diff] [blame] | 727 | return new_block( tid, NULL, n, align, False ); |
fitzhardinge | 51f3ff1 | 2004-03-04 22:42:03 +0000 | [diff] [blame] | 728 | } |
| 729 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 730 | static void ms_free ( ThreadId tid, void* p ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 731 | { |
| 732 | die_block( p, /*custom_free*/False ); |
| 733 | } |
| 734 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 735 | static void ms___builtin_delete ( ThreadId tid, void* p ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 736 | { |
| 737 | die_block( p, /*custom_free*/False); |
| 738 | } |
| 739 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 740 | static void ms___builtin_vec_delete ( ThreadId tid, void* p ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 741 | { |
| 742 | die_block( p, /*custom_free*/False ); |
| 743 | } |
| 744 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 745 | static void* ms_realloc ( ThreadId tid, void* p_old, SizeT new_size ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 746 | { |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 747 | HP_Chunk* hc; |
| 748 | void* p_new; |
| 749 | SizeT old_size; |
| 750 | XPt *old_where, *new_where; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 751 | |
njn | a079365 | 2005-08-16 03:34:56 +0000 | [diff] [blame] | 752 | // Remove the old block |
njn | 9a46324 | 2005-08-16 03:29:50 +0000 | [diff] [blame] | 753 | hc = VG_(HT_remove)(malloc_list, (UWord)p_old); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 754 | if (hc == NULL) { |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 755 | return NULL; // must have been a bogus realloc() |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 756 | } |
| 757 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 758 | old_size = hc->size; |
| 759 | |
| 760 | if (new_size <= old_size) { |
| 761 | // new size is smaller or same; block not moved |
| 762 | p_new = p_old; |
| 763 | |
| 764 | } else { |
| 765 | // new size is bigger; make new block, copy shared contents, free old |
| 766 | p_new = VG_(cli_malloc)(VG_(clo_alignment), new_size); |
tom | 0cd42f0 | 2005-10-06 09:00:17 +0000 | [diff] [blame] | 767 | if (p_new) { |
| 768 | VG_(memcpy)(p_new, p_old, old_size); |
| 769 | VG_(cli_free)(p_old); |
| 770 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 771 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 772 | |
tom | 0cd42f0 | 2005-10-06 09:00:17 +0000 | [diff] [blame] | 773 | if (p_new) { |
| 774 | old_where = hc->where; |
| 775 | new_where = get_XCon( tid, /*custom_malloc*/False); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 776 | |
tom | 0cd42f0 | 2005-10-06 09:00:17 +0000 | [diff] [blame] | 777 | // Update HP_Chunk |
| 778 | hc->data = (Addr)p_new; |
| 779 | hc->size = new_size; |
| 780 | hc->where = new_where; |
| 781 | |
| 782 | // Update XPt curr_space fields |
| 783 | if (clo_heap) { |
| 784 | if (0 != old_size) update_XCon(old_where, -old_size); |
| 785 | if (0 != new_size) update_XCon(new_where, new_size); |
| 786 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 787 | } |
| 788 | |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 789 | // Now insert the new hc (with a possibly new 'data' field) into |
| 790 | // malloc_list. If this realloc() did not increase the memory size, we |
| 791 | // will have removed and then re-added mc unnecessarily. But that's ok |
| 792 | // because shrinking a block with realloc() is (presumably) much rarer |
| 793 | // than growing it, and this way simplifies the growing case. |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 794 | VG_(HT_add_node)(malloc_list, hc); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 795 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 796 | return p_new; |
| 797 | } |
| 798 | |
| 799 | |
| 800 | /*------------------------------------------------------------*/ |
| 801 | /*--- Taking a census ---*/ |
| 802 | /*------------------------------------------------------------*/ |
| 803 | |
| 804 | static Census censi[MAX_N_CENSI]; |
| 805 | static UInt curr_census = 0; |
| 806 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 807 | static UInt get_xtree_size(XPt* xpt, UInt ix) |
| 808 | { |
| 809 | UInt i; |
| 810 | |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 811 | // If no memory allocated at all, nothing interesting to record. |
| 812 | if (alloc_xpt->curr_space == 0) return 0; |
| 813 | |
| 814 | // Ignore sub-XTrees that account for a miniscule fraction of current |
| 815 | // allocated space. |
| 816 | if (xpt->curr_space / (double)alloc_xpt->curr_space > 0.002) { |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 817 | ix++; |
| 818 | |
| 819 | // Count all (non-zero) descendent XPts |
| 820 | for (i = 0; i < xpt->n_children; i++) |
| 821 | ix = get_xtree_size(xpt->children[i], ix); |
| 822 | } |
| 823 | return ix; |
| 824 | } |
| 825 | |
| 826 | static |
| 827 | UInt do_space_snapshot(XPt xpt[], XTreeSnapshot xtree_snapshot, UInt ix) |
| 828 | { |
| 829 | UInt i; |
| 830 | |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 831 | // Structure of this function mirrors that of get_xtree_size(). |
| 832 | |
| 833 | if (alloc_xpt->curr_space == 0) return 0; |
| 834 | |
| 835 | if (xpt->curr_space / (double)alloc_xpt->curr_space > 0.002) { |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 836 | xtree_snapshot[ix].xpt = xpt; |
| 837 | xtree_snapshot[ix].space = xpt->curr_space; |
| 838 | ix++; |
| 839 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 840 | for (i = 0; i < xpt->n_children; i++) |
| 841 | ix = do_space_snapshot(xpt->children[i], xtree_snapshot, ix); |
| 842 | } |
| 843 | return ix; |
| 844 | } |
| 845 | |
| 846 | static UInt ms_interval; |
| 847 | static UInt do_every_nth_census = 30; |
| 848 | |
| 849 | // Weed out half the censi; we choose those that represent the smallest |
| 850 | // time-spans, because that loses the least information. |
| 851 | // |
| 852 | // Algorithm for N censi: We find the census representing the smallest |
| 853 | // timeframe, and remove it. We repeat this until (N/2)-1 censi are gone. |
| 854 | // (It's (N/2)-1 because we never remove the first and last censi.) |
| 855 | // We have to do this one census at a time, rather than finding the (N/2)-1 |
| 856 | // smallest censi in one hit, because when a census is removed, it's |
| 857 | // neighbours immediately cover greater timespans. So it's N^2, but N only |
| 858 | // equals 200, and this is only done every 100 censi, which is not too often. |
| 859 | static void halve_censi(void) |
| 860 | { |
| 861 | Int i, jp, j, jn, k; |
| 862 | Census* min_census; |
| 863 | |
| 864 | n_halvings++; |
| 865 | if (VG_(clo_verbosity) > 1) |
| 866 | VG_(message)(Vg_UserMsg, "Halving censi..."); |
| 867 | |
| 868 | // Sets j to the index of the first not-yet-removed census at or after i |
| 869 | #define FIND_CENSUS(i, j) \ |
njn | 6f1f76d | 2005-05-24 21:28:54 +0000 | [diff] [blame] | 870 | for (j = i; j < MAX_N_CENSI && -1 == censi[j].ms_time; j++) { } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 871 | |
| 872 | for (i = 2; i < MAX_N_CENSI; i += 2) { |
| 873 | // Find the censi representing the smallest timespan. The timespan |
| 874 | // for census n = d(N-1,N)+d(N,N+1), where d(A,B) is the time between |
| 875 | // censi A and B. We don't consider the first and last censi for |
| 876 | // removal. |
| 877 | Int min_span = 0x7fffffff; |
| 878 | Int min_j = 0; |
| 879 | |
| 880 | // Initial triple: (prev, curr, next) == (jp, j, jn) |
| 881 | jp = 0; |
| 882 | FIND_CENSUS(1, j); |
| 883 | FIND_CENSUS(j+1, jn); |
| 884 | while (jn < MAX_N_CENSI) { |
| 885 | Int timespan = censi[jn].ms_time - censi[jp].ms_time; |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 886 | tl_assert(timespan >= 0); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 887 | if (timespan < min_span) { |
| 888 | min_span = timespan; |
| 889 | min_j = j; |
| 890 | } |
| 891 | // Move on to next triple |
| 892 | jp = j; |
| 893 | j = jn; |
| 894 | FIND_CENSUS(jn+1, jn); |
| 895 | } |
| 896 | // We've found the least important census, now remove it |
| 897 | min_census = & censi[ min_j ]; |
| 898 | for (k = 0; NULL != min_census->xtree_snapshots[k]; k++) { |
| 899 | n_snapshot_frees++; |
| 900 | VG_(free)(min_census->xtree_snapshots[k]); |
| 901 | min_census->xtree_snapshots[k] = NULL; |
| 902 | } |
| 903 | min_census->ms_time = -1; |
| 904 | } |
| 905 | |
| 906 | // Slide down the remaining censi over the removed ones. The '<=' is |
| 907 | // because we are removing on (N/2)-1, rather than N/2. |
| 908 | for (i = 0, j = 0; i <= MAX_N_CENSI / 2; i++, j++) { |
| 909 | FIND_CENSUS(j, j); |
| 910 | if (i != j) { |
| 911 | censi[i] = censi[j]; |
| 912 | } |
| 913 | } |
| 914 | curr_census = i; |
| 915 | |
| 916 | // Double intervals |
| 917 | ms_interval *= 2; |
| 918 | do_every_nth_census *= 2; |
| 919 | |
| 920 | if (VG_(clo_verbosity) > 1) |
| 921 | VG_(message)(Vg_UserMsg, "...done"); |
| 922 | } |
| 923 | |
| 924 | // Take a census. Census time seems to be insignificant (usually <= 0 ms, |
| 925 | // almost always <= 1ms) so don't have to worry about subtracting it from |
| 926 | // running time in any way. |
| 927 | // |
| 928 | // XXX: NOT TRUE! with bigger depths, konqueror censuses can easily take |
| 929 | // 50ms! |
| 930 | static void hp_census(void) |
| 931 | { |
| 932 | static UInt ms_prev_census = 0; |
| 933 | static UInt ms_next_census = 0; // zero allows startup census |
| 934 | |
| 935 | Int ms_time, ms_time_since_prev; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 936 | Census* census; |
| 937 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 938 | // Only do a census if it's time |
| 939 | ms_time = VG_(read_millisecond_timer)(); |
| 940 | ms_time_since_prev = ms_time - ms_prev_census; |
| 941 | if (ms_time < ms_next_census) { |
| 942 | n_fake_censi++; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 943 | return; |
| 944 | } |
| 945 | n_real_censi++; |
| 946 | |
| 947 | census = & censi[curr_census]; |
| 948 | |
| 949 | census->ms_time = ms_time; |
| 950 | |
| 951 | // Heap: snapshot the K most significant XTrees ------------------- |
| 952 | if (clo_heap) { |
njn | 6f1f76d | 2005-05-24 21:28:54 +0000 | [diff] [blame] | 953 | Int i, K; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 954 | K = ( alloc_xpt->n_children < MAX_SNAPSHOTS |
| 955 | ? alloc_xpt->n_children |
| 956 | : MAX_SNAPSHOTS); // max out |
| 957 | |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 958 | // Update .approx_ST field (approximatively) for all top-XPts. |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 959 | // We *do not* do it for any non-top-XPTs. |
| 960 | for (i = 0; i < alloc_xpt->n_children; i++) { |
| 961 | XPt* top_XPt = alloc_xpt->children[i]; |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 962 | top_XPt->approx_ST += top_XPt->curr_space * ms_time_since_prev; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 963 | } |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 964 | // Sort top-XPts by approx_ST field. |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 965 | VG_(ssort)(alloc_xpt->children, alloc_xpt->n_children, sizeof(XPt*), |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 966 | XPt_cmp_approx_ST); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 967 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 968 | // For each significant top-level XPt, record space info about its |
| 969 | // entire XTree, in a single census entry. |
| 970 | // Nb: the xtree_size count/snapshot buffer allocation, and the actual |
| 971 | // snapshot, take similar amounts of time (measured with the |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 972 | // millisecond counter). |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 973 | for (i = 0; i < K; i++) { |
| 974 | UInt xtree_size, xtree_size2; |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 975 | // VG_(printf)("%7u ", alloc_xpt->children[i]->approx_ST); |
| 976 | // Count how many XPts are in the XTree |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 977 | xtree_size = get_xtree_size( alloc_xpt->children[i], 0 ); |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 978 | |
| 979 | // If no XPts counted (ie. alloc_xpt.curr_space==0 or XTree |
| 980 | // insignificant) then don't take any more snapshots. |
| 981 | if (0 == xtree_size) break; |
| 982 | |
| 983 | // Make array of the appropriate size (+1 for zero termination, |
| 984 | // which calloc() does for us). |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 985 | census->xtree_snapshots[i] = |
| 986 | VG_(calloc)(xtree_size+1, sizeof(XPtSnapshot)); |
jseward | 612e836 | 2004-03-07 10:23:20 +0000 | [diff] [blame] | 987 | if (0 && VG_(clo_verbosity) > 1) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 988 | VG_(printf)("calloc: %d (%d B)\n", xtree_size+1, |
| 989 | (xtree_size+1) * sizeof(XPtSnapshot)); |
| 990 | |
| 991 | // Take space-snapshot: copy 'curr_space' for every XPt in the |
| 992 | // XTree into the snapshot array, along with pointers to the XPts. |
| 993 | // (Except for ones with curr_space==0, which wouldn't contribute |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 994 | // to the final exact_ST_dbld calculation anyway; excluding them |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 995 | // saves a lot of memory and up to 40% time with big --depth valus. |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 996 | xtree_size2 = do_space_snapshot(alloc_xpt->children[i], |
| 997 | census->xtree_snapshots[i], 0); |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 998 | tl_assert(xtree_size == xtree_size2); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 999 | } |
| 1000 | // VG_(printf)("\n\n"); |
| 1001 | // Zero-terminate 'xtree_snapshot' array |
| 1002 | census->xtree_snapshots[i] = NULL; |
| 1003 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1004 | //VG_(printf)("printed %d censi\n", K); |
| 1005 | |
| 1006 | // Lump the rest into a single "others" entry. |
| 1007 | census->others_space = 0; |
| 1008 | for (i = K; i < alloc_xpt->n_children; i++) { |
| 1009 | census->others_space += alloc_xpt->children[i]->curr_space; |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | // Heap admin ------------------------------------------------------- |
| 1014 | if (clo_heap_admin > 0) |
| 1015 | census->heap_admin_space = clo_heap_admin * n_heap_blocks; |
| 1016 | |
| 1017 | // Stack(s) --------------------------------------------------------- |
| 1018 | if (clo_stacks) { |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 1019 | ThreadId tid; |
| 1020 | Addr stack_min, stack_max; |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 1021 | census->stacks_space = sigstacks_space; |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 1022 | VG_(thread_stack_reset_iter)(); |
| 1023 | while ( VG_(thread_stack_next)(&tid, &stack_min, &stack_max) ) { |
| 1024 | census->stacks_space += (stack_max - stack_min); |
| 1025 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
| 1028 | // Finish, update interval if necessary ----------------------------- |
| 1029 | curr_census++; |
| 1030 | census = NULL; // don't use again now that curr_census changed |
| 1031 | |
| 1032 | // Halve the entries, if our census table is full |
| 1033 | if (MAX_N_CENSI == curr_census) { |
| 1034 | halve_censi(); |
| 1035 | } |
| 1036 | |
| 1037 | // Take time for next census from now, rather than when this census |
| 1038 | // should have happened. Because, if there's a big gap due to a kernel |
| 1039 | // operation, there's no point doing catch-up censi every BB for a while |
| 1040 | // -- that would just give N censi at almost the same time. |
| 1041 | if (VG_(clo_verbosity) > 1) { |
| 1042 | VG_(message)(Vg_UserMsg, "census: %d ms (took %d ms)", ms_time, |
| 1043 | VG_(read_millisecond_timer)() - ms_time ); |
| 1044 | } |
| 1045 | ms_prev_census = ms_time; |
| 1046 | ms_next_census = ms_time + ms_interval; |
| 1047 | //ms_next_census += ms_interval; |
| 1048 | |
| 1049 | //VG_(printf)("Next: %d ms\n", ms_next_census); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | /*------------------------------------------------------------*/ |
| 1053 | /*--- Tracked events ---*/ |
| 1054 | /*------------------------------------------------------------*/ |
| 1055 | |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 1056 | static void new_mem_stack_signal(Addr a, SizeT len) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1057 | { |
| 1058 | sigstacks_space += len; |
| 1059 | } |
| 1060 | |
nethercote | 8b5f40c | 2004-11-02 13:29:50 +0000 | [diff] [blame] | 1061 | static void die_mem_stack_signal(Addr a, SizeT len) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1062 | { |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 1063 | tl_assert(sigstacks_space >= len); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1064 | sigstacks_space -= len; |
| 1065 | } |
| 1066 | |
| 1067 | /*------------------------------------------------------------*/ |
| 1068 | /*--- Client Requests ---*/ |
| 1069 | /*------------------------------------------------------------*/ |
| 1070 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1071 | static Bool ms_handle_client_request ( ThreadId tid, UWord* argv, UWord* ret ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1072 | { |
| 1073 | switch (argv[0]) { |
| 1074 | case VG_USERREQ__MALLOCLIKE_BLOCK: { |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 1075 | void* res; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1076 | void* p = (void*)argv[1]; |
nethercote | d1b64b2 | 2004-11-04 18:22:28 +0000 | [diff] [blame] | 1077 | SizeT sizeB = argv[2]; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1078 | *ret = 0; |
njn | 5773590 | 2004-11-25 18:04:54 +0000 | [diff] [blame] | 1079 | res = new_block( tid, p, sizeB, /*align--ignored*/0, /*is_zeroed*/False ); |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 1080 | tl_assert(res == p); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1081 | return True; |
| 1082 | } |
| 1083 | case VG_USERREQ__FREELIKE_BLOCK: { |
| 1084 | void* p = (void*)argv[1]; |
| 1085 | *ret = 0; |
| 1086 | die_block( p, /*custom_free*/True ); |
| 1087 | return True; |
| 1088 | } |
| 1089 | default: |
| 1090 | *ret = 0; |
| 1091 | return False; |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | /*------------------------------------------------------------*/ |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1096 | /*--- Instrumentation ---*/ |
| 1097 | /*------------------------------------------------------------*/ |
| 1098 | |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 1099 | static |
sewardj | 0b9d74a | 2006-12-24 02:24:11 +0000 | [diff] [blame] | 1100 | IRSB* ms_instrument ( VgCallbackClosure* closure, |
| 1101 | IRSB* bb_in, |
sewardj | 461df9c | 2006-01-17 02:06:39 +0000 | [diff] [blame] | 1102 | VexGuestLayout* layout, |
| 1103 | VexGuestExtents* vge, |
sewardj | 4ba057c | 2005-10-18 12:04:18 +0000 | [diff] [blame] | 1104 | IRType gWordTy, IRType hWordTy ) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1105 | { |
sewardj | d54babf | 2005-03-21 00:55:49 +0000 | [diff] [blame] | 1106 | /* XXX Will Massif work when gWordTy != hWordTy ? */ |
njn | ee8a586 | 2004-11-22 21:08:46 +0000 | [diff] [blame] | 1107 | return bb_in; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | /*------------------------------------------------------------*/ |
| 1111 | /*--- Spacetime recomputation ---*/ |
| 1112 | /*------------------------------------------------------------*/ |
| 1113 | |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1114 | // Although we've been calculating space-time along the way, because the |
| 1115 | // earlier calculations were done at a finer timescale, the .approx_ST field |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1116 | // might not agree with what hp2ps sees, because we've thrown away some of |
| 1117 | // the information. So recompute it at the scale that hp2ps sees, so we can |
| 1118 | // confidently determine which contexts hp2ps will choose for displaying as |
| 1119 | // distinct bands. This recomputation only happens to the significant ones |
| 1120 | // that get printed in the .hp file, so it's cheap. |
| 1121 | // |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1122 | // The approx_ST calculation: |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1123 | // ( a[0]*d(0,1) + a[1]*(d(0,1) + d(1,2)) + ... + a[N-1]*d(N-2,N-1) ) / 2 |
| 1124 | // where |
| 1125 | // a[N] is the space at census N |
| 1126 | // d(A,B) is the time interval between censi A and B |
| 1127 | // and |
| 1128 | // d(A,B) + d(B,C) == d(A,C) |
| 1129 | // |
| 1130 | // Key point: we can calculate the area for a census without knowing the |
| 1131 | // previous or subsequent censi's space; because any over/underestimates |
| 1132 | // for this census will be reversed in the next, balancing out. This is |
| 1133 | // important, as getting the previous/next census entry for a particular |
| 1134 | // AP is a pain with this data structure, but getting the prev/next |
| 1135 | // census time is easy. |
| 1136 | // |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1137 | // Each heap calculation gets added to its context's exact_ST_dbld field. |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1138 | // The ULong* values are all running totals, hence the use of "+=" everywhere. |
| 1139 | |
| 1140 | // This does the calculations for a single census. |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1141 | static void calc_exact_ST_dbld2(Census* census, UInt d_t1_t2, |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1142 | ULong* twice_heap_ST, |
| 1143 | ULong* twice_heap_admin_ST, |
| 1144 | ULong* twice_stack_ST) |
| 1145 | { |
| 1146 | UInt i, j; |
| 1147 | XPtSnapshot* xpt_snapshot; |
| 1148 | |
| 1149 | // Heap -------------------------------------------------------- |
| 1150 | if (clo_heap) { |
| 1151 | for (i = 0; NULL != census->xtree_snapshots[i]; i++) { |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1152 | // Compute total heap exact_ST_dbld for the entire XTree using only |
| 1153 | // the top-XPt (the first XPt in xtree_snapshot). |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1154 | *twice_heap_ST += d_t1_t2 * census->xtree_snapshots[i][0].space; |
| 1155 | |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1156 | // Increment exact_ST_dbld for every XPt in xtree_snapshot (inc. |
| 1157 | // top one) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1158 | for (j = 0; NULL != census->xtree_snapshots[i][j].xpt; j++) { |
| 1159 | xpt_snapshot = & census->xtree_snapshots[i][j]; |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1160 | xpt_snapshot->xpt->exact_ST_dbld += d_t1_t2 * xpt_snapshot->space; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | *twice_heap_ST += d_t1_t2 * census->others_space; |
| 1164 | } |
| 1165 | |
| 1166 | // Heap admin -------------------------------------------------- |
| 1167 | if (clo_heap_admin > 0) |
| 1168 | *twice_heap_admin_ST += d_t1_t2 * census->heap_admin_space; |
| 1169 | |
| 1170 | // Stack(s) ---------------------------------------------------- |
| 1171 | if (clo_stacks) |
| 1172 | *twice_stack_ST += d_t1_t2 * census->stacks_space; |
| 1173 | } |
| 1174 | |
| 1175 | // This does the calculations for all censi. |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1176 | static void calc_exact_ST_dbld(ULong* heap2, ULong* heap_admin2, ULong* stack2) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1177 | { |
| 1178 | UInt i, N = curr_census; |
| 1179 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1180 | *heap2 = 0; |
| 1181 | *heap_admin2 = 0; |
| 1182 | *stack2 = 0; |
| 1183 | |
| 1184 | if (N <= 1) |
| 1185 | return; |
| 1186 | |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1187 | calc_exact_ST_dbld2( &censi[0], censi[1].ms_time - censi[0].ms_time, |
| 1188 | heap2, heap_admin2, stack2 ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1189 | |
| 1190 | for (i = 1; i <= N-2; i++) { |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1191 | calc_exact_ST_dbld2( & censi[i], censi[i+1].ms_time - censi[i-1].ms_time, |
| 1192 | heap2, heap_admin2, stack2 ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1195 | calc_exact_ST_dbld2( & censi[N-1], censi[N-1].ms_time - censi[N-2].ms_time, |
| 1196 | heap2, heap_admin2, stack2 ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1197 | // Now get rid of the halves. May lose a 0.5 on each, doesn't matter. |
| 1198 | *heap2 /= 2; |
| 1199 | *heap_admin2 /= 2; |
| 1200 | *stack2 /= 2; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | /*------------------------------------------------------------*/ |
| 1204 | /*--- Writing the graph file ---*/ |
| 1205 | /*------------------------------------------------------------*/ |
| 1206 | |
| 1207 | static Char* make_filename(Char* dir, Char* suffix) |
| 1208 | { |
| 1209 | Char* filename; |
| 1210 | |
| 1211 | /* Block is big enough for dir name + massif.<pid>.<suffix> */ |
| 1212 | filename = VG_(malloc)((VG_(strlen)(dir) + 32)*sizeof(Char)); |
| 1213 | VG_(sprintf)(filename, "%s/massif.%d%s", dir, VG_(getpid)(), suffix); |
| 1214 | |
| 1215 | return filename; |
| 1216 | } |
| 1217 | |
| 1218 | // Make string acceptable to hp2ps (sigh): remove spaces, escape parentheses. |
| 1219 | static Char* clean_fnname(Char *d, Char* s) |
| 1220 | { |
| 1221 | Char* dorig = d; |
| 1222 | while (*s) { |
| 1223 | if (' ' == *s) { *d = '%'; } |
| 1224 | else if ('(' == *s) { *d++ = '\\'; *d = '('; } |
| 1225 | else if (')' == *s) { *d++ = '\\'; *d = ')'; } |
| 1226 | else { *d = *s; }; |
| 1227 | s++; |
| 1228 | d++; |
| 1229 | } |
| 1230 | *d = '\0'; |
| 1231 | return dorig; |
| 1232 | } |
| 1233 | |
| 1234 | static void file_err ( Char* file ) |
| 1235 | { |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 1236 | VG_(message)(Vg_UserMsg, "error: can't open output file '%s'", file ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1237 | VG_(message)(Vg_UserMsg, " ... so profile results will be missing."); |
| 1238 | } |
| 1239 | |
| 1240 | /* Format, by example: |
| 1241 | |
| 1242 | JOB "a.out -p" |
| 1243 | DATE "Fri Apr 17 11:43:45 1992" |
| 1244 | SAMPLE_UNIT "seconds" |
| 1245 | VALUE_UNIT "bytes" |
| 1246 | BEGIN_SAMPLE 0.00 |
| 1247 | SYSTEM 24 |
| 1248 | END_SAMPLE 0.00 |
| 1249 | BEGIN_SAMPLE 1.00 |
| 1250 | elim 180 |
| 1251 | insert 24 |
| 1252 | intersect 12 |
| 1253 | disin 60 |
| 1254 | main 12 |
| 1255 | reduce 20 |
| 1256 | SYSTEM 12 |
| 1257 | END_SAMPLE 1.00 |
| 1258 | MARK 1.50 |
| 1259 | MARK 1.75 |
| 1260 | MARK 1.80 |
| 1261 | BEGIN_SAMPLE 2.00 |
| 1262 | elim 192 |
| 1263 | insert 24 |
| 1264 | intersect 12 |
| 1265 | disin 84 |
| 1266 | main 12 |
| 1267 | SYSTEM 24 |
| 1268 | END_SAMPLE 2.00 |
| 1269 | BEGIN_SAMPLE 2.82 |
| 1270 | END_SAMPLE 2.82 |
| 1271 | */ |
| 1272 | static void write_hp_file(void) |
| 1273 | { |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1274 | Int i, j; |
| 1275 | Int fd, res; |
| 1276 | SysRes sres; |
| 1277 | Char *hp_file, *ps_file, *aux_file; |
| 1278 | Char* cmdfmt; |
| 1279 | Char* cmdbuf; |
| 1280 | Int cmdlen; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1281 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1282 | // Open file |
| 1283 | hp_file = make_filename( base_dir, ".hp" ); |
| 1284 | ps_file = make_filename( base_dir, ".ps" ); |
| 1285 | aux_file = make_filename( base_dir, ".aux" ); |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1286 | sres = VG_(open)(hp_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY, |
| 1287 | VKI_S_IRUSR|VKI_S_IWUSR); |
| 1288 | if (sres.isError) { |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1289 | file_err( hp_file ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1290 | return; |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1291 | } else { |
sewardj | e808930 | 2006-10-17 02:15:17 +0000 | [diff] [blame] | 1292 | fd = sres.res; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | // File header, including command line |
| 1296 | SPRINTF(buf, "JOB \""); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1297 | if (VG_(args_the_exename)) { |
| 1298 | SPRINTF(buf, "%s", VG_(args_the_exename)); |
| 1299 | } |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1300 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) { |
| 1301 | HChar* arg = * (HChar**) VG_(indexXA)( VG_(args_for_client), i ); |
| 1302 | if (arg) |
| 1303 | SPRINTF(buf, " %s", arg); |
njn | d111d10 | 2005-09-13 00:46:27 +0000 | [diff] [blame] | 1304 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1305 | SPRINTF(buf, /*" (%d ms/sample)\"\n"*/ "\"\n" |
| 1306 | "DATE \"\"\n" |
| 1307 | "SAMPLE_UNIT \"ms\"\n" |
| 1308 | "VALUE_UNIT \"bytes\"\n", ms_interval); |
| 1309 | |
| 1310 | // Censi |
| 1311 | for (i = 0; i < curr_census; i++) { |
| 1312 | Census* census = & censi[i]; |
| 1313 | |
| 1314 | // Census start |
| 1315 | SPRINTF(buf, "MARK %d.0\n" |
| 1316 | "BEGIN_SAMPLE %d.0\n", |
| 1317 | census->ms_time, census->ms_time); |
| 1318 | |
| 1319 | // Heap ----------------------------------------------------------- |
| 1320 | if (clo_heap) { |
| 1321 | // Print all the significant XPts from that census |
| 1322 | for (j = 0; NULL != census->xtree_snapshots[j]; j++) { |
| 1323 | // Grab the jth top-XPt |
| 1324 | XTreeSnapshot xtree_snapshot = & census->xtree_snapshots[j][0]; |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 1325 | if ( ! VG_(get_fnname)(xtree_snapshot->xpt->ip, buf2, 16)) { |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1326 | VG_(sprintf)(buf2, "???"); |
| 1327 | } |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 1328 | SPRINTF(buf, "x%x:%s %d\n", xtree_snapshot->xpt->ip, |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1329 | clean_fnname(buf3, buf2), xtree_snapshot->space); |
| 1330 | } |
| 1331 | |
| 1332 | // Remaining heap block alloc points, combined |
| 1333 | if (census->others_space > 0) |
| 1334 | SPRINTF(buf, "other %d\n", census->others_space); |
| 1335 | } |
| 1336 | |
| 1337 | // Heap admin ----------------------------------------------------- |
| 1338 | if (clo_heap_admin > 0 && census->heap_admin_space) |
| 1339 | SPRINTF(buf, "heap-admin %d\n", census->heap_admin_space); |
| 1340 | |
| 1341 | // Stack(s) ------------------------------------------------------- |
| 1342 | if (clo_stacks) |
| 1343 | SPRINTF(buf, "stack(s) %d\n", census->stacks_space); |
| 1344 | |
| 1345 | // Census end |
| 1346 | SPRINTF(buf, "END_SAMPLE %d.0\n", census->ms_time); |
| 1347 | } |
| 1348 | |
| 1349 | // Close file |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 1350 | tl_assert(fd >= 0); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1351 | VG_(close)(fd); |
| 1352 | |
| 1353 | // Attempt to convert file using hp2ps |
| 1354 | cmdfmt = "%s/hp2ps -c -t1 %s"; |
| 1355 | cmdlen = VG_(strlen)(VG_(libdir)) + VG_(strlen)(hp_file) |
| 1356 | + VG_(strlen)(cmdfmt); |
| 1357 | cmdbuf = VG_(malloc)( sizeof(Char) * cmdlen ); |
| 1358 | VG_(sprintf)(cmdbuf, cmdfmt, VG_(libdir), hp_file); |
| 1359 | res = VG_(system)(cmdbuf); |
| 1360 | VG_(free)(cmdbuf); |
| 1361 | if (res != 0) { |
| 1362 | VG_(message)(Vg_UserMsg, |
| 1363 | "Conversion to PostScript failed. Try converting manually."); |
| 1364 | } else { |
| 1365 | // remove the .hp and .aux file |
| 1366 | VG_(unlink)(hp_file); |
| 1367 | VG_(unlink)(aux_file); |
| 1368 | } |
| 1369 | |
| 1370 | VG_(free)(hp_file); |
| 1371 | VG_(free)(ps_file); |
| 1372 | VG_(free)(aux_file); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | /*------------------------------------------------------------*/ |
| 1376 | /*--- Writing the XPt text/HTML file ---*/ |
| 1377 | /*------------------------------------------------------------*/ |
| 1378 | |
| 1379 | static void percentify(Int n, Int pow, Int field_width, char xbuf[]) |
| 1380 | { |
| 1381 | int i, len, space; |
| 1382 | |
| 1383 | VG_(sprintf)(xbuf, "%d.%d%%", n / pow, n % pow); |
| 1384 | len = VG_(strlen)(xbuf); |
| 1385 | space = field_width - len; |
| 1386 | if (space < 0) space = 0; /* Allow for v. small field_width */ |
| 1387 | i = len; |
| 1388 | |
| 1389 | /* Right justify in field */ |
| 1390 | for ( ; i >= 0; i--) xbuf[i + space] = xbuf[i]; |
| 1391 | for (i = 0; i < space; i++) xbuf[i] = ' '; |
| 1392 | } |
| 1393 | |
| 1394 | // Nb: uses a static buffer, each call trashes the last string returned. |
| 1395 | static Char* make_perc(ULong spacetime, ULong total_spacetime) |
| 1396 | { |
| 1397 | static Char mbuf[32]; |
| 1398 | |
| 1399 | UInt p = 10; |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 1400 | tl_assert(0 != total_spacetime); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1401 | percentify(spacetime * 100 * p / total_spacetime, p, 5, mbuf); |
| 1402 | return mbuf; |
| 1403 | } |
| 1404 | |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 1405 | // Nb: passed in XPt is a lower-level XPt; IPs are grabbed from |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1406 | // bottom-to-top of XCon, and then printed in the reverse order. |
| 1407 | static UInt pp_XCon(Int fd, XPt* xpt) |
| 1408 | { |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 1409 | Addr rev_ips[clo_depth+1]; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1410 | Int i = 0; |
| 1411 | Int n = 0; |
| 1412 | Bool is_HTML = ( XHTML == clo_format ); |
| 1413 | Char* maybe_br = ( is_HTML ? "<br>" : "" ); |
| 1414 | Char* maybe_indent = ( is_HTML ? " " : "" ); |
| 1415 | |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 1416 | tl_assert(NULL != xpt); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1417 | |
| 1418 | while (True) { |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 1419 | rev_ips[i] = xpt->ip; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1420 | n++; |
| 1421 | if (alloc_xpt == xpt->parent) break; |
| 1422 | i++; |
| 1423 | xpt = xpt->parent; |
| 1424 | } |
| 1425 | |
| 1426 | for (i = n-1; i >= 0; i--) { |
| 1427 | // -1 means point to calling line |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 1428 | VG_(describe_IP)(rev_ips[i]-1, buf2, BUF_LEN); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1429 | SPRINTF(buf, " %s%s%s\n", maybe_indent, buf2, maybe_br); |
| 1430 | } |
| 1431 | |
| 1432 | return n; |
| 1433 | } |
| 1434 | |
| 1435 | // Important point: for HTML, each XPt must be identified uniquely for the |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 1436 | // HTML links to all match up correctly. Using xpt->ip is not |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1437 | // sufficient, because function pointers mean that you can call more than |
| 1438 | // one other function from a single code location. So instead we use the |
| 1439 | // address of the xpt struct itself, which is guaranteed to be unique. |
| 1440 | |
| 1441 | static void pp_all_XPts2(Int fd, Queue* q, ULong heap_spacetime, |
| 1442 | ULong total_spacetime) |
| 1443 | { |
| 1444 | UInt i; |
| 1445 | XPt *xpt, *child; |
| 1446 | UInt L = 0; |
| 1447 | UInt c1 = 1; |
| 1448 | UInt c2 = 0; |
| 1449 | ULong sum = 0; |
| 1450 | UInt n; |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 1451 | Char *ip_desc, *perc; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1452 | Bool is_HTML = ( XHTML == clo_format ); |
| 1453 | Char* maybe_br = ( is_HTML ? "<br>" : "" ); |
| 1454 | Char* maybe_p = ( is_HTML ? "<p>" : "" ); |
| 1455 | Char* maybe_ul = ( is_HTML ? "<ul>" : "" ); |
| 1456 | Char* maybe_li = ( is_HTML ? "<li>" : "" ); |
| 1457 | Char* maybe_fli = ( is_HTML ? "</li>" : "" ); |
| 1458 | Char* maybe_ful = ( is_HTML ? "</ul>" : "" ); |
| 1459 | Char* end_hr = ( is_HTML ? "<hr>" : |
| 1460 | "=================================" ); |
| 1461 | Char* depth = ( is_HTML ? "<code>--depth</code>" : "--depth" ); |
| 1462 | |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1463 | if (total_spacetime == 0) { |
| 1464 | SPRINTF(buf, "(No heap memory allocated)\n"); |
| 1465 | return; |
| 1466 | } |
| 1467 | |
| 1468 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1469 | SPRINTF(buf, "== %d ===========================%s\n", L, maybe_br); |
| 1470 | |
| 1471 | while (NULL != (xpt = (XPt*)dequeue(q))) { |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1472 | // Check that non-top-level XPts have a zero .approx_ST field. |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 1473 | if (xpt->parent != alloc_xpt) tl_assert( 0 == xpt->approx_ST ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1474 | |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1475 | // Check that the sum of all children .exact_ST_dbld fields equals |
| 1476 | // parent's (unless alloc_xpt, when it should == 0). |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1477 | if (alloc_xpt == xpt) { |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 1478 | tl_assert(0 == xpt->exact_ST_dbld); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1479 | } else { |
| 1480 | sum = 0; |
| 1481 | for (i = 0; i < xpt->n_children; i++) { |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1482 | sum += xpt->children[i]->exact_ST_dbld; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1483 | } |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 1484 | //tl_assert(sum == xpt->exact_ST_dbld); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1485 | // It's possible that not all the children were included in the |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1486 | // exact_ST_dbld calculations. Hopefully almost all of them were, and |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1487 | // all the important ones. |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 1488 | // tl_assert(sum <= xpt->exact_ST_dbld); |
| 1489 | // tl_assert(sum * 1.05 > xpt->exact_ST_dbld ); |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1490 | // if (sum != xpt->exact_ST_dbld) { |
njn | 68e4659 | 2005-08-26 19:42:27 +0000 | [diff] [blame] | 1491 | // VG_(printf)("%lld, %lld\n", sum, xpt->exact_ST_dbld); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1492 | // } |
| 1493 | } |
| 1494 | |
| 1495 | if (xpt == alloc_xpt) { |
| 1496 | SPRINTF(buf, "Heap allocation functions accounted for " |
| 1497 | "%s of measured spacetime%s\n", |
| 1498 | make_perc(heap_spacetime, total_spacetime), maybe_br); |
| 1499 | } else { |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1500 | // Remember: exact_ST_dbld is space.time *doubled* |
| 1501 | perc = make_perc(xpt->exact_ST_dbld / 2, total_spacetime); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1502 | if (is_HTML) { |
| 1503 | SPRINTF(buf, "<a name=\"b%x\"></a>" |
| 1504 | "Context accounted for " |
| 1505 | "<a href=\"#a%x\">%s</a> of measured spacetime<br>\n", |
| 1506 | xpt, xpt, perc); |
| 1507 | } else { |
| 1508 | SPRINTF(buf, "Context accounted for %s of measured spacetime\n", |
| 1509 | perc); |
| 1510 | } |
| 1511 | n = pp_XCon(fd, xpt); |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 1512 | tl_assert(n == L); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1515 | // Sort children by exact_ST_dbld |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1516 | VG_(ssort)(xpt->children, xpt->n_children, sizeof(XPt*), |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1517 | XPt_cmp_exact_ST_dbld); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1518 | |
| 1519 | SPRINTF(buf, "%s\nCalled from:%s\n", maybe_p, maybe_ul); |
| 1520 | for (i = 0; i < xpt->n_children; i++) { |
| 1521 | child = xpt->children[i]; |
| 1522 | |
| 1523 | // Stop when <1% of total spacetime |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1524 | if (child->exact_ST_dbld * 1000 / (total_spacetime * 2) < 5) { |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1525 | UInt n_insig = xpt->n_children - i; |
| 1526 | Char* s = ( n_insig == 1 ? "" : "s" ); |
| 1527 | Char* and = ( 0 == i ? "" : "and " ); |
| 1528 | Char* other = ( 0 == i ? "" : "other " ); |
| 1529 | SPRINTF(buf, " %s%s%d %sinsignificant place%s%s\n\n", |
| 1530 | maybe_li, and, n_insig, other, s, maybe_fli); |
| 1531 | break; |
| 1532 | } |
| 1533 | |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1534 | // Remember: exact_ST_dbld is space.time *doubled* |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 1535 | perc = make_perc(child->exact_ST_dbld / 2, total_spacetime); |
| 1536 | ip_desc = VG_(describe_IP)(child->ip-1, buf2, BUF_LEN); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1537 | if (is_HTML) { |
| 1538 | SPRINTF(buf, "<li><a name=\"a%x\"></a>", child ); |
| 1539 | |
| 1540 | if (child->n_children > 0) { |
| 1541 | SPRINTF(buf, "<a href=\"#b%x\">%s</a>", child, perc); |
| 1542 | } else { |
| 1543 | SPRINTF(buf, "%s", perc); |
| 1544 | } |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 1545 | SPRINTF(buf, ": %s\n", ip_desc); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1546 | } else { |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 1547 | SPRINTF(buf, " %6s: %s\n\n", perc, ip_desc); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | if (child->n_children > 0) { |
| 1551 | enqueue(q, (void*)child); |
| 1552 | c2++; |
| 1553 | } |
| 1554 | } |
| 1555 | SPRINTF(buf, "%s%s", maybe_ful, maybe_p); |
| 1556 | c1--; |
| 1557 | |
| 1558 | // Putting markers between levels of the structure: |
| 1559 | // c1 tracks how many to go on this level, c2 tracks how many we've |
| 1560 | // queued up for the next level while finishing off this level. |
| 1561 | // When c1 gets to zero, we've changed levels, so print a marker, |
| 1562 | // move c2 into c1, and zero c2. |
| 1563 | if (0 == c1) { |
| 1564 | L++; |
| 1565 | c1 = c2; |
| 1566 | c2 = 0; |
| 1567 | if (! is_empty_queue(q) ) { // avoid empty one at end |
| 1568 | SPRINTF(buf, "== %d ===========================%s\n", L, maybe_br); |
| 1569 | } |
| 1570 | } else { |
| 1571 | SPRINTF(buf, "---------------------------------%s\n", maybe_br); |
| 1572 | } |
| 1573 | } |
| 1574 | SPRINTF(buf, "%s\n\nEnd of information. Rerun with a bigger " |
| 1575 | "%s value for more.\n", end_hr, depth); |
| 1576 | } |
| 1577 | |
| 1578 | static void pp_all_XPts(Int fd, XPt* xpt, ULong heap_spacetime, |
| 1579 | ULong total_spacetime) |
| 1580 | { |
| 1581 | Queue* q = construct_queue(100); |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1582 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1583 | enqueue(q, xpt); |
| 1584 | pp_all_XPts2(fd, q, heap_spacetime, total_spacetime); |
| 1585 | destruct_queue(q); |
| 1586 | } |
| 1587 | |
| 1588 | static void |
| 1589 | write_text_file(ULong total_ST, ULong heap_ST) |
| 1590 | { |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1591 | SysRes sres; |
| 1592 | Int fd, i; |
| 1593 | Char* text_file; |
| 1594 | Char* maybe_p = ( XHTML == clo_format ? "<p>" : "" ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1595 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1596 | // Open file |
| 1597 | text_file = make_filename( base_dir, |
| 1598 | ( XText == clo_format ? ".txt" : ".html" ) ); |
| 1599 | |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1600 | sres = VG_(open)(text_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY, |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1601 | VKI_S_IRUSR|VKI_S_IWUSR); |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1602 | if (sres.isError) { |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1603 | file_err( text_file ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1604 | return; |
sewardj | 9264559 | 2005-07-23 09:18:34 +0000 | [diff] [blame] | 1605 | } else { |
sewardj | e808930 | 2006-10-17 02:15:17 +0000 | [diff] [blame] | 1606 | fd = sres.res; |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | // Header |
| 1610 | if (XHTML == clo_format) { |
| 1611 | SPRINTF(buf, "<html>\n" |
| 1612 | "<head>\n" |
| 1613 | "<title>%s</title>\n" |
| 1614 | "</head>\n" |
| 1615 | "<body>\n", |
| 1616 | text_file); |
| 1617 | } |
| 1618 | |
| 1619 | // Command line |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1620 | SPRINTF(buf, "Command:"); |
| 1621 | if (VG_(args_the_exename)) { |
| 1622 | SPRINTF(buf, " %s", VG_(args_the_exename)); |
| 1623 | } |
sewardj | 14c7cc5 | 2007-02-25 15:08:24 +0000 | [diff] [blame] | 1624 | for (i = 0; i < VG_(sizeXA)( VG_(args_for_client) ); i++) { |
| 1625 | HChar* arg = * (HChar**) VG_(indexXA)( VG_(args_for_client), i ); |
| 1626 | if (arg) |
| 1627 | SPRINTF(buf, " %s", arg); |
njn | d111d10 | 2005-09-13 00:46:27 +0000 | [diff] [blame] | 1628 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1629 | SPRINTF(buf, "\n%s\n", maybe_p); |
| 1630 | |
| 1631 | if (clo_heap) |
| 1632 | pp_all_XPts(fd, alloc_xpt, heap_ST, total_ST); |
| 1633 | |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 1634 | tl_assert(fd >= 0); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1635 | VG_(close)(fd); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | /*------------------------------------------------------------*/ |
| 1639 | /*--- Finalisation ---*/ |
| 1640 | /*------------------------------------------------------------*/ |
| 1641 | |
| 1642 | static void |
| 1643 | print_summary(ULong total_ST, ULong heap_ST, ULong heap_admin_ST, |
| 1644 | ULong stack_ST) |
| 1645 | { |
njn | 99cb9e3 | 2005-09-25 17:59:16 +0000 | [diff] [blame] | 1646 | VG_(message)(Vg_UserMsg, "Total spacetime: %,llu ms.B", total_ST); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1647 | |
| 1648 | // Heap -------------------------------------------------------------- |
| 1649 | if (clo_heap) |
| 1650 | VG_(message)(Vg_UserMsg, "heap: %s", |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1651 | ( 0 == total_ST ? (Char*)"(n/a)" |
| 1652 | : make_perc(heap_ST, total_ST) ) ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1653 | |
| 1654 | // Heap admin -------------------------------------------------------- |
| 1655 | if (clo_heap_admin) |
| 1656 | VG_(message)(Vg_UserMsg, "heap admin: %s", |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1657 | ( 0 == total_ST ? (Char*)"(n/a)" |
| 1658 | : make_perc(heap_admin_ST, total_ST) ) ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1659 | |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 1660 | tl_assert( VG_(HT_count_nodes)(malloc_list) == n_heap_blocks ); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1661 | |
| 1662 | // Stack(s) ---------------------------------------------------------- |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1663 | if (clo_stacks) { |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1664 | VG_(message)(Vg_UserMsg, "stack(s): %s", |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1665 | ( 0 == stack_ST ? (Char*)"0%" |
| 1666 | : make_perc(stack_ST, total_ST) ) ); |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1667 | } |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1668 | |
| 1669 | if (VG_(clo_verbosity) > 1) { |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 1670 | tl_assert(n_xpts > 0); // always have alloc_xpt |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1671 | VG_(message)(Vg_DebugMsg, " allocs: %u", n_allocs); |
| 1672 | VG_(message)(Vg_DebugMsg, "zeroallocs: %u (%d%%)", n_zero_allocs, |
| 1673 | n_zero_allocs * 100 / n_allocs ); |
| 1674 | VG_(message)(Vg_DebugMsg, " frees: %u", n_frees); |
| 1675 | VG_(message)(Vg_DebugMsg, " XPts: %u (%d B)", n_xpts, |
| 1676 | n_xpts*sizeof(XPt)); |
| 1677 | VG_(message)(Vg_DebugMsg, " bot-XPts: %u (%d%%)", n_bot_xpts, |
| 1678 | n_bot_xpts * 100 / n_xpts); |
| 1679 | VG_(message)(Vg_DebugMsg, " top-XPts: %u (%d%%)", alloc_xpt->n_children, |
| 1680 | alloc_xpt->n_children * 100 / n_xpts); |
| 1681 | VG_(message)(Vg_DebugMsg, "c-reallocs: %u", n_children_reallocs); |
| 1682 | VG_(message)(Vg_DebugMsg, "snap-frees: %u", n_snapshot_frees); |
| 1683 | VG_(message)(Vg_DebugMsg, "atmp censi: %u", n_attempted_censi); |
| 1684 | VG_(message)(Vg_DebugMsg, "fake censi: %u", n_fake_censi); |
| 1685 | VG_(message)(Vg_DebugMsg, "real censi: %u", n_real_censi); |
| 1686 | VG_(message)(Vg_DebugMsg, " halvings: %u", n_halvings); |
| 1687 | } |
| 1688 | } |
| 1689 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1690 | static void ms_fini(Int exit_status) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1691 | { |
| 1692 | ULong total_ST = 0; |
| 1693 | ULong heap_ST = 0; |
| 1694 | ULong heap_admin_ST = 0; |
| 1695 | ULong stack_ST = 0; |
| 1696 | |
| 1697 | // Do a final (empty) sample to show program's end |
| 1698 | hp_census(); |
| 1699 | |
| 1700 | // Redo spacetimes of significant contexts to match the .hp file. |
nethercote | 43a15ce | 2004-08-30 19:15:12 +0000 | [diff] [blame] | 1701 | calc_exact_ST_dbld(&heap_ST, &heap_admin_ST, &stack_ST); |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1702 | total_ST = heap_ST + heap_admin_ST + stack_ST; |
| 1703 | write_hp_file ( ); |
| 1704 | write_text_file( total_ST, heap_ST ); |
| 1705 | print_summary ( total_ST, heap_ST, heap_admin_ST, stack_ST ); |
| 1706 | } |
| 1707 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1708 | /*------------------------------------------------------------*/ |
| 1709 | /*--- Initialisation ---*/ |
| 1710 | /*------------------------------------------------------------*/ |
| 1711 | |
| 1712 | static void ms_post_clo_init(void) |
| 1713 | { |
| 1714 | ms_interval = 1; |
| 1715 | |
| 1716 | // Do an initial sample for t = 0 |
| 1717 | hp_census(); |
| 1718 | } |
| 1719 | |
tom | 151a639 | 2005-11-11 12:30:36 +0000 | [diff] [blame] | 1720 | static void ms_pre_clo_init(void) |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1721 | { |
| 1722 | VG_(details_name) ("Massif"); |
| 1723 | VG_(details_version) (NULL); |
| 1724 | VG_(details_description) ("a space profiler"); |
njn | 9a0cba4 | 2007-04-15 22:15:57 +0000 | [diff] [blame] | 1725 | VG_(details_copyright_author)( |
| 1726 | "Copyright (C) 2003-2007, Nicholas Nethercote"); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1727 | VG_(details_bug_reports_to) (VG_BUGS_TO); |
| 1728 | |
| 1729 | // Basic functions |
| 1730 | VG_(basic_tool_funcs) (ms_post_clo_init, |
| 1731 | ms_instrument, |
| 1732 | ms_fini); |
| 1733 | |
| 1734 | // Needs |
| 1735 | VG_(needs_libc_freeres)(); |
| 1736 | VG_(needs_command_line_options)(ms_process_cmd_line_option, |
| 1737 | ms_print_usage, |
| 1738 | ms_print_debug_usage); |
| 1739 | VG_(needs_client_requests) (ms_handle_client_request); |
njn | fc51f8d | 2005-06-21 03:20:17 +0000 | [diff] [blame] | 1740 | VG_(needs_malloc_replacement) (ms_malloc, |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1741 | ms___builtin_new, |
| 1742 | ms___builtin_vec_new, |
| 1743 | ms_memalign, |
| 1744 | ms_calloc, |
| 1745 | ms_free, |
| 1746 | ms___builtin_delete, |
| 1747 | ms___builtin_vec_delete, |
| 1748 | ms_realloc, |
| 1749 | 0 ); |
| 1750 | |
| 1751 | // Events to track |
| 1752 | VG_(track_new_mem_stack_signal)( new_mem_stack_signal ); |
| 1753 | VG_(track_die_mem_stack_signal)( die_mem_stack_signal ); |
| 1754 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1755 | // HP_Chunks |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 1756 | malloc_list = VG_(HT_construct)( 80021 ); // prime, big |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1757 | |
| 1758 | // Dummy node at top of the context structure. |
| 1759 | alloc_xpt = new_XPt(0, NULL, /*is_bottom*/False); |
| 1760 | |
sewardj | 198f34f | 2007-07-09 23:13:07 +0000 | [diff] [blame^] | 1761 | tl_assert( VG_(get_startup_wd)(base_dir, VKI_PATH_MAX) ); |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1764 | VG_DETERMINE_INTERFACE_VERSION(ms_pre_clo_init) |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1765 | |
| 1766 | /*--------------------------------------------------------------------*/ |
njn | f1c5def | 2005-08-11 02:17:07 +0000 | [diff] [blame] | 1767 | /*--- end ---*/ |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 1768 | /*--------------------------------------------------------------------*/ |
| 1769 | |