summaryrefslogtreecommitdiff
path: root/ufo/ufo-profiler.c
blob: 3c372ed5a2865ed9cbffecebf3c2174a2d2e9919 (plain)
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
/*
 * Copyright (C) 2011-2013 Karlsruhe Institute of Technology
 *
 * This file is part of Ufo.
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation, either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef __APPLE__
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
#include <gmodule.h>
#include <glob.h>

#include <ufo/ufo-profiler.h>
#include <ufo/ufo-resources.h>

/**
 * SECTION:ufo-profiler
 * @Short_description: Profile different measures
 * @Title: UfoProfiler
 *
 * The #UfoProfiler provides a drop-in replacement for a manual
 * clEnqueueNDRangeKernel() call and tracks any associated events.
 *
 * Each #UfoTaskNode is assigned a profiler with ufo_task_node_set_profiler() by
 * the managing #UfoBaseScheduler. Task implementations should call
 * ufo_task_node_get_profiler() to receive their profiler and make profiled
 * kernel calls with ufo_profiler_call().
 */

G_DEFINE_TYPE(UfoProfiler, ufo_profiler, G_TYPE_OBJECT)

#define UFO_PROFILER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), UFO_TYPE_PROFILER, UfoProfilerPrivate))

struct EventRow {
    cl_event    event;
    cl_kernel   kernel;
    cl_command_queue queue;
};

struct _UfoProfilerPrivate {
    GArray  *event_array;
    GTimer **timers;
    GList   *trace_events;
    gboolean trace;
};

enum {
    PROP_0,
    N_PROPERTIES
};

static GTimer *global_clock = NULL;


/**
 * UfoProfilerTimer:
 * @UFO_PROFILER_TIMER_IO: Select I/O timer
 * @UFO_PROFILER_TIMER_CPU: Select CPU timer
 * @UFO_PROFILER_TIMER_GPU: Select GPU timer
 * @UFO_PROFILER_TIMER_FETCH: Select timer that measures the synchronization
 *  time to fetch data from the queues.
 * @UFO_PROFILER_TIMER_RELEASE: Select timer that measures the synchronization
 *  time to push data to the queues.
 * @UFO_PROFILER_TIMER_LAST: Auxiliary value, do not use.
 *
 * Use these values to select a specific timer when calling
 * ufo_profiler_start(), ufo_profiler_stop() and ufo_profiler_elapsed().
 */

/**
 * ufo_profiler_new:
 *
 * Create a profiler object.
 *
 * Return value: A new profiler object.
 */
UfoProfiler *
ufo_profiler_new (void)
{
    return UFO_PROFILER (g_object_new (UFO_TYPE_PROFILER, NULL));
}

static void
_ufo_profiler_call (UfoProfiler    *profiler,
                    gpointer        command_queue,
                    gpointer        kernel,
                    guint           work_dim,
                    const gsize    *global_work_size,
                    const gsize    *local_work_size,
                    gboolean        block)
{
    UfoProfilerPrivate *priv;
    cl_int              cl_err;
    cl_event            event;

    g_return_if_fail (UFO_IS_PROFILER (profiler));
    priv = profiler->priv;

    if (priv->trace) {
        struct EventRow row;

        cl_err = clEnqueueNDRangeKernel (command_queue, kernel, work_dim, NULL, global_work_size, local_work_size, 0, NULL, &event);

        row.event = event;
        row.kernel = kernel;
        row.queue = command_queue;
        g_array_append_val (priv->event_array, row);
    }
    else {
        cl_err = clEnqueueNDRangeKernel (command_queue, kernel, work_dim, NULL, global_work_size, local_work_size, 0, NULL, &event);
    }

    UFO_RESOURCES_CHECK_CLERR (cl_err);

    if (block) {
        /* Wait for the kernel to finish */
        UFO_RESOURCES_CHECK_CLERR (clWaitForEvents (1, &event));
        /* Let the tracing handle the event if enabled */
        if (!priv->trace) {
            UFO_RESOURCES_CHECK_CLERR (clReleaseEvent (event));
        }
    }
}

