gen_babeltrace_cuda_lib.rb 5.68 KB
Newer Older
Brice Videau's avatar
WIP.  
Brice Videau committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
require_relative 'gen_cuda_library_base.rb'

meta_parameter_lambda = lambda { |m, dir|
  if dir == :start
    lttng = m.lttng_in_type
  else
    lttng = m.lttng_out_type
  end
  name = lttng.name
  t = m.command[m.name].type.type
  case m
  when ScalarMetaParameter
    case t
    when YAMLCAst::Pointer
      "s << \"#{name}: \#{\"0x%016x\" % defi[\"#{name}\"]}\""
    when YAMLCAst::CustomType
      if $objects.include?(t.name)
        "s << \"#{name}: \#{\"0x%016x\" % defi[\"#{name}\"]}\""
19
20
      elsif t.name == "CUdeviceptr"
        "s << \"#{name}: \#{\"0x%016x\" % defi[\"#{name}\"]}\""
Brice Videau's avatar
WIP.  
Brice Videau committed
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
      elsif $all_enum_names.include?(t.name)
        "s << \"#{name}: \#{CUDA::#{to_class_name(t.name)}.from_native(defi[\"#{name}\"], nil)}\""
      elsif $all_bitfield_names.include?(t.name)
        "s << \"#{name}: [ \#{CUDA::#{to_class_name(t.name)}.from_native(defi[\"#{name}\"], nil).join(\", \")} ]\""
      elsif $all_struct_names.include?(t.name)
        "s << \"#{name}: \#{defi[\"#{name}\"].size > 0 ? CUDA::#{to_class_name(t.name)}.new(FFI::MemoryPointer.from_string(defi[\"#{name}\"])) : nil}\""
      else
        "s << \"#{name}: \#{defi[\"#{name}\"]}\""
      end
    else
      "s << \"#{name}: \#{defi[\"#{name}\"]}\""
    end
  when ArrayMetaParameter
    case t
    when YAMLCAst::Pointer
      "s << \"#{name}: [ \#{defi[\"#{name}\"].collect { |v| \"0x%016x\" % v }.join(\", \")} ]\""
    when YAMLCAst::CustomType
      if $objects.include?(t.name)
        "s << \"#{name}: [ \#{defi[\"#{name}\"].collect { |v| \"0x%016x\" % v }.join(\", \")} ]\""
      elsif $all_struct_names.include?(t.name)
        "s << \"#{name}: [ \#{p = FFI::MemoryPointer.from_string(defi[\"#{name}\"]); sz = CUDA::#{to_class_name(t.name)}.size; n = p.size / sz; (0...n).collect { |i| CUDA::#{to_class_name(t.name)}.new(p + i*sz).to_s}.join(\", \")} ]\""
      else
        "s << \"#{name}: \#{defi[\"#{name}\"]}\""
      end
    else
      "s << \"#{name}: \#{defi[\"#{name}\"]}\""
    end
  else
    "s << \"#{name}: \#{defi[\"#{name}\"]}\""
  end
}

