gen_babeltrace_cl_dispatchers.rb 4.55 KB
Newer Older
1
2
3
require 'yaml'
opencl_model = YAML::load_file("opencl_model.yaml")

Thomas Applencourt's avatar
Thomas Applencourt committed
4
5
6
7
8
9
if ARGV.empty?
  namespace = "babeltrace_cl"
else
  namespace = ARGV[0]
end

10
puts <<EOF
Thomas Applencourt's avatar
Thomas Applencourt committed
11
12
#include "#{namespace}.h"
#include "#{namespace}_callbacks.h"
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

EOF

def print_field_member_access(lttng, type, name, i)
  puts <<EOF
  {
    const bt_field *_field = NULL;
    _field = bt_field_structure_borrow_member_field_by_index_const(payload_field, #{i});
EOF
  case lttng
  when "ctf_integer", "ctf_integer_hex", "ctf_enum"
    puts <<EOF
    bt_field_class_type _type = bt_field_get_class_type(_field);
    if (bt_field_class_type_is(_type, BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER))
      #{name} = (#{type})bt_field_integer_unsigned_get_value(_field);
    else
      #{name} = (#{type})bt_field_integer_signed_get_value(_field);
EOF
Brice Videau's avatar
Brice Videau committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  when "ctf_array"
    scalar_type = type.gsub("const","").sub("*", "")
    puts <<EOF
    _#{name}_length = bt_field_array_get_length(_field);
    if (_#{name}_length > 0) {
      bt_field_class_type _type = bt_field_get_class_type(bt_field_array_borrow_element_field_by_index_const(_field, 0));
      #{name} = (#{type})malloc(_#{name}_length*sizeof(#{scalar_type}));
      for (uint64_t _i = 0; _i < _#{name}_length; _i++) {
        if (bt_field_class_type_is(_type, BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER))
          #{name}[_i] = (#{scalar_type})bt_field_integer_unsigned_get_value(bt_field_array_borrow_element_field_by_index_const(_field, _i));
        else
          #{name}[_i] = (#{scalar_type})bt_field_integer_signed_get_value(bt_field_array_borrow_element_field_by_index_const(_field, _i));
      }
    } else
      #{name} = NULL;
EOF
  when "ctf_sequence", "ctf_sequence_hex"
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
    scalar_type = type.gsub("const","").sub("*", "")
    puts <<EOF
    uint64_t _sz = bt_field_array_get_length(_field);
    if (_sz > 0) {
      bt_field_class_type _type = bt_field_get_class_type(bt_field_array_borrow_element_field_by_index_const(_field, 0));
      #{name} = (#{type})malloc(_sz*sizeof(#{scalar_type}));
      for (uint64_t _i = 0; _i < _sz; _i++) {
        if (bt_field_class_type_is(_type, BT_FIELD_CLASS_TYPE_UNSIGNED_INTEGER))
          #{name}[_i] = (#{scalar_type})bt_field_integer_unsigned_get_value(bt_field_array_borrow_element_field_by_index_const(_field, _i));
        else
          #{name}[_i] = (#{scalar_type})bt_field_integer_signed_get_value(bt_field_array_borrow_element_field_by_index_const(_field, _i));
      }
    } else
      #{name} = NULL;
EOF
  when "ctf_sequence_text", "ctf_string"
    puts <<EOF
    #{name} = (#{type})bt_field_string_get_value(_field);
EOF
  end
  puts <<EOF
  }
EOF
end

def print_field_members_access(fields)
  arr = []
  fields.each.collect { |n, f|
    lttng = f["lttng"]
    type = "#{f["type"].gsub("cl_errcode", "cl_int")}"
    type << " *"  if f["pointer"]
    type << " *"  if f["array"]
    type << " *"  if f["string"]
    name = n
Brice Videau's avatar
Brice Videau committed
82
    if f["array"] and f["lttng"].match("ctf_sequence")
83
84
85
86
87
      arr.push ["ctf_integer", "size_t", "_#{n}_length"]
    end
    arr.push [lttng, type, name]
  }
  puts <<EOF unless arr.empty?
Brice Videau's avatar
Brice Videau committed
88
  const bt_field *payload_field = bt_event_borrow_payload_field_const(bt_evt);
89
90
91
92
93
94
95
96
EOF
  arr.each_with_index { |(lttng, type, name), i|
    print_field_member_access(lttng, type, name, i)
  }
end

opencl_model["events"].each { |name, fields|
  puts <<EOF
Brice Videau's avatar
Brice Videau committed
97
98
static void
#{name.gsub(":","_")}_dispatcher(
Thomas Applencourt's avatar
Thomas Applencourt committed
99
100
    struct #{namespace}_dispatch  *dispatch,
    struct #{namespace}_callbacks *callbacks,
Brice Videau's avatar
Brice Videau committed
101
    const bt_event          *bt_evt,
Brice Videau's avatar
Brice Videau committed
102
    const bt_clock_snapshot *bt_clock) {
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  #{fields.each.collect { |n, f|
  s =  "#{f["type"].gsub("cl_errcode", "cl_int")}"
  s << " *" if f["pointer"]
  s << " *" if f["array"]
  s << " *" if f["string"]
  s << " #{n}"
  if f["array"]
    ["size_t _#{n}_length", s]
  else
    s
  end
}.flatten.join(";\n  ")};
EOF
print_field_members_access(fields)
puts <<EOF
Brice Videau's avatar
Bugfix.    
Brice Videau committed
118
  void **p = NULL;
119
  while( (p = utarray_next(callbacks->callbacks, p)) ) {
Thomas Applencourt's avatar
Thomas Applencourt committed
120
    ((#{namespace}_#{name.gsub(":","_")}_cb *)*p)(
Brice Videau's avatar
Brice Videau committed
121
      #{(["bt_evt", "bt_clock"] + fields.each.collect { |n, f|
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
        s = "#{n}"
        if f["array"]
          ["_#{n}_length", s]
        else
          s
        end
      }).flatten.join(",\n      ")});
  }
  #{fields.each.collect { |n, f|
    if f["array"] && f["lttng"] != "ctf_sequence_text"
      "if (#{n}) free((void *)#{n})"
    else
      nil
    end
  }.compact.join(";\n  ")};
}

EOF
}

puts <<EOF
Thomas Applencourt's avatar
Thomas Applencourt committed
143
void init_#{namespace}_dispatcher(struct #{namespace}_dispatch *dispatch) {
144
145
146
EOF
opencl_model["events"].each_key { |name|
  puts <<EOF
Thomas Applencourt's avatar
Thomas Applencourt committed
147
  #{namespace}_register_dispatcher(dispatch, "#{name}", &#{name.gsub(":","_")}_dispatcher);
148
149
150
151
152
EOF
}
puts <<EOF
}
EOF