tracer_opencl_helpers.include.c 41 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
void CL_CALLBACK  event_notify (cl_event event, cl_int event_command_exec_status, void *user_data) {
  (void)user_data;
  if (tracepoint_enabled(lttng_ust_opencl_profiling, event_profiling_results)) {
    cl_ulong queued;
    cl_ulong submit;
    cl_ulong start;
    cl_ulong end;
    cl_int queued_status = CL_GET_EVENT_PROFILING_INFO_PTR(event, CL_PROFILING_COMMAND_QUEUED, sizeof(cl_ulong), &queued, NULL);
    cl_int submit_status = CL_GET_EVENT_PROFILING_INFO_PTR(event, CL_PROFILING_COMMAND_SUBMIT, sizeof(cl_ulong), &submit, NULL);
    cl_int start_status = CL_GET_EVENT_PROFILING_INFO_PTR(event, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &start, NULL);
    cl_int end_status = CL_GET_EVENT_PROFILING_INFO_PTR(event, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &end, NULL);
    do_tracepoint(lttng_ust_opencl_profiling, event_profiling_results, event, event_command_exec_status,
13
                  queued_status, queued, submit_status, submit, start_status, start, end_status, end);
14
15
16
  }
}

17
18
19
20
21
struct opencl_version {
  cl_uint minor;
  cl_uint major;
};

22
23
//static const struct opencl_version opencl_version_1_0 = {1, 0};
//static const struct opencl_version opencl_version_1_1 = {1, 1};
24
static const struct opencl_version opencl_version_1_2 = {1, 2};
25
26
27
//static const struct opencl_version opencl_version_2_0 = {2, 0};
//static const struct opencl_version opencl_version_2_1 = {2, 1};
//static const struct opencl_version opencl_version_2_2 = {2, 2};
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

static inline int compare_opencl_version(const struct opencl_version *v1, const struct opencl_version *v2) {
  if (v1->major > v2->major)
    return 1;
  if (v1->major > v2->major)
    return -1;
  if (v1->minor > v2->minor)
    return 1;
  if (v1->minor > v2->minor)
    return -1;
  return 0;
}

static void get_platform_version(cl_platform_id platform, struct opencl_version *v) {
  if (!v)
    return;
  cl_int err;
  char version[128];
  char major[2], minor[2];
  size_t version_sz;

  v->major = 1;
  v->minor = 0;
51
  err = CL_GET_PLATFORM_INFO_PTR(platform, CL_PLATFORM_VERSION, 128, version, &version_sz);
52
53
54
55
56
57
58
59
60
61
62
  if (err != CL_SUCCESS || version_sz < 10)
    return;
  major[0] = version[7];
  major[1] = 0;
  minor[0] = version[9];
  minor[1] = 0;
  v->major = atoi(major);
  v->minor = atoi(minor);
}

static void get_device_platform_version(cl_device_id device, struct opencl_version *v) {
63
  if (!v)
64
65
66
    return;
  cl_platform_id platform;

67
  if (CL_GET_DEVICE_INFO_PTR(device, CL_DEVICE_PLATFORM, sizeof(platform), &platform, NULL) == CL_SUCCESS)
68
69
70
71
    get_platform_version(platform, v);
  else {
    v->major = 1;
    v->minor = 0;
72
  }
73
74
}

75
76
77
78
static inline cl_device_id* get_program_devices(cl_program program, cl_uint *num_devices_ret) {
  cl_device_id *devices = NULL;
  cl_uint num_devices = 0;

79
  if (CL_GET_PROGRAM_INFO_PTR(program, CL_PROGRAM_NUM_DEVICES, sizeof(num_devices), &num_devices, NULL) != CL_SUCCESS)
80
81
82
83
84
85
    num_devices = 0;
  else if (num_devices != 0) {
    devices = (cl_device_id *)malloc(num_devices * sizeof(cl_device_id));
    if (!devices)
      num_devices = 0;
    else
86
      if (CL_GET_PROGRAM_INFO_PTR(program, CL_PROGRAM_DEVICES, num_devices * sizeof(cl_device_id), devices, NULL) != CL_SUCCESS) {
87
88
89
90
91
92
93
94
95
96
        free(devices);
        devices = NULL;
        num_devices = 0;
      }
  }
  if (num_devices_ret)
    *num_devices_ret = num_devices;
  return devices;
}

97
static void get_program_platform_version(cl_program program, struct opencl_version *v) {
98
  if (!v)
99
100
101
102
103
    return;
  cl_device_id *devices;

  v->major = 1;
  v->minor = 0;
104
105
  devices = get_program_devices(program, NULL);
  if (devices) {
106
107
108
109
110
    get_device_platform_version(*devices, v);
    free(devices);
  }
}

111
static int     do_host_profile = 0;
112
113
114
115
116
117
118
119
120
121
static int     do_dump = 0;
pthread_mutex_t enqueue_counter_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t opencl_obj_mutex = PTHREAD_MUTEX_INITIALIZER;
static int64_t enqueue_counter = 0;
static int64_t dump_start = 0;
static int64_t dump_end = INT64_MAX;

#define BIN_TEMPLATE "/tmp/binXXXXXX"
#define SOURCE_TEMPLATE "/tmp/sourceXXXXXX"
#define BIN_SOURCE_TEMPLATE "/tmp/binsourceXXXXXX"
Brice Videau's avatar
Brice Videau committed
122
#define BIN_BUILD_TEMPLATE "/tmp/binbuildXXXXXX"
123
#define OBJ_BUILD_TEMPLATE "/tmp/objbuildXXXXXX"
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
#define IL_SOURCE_TEMPLATE "/tmp/ilsourceXXXXXX"

pthread_mutex_t opencl_closures_mutex = PTHREAD_MUTEX_INITIALIZER;

struct opencl_closure {
  void *ptr;
  void *c_ptr;
  UT_hash_handle hh;
  ffi_cif cif;
  ffi_closure *closure;
  ffi_type **types;
};

struct opencl_closure * opencl_closures = NULL;

struct buffer_obj_data {
  size_t size;
};

struct svmptr_obj_data;

struct svmptr_obj_data {
  struct svmptr_obj_data *prev;
  struct svmptr_obj_data *next;
  void* ptr;
  size_t size;
};

struct svmptr_obj_data *svmptr_objs = NULL;

struct kernel_arg {
  // 0 regular, 1 SVM
  int type;
  size_t arg_size;
  void *arg_value;
};

struct kernel_obj_data {
  cl_uint num_args;
  struct kernel_arg *args;
};

void kernel_obj_data_free(void *obj_data) {
  struct kernel_obj_data *data = (struct kernel_obj_data *)obj_data;
  for (cl_uint i = 0; i < data->num_args; i++) {
    if (data->args[i].arg_value) {
      free(data->args[i].arg_value);
    }
  }
  free(obj_data);
}

