HPCToolkit
retcnt.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-2019, 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 // RETCNT sample source simple oo interface
49 //
50 
51 
52 /******************************************************************************
53  * system includes
54  *****************************************************************************/
55 
56 #include <stdlib.h>
57 #include <stddef.h>
58 #include <string.h>
59 #include <assert.h>
60 
61 
62 /******************************************************************************
63  * libmonitor
64  *****************************************************************************/
65 
66 #include <monitor.h>
67 
68 
69 /******************************************************************************
70  * local includes
71  *****************************************************************************/
72 
73 #include "simple_oo.h"
74 #include "sample_source_obj.h"
75 #include "common.h"
76 
77 #include <hpcrun/hpcrun_options.h>
78 #include <hpcrun/metrics.h>
79 #include <hpcrun/sample_event.h>
81 #include <hpcrun/thread_data.h>
82 #include <utilities/tokenize.h>
83 #include <cct/cct.h>
84 
85 #if defined (HOST_CPU_x86_64)
86 
87 #include <messages/messages.h>
88 
90 
91 extern void hpcrun_set_retain_recursion_mode(bool mode);
92 
93 //***************************************************************************
94 
95 // ******** Local Constants ****************
96 
97 //
98 // There is only 1 "event" associated with a return count
99 // so the event id is necessarily 0.
100 //
101 static const int RETCNT_EVENT = 0;
102 //
103 // The "period" for a return count event
104 // is irrelevant (so it is arbitrarily 0).
105 //
106 static const int IRRELEVANT = 0;
107 
108 
109 // ******* METHOD DEFINITIONS ***********
110 
111 static void
112 METHOD_FN(init)
113 {
114  self->state = INIT;
115 }
116 
117 static void
118 METHOD_FN(thread_init)
119 {
120  TMSG(RETCNT_CTL, "thread init (no action needed)");
121 }
122 
123 static void
124 METHOD_FN(thread_init_action)
125 {
126  TMSG(RETCNT_CTL, "thread action (noop)");
127 }
128 
129 static void
130 METHOD_FN(start)
131 {
132  TMSG(RETCNT_CTL,"starting " HPCRUN_METRIC_RetCnt);
133 
134  TD_GET(ss_state)[self->sel_idx] = START;
135 }
136 
137 static void
138 METHOD_FN(thread_fini_action)
139 {
140  TMSG(RETCNT_CTL, "thread fini (no action needed");
141 }
142 
143 static void
144 METHOD_FN(stop)
145 {
146  TMSG(RETCNT_CTL,"stopping " HPCRUN_METRIC_RetCnt);
147  TD_GET(ss_state)[self->sel_idx] = STOP;
148 }
149 
150 static void
151 METHOD_FN(shutdown)
152 {
153  METHOD_CALL(self,stop); // make sure stop has been called
154  self->state = UNINIT;
155 }
156 
157 static bool
158 METHOD_FN(supports_event,const char *ev_str)
159 {
160  return hpcrun_ev_is(ev_str, HPCRUN_METRIC_RetCnt);
161 }
162 
163 
164 static void
165 METHOD_FN(process_event_list, int lush_metrics)
166 {
167  int metric_id = hpcrun_new_metric();
168  TMSG(RETCNT_CTL, "Setting up return counts(trampolines)");
169 
170  // FIXME: MetricFlags_Ty_Final
172 
173  METHOD_CALL(self, store_event, RETCNT_EVENT, IRRELEVANT);
174  METHOD_CALL(self, store_metric_id, RETCNT_EVENT, metric_id);
175 
176  // turn on trampoline processing
177  ENABLE(USE_TRAMP);
178 }
179 
180 //
181 // Event sets not truly relevant for this sample source,
182 //
183 static void
184 METHOD_FN(gen_event_set,int lush_metrics)
185 {
186  TMSG(REC_COMPRESS, "RETCNT event ==> retain recursion");
187  hpcrun_set_retain_recursion_mode(true); // make sure all recursion elements are retained
188  // whenever RETCNT is used.
189 }
190 
191 static void
192 METHOD_FN(display_events)
193 {
194  printf("===========================================================================\n");
195  printf("Available return-count events\n");
196  printf("===========================================================================\n");
197  printf("Name\t\tDescription\n");
198  printf("---------------------------------------------------------------------------\n");
199  printf("%s\t\tEach time a procedure returns, the return count for that\n"
200  "\t\tprocedure is incremented\n"
201  "(experimental feature, x86 only)\n", HPCRUN_METRIC_RetCnt);
202  printf("\n");
203 }
204 
205 #define ss_name retcnt
206 #define ss_cls SS_SOFTWARE
207 #define ss_sort_order 100
208 
209 #include "ss_obj.h"
210 
211 // ***************************************************************************
212 // Interface functions
213 // ***************************************************************************
214 
215 // increment the return count
216 //
217 // N.B. : This function is necessary to avoid exposing the retcnt_obj.
218 // For the case of the retcnt sample source, the handler (the trampoline)
219 // is separate from the sample source code.
220 // Consequently, the interaction with metrics must be done procedurally
221 
222 void
224 {
225  int metric_id = hpcrun_event2metric(&_retcnt_obj, RETCNT_EVENT);
226 
227  TMSG(TRAMP, "Increment retcnt (metric id = %d), by %d", metric_id, incr);
228  cct_metric_data_increment(metric_id,
229  node,
230  (cct_metric_data_t){.i = incr});
231 }
232 
233 #else
234 
235 void
237 {
238 }
239 
240 #endif
cct_node_t * node
Definition: cct.c:128
static void cct_metric_data_increment(int metric_id, cct_node_t *x, cct_metric_data_t incr)
Definition: cct2metrics.h:86
#define ENABLE(f)
Definition: debug-flag.h:73
#define METHOD_FN(n,...)
void hpcrun_retcnt_inc(cct_node_t *node, int incr)
Definition: retcnt.c:236
#define HPCRUN_METRIC_RetCnt
Definition: hpcrun-metric.h:82
int lush_metrics
Definition: main.c:188
#define TD_GET(field)
Definition: thread_data.h:256
bool hpcrun_ev_is(const char *candidate, const char *event_name)
Definition: tokenize.c:194
#define TMSG(f,...)
Definition: messages.h:93
int hpcrun_event2metric(sample_source_t *ss, int event_idx)
Definition: common.c:143
#define METHOD_CALL(obj, meth,...)
Definition: simple_oo.h:87
Definition: cct.c:96
int hpcrun_new_metric(void)
Definition: metrics.c:333
metric_desc_t * hpcrun_set_metric_info(int metric_id, const char *name)
Definition: metrics.c:423
void hpcrun_set_retain_recursion_mode(bool mode)