HPCToolkit
trace.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 // global includes
49 //*********************************************************************
50 
51 #include <stdio.h>
52 #include <sys/time.h>
53 #include <assert.h>
54 #include <limits.h>
55 
56 
57 //*********************************************************************
58 // local includes
59 //*********************************************************************
60 
61 #include <include/hpctoolkit-config.h>
62 
63 #include "disabled.h"
64 #include "env.h"
65 #include "files.h"
66 #include "monitor.h"
67 #include "rank.h"
68 #include "string.h"
69 #include "trace.h"
70 #include "thread_data.h"
71 #include "sample_prob.h"
72 
73 #include <memory/hpcrun-malloc.h>
74 #include <messages/messages.h>
75 
76 #include <lib/prof-lean/hpcfmt.h>
78 #include <lib/prof-lean/hpcio.h>
80 
81 
82 //*********************************************************************
83 // type declarations
84 //*********************************************************************
85 
86 
87 
88 //*********************************************************************
89 // forward declarations
90 //*********************************************************************
91 
92 static void hpcrun_trace_file_validate(int valid, char *op);
93 static inline void hpcrun_trace_append_with_time_real(core_profile_trace_data_t *cptd, unsigned int call_path_id, uint metric_id, uint64_t microtime);
94 
95 
96 //*********************************************************************
97 // local variables
98 //*********************************************************************
99 
100 static int tracing = 0;
101 
102 //*********************************************************************
103 // interface operations
104 //*********************************************************************
105 
106 int
108 {
109  return tracing;
110 }
111 
112 
113 void
115 {
116  if (getenv(HPCRUN_TRACE)) {
117  tracing = 1;
118  TMSG(TRACE, "Tracing is ON");
119  }
120 }
121 
122 
123 void
125 {
126  if (hpcrun_get_disabled()) {
127  tracing = 0;
128  return;
129  }
130 
131  TMSG(TRACE, "Trace open called");
132  // With fractional sampling, if this process is inactive, then don't
133  // open an output file, not even /dev/null.
135 
136  TMSG(TRACE, "Hit active portion");
137  int fd, ret;
138 
139  // I think unlocked is ok here (we don't overlap any system
140  // locks). At any rate, locks only protect against threads, they
141  // don't help with signal handlers (that's much harder).
142  fd = hpcrun_open_trace_file(cptd->id);
143  hpcrun_trace_file_validate(fd >= 0, "open");
145  ret = hpcio_outbuf_attach(&cptd->trace_outbuf, fd, cptd->trace_buffer,
147  hpcrun_trace_file_validate(ret == HPCFMT_OK, "open");
148 
150 #ifdef DATACENTRIC_TRACE
151  flags.fields.isDataCentric = true;
152 #else
153  flags.fields.isDataCentric = false;
154 #endif
155 
156  ret = hpctrace_fmt_hdr_outbuf(flags, &cptd->trace_outbuf);
157  hpcrun_trace_file_validate(ret == HPCFMT_OK, "write header to");
158  }
159  TMSG(TRACE, "Trace open done");
160 }
161 
162 
163 void
164 hpcrun_trace_append_with_time(core_profile_trace_data_t *st, unsigned int call_path_id, uint metric_id, uint64_t microtime)
165 {
167  hpcrun_trace_append_with_time_real(st, call_path_id, metric_id, microtime);
168  }
169 }
170 
171 
172 void
174 {
176  struct timeval tv;
177  int ret = gettimeofday(&tv, NULL);
178  assert(ret == 0 && "in trace_append: gettimeofday failed!");
179  uint64_t microtime = ((uint64_t)tv.tv_usec
180  + (((uint64_t)tv.tv_sec) * 1000000));
181 
182  // mark the leaf of a call path recorded in a trace record for retention
183  // so that the call path associated with the trace record can be recovered.
184  hpcrun_cct_retain(node);
185 
186  int32_t call_path_id = hpcrun_cct_persistent_id(node);
187 
188  hpcrun_trace_append_with_time_real(cptd, call_path_id, metric_id, microtime);
189  }
190 }
191 
192 
193 
194 void
196 {
197  TMSG(TRACE, "Trace close called");
199 
200  TMSG(TRACE, "Trace active close code");
201  int ret = hpcio_outbuf_close(&cptd->trace_outbuf);
202  if (ret != HPCFMT_OK) {
203  EMSG("unable to flush and close trace file");
204  }
205 
206  int rank = hpcrun_get_rank();
207  if (rank >= 0) {
208  hpcrun_rename_trace_file(rank, cptd->id);
209  }
210  }
211  TMSG(TRACE, "trace close done");
212 }
213 
214 //*********************************************************************
215 // private operations
216 //*********************************************************************
217 
218 static inline void hpcrun_trace_append_with_time_real(core_profile_trace_data_t *cptd, unsigned int call_path_id, uint metric_id, uint64_t microtime)
219 {
220  if (cptd->trace_min_time_us == 0) {
221  cptd->trace_min_time_us = microtime;
222  }
223 
224  // TODO: should we need this check???
225  if(cptd->trace_max_time_us < microtime) {
226  cptd->trace_max_time_us = microtime;
227  }
228 
229  hpctrace_fmt_datum_t trace_datum;
230  trace_datum.time = microtime;
231  trace_datum.cpId = (uint32_t)call_path_id;
232  //TODO: was not in GPU version
233  trace_datum.metricId = (uint32_t)metric_id;
234 
236 #ifdef DATACENTRIC_TRACE
237  flags.fields.isDataCentric = true;
238 #else
239  flags.fields.isDataCentric = false;
240 #endif
241 
242  int ret = hpctrace_fmt_datum_outbuf(&trace_datum, flags, &cptd->trace_outbuf);
243  hpcrun_trace_file_validate(ret == HPCFMT_OK, "append");
244 }
245 
246 
247 static void
248 hpcrun_trace_file_validate(int valid, char *op)
249 {
250  if (!valid) {
251  EMSG("unable to %s trace file\n", op);
253  }
254 }
static void hpcrun_trace_append_with_time_real(core_profile_trace_data_t *cptd, unsigned int call_path_id, uint metric_id, uint64_t microtime)
Definition: trace.c:218
static int tracing
Definition: trace.c:100
void hpcrun_trace_open(core_profile_trace_data_t *cptd)
Definition: trace.c:124
void hpcrun_trace_append(core_profile_trace_data_t *cptd, cct_node_t *node, uint metric_id)
Definition: trace.c:173
cct_node_t * node
Definition: cct.c:128
int hpctrace_fmt_datum_outbuf(hpctrace_fmt_datum_t *x, hpctrace_hdr_flags_t flags, hpcio_outbuf_t *outbuf)
Definition: hpcrun-fmt.c:927
#define HPCIO_OUTBUF_UNLOCKED
Definition: hpcio-buffer.h:75
const hpctrace_hdr_flags_t hpctrace_hdr_flags_NULL
Definition: hpcrun-fmt.c:789
void hpcrun_trace_close(core_profile_trace_data_t *cptd)
Definition: trace.c:195
int hpcrun_sample_prob_active(void)
Definition: sample_prob.c:193
static const size_t HPCRUN_TraceBufferSz
Definition: thread_data.h:248
unsigned int uint
Definition: uint.h:124
int hpcrun_trace_isactive()
Definition: trace.c:107
#define EMSG
Definition: messages.h:70
static void hpcrun_trace_file_validate(int valid, char *op)
Definition: trace.c:248
int hpcrun_open_trace_file(int thread)
Definition: files.c:450
void hpcrun_cct_retain(cct_node_t *x)
Definition: cct.c:567
int hpcio_outbuf_close(hpcio_outbuf_t *outbuf)
Definition: hpcio-buffer.c:241
void * hpcrun_malloc(size_t size)
Definition: mem.c:275
int32_t hpcrun_cct_persistent_id(cct_node_t *x)
Definition: cct.c:363
int hpcrun_get_rank(void)
Definition: rank.c:89
#define TMSG(f,...)
Definition: messages.h:93
hpctrace_hdr_flags_bitfield fields
Definition: hpcrun-fmt.h:637
int hpcio_outbuf_attach(hpcio_outbuf_t *outbuf, int fd, void *buf_start, size_t buf_size, int flags)
Definition: hpcio-buffer.c:148
void hpcrun_trace_append_with_time(core_profile_trace_data_t *st, unsigned int call_path_id, uint metric_id, uint64_t microtime)
Definition: trace.c:164
void hpcrun_trace_init()
Definition: trace.c:114
#define NULL
Definition: ElfHelper.cpp:85
Definition: cct.c:96
int hpctrace_fmt_hdr_outbuf(hpctrace_hdr_flags_t flags, hpcio_outbuf_t *outbuf)
Definition: hpcrun-fmt.c:833
bool hpcrun_get_disabled(void)
Definition: disabled.c:80
int hpcrun_rename_trace_file(int rank, int thread)
Definition: files.c:512
void monitor_real_abort(void)
const char * HPCRUN_TRACE
Definition: env.c:51