enum cl_obj_type {
  UNKNOWN = 0,
  PLATFORM,
  DEVICE,
  CONTEXT,
  COMMAND_QUEUE,
  BUFFER,
  PIPE,
  IMAGE,
  KERNEL,
  EVENT,
  SAMPLER,
  SVMMEM
};

struct opencl_obj_h {
  void *ptr;
  UT_hash_handle hh;
  enum cl_obj_type type;
  void *obj_data;
  void (*obj_data_free)(void *obj_data);
};

struct opencl_obj_h *opencl_objs = NULL;

static inline void delete_opencl_obj(struct opencl_obj_h *o_h) {
  HASH_DEL(opencl_objs, o_h);
  if (o_h->obj_data != NULL) {
    if (o_h->obj_data_free != NULL)
      o_h->obj_data_free(o_h->obj_data);
    else
      free(o_h->obj_data);
  }
  free(o_h);
}

int cmp_svm_ptr(struct svmptr_obj_data *svm_a, struct svmptr_obj_data *svm_b) {
  intptr_t a = (intptr_t)(svm_a->ptr);
  intptr_t b = (intptr_t)(svm_b->ptr);
  return (a < b ? -1 : (a == b ? 0 : 1));
}

static inline void add_svmptr(void *ptr, size_t size) {
  struct opencl_obj_h *o_h = NULL;
  struct svmptr_obj_data *svm_data = NULL;

  pthread_mutex_lock(&opencl_obj_mutex);
  HASH_FIND_PTR(opencl_objs, &ptr, o_h);
  if (o_h != NULL) {
    delete_opencl_obj(o_h);
  }
  pthread_mutex_unlock(&opencl_obj_mutex);

  o_h = (struct opencl_obj_h *)calloc(1, sizeof(struct opencl_obj_h));
  if (o_h == NULL)
    return;
  svm_data = (struct svmptr_obj_data *)calloc(1, sizeof(struct svmptr_obj_data));
  if (svm_data == NULL) {
    free(o_h);
    return;
  }
  o_h->ptr = ptr;
  o_h->type = SVMMEM;
  svm_data->ptr = ptr;
  svm_data->size = size;
  o_h->obj_data = (void *)svm_data;

  pthread_mutex_lock(&opencl_obj_mutex);
  HASH_ADD_PTR(opencl_objs, ptr, o_h);
  DL_INSERT_INORDER(svmptr_objs, svm_data, cmp_svm_ptr);
  pthread_mutex_unlock(&opencl_obj_mutex);
}

static inline void remove_svmptr(void *ptr) {
  struct opencl_obj_h *o_h = NULL;
  struct svmptr_obj_data *svm_data = NULL;

  pthread_mutex_lock(&opencl_obj_mutex);
  HASH_FIND_PTR(opencl_objs, &ptr, o_h);
  if (o_h != NULL) {
    svm_data = (struct svmptr_obj_data *)o_h->obj_data;
    DL_DELETE(svmptr_objs, svm_data);
    delete_opencl_obj(o_h);
  }
  pthread_mutex_unlock(&opencl_obj_mutex);
}

static inline void add_buffer(cl_mem b, size_t size) {
  struct opencl_obj_h *o_h = NULL;
  struct buffer_obj_data *b_data = NULL;

  pthread_mutex_lock(&opencl_obj_mutex);
  HASH_FIND_PTR(opencl_objs, &b, o_h);
  if (o_h != NULL) {
      delete_opencl_obj(o_h);
  }
  pthread_mutex_unlock(&opencl_obj_mutex);

  o_h = (struct opencl_obj_h *)calloc(1, sizeof(struct opencl_obj_h));
  if (o_h == NULL)
    return;
  b_data = (struct buffer_obj_data *)calloc(1, sizeof(struct buffer_obj_data));
  if (b_data == NULL) {
    free(o_h);
    return;
  }
  o_h->ptr = (void *)b;
  o_h->type = BUFFER;
  b_data->size = size;
  o_h->obj_data = (void *)b_data;

  pthread_mutex_lock(&opencl_obj_mutex);
  HASH_ADD_PTR(opencl_objs, ptr, o_h);
  pthread_mutex_unlock(&opencl_obj_mutex);
}

static inline void add_kernel(cl_kernel k) {
  cl_uint num_args = 0;
  cl_int err = CL_GET_KERNEL_INFO_PTR(k, CL_KERNEL_NUM_ARGS, sizeof(cl_uint), &num_args, NULL);
  if (err == CL_SUCCESS) {
    struct opencl_obj_h *o_h = NULL;
    struct kernel_obj_data *k_data = NULL;

    pthread_mutex_lock(&opencl_obj_mutex);
    HASH_FIND_PTR(opencl_objs, &k, o_h);
    if (o_h != NULL) {
      delete_opencl_obj(o_h);
    }
    pthread_mutex_unlock(&opencl_obj_mutex);

    o_h = (struct opencl_obj_h *)calloc(1, sizeof(struct opencl_obj_h));
    if (o_h == NULL)
      return;
    k_data = (struct kernel_obj_data *)calloc(1, sizeof(struct kernel_obj_data)+num_args*sizeof(struct kernel_arg));
    if (k_data == NULL) {
      free(o_h);
      return;
    }
    o_h->ptr = (void *)k;
    o_h->type = KERNEL;
    k_data->num_args = num_args;
    k_data->args = (struct kernel_arg *)((intptr_t)k_data + sizeof(struct kernel_obj_data));
    o_h->obj_data = (void *)k_data;
    o_h->obj_data_free = kernel_obj_data_free;

    pthread_mutex_lock(&opencl_obj_mutex);
    HASH_ADD_PTR(opencl_objs, ptr, o_h);
    pthread_mutex_unlock(&opencl_obj_mutex);
  }
}

static inline void add_kernel_arg(cl_kernel kernel, cl_uint arg_index, size_t arg_size, const void *arg_value, int type) {
  struct opencl_obj_h *o_h = NULL;
  pthread_mutex_lock(&opencl_obj_mutex);
  HASH_FIND_PTR(opencl_objs, &kernel, o_h);
  if (o_h != NULL && o_h->type == KERNEL) {
    struct kernel_obj_data *k_data = (struct kernel_obj_data *)o_h->obj_data;
    if (k_data !=NULL && arg_index < k_data->num_args) {
      struct kernel_arg *arg = k_data->args + arg_index;
      if (arg_value != NULL) {
        if (arg->arg_value == NULL) {
          arg->arg_value = malloc(arg_size);
        } else if (arg->arg_size < arg_size) {
          arg->arg_value = realloc(arg->arg_value, arg_size);
        }
        memcpy(arg->arg_value, arg_value, arg_size);
      }
      arg->arg_size = arg_size;
      arg->type = type;
    }
  }
  pthread_mutex_unlock(&opencl_obj_mutex);
}

