clinterval_callbacks.cpp.erb 15.3 KB
Newer Older
Thomas Applencourt's avatar
Thomas Applencourt committed
1
2
#include <iomanip>
#include <iostream>
Thomas Applencourt's avatar
Thomas Applencourt committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "clinterval.h"
#include "clinterval_callbacks.hpp"
#include "xprof_utils.hpp"
#include <babeltrace2/babeltrace.h>

#define CL_TARGET_OPENCL_VERSION 300
#define CL_USE_DEPRECATED_OPENCL_1_0_APIS
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
#define CL_USE_DEPRECATED_OPENCL_1_2_APIS
#define CL_USE_DEPRECATED_OPENCL_2_0_APIS
#define CL_USE_DEPRECATED_OPENCL_2_1_APIS
#define CL_USE_DEPRECATED_OPENCL_2_2_APIS
#include <CL/opencl.h>
#include <CL/cl_gl_ext.h>
#include <CL/cl_egl.h>
#include <CL/cl_ext_intel.h>
#include "tracer_opencl.h"
Thomas Applencourt's avatar
Thomas Applencourt committed
20

21
22
23
24
void *init_clinterval_callbacks_state() {
    clinterval_callbacks_state *s = new clinterval_callbacks_state;
    return (void*) s;
}
Thomas Applencourt's avatar
Thomas Applencourt committed
25
26
27

static void create_and_enqueue_host_message(const char* hostname, const process_id_t process_id, const thread_id_t thread_id, const char* name, 
                                     const uint64_t ts, const uint64_t duration, const bool err) {
28

Thomas Applencourt's avatar
Thomas Applencourt committed
29
     /* Message creation */
Thomas Applencourt's avatar
Thomas Applencourt committed
30
31
32
33
     bt_message *message = create_host_message(hostname, process_id, thread_id, name, ts, duration, err, 
                            clinterval_iter_g->dispatch->host_event_class,
                            self_message_iterator_g,
                            clinterval_iter_g->dispatch->stream);
Thomas Applencourt's avatar
Thomas Applencourt committed
34

35
36
     clinterval_callbacks_state* state = (clinterval_callbacks_state*) clinterval_iter_g->callbacks_state;
     state->downstream_message_queue.push(message);
Thomas Applencourt's avatar
Thomas Applencourt committed
37
38
39
40
41
42
}

static void create_and_enqueue_device_message(const char* hostname, const process_id_t process_id, const thread_id_t thread_id, const thapi_device_id device_id, const thapi_device_id subdevice_id,
                                       const char* name, const uint64_t ts, const uint64_t duration) {
    
     /* Message creation */
Thomas Applencourt's avatar
Thomas Applencourt committed
43
44
45
46
47
     bt_message *message = create_device_message(hostname, process_id, thread_id, device_id, subdevice_id, name, ts, duration,
                            clinterval_iter_g->dispatch->device_event_class,
                            self_message_iterator_g,
                            clinterval_iter_g->dispatch->stream);
 
Thomas Applencourt's avatar
Thomas Applencourt committed
48
49
 
     /* Set message */
50
51
     clinterval_callbacks_state* state = (clinterval_callbacks_state*) clinterval_iter_g->callbacks_state;
     state->downstream_message_queue.push(message);
Thomas Applencourt's avatar
Thomas Applencourt committed
52
53
54
55
56
57
58
59
60
}

