HPCToolkit
monitor.h
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 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // General header.
54 //
55 // Description:
56 // Shared declarations, etc for monitoring.
57 //
58 // *** N.B. ***
59 // There is an automake install hook that will hide these external
60 // symbols.
61 //
62 // Author:
63 // Written by John Mellor-Crummey and Nathan Tallent, Rice University.
64 //
65 *****************************************************************************/
66 
67 #ifndef hpcrun_flat_monitor_h
68 #define hpcrun_flat_monitor_h
69 
70 /************************** System Include Files ****************************/
71 
72 #include <stdio.h>
73 #include <unistd.h> /* getpid() */
74 #include <inttypes.h>
75 #include <stdarg.h>
76 
77 #include <sys/time.h> /* for sprofil() */
78 #include <sys/profil.h> /* for sprofil() */
79 
80 /**************************** User Include Files ****************************/
81 
82 #include <include/hpctoolkit-config.h>
83 #include <include/uint.h>
84 
85 #include "hpcrun.h"
86 #include "hpcpapi.h"
87 
88 /**************************** Forward Declarations **************************/
89 
90 // Private debugging level: messages for in-house debugging [0-9]
91 #define HPCRUN_DBG_LVL 0
92 
93 // MSG should be atomic so that thread messages are not interleaved.
94 // Because we want to expand the format string with additional output
95 // parameters, we need to know how to rebuild the fprintf parameters,
96 // which involves knowing when to include a comma and when not to:
97 // "%x1 %x2" fmt, x1, x2 -OR- "%x1 %x2" fmt, x1, x2, fmt_args
98 // But doing this requires more compile time evaluation than macros
99 // support. Thus, the user must perform this selection by choosing
100 // between MSG0 and MSGx.
101 
102 #define MSG_str(fmt) \
103  HPCRUN_NAME" [pid %d, tid 0x%lx]: " fmt "\n", getpid(), hpcrun_gettid()
104 
105 
106 #define MSG0(x, fmt) \
107  { fprintf(x, MSG_str(fmt)); }
108 
109 #define MSGx(x, fmt, ...) \
110  { fprintf(x, MSG_str(fmt), __VA_ARGS__); }
111 
112 /*#define MSG(x, ...) \
113  { fprintf((x), "hpcrun (pid %d, tid 0x%lx): ", getpid(), hpcrun_gettid()); fprintf((x), __VA_ARGS__); fputs("\n", (x)); } */
114 
115 
116 #define ERRMSG0(fmt) \
117  { if (HPCRUN_DBG_LVL) { \
118  fprintf(stderr, MSG_str("[%s:%d]: " fmt), __FILE__, __LINE__); } \
119  else { \
120  fprintf(stderr, MSG_str(fmt)); } \
121  }
122 
123 #define ERRMSGx(fmt, ...) \
124  { if (HPCRUN_DBG_LVL) { \
125  fprintf(stderr, MSG_str("[%s:%d]: " fmt), __FILE__, __LINE__, __VA_ARGS__); } \
126  else { \
127  fprintf(stderr, MSG_str(fmt), __VA_ARGS__); } \
128  }
129 
130 /*#define ERRMSG(...) \
131  { fputs("hpcrun", stderr); \
132  if (HPCRUN_DBG_LVL) { \
133  fprintf(stderr, " [%s:%d]", __FILE__, __LINE__); } \
134  fprintf(stderr, " (pid %d, tid 0x%lx): ", getpid(), hpcrun_gettid()); fprintf(stderr, __VA_ARGS__); fputs("\n", stderr); } */
135 
136 
137 #define DIE0(fmt) ERRMSG0(fmt); { exit(1); }
138 
139 #define DIEx(fmt, ...) ERRMSGx(fmt, __VA_ARGS__); { exit(1); }
140 
141 /**************************** Forward Declarations **************************/
142 
143 // hpcsys_profile_desc_t: Collects all information to describe system
144 // based (i.e. non-PAPI) profiles, e.g. a call to sprofil(). Note that
145 // the segmented-profile buffers will correspond to data in the
146 // run-time-load-map.
147 typedef struct {
148  /* currently we only have one type of system prof. If we ever need
149  more we can add a prof-type field */
150  char* ename; // event name
151  uint64_t period; // sampling period
152  //struct timeval* tval; // contains info after a call to sprofil()
153  unsigned int flags; // profiling flags
154 
155  unsigned int bytesPerCodeBlk; // bytes per block of monitored code
156  unsigned int bytesPerCntr; // bytes per histogram counter
157  unsigned int scale; // relationship between the two
158 
159  struct prof* sprofs; // vector of histogram buffers, one for each
160  unsigned int numsprofs; // run time load module
162 
163 // hpcsys_profile_desc_vec_t: A vector of hpcsys_profile_desc_t.
164 typedef struct {
165  unsigned int size; // vector size
166  hpcsys_profile_desc_t* vec; // one for each profile
167 
169 
170 /**************************** Forward Declarations **************************/
171 
172 // hpcrun_ofile_desc_t: Describes an hpcrun output file
173 typedef struct {
174  FILE* fs; // file stream
175  char* fname; // file name
177 
178 
179 // hpcrun_profiles_desc_t: Describes all concurrent profiles for a
180 // particular process or thread.
181 typedef struct {
182  /* We use void* to make conditional compilation easy. See macros below. */
183  void* sysprofs; /* hpcsys_profile_desc_vec_t* */
184  void* papiprofs; /* hpcpapi_profile_desc_vec_t* */
185 
188 
189 /* Each accessor macro has two versions, one for use as lvalue and
190  rvalue. The reason is that casts in lvalue expressions is a
191  non-standard. */
192 #define HPC_GETL_SYSPROFS(x) ((x)->sysprofs)
193 #define HPC_GET_SYSPROFS(x) ((hpcsys_profile_desc_vec_t*)((x)->sysprofs))
194 
195 //#if HAVE_PAPI
196 #define HPC_GETL_PAPIPROFS(x) ((x)->papiprofs)
197 #define HPC_GET_PAPIPROFS(x) ((hpcpapi_profile_desc_vec_t*)((x)->papiprofs))
198 //#else
199 //#define HPC_GET_PAPIPROFS(x) (x->papiprofs)
200 //#endif
201 
202 /**************************** Forward Declarations **************************/
203 
204 /* 'intercepted' libc routines */
205 
206 #define PARAMS_START_MAIN (int (*main) (int, char **, char **), \
207  int argc, \
208  char *__unbounded *__unbounded ubp_av, \
209  void (*init) (void), \
210  void (*fini) (void), \
211  void (*rtld_fini) (void), \
212  void *__unbounded stack_end)
213 
214 typedef int (*libc_start_main_fptr_t) PARAMS_START_MAIN;
215 typedef void (*libc_start_main_fini_fptr_t) (void);
216 
217 
218 #define PARAMS_EXECV (const char *path, char *const argv[])
219 #define PARAMS_EXECVP (const char *file, char *const argv[])
220 #define PARAMS_EXECVE (const char *path, char *const argv[], \
221  char *const envp[])
222 
223 typedef int (*execv_fptr_t) PARAMS_EXECV;
224 typedef int (*execvp_fptr_t) PARAMS_EXECVP;
225 typedef int (*execve_fptr_t) PARAMS_EXECVE;
226 
227 typedef pid_t (*fork_fptr_t) (void);
228 typedef void* (*dlopen_fptr_t) (const char *filename, int flag);
229 
230 typedef void (*_exit_fptr_t) (int);
231 
232 /* 'intercepted' libpthread routines */
233 
234 #define PARAMS_PTHREAD_CREATE (pthread_t* thread, \
235  const pthread_attr_t* attr, \
236  void *(*start_routine)(void*), \
237  void* arg)
238 
239 typedef struct {
240  void* (*start_routine)(void*); /* from pthread_create() */
241  void* arg; /* from pthread_create() */
242  hpcrun_profiles_desc_t* profdesc; /* profiling info */
244 
245 
246 typedef int (*pthread_create_fptr_t) PARAMS_PTHREAD_CREATE;
247 
248 typedef pthread_t (*pthread_self_fptr_t) (void);
249 
250 
251 /**************************** Forward Declarations **************************/
252 
253 #ifdef __cplusplus
254 extern "C" {
255 #endif
256 
257 /* These routines can be called externally. */
258 
259 extern void init_library();
260 extern void fini_library();
261 
262 extern void init_process();
263 extern void fini_process();
264 
265 extern hpcrun_profiles_desc_t*
266 init_thread(int is_thread);
267 
268 extern void
269 fini_thread(hpcrun_profiles_desc_t** profdesc, int is_thread);
270 
271 extern long hpcrun_gettid();
272 
273 extern void
274 hpcrun_parse_execl(const char*** argv, const char* const** envp,
275  const char* arg, va_list arglist);
276 
277 extern void handle_dlopen();
278 
279 
280 /* These routines must be specialized and supplied by someone else.
281  monitor.c does _not_ contain their definitions. */
282 
283 extern void init_library_SPECIALIZED();
284 
286 
287 extern long hpcrun_gettid_SPECIALIZED();
288 
289 
290 #ifdef __cplusplus
291 }
292 #endif
293 
294 
295 /*************************** Variable Declarations **************************/
296 
297 #ifdef __cplusplus
298 extern "C" {
299 #endif
300 
301 
302 /* hpcrun options: set when the library is initialized */
303 extern int opt_debug;
304 extern int opt_recursive;
306 extern char* opt_eventlist;
307 extern char opt_outpath[PATH_MAX];
308 extern int opt_flagscode;
309 
310 /* monitored command: set when library or process is initialized */
311 extern const char* hpcrun_cmd; /* profiled command */
312 
313 
314 #ifdef __cplusplus
315 }
316 #endif
317 
318 
319 /****************************************************************************/
320 
321 #endif
unsigned int scale
Definition: monitor.h:157
unsigned int bytesPerCntr
Definition: monitor.h:156
void init_papi_for_process_SPECIALIZED()
unsigned int numsprofs
Definition: monitor.h:160
#define PARAMS_PTHREAD_CREATE
Definition: monitor.h:234
unsigned int flags
Definition: monitor.h:153
void(* _exit_fptr_t)(int)
Definition: monitor.h:230
void init_library_SPECIALIZED()
void fini_process()
Definition: monitor.c:1466
void fini_library()
Definition: monitor.c:183
void(* libc_start_main_fini_fptr_t)(void)
Definition: monitor.h:215
void init_library()
Definition: monitor.c:164
hpcrun_profiles_desc_t * profdesc
Definition: monitor.h:242
long hpcrun_gettid_SPECIALIZED()
#define PARAMS_EXECV
Definition: monitor.h:218
hpc_threadprof_t opt_thread
Definition: monitor.c:128
enum enum_hpc_threadprof_t hpc_threadprof_t
#define PARAMS_EXECVP
Definition: monitor.h:219
void fini_thread(hpcrun_profiles_desc_t **profdesc, int is_thread)
Definition: monitor.c:1491
int opt_debug
Definition: monitor.c:126
pthread_t(* pthread_self_fptr_t)(void)
Definition: monitor.h:248
const char * hpcrun_cmd
Definition: monitor.c:136
int opt_recursive
Definition: monitor.c:127
long hpcrun_gettid()
Definition: monitor.c:1864
#define PARAMS_START_MAIN
Definition: monitor.h:206
void handle_dlopen()
Definition: monitor.c:455
unsigned int bytesPerCodeBlk
Definition: monitor.h:155
void init_process()
Definition: monitor.c:430
hpcrun_ofile_desc_t ofile
Definition: monitor.h:186
struct prof * sprofs
Definition: monitor.h:159
char * opt_eventlist
Definition: monitor.c:129
int opt_flagscode
Definition: monitor.c:133
pid_t(* fork_fptr_t)(void)
Definition: monitor.h:227
hpcrun_profiles_desc_t * init_thread(int is_thread)
Definition: monitor.c:510
char opt_outpath[PATH_MAX]
Definition: monitor.c:130
#define PARAMS_EXECVE
Definition: monitor.h:220
hpcsys_profile_desc_t * vec
Definition: monitor.h:166
void hpcrun_parse_execl(const char ***argv, const char *const **envp, const char *arg, va_list arglist)
Definition: monitor.c:1874