/**
 * ufo_profiler_call:
 * @profiler: A #UfoProfiler object.
 * @command_queue: A %cl_command_queue
 * @kernel: A %cl_kernel
 * @work_dim: Number of working dimensions.
 * @global_work_size: Sizes of global dimensions. The array must have at least
 *      @work_dim entries.
 * @local_work_size: Sizes of local work group dimensions. The array must have
 *      at least @work_dim entries.
 *
 * Execute the @kernel using the command queue and execution parameters. The
 * event associated with the clEnqueueNDRangeKernel() call is recorded and may
 * be used for profiling purposes later on.
 */
void
ufo_profiler_call (UfoProfiler    *profiler,
                   gpointer        command_queue,
                   gpointer        kernel,
                   guint           work_dim,
                   const gsize    *global_work_size,
                   const gsize    *local_work_size)
{
    _ufo_profiler_call (profiler, command_queue, kernel, work_dim, global_work_size, local_work_size, FALSE);
}

/**
 * ufo_profiler_call_blocking:
 * @profiler: A #UfoProfiler object.
 * @command_queue: A %cl_command_queue
 * @kernel: A %cl_kernel
 * @work_dim: Number of working dimensions.
 * @global_work_size: Sizes of global dimensions. The array must have at least
 *      @work_dim entries.
 * @local_work_size: Sizes of local work group dimensions. The array must have
 *      at least @work_dim entries.
 *
 * Execute the @kernel using the command queue and execution parameters and wait
 * for the kernel to finish. The event associated with the
 * clEnqueueNDRangeKernel() call is recorded and may be used for profiling
 * purposes later on.
 */
void
ufo_profiler_call_blocking (UfoProfiler    *profiler,
                            gpointer        command_queue,
                            gpointer        kernel,
                            guint           work_dim,
                            const gsize    *global_work_size,
                            const gsize    *local_work_size)
{
    _ufo_profiler_call (profiler, command_queue, kernel, work_dim, global_work_size, local_work_size, TRUE);
}

/**
 * ufo_profiler_register_event:
 * @profiler: A #UfoProfiler object
 * @command_queue: Queue the OpenCL event was issued with
 * @kernel: An OpenCL kernel the event stems from
 * @event: An OpenCL event
 *
 * Register a kernel execution manually
 */
void
ufo_profiler_register_event (UfoProfiler *profiler,
                             gpointer command_queue,
                             gpointer kernel,
                             gpointer event)
{
    UfoProfilerPrivate *priv;
    g_return_if_fail (UFO_IS_PROFILER (profiler));
    priv = profiler->priv;

    if (priv->trace) {
        struct EventRow row;
        row.event = event;
        row.kernel = kernel;
        row.queue = command_queue;
        g_array_append_val (priv->event_array, row);
    }
}

/**
 * ufo_profiler_start:
 * @profiler: A #UfoProfiler object
 * @timer: Which timer to start
 *
 * Start @timer. The timer is not reset but accumulates the time elapsed between
 * ufo_profiler_start() and ufo_profiler_stop() calls.
 */
void
ufo_profiler_start (UfoProfiler      *profiler,
                    UfoProfilerTimer  timer)
{
    g_return_if_fail (UFO_IS_PROFILER (profiler));
    g_timer_continue (profiler->priv->timers[timer]);
}

/**
 * ufo_profiler_stop:
 * @profiler: A #UfoProfiler object
 * @timer: Which timer to stop
 *
 * Stop @timer. The timer is not reset but accumulates the time elapsed between
 * ufo_profiler_start() and ufo_profiler_stop() calls.
 */
void
ufo_profiler_stop (UfoProfiler       *profiler,
                   UfoProfilerTimer   timer)
{
    g_return_if_fail (UFO_IS_PROFILER (profiler));
    g_timer_stop (profiler->priv->timers[timer]);
}

/**
 * ufo_profiler_trace_event:
 * @profiler: A #UfoProfiler object
 * @type: trav event type
 *
 * Register a new trace event. The given event type, the thread id and the
 * global time is stored when this function is called.
 */
