Source

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

Full commit
  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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/**
 * 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
 **/

#define _GNU_SOURCE

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#include "vt_comp.h"
#include "vt_defs.h"
#include "vt_env.h"
#include "vt_error.h"
#include "vt_inttypes.h"
#include "vt_iowrap.h"
#include "vt_memhook.h"
#include "vt_pform.h"
#include "vt_trc.h"
#include "vt_thrd.h"

#if (defined(HAVE_DL) && HAVE_DL) && (defined(HAVE_DECL_RTLD_DEFAULT) && HAVE_DECL_RTLD_DEFAULT)
# include <dlfcn.h>
# define GET_SO_FUNC_ADDR(func) \
  GET_IA64_FUNC_ADDR(dlsym(RTLD_DEFAULT, (func)))
#else /* HAVE_DL && HAVE_DECL_RTLD_DEFAULT */
# define GET_SO_FUNC_ADDR(func) 0
#endif /* HAVE_DL && HAVE_DECL_RTLD_DEFAULT */

#ifdef __ia64__
# define GET_IA64_FUNC_ADDR(addr) (long)((addr) ? *(void**)(addr) : (addr))
#else /* __ia64__ */
# define GET_IA64_FUNC_ADDR(addr) (long)(addr)
#endif /* __ia64__ */

#ifdef VT_COMPINST_CRAYCCE
# define __cyg_profile_func_enter __pat_tp_func_entry
# define __cyg_profile_func_exit  __pat_tp_func_return
#endif /* VT_COMPINST_CRAYCCE */

#define NM_LINE_BLK_LEN 1024
#define NM_LINE_MAX_LEN 16384

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

/*
 *-----------------------------------------------------------------------------
 * Simple hash table to map function addresses to region names/identifier
 *-----------------------------------------------------------------------------
 */

typedef struct HN {
  long id;            /* hash code (address of function */
  char* name;         /* associated function name       */
  char* fname;        /*            file name           */
  int lno;            /*            line number         */
  uint32_t vtid;      /* associated region identifier   */
  struct HN* next;
} HashNode;

#define HASH_MAX 1021

static HashNode* htab[HASH_MAX];
static uint32_t n_htab_entries = 0;

/*
 * Stores function name `n' under hash code `h'
 */

static void hash_put(long h, const char* n, const char* fn, int lno) {
  long id = h % HASH_MAX;
  HashNode* add = (HashNode*)malloc(sizeof(HashNode));
  add->id = h;
  add->name  = (char*)n;
  add->fname = fn ? strdup(fn) : (char*)fn;
  add->lno   = lno;
  add->vtid = VT_NO_ID;
  add->next = htab[id];
  htab[id] = add;
  n_htab_entries++;
}

/*
 * Lookup hash code `h'
 * Returns hash table entry if already stored, otherwise NULL
 */

static HashNode* hash_get(long h) {
  long id = h % HASH_MAX;
  HashNode* curr = htab[id];
  while ( curr ) {
    if ( curr->id == h ) {
      return curr;
    }
    curr = curr->next;
  }
  return NULL;
}

/*
 * Get symbol table by 'nm'
 */