struct buffer_dump_notify_data {
  uint64_t enqueue_counter;
  cl_uint arg_index;
  int direction;
  size_t size;
  int fd;
  char path[sizeof(BIN_TEMPLATE)];
};

void CL_CALLBACK  buffer_dump_notify (cl_event event, cl_int event_command_exec_status, void *user_data) {
  struct buffer_dump_notify_data * data = (struct buffer_dump_notify_data *)user_data;
  tracepoint(lttng_ust_opencl_dump, buffer_dump_result, data->enqueue_counter, data->arg_index, data->direction, event, event_command_exec_status, data->size, data->path);
  if (event_command_exec_status == CL_COMPLETE) {
    int err = ftruncate(data->fd, data->size);
    if(err)
      unlink(data->path);
  } else {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
    ftruncate(data->fd, 0);
#pragma GCC diagnostic pop
    unlink(data->path);
  }
  close(data->fd);
  free(user_data);
}

struct svmptr_dump_notify_data {
  uint64_t enqueue_counter;
  cl_uint arg_index;
  int direction;
  size_t size;
  int fd;
  char path[sizeof(BIN_TEMPLATE)];
};

void CL_CALLBACK  svmptr_dump_notify (cl_event event, cl_int event_command_exec_status, void *user_data) {
  struct svmptr_dump_notify_data * data = (struct svmptr_dump_notify_data *)user_data;
  tracepoint(lttng_ust_opencl_dump, buffer_dump_result, data->enqueue_counter, data->arg_index, data->direction, event, event_command_exec_status, data->size, data->path);
  if (event_command_exec_status == CL_COMPLETE) {
    int err = ftruncate(data->fd, data->size);
    if(err)
      unlink(data->path);
  } else {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
    ftruncate(data->fd, 0);
#pragma GCC diagnostic pop
    unlink(data->path);
  }
  close(data->fd);
  free(user_data);
}

static inline size_t align(size_t size, unsigned int bit) {
  size_t alignment_mask = (1 << bit) - 1;
  return (size + alignment_mask) & ~(alignment_mask);
}

static inline int create_file_and_map(char template[], size_t size, void **ptr) {
  int fd = mkstemp(template);
  size_t map_size = align(size, 12);
  int err = ftruncate(fd, map_size); //page size
  if (err == 0) {
    *ptr = mmap(NULL, map_size, PROT_WRITE, MAP_SHARED, fd, 0);
    if (*ptr == MAP_FAILED) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
      ftruncate(fd, 0);
#pragma GCC diagnostic pop
      close(fd);
      unlink(template);
      return -1;
    }
    return fd;
  } else {
    return -1;
  }
}

static inline void create_file_and_write(char template[], size_t size, const void * ptr) {
  int fd = mkstemp(template);
  if (fd != -1) {
    if (ptr != NULL) {
      ssize_t ret;
      size_t written = 0;
      while (written < size) {
        ret = write(fd, ptr, size);
        if (ret > 0) {
          written += ret;
        } else //if (errno != EINTR) avoid errno...
          break;
      }
    }
    close(fd);
  }
}

static void dump_buffer(cl_command_queue command_queue, struct opencl_obj_h *o_h, uint64_t enqueue_counter, cl_uint arg_index, int direction, cl_uint num_events_in_wait_list, cl_event *event_wait_list, cl_uint *new_num_events_in_wait_list, cl_event **new_event_wait_list) {
  cl_event event;
  void *ptr = NULL;
  struct buffer_dump_notify_data *data = NULL;
  struct buffer_obj_data *obj_data = (struct buffer_obj_data *)(o_h->obj_data);

  data = (struct buffer_dump_notify_data *)malloc(sizeof(struct buffer_dump_notify_data));
  if (data == NULL)
    return;
  data->enqueue_counter = enqueue_counter;
  data->arg_index = arg_index;
  data->direction = direction;
  data->size = obj_data->size;
  strncpy(data->path, BIN_TEMPLATE, sizeof(data->path));
  data->fd = create_file_and_map(data->path, data->size, &ptr);
  if (data->fd == -1) {
    free(data);
    return;
  }

  cl_int err = CL_ENQUEUE_READ_BUFFER_PTR(command_queue, (cl_mem)(o_h->ptr), CL_FALSE, 0, obj_data->size, ptr, num_events_in_wait_list, event_wait_list, &event);
  if (err == CL_SUCCESS) {
    int _set_retval = CL_SET_EVENT_CALLBACK_PTR(event, CL_COMPLETE, buffer_dump_notify, data);
    tracepoint(lttng_ust_opencl_dump, buffer_dump_event, enqueue_counter, arg_index, direction, (cl_mem)(o_h->ptr), _set_retval, event);
    if (new_event_wait_list != NULL) {
      *new_num_events_in_wait_list += 1;
      *new_event_wait_list = (cl_event *)realloc(*new_event_wait_list, *new_num_events_in_wait_list * sizeof(cl_event));
      if (*new_event_wait_list != NULL) {
        (*new_event_wait_list)[*new_num_events_in_wait_list -1] = event;
      } else {
        CL_RELEASE_EVENT_PTR(event);
      }
    } else {
      CL_RELEASE_EVENT_PTR(event);
    }
    if (_set_retval != CL_SUCCESS) {
      free(data);
    }
  } else {
    free(data);
  }
}

static void dump_svmptr(cl_command_queue command_queue, struct opencl_obj_h *o_h, uint64_t enqueue_counter, cl_uint arg_index, int direction, cl_uint num_events_in_wait_list, cl_event *event_wait_list, cl_uint *new_num_events_in_wait_list, cl_event **new_event_wait_list) {
  cl_event event;
  void *ptr = NULL;
  struct svmptr_dump_notify_data *data = NULL;
  struct svmptr_obj_data *obj_data = (struct svmptr_obj_data *)(o_h->obj_data);

  data = (struct svmptr_dump_notify_data *)malloc(sizeof(struct svmptr_dump_notify_data));
  if (data == NULL)
    return;
  data->enqueue_counter = enqueue_counter;
  data->arg_index = arg_index;
  data->direction = direction;
  data->size = obj_data->size;
  strncpy(data->path, BIN_TEMPLATE, sizeof(data->path));
  data->fd = create_file_and_map(data->path, data->size, &ptr);
  if (data->fd == -1) {
    free(data);
    return;
  }

511
  cl_int err = CL_ENQUEUE_SVMMEMCPY_PTR(command_queue, CL_FALSE, ptr, obj_data->ptr, obj_data->size, num_events_in_wait_list, event_wait_list, &event);
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
  if (err == CL_SUCCESS) {
    int _set_retval = CL_SET_EVENT_CALLBACK_PTR(event, CL_COMPLETE, svmptr_dump_notify, data);
    tracepoint(lttng_ust_opencl_dump, svmptr_dump_event, enqueue_counter, arg_index, direction, obj_data->ptr, _set_retval, event);
    if (new_event_wait_list != NULL) {
      *new_num_events_in_wait_list += 1;
      *new_event_wait_list = (cl_event *)realloc(*new_event_wait_list, *new_num_events_in_wait_list * sizeof(cl_event));
      if (*new_event_wait_list != NULL) {
        (*new_event_wait_list)[*new_num_events_in_wait_list -1] = event;
      } else {
        CL_RELEASE_EVENT_PTR(event);
      }
    } else {
      CL_RELEASE_EVENT_PTR(event);
    }
    if (_set_retval != CL_SUCCESS) {
      free(data);
    }
  } else {
    free(data);
  }
}

