HPCToolkit
sample_sources_all.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 // The sample sources data structure
49 //
50 
51 //*******************************************************************
52 // System includes
53 //*******************************************************************
54 
55 #include <stdlib.h>
56 #include <stdbool.h>
57 #include <string.h>
58 
59 //*******************************************************************
60 // Local Includes
61 //*******************************************************************
62 
63 #include <monitor.h>
64 
65 #include "sample_sources_all.h"
67 
68 #include "thread_data.h"
71 #include <sample-sources/common.h>
72 #include <utilities/tokenize.h>
73 #include <messages/messages.h>
74 
75 //*******************************************************************
76 // Macros
77 //*******************************************************************
78 
79 #define THREAD_DOINIT 0
80 #define THREAD_NOSAMPLING 1
81 #define THREAD_SAMPLING 2
82 
83 //*******************************************************************
84 // Function Defining Macros
85 //*******************************************************************
86 
87 #define _AS0(n, necessary) \
88 void \
89 hpcrun_all_sources_ ##n(void) \
90 { \
91  if (necessary) { if (ignore_this_thread()) return; } \
92  TMSG(SS_ALL, "calling function %s", __func__); \
93  for(sample_source_t* ss = sample_sources; ss; ss = ss->next_sel) { \
94  TMSG(SS_ALL,"sample source (%s) method call: %s", \
95  ss->name, #n); \
96  METHOD_CALL(ss, n); \
97  } \
98 }
99 
100 #define _AS1(n,t,arg) \
101 void \
102 hpcrun_all_sources_ ##n(t arg) \
103 { \
104  for(sample_source_t* ss = sample_sources; ss; ss = ss->next_sel) { \
105  METHOD_CALL(ss, n, arg); \
106  } \
107 }
108 
109 #define _ASB(n) \
110 bool \
111 hpcrun_all_sources_ ##n(void) \
112 { \
113  TMSG(SS_ALL, "checking %d sources",n_sources); \
114  for(sample_source_t* ss = sample_sources; ss; ss = ss->next_sel) { \
115  if (! METHOD_CALL(ss, n)) { \
116  TMSG(SS_ALL, "%s not started",ss->name); \
117  return false; \
118  } \
119  } \
120  return true; \
121 }
122 
123 //
124 // END Function Defining Macros
125 //
126 
127 //*******************************************************************
128 // Local variables
129 //*******************************************************************
130 
133 static size_t n_sources = 0;
134 
135 static __thread int ignore_thread = THREAD_DOINIT;
136 
137 //*******************************************************************
138 // private functions
139 //*******************************************************************
140 
141 static int
143 {
144  if (ignore_thread == THREAD_DOINIT) {
146 
147  char *string = getenv("HPCRUN_IGNORE_THREAD");
148  if (string) {
149 
150  // eliminate special cases by adding comma delimiters at front and back
151  char all_str[1024];
152  sprintf(all_str, ",%s,", string);
153 
154  int myid = monitor_get_thread_num();
155  char myid_str[20];
156  sprintf(myid_str, ",%d,", myid);
157 
158  if (strstr(all_str, myid_str)) {
160  TMSG(IGNORE, "Thread %d ignore sampling", myid);
161  }
162  }
163  }
165 }
166 
167 //*******************************************************************
168 // Interface functions
169 //*******************************************************************
170 
171 
174 {
175  for (sample_source_t* ss = sample_sources; ss; ss = ss->next_sel){
176  if (strcmp(ss->name, src) == 0) {
177  return ss;
178  }
179  }
180  return NULL;
181 }
182 
183 bool
185 {
186  for (sample_source_t* ss = sample_sources; ss; ss = ss->next_sel){
187  if (strcmp(ss->name, src) == 0) {
188  return true;
189  }
190  }
191  return false;
192 }
193 
194 static bool
196 {
197  for (sample_source_t* ss = sample_sources; ss; ss = ss->next_sel){
198  if (ss == ss_in)
199  return true;
200  }
201  return false;
202 }
203 
204 
205 static void
207 {
208  TMSG(SS_ALL, "%s", ss->name);
209  if (in_sources(ss)) {
210  return;
211  }
212  *ss_insert = ss;
213  ss->next_sel = NULL;
214  ss_insert = &(ss->next_sel);
215  ss->sel_idx = n_sources++;
216  TMSG(SS_ALL, "Sample source %s has selection index %d", ss->name, ss->sel_idx);
217  TMSG(SS_ALL, "# sources now = %d", n_sources);
218 }
219 
220 
221 //
222 // Return the number of -selected- sample sources
223 //
224 size_t
226 {
227  return n_sources;
228 }
229 
230 void
232 {
233  if (evl == NULL) {
235  }
236 
237  TMSG(EVENTS,"evl (before processing) = |%s|",evl);
238 
239  for(char *event = start_tok(evl); more_tok(); event = next_tok()){
240  sample_source_t *s;
241  if (strcasecmp(event, "LIST") == 0) {
243  }
244  else if ( (s = hpcrun_source_can_process(event)) ){
245  add_source(s);
246  METHOD_CALL(s, add_event, event);
247  }
248  else {
249  hpcrun_ssfail_unknown(event);
250  }
251  }
252 }
253 
254 // The mapped operations
255 
256 _AS1(process_event_list, int, lush_metrics)
257 _AS0(init, 0)
258 _AS0(thread_init, 0)
259 _AS0(thread_init_action, 0)
260 _AS1(gen_event_set, int, lush_metrics)
261 _AS0(start, 1)
262 _AS0(thread_fini_action, 0)
263 _AS0(stop, 1)
264 _AS0(shutdown, 0)
265 _ASB(started)
static size_t n_sources
bool hpcrun_check_named_source(const char *src)
void hpcrun_sample_sources_from_eventlist(char *evl)
struct sample_source_t * next_sel
size_t hpcrun_get_num_sample_sources(void)
void hpcrun_ssfail_unknown(char *event)
Definition: common.c:220
#define _ASB(n)
#define THREAD_SAMPLING
static sample_source_t * sample_sources
char * next_tok(void)
Definition: tokenize.c:87
#define THREAD_NOSAMPLING
static void add_source(sample_source_t *ss)
static __thread int ignore_thread
static bool in_sources(sample_source_t *ss_in)
char * start_tok(char *lst)
Definition: tokenize.c:70
int lush_metrics
Definition: main.c:188
void hpcrun_ssfail_none(void)
Definition: common.c:209
#define THREAD_DOINIT
#define TMSG(f,...)
Definition: messages.h:93
static sample_source_t ** ss_insert
#define METHOD_CALL(obj, meth,...)
Definition: simple_oo.h:87
sample_source_t * hpcrun_source_can_process(char *event)
#define NULL
Definition: ElfHelper.cpp:85
#define _AS1(n, t, arg)
sample_source_t * hpcrun_fetch_source_by_name(const char *src)
#define _AS0(n, necessary)
static int ignore_this_thread()
void hpcrun_display_avail_events(void)
int more_tok(void)
Definition: tokenize.c:78