Source

mpi3-fortran / ompi / contrib / vt / vt / vtlib / vt_user_region.c

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/**
 * VampirTrace
 * http://www.tu-dresden.de/zih/vampirtrace
 *
 * Copyright (c) 2005-2012, ZIH, TU Dresden, Federal Republic of Germany
 *
 * Copyright (c) 1998-2005, Forschungszentrum Juelich, Juelich Supercomputing
 *                          Centre, Federal Republic of Germany
 *
 * See the file COPYING in the package base directory for details
 **/

#include "config.h"

#include "vt_defs.h"
#include "vt_error.h"
#include "vt_fbindings.h"
#include "vt_mallocwrap.h"
#include "vt_pform.h"
#include "vt_thrd.h"
#include "vt_trc.h"
#define VTRACE
#undef VTRACE_NO_REGION
#include "vt_user.h"

#include "util/hash.h"

#include <stdlib.h>
#include <string.h>

static int vt_init = 1;        /* is initialization needed? */

#define VT_INIT \
  if ( vt_init ) { \
    vt_init = 0; \
    vt_open(); \
  }

#define REGION_HASH_MAX 1024
#define ADDR_HASH_MAX   1021

typedef struct HN_RegionS
{
  const char* name;   /* region name (hash-key) */
  const char* file;   /* source file (hash-key) */
  int lno;            /* line number within source file (hash-key) */
  uint32_t rid;       /* associated region identifier  */
  struct HN_RegionS* next;
} HN_RegionT;

typedef struct HN_AddrS
{
  unsigned long addr; /* region address (hash-key) */
  uint32_t rid;       /* associated region identifier */
  struct HN_AddrS* next;
} HN_AddrT;

static HN_RegionT* htab_region[REGION_HASH_MAX];
static HN_AddrT* htab_addr[ADDR_HASH_MAX];

static uint32_t hash_get_region(const char* name, const char* file,
                                int lno)
{
  uint32_t idx;
  HN_RegionT* curr;

  /* -- get hash index -- */
  idx = vt_hash(name, strlen(name), 0);
  if ( file )
  {
    idx = vt_hash(file, strlen(file), idx);
    idx = vt_hashtriple(lno, 0, 0, idx);
  }
  idx &= (REGION_HASH_MAX - 1);

  /* -- search for matching entry at calculated hash index -- */
  curr = htab_region[idx];
  while ( curr )
  {
    if ( strcmp( curr->name, name ) == 0 )
    {
      if ( ( !curr->file && !file ) ||
           ( curr->file && file && strcmp( curr->file, file ) == 0 &&
             curr->lno == lno ) )
      {
        return curr->rid;
      }
    }
    curr = curr->next;
  }

  return VT_NO_ID;
}

static void hash_put_region(const char* name, const char* file, int lno,
                            uint32_t rid)
{
  uint32_t idx;
  HN_RegionT* add;

  /* -- get hash index -- */
  idx = vt_hash(name, strlen(name), 0);
  if ( file )
  {
    idx = vt_hash(file, strlen(file), idx);
    idx = vt_hashtriple(lno, 0, 0, idx);
  }
  idx &= (REGION_HASH_MAX - 1);

  /* -- allocate/initialize new hash entry -- */
  add = (HN_RegionT*)calloc(1, sizeof(HN_RegionT));
  if ( add == NULL )
    vt_error();

  add->name = strdup(name);
  if ( file )
  {
    add->file = strdup(file);
    add->lno = lno;
  }
  add->rid = rid;

  /* -- insert new hash entry at calculated hash index -- */
  add->next = htab_region[idx];
  htab_region[idx] = add;
}

static uint32_t hash_get_addr(unsigned long addr)
{
  /* -- get hash index */
  unsigned long idx = addr % ADDR_HASH_MAX;

  /* -- search for matching entry at calculated hash index -- */
  HN_AddrT* curr = htab_addr[idx];
  while ( curr )
  {
    if ( curr->addr == addr )
      return curr->rid;
    curr = curr->next;
  }

  return VT_NO_ID;
}

static void hash_put_addr(unsigned long addr, uint32_t rid)
{
  /* -- get hash index */
  unsigned long idx = addr % ADDR_HASH_MAX;

  /* -- allocate/initialize new hash entry -- */
  HN_AddrT* add = (HN_AddrT*)malloc(sizeof(HN_AddrT));
  if ( add == NULL )
    vt_error();

  add->addr = addr;
  add->rid = rid;

  /* -- insert new hash entry at calculated hash index -- */
  add->next = htab_addr[idx];
  htab_addr[idx] = add;
}

