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

c = Babeltrace::Context::new
traces = c.add_traces(path: ARGV[0])
10
raise "Trace not found: #{ARGV[0].inspect}, check path." if traces == []
Brice Videau's avatar
Brice Videau committed
11
12
13

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")}"

14
opencl_model = YAML::load_file(File.join(DATADIR,"opencl_model.yaml"))
15
16
17
SUFFIXES = opencl_model["suffixes"]
START = SUFFIXES["start"]
STOP = SUFFIXES["stop"]
18
infos = YAML::load_file(File.join(DATADIR,"opencl_infos.yaml"))
Brice Videau's avatar
Brice Videau committed
19
20
21
22
23
24
25
26
27
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
28
      default = self.const_get(:DEFAULT)
Brice Videau's avatar
Brice Videau committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
      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
50
    "[#{res.join(", ")}]"
Brice Videau's avatar
Brice Videau committed
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
  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
97
98
    else
      self.const_set(:DEFAULT, nil)
Brice Videau's avatar
Brice Videau committed
99
100
101
    end
    if special
      self.const_set(:SPECIAL, special)
Brice Videau's avatar
Brice Videau committed
102
103
    else
      self.const_set(:SPECIAL, nil)
Brice Videau's avatar
Brice Videau committed
104
105
106
107
108
109
110
111
112
113
114
    end
    self.const_set(:FLAGS, hash)
  }
  bitfields_by_type[k] = klass
}

infos_type = {}

type_map = {
  "unsigned int" => "L",
  "int" => "l",
115
116
  "intptr_t" => "j",
  "uintptr_t" => "J",
Brice Videau's avatar
Brice Videau committed
117
  "size_t" => "Q",
118
  "cl_bool" => "l",
Brice Videau's avatar
Brice Videau committed
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
  "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)|
178
    next if !v
Brice Videau's avatar
Brice Videau committed
179
180
181
182
183
184
185
186
    src = <<EOF
lambda { |defi|
  {
EOF
  src << "  "
  src << v.collect { |name, desc|
    expr =
      if name == "param_value_vals"
187
        startn = n.gsub("_#{STOP}", "_#{START}")
Brice Videau's avatar
Brice Videau committed
188
        type = opencl_model["events"][startn]["param_name"]["type"]
189
        if n == "lttng_ust_opencl:clSetKernelExecInfo_#{START}"
190
          "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"
191
        else
192
          "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"
193
        end
Brice Videau's avatar
Brice Videau committed
194
195
196
197
198
199
      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"] )
200
          "\"[\#{defi[\"#{name}\"].collect { |v| \"0x\#{v.to_s(16)}\" }.join(\", \")}]\""
Brice Videau's avatar
Brice Videau committed
201
        else
202
          "defi[\"#{name}\"].inspect"
Brice Videau's avatar
Brice Videau committed
203
        end
Brice Videau's avatar
Brice Videau committed
204
205
      elsif desc["string"]
        "defi[\"#{name}\"]"
Brice Videau's avatar
Brice Videau committed
206
207
208
209
210
211
212
213
      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
214
          "defi[\"#{name}\"].inspect"
Brice Videau's avatar
Brice Videau committed
215
216
217
218
219
220
        end
      end
    "\"#{name}\" => (#{expr})"
  }.join(",\n  ")
  src << "\n"
    src << <<EOF
221
  }.collect { |k,v| "\#{k}: \#{v}"}.join(", ")
Brice Videau's avatar
Brice Videau committed
222
223
224
225
226
227
228
229
230
231
232
233
}
EOF
    eval(src)
  }
}

it = c.iter_create

it.each { |ev|
  name, descs = events[ev.handle_id][ev.id]
  print "#{ev.timestamp.strftime("%H:%M:%S.%9L")} - #{name}: "
  defi = ev.top_level_scope(:EVENT_FIELDS)
234
235
236
237
238
239
  l = event_lambdas[ev.handle_id][ev.id]
  if l
    puts l.call(defi.value)
  else
    puts defi.value
  end
Brice Videau's avatar
Brice Videau committed
240
241
}