void
ufo_profiler_trace_event (UfoProfiler *profiler,
                          UfoTraceEventType type)
{
    UfoTraceEvent *event;

    g_return_if_fail (UFO_IS_PROFILER (profiler));

    if (!profiler->priv->trace)
        return;

    event = g_malloc0 (sizeof(UfoTraceEvent));
    event->type = type;
    event->thread_id = g_thread_self ();
    event->timestamp = g_timer_elapsed (global_clock, NULL);
    profiler->priv->trace_events = g_list_append (profiler->priv->trace_events, event);
}

/**
 * ufo_profiler_enable_tracing:
 * @profiler: A #UfoProfiler object
 * @enable: %TRUE if tracing should be enabled
 *
 * Enable or disable tracing of @profiler. Calls to ufo_profiler_trace_event()
 * will be ignored if tracing is disabled.
 */
void
ufo_profiler_enable_tracing (UfoProfiler *profiler,
                             gboolean enable)
{
    g_return_if_fail (UFO_IS_PROFILER (profiler));
    profiler->priv->trace = enable;
}

/**
 * ufo_profiler_get_trace_events: (skip)
 * @profiler: A #UfoProfiler object.
 *
 * Get all events recorded with @profiler.
 *
 * Returns: (element-type UfoTraceEvent): A list with #UfoTraceEvent objects.
 */
GList *
ufo_profiler_get_trace_events (UfoProfiler *profiler)
{
    g_return_val_if_fail (UFO_IS_PROFILER (profiler), NULL);
    return profiler->priv->trace_events;
}

static void
get_time_stamps (cl_event event, gulong *queued, gulong *submitted, gulong *start, gulong *end)
{
    UFO_RESOURCES_CHECK_CLERR (clWaitForEvents (1, &event));
    UFO_RESOURCES_CHECK_CLERR (clGetEventProfilingInfo (event, CL_PROFILING_COMMAND_QUEUED, sizeof (cl_ulong), queued, NULL));
    UFO_RESOURCES_CHECK_CLERR (clGetEventProfilingInfo (event, CL_PROFILING_COMMAND_SUBMIT, sizeof (cl_ulong), submitted, NULL));
    UFO_RESOURCES_CHECK_CLERR (clGetEventProfilingInfo (event, CL_PROFILING_COMMAND_START, sizeof (cl_ulong), start, NULL));
    UFO_RESOURCES_CHECK_CLERR (clGetEventProfilingInfo (event, CL_PROFILING_COMMAND_END, sizeof (cl_ulong), end, NULL));
}

static gdouble
gpu_elapsed (UfoProfilerPrivate *priv)
{
    struct EventRow *row;
    gdouble elapsed = 0.0;
    guint len = priv->event_array->len;

    if (len == 0)
        return 0.0;

    for (guint i = 0; i < len; i++) {
        gulong start, end;

        row = &g_array_index (priv->event_array, struct EventRow, i);

        UFO_RESOURCES_CHECK_CLERR (clGetEventInfo (row->event, CL_EVENT_COMMAND_QUEUE,
                                                   sizeof (cl_command_queue), &row->queue,
                                                   NULL));

        get_time_stamps (row->event, NULL, NULL, &start, &end);

        if (end < start)
            elapsed += (gdouble) ((G_MAXULONG - start) + end) * 1e-9;
        else
            elapsed += ((gdouble) (end - start)) * 1e-9;
    }

    return elapsed;
}

/**
 * ufo_profiler_elapsed:
 * @profiler: A #UfoProfiler object
 * @timer: Which timer to start
 *
 * Get the elapsed time in seconds for @timer.
 *
 * Returns: Elapsed time in seconds.
 */
gdouble
ufo_profiler_elapsed (UfoProfiler       *profiler,
                      UfoProfilerTimer   timer)
{
    g_return_val_if_fail (UFO_IS_PROFILER (profiler), 0.0);

    if (timer == UFO_PROFILER_TIMER_GPU)
        return gpu_elapsed (profiler->priv);

    return g_timer_elapsed (profiler->priv->timers[timer], NULL);
}