static void get_symtab(void)
{
  char* nm_cmd = NULL;
  char* nm_filename;
  FILE* nm_stream;

  char* line;
  size_t line_size;
  uint32_t lineno = 0;

  uint8_t parse_error = 0;

  VT_SUSPEND_IO_TRACING(VT_CURRENT_THREAD);

  /* open nm-file, if given */
  nm_filename = vt_env_gnu_nmfile();
  if ( nm_filename != NULL && strlen(nm_filename) > 0 )
  {
    vt_cntl_msg(2, "Collecting symbols from file %s", nm_filename);

    /* open nm-file */
    if ( (nm_stream = fopen(nm_filename, "r")) == NULL )
      vt_error_msg("Could not open symbol list file %s", nm_filename);
  }
  /* otherwise, try to get symbol table automatically */
  else
  {
    char* apppath;
    char* nm;
    size_t nm_cmd_len;

    vt_cntl_msg(2, "Collecting symbols by 'nm'");

    /* get executable path specified by VT_APPPATH */
    apppath = vt_env_apppath();
    if ( apppath == NULL || strlen(apppath) == 0 )
    {
      vt_error_msg("Could not determine path of executable.\n"
                   "Please set the environment variable VT_APPPATH to the path "
                   "of your executable or set VT_GNU_NMFILE to a symbol list "
                   "file created with 'nm'.");
    }

    /* get nm command specified by VT_GNU_NM */
    nm = vt_env_gnu_nm();
    if ( nm == NULL )
    {
      vt_error_msg("VampirTrace was configured without an 'nm' command.\n"
                   "Please set the environment variable VT_GNU_NM to the 'nm' "
                   "command including command line switches which lists "
                   "symbol/addresses of an object file in BSD-style or set "
                   "VT_GNU_NMFILE to a pre-created symbol list file." );
    }

    /* allocate memory for nm command */
    nm_cmd_len = strlen(nm) + 1 + strlen(apppath) + 1;
    nm_cmd = (char*)malloc(nm_cmd_len * sizeof(char));
    if ( nm_cmd == NULL )
      vt_error();

    /* compose nm command */
    snprintf(nm_cmd, nm_cmd_len, "%s %s", nm, apppath);

    /* execute nm command */
    vt_cntl_msg(2, "Executing %s", nm_cmd);
    nm_stream = popen(nm_cmd, "r");
    /* error handling after pclose below */

    nm_filename = NULL;
  }

  /* allocate memory for lines */
  line = (char*)malloc(NM_LINE_BLK_LEN * sizeof(char));
  if ( line == NULL )
    vt_error();
  line_size = NM_LINE_BLK_LEN;

  /* read lines */

  while( nm_stream != NULL && fgets(line, line_size, nm_stream) )
  {
    char* col;
    char  delim[2] = " ";
    int   nc = 1;

    long  addr = 0;
    char* filename = NULL;
    char* funcname = NULL;
    unsigned int lno = VT_NO_LNO;

    lineno++;

    /* trigger a parse error, if line is empty */
    if ( strlen(line) == 0 )
    {
      parse_error = 1;
      break;
    }

    /* if line seems to be incomplete, enlarge line buffer and read the
       remaining line */
    while( !parse_error && line[strlen(line)-1] != '\n' )
    {
      char tmp[NM_LINE_BLK_LEN];

      /* read the remaining line; if it fails (EOF) the line seems to
         be complete after all */
      if ( !fgets(tmp, sizeof(tmp), nm_stream) )
        break;

      /* trigger a parse error, if line is to long (>NM_LINE_MAX_LEN) */
      if ( line_size + NM_LINE_BLK_LEN > NM_LINE_MAX_LEN )
      {
        parse_error = 1;
        break;
      }

      /* enlarge line buffer */
      line = (char*)realloc(line, (line_size + NM_LINE_BLK_LEN) * sizeof(char));
      if ( line == NULL )
        vt_error();
      line_size += NM_LINE_BLK_LEN;

      /* complete line */
      strcat(line, tmp);
    }
    if ( parse_error )
      break;

    /* chop new-line character from line */
    if ( line[strlen(line)-1] == '\n' )
      line[strlen(line)-1] = '\0';

    /* ignore line if it is empty */
    if ( *line == '\0' )
      continue;

    /* ignore nm input file name */
    if ( line[strlen(line)-1] == ':' )
      continue;

    /* split line to columns */
    col = strtok(line, delim);
    do
    {
      if ( nc == 1 ) /* column 1 (address) */
      {
        /* if there is no address in the first column the symbol could be
           defined within a shared object; try get its address later (nc==3) */
        if ( strlen(col) == 1 )
        {
          nc++; /* <- will be 3 in the next round */
          *delim = '\t';
        }
        /* otherwise, convert address string */
        else
        {
          addr = strtol(col, NULL, 16);
        }
      }
      else if ( nc == 2 ) /* column 2 (type) */
      {
        /* type must have a length of 1 */
        if ( strlen(col) != 1 )
        {
          parse_error = 1;
          break;
        }

        *delim = '\t';
      }
      else if ( nc == 3 ) /* column 3 (symbol) */
      {
        long soaddr;

        funcname = col;

        /* the symbol might be defined within a shared object; try to get
           its real address */
        if ( ( soaddr = GET_SO_FUNC_ADDR(funcname) ) != 0 )
          addr = soaddr;

        /* ignore function, if its address could not be determined */
        if ( addr == 0 )
          break;

        *delim = ':';
      }
      else if ( nc == 4 ) /* column 4 (filename) */
      {
        filename = col;
      }
      else /* column 5 (line) */
      {
        lno = atoi(col);
        if( lno == 0 ) lno = VT_NO_LNO;
        break;
      }

      nc++;
      col = strtok(0, delim);
    } while( col );

    /* stop reading file, if an parse error occurred */
    if ( parse_error )
    {
      break;
    }
    /* at least two columns must be read */
    else if ( nc < 3 )
    {
      parse_error = 1;
      break;
    }
    /* add symbol to hash table, if we have its address and name */
    else if ( addr > 0 && funcname )
    {
      char* n = strdup(funcname);
      char* p;

      if ( n == NULL )
        vt_error();

      /* chop function name at '??', if necessary */
      p = strstr(n, "??");
      if ( p != NULL && p != n )
        *p = '\0';

      hash_put(addr, n, filename, lno);
    }
  }

  /* close file/pipe stream */

  if ( nm_filename != NULL )
  {
    fclose(nm_stream);

    if ( parse_error )
    {
      vt_error_msg("%s:%u: could not be parsed.\n"
                   "Please check the content of %s for BSD-style.",
                   nm_filename, lineno, nm_filename);
    }
  }
  else
  {
    uint8_t nmcmd_error = (nm_stream == NULL || pclose(nm_stream) != 0);

    if ( parse_error )
    {
      vt_error_msg("Could not parse 'nm' output created with %s.\n"
                   "Please set the environment variable VT_GNU_NM to the 'nm' "
                   "command including command line switches which lists "
                   "symbol/addresses of an object file in BSD-style or set "
                   "VT_GNU_NMFILE to a pre-created symbol list file.",
                   nm_cmd);
    }
    else if ( nmcmd_error )
    {
      vt_error_msg("Failed to execute %s\n"
                   "Please set the environment variable VT_GNU_NM to the 'nm' "
                   "command including command line switches which lists "
                   "symbol/addresses of an object file in BSD-style or set "
                   "VT_GNU_NMFILE to a pre-created symbol list file.",
                   nm_cmd);
    }

    free(nm_cmd);
  }

  free(line);

  VT_RESUME_IO_TRACING(VT_CURRENT_THREAD);
}