static void dump_opencl_object(cl_command_queue command_queue, uint64_t enqueue_counter, struct kernel_arg *arg, int do_event, cl_uint arg_index, int direction, cl_uint num_events_in_wait_list, cl_event *event_wait_list, cl_uint *new_num_events_in_wait_list, cl_event **new_event_wait_list) {
  struct opencl_obj_h *oo_h = NULL;
  struct svmptr_obj_data *svm_data = NULL;
  pthread_mutex_lock(&opencl_obj_mutex);
  HASH_FIND_PTR(opencl_objs, (void **)(arg->arg_value), oo_h);
  pthread_mutex_unlock(&opencl_obj_mutex);
  if (oo_h != NULL) {
    switch (oo_h->type) {
    case BUFFER:
      if (do_event)
        dump_buffer(command_queue, oo_h, enqueue_counter, arg_index, direction, num_events_in_wait_list, event_wait_list, new_num_events_in_wait_list, new_event_wait_list);
      else
        dump_buffer(command_queue, oo_h, enqueue_counter, arg_index, direction, num_events_in_wait_list, event_wait_list, NULL, NULL);
      break;
    case SVMMEM:
      if (do_event)
        dump_svmptr(command_queue, oo_h, enqueue_counter, arg_index, direction, num_events_in_wait_list, event_wait_list, new_num_events_in_wait_list, new_event_wait_list);
      else
        dump_svmptr(command_queue, oo_h, enqueue_counter, arg_index, direction, num_events_in_wait_list, event_wait_list, NULL, NULL);
      break;
    default:
      break;
    }
  } else {
    //check if it is a pointer into an SVM region
    pthread_mutex_lock(&opencl_obj_mutex);
    DL_FOREACH(svmptr_objs, svm_data) {
      if (*(uintptr_t *)(arg->arg_value) > (uintptr_t)(svm_data->ptr) && *(uintptr_t *)(arg->arg_value) < (uintptr_t)(svm_data->ptr) + svm_data->size) {
        HASH_FIND_PTR(opencl_objs, (void **)(arg->arg_value), oo_h);
        break;
      }
    }
    pthread_mutex_unlock(&opencl_obj_mutex);
    if (oo_h != NULL && oo_h->type == SVMMEM) {
      if (do_event)
        dump_svmptr(command_queue, oo_h, enqueue_counter, arg_index, direction, num_events_in_wait_list, event_wait_list, new_num_events_in_wait_list, new_event_wait_list);
      else
        dump_svmptr(command_queue, oo_h, enqueue_counter, arg_index, direction, num_events_in_wait_list, event_wait_list, NULL, NULL);
    }
  }
}


static int dump_kernel_args(cl_command_queue command_queue, cl_kernel kernel, uint64_t enqueue_counter, cl_command_queue_properties properties, cl_uint *num_events_in_wait_list, cl_event **event_wait_list) {
  cl_event * new_event_wait_list = NULL;
  cl_uint new_num_events_in_wait_list = 0;
  struct opencl_obj_h *o_h = NULL;
  pthread_mutex_lock(&opencl_obj_mutex);
  HASH_FIND_PTR(opencl_objs, &kernel, o_h);
  pthread_mutex_unlock(&opencl_obj_mutex);
  if (o_h != NULL && o_h->type == KERNEL) {
    struct kernel_obj_data *k_data = (struct kernel_obj_data *)o_h->obj_data;
    if (k_data !=NULL) {
      for (cl_uint arg_index = 0; arg_index < k_data->num_args; arg_index++) {
        struct kernel_arg *arg = k_data->args + arg_index;
        tracepoint(lttng_ust_opencl_dump, kernel_arg_value, enqueue_counter, arg_index, arg->arg_size, arg->arg_value);
        if (arg->arg_value != NULL && arg->arg_size == 8) {
          dump_opencl_object(command_queue, enqueue_counter, arg, properties | CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, arg_index, 0, *num_events_in_wait_list, *event_wait_list, &new_num_events_in_wait_list, &new_event_wait_list);
        }
      }
    }
  }
  if (new_event_wait_list != NULL && new_num_events_in_wait_list > 0) {
    *event_wait_list = new_event_wait_list;
    *num_events_in_wait_list = new_num_events_in_wait_list;
    return 1;
  }
  if (new_event_wait_list != NULL)
    free(new_event_wait_list);
  return 0;
}

static cl_event dump_kernel_buffers(cl_command_queue command_queue, cl_kernel kernel, uint64_t enqueue_counter, cl_event *event) {
  cl_event * new_event_wait_list = NULL;
  cl_uint new_num_events_in_wait_list = 0;
  cl_uint num_event = (event == NULL ? 0 : 1);
  struct opencl_obj_h *o_h = NULL;
  pthread_mutex_lock(&opencl_obj_mutex);
  HASH_FIND_PTR(opencl_objs, &kernel, o_h);
  pthread_mutex_unlock(&opencl_obj_mutex);
  if (o_h != NULL && o_h->type == KERNEL) {
    struct kernel_obj_data *k_data = (struct kernel_obj_data *)o_h->obj_data;
    if (k_data !=NULL) {
      for (cl_uint arg_index = 0; arg_index < k_data->num_args; arg_index++) {
        struct kernel_arg *arg = k_data->args + arg_index;
        if (arg->arg_value != NULL && arg->arg_size == 8) {
          dump_opencl_object(command_queue, enqueue_counter, arg, event != NULL, arg_index, 1, num_event, event, &new_num_events_in_wait_list, &new_event_wait_list);
        }
      }
    }
  }
  if (new_event_wait_list != NULL && new_num_events_in_wait_list > 0) {
    cl_event ev;
627
    CL_ENQUEUE_BARRIER_WITH_WAIT_LIST_PTR(command_queue, new_num_events_in_wait_list, new_event_wait_list, &ev);
628
629
630
631
632
633
634
635
636
637
638
    for (cl_uint i = 0; i < new_num_events_in_wait_list; i++) {
      CL_RELEASE_EVENT_PTR(new_event_wait_list[i]);
    }
    free(new_event_wait_list);
    return ev;
  }
  if (new_event_wait_list != NULL)
    free(new_event_wait_list);
  return NULL;
}

