babeltrace_opencl.in 6.69 KB
Newer Older
Brice Videau's avatar
Brice Videau committed
1
#!/usr/bin/env ruby
2
DATADIR = File.join("@prefix@", "share")
3
require 'optparse'
Brice Videau's avatar
Brice Videau committed
4
5
6
7
8
require 'babeltrace'
require 'babeltrace/ctf'
require 'yaml'
require 'pp'

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$options = {
  context: false
}

OptionParser.new do |opts|
  opts.banner = "Usage: babeltrace_opencl [OPTIONS] target_trace_directory"

  opts.on("-c", "--[no-]context", "Add context information") do |context|
    $options[:context] = context
  end

  opts.on("-h", "--help", "Prints this help") do
    puts opts
    exit
  end

end.parse!

Brice Videau's avatar
Brice Videau committed
27
28
c = Babeltrace::Context::new
traces = c.add_traces(path: ARGV[0])
29
raise "Trace not found: #{ARGV[0].inspect}, check path." if traces == []
Brice Videau's avatar
Brice Videau committed
30
31
32

puts "#{c.get_timestamp_begin.strftime("%Y-%m-%d %H:%M:%S.%9L %z")} -- #{c.get_timestamp_end.strftime("%Y-%m-%d %H:%M:%S.%9L %z")}"

33
opencl_model = YAML::load_file(File.join(DATADIR,"opencl_model.yaml"))
34
35
36
SUFFIXES = opencl_model["suffixes"]
START = SUFFIXES["start"]
STOP = SUFFIXES["stop"]
37
infos = YAML::load_file(File.join(DATADIR,"opencl_infos.yaml"))
Brice Videau's avatar
Brice Videau committed
38
39
40
41
42
43
44
45
46
enums_by_type = {}
enums_by_value = {}
enums_by_name = {}
bitfields_by_type = {}

class Bitfield
  def self.to_s(v)
    res = []
    if v == 0
Brice Videau's avatar
Brice Videau committed
47
      default = self.const_get(:DEFAULT)
Brice Videau's avatar
Brice Videau committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
      res.push default if default
    else
      s = self.const_get(:SPECIAL)
      if s
        n, s = s
        if v & s == s
          res.push n
          v ^= s
        end
      end
      self.const_get(:FLAGS).each { |n, f|
        if f & v == f
          res.push(n)
          v ^= f
        end
      }
      if v != 0
        res.push(v)
      end
    end
    res
69
    "[#{res.join(", ")}]"
Brice Videau's avatar
Brice Videau committed
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
  end
end

enums = opencl_model["enums"].collect { |k, v|
  hash = {}
  v["values"].each { |n, str|
    int =
      begin
        eval(str)
      rescue
        nil
      end
    hash[int] = n if int
  }
  k = v["type_name"] if v["type_name"]
  enums_by_type[k] = hash
  enums_by_value[k] = hash.invert
  enums_by_name[v["trace_name"]] = hash if v["trace_name"]
}

bifields = opencl_model["bitfields"].collect { |k, v|
  hash = {}
  default = nil
  special = nil
  v["values"].each { |n, str|
    int =
      begin
        eval(str)
      rescue
        nil
      end
    if int
      if int > 0
        if int.to_s(2).count("1") > 1
          special = [n, int]
        else
          hash[n] = int
        end
      else
        default = n
      end
    end
  }
  klass = Class::new(Bitfield) {
    if default
      self.const_set(:DEFAULT, default)
Brice Videau's avatar
Brice Videau committed
116
117
    else
      self.const_set(:DEFAULT, nil)
Brice Videau's avatar
Brice Videau committed
118
119
120
    end
    if special
      self.const_set(:SPECIAL, special)
Brice Videau's avatar
Brice Videau committed
121
122
    else
      self.const_set(:SPECIAL, nil)
Brice Videau's avatar
Brice Videau committed
123
124
125
126
127
128
129
130
131
132
133
    end
    self.const_set(:FLAGS, hash)
  }
  bitfields_by_type[k] = klass
}

infos_type = {}

type_map = {
  "unsigned int" => "L",
  "int" => "l",
134
135
  "intptr_t" => "j",
  "uintptr_t" => "J",
Brice Videau's avatar
Brice Videau committed
136
  "size_t" => "Q",
137
  "cl_bool" => "l",
Brice Videau's avatar
Brice Videau committed
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
  "cl_int" => "l",
  "cl_uint" => "L",
  "cl_long" => "q",
  "cl_ulong" => "Q",
  "cl_short" => "s",
  "cl_ushort" => "S",
  "cl_char" => "c",
  "cl_uchar" => "C",
  "cl_half" => "S",
  "cl_float" => "f",
  "cl_double" => "d"
}
opencl_model["objects"].each { |n|
  type_map[n] = "J"
}
enums_by_type.keys.each { |n|
  type_map[n] = "l"
}
bitfields_by_type.keys.each { |n|
  type_map[n] = "Q"
}