/*
 * Register new region
 */

static void register_region(HashNode* hn) {
  uint32_t fid = VT_NO_ID;
  uint32_t lno = VT_NO_LNO;

  /* -- register file if available -- */
  if (hn->fname != NULL)
  {
    fid = vt_def_scl_file(VT_CURRENT_THREAD, hn->fname);
    lno = hn->lno;
  }

  /* -- register region and store region identifier -- */
  hn->vtid = vt_def_region(VT_CURRENT_THREAD, hn->name, fid, lno, VT_NO_LNO,
                           NULL, VT_FUNCTION);
}

void gnu_finalize(void);
void __cyg_profile_func_enter(void* func, void* callsite);
void __cyg_profile_func_exit(void* func, void* callsite);

/*
 * Finalize instrumentation interface
 */

void gnu_finalize()
{
  int i, idx_min, idx_max;
  uint32_t min, max;
  double avg;
  min = 0xffffffff;
  max = 0;
  idx_min = idx_max = 0;
  avg = 0.0;

  for( i = 0; i < HASH_MAX; i++ )
  {
    uint32_t n_bucket_entries = 0;

    struct HN* p = htab[i];
    while( p )
    {
      n_bucket_entries++;

      /* Set assigned region id back to VT_NO_ID instead of freeing the
         hash-node, because after a fork the hash-node will be re-used for the
         child process. This implies a small/non-increasing memory leak. */
      p->vtid = VT_NO_ID;
      p = p->next;
    }
    if( n_bucket_entries < min ) {
      min = n_bucket_entries;
      idx_min = i;
    }
    if( n_bucket_entries > max ) {
      max = n_bucket_entries;
      idx_max = i;
    }
    vt_cntl_msg(3, "Hash bucket %i had %u entries (%.1f/1000)",
                i, n_bucket_entries, ((double)n_bucket_entries*1000)/n_htab_entries);
  }
  avg = (double)n_htab_entries / HASH_MAX;
  vt_cntl_msg( 3, "Hash statistics:\n"
                  "\tNumber of entries: %u\n"
                  "\tMin bucket size:   %u (%.1f/1000) at index %i\n"
                  "\tMax bucket size:   %u (%.1f/1000) at index %i\n"
                  "\tAvg bucket size:   %.1f",
                  n_htab_entries,
                  min, ((double)min*1000)/n_htab_entries, idx_min,
                  max, ((double)max*1000)/n_htab_entries, idx_max,
                  avg );
}