639
640
641
642
643
644
645
646
647
648
649
650
static void dump_kernel_info(cl_kernel kernel) {
  cl_int error = CL_SUCCESS;
  char *function_name = "";
  size_t function_name_sz = 0;
  int free_function_name = 0;
  cl_uint num_args;
  cl_context context = NULL;
  cl_program program = NULL;
  char *attributes = "";
  size_t attributes_sz = 0;
  int free_attributes = 0;

651
  error = CL_GET_KERNEL_INFO_PTR(kernel, CL_KERNEL_NUM_ARGS, sizeof(num_args), &num_args, NULL);
652
653
  if (error != CL_SUCCESS)
    return;
654
  error = CL_GET_KERNEL_INFO_PTR(kernel, CL_KERNEL_FUNCTION_NAME, 0, NULL, &function_name_sz);
655
656
657
  if (error == CL_SUCCESS && function_name_sz > 0) {
    char *new_function_name = (char *)calloc(function_name_sz + 1, 1);
    if (new_function_name) {
658
      error = CL_GET_KERNEL_INFO_PTR(kernel, CL_KERNEL_FUNCTION_NAME, function_name_sz, new_function_name, NULL);
659
660
661
662
663
664
      if (error == CL_SUCCESS) {
        function_name = new_function_name;
        free_function_name = 1;
      }
    }
  }
665
666
667
  CL_GET_KERNEL_INFO_PTR(kernel, CL_KERNEL_CONTEXT, sizeof(context), &context, NULL);
  CL_GET_KERNEL_INFO_PTR(kernel, CL_KERNEL_PROGRAM, sizeof(program), &program, NULL);
  error = CL_GET_KERNEL_INFO_PTR(kernel, CL_KERNEL_ATTRIBUTES, 0, NULL, &attributes_sz);
668
669
670
  if (error == CL_SUCCESS && attributes_sz > 0) {
     char *new_attributes = (char *)calloc(attributes_sz + 1, 1);
     if (new_attributes) {
Brice Videau's avatar
Brice Videau committed
671
       error = CL_GET_KERNEL_INFO_PTR(kernel, CL_KERNEL_ATTRIBUTES, attributes_sz, new_attributes, NULL);
672
673
674
675
676
677
678
679
680
681
682
683
684
       if (error == CL_SUCCESS) {
         attributes = new_attributes;
         free_attributes = 1;
       }
     }
  }
  do_tracepoint(lttng_ust_opencl_arguments, kernel_info, kernel, function_name, num_args, context, program, attributes);
  if (free_function_name)
    free(function_name);
  if (free_attributes)
    free(attributes);
}

Brice Videau's avatar
Brice Videau committed
685
static void dump_argument_info(cl_kernel kernel, cl_uint arg_index) {
686
687
688
689
690
691
692
693
694
695

  cl_int error = CL_SUCCESS;
  cl_kernel_arg_address_qualifier address_qualifier;
  cl_kernel_arg_access_qualifier access_qualifier;
  char * type_name;
  size_t type_name_sz;
  cl_kernel_arg_type_qualifier type_qualifier;
  char * name;
  size_t name_sz;

Brice Videau's avatar
Brice Videau committed
696
  error = CL_GET_KERNEL_ARG_INFO_PTR(kernel, arg_index, CL_KERNEL_ARG_ADDRESS_QUALIFIER, sizeof(address_qualifier), &address_qualifier, NULL);
697
698
  if (error != CL_SUCCESS)
    return;
Brice Videau's avatar
Brice Videau committed
699
  error = CL_GET_KERNEL_ARG_INFO_PTR(kernel, arg_index, CL_KERNEL_ARG_ACCESS_QUALIFIER, sizeof(access_qualifier), &access_qualifier, NULL);
700
701
  if (error != CL_SUCCESS)
    return;
Brice Videau's avatar
Brice Videau committed
702
  error = CL_GET_KERNEL_ARG_INFO_PTR(kernel, arg_index, CL_KERNEL_ARG_TYPE_QUALIFIER, sizeof(type_qualifier), &type_qualifier, NULL);
703
704
705
  if (error != CL_SUCCESS)
    return;
  //Strings are forced to be zero terminated
Brice Videau's avatar
Brice Videau committed
706
  error = CL_GET_KERNEL_ARG_INFO_PTR(kernel, arg_index, CL_KERNEL_ARG_TYPE_NAME, 0, NULL, &type_name_sz);
707
708
  if (error != CL_SUCCESS)
    return;
Brice Videau's avatar
Brice Videau committed
709
  error = CL_GET_KERNEL_ARG_INFO_PTR(kernel, arg_index, CL_KERNEL_ARG_NAME, 0, NULL, &name_sz);
710
711
712
713
714
715
716
717
  if (error != CL_SUCCESS)
    return;
  type_name = (char*)calloc(type_name_sz+1, 1);
  if (type_name == NULL)
    return;
  name = (char*)calloc(name_sz+1, 1);
  if (name == NULL)
    goto type_name_lb;
Brice Videau's avatar
Brice Videau committed
718
  error = CL_GET_KERNEL_ARG_INFO_PTR(kernel, arg_index, CL_KERNEL_ARG_TYPE_NAME, type_name_sz, type_name, NULL);
719
720
  if (error != CL_SUCCESS)
    goto name_lb;
Brice Videau's avatar
Brice Videau committed
721
  error = CL_GET_KERNEL_ARG_INFO_PTR(kernel, arg_index, CL_KERNEL_ARG_NAME, name_sz, name, NULL);
722
723
724
  if (error != CL_SUCCESS)
    goto name_lb;
  //Menbers are initialized, call tracepoint
Brice Videau's avatar
Brice Videau committed
725
  do_tracepoint(lttng_ust_opencl_arguments, argument_info, kernel, arg_index, address_qualifier, access_qualifier, type_name, type_qualifier,  name);
726
727

  name_lb:
728
    free(name);
729
730
731
732
  type_name_lb:
    free(type_name);
}

733
734
735
736
737
static void dump_kernel_arguments(cl_program program, cl_kernel kernel) {
  struct opencl_version version = {1, 0};
  get_program_platform_version(program, &version);
  if (compare_opencl_version(&version, &opencl_version_1_2) >= 0) {
    cl_uint num_args;
738
    if ( CL_GET_KERNEL_INFO_PTR(kernel, CL_KERNEL_NUM_ARGS, sizeof(num_args), &num_args, NULL) == CL_SUCCESS ) {
739
740
741
742
743
      for (cl_uint i = 0; i < num_args ; i++)
        dump_argument_info(kernel, i);
    }
  }
}
744