infos.each { |inf, vers|
  infos_type[inf] = {}
  vers.each { |vername, list|
    list.each { |i|
      type =
        if i["array"]
          if i["pointer"]
            ["J*", true]
          elsif type_map[i["type"]]
            [type_map[i["type"]] + "*", true]
          else
            nil
          end
        elsif i["string"]
          ["Z*", false]
        elsif i["pointer"]
          ["J", false]
        elsif type_map[i["type"]]
          [type_map[i["type"]], false]
        else
          nil
        end
      infos_type[inf][enums_by_value[inf][i["name"]]] = type
    }
  }
}

events = {}
traces.each { |t|
  events[t.handle_id] = t.get_event_decl_list.collect(&:name).collect { |n|
    [n, opencl_model["events"][n]]
  }
}

event_lambdas = {}
events.each { |handle, arr|
  event_lambdas[handle] = arr.collect { |(n, v)|
197
    next if !v
Brice Videau's avatar
Brice Videau committed
198
199
200
201
202
203
204
205
    src = <<EOF
lambda { |defi|
  {
EOF
  src << "  "
  src << v.collect { |name, desc|
    expr =
      if name == "param_value_vals"
206
        startn = n.gsub("_#{STOP}", "_#{START}")
Brice Videau's avatar
Brice Videau committed
207
        type = opencl_model["events"][startn]["param_name"]["type"]
208
        if n == "lttng_ust_opencl:clSetKernelExecInfo_#{START}"
209
          "begin if defi[\"#{name}\"].size > 0 && infos_type[\"#{type}\"] then i = infos_type[\"#{type}\"][defi[\"param_name\"]]; if i then v = defi[\"#{name}\"].unpack(i[0]); v.collect! { |j| '0x' << j.to_s(16) } if i[0].match(\"J\"); v = v.first unless i[1]; v = \"[ \#{v.join(\", \")} ]\" if v.kind_of?(Array); v; else defi[\"#{name}\"]; end; else defi[\"#{name}\"]; end; end"
210
        else
211
          "begin if defi[\"#{name}\"].size > 0 && infos_type[\"#{type}\"] then i = infos_type[\"#{type}\"][defi[\"_param_name\"]]; if i then v = defi[\"#{name}\"].unpack(i[0]); v.collect! { |j| '0x' << j.to_s(16) } if i[0].match(\"J\"); v = v.first unless i[1]; v = \"[ \#{v.join(\", \")} ]\" if v.kind_of?(Array); v; else defi[\"#{name}\"]; end; else defi[\"#{name}\"]; end; end"
212
        end
Brice Videau's avatar
Brice Videau committed
213
214
215
216
217
218
      elsif desc["array"]
        if enums_by_type[desc["type"]]
          "defi[\"#{name}\"].collect { |v| (tmp = enums_by_type[\"#{desc["type"]}\"][v]) ? tmp : v }"
        elsif bitfields_by_type[desc["type"]]
          "defi[\"#{name}\"].collect { |v| bitfields_by_type[\"#{desc["type"]}\"].to_s(v) }"
        elsif desc["pointer"] || opencl_model["objects"].include?( desc["type"] )
219
          "\"[\#{defi[\"#{name}\"].collect { |v| \"0x\#{v.to_s(16)}\" }.join(\", \")}]\""
Brice Videau's avatar
Brice Videau committed
220
        else
221
          "defi[\"#{name}\"].inspect"
Brice Videau's avatar
Brice Videau committed
222
        end
Brice Videau's avatar
Brice Videau committed
223
224
      elsif desc["string"]
        "defi[\"#{name}\"]"
Brice Videau's avatar
Brice Videau committed
225
226
227
228
229
230
231
232
      else
        if enums_by_type[desc["type"]]
          "(tmp = enums_by_type[\"#{desc["type"]}\"][defi[\"#{name}\"]]) ? tmp : defi[\"#{name}\"]"
        elsif bitfields_by_type[desc["type"]]
          "bitfields_by_type[\"#{desc["type"]}\"].to_s(defi[\"#{name}\"])"
        elsif desc["pointer"] || opencl_model["objects"].include?( desc["type"] )
          "\"0x\#{defi[\"#{name}\"].to_s(16)}\""
        else
233
          "defi[\"#{name}\"].inspect"
Brice Videau's avatar
Brice Videau committed
234
235
236
237
238
239
        end
      end
    "\"#{name}\" => (#{expr})"
  }.join(",\n  ")
  src << "\n"
    src << <<EOF
240
  }.collect { |k,v| "\#{k}: \#{v}"}.join(", ")
Brice Videau's avatar
Brice Videau committed
241
242
243
244
245
246
247
248
249
250
}
EOF
    eval(src)
  }
}

it = c.iter_create

it.each { |ev|
  name, descs = events[ev.handle_id][ev.id]
251
252
253
  str = "#{ev.timestamp.strftime("%H:%M:%S.%9L")}"
  str << " - " << ev.top_level_scope(:STREAM_EVENT_CONTEXT).value.collect { |k, v| "#{k}: #{v}" }.join(", ") if $options[:context]
  str << " - #{name}: "
Brice Videau's avatar
Brice Videau committed
254
  defi = ev.top_level_scope(:EVENT_FIELDS)
255
256
  l = event_lambdas[ev.handle_id][ev.id]
  if l
257
    str << l.call(defi.value).to_s
258
  else
259
    str << defi.value.to_s
260
  end
261
  puts str
Brice Videau's avatar
Brice Videau committed
262
263
}