| 1 | /** |
|---|
| 2 | * (C) Copyright 2014- ECMWF. |
|---|
| 3 | * |
|---|
| 4 | * This software is licensed under the terms of the Apache Licence Version 2.0 |
|---|
| 5 | * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
|---|
| 6 | * |
|---|
| 7 | * In applying this licence, ECMWF does not waive the privileges and immunities |
|---|
| 8 | * granted to it by virtue of its status as an intergovernmental organisation |
|---|
| 9 | * nor does it submit to any jurisdiction. |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | #ifndef _DRHOOK_H_ |
|---|
| 13 | #define _DRHOOK_H_ |
|---|
| 14 | |
|---|
| 15 | #ifndef GNUC_BTRACE |
|---|
| 16 | #define GNUC_BTRACE 128 |
|---|
| 17 | #endif |
|---|
| 18 | |
|---|
| 19 | #ifdef _DRHOOK_C_ |
|---|
| 20 | |
|---|
| 21 | #if defined(__GNUC__) |
|---|
| 22 | #define _GNU_SOURCE |
|---|
| 23 | #endif |
|---|
| 24 | |
|---|
| 25 | #include <stdio.h> |
|---|
| 26 | #include <string.h> |
|---|
| 27 | #include <stdlib.h> |
|---|
| 28 | #include <sys/time.h> |
|---|
| 29 | #include <sys/resource.h> |
|---|
| 30 | #include <unistd.h> |
|---|
| 31 | #include <ctype.h> |
|---|
| 32 | #include <signal.h> |
|---|
| 33 | #include <errno.h> |
|---|
| 34 | #include <time.h> |
|---|
| 35 | #include <math.h> |
|---|
| 36 | #include <sys/syscall.h> |
|---|
| 37 | #include <sys/types.h> |
|---|
| 38 | #include <pthread.h> |
|---|
| 39 | #include <limits.h> |
|---|
| 40 | #ifdef __NEC__ |
|---|
| 41 | static int backtrace(void **buffer, int size) { return 0; } |
|---|
| 42 | #else |
|---|
| 43 | #include <execinfo.h> |
|---|
| 44 | #endif |
|---|
| 45 | #include <sys/file.h> |
|---|
| 46 | |
|---|
| 47 | //#ifdef _OPENMP |
|---|
| 48 | //#include <omp.h> |
|---|
| 49 | //#endif |
|---|
| 50 | |
|---|
| 51 | #ifdef RS6K |
|---|
| 52 | #include <fptrap.h> |
|---|
| 53 | #endif |
|---|
| 54 | |
|---|
| 55 | #ifdef VPP |
|---|
| 56 | #include <ucontext.h> |
|---|
| 57 | #endif |
|---|
| 58 | |
|---|
| 59 | int drhook_lhook = 1; |
|---|
| 60 | #else |
|---|
| 61 | extern int drhook_lhook; |
|---|
| 62 | #endif |
|---|
| 63 | |
|---|
| 64 | #ifndef O_LOCK_DONE |
|---|
| 65 | #define O_LOCK_DONE |
|---|
| 66 | |
|---|
| 67 | /* OpenMP/ODB lock type */ |
|---|
| 68 | /* Keep consistent with "odb/include/privpub.h" */ |
|---|
| 69 | /* Be ALSO consistent with OML_LOCK_KIND in ifsaux/module/oml_mod.F90 */ |
|---|
| 70 | |
|---|
| 71 | typedef long long int o_lock_t; /* i.e. 64-bit integer */ |
|---|
| 72 | |
|---|
| 73 | #define INIT_LOCKID_WITH_NAME(mylock, lockname) \ |
|---|
| 74 | coml_init_lockid_with_name_(mylock, lockname, strlen(lockname)) |
|---|
| 75 | |
|---|
| 76 | extern void coml_set_debug_(const int *konoff, int *kret); |
|---|
| 77 | extern void coml_init_lock_(); |
|---|
| 78 | extern void coml_init_lockid_(o_lock_t *mylock); |
|---|
| 79 | extern void coml_init_lockid_with_name_(o_lock_t *mylock, const char *name, int name_len); |
|---|
| 80 | extern void coml_set_lock_(); |
|---|
| 81 | extern void coml_set_lockid_(o_lock_t *mylock); |
|---|
| 82 | extern void coml_unset_lock_(); |
|---|
| 83 | extern void coml_unset_lockid_(o_lock_t *mylock); |
|---|
| 84 | extern void coml_test_lock_(int *is_set); |
|---|
| 85 | extern void coml_test_lockid_(int *is_set, o_lock_t *mylock); |
|---|
| 86 | extern void coml_in_parallel_(int *is_parallel_region); |
|---|
| 87 | |
|---|
| 88 | #endif |
|---|
| 89 | |
|---|
| 90 | /* drhook.c external interfaces */ |
|---|
| 91 | |
|---|
| 92 | extern void |
|---|
| 93 | c_drhook_getenv_(const char *s, |
|---|
| 94 | char *value, |
|---|
| 95 | /* Hidden arguments */ |
|---|
| 96 | int slen, |
|---|
| 97 | const int valuelen); |
|---|
| 98 | |
|---|
| 99 | extern void |
|---|
| 100 | c_drhook_memcounter_(const int *thread_id, |
|---|
| 101 | const long long int *size, |
|---|
| 102 | long long int *keyptr_addr); |
|---|
| 103 | |
|---|
| 104 | extern void |
|---|
| 105 | c_drhook_raise_(const int *sig); |
|---|
| 106 | |
|---|
| 107 | extern void |
|---|
| 108 | c_drhook_print_(const int *ftnunitno, |
|---|
| 109 | const int *thread_id, |
|---|
| 110 | const int *print_option, /* |
|---|
| 111 | 1=raw call counts |
|---|
| 112 | 2=calling tree |
|---|
| 113 | 3=profiling info |
|---|
| 114 | */ |
|---|
| 115 | int *level); |
|---|
| 116 | |
|---|
| 117 | extern void |
|---|
| 118 | c_drhook_init_signals_(const int *enforce); |
|---|
| 119 | |
|---|
| 120 | extern void |
|---|
| 121 | c_drhook_set_lhook_(const int *lhook); |
|---|
| 122 | |
|---|
| 123 | extern void |
|---|
| 124 | c_drhook_init_(const char *progname, |
|---|
| 125 | const int *num_threads |
|---|
| 126 | /* Hidden length */ |
|---|
| 127 | ,int progname_len); |
|---|
| 128 | |
|---|
| 129 | extern void |
|---|
| 130 | c_drhook_start_(const char *name, |
|---|
| 131 | const int *thread_id, |
|---|
| 132 | double *key, |
|---|
| 133 | const char *filename, |
|---|
| 134 | const int *sizeinfo |
|---|
| 135 | /* Hidden length */ |
|---|
| 136 | ,int name_len, int filename_len); |
|---|
| 137 | |
|---|
| 138 | extern void |
|---|
| 139 | c_drhook_end_(const char *name, |
|---|
| 140 | const int *thread_id, |
|---|
| 141 | const double *key, |
|---|
| 142 | const char *filename, |
|---|
| 143 | const int *sizeinfo |
|---|
| 144 | /* Hidden length */ |
|---|
| 145 | ,int name_len, int filename_len); |
|---|
| 146 | |
|---|
| 147 | extern void |
|---|
| 148 | c_drhook_watch_(const int *onoff, |
|---|
| 149 | const char *array_name, |
|---|
| 150 | const void *array_ptr, |
|---|
| 151 | const int *nbytes, |
|---|
| 152 | const int *abort_if_changed, |
|---|
| 153 | const int *printkey, |
|---|
| 154 | const int *nvals, |
|---|
| 155 | const int *print_traceback_when_set |
|---|
| 156 | /* Hidden length */ |
|---|
| 157 | ,int array_name_len); |
|---|
| 158 | |
|---|
| 159 | extern void |
|---|
| 160 | c_drhook_check_watch_(const char *where, |
|---|
| 161 | const int *allow_abort |
|---|
| 162 | /* Hidden length */ |
|---|
| 163 | , int where_len); |
|---|
| 164 | |
|---|
| 165 | /* see dr_hook_prt.F90 for below */ |
|---|
| 166 | extern void dr_hook_prt_logical_(const int *ftnunitno, const void *ptr, const int *nmax); |
|---|
| 167 | extern void dr_hook_prt_char_(const int *ftnunitno, const void *ptr, const int *nmax); |
|---|
| 168 | extern void dr_hook_prt_i4_(const int *ftnunitno, const void *ptr, const int *nmax); |
|---|
| 169 | extern void dr_hook_prt_i8_(const int *ftnunitno, const void *ptr, const int *nmax); |
|---|
| 170 | extern void dr_hook_prt_r4_(const int *ftnunitno, const void *ptr, const int *nmax); |
|---|
| 171 | extern void dr_hook_prt_r8_(const int *ftnunitno, const void *ptr, const int *nmax); |
|---|
| 172 | |
|---|
| 173 | extern void ec_meminfo_(const int *KU, const char *CDSTRING, |
|---|
| 174 | const int *KCOMM, const int *KBARR, |
|---|
| 175 | const int *KIOTASK, const int *KCALL, |
|---|
| 176 | int len_CDSTRING); /* see ec_meminfo.F90 */ |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | /* see comp_binding.F90 */ |
|---|
| 180 | extern void coml_get_max_threads_(int *numthreads); |
|---|
| 181 | extern void coml_get_num_threads_(int *numthreads); |
|---|
| 182 | extern void coml_my_thread_(int *mytid); |
|---|
| 183 | |
|---|
| 184 | /* see drhook.c */ |
|---|
| 185 | extern const char *drhook_TIMESTR(int tid); |
|---|
| 186 | extern const char *drhook_PREFIX(int tid); |
|---|
| 187 | |
|---|
| 188 | /**** C-interface to Dr.Hook ****/ |
|---|
| 189 | |
|---|
| 190 | extern void |
|---|
| 191 | Dr_Hook(const char *name, int option, double *handle, |
|---|
| 192 | const char *filename, int sizeinfo, |
|---|
| 193 | int name_len, int filename_len); |
|---|
| 194 | |
|---|
| 195 | #define DRHOOK_START_RECUR(name,recur) \ |
|---|
| 196 | static const char *drhook_name = #name; \ |
|---|
| 197 | static const int drhook_name_len = sizeof(#name) - 1; /* Compile time eval */ \ |
|---|
| 198 | static const char *drhook_filename = __FILE__; \ |
|---|
| 199 | static const int drhook_filename_len = sizeof(__FILE__) - 1; /* Compile time eval */ \ |
|---|
| 200 | double zhook_handle; \ |
|---|
| 201 | if (!recur && drhook_lhook) Dr_Hook(drhook_name, 0, &zhook_handle, \ |
|---|
| 202 | drhook_filename, 0, \ |
|---|
| 203 | drhook_name_len, drhook_filename_len); { |
|---|
| 204 | |
|---|
| 205 | #define DRHOOK_START(name) DRHOOK_START_RECUR(name,0) |
|---|
| 206 | |
|---|
| 207 | #define DRHOOK_START_BY_STRING_RECUR(name, recur) \ |
|---|
| 208 | static const char *drhook_name = name; \ |
|---|
| 209 | static const int drhook_name_len = sizeof(name) - 1; /* Compile time eval */ \ |
|---|
| 210 | static const char *drhook_filename = __FILE__; \ |
|---|
| 211 | static const int drhook_filename_len = sizeof(__FILE__) - 1; /* Compile time eval */ \ |
|---|
| 212 | double zhook_handle; \ |
|---|
| 213 | if (!recur && drhook_lhook) Dr_Hook(drhook_name, 0, &zhook_handle, \ |
|---|
| 214 | drhook_filename, 0, \ |
|---|
| 215 | drhook_name_len, drhook_filename_len); { |
|---|
| 216 | |
|---|
| 217 | #define DRHOOK_START_BY_STRING(name) DRHOOK_START_BY_STRING_RECUR(name,0) |
|---|
| 218 | |
|---|
| 219 | #define DRHOOK_RETURN_RECUR(sizeinfo,recur) \ |
|---|
| 220 | if (!recur && drhook_lhook) Dr_Hook(drhook_name, 1, &zhook_handle, \ |
|---|
| 221 | drhook_filename, sizeinfo, \ |
|---|
| 222 | drhook_name_len, drhook_filename_len) |
|---|
| 223 | |
|---|
| 224 | #define DRHOOK_RETURN(sizeinfo) DRHOOK_RETURN_RECUR(sizeinfo,0) |
|---|
| 225 | |
|---|
| 226 | #define DRHOOK_END_RECUR(sizeinfo,recur) ; } DRHOOK_RETURN_RECUR(sizeinfo,recur) |
|---|
| 227 | |
|---|
| 228 | #define DRHOOK_END(sizeinfo) DRHOOK_END_RECUR(sizeinfo,0) |
|---|
| 229 | |
|---|
| 230 | /* Fortran routines */ |
|---|
| 231 | |
|---|
| 232 | extern void |
|---|
| 233 | dr_hook_prt_(const int *ftnunitno, |
|---|
| 234 | const char *s |
|---|
| 235 | /* Hidden arguments */ |
|---|
| 236 | , int s_len); |
|---|
| 237 | |
|---|
| 238 | extern void |
|---|
| 239 | dr_hook_procinfo_(int *myproc, int *nproc); |
|---|
| 240 | |
|---|
| 241 | #endif /* _DRHOOK_H_ */ |
|---|
| 242 | |
|---|