Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:32

0001 
0002 /*--------------------------------------------------------------------*/
0003 /*--- An xtree, tree of stacktraces with data     pub_tool_xtree.h ---*/
0004 /*--------------------------------------------------------------------*/
0005 
0006 /*
0007    This file is part of Valgrind, a dynamic binary instrumentation
0008    framework.
0009 
0010    Copyright (C) 2015-2017 Philippe Waroquiers
0011 
0012    This program is free software; you can redistribute it and/or
0013    modify it under the terms of the GNU General Public License as
0014    published by the Free Software Foundation; either version 2 of the
0015    License, or (at your option) any later version.
0016 
0017    This program is distributed in the hope that it will be useful, but
0018    WITHOUT ANY WARRANTY; without even the implied warranty of
0019    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0020    General Public License for more details.
0021 
0022    You should have received a copy of the GNU General Public License
0023    along with this program; if not, see <http://www.gnu.org/licenses/>.
0024 
0025    The GNU General Public License is contained in the file COPYING.
0026 */
0027 
0028 #ifndef __PUB_TOOL_XTREE_H
0029 #define __PUB_TOOL_XTREE_H
0030 
0031 #include "pub_tool_basics.h"
0032 #include "pub_tool_execontext.h"
0033 
0034 //--------------------------------------------------------------------
0035 // PURPOSE: an XTree is conceptually a set of stacktraces organised
0036 // as a tree structure. 
0037 // A stacktrace (an Addr* ips, i.e. an array of IPs : Instruction Pointers)
0038 // can be added to the tree once transformed into an execontext (ec).
0039 // Some data (typically one or more integer values) can be attached to
0040 // leafs of the tree.
0041 // Non-leaf nodes data is build by combining (typically adding together)
0042 // the data of their children nodes.
0043 // An XTree can be output in various formats.
0044 //
0045 //--------------------------------------------------------------------
0046 
0047 
0048 /* It's an abstract type. */
0049 typedef struct _XTree  XTree;
0050 
0051 /* 3 functions types used by an xtree to manipulate the data attached to leafs
0052    of an XTree.
0053    XT_init_data_t function is used to initialise (typically to 0) the data
0054    of a new node.
0055    XT_add_data_t function is used to add 'value' to the data 'to'.
0056    XT_sub_data_t function is used to substract 'value' from the data 'from'.
0057 
0058    Note that the add/sub functions can do whatever operations to
0059    combine/integrate value with/into to or from. In other words, add/sub
0060    functions are in fact equivalent to 'Reduce' functions. Add/sub is used
0061    as it is assumed that this module will be mostly used to follow
0062    resource consumption, which can be more clearly modelled with add/sub.
0063    For such resource consumption usage, typically, a call to add means that
0064    some additional resource has been allocated or consumed or ... by the
0065    given ExeContext. Similarly, a call to sub means that some resource
0066    has been released/freed/... by the given execontext.
0067 
0068    Note however that there is no constraints in what add (or sub) can do. For
0069    example, the add function could maintain Min/Max values, or an histogram of
0070    values, or ... */
0071 typedef void (*XT_init_data_t) (void* value);
0072 typedef void (*XT_add_data_t) (void* to,   const void* value);
0073 typedef void (*XT_sub_data_t) (void* from, const void* value);
0074 
0075 /* If not NULL, the XT_filter_IPs_t function is called when a new ec is inserted
0076    in the XTree. 
0077    It indicates to the XTree to filter a range of IPs at the top and/or at
0078    the bottom of the ec Stacktrace : *top is the offset of the first IP to take
0079    into account. *n_ips_sel is the nr of IPs selected starting from *top.
0080 
0081    If XT_filter_IPs_t gives *n_ips_sel equal to 0, then the inserted ec will
0082    be fully ignored when outputting the xtree: 
0083      the ec value(s) will not be counted in the XTree total,
0084      the ec will not be printed/shown.
0085    Note however that the filtering only influences the output of an XTree :
0086      the ec is still inserted in the XTree, and the XT_*_data_t functions are
0087      called in any case for such filtered ec. */
0088 typedef void (*XT_filter_IPs_t) (Addr* ips, Int n_ips,
0089                                  UInt* top, UInt* n_ips_sel);
0090 
0091 /* Create new XTree, using given allocation and free function.
0092    This function never returns NULL.
0093    cc is the allocation cost centre.
0094    alloc_fn must not return NULL (that is, if it returns it must have
0095    succeeded.).
0096    See respective typedef for *_fn arguments. */
0097 extern XTree* VG_(XT_create) ( Alloc_Fn_t alloc_fn,
0098                                const HChar* cc,
0099                                Free_Fn_t free_fn,
0100                                Word dataSzB,
0101                                XT_init_data_t init_data_fn,
0102                                XT_add_data_t add_data_fn,
0103                                XT_sub_data_t sub_data_fn,
0104                                XT_filter_IPs_t filter_IPs_fn);
0105 
0106 
0107 /* General useful filtering functions. */
0108 
0109 /* Filter functions below main, unless VG_(clo_show_below_main) is True. */
0110 extern void VG_(XT_filter_maybe_below_main)
0111      (Addr* ips, Int n_ips,
0112       UInt* top, UInt* n_ips_sel);
0113 /* Same as VG_(XT_filter_maybe_below_main) but also filters one top function
0114    (typically to ignore the top level malloc/new/... fn). */
0115 extern void VG_(XT_filter_1top_and_maybe_below_main)
0116      (Addr* ips, Int n_ips,
0117       UInt* top, UInt* n_ips_sel);
0118 
0119 /* Search in ips[0..n_ips-1] the first function which is main or below main
0120    and return its offset.
0121    If no main or below main is found, return n_ips-1 */
0122 extern Int VG_(XT_offset_main_or_below_main)(DiEpoch ep, Addr* ips, Int n_ips);
0123 
0124 
0125 /* Take a (frozen) snapshot of xt.
0126    Note that the resulting XTree is 'read-only' : calls to 
0127    VG_(XT_add_to_*)/VG_(XT_sub_from_*) will assert.
0128 
0129    Note: to spare memory, some data is shared between an xt and all its
0130    snapshots. This memory is released when the last XTree using this memory
0131    is deleted. */
0132 extern XTree* VG_(XT_snapshot)(XTree* xt);
0133 
0134 /*  Non frozen dup currently not needed : 
0135     extern XTree* VG_(XT_dup)(XTree* xt); */
0136 
0137 /* Free all memory associated with an XTRee. */
0138 extern void VG_(XT_delete)(XTree* xt);
0139 
0140 /* an Xecu identifies an exe context+its associated data in an XTree. */
0141 typedef UInt Xecu;
0142 
0143 /* If not yet in xt, inserts the provided ec and initialises its
0144    data by calling init_data_fn.
0145    If already present (or after insertion), updates the data by calling
0146    add_data_fn. */
0147 extern Xecu VG_(XT_add_to_ec)(XTree* xt, ExeContext* ec, const void* value);
0148 
0149 /* If not yet in xt, inserts the provided ec and initialises its
0150    data by calling init_data_fn.
0151    If already present (or after insertion), updates the data by calling
0152    sub_data_fn to substract value from the data associated to ec. */
0153 extern Xecu VG_(XT_sub_from_ec)(XTree* xt, ExeContext* ec, const void* value);
0154 
0155 /* Same as (but more efficient than) VG_(XT_add_to_ec) and VG_(XT_sub_from_ec)
0156    for an ec already inserted in xt. */
0157 extern void VG_(XT_add_to_xecu)(XTree* xt, Xecu xecu, const void* value);
0158 extern void VG_(XT_sub_from_xecu)(XTree* xt, Xecu xecu, const void* value);
0159 
0160 /* Return the nr of IPs selected for xecu. 0 means fully filtered. */
0161 extern UInt VG_(XT_n_ips_sel)(XTree* xt, Xecu xecu);
0162 
0163 /* Return the ExeContext associated to the Xecu. */
0164 extern ExeContext* VG_(XT_get_ec_from_xecu) (XTree* xt, Xecu xecu);
0165 
0166 /* -------------------- CALLGRIND/KCACHEGRIND OUTPUT FORMAT --------------*/
0167 /* Prints xt in outfilename in callgrind/kcachegrind format.
0168    events is a comma separated list of events, used by 
0169    kcachegrind/callgrind_annotate/... to name the value various components.
0170    An event can optionally have a longer description, separated from the
0171    event name by " : ", e.g.
0172    "curB : currently allocated Bytes,curBk : Currently allocated Blocks"
0173    img_value returns an image of the value. The image must be a space
0174    separated set of integers, matching the corresponding event in events.
0175    Note that the returned pointer can be static data. 
0176    img_value can return NULL if value (and its associated ExeContext) should
0177    not be printed.
0178 */
0179 extern void VG_(XT_callgrind_print) 
0180      (XTree* xt,
0181       const HChar* outfilename,
0182       const HChar* events,
0183       const HChar* (*img_value) (const void* value));
0184 
0185 
0186 /* -------------------- MASSIF OUTPUT FORMAT --------------*/
0187 // Time is measured either in i or ms or bytes, depending on the --time-unit
0188 // option.  It's a Long because it can exceed 32-bits reasonably easily, and
0189 // because we need to allow negative values to represent unset times.
0190 typedef Long Time;
0191 
0192 typedef void MsFile;
0193 
0194 /* Create a new file or truncate existing file for printing xtrees in
0195    massif format. time_unit is a string describing the unit used
0196    in Massif_Header time.
0197    Produces a user error msg and returns NULL if file cannot be opened.
0198    Caller must VG_(XT_massif_close) the returned file. */
0199 extern MsFile* VG_(XT_massif_open)(const HChar* outfilename,
0200                                    const HChar* desc, // can be NULL
0201                                    const XArray* desc_args, // can be NULL
0202                                    const HChar* time_unit);
0203 
0204 extern void VG_(XT_massif_close)(MsFile* fp);
0205 
0206 typedef 
0207    struct {
0208       int snapshot_n; // starting at 0.
0209       Time time;
0210 
0211       ULong sz_B;     // sum of values, only used when printing a NULL xt.
0212       ULong extra_B;
0213       ULong stacks_B;
0214 
0215       Bool detailed;
0216       Bool peak;
0217 
0218       /*   top_node_desc: description for the top node.
0219            Typically for memory usage, give xt_heap_alloc_functions_desc. */
0220       const HChar* top_node_desc;
0221 
0222       /* children with less than sig_threshold * total xt sz will be aggregated
0223          and printed as one single child. */
0224       double sig_threshold;
0225 
0226    } Massif_Header;
0227 
0228 /* Prints xt in outfilename in massif format.
0229    If a NULL xt is provided, then only the header information is used
0230    to produce the (necessarily not detailed) snapshot.
0231    report_value must return the value to be used for the report production.
0232    It will typically be the nr of bytes allocated stored with the execontext
0233    but it could be anything measured with a ULong (e.g. the nr of blocks
0234    allocated, or a number of calls, ...).
0235 */
0236 extern void VG_(XT_massif_print)
0237      (MsFile* fp,
0238       XTree* xt,
0239       const Massif_Header* header,
0240       ULong (*report_value)(const void* value));
0241 
0242 #endif   // __PUB_TOOL_XTREE_H
0243 
0244 /*--------------------------------------------------------------------*/
0245 /*--- end                                         pub_tool_xtree.h ---*/
0246 /*--------------------------------------------------------------------*/