gen_babeltrace_cl_dispatchers.rb 3.84 KB
Newer Older
1
require 'yaml'
2
opencl_babeltrace_model = YAML::load_file("opencl_babeltrace_model.yaml")
3
4
5

puts <<EOF
#include "babeltrace_cl.h"
Brice Videau's avatar
Brice Videau committed
6
#include "babeltrace_cl_callbacks.h"
7
8
9

EOF

10
11
12
13
def print_field_member_access(f, i)
  klass = f[:class]
  name = f[:name]
  type = f[:cast_type]
14
15
16
17
18
  puts <<EOF
  {
    const bt_field *_field = NULL;
    _field = bt_field_structure_borrow_member_field_by_index_const(payload_field, #{i});
EOF
19
20
  case klass
  when 'signed', 'enumeration_signed'
21
    puts <<EOF
22
23
24
25
26
    #{name} = (#{type})bt_field_integer_signed_get_value(_field);
EOF
  when 'unsigned', 'enumeration_unsigned'
    puts <<EOF
    #{name} = (#{type})bt_field_integer_unsigned_get_value(_field);
27
EOF
28
  when 'array_static'
Brice Videau's avatar
Brice Videau committed
29
30
31
32
33
34
    scalar_type = type.gsub("const","").sub("*", "")
    puts <<EOF
    _#{name}_length = bt_field_array_get_length(_field);
    if (_#{name}_length > 0) {
      #{name} = (#{type})malloc(_#{name}_length*sizeof(#{scalar_type}));
      for (uint64_t _i = 0; _i < _#{name}_length; _i++) {
35
36
37
38
39
40
41
42
43
44
45
46
47
48
EOF
    case f[:field][:class]
    when 'unsigned'
      puts <<EOF
        #{name}[_i] = (#{scalar_type})bt_field_integer_unsigned_get_value(bt_field_array_borrow_element_field_by_index_const(_field, _i));
EOF
    when 'signed'
      puts <<EOF
        #{name}[_i] = (#{scalar_type})bt_field_integer_signed_get_value(bt_field_array_borrow_element_field_by_index_const(_field, _i));
EOF
    else
      raise "unsupported array element #{f[:field][:class]}"
    end
    puts <<EOF
Brice Videau's avatar
Brice Videau committed
49
50
51
52
      }
    } else
      #{name} = NULL;
EOF
53
  when 'array_dynamic'
54
55
56
57
58
59
    scalar_type = type.gsub("const","").sub("*", "")
    puts <<EOF
    uint64_t _sz = bt_field_array_get_length(_field);
    if (_sz > 0) {
      #{name} = (#{type})malloc(_sz*sizeof(#{scalar_type}));
      for (uint64_t _i = 0; _i < _sz; _i++) {
60
61
62
63
64
65
66
67
68
69
70
71
72
73
EOF
    case f[:field][:class]
    when 'unsigned'
      puts <<EOF
        #{name}[_i] = (#{scalar_type})bt_field_integer_unsigned_get_value(bt_field_array_borrow_element_field_by_index_const(_field, _i));
EOF
    when 'signed'
      puts <<EOF
        #{name}[_i] = (#{scalar_type})bt_field_integer_signed_get_value(bt_field_array_borrow_element_field_by_index_const(_field, _i));
EOF
    else
      raise "unsupported array element #{f[:field][:class]}"
    end
    puts <<EOF
74
75
76
77
      }
    } else
      #{name} = NULL;
EOF
78
  when 'string'
79
80
81
82
83
84
85
86
87
88
    puts <<EOF
    #{name} = (#{type})bt_field_string_get_value(_field);
EOF
  end
  puts <<EOF
  }
EOF
end

def print_field_members_access(fields)
89
  puts <<EOF unless fields.empty?
Brice Videau's avatar
Brice Videau committed
90
  const bt_field *payload_field = bt_event_borrow_payload_field_const(bt_evt);
91
EOF
92
93
  fields.each_with_index { |f, i|
    print_field_member_access(f, i)
94
95
96
  }
end

97
98
99
100
101
102
103
104
opencl_babeltrace_model[:event_classes].each { |klass|
  name = klass[:name]
  fields = klass[:payload]
  decls = []
  fields.each { |f|
    decls.push ['size_t', "_#{f[:name]}_length"] if f[:class] == 'array_static'
    decls.push [f[:cast_type], f[:name]]
  }
105
  puts <<EOF
Brice Videau's avatar
Brice Videau committed
106
107
static void
#{name.gsub(":","_")}_dispatcher(
108
109
    struct opencl_dispatch  *opencl_dispatch,
    struct opencl_callbacks *callbacks,
Brice Videau's avatar
Brice Videau committed
110
    const bt_event          *bt_evt,
Brice Videau's avatar
Brice Videau committed
111
    const bt_clock_snapshot *bt_clock) {
112
  #{decls.each.collect { |f| "#{f[0]} #{f[1]}" }.join(";\n  ")};
113
114
115
EOF
print_field_members_access(fields)
puts <<EOF
Brice Videau's avatar
Bugfix.    
Brice Videau committed
116
  void **p = NULL;
117
  while( (p = utarray_next(callbacks->callbacks, p)) ) {
Brice Videau's avatar
Bugfix.    
Brice Videau committed
118
    ((#{name.gsub(":","_")}_cb *)*p)(
119
      #{(["bt_evt", "bt_clock"] + decls.collect { |f| f[1] }).join(",\n      ")});
120
  }
121
122
123
  #{fields.each.collect { |f|
    if f[:class].match('array')
      "if (#{f[:name]}) free((void *)#{f[:name]})"
124
125
126
127
128
129
130
131
132
133
134
135
    else
      nil
    end
  }.compact.join(";\n  ")};
}

EOF
}

puts <<EOF
void init_dispatchers(struct opencl_dispatch   *opencl_dispatch) {
EOF
136
137
opencl_babeltrace_model[:event_classes].each { |klass|
  name = klass[:name]
138
139
140
141
142
143
144
  puts <<EOF
  opencl_register_dispatcher(opencl_dispatch, "#{name}", &#{name.gsub(":","_")}_dispatcher);
EOF
}
puts <<EOF
}
EOF