Source

mpi3-fortran / ompi / contrib / vt / vt / vtlib / vt_comp_openuh.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
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
/**
 * 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 <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vt_comp.h"
#include "vt_defs.h"
#include "vt_error.h"
#include "vt_mallocwrap.h"
#include "vt_pform.h"
#include "vt_thrd.h"
#include "vt_trc.h"

/*
 * Macro for getting id of calling thread
 */

#define GET_THREAD_ID(tid) \
  VT_CHECK_THREAD;         \
  (tid) = VT_MY_THREAD

/*
 * Subtypes for loops.
 * A loop can be a 'DO-LOOP' in Fortran or 'while-do'/'do-while' in C/C++.
 */

typedef enum
{
  DO_LOOP_SUBTYPE  = 1,
  WHILE_DO_SUBTYPE = 2,
  DO_WHILE_SUBTYPE = 3
} LoopSubType;

/*
 * Data structures for context information of the API calls.
 */

struct profile_init_struct
{
  char*   output_filename;
  int32_t phase_num;
  int32_t unique_name;
};

struct profile_gen_struct
{
  int64_t pu_handle;
  char*   file_name;
  char*   pu_name;
  int32_t id;
  int32_t linenum;
  int32_t endline;
  int32_t subtype;
  int32_t taken;
  char*   callee;
  int32_t target;
  int32_t num_target;
  void*   called_fun_address;
  void*   data;
};

/*
 * Data structure for list of pointers to stored data.
 * e.g. assigned region identifiers
 */

struct data_list_struct
{
  void** ptr;
  struct data_list_struct* next;
};

/* flag: is initialization needed? */
static int openuh_init = 1;

/* list of pointers to stored data */
static struct data_list_struct* data_list = NULL;

/*
 * Registers a new region.
 */

static void register_region(uint32_t tid, struct profile_gen_struct* d,
                            uint8_t rtype)
{
  struct data_list_struct* data;
  char* rname = d->pu_name;
  uint32_t fid = VT_NO_ID;
  uint32_t begln = VT_NO_LNO;
  uint32_t endln = VT_NO_LNO;

  /* -- register file, if available -- */
  if ( d->file_name != NULL )
  {
    fid = vt_def_scl_file(tid, d->file_name);
    begln = d->linenum;
    endln = d->endline;
  }

  /* -- generate region name, if region is a loop -- */
  if ( rtype == VT_LOOP )
  {
    char stname[10];

    rname = (char*)malloc(1024 * sizeof(char));
    if ( rname == NULL ) vt_error();

    switch ( d->subtype )
    {
      case DO_LOOP_SUBTYPE:
        strncpy(stname, "do_loop", 7+1);
        break;
      case WHILE_DO_SUBTYPE:
        strncpy(stname, "while_do", 8+1);
        break;
      case DO_WHILE_SUBTYPE:
        strncpy(stname, "do_while", 8+1);
        break;
      default:
        vt_libassert(0);
        break;
    }

    /* -- add source location to name, if available -- */
    if ( d->file_name != NULL )
    {
      snprintf(rname, 1023, "%s @%s:%i-%i",
               stname, basename(d->file_name), begln, endln);
    }
    /* -- otherwise, add the internal compiler id to name -- */
    else
    {
      snprintf(rname, 1023, "%s%i", stname, d->id);
    }
  }

  /* -- register region -- */
  d->data = (uint32_t*)malloc(sizeof(uint32_t));
  if ( d->data == NULL ) vt_error();
  *((uint32_t*)(d->data)) =
    vt_def_region(tid, rname, fid, begln, endln, NULL, rtype);

  /* -- free generated region name -- */
  if ( rtype == VT_LOOP )
    free( rname );

  /* -- add pointer to stored data (region identifier) to list -- */
  data = (struct data_list_struct*)malloc(sizeof(struct data_list_struct));
  if ( data == NULL ) vt_error();

  data->ptr = &(d->data);
  if ( data_list == NULL )
  {
    data->next = NULL;
    data_list = data;
  }
  else
  {
    data->next = data_list->next;
    data_list->next = data;
  }
}

/*
 * Finalizes the performance library.
 */

VT_DECLDEF(void __profile_finish(void))
{
  struct data_list_struct* tmp;

  /* -- free all stored data -- */
  while( data_list )
  {
    tmp = data_list->next;

    free(*(data_list->ptr));  /* stored data (=profile_gen_struct.data) */
    *(data_list->ptr) = NULL;
    free(data_list);          /* list entry */

    data_list = tmp;
  }
}

/*
 * Initializes the performance library.
 * It should be invoked once in the entire application.
 */

VT_DECLDEF(void __profile_init(struct profile_init_struct* d))
{
  (void)d;

  if ( openuh_init )
  {
    openuh_init = 0;
    vt_open();
    vt_comp_finalize = &__profile_finish;
  }
}

/*
 * Instrumented at the beginning of each procedure, method, function. 
 */

