HPCToolkit
memaddress.c
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2017, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 
48 
49 /******************************************************************************
50  * local includes
51  ******************************************************************************/
52 
53 #include <cct/cct.h> // cct_node_t
54 
55 #include "sample-sources/perf/perf-util.h" // event_info_t, perf_attr_init
56 #include "sample-sources/perf/event_custom.h" // event_custom_t
57 #include "sample-sources/perf/perf_skid.h" // precice ip
58 
59 #include "data_tree.h"
60 
61 #include "place_folder.h"
62 
63 /******************************************************************************
64  * MACROs
65  ******************************************************************************/
66 
67 #define EVNAME_MEMORY_CENTRIC "MEMCENTRIC"
68 
69 
70 /******************************************************************************
71  * function folder
72  ******************************************************************************/
73 
74 FUNCTION_FOLDER(first_touch)
75 
76 /******************************************************************************
77  * data structure
78  *****************************************************************************/
79 
80 
81 /******************************************************************************
82  * local variables
83  *****************************************************************************/
84 
85 
86 /******************************************************************************
87  * local methods
88  *****************************************************************************/
89 
90 
91 static void
93 {
94  if ( (args->current == NULL) || (args->data == NULL) ||
95  (args->data->addr == 0) || (args->sample->sample_node == NULL))
96  return;
97 
98  // memory information exists
99  void *start, *end;
100  struct datatree_info_s *info = datatree_splay_lookup((void*) args->data->addr, &start, &end);
101 
102  if (info && info->context) {
105 
108 
109  // copy the call path of the malloc
111 
113 
114  hpcrun_metricVal_t* loc = hpcrun_metric_set_loc(mset, args->metric);
115  if (loc->i == 0) {
116  loc->i = args->data->addr;
117  }
118 
120 
121  TMSG(DATACENTRIC, "handling node %p, cct: %p, addr: %p", node, args->sample->sample_node, args->data->addr);
122  }
123 }
124 
125 
126 static int
128 {
130  if (event_info == NULL)
131  return -1;
132 
133  memset(event_info, 0, sizeof(event_info_t));
134 
135  event_info->metric_custom = event;
136 
137  struct event_threshold_s threshold;
138  perf_util_get_default_threshold( &threshold );
139 
140  // ------------------------------------------
141  // create metric page-fault
142  // ------------------------------------------
143  int metric_page_fault = hpcrun_new_metric();
144 
146  metric_page_fault, "PAGE-FAULTS",
148 
149  // ------------------------------------------
150  // create custom metric page-address
151  // ------------------------------------------
152  int page_address = hpcrun_new_metric();
154  page_address, "PAGE-ADDR",
155  MetricFlags_ValFmt_Int, 1 /* frequency*/, metric_property_none);
156 
157  // ------------------------------------------
158  // initialize perf attributes
159  // ------------------------------------------
160  u64 sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_TID |
161  PERF_SAMPLE_TIME | PERF_SAMPLE_CALLCHAIN |
162  PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD;
163 
164  // set the default attributes for page fault
165  struct perf_event_attr *attr = &(event_info->attr);
166  attr->config = PERF_COUNT_SW_PAGE_FAULTS;
167  attr->type = PERF_TYPE_SOFTWARE;
168 
171  attr,
172  true /* use_period*/,
173  threshold.threshold_num /* use the default */,
174  sample_type /* need additional info to gather memory address */
175  );
177 
178  event_info->attr.sample_id_all = 1;
179 
180  METHOD_CALL(self, store_event_and_info,
181  attr->config, 1, page_address, event_info);;
182  return 1;
183 }
184 
185 
186 /******************************************************************************
187  * Interface
188  *****************************************************************************/
189 
190 
191 void
193 {
194  event_custom_t *event_memcentric = hpcrun_malloc(sizeof(event_custom_t));
195  event_memcentric->name = EVNAME_MEMORY_CENTRIC;
196  event_memcentric->desc = "Experimental counter: identifying first-touch memory address.";
197  event_memcentric->register_fn = memcentric_register; // call backs
198  event_memcentric->handler_fn = memcentric_handler;
199  event_memcentric->handle_type = EXCLUSIVE;// call me only for my events
200 
201  event_custom_register(event_memcentric);
202 }
203 
204 
205 
206 
struct event_info_s * current
Definition: event_custom.h:64
metric_aux_info_t * perf_event_info
static int memcentric_register(sample_source_t *self, event_custom_t *event, struct event_threshold_s *period)
Definition: memaddress.c:127
metric_set_t * hpcrun_reify_metric_set(cct_node_id_t cct_id)
Definition: cct2metrics.c:115
int perf_skid_set_max_precise_ip(struct perf_event_attr *attr)
Definition: perf_skid.c:176
void memcentric_init()
Definition: memaddress.c:192
struct perf_event_attr attr
Definition: perf-util.h:138
cct_node_t * node
Definition: cct.c:128
cct_node_t * tree_root
Definition: cct_bundle.h:65
event_handler_t * handler_fn
Definition: event_custom.h:89
struct event_custom_s * metric_custom
Definition: perf-util.h:139
cct_node_t * sample_node
Definition: sample_event.h:96
metric_desc_t * hpcrun_set_metric_info_and_period(int metric_id, const char *name, MetricFlags_ValFmt_t valFmt, size_t period, metric_desc_properties_t prop)
Definition: metrics.c:411
cct_bundle_t csdata
Definition: epoch.h:65
uint64_t num_samples
Definition: hpcfmt.h:349
cct_node_t * hpcrun_insert_special_node(cct_node_t *root, void *addr)
Definition: cct.c:861
void perf_util_get_default_threshold(struct event_threshold_s *threshold)
Definition: perf-util.c:347
#define FUNCTION_FOLDER_NAME(name)
Definition: place_folder.h:50
core_profile_trace_data_t core_profile_trace_data
Definition: thread_data.h:168
struct perf_mmap_data_s * data
Definition: event_custom.h:65
static void DATACENTRIC()
Definition: cct_bundle.c:71
void * hpcrun_malloc(size_t size)
Definition: mem.c:275
cct_node_t * context
Definition: data_tree.h:68
const char * desc
Definition: event_custom.h:86
cct_metric_data_t * hpcrun_metric_set_loc(metric_set_t *s, int id)
Definition: metrics.c:465
int perf_util_attr_init(const char *event_name, struct perf_event_attr *attr, bool usePeriod, u64 threshold, u64 sampletype)
Definition: perf-util.c:403
#define TMSG(f,...)
Definition: messages.h:93
__u64 u64
#define METHOD_CALL(obj, meth,...)
Definition: simple_oo.h:87
#define FUNCTION_FOLDER(name)
Definition: place_folder.h:54
register_event_t * register_fn
Definition: event_custom.h:88
#define NULL
Definition: ElfHelper.cpp:85
cct_node_t * hpcrun_cct_insert_path_return_leaf(cct_node_t *path, cct_node_t *root)
Definition: cct.c:871
Definition: cct.c:96
#define EVNAME_MEMORY_CENTRIC
Definition: memaddress.c:67
struct datatree_info_s * datatree_splay_lookup(void *key, void **start, void **end)
Definition: data_tree.c:203
int hpcrun_new_metric(void)
Definition: metrics.c:333
event_handle_type_t handle_type
Definition: event_custom.h:91
#define POINTER_TO_FUNCTION
Definition: place_folder.h:63
static void memcentric_handler(event_handler_arg_t *args)
Definition: memaddress.c:92
const char * name
Definition: event_custom.h:85
static long period
Definition: itimer.c:194
thread_data_t *(* hpcrun_get_thread_data)(void)
Definition: thread_data.c:168
#define metric_property_none
Definition: hpcrun-fmt.h:202
int event_custom_register(event_custom_t *event)
Definition: event_custom.c:110
sample_val_t * sample
Definition: event_custom.h:62