745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
static inline void dump_device_name(cl_device_id device) {
  size_t  name_sz = 0;
  char   *name = "";
  if (CL_GET_DEVICE_INFO_PTR(device, CL_DEVICE_NAME, 0, NULL, &name_sz) != CL_SUCCESS)
    return;
  if (name_sz > 0) {
    name = (char *)calloc(name_sz+1, 1);
    if (!name)
      return;
    if (CL_GET_DEVICE_INFO_PTR(device, CL_DEVICE_NAME, name_sz, name, NULL) != CL_SUCCESS)
      goto cleanup;
  }
  do_tracepoint(lttng_ust_opencl_devices, device_name, device, name);
cleanup:
  free(name);
}

762
763
764
765
766
767
768
769
770
static inline void dump_program_device_build_infos(cl_program program, cl_device_id device) {
  cl_build_status build_status;
  char *build_options = "";
  size_t build_options_sz = 0;
  int free_build_options = 0;
  char *build_log = "";
  size_t build_log_sz = 0;
  int free_build_log = 0;

771
  if (CL_GET_PROGRAM_BUILD_INFO_PTR(program, device, CL_PROGRAM_BUILD_STATUS, sizeof(build_status), &build_status, NULL) != CL_SUCCESS)
772
    return;
773
  if (CL_GET_PROGRAM_BUILD_INFO_PTR(program, device, CL_PROGRAM_BUILD_OPTIONS, 0, NULL, &build_options_sz) != CL_SUCCESS)
774
    return;
775
  if (CL_GET_PROGRAM_BUILD_INFO_PTR(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &build_log_sz) != CL_SUCCESS)
776
777
778
779
780
781
782
    return;

  if (build_options_sz > 0) {
    build_options = (char *)calloc(build_options_sz+1, 1);
    if(!build_options)
      return;
    free_build_options = 1;
783
    if(CL_GET_PROGRAM_BUILD_INFO_PTR(program, device, CL_PROGRAM_BUILD_OPTIONS, build_options_sz, build_options, NULL) != CL_SUCCESS)
784
785
      goto cleanup;
  }
786

787
788
789
790
791
  if (build_log_sz > 0) {
    build_log = (char *)calloc(build_log_sz+1, 1);
    if(!build_log)
      goto cleanup;
    free_build_log = 1;
792
    if(CL_GET_PROGRAM_BUILD_INFO_PTR(program, device, CL_PROGRAM_BUILD_LOG, build_log_sz, build_log, NULL) != CL_SUCCESS)
793
794
795
796
797
798
799
800
801
802
803
804
805
806
      goto cleanup;
  }

  do_tracepoint(lttng_ust_opencl_build, infos, program, device, build_status, build_options, build_log);
cleanup:
  if (free_build_options)
    free(build_options);
  if (free_build_log)
    free(build_log);
}

static inline void dump_program_device_build_infos_1_2(cl_program program, cl_device_id device) {
  cl_program_binary_type binary_type;

807
  if (CL_GET_PROGRAM_BUILD_INFO_PTR(program, device, CL_PROGRAM_BINARY_TYPE, sizeof(binary_type), &binary_type, NULL) != CL_SUCCESS)
808
809
810
811
812
813
814
815
    return;

  do_tracepoint(lttng_ust_opencl_build, infos_1_2, program, device, binary_type);
}

static inline void dump_program_device_build_infos_2_0(cl_program program, cl_device_id device) {
  size_t build_global_variable_total_size;

816
  if (CL_GET_PROGRAM_BUILD_INFO_PTR(program, device, CL_PROGRAM_BUILD_GLOBAL_VARIABLE_TOTAL_SIZE, sizeof(size_t), &build_global_variable_total_size, NULL) != CL_SUCCESS)
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
    return;

  do_tracepoint(lttng_ust_opencl_build, infos_2_0, program, device, build_global_variable_total_size);
}

static void dump_program_build_infos(cl_program program) {
  cl_device_id *devices;
  cl_uint num_devices;
  cl_uint i;

  devices = get_program_devices(program, &num_devices);
  if (!devices)
    return;

  for(i = 0; i < num_devices; i++) {
    dump_program_device_build_infos(program, devices[i]);
    dump_program_device_build_infos_1_2(program, devices[i]);
    dump_program_device_build_infos_2_0(program, devices[i]);
  }
}

838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
static void dump_program_objects(cl_program program) {
  cl_int err = CL_SUCCESS;
  cl_device_id *devices;
  cl_uint num_devices;
  size_t *binary_sizes;
  size_t total_size = 0;
  void **binaries;
  char (*paths)[sizeof(OBJ_BUILD_TEMPLATE)];
  int *fds;
  cl_uint i;

  devices = get_program_devices(program, &num_devices);
  if (!devices)
    goto with_noting;
  binary_sizes = (size_t *)malloc(num_devices*sizeof(size_t));
  if (!binary_sizes)
    goto with_devices;

  err = CL_GET_PROGRAM_INFO_PTR(program, CL_PROGRAM_BINARY_SIZES, num_devices*sizeof(size_t), binary_sizes, NULL);
  if (err != CL_SUCCESS)
    goto with_binary_sizes;

  binaries = (void **)calloc(num_devices*sizeof(void *), 1);
  if (!binaries)
    goto with_binary_sizes;
  paths = (char (*)[sizeof(OBJ_BUILD_TEMPLATE)])calloc(num_devices*sizeof(OBJ_BUILD_TEMPLATE), 1);
  if (!paths)
    goto with_binaries;
  fds = (int *)calloc(num_devices*sizeof(int), 1);
  if (!fds)
    goto with_paths;

  for (i = 0; i < num_devices; i++) {
    if (binary_sizes[i] > 0) {
      strncpy(paths[i], OBJ_BUILD_TEMPLATE, sizeof(paths[i]));
      fds[i] = create_file_and_map(paths[i], binary_sizes[i], binaries + i);
      if (fds[i] == -1) {
        for (cl_uint j = i; j > 0; j--) {
          if (binary_sizes[j-1] > 0) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
            ftruncate(fds[j-1], 0);
#pragma GCC diagnostic pop
            close(fds[j-1]);
            unlink(paths[j-1]);
          }
        }
        goto with_fds;
      }
      total_size += binary_sizes[i];
    }
  }

  err = CL_GET_PROGRAM_INFO_PTR(program, CL_PROGRAM_BINARIES, total_size, binaries, NULL);
  if (err != CL_SUCCESS) {
    for (i = 0; i < num_devices; i++) {
      if (binary_sizes[i] > 0) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
        ftruncate(fds[i], 0);
#pragma GCC diagnostic pop
        close(fds[i]);
        unlink(paths[i]);
      }
    }
  }
  for (i = 0; i < num_devices; i++) {
    int e = ftruncate(fds[i], binary_sizes[i]);
    if (e) {
      unlink(paths[i]);
      close(fds[i]);
    } else {
      close(fds[i]);
      do_tracepoint(lttng_ust_opencl_build, objects, program, devices[i], binary_sizes[i], paths[i]);
    }
  }
with_fds:
  free(fds);
with_paths:
  free(paths);