static gchar *
get_kernel_name (cl_kernel kernel)
{
    gsize size;
    gchar *s;

    clGetKernelInfo (kernel, CL_KERNEL_FUNCTION_NAME, 0, NULL, &size);
    s = g_malloc0(size + 1);
    clGetKernelInfo (kernel, CL_KERNEL_FUNCTION_NAME, size, s, NULL);
    return s;
}

/**
 * ufo_profiler_foreach:
 * @profiler: A #UfoProfiler object
 * @func: (scope call): The function to be called for an entry
 * @user_data: User parameters
 *
 * Iterates through the recorded events and calls @func on each entry.
 */
void
ufo_profiler_foreach (UfoProfiler    *profiler,
                      UfoProfilerFunc func,
                      gpointer        user_data)
{
    UfoProfilerPrivate *priv;
    GHashTable *names;
    struct EventRow *row;

    g_return_if_fail (UFO_IS_PROFILER (profiler));

    priv = profiler->priv;
    names = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);

    for (guint i = 0; i < priv->event_array->len; i++) {
        cl_command_queue queue;
        gchar *name;
        gulong queued, submitted, start, end;

        row = &g_array_index (priv->event_array, struct EventRow, i);
        name = g_hash_table_lookup (names, row->kernel);

        if (name == NULL) {
            name = get_kernel_name (row->kernel);
            g_hash_table_insert (names, row->kernel, name);
        }

        clGetEventInfo (row->event, CL_EVENT_COMMAND_QUEUE, sizeof (cl_command_queue), &queue, NULL);
        get_time_stamps (row->event, &queued, &submitted, &start, &end);
        func (name, queue, queued, submitted, start, end, user_data);
    }

    g_hash_table_destroy (names);
}

static void
ufo_profiler_dispose (GObject *object)
{
    G_OBJECT_CLASS (ufo_profiler_parent_class)->dispose (object);
}

static void
ufo_profiler_finalize (GObject *object)
{
    UfoProfilerPrivate *priv;
    struct EventRow *row;

    G_OBJECT_CLASS (ufo_profiler_parent_class)->finalize (object);
    priv = UFO_PROFILER_GET_PRIVATE (object);

    for (guint i = 0; i < priv->event_array->len; i++) {
        row = &g_array_index (priv->event_array, struct EventRow, i);
        clReleaseEvent (row->event);
    }

    g_array_free (priv->event_array, TRUE);

    g_list_foreach (priv->trace_events, (GFunc) g_free, NULL);
    g_list_free (priv->trace_events);

    for (guint i = 0; i < UFO_PROFILER_TIMER_LAST; i++)
        g_timer_destroy (priv->timers[i]);

    g_free (priv->timers);
}

static void
ufo_profiler_class_init (UfoProfilerClass *klass)
{
    GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
    gobject_class->dispose = ufo_profiler_dispose;
    gobject_class->finalize = ufo_profiler_finalize;

    g_type_class_add_private (klass, sizeof (UfoProfilerPrivate));

    if (global_clock == NULL)
        global_clock = g_timer_new ();
}

static void
ufo_profiler_init (UfoProfiler *manager)
{
    UfoProfilerPrivate *priv;

    manager->priv = priv = UFO_PROFILER_GET_PRIVATE (manager);
    priv->event_array = g_array_sized_new (FALSE, TRUE, sizeof(struct EventRow), 2048);
    priv->trace_events = NULL;
    priv->trace = FALSE;

    /* Setup timers for all events */
    priv->timers = g_new0 (GTimer *, UFO_PROFILER_TIMER_LAST);

    for (guint i = 0; i < UFO_PROFILER_TIMER_LAST; i++) {
        GTimer *timer;

        timer = g_timer_new ();
        g_timer_stop (timer);
        g_timer_reset (timer);
        priv->timers[i] = timer;
    }
}