gen_event_lambda = lambda { |provider, c, dir|
  puts <<EOF
55
$event_lambdas["#{provider}:#{c.name}_#{SUFFIXES[dir]}"] = lambda { |defi|
Brice Videau's avatar
WIP.  
Brice Videau committed
56
57
58
59
60
  s = "{ "
EOF
  fields = []
  if dir == :start
    if c.parameters
61
      fields += c.parameters.collect { |p|
Brice Videau's avatar
WIP.  
Brice Videau committed
62
63
64
65
66
67
        case p.type
        when YAMLCAst::Pointer
          "s << \"#{p.name}: \#{\"0x%016x\" % defi[\"#{p.name}\"]}\""
        when YAMLCAst::CustomType
          if $objects.include?(p.type.name)
            "s << \"#{p.name}: \#{\"0x%016x\" % defi[\"#{p.name}\"]}\""
Brice Videau's avatar
Brice Videau committed
68
69
          elsif p.type.name == "CUdeviceptr"
            "s << \"#{p.name}: \#{\"0x%016x\" % defi[\"#{p.name}\"]}\""
Brice Videau's avatar
WIP.  
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
          elsif $all_enum_names.include?(p.type.name)
            "s << \"#{p.name}: \#{CUDA::#{to_class_name(p.type.name)}.from_native(defi[\"#{p.name}\"], nil)}\""
          elsif $all_bitfield_names.include?(p.type.name)
            "s << \"#{p.name}: [ \#{CUDA::#{to_class_name(p.type.name)}.from_native(defi[\"#{p.name}\"], nil).join(\", \")} ]\""
          else
            "s << \"#{p.name}: \#{defi[\"#{p.name}\"]}\""
          end
        else
          "s << \"#{p.name}: \#{defi[\"#{p.name}\"]}\""
        end
      }
      fields += c.meta_parameters.select { |m| m.kind_of?(In) }.collect { |m|
        meta_parameter_lambda.call(m, :start)
      }
    end
  else
    fields.push "s << \"#{RESULT_NAME}: \#{CUDA::CUResult.from_native(defi[\"#{RESULT_NAME}\"], nil)}\""
    fields += c.meta_parameters.select { |m| m.kind_of?(Out) }.collect { |m|
        meta_parameter_lambda.call(m, :stop)
      }
  end
  puts <<EOF
  #{fields.join("\n  s << ', '\n  ")}
EOF
  puts <<EOF
  s << " }"
}
EOF
}

puts <<EOF
require_relative 'cuda_library.rb'

$event_lambdas = {}
EOF

provider = :lttng_ust_cuda

$cuda_commands.each { |c|
  gen_event_lambda.call(provider, c, :start)
  gen_event_lambda.call(provider, c, :stop)
}

Brice Videau's avatar
Brice Videau committed
113
extra_events = YAML::load_file(File.join(SRC_DIR,"cuda_events.yaml"))
Brice Videau's avatar
WIP.  
Brice Videau committed
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

extra_events.each { |provider, h|
  h["events"].each { |e|
    puts <<EOF
$event_lambdas["#{provider}:#{e["name"]}"] = lambda { |defi|
  s = "{ "
EOF
    fields = e["fields"].collect { |f|
      field = LTTng::TracepointField::new(*f)
      case field.macro
      when :ctf_integer_hex
        "s << \"#{field.name}: \#{\"0x%016x\" % defi[\"#{field.name}\"]}\""
      when :ctf_integer
        arg = e["args"].find { |type, name|
          name == field.expression
        }
        if arg
          if $all_enum_names.include?(arg[0])
            "s << \"#{field.name}: \#{CUDA::#{to_class_name(arg[0])}.from_native(defi[\"#{field.name}\"], nil)}\""
          elsif $all_bitfield_names.include?(arg[0])
            "s << \"#{field.name}: [ \#{CUDA::#{to_class_name(arg[0])}.from_native(defi[\"#{field.name}\"], nil)} ]\""
          else
            "s << \"#{field.name}: \#{defi[\"#{field.name}\"]}\""
          end
        else
          "s << \"#{field.name}: \#{defi[\"#{field.name}\"]}\""
        end
Brice Videau's avatar
Brice Videau committed
141
142
      when :ctf_float
        "s << \"#{field.name}: \#{defi[\"#{field.name}\"]}\""
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
      when :ctf_sequence_text
        arg = e["args"].find { |type, name|
          name == field.expression
        }
        name = field.name
        if arg
          t = arg[0].sub("*","").strip
          if $all_struct_names.include?(t)
            "s << \"#{name}: \#{defi[\"#{name}\"].size > 0 ? CUDA::#{to_class_name(t)}.new(FFI::MemoryPointer.from_string(defi[\"#{name}\"])) : nil}\""
          else
            "s << \"#{name}: \#{defi[\"#{name}\"].inspect}\""
          end
        else
          "s << \"#{name}: \#{defi[\"#{name}\"].inspect}\""
        end
Brice Videau's avatar
WIP.  
Brice Videau committed
158
159
160
161
162
163
164
165
166
167
168
169
170
      else
        raise "Unsupported LTTng macro #{field.macro}!"
      end
    }
    puts <<EOF
  #{fields.join("\n  s << ', '\n  ")}
EOF
    puts <<EOF
  s << " }"
}
EOF
  }
}