with_binaries:
  free(binaries);
with_binary_sizes:
  free(binary_sizes);
with_devices:
  free(devices);
with_noting:
  return;
}

Brice Videau's avatar
Brice Videau committed
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
static void dump_program_binaries(cl_program program) {
  cl_int err = CL_SUCCESS;
  cl_device_id *devices;
  cl_uint num_devices;
  size_t *binary_sizes;
  size_t total_size = 0;
  void **binaries;
  char (*paths)[sizeof(BIN_BUILD_TEMPLATE)];
  int *fds;
  cl_uint i;

  devices = get_program_devices(program, &num_devices);
  if (!devices)
    goto with_noting;
  binary_sizes = (size_t *)malloc(num_devices*sizeof(size_t));
  if (!binary_sizes)
    goto with_devices;

946
  err = CL_GET_PROGRAM_INFO_PTR(program, CL_PROGRAM_BINARY_SIZES, num_devices*sizeof(size_t), binary_sizes, NULL);
Brice Videau's avatar
Brice Videau committed
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
  if (err != CL_SUCCESS)
    goto with_binary_sizes;

  binaries = (void **)calloc(num_devices*sizeof(void *), 1);
  if (!binaries)
    goto with_binary_sizes;
  paths = (char (*)[sizeof(BIN_BUILD_TEMPLATE)])calloc(num_devices*sizeof(BIN_BUILD_TEMPLATE), 1);
  if (!paths)
    goto with_binaries;
  fds = (int *)calloc(num_devices*sizeof(int), 1);
  if (!fds)
    goto with_paths;

  for (i = 0; i < num_devices; i++) {
    if (binary_sizes[i] > 0) {
      strncpy(paths[i], BIN_BUILD_TEMPLATE, sizeof(paths[i]));
      fds[i] = create_file_and_map(paths[i], binary_sizes[i], binaries + i);
      if (fds[i] == -1) {
        for (cl_uint j = i; j > 0; j--) {
          if (binary_sizes[j-1] > 0) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
            ftruncate(fds[j-1], 0);
#pragma GCC diagnostic pop
            close(fds[j-1]);
            unlink(paths[j-1]);
          }
        }
        goto with_fds;
      }
      total_size += binary_sizes[i];
    }
  }

981
  err = CL_GET_PROGRAM_INFO_PTR(program, CL_PROGRAM_BINARIES, total_size, binaries, NULL);
Brice Videau's avatar
Brice Videau committed
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
  if (err != CL_SUCCESS) {
    for (i = 0; i < num_devices; i++) {
      if (binary_sizes[i] > 0) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-result"
        ftruncate(fds[i], 0);
#pragma GCC diagnostic pop
        close(fds[i]);
        unlink(paths[i]);
      }
    }
  }
  for (i = 0; i < num_devices; i++) {
    int e = ftruncate(fds[i], binary_sizes[i]);
    if (e) {
      unlink(paths[i]);
      close(fds[i]);
    } else {
      close(fds[i]);
      do_tracepoint(lttng_ust_opencl_build, binaries, program, devices[i], binary_sizes[i], paths[i]);
    }
  }
with_fds:
  free(fds);
with_paths:
  free(paths);
with_binaries:
  free(binaries);
with_binary_sizes:
  free(binary_sizes);
with_devices:
  free(devices);
with_noting:
  return;
}

1018
1019
1020
1021
1022
1023
1024
struct clLinkProgram_callback_payload {
  void (CL_CALLBACK *pfn_notify)(cl_program program, void *user_data);
  void *user_data;
};

void CL_CALLBACK clLinkProgram_callback(cl_program program, void *user_data) {
  struct clLinkProgram_callback_payload *payload = (struct clLinkProgram_callback_payload *)user_data;
1025

1026
  do_tracepoint_safe(lttng_ust_opencl, STARTEV(clLinkProgram_callback), program, payload->user_data);
1027
  payload->pfn_notify(program, payload->user_data);
1028
  do_tracepoint_safe(lttng_ust_opencl, STOPEV(clLinkProgram_callback), program, payload->user_data);
1029
  if (tracepoint_enabled(lttng_ust_opencl_build, binaries))
1030
    dump_program_binaries(program);
1031
  if (tracepoint_enabled(lttng_ust_opencl_build, infos))
1032
    dump_program_build_infos(program);
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
  free(user_data);
}

struct clCompileProgram_callback_payload {
  void (CL_CALLBACK *pfn_notify)(cl_program program, void *user_data);
  void *user_data;
};

void CL_CALLBACK clCompileProgram_callback(cl_program program, void *user_data) {
  struct clCompileProgram_callback_payload *payload = (struct clCompileProgram_callback_payload *)user_data;
1043

1044
  do_tracepoint_safe(lttng_ust_opencl, STARTEV(clCompileProgram_callback), program, payload->user_data);
1045
  payload->pfn_notify(program, payload->user_data);
1046
  do_tracepoint_safe(lttng_ust_opencl, STOPEV(clCompileProgram_callback), program, payload->user_data);
1047
1048
  if (tracepoint_enabled(lttng_ust_opencl_build, objects))
    dump_program_objects(program);
1049
1050
  if (tracepoint_enabled(lttng_ust_opencl_build, infos))
    dump_program_build_infos(program);
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
  free(user_data);
}

struct clBuildProgram_callback_payload {
  void (CL_CALLBACK *pfn_notify)(cl_program program, void *user_data);
  void *user_data;
};

void CL_CALLBACK clBuildProgram_callback(cl_program program, void *user_data) {
  struct clBuildProgram_callback_payload *payload = (struct clBuildProgram_callback_payload *)user_data;
1061

1062
  do_tracepoint_safe(lttng_ust_opencl, STARTEV(clBuildProgram_callback), program, payload->user_data);
1063
  payload->pfn_notify(program, payload->user_data);
1064
  do_tracepoint_safe(lttng_ust_opencl, STOPEV(clBuildProgram_callback), program, payload->user_data);
1065
1066
  if (tracepoint_enabled(lttng_ust_opencl_build, binaries))
    dump_program_binaries(program);
1067
1068
  if (tracepoint_enabled(lttng_ust_opencl_build, infos))
    dump_program_build_infos(program);
1069
1070
1071
  free(user_data);
}

1072
1073
1074
1075
1076
1077
1078
1079
struct clCreateContext_callback_payload {
  void (CL_CALLBACK *pfn_notify)(const char *errinfo, const void *private_info, size_t cb, void *user_data);
  void *user_data;
};

void CL_CALLBACK clCreateContext_callback(const char *errinfo, const void *private_info, size_t cb, void *user_data) {
  struct clCreateContext_callback_payload *payload = (struct clCreateContext_callback_payload *)user_data;

1080
  tracepoint_safe(lttng_ust_opencl, STARTEV(clCreateContext_callback), errinfo, private_info, cb, payload->user_data);
1081
  payload->pfn_notify(errinfo, private_info, cb, payload->user_data);
1082
  tracepoint_safe(lttng_ust_opencl, STOPEV(clCreateContext_callback), errinfo, private_info, cb, payload->user_data);
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
}

