HPCToolkit
epoch.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 #include <stdio.h>
48 #include <fcntl.h>
49 #include <stdlib.h>
50 #include <errno.h>
51 #include <string.h>
52 
53 #include "env.h"
54 #include "epoch.h"
55 #include "loadmap.h"
56 #include "name.h"
57 #include "thread_data.h"
58 #include "hpcrun_return_codes.h"
59 #include "monitor.h"
61 #include <messages/messages.h>
62 #include <cct/cct_bundle.h>
63 
64 void
66 {
67  epoch->next = NULL;
68  TD_GET(core_profile_trace_data.epoch) = epoch;
69 }
70 
71 void
73 {
74  TMSG(EPOCH,"init");
77 
78  hpcrun_cct_bundle_init(&(epoch->csdata), ctxt);
79 
80  epoch->loadmap = hpcrun_getLoadmap();
81  epoch->next = NULL;
82 }
83 
84 epoch_t*
86 {
87  /* ugh, nasty race condition here:
88 
89  1. shared library epoch has changed since the last profile
90  signal, so we enter the if;
91 
92  2. somebody else dlclose()'s a library which holds something
93  located in our backtrace. this is not in itself a problem,
94  since we don't bother doing anything on dlclose()...;
95 
96  3. somebody else (thread in step 2 or a different thread)
97  dlopen()'s a new shared object, which begins an entirely
98  new loadmap--one which does not include the shared object
99  which resides in our backtrace;
100 
101  4. we create a new epoch which receives the loadmap from step 3,
102  not step 1, which is wrong.
103 
104  attempt to take baby steps to stop this. more drastic action
105  would involve grabbing the loadmap lock, but I believe that would
106  be unacceptably slow (both in the atomic instruction overhead
107  and the simple fact that most programs are not frequent users
108  of dl*). */
109 
110  hpcrun_loadmap_t* current = hpcrun_getLoadmap();
111 
112  if(epoch->loadmap != current) {
113  TMSG(LOADMAP, "Need new loadmap!");
114  TMSG(MALLOC," -new_epoch-");
115  epoch_t* newepoch = hpcrun_malloc(sizeof(epoch_t));
116 
117  TMSG(EPOCH, "check_new_epoch creating new epoch (new loadmap/cct pair)...");
118 
119  memcpy(newepoch, epoch, sizeof(epoch_t));
120  hpcrun_cct_bundle_init(&(epoch->csdata), (epoch->csdata).ctxt);
121 
123 
124  newepoch->loadmap = current;
125  newepoch->next = epoch;
126 
127  TD_GET(core_profile_trace_data.epoch) = newepoch;
128  return newepoch;
129  }
130  else {
131  return epoch;
132  }
133 }
134 
135 int
137 
138  TMSG(EPOCH,"--Fini");
139  return HPCRUN_OK;
140 }
141 
142 void
144 {
145  //
146  // create a new epoch list:
147  // preserve current loadmap
148  // re-init cct
149  // reset epoch list for thread to point be a list consisting of only the new epoch
150  //
151  TMSG(EPOCH_RESET,"--started");
152  epoch_t *epoch = hpcrun_get_thread_epoch();
153  epoch_t *newepoch = hpcrun_malloc(sizeof(epoch_t));
154  memcpy(newepoch, epoch, sizeof(epoch_t));
155  TMSG(EPOCH_RESET, "check new loadmap = old loadmap = %d", newepoch->loadmap == epoch->loadmap);
156  hpcrun_cct_bundle_init(&(newepoch->csdata), newepoch->csdata_ctxt); // reset cct
157  hpcrun_reset_epoch(newepoch);
158  TMSG(EPOCH_RESET," ==> no new epoch for next sample = %d", newepoch->loadmap == hpcrun_getLoadmap());
159 }
cct_ctxt_t * csdata_ctxt
Definition: epoch.h:66
hpcrun_loadmap_t * loadmap
Definition: epoch.h:67
#define hpcrun_get_thread_epoch()
Definition: thread_data.h:278
void hpcrun_cct_bundle_init(cct_bundle_t *bundle, cct_ctxt_t *ctxt)
Definition: cct_bundle.c:85
cct_bundle_t csdata
Definition: epoch.h:65
void hpcrun_epoch_init(cct_ctxt_t *ctxt)
Definition: epoch.c:72
epoch_t * hpcrun_check_for_new_loadmap(epoch_t *epoch)
Definition: epoch.c:85
void hpcrun_reset_epoch(epoch_t *epoch)
Definition: epoch.c:65
core_profile_trace_data_t core_profile_trace_data
Definition: thread_data.h:168
Definition: epoch.h:64
void * hpcrun_malloc(size_t size)
Definition: mem.c:275
#define TD_GET(field)
Definition: thread_data.h:256
int hpcrun_epoch_fini(epoch_t *x)
Definition: epoch.c:136
#define TMSG(f,...)
Definition: messages.h:93
#define NULL
Definition: ElfHelper.cpp:85
#define HPCRUN_OK
void hpcrun_epoch_reset(void)
Definition: epoch.c:143
void hpcrun_trampoline_remove(void)
Definition: trampoline.c:197
struct epoch_t * next
Definition: epoch.h:68
thread_data_t *(* hpcrun_get_thread_data)(void)
Definition: thread_data.c:168
hpcrun_loadmap_t * hpcrun_getLoadmap()
Definition: loadmap.c:501