VT_DECLDEF(void __profile_invoke(struct profile_gen_struct* d))
{
  uint32_t tid;
  uint64_t time;

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

  /* -- get calling thread id -- */
  GET_THREAD_ID(tid);

  VT_SUSPEND_MALLOC_TRACING(tid);

  time = vt_pform_wtime();

  /* -- get region identifier -- */
  if ( d->data == NULL )
  {
    /* -- region entered the first time, register region -- */
#if (defined(VT_MT) || defined(VT_HYB))
    VTTHRD_LOCK_IDS();
    if ( d->data == NULL )
      register_region(tid, d, VT_FUNCTION);
    VTTHRD_UNLOCK_IDS();
#else /* VT_MT || VT_HYB */
      register_region(tid, d, VT_FUNCTION);
#endif /* VT_MT || VT_HYB */
  }

  /* -- write enter record -- */
  vt_enter(tid, &time, *((uint32_t*)(d->data)));

  VT_RESUME_MALLOC_TRACING(tid);
}

/*
 * Instrumentation at the end of each procedure, method, function.
 * A procedure can have multiple exits.
 */

VT_DECLDEF(void __profile_invoke_exit(struct profile_gen_struct* d))
{
  uint32_t tid;
  uint64_t time;

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

  /* -- get calling thread id -- */
  GET_THREAD_ID(tid);

  VT_SUSPEND_MALLOC_TRACING(tid);

  time = vt_pform_wtime();

  /* -- write exit record -- */
  if ( d->data != NULL ) {
    vt_exit(tid, &time);
  }

  VT_RESUME_MALLOC_TRACING(tid);
}

/*
 * Instrumentation at the beginning of a branch.
 * A branch can be of a subBranchType.
 */

VT_DECLDEF(void __profile_branch(struct profile_gen_struct* d))
{
  (void)d;
}

/*
 * Instrumentation at the beginning of a loop.
 * A loop can be subLoopType.
 */

VT_DECLDEF(void __profile_loop(struct profile_gen_struct* d))
{
  uint32_t tid;
  uint64_t time;

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

  /* -- get calling thread id -- */
  GET_THREAD_ID(tid);

  VT_SUSPEND_MALLOC_TRACING(tid);

  time = vt_pform_wtime();

  /* -- get region identifier -- */
  if ( d->data == NULL )
  {
    /* -- loop entered the first time, register region -- */
#if (defined(VT_MT) || defined(VT_HYB))
    VTTHRD_LOCK_IDS();
    if ( d->data == NULL )
      register_region(tid, d, VT_LOOP);
    VTTHRD_UNLOCK_IDS();
#else /* VT_MT || VT_HYB */
    register_region(tid, d, VT_LOOP);
#endif /* VT_MT || VT_HYB */
  }

  /* -- write enter record -- */
  vt_enter(tid, &time, *((uint32_t*)(d->data)));

  VT_RESUME_MALLOC_TRACING(tid);
}

/*
 * Instrumentation inside the body of a loop.
 */

VT_DECLDEF(void __profile_loop_iter(struct profile_gen_struct* d))
{
  (void)d;
}

/*
 * Instrumentation at the end of each exit of the loop.
 * A loop can have several exits points, such stops or returns.
 */

VT_DECLDEF(void  __profile_loop_exit(struct profile_gen_struct* d))
{
  uint32_t tid;
  uint64_t time;

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

  /* -- get calling thread id -- */
  GET_THREAD_ID(tid);

  VT_SUSPEND_MALLOC_TRACING(tid);

  time = vt_pform_wtime();

  /* -- write exit record -- */
  if ( d->data != NULL ) {
    vt_exit(tid, &time);
  }

  VT_RESUME_MALLOC_TRACING(tid);
}

/*
 * Instrumentation before a callsite.
 */

VT_DECLDEF(void __profile_call_entry(struct profile_gen_struct* d))
{
  (void)d;
}

/*
 * Instrumentation at the end of a call site.
 */

VT_DECLDEF(void __profile_call_exit(struct profile_gen_struct* d))
{
  (void)d;
}

/*
 * Instrumentation before an indirect call, such as pointer call.
 */

VT_DECLDEF(void __profile_icall_entry(struct profile_gen_struct* d))
{
  (void)d;
}

/*
 * Instrumentation after an indirect call.
 */

VT_DECLDEF(void __profile_icall_exit(struct profile_gen_struct* d))
{
  (void)d;
}

/*
 * Instrumentation before a computed GOTO in Fortran.
 */

VT_DECLDEF(void __profile_compgoto(struct profile_gen_struct* d))
{
  (void)d;
}

/*
 * Instrumentation at the beginning of a switch statement.
 */

VT_DECLDEF(void __profile_switch(struct profile_gen_struct* d))
{
  (void)d;
}

/*
 * Instrumentation at the end of the switch statement.
 */

VT_DECLDEF(void __profile_switch_exit(struct profile_gen_struct* d))
{
  (void)d;
}

/*
 * Instrumentation for each expression in the conditional statement evaluation.
 */

VT_DECLDEF(void __profile_short_circuit(struct profile_gen_struct* d))
{
  (void)d;
}

/*
 * Instrumentation for each floating point multiplication, division, modulus.
 */

VT_DECLDEF(void __profile_value(struct profile_gen_struct* d))
{
  (void)d;
}