HPCToolkit
thread_data.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 #ifndef THREAD_DATA_H
49 #define THREAD_DATA_H
50 
51 // This is called "thread data", but it applies to process as well
52 // (there is just 1 thread).
53 
54 #include <setjmp.h>
55 #include <signal.h>
56 #include <stdbool.h>
57 #include <stdint.h>
58 #include <stdlib.h>
59 #include <time.h>
60 
62 #include "newmem.h"
63 #include "epoch.h"
64 #include "cct2metrics.h"
66 
67 #include <lush/lush-pthread.i>
69 
70 #include <lib/prof-lean/hpcio.h>
72 
73 typedef struct {
74  sigjmp_buf jb;
75 } sigjmp_buf_t;
76 
77 typedef struct gpu_data_t {
78  // True if this thread is at CuXXXXSynchronize.
80  // maintains state to account for overload potential
81  uint8_t overload_state;
82  // current active stream
83  uint64_t active_stream;
84  // last examined event
85  void * event_node;
86  // maintain the total number of threads (global: think shared
87  // blaming) at synchronize (could be device/stream/...)
89  // holds the number of times the above accum_num_sync_threads
90  // is updated
92 } gpu_data_t;
93 
94 
95 /* ******
96  TODO:
97 
98  thread_data_t needs more structure.
99  Organize according to following plan:
100 
101  backtrace_buffer
102  factor out of the current epoch, and put it as a separate item in thread_data
103  it will become an opaque datatype
104  each epoch has:
105  loadmap (currently called epoch)
106  cct (the main sample data container)
107  cct_ctxt (pthread creation context)
108  exceptions package (items used when something goes wrong)
109  bad_unwind
110  mem_error
111  handling_sample (used to distinguish a sample-based segv from a user segv)
112  thread_locks package
113  (each of the 'lock' elements is true if this thread owns the lock.
114  locks must be released in an exceptional situation)
115  fnbounds_lock
116  splay_lock
117  sample source package
118  event_set
119  ss_state
120  trampoline
121  all trampoline-related items should be collected into a struct
122  NOTE: a single backtrace_buffer item will replace the collection of fields
123  that currently implement the cached backtrace.
124  io_support
125  This is a collection of files:
126  trace_file
127  hpcrun_file
128 
129  lushPthr_t
130  lush items can stand alone
131 
132  debug
133  a few bools & integers. General purpose. Used for simulating error conditions.
134  */
135 
136 
137 typedef struct thread_data_t {
138  int idle; // indicate whether the thread is idle
139 
140  // ----------------------------------------
141  // hpcrun_malloc() memory data structures
142  // ----------------------------------------
144  int mem_low;
145 
146  // ----------------------------------------
147  // sample sources
148  // ----------------------------------------
149 
150  source_state_t* ss_state; // allocate at initialization time
151  source_info_t* ss_info; // allocate at initialization time
152 
153  struct sigevent sigev; // POSIX real-time timer
154  timer_t timerid;
156 
157  uint64_t last_time_us; // microseconds
158 
159  // ----------------------------------------
160  // core_profile_trace_data contains the following
161  // epoch: loadmap + cct + cct_ctxt
162  // cct2metrics map: associate a metric_set with
163  // tracing: trace_min_time_us and trace_max_time_us
164  // IO support file handle: hpcrun_file;
165  // Perf event support
166  // ----------------------------------------
167 
169 
170  // ----------------------------------------
171  // backtrace buffer
172  // ----------------------------------------
173 
174  // btbuf_beg btbuf_end
175  // | |
176  // v low VMAs v
177  // +------------------------------------------------------------+
178  // [new backtrace ) [cached backtrace )
179  // +------------------------------------------------------------+
180  // ^ ^
181  // | |
182  // btbuf_cur btbuf_sav
183 
184  frame_t* btbuf_cur; // current frame when actively constructing a backtrace
185  frame_t* btbuf_beg; // beginning of the backtrace buffer
186  // also, location of the innermost frame in
187  // newly recorded backtrace (although skipInner may
188  // adjust the portion of the backtrace that is recorded)
189  frame_t* btbuf_end; // end of the current backtrace buffer
190  frame_t* btbuf_sav; // innermost frame in cached backtrace
191 
192 
193  backtrace_t bt; // backtrace used for unwinding
194 
195  // ----------------------------------------
196  // trampoline
197  // ----------------------------------------
198  bool tramp_present; // TRUE if a trampoline installed; FALSE otherwise
199  void* tramp_retn_addr; // return address that the trampoline replaced
200  void* tramp_loc; // current (stack) location of the trampoline
201  size_t cached_frame_count; // (sanity check) length of cached frame list
202  frame_t* cached_bt; // the latest backtrace (start)
203  frame_t* cached_bt_end; // the latest backtrace (end)
204  frame_t* cached_bt_buf_end; // the end of the cached backtrace buffer
205  frame_t* tramp_frame; // (cached) frame assoc. w/ cur. trampoline loc.
206  cct_node_t* tramp_cct_node; // cct node associated with the trampoline
207 
208  // ----------------------------------------
209  // exception stuff
210  // ----------------------------------------
214 
218 
219  // ----------------------------------------
220  // Logical unwinding
221  // ----------------------------------------
222  lushPthr_t pthr_metrics;
223 
224 
225  // ----------------------------------------
226  // debug stuff
227  // ----------------------------------------
228  bool debug1;
229 
230  // ----------------------------------------
231  // miscellaneous
232  // ----------------------------------------
233  // Set to 1 while inside hpcrun code for safe sampling.
235 
236  // True if this thread is inside dlopen or dlclose. A synchronous
237  // override that is called from dlopen (eg, malloc) must skip this
238  // sample or else deadlock on the dlopen lock.
240 
241 #ifdef ENABLE_CUDA
242  gpu_data_t gpu_data;
243 #endif
244 
245 } thread_data_t;
246 
247 
249 
250 
251 void hpcrun_init_pthread_key(void);
252 void hpcrun_set_thread0_data(void);
254 
255 
256 #define TD_GET(field) hpcrun_get_thread_data()->field
257 
258 extern thread_data_t* (*hpcrun_get_thread_data)(void);
259 extern bool (*hpcrun_td_avail)(void);
260 extern thread_data_t* hpcrun_safe_get_td(void);
261 
262 void hpcrun_unthreaded_data(void);
263 void hpcrun_threaded_data(void);
264 
265 
267 
268 void
269 hpcrun_thread_data_init(int id, cct_ctxt_t* thr_ctxt, int is_child, size_t n_sources);
270 
271 
272 void hpcrun_cached_bt_adjust_size(size_t n);
274 void hpcrun_ensure_btbuf_avail(void);
275 
276 
277 // utilities to match previous api
278 #define hpcrun_get_thread_epoch() TD_GET(core_profile_trace_data.epoch)
279 
280 #endif // !defined(THREAD_DATA_H)
uint8_t overload_state
Definition: thread_data.h:81
size_t cached_frame_count
Definition: thread_data.h:201
bool(* hpcrun_td_avail)(void)
Definition: thread_data.c:169
void hpcrun_set_thread_data(thread_data_t *td)
Definition: thread_data.c:116
source_state_t
bool deadlock_drop
Definition: thread_data.h:215
void hpcrun_threaded_data(void)
Definition: thread_data.c:195
void hpcrun_unthreaded_data(void)
Definition: thread_data.c:187
bool is_thread_at_cuda_sync
Definition: thread_data.h:79
static size_t n_sources
void hpcrun_ensure_btbuf_avail(void)
Definition: thread_data.c:426
lushPthr_t pthr_metrics
Definition: thread_data.h:222
frame_t * cached_bt_buf_end
Definition: thread_data.h:204
void * tramp_loc
Definition: thread_data.h:200
frame_t * cached_bt_end
Definition: thread_data.h:203
void hpcrun_thread_data_init(int id, cct_ctxt_t *thr_ctxt, int is_child, size_t n_sources)
Definition: thread_data.c:265
frame_t * btbuf_cur
Definition: thread_data.h:184
uint64_t accum_num_samples
Definition: thread_data.h:91
struct gpu_data_t gpu_data_t
sigjmp_buf_t bad_unwind
Definition: thread_data.h:213
void * tramp_retn_addr
Definition: thread_data.h:199
void hpcrun_init_pthread_key(void)
Definition: thread_data.c:96
void * event_node
Definition: thread_data.h:85
frame_t * cached_bt
Definition: thread_data.h:202
frame_t * tramp_frame
Definition: thread_data.h:205
thread_data_t * hpcrun_safe_get_td(void)
Definition: thread_data.c:140
static const size_t HPCRUN_TraceBufferSz
Definition: thread_data.h:248
sigjmp_buf_t bad_interval
Definition: thread_data.h:212
frame_t * btbuf_sav
Definition: thread_data.h:190
source_state_t * ss_state
Definition: thread_data.h:150
void hpcrun_cached_bt_adjust_size(size_t n)
Definition: thread_data.c:373
core_profile_trace_data_t core_profile_trace_data
Definition: thread_data.h:168
sigjmp_buf_t * current_jmp_buf
Definition: thread_data.h:211
struct thread_data_t thread_data_t
bool tramp_present
Definition: thread_data.h:198
cct_node_t * tramp_cct_node
Definition: thread_data.h:206
uint64_t active_stream
Definition: thread_data.h:83
thread_data_t * hpcrun_allocate_thread_data(int id)
Definition: thread_data.c:208
uint64_t accum_num_sync_threads
Definition: thread_data.h:88
timer_t timerid
Definition: thread_data.h:154
uint64_t last_time_us
Definition: thread_data.h:157
frame_t * hpcrun_expand_btbuf(void)
Definition: thread_data.c:390
Definition: cct.c:96
hpcrun_meminfo_t memstore
Definition: thread_data.h:143
frame_t * btbuf_beg
Definition: thread_data.h:185
Definition: frame.h:58
<!-- ********************************************************************--> n<!-- HPCToolkit Experiment DTD --> n<!-- Version 2.1 --> n<!-- ********************************************************************--> n<!ELEMENT HPCToolkitExperiment(Header,(SecCallPathProfile|SecFlatProfile) *)> n<!ATTLIST HPCToolkitExperiment\n version CDATA #REQUIRED > n n<!-- ******************************************************************--> n n<!-- Info/NV:flexible name-value pairs:(n) ame;(t) ype;(v) alue --> n<!ELEMENT Info(NV *)> n<!ATTLIST Info\n n CDATA #IMPLIED > n<!ELEMENT NV EMPTY > n<!ATTLIST NV\n n CDATA #REQUIRED\n t CDATA #IMPLIED\n v CDATA #REQUIRED > n n<!-- ******************************************************************--> n<!-- Header --> n<!-- ******************************************************************--> n<!ELEMENT Header(Info *)> n<!ATTLIST Header\n n CDATA #REQUIRED > n n<!-- ******************************************************************--> n<!-- Section Header --> n<!-- ******************************************************************--> n<!ELEMENT SecHeader(MetricTable?, MetricDBTable?, TraceDBTable?, LoadModuleTable?, FileTable?, ProcedureTable?, Info *)> n n<!-- MetricTable:--> n<!ELEMENT MetricTable(Metric) * > n n<!-- Metric:(i) d;(n) ame --> n<!--(v) alue-type:transient type of values --> n<!--(t) ype:persistent type of metric --> n<!-- fmt:format;show;--> n<!ELEMENT Metric(MetricFormula *, Info?)> n<!ATTLIST Metric\n i CDATA #REQUIRED\n n CDATA #REQUIRED\n es CDATA #IMPLIED\n em CDATA #IMPLIED\n ep CDATA #IMPLIED\n v(raw|final|derived-incr|derived) \"raw\\ t (inclusive|exclusive|nil) \nil\\ partner CDATA #IMPLIED\ fmt CDATA #IMPLIED\ show (1|0) \1\\ show-percent (1|0) \1> n n<!-- MetricFormula represents derived metrics: (t)ype; (frm): formula --> n<!ELEMENT MetricFormula (Info?)> n<!ATTLIST MetricFormula\ t (combine|finalize) \finalize\\ i CDATA #IMPLIED\ frm CDATA #REQUIRED> n n<!-- Metric data, used in sections: (n)ame [from Metric]; (v)alue --> n<!ELEMENT M EMPTY> n<!ATTLIST M\ n CDATA #REQUIRED\ v CDATA #REQUIRED> n n<!-- MetricDBTable: --> n<!ELEMENT MetricDBTable (MetricDB)*> n n<!-- MetricDB: (i)d; (n)ame --> n<!-- (t)ype: persistent type of metric --> n<!-- db-glob: file glob describing files in metric db --> n<!-- db-id: id within metric db --> n<!-- db-num-metrics: number of metrics in db --> n<!-- db-header-sz: size (in bytes) of a db file header --> n<!ELEMENT MetricDB EMPTY> n<!ATTLIST MetricDB\ i CDATA #REQUIRED\ n CDATA #REQUIRED\ t (inclusive|exclusive|nil) \nil\\ partner CDATA #IMPLIED\ db-glob CDATA #IMPLIED\ db-id CDATA #IMPLIED\ db-num-metrics CDATA #IMPLIED\ db-header-sz CDATA #IMPLIED> n n<!-- TraceDBTable: --> n<!ELEMENT TraceDBTable (TraceDB)> n n<!-- TraceDB: (i)d --> n<!-- db-min-time: min beginning time stamp (global) --> n<!-- db-max-time: max ending time stamp (global) --> n<!ELEMENT TraceDB EMPTY> n<!ATTLIST TraceDB\ i CDATA #REQUIRED\ db-glob CDATA #IMPLIED\ db-min-time CDATA #IMPLIED\ db-max-time CDATA #IMPLIED\ db-header-sz CDATA #IMPLIED> n n<!-- LoadModuleTable assigns a short name to a load module --> n<!ELEMENT LoadModuleTable (LoadModule)*> n n<!ELEMENT LoadModule (Info?)> n<!ATTLIST LoadModule\ i CDATA #REQUIRED\ n CDATA #REQUIRED> n n<!-- FileTable assigns a short name to a file --> n<!ELEMENT FileTable (File)*> n n<!ELEMENT File (Info?)> n<!ATTLIST File\ i CDATA #REQUIRED\ n CDATA #REQUIRED> n n<!-- ProcedureTable assigns a short name to a procedure --> n<!ELEMENT ProcedureTable (Procedure)*> n n<!-- Info/NV: flexible name-value pairs: (n)ame; (t)ype; (v)alue --> n<!-- f: family of the procedure (fake, root, ...)--> n<!ELEMENT Procedure (Info?)> n<!ATTLIST Procedure\ i CDATA #REQUIRED\ n CDATA #REQUIRED\ f CDATA #IMPLIED> n n<!-- ****************************************************************** --> n<!-- Section: Call path profile --> n<!-- ****************************************************************** --> n<!ELEMENT SecCallPathProfile (SecHeader, SecCallPathProfileData)> n<!ATTLIST SecCallPathProfile\ i CDATA #REQUIRED\ n CDATA #REQUIRED> n n<!ELEMENT SecCallPathProfileData (PF|M)*> n<!-- Procedure frame --> n<!-- (i)d: unique identifier for cross referencing --> n<!-- (s)tatic scope id --> n<!-- (n)ame: a string or an id in ProcedureTable --> n<!-- (lm) load module: a string or an id in LoadModuleTable --> n<!-- (f)ile name: a string or an id in LoadModuleTable --> n<!-- (l)ine range: \beg-end\ (inclusive range) --> n<!-- (a)lien: whether frame is alien to enclosing P --> n<!-- (str)uct: hpcstruct node id --> n<!-- (t)ype: hpcrun node type: memory access, variable declaration, ... --> n<!-- (v)ma-range-set: \{[beg-end), [beg-end)...}\ --> n<!ELEMENT PF (PF|Pr|L|C|S|M)*> n<!ATTLIST PF\ i CDATA #IMPLIED\ s CDATA #IMPLIED\ n CDATA #REQUIRED\ lm CDATA #IMPLIED\ f CDATA #IMPLIED\ l CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Procedure (static): GOAL: replace with 'P' --> n<!ELEMENT Pr (Pr|L|C|S|M)*> n<!ATTLIST Pr\ i CDATA #IMPLIED\ s CDATA #IMPLIED\ n CDATA #REQUIRED\ lm CDATA #IMPLIED\ f CDATA #IMPLIED\ l CDATA #IMPLIED\ a (1|0) \0\\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Callsite (a special StatementRange) --> n<!ELEMENT C (PF|M)*> n<!ATTLIST C\ i CDATA #IMPLIED\ s CDATA #IMPLIED\ l CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n n<!-- ****************************************************************** --> n<!-- Section: Flat profile --> n<!-- ****************************************************************** --> n<!ELEMENT SecFlatProfile (SecHeader, SecFlatProfileData)> n<!ATTLIST SecFlatProfile\ i CDATA #REQUIRED\ n CDATA #REQUIRED> n n<!ELEMENT SecFlatProfileData (LM|M)*> n<!-- Load module: (i)d; (n)ame; (v)ma-range-set --> n<!ELEMENT LM (F|P|M)*> n<!ATTLIST LM\ i CDATA #IMPLIED\ n CDATA #REQUIRED\ v CDATA #IMPLIED> n<!-- File --> n<!ELEMENT F (P|L|S|M)*> n<!ATTLIST F\ i CDATA #IMPLIED\ n CDATA #REQUIRED> n<!-- Procedure (Note 1) --> n<!ELEMENT P (P|A|L|S|C|M)*> n<!ATTLIST P\ i CDATA #IMPLIED\ n CDATA #REQUIRED\ l CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Alien (Note 1) --> n<!ELEMENT A (A|L|S|C|M)*> n<!ATTLIST A\ i CDATA #IMPLIED\ f CDATA #IMPLIED\ n CDATA #IMPLIED\ l CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Loop (Note 1,2) --> n<!ELEMENT L (A|Pr|L|S|C|M)*> n<!ATTLIST L\ i CDATA #IMPLIED\ s CDATA #IMPLIED\ l CDATA #IMPLIED\ f CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Statement (Note 2) --> n<!-- (it): trace record identifier --> n<!ELEMENT S (S|M)*> n<!ATTLIST S\ i CDATA #IMPLIED\ it CDATA #IMPLIED\ s CDATA #IMPLIED\ l CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Note 1: Contained Cs may not contain PFs --> n<!-- Note 2: The 's' attribute is not used for flat profiles --> n
void hpcrun_set_thread0_data(void)
Definition: thread_data.c:108
sigjmp_buf jb
Definition: thread_data.h:74
source_info_t * ss_info
Definition: thread_data.h:151
backtrace_t bt
Definition: thread_data.h:193
frame_t * btbuf_end
Definition: thread_data.h:189
#define HPCIO_RWBufferSz
Definition: hpcio.h:86