<%#
  _                                         _                          _ ___ 
 | \  _       ._   _ _|_ ._ _   _. ._ _    / \      _       _     /\  |_) |  
 |_/ (_) \/\/ | | _>  |_ | (/_ (_| | | |   \_X |_| (/_ |_| (/_   /--\ |  _|_ 
                                                                             
%>

61
bool downstream_message_queue_empty(struct clinterval_message_iterator *clinterval_dispatch_iter)
Thomas Applencourt's avatar
Thomas Applencourt committed
62
{
63
64
    clinterval_callbacks_state* state = (clinterval_callbacks_state*)  clinterval_dispatch_iter->callbacks_state;
    return state->downstream_message_queue.empty();
Thomas Applencourt's avatar
Thomas Applencourt committed
65
66
}

67
size_t downstream_message_queue_size(struct clinterval_message_iterator *clinterval_dispatch_iter)
Thomas Applencourt's avatar
Thomas Applencourt committed
68
{
69
70
    clinterval_callbacks_state* state = (clinterval_callbacks_state*)  clinterval_dispatch_iter->callbacks_state;
    return state->downstream_message_queue.size();
Thomas Applencourt's avatar
Thomas Applencourt committed
71
72
}

73
const bt_message* downstream_message_queue_pop(struct clinterval_message_iterator *clinterval_dispatch_iter)
Thomas Applencourt's avatar
Thomas Applencourt committed
74
{
75
76
77
    clinterval_callbacks_state* state = (clinterval_callbacks_state*)  clinterval_dispatch_iter->callbacks_state;
    const bt_message* m = state->downstream_message_queue.front();
    state->downstream_message_queue.pop();
Thomas Applencourt's avatar
Thomas Applencourt committed
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
    return m;
}

<%#
  _                          
 /   _. | | |_   _.  _ |   _ 
 \_ (_| | | |_) (_| (_ |< _> 
                             
%>
// Some of the map used may grow arbitraly large
// we may do something about it at some point buy remove data we don't need anymore

<% $dbt_events.each do | dbt_event| %>
static void clinterval_<%= dbt_event.name %>_callback(
<%= dbt_event.callback_signature %>
){
  <%#                             ___      _    
       |   _   _  _. | o _|_       |  ._ _|_ _  
       |_ (_) (_ (_| | |  |_ \/   _|_ | | | (_) 
                             /                  
  %>
  <%if dbt_event.name_unsanitized.start_with?('lttng_ust_opencl:') %>
Thomas Applencourt's avatar
Thomas Applencourt committed
100
    const hostname_t   hostname{borrow_hostname(bt_evt)};
Thomas Applencourt's avatar
Thomas Applencourt committed
101
102
    const process_id_t process_id = borrow_process_id(bt_evt);
    const thread_id_t  thread_id  = borrow_thread_id(bt_evt);
103
104
    clinterval_callbacks_state* state = (clinterval_callbacks_state*) clinterval_iter_g->callbacks_state;
 <%end %>
Thomas Applencourt's avatar
Thomas Applencourt committed
105
106
107
108
109
110
111
112
113


  <%#        _ ___                 
        /\  |_) |     _  _. | |  _ 
       /--\ |  _|_   (_ (_| | | _> 
  %>                              
  <%if dbt_event.name_unsanitized.start_with?('lttng_ust_opencl:') %>
    int64_t ns_from_origin;
    bt_clock_snapshot_get_ns_from_origin(bt_clock, &ns_from_origin);
114
   
Thomas Applencourt's avatar
Thomas Applencourt committed
115
    <%if dbt_event.name.end_with?(START) %>
116
    state->host_start[hpt_function_name_t(hostname,process_id, thread_id, "<%= dbt_event.name_striped %>")] = ns_from_origin;
Thomas Applencourt's avatar
Thomas Applencourt committed
117
    <%elsif dbt_event.name.end_with?(STOP) %>
118
    const uint64_t start_g = state->host_start[hpt_function_name_t(hostname,process_id, thread_id, "<%= dbt_event.name_striped %>")];
Thomas Applencourt's avatar
Thomas Applencourt committed
119
120
121
122
123
124
125
      <% if dbt_event.fields.key?('errcode_ret_val') %>
    create_and_enqueue_host_message(hostname.c_str(), process_id, thread_id, "<%= dbt_event.name_striped %>", start_g, ns_from_origin-start_g, errcode_ret_val);
      <% else %>
    create_and_enqueue_host_message(hostname.c_str(), process_id, thread_id, "<%= dbt_event.name_striped %>", start_g, ns_from_origin-start_g, 0);
      <% end %>
    <% end %>
  <% end %>
126
  
Thomas Applencourt's avatar
Thomas Applencourt committed
127
128
129
130
131
132
133
134
  <%#                                                       
       |\/|  _  ._ _   _  ._      _.  _  _  _   _  _  _   _ 
       |  | (/_ | | | (_) | \/   (_| (_ (_ (/_ _> _> (/_ _> 
                            /                               
  %>

  <%# To do handle Alloc and cl_mem_host_ptr %>
  <%if dbt_event.name.include?("clEnqueue") and dbt_event.fields.key?("size") %>
135
  //   state->memory_trafic[hpt_function_name_t(hostname,process_id, thread_id, "<%= dbt_event.name_striped %>")].delta(size);
Thomas Applencourt's avatar
Thomas Applencourt committed
136
137
138
139
140
141
142
143
  <% end %>

  <%#  ___                                    __         _     
        | o ._ _   _   _ _|_  _. ._ _  ._    (_  |_  o _|_ _|_ 
        | | | | | (/_ _>  |_ (_| | | | |_)   __) | | |  |   |_ 
                                     |                       
  %>
  <%if dbt_event.name_unsanitized == "lttng_ust_opencl_devices:device_timer"  %>
Thomas Applencourt's avatar
Thomas Applencourt committed
144
      const hostname_t  hostname{borrow_hostname(bt_evt)};
Thomas Applencourt's avatar
Thomas Applencourt committed
145
    const process_id_t process_id = borrow_process_id(bt_evt);
146
147
    clinterval_callbacks_state* state = (clinterval_callbacks_state*) clinterval_iter_g->callbacks_state;

Thomas Applencourt's avatar
Thomas Applencourt committed
148
149
    int64_t ns_from_origin;
    bt_clock_snapshot_get_ns_from_origin(bt_clock, &ns_from_origin);
150
    state->device_ts_to_llng_ts[hp_device_t(hostname,process_id, (thapi_device_id) device)] = ns_from_origin - device_timestamp; 
Thomas Applencourt's avatar
Thomas Applencourt committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
  <% end %>
 
  <%#
    _         _                    _                                   __          _                  
   |_) ._ _ _|_ o | o ._   _  o   | \  _     o  _  _     _. ._   _|   (_      |_  | \  _     o  _  _  
   |   | (_) |  | | | | | (_| o   |_/ (/_ \/ | (_ (/_   (_| | | (_|   __) |_| |_) |_/ (/_ \/ | (_ (/_ 
                           _|                                                                         
   Map [ Tuple [ Hostnane, process, cl_command_queue ], Tuple [ device, subdevice] ]
  %>

  <%# Map [ Tuple [hostname, process, device_or_subdevice], device] ] %>
  <% if dbt_event.name_unsanitized == "lttng_ust_opencl:clGetDeviceIDs_#{STOP}" %>
    if (devices_vals != nullptr) {
        for (unsigned int i=0; i < num_devices_val; i++ ) {
             const thapi_device_id d = (thapi_device_id) devices_vals[i];
166
             state->device_to_rootdevice[hp_device_t(hostname,process_id, d) ] = d;
Thomas Applencourt's avatar
Thomas Applencourt committed
167
168
169
        }
    }
  <% elsif dbt_event.name_unsanitized == "lttng_ust_opencl:clCreateSubDevices_#{START}" %>
170
    state->start_device[hpt_t(hostname,process_id,thread_id) ] =  (thapi_device_id) in_device;
Thomas Applencourt's avatar
Thomas Applencourt committed
171
  <% elsif dbt_event.name_unsanitized == "lttng_ust_opencl:clCreateSubDevices_#{STOP}" %>
172
173
    const thapi_device_id device = state->start_device[hpt_t(hostname,process_id,thread_id) ];
    const thapi_device_id root_device = state->device_to_rootdevice[hp_device_t(hostname,process_id, device) ];
Thomas Applencourt's avatar
Thomas Applencourt committed
174
175
176
    if (out_devices_vals != nullptr) {
        for (unsigned int i=0; i < num_devices_ret_val; i++ ) {
            const thapi_device_id d = (thapi_device_id) out_devices_vals[i];
177
            state->device_to_rootdevice[hp_device_t(hostname,process_id, d) ] = root_device;
Thomas Applencourt's avatar
Thomas Applencourt committed
178
179
180
181
182
183
        }
    }
  <% end%>
 
  <%# Map [ Tuple [ Hostnane, process, cl_command_queue ], Tuple [ device, subdevice] ] %>
  <% if dbt_event.name.end_with?(START) and dbt_event.name.include?('clCreateCommandQueue') %>
184
    state->start_device[hpt_t(hostname,process_id, thread_id) ] =  (thapi_device_id) device;
Thomas Applencourt's avatar
Thomas Applencourt committed
185
  <%elsif dbt_event.name.end_with?(STOP) and dbt_event.name.include?('clCreateCommandQueue') %>
186
187
188
    const thapi_device_id device = state->start_device[hpt_t(hostname,process_id, thread_id) ];
    const thapi_device_id root_device = state->device_to_rootdevice[hp_device_t(hostname,process_id, device) ];
    state->command_queue_to_device[hp_command_queue_t(hostname,process_id,command_queue)] =   dsd_t(root_device, device) ; 
Thomas Applencourt's avatar
Thomas Applencourt committed
189
  <% elsif dbt_event.name_unsanitized == "lttng_ust_opencl_devices:device_name" %>
190
    clinterval_callbacks_state* state = (clinterval_callbacks_state*) clinterval_iter_g->callbacks_state;       
Thomas Applencourt's avatar
Thomas Applencourt committed
191
    const hostname_t hostname{ borrow_hostname(bt_evt)};
Thomas Applencourt's avatar
Thomas Applencourt committed
192
    const process_id_t process_id = borrow_process_id(bt_evt);
193
    state->device_to_name[hp_device_t(hostname,process_id,(thapi_device_id) device)] = hostname_t{name};
Thomas Applencourt's avatar
Thomas Applencourt committed
194
195
196
197
198
199
200
201
202
203
  <% end %>

  <%#
    _         _                    _
   |_) ._ _ _|_ o | o ._   _  o   |_    ._   _ _|_ o  _  ._    ._   _. ._ _   _
   |   | (_) |  | | | | | (_| o   | |_| | | (_  |_ | (_) | |   | | (_| | | | (/_
                           _|
   Map [ Tuple [ Hostnane, process, thread, function_name], Tuple [ device, subdevice] ]                   
  %>
  <% if dbt_event.name_unsanitized == "lttng_ust_opencl_arguments:kernel_info" %>
204
    clinterval_callbacks_state* state = (clinterval_callbacks_state*) clinterval_iter_g->callbacks_state;  
Thomas Applencourt's avatar
Thomas Applencourt committed
205
    const hostname_t hostname{borrow_hostname(bt_evt)};
Thomas Applencourt's avatar
Thomas Applencourt committed
206
    const process_id_t process_id = borrow_process_id(bt_evt);
207
    state->kernel_to_name[hp_kernel_t(hostname,process_id,kernel)] = hostname_t(function_name);
Thomas Applencourt's avatar
Thomas Applencourt committed
208
209
  <% elsif dbt_event.name.end_with?(START) and dbt_event.fields['command_queue'] %>
    <% if dbt_event.fields['kernel'] %>
210
    const thapi_function_name name{ state->kernel_to_name[hp_kernel_t(hostname,process_id,kernel)]};
Thomas Applencourt's avatar
Thomas Applencourt committed
211
212
213
    <% else %>
    constexpr char name[] = "<%= dbt_event.name_striped %>";
    <% end %>
214
215
    state->profiled_function_name_and_ts[hpt_t(hostname,process_id,thread_id)] = fn_ts_t(name,ns_from_origin);
    state->function_name_to_dsd[hpt_function_name_t(hostname,process_id,thread_id,name)] = state->command_queue_to_device[hp_command_queue_t(hostname,process_id,command_queue)];
Thomas Applencourt's avatar
Thomas Applencourt committed
216
217
218
219
220
221
222
223
224
225
226
  <% end %>
  
  <%#
    _         _                    _                        ___               
   |_) ._ _ _|_ o | o ._   _  o   |_ |  _. ._   _  _   _|    | o ._ _   _   _ 
   |   | (_) |  | | | | | (_| o   |_ | (_| |_) _> (/_ (_|    | | | | | (/_ _> 
                           _|              |                                  
   Map [ Tuple[hostname, process, device, subdevice, cl_function_name], elapsed_time ]
  %>

  <% if dbt_event.name_unsanitized == "lttng_ust_opencl_profiling:event_profiling" %>
227
    clinterval_callbacks_state* state = (clinterval_callbacks_state*) clinterval_iter_g->callbacks_state;  
Thomas Applencourt's avatar
Thomas Applencourt committed
228
    const hostname_t hostname{borrow_hostname(bt_evt)};
Thomas Applencourt's avatar
Thomas Applencourt committed
229
230
231
    const process_id_t process_id = borrow_process_id(bt_evt);
    const thread_id_t thread_id = borrow_thread_id(bt_evt);
    const hp_event_t hp_event{hostname,process_id, event};
232
    const auto [function_name, ts] = state->profiled_function_name_and_ts[hpt_t(hostname,process_id,thread_id)];
Thomas Applencourt's avatar
Thomas Applencourt committed
233

234
235
    if (!state->event_to_function_name_and_ts.count(hp_event)){
        state->event_to_function_name_and_ts[hp_event] = tfn_ts_t(thread_id, function_name,ts);
Thomas Applencourt's avatar
Thomas Applencourt committed
236
    } else {
237
238
        const auto [start,delta] = state->event_result_to_start_and_delta[hp_event];
        const auto [device,subdevice] = state->function_name_to_dsd[hpt_function_name_t(hostname,process_id,thread_id, function_name)];
Thomas Applencourt's avatar
Thomas Applencourt committed
239
240
241
        if (delta != 0)
            create_and_enqueue_device_message(hostname.c_str(),process_id,thread_id,device,subdevice,function_name.c_str(),start,delta);

242
        state->event_to_function_name_and_ts.erase(hp_event);
Thomas Applencourt's avatar
Thomas Applencourt committed
243
244
    }
  <% elsif dbt_event.name_unsanitized == "lttng_ust_opencl_profiling:event_profiling_results" %>
245
    clinterval_callbacks_state* state = (clinterval_callbacks_state*) clinterval_iter_g->callbacks_state; 
Thomas Applencourt's avatar
Thomas Applencourt committed
246
    const hostname_t hostname{borrow_hostname(bt_evt)};
Thomas Applencourt's avatar
Thomas Applencourt committed
247
248
249
250
251
252
253
254
255
256
257
258
    const process_id_t process_id = borrow_process_id(bt_evt);
    const hp_event_t hp_event{hostname,process_id, event};
    
    //Should we check that end > start?
    uint64_t delta = end - start;
    if (queued_status != 0 or submit_status != 0 or start_status != 0) {
        std::cerr << "Warning: 'lttng_ust_opencl_profiling:event_profiling_results' for event " 
                  << std::hex << std::showbase << std::internal << std::setfill('0') << event  
                  << " returned invalid status" << std::endl;
        delta = 0;
    }

259
260
261
    if (state->event_to_function_name_and_ts.count(hp_event)) {
        const auto [thread_id,function_name,ts] = state->event_to_function_name_and_ts[hp_event];
        const auto [device,subdevice] = state->function_name_to_dsd[hpt_function_name_t(hostname,process_id,thread_id, function_name)];
Thomas Applencourt's avatar
Thomas Applencourt committed
262
263
       
        const hp_device_t hp_device{hostname, process_id, device};
264
265
266
        //const uint64_t start_event = state->device_ts_to_llng_ts.count(hp_device) ? state->device_ts_to_llng_ts[hp_device] + start:  ts + (start - queued);
        const uint64_t start_event = ts + (start - queued);

Thomas Applencourt's avatar
Thomas Applencourt committed
267
268
269
270
        // Check for error 
        if (delta != 0)
             create_and_enqueue_device_message(hostname.c_str(),process_id,thread_id,device, subdevice, function_name.c_str(),start_event,delta);

271
        state->event_to_function_name_and_ts.erase(hp_event);
Thomas Applencourt's avatar
Thomas Applencourt committed
272
    } else {
273
274
275
        const thread_id_t thread_id = borrow_thread_id(bt_evt);
        const auto [function_name, ts] = state->profiled_function_name_and_ts[hpt_t(hostname,process_id,thread_id)];
        const auto [device,subdevice] = state->function_name_to_dsd[hpt_function_name_t(hostname,process_id,thread_id, function_name)];
Thomas Applencourt's avatar
Thomas Applencourt committed
276
277

        const hp_device_t hp_device{hostname, process_id, device};
278
279
280
        //const uint64_t start_event = state->device_ts_to_llng_ts.count(hp_device) ? state->device_ts_to_llng_ts[hp_device] + start:  ts + (start - queued);
        const uint64_t start_event = ts + (start - queued);

281
282
        state->event_result_to_start_and_delta[hp_event] = sd_t(start_event,delta);
        state->event_to_function_name_and_ts[hp_event] = tfn_ts_t(thread_id, function_name,ts);
Thomas Applencourt's avatar
Thomas Applencourt committed
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
    }
  <% end %>
}
<% end %>

<%# 
 ___                            
  |  ._  o _|_ o  _. | o _   _  
 _|_ | | |  |_ | (_| | | /_ (/_ 
                                
%>

void init_clinterval_callbacks(struct clinterval_dispatch  *opencl_interval) {
<% $dbt_events.each do | dbt_event| %>
    clinterval_register_callback(opencl_interval, "<%= dbt_event.name_unsanitized %>", (void *) &clinterval_<%= dbt_event.name %>_callback);
<% end %>
}