struct clCreateContextFromType_callback_payload {
  void (CL_CALLBACK *pfn_notify)(const char *errinfo, const void *private_info, size_t cb, void *user_data);
  void *user_data;
};

void CL_CALLBACK clCreateContextFromType_callback(const char *errinfo, const void *private_info, size_t cb, void *user_data) {
  struct clCreateContextFromType_callback_payload *payload = (struct clCreateContextFromType_callback_payload *)user_data;

1093
  tracepoint_safe(lttng_ust_opencl, STARTEV(clCreateContextFromType_callback), errinfo, private_info, cb, payload->user_data);
1094
  payload->pfn_notify(errinfo, private_info, cb, payload->user_data);
1095
  tracepoint_safe(lttng_ust_opencl, STOPEV(clCreateContextFromType_callback), errinfo, private_info, cb, payload->user_data);
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
}

struct clSetMemObjectDestructorCallback_callback_payload {
  void (CL_CALLBACK *pfn_notify)(cl_mem memobj, void * user_data);
  void *user_data;
};

void CL_CALLBACK clSetMemObjectDestructorCallback_callback(cl_mem memobj, void * user_data) {
  struct clSetMemObjectDestructorCallback_callback_payload *payload = (struct clSetMemObjectDestructorCallback_callback_payload *)user_data;

1106
  do_tracepoint_safe(lttng_ust_opencl, STARTEV(clSetMemObjectDestructorCallback_callback), memobj, payload->user_data);
1107
  payload->pfn_notify(memobj, payload->user_data);
1108
  do_tracepoint_safe(lttng_ust_opencl, STOPEV(clSetMemObjectDestructorCallback_callback), memobj, payload->user_data);
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
  free(user_data);
}

struct clSetProgramReleaseCallback_callback_payload {
  void (CL_CALLBACK *pfn_notify)(cl_program program, void * user_data);
  void *user_data;
};

void CL_CALLBACK clSetProgramReleaseCallback_callback(cl_program program, void * user_data) {
  struct clSetProgramReleaseCallback_callback_payload *payload = (struct clSetProgramReleaseCallback_callback_payload *)user_data;

1120
  do_tracepoint_safe(lttng_ust_opencl, STARTEV(clSetProgramReleaseCallback_callback), program, payload->user_data);
1121
  payload->pfn_notify(program, payload->user_data);
1122
  do_tracepoint_safe(lttng_ust_opencl, STOPEV(clSetProgramReleaseCallback_callback), program, payload->user_data);
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
  free(user_data);
}

struct clSetEventCallback_callback_payload {
  void (CL_CALLBACK *pfn_notify)(cl_event event, cl_int type, void * user_data);
  void *user_data;
};

void CL_CALLBACK clSetEventCallback_callback(cl_event event, cl_int type, void * user_data) {
  struct clSetEventCallback_callback_payload *payload = (struct clSetEventCallback_callback_payload *)user_data;

1134
  do_tracepoint_safe(lttng_ust_opencl, STARTEV(clSetEventCallback_callback), event, type, payload->user_data);
1135
  payload->pfn_notify(event, type, payload->user_data);
1136
  do_tracepoint_safe(lttng_ust_opencl, STOPEV(clSetEventCallback_callback), event, type, payload->user_data);
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
  free(user_data);
}

struct clEnqueueSVMFree_callback_payload {
  void (CL_CALLBACK *pfn_free_func)(cl_command_queue command_queue, cl_uint num_svm_pointers, void *svm_pointers[], void* user_data);
  void *user_data;
};

void CL_CALLBACK clEnqueueSVMFree_callback(cl_command_queue command_queue, cl_uint num_svm_pointers, void *svm_pointers[], void* user_data) {
  struct clEnqueueSVMFree_callback_payload *payload = (struct clEnqueueSVMFree_callback_payload *)user_data;

1148
  do_tracepoint_safe(lttng_ust_opencl, STARTEV(clEnqueueSVMFree_callback), command_queue, num_svm_pointers, svm_pointers, payload->user_data);
1149
  payload->pfn_free_func(command_queue, num_svm_pointers, svm_pointers, payload->user_data);
1150
  do_tracepoint_safe(lttng_ust_opencl, STOPEV(clEnqueueSVMFree_callback), command_queue, num_svm_pointers, svm_pointers, payload->user_data);
1151
1152
1153
  free(user_data);
}

1154
1155
1156
1157
1158
1159
static inline uint64_t get_timestamp_ns() {
  struct timespec ts;
  clock_gettime(CLOCK_MONOTONIC, &ts);
  return ((uint64_t)ts.tv_sec)*1000000000+((uint64_t)ts.tv_nsec);
}

Brice Videau's avatar
Brice Videau committed
1160
static pthread_once_t _init = PTHREAD_ONCE_INIT;
Brice Videau's avatar
Brice Videau committed
1161
1162
static __thread volatile int in_init = 0;
static volatile cl_uint _initialized = 0;
Brice Videau's avatar
Brice Videau committed
1163
1164

static void _load_tracer(void) {
1165
1166
1167
1168
1169
  char *s = NULL;
  void * handle = NULL;

  s = getenv("LTTNG_UST_OPENCL_LIBOPENCL");
  if (s)
1170
      handle = dlopen(s, RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND);
1171
  else
1172
      handle = dlopen("libOpenCL.so", RTLD_LAZY | RTLD_LOCAL | RTLD_DEEPBIND);
Brice Videau's avatar
Brice Videau committed
1173
1174
1175
1176
1177
  if( !handle ) {
    printf("Failure: could not load OpenCL library!\n");
    exit(1);
  }

1178
1179
1180
  s = getenv("LTTNG_UST_OPENCL_HOST_PROFILE");
  if (s)
    do_host_profile = 1;
Brice Videau's avatar
Brice Videau committed
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
  s = getenv("LTTNG_UST_OPENCL_DUMP");
  if (s)
    do_dump = 1;
  s = getenv("LTTNG_UST_OPENCL_DUMP_START");
  if (s)
    dump_start = atoll(s);
  s = getenv("LTTNG_UST_OPENCL_DUMP_END");
  if (s)
    dump_end = atoll(s);

  find_opencl_symbols(handle);
}

static inline void _init_tracer(void) {
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
  if( __builtin_expect (_initialized, 1) )
    return;
  /* Avoid reentrancy */
  if (!in_init) {
    in_init=1;
    __sync_synchronize();
    pthread_once(&_init, _load_tracer);
    __sync_synchronize();
    in_init=0;
  }
  _initialized = 1;
Brice Videau's avatar
Brice Videau committed
1206
1207
}