/*
 * This function is called at the entry of each function
 * The call is generated by the GNU/Intel (>=v10) compilers
 */

void __cyg_profile_func_enter(void* func, void* callsite) {
  long addr;
  uint64_t time;
  HashNode* hn;

  addr = GET_IA64_FUNC_ADDR(func);

  /* -- if not yet initialized, initialize VampirTrace -- */
  if ( gnu_init ) {
    VT_MEMHOOKS_OFF();
    gnu_init = 0;
    vt_open();
    vt_comp_finalize = gnu_finalize;
    get_symtab();
    VT_MEMHOOKS_ON();
  }

  /* -- if VampirTrace already finalized, return -- */
  if ( !vt_is_alive ) return;

  VT_MEMHOOKS_OFF();

  time = vt_pform_wtime();

  /* -- get region identifier -- */
  if ( (hn = hash_get(addr)) ) {
    if ( hn->vtid == VT_NO_ID ) {
      /* -- region entered the first time, register region -- */
#if (defined(VT_MT) || defined(VT_HYB))
      VTTHRD_LOCK_IDS();
      if( hn->vtid == VT_NO_ID )
        register_region(hn);
      VTTHRD_UNLOCK_IDS();
#else /* VT_MT || VT_HYB */
      register_region(hn);
#endif /* VT_MT || VT_HYB */
    }

    /* -- write enter record -- */
    vt_enter(VT_CURRENT_THREAD, &time, hn->vtid);
  }

  VT_MEMHOOKS_ON();
}

/*
 * This function is called at the exit of each function
 * The call is generated by the GNU/Intel (>=v10) compilers
 */

void __cyg_profile_func_exit(void* func, void* callsite) {
  long addr;
  uint64_t time;

  addr = GET_IA64_FUNC_ADDR(func);

  /* -- if VampirTrace already finalized, return -- */
  if ( !vt_is_alive ) return;

  VT_MEMHOOKS_OFF();

  time = vt_pform_wtime();

  /* -- write exit record -- */
  if ( hash_get(addr) ) {
    vt_exit(VT_CURRENT_THREAD, &time);
  }

  VT_MEMHOOKS_ON();
}