static uint32_t register_region(unsigned long addr, const char* name,
                                const char* file, int lno)
{
  uint32_t rid;
  uint32_t fid;

  /* -- register file if available -- */
  if ( file && file[0] != '\0' )
  {
    fid = vt_def_scl_file(VT_CURRENT_THREAD, file);
  }
  else
  {
    fid = VT_NO_ID;
    lno = VT_NO_LNO;
  }

  /* -- register region and store region identifier -- */
  rid = vt_def_region(VT_CURRENT_THREAD, name, fid, lno, VT_NO_LNO, NULL,
                      VT_FUNCTION);
  if ( addr )
    hash_put_addr( addr, rid );
  else
    hash_put_region( name, (fid != VT_NO_ID) ? file : NULL, lno, rid );

  return rid;
}

void VT_User_start__(const char* name, const char* file, int lno)
{
  uint32_t rid;
  uint64_t time;

  VT_INIT;

  VT_SUSPEND_MALLOC_TRACING(VT_CURRENT_THREAD);

  time = vt_pform_wtime();

  /* -- get region identifier by address -- */
  if ( (rid = hash_get_addr((unsigned long)name)) == VT_NO_ID )
  {
    /* -- region entered the first time, register region -- */
#if (defined(VT_MT) || defined(VT_HYB))
    VTTHRD_LOCK_IDS();
    if ( (rid = hash_get_addr((unsigned long)name)) == VT_NO_ID )
      rid = register_region((unsigned long)name, name, file, lno);
    VTTHRD_UNLOCK_IDS();
#else /* VT_MT || VT_HYB */
    rid = register_region((unsigned long)name, name, file, lno);
#endif /* VT_MT || VT_HYB */
  }

  /* -- write enter record -- */
  vt_enter(VT_CURRENT_THREAD, &time, rid);

  VT_RESUME_MALLOC_TRACING(VT_CURRENT_THREAD);
}

void VT_User_end__(const char* name)
{
  uint64_t time;

  (void)name;

  VT_SUSPEND_MALLOC_TRACING(VT_CURRENT_THREAD);

  /* -- write exit record -- */
  time = vt_pform_wtime();
  vt_exit(VT_CURRENT_THREAD, &time);

  VT_RESUME_MALLOC_TRACING(VT_CURRENT_THREAD);
}

unsigned int VT_User_def__(const char* name, const char* file, int lno)
{
  uint32_t rid;

  VT_INIT;

  VT_SUSPEND_MALLOC_TRACING(VT_CURRENT_THREAD);

  /* -- get region identifier by name, file, and line number -- */
  if ( (rid = hash_get_region(name, file, lno)) == VT_NO_ID )
  {
    /* -- register region -- */
#if (defined(VT_MT) || defined(VT_HYB))
    VTTHRD_LOCK_IDS();
    if ( (rid = hash_get_region(name, file, lno)) == VT_NO_ID )
      rid = register_region(0, name, file, lno);
    VTTHRD_UNLOCK_IDS();
#else /* VT_MT || VT_HYB */
    rid = register_region(0, name, file, lno);
#endif /* VT_MT || VT_HYB */
  }

  VT_RESUME_MALLOC_TRACING(VT_CURRENT_THREAD);

  return rid;
}

void VT_User_start_id__(unsigned int rid)
{
  uint64_t time;

  VT_SUSPEND_MALLOC_TRACING(VT_CURRENT_THREAD);

  /* -- write enter record -- */
  time = vt_pform_wtime();
  vt_enter(VT_CURRENT_THREAD, &time, rid);

  VT_RESUME_MALLOC_TRACING(VT_CURRENT_THREAD);
}

void VT_User_end_id__(unsigned int rid)
{
  uint64_t time;

  (void)rid;

  VT_SUSPEND_MALLOC_TRACING(VT_CURRENT_THREAD);

  /* -- write exit record -- */
  time = vt_pform_wtime();
  vt_exit(VT_CURRENT_THREAD, &time);

  VT_RESUME_MALLOC_TRACING(VT_CURRENT_THREAD);
}

/*
 * Fortran version
 */

VT_DECLDEF(void VT_User_start___f(const char* name, const char* file, int* lno,
                                  int nl, int fl))
{
  uint32_t rid;
  uint64_t time;

  int namlen;
  int fillen;
  char fnambuf[128];
  char ffilbuf[1024];

  VT_INIT;

  VT_SUSPEND_MALLOC_TRACING(VT_CURRENT_THREAD);

  time = vt_pform_wtime();

  /* -- convert Fortran to C strings -- */
  namlen = ( nl < 128 ) ? nl : 127;
  strncpy(fnambuf, name, namlen);
  fnambuf[namlen] = '\0';
  fillen = ( fl < 1024 ) ? fl : 1023;
  strncpy(ffilbuf, file, fillen);
  ffilbuf[fillen] = '\0';

  /* -- get region identifier by address -- */
  if ( (rid = hash_get_addr((unsigned long)name)) == VT_NO_ID )
  {
    /* -- region entered the first time, register region -- */
#if (defined(VT_MT) || defined(VT_HYB))
    VTTHRD_LOCK_IDS();
    if ( (rid = hash_get_addr((unsigned long)name)) == VT_NO_ID )
      rid = register_region((unsigned long)name, fnambuf, ffilbuf, *lno);
    VTTHRD_UNLOCK_IDS();
#else /* VT_MT || VT_HYB */
    rid = register_region((unsigned long)name, fnambuf, ffilbuf, *lno);
#endif /* VT_MT || VT_HYB */
  }

  /* -- write enter record -- */
  vt_enter(VT_CURRENT_THREAD, &time, rid);

  VT_RESUME_MALLOC_TRACING(VT_CURRENT_THREAD);
} VT_GENERATE_F77_BINDINGS(vt_user_start__, VT_USER_START__,
                           VT_User_start___f,
                           (const char* name, const char* file, int* lno,
                            int nl, int fl),
                           (name, file, lno, nl, fl))


VT_DECLDEF(void VT_User_end___f(const char* name, int nl))
{
  uint64_t time;

  VT_SUSPEND_MALLOC_TRACING(VT_CURRENT_THREAD);

  /* -- write exit record -- */
  time = vt_pform_wtime();
  vt_exit(VT_CURRENT_THREAD, &time);

  VT_RESUME_MALLOC_TRACING(VT_CURRENT_THREAD);
} VT_GENERATE_F77_BINDINGS(vt_user_end__, VT_USER_END__,
                           VT_User_end___f,
                           (const char *name, int nl),
                           (name, nl))

VT_DECLDEF(void VT_User_def___f(const char* name, const char* file,
                                int* lno, unsigned int* rid, int nl, int fl))
{
  int namlen;
  int fillen;
  char fnambuf[128];
  char ffilbuf[1024];

  /* -- convert Fortran to C strings -- */
  namlen = ( nl < 128 ) ? nl : 127;
  strncpy(fnambuf, name, namlen);
  fnambuf[namlen] = '\0';
  fillen = ( fl < 1024 ) ? fl : 1023;
  strncpy(ffilbuf, file, fillen);
  ffilbuf[fillen] = '\0';

  /* -- get region identifier from C version */
  *rid = VT_User_def__(fnambuf, ffilbuf, *lno);
} VT_GENERATE_F77_BINDINGS(vt_user_def__, VT_USER_DEF__,
                           VT_User_def___f,
                           (const char* name, const char* file,
                            int* lno, unsigned int* rid, int nl, int fl),
                           (name, file, lno, rid, nl, fl))

VT_DECLDEF(void VT_User_start_id___f(unsigned int* rid))
{
  uint64_t time;

  VT_SUSPEND_MALLOC_TRACING(VT_CURRENT_THREAD);

  /* -- write enter record -- */
  time = vt_pform_wtime();
  vt_enter(VT_CURRENT_THREAD, &time, *rid);

  VT_RESUME_MALLOC_TRACING(VT_CURRENT_THREAD);
} VT_GENERATE_F77_BINDINGS(vt_user_start_id__, VT_USER_START_ID__,
                           VT_User_start_id___f,
                           (unsigned int* rid),
                           (rid))

VT_DECLDEF(void VT_User_end_id___f(unsigned int* rid))
{
  uint64_t time;

  (void)rid;

  VT_SUSPEND_MALLOC_TRACING(VT_CURRENT_THREAD);

  /* -- write exit record -- */
  time = vt_pform_wtime();
  vt_exit(VT_CURRENT_THREAD, &time);

  VT_RESUME_MALLOC_TRACING(VT_CURRENT_THREAD);
} VT_GENERATE_F77_BINDINGS(vt_user_end_id__, VT_USER_END_ID__,
                           VT_User_end_id___f,
                           (unsigned int* rid),
                           (rid))