metababel 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,132 @@
1
+ require_relative 'bt2_generator_utils'
2
+
3
+ module Babeltrace2Gen
4
+ class BTValueCLass
5
+ attr_accessor :cast_type
6
+
7
+ def self.from_h(model)
8
+ key = model.delete(:type)
9
+ raise "No type in #{model}" unless key
10
+
11
+ h = { 'map' => BTValueCLass::Map,
12
+ 'string' => BTValueCLass::String,
13
+ 'bool' => BTValueCLass::Bool,
14
+ 'integer_unsigned' => BTValueCLass::IntegerUnsigned }.freeze
15
+ raise "Type #{key} not supported" unless h.include?(key)
16
+
17
+ cast_type = model.delete(:cast_type)
18
+ fc = h[key].from_h(model)
19
+ fc.cast_type = cast_type if cast_type
20
+ fc
21
+ end
22
+ end
23
+
24
+ class BTValueCLass::Scalar < BTValueCLass
25
+ attr_accessor :name, :usr_default_value
26
+
27
+ include BTPrinter
28
+
29
+ # Scalars are leafs, avoid recursion
30
+ def self.from_h(model)
31
+ new(model[:name],model.fetch(:default_value,nil))
32
+ end
33
+
34
+ def initialize(name,usr_default_value)
35
+ @name = name
36
+ @usr_default_value = usr_default_value
37
+ end
38
+
39
+ def get(name, val)
40
+ cast_func = @cast_type ? "(#{@cast_type})" : ''
41
+ bt_default_value = self.class.instance_variable_get(:@bt_default_value)
42
+ bt_type = self.class.instance_variable_get(:@bt_type)
43
+ bt_type_is = self.class.instance_variable_get(:@bt_type_is)
44
+
45
+ default_value = @usr_default_value || bt_default_value
46
+
47
+ pr "if (#{val} != NULL) {"
48
+ pr " if (!#{bt_type_is}(#{val})) {"
49
+ pr " fprintf(stderr,\"Bad value for command line argument '%s' the value must be '%s'. \\n\",\"#{@name}\",\"#{bt_type}\");"
50
+ pr " exit(1);"
51
+ pr " }"
52
+ pr " #{name} = #{cast_func}bt_value_#{bt_type}_get(#{val});"
53
+ pr "} else {"
54
+ pr " #{name} = #{cast_func}#{default_value};"
55
+ pr "}"
56
+ end
57
+ end
58
+
59
+ class BTValueCLass::Map < BTValueCLass
60
+ include BTPrinter
61
+
62
+ def self.from_h(model)
63
+ new(model[:entries])
64
+ end
65
+
66
+ def initialize(entries = [])
67
+ @entries = entries.collect { |m| BTValueCLass.from_h(**m) }
68
+ end
69
+
70
+ def get(struct, map)
71
+ @entries.map do |m|
72
+ scope do
73
+ pr "const bt_value *val = bt_value_map_borrow_entry_value_const(#{map}, \"#{m.name}\");"
74
+ m.get("#{struct}->#{m.name}", 'val')
75
+ end
76
+ end
77
+ end
78
+
79
+ def get_struct_definition(_name)
80
+ @entries.map do |e|
81
+ type = e.cast_type || e.class.instance_variable_get(:@bt_return_type)
82
+ " #{type} #{e.name};"
83
+ end.join("\n")
84
+ end
85
+ end
86
+
87
+ class BTValueCLass::Bool < BTValueCLass::Scalar
88
+ @bt_type = 'bool'
89
+ @bt_type_is = 'bt_value_is_bool'
90
+ @bt_return_type = 'bt_bool'
91
+ @bt_default_value = 'BT_FALSE'
92
+
93
+ def initialize(name,usr_default_value)
94
+ bt_type = self.class.instance_variable_get(:@bt_type)
95
+ if !usr_default_value.nil? and !['BT_TRUE', 'BT_FALSE'].include? usr_default_value
96
+ raise "Bad default_value for '#{name}' in params.yaml, it must be #{bt_type} (BT_TRUE or BT_FALSE) but provided '#{usr_default_value}'."
97
+ end
98
+ super(name,usr_default_value)
99
+ end
100
+ end
101
+
102
+ class BTValueCLass::String < BTValueCLass::Scalar
103
+ @bt_type = 'string'
104
+ @bt_type_is = 'bt_value_is_string'
105
+ @bt_return_type = 'const char*'
106
+ @bt_default_value = 'NULL'
107
+
108
+ def initialize(name,usr_default_value)
109
+ bt_type = self.class.instance_variable_get(:@bt_type)
110
+ # Every object that can be converted to string is being supported.
111
+ if !usr_default_value.nil? and !usr_default_value.respond_to?(:to_s)
112
+ raise "Bad default_value for '#{name}' in params.yaml, it must be #{bt_type} but provided '#{usr_default_value}'."
113
+ end
114
+ super(name,usr_default_value.to_s.inspect)
115
+ end
116
+ end
117
+
118
+ class BTValueCLass::IntegerUnsigned < BTValueCLass::Scalar
119
+ @bt_type = 'integer_unsigned'
120
+ @bt_type_is = 'bt_value_is_signed_integer'
121
+ @bt_return_type = 'uint64_t'
122
+ @bt_default_value = '0'
123
+
124
+ def initialize(name,usr_default_value)
125
+ bt_type = self.class.instance_variable_get(:@bt_type)
126
+ if !usr_default_value.nil? and (!usr_default_value.kind_of? Integer or !usr_default_value.between?(0,2**64-1))
127
+ raise "Bad default_value for '#{name}' in params.yaml, it must be #{bt_type} and must be in [0,2^64-1], but provided '#{usr_default_value}'."
128
+ end
129
+ super(name,usr_default_value)
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,3 @@
1
+ module Metababel
2
+ VERSION = '0.0.0'
3
+ end
data/lib/metababel.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'metababel/bt2_generator_utils'
2
+ require 'metababel/bt2_stream_classes_generator'
3
+ require 'metababel/bt2_values_generator'
4
+ require 'metababel/version'
@@ -0,0 +1,69 @@
1
+ #include "utarray.h"
2
+ #include "uthash.h"
3
+ #include <assert.h>
4
+ #include <babeltrace2/babeltrace.h>
5
+ #include <metababel/btx_component.h>
6
+ #include <stdbool.h>
7
+ // stdio needed because `params_definition` contain fprintf
8
+ #include <stdio.h>
9
+
10
+ void btx_populate_params(common_data_t *common_data) {
11
+ const bt_value *params = common_data->params;
12
+ btx_params_t *usr_params = common_data->btx_params;
13
+ (void)params;
14
+ (void)usr_params;
15
+ <%= params_definition %>
16
+ }
17
+
18
+ <% component_dispatchers.each do |e| %>
19
+
20
+ typedef void btx_dispatch_<%= e.name_sanitized %>_f(
21
+ UT_array *callbacks, common_data_t *common_data,
22
+ <%= e.args.map{ |s| s.type }.join(", ") %>);
23
+
24
+ static void btx_dispatch_<%= e.name_sanitized %>(
25
+ UT_array *callbacks, common_data_t *common_data,
26
+ <%= e.args.map{ |s| "#{s.type} #{s.name}" }.join(", ") %>) {
27
+
28
+ // Call all the callbacks who where registered
29
+ // Their type are declared in 'upstream.h'
30
+ <%= e.name_sanitized %>_callback_f **p = NULL;
31
+ while ((p = utarray_next(callbacks, p))) {
32
+ (*p)((void *)common_data, <%= e.args.map{ |s| s.name }.join(", ") %>);
33
+ }
34
+ }
35
+
36
+ void btx_register_callbacks_<%= e.name_sanitized %>(
37
+ void *btx_handle, <%= e.name_sanitized %>_callback_f *callback) {
38
+ // Look-up our dispatcher
39
+ name_to_dispatcher_t *s = NULL;
40
+ name_to_dispatcher_t **name_to_dispatcher =
41
+ &((common_data_t *)btx_handle)->name_to_dispatcher;
42
+ HASH_FIND_STR(*name_to_dispatcher, "<%= e.name %>", s);
43
+ if (!s) {
44
+ // We didn't find the dispatcher, so we need to:
45
+ // 1. Create it
46
+ s = (name_to_dispatcher_t *)malloc(sizeof(name_to_dispatcher_t));
47
+ s->name = "<%= e.name %>";
48
+ s->dispatcher = (void *)&btx_dispatch_<%= e.name_sanitized %>;
49
+ utarray_new(s->callbacks, &ut_ptr_icd);
50
+ // 2. Register it
51
+ HASH_ADD_KEYPTR(hh, *name_to_dispatcher, s->name, strlen(s->name), s);
52
+ // 3. Add the callbacks to the array
53
+ utarray_push_back(s->callbacks, &callback);
54
+ } else {
55
+ assert(false && "Only one callbacks for <%= e.name_sanitized %>");
56
+ }
57
+ }
58
+
59
+ void btx_call_callbacks_<%= e.name_sanitized %>(
60
+ common_data_t *common_data,
61
+ <%= e.args.map{ |s| "#{s.type} #{s.name}" }.join(", ") %>) {
62
+ name_to_dispatcher_t *s = NULL;
63
+ HASH_FIND_STR(common_data->name_to_dispatcher, "<%= e.name %>", s);
64
+
65
+ if (s)
66
+ (*((btx_dispatch_<%= e.name_sanitized %>_f(*))(s->dispatcher)))(
67
+ s->callbacks, common_data, <%= e.args.map{ |s| s.name }.join(", ") %>);
68
+ }
69
+ <% end %>
@@ -0,0 +1,160 @@
1
+ #pragma once
2
+
3
+ #include "utarray.h"
4
+ #include "uthash.h"
5
+ #include <babeltrace2/babeltrace.h>
6
+
7
+ #ifdef __cplusplus
8
+ extern "C" {
9
+ #endif
10
+
11
+ // Forward declaration of common_data
12
+ struct common_data_s;
13
+ typedef struct common_data_s common_data_t;
14
+
15
+ // Params
16
+ struct btx_params_s {
17
+ <%= params_declaration %>
18
+
19
+ }; <%# Need empty line for nice code gen %>
20
+ typedef struct btx_params_s btx_params_t;
21
+
22
+ void btx_populate_params(common_data_t *common_data_t);
23
+
24
+ <% if options.key?(:usr_data_header) %>
25
+ #include "<%= options[:usr_data_header] %>"
26
+ <% end %>
27
+
28
+ struct name_to_dispatcher_s {
29
+ const char *name;
30
+ void *dispatcher;
31
+ UT_array *callbacks;
32
+ UT_hash_handle hh;
33
+ };
34
+ typedef struct name_to_dispatcher_s name_to_dispatcher_t;
35
+
36
+ <% if ['SOURCE', 'FILTER'].include?(options[:component_type]) %>
37
+ // Structure for Downstream Message
38
+ struct el {
39
+ const bt_message *message;
40
+ struct el *next, *prev;
41
+ };
42
+
43
+ <% end %>
44
+ // Struct stored in the component via `bt_self_component_set_data`
45
+ <% if options[:component_type] == 'SOURCE' %>
46
+ struct common_data_s {
47
+ name_to_dispatcher_t *name_to_dispatcher;
48
+ void *usr_data;
49
+ const bt_value *params;
50
+ btx_params_t *btx_params;
51
+ bt_trace *downstream_trace;
52
+ /* Used by downstream.c */
53
+ bt_self_message_iterator *self_message_iterator;
54
+ };
55
+
56
+ enum btx_source_state_e {
57
+ BTX_SOURCE_STATE_INITIALIZING,
58
+ BTX_SOURCE_STATE_PROCESSING,
59
+ BTX_SOURCE_STATE_FINALIZING,
60
+ BTX_SOURCE_STATE_FINISHED,
61
+ BTX_SOURCE_STATE_ERROR,
62
+ };
63
+ typedef enum btx_source_state_e btx_source_state_t;
64
+
65
+ /* Message iterator's private data */
66
+ struct btx_message_iterator_s {
67
+ /* (Weak) link to the component's private data */
68
+ common_data_t *common_data;
69
+ btx_source_state_t state;
70
+
71
+ /* Handling the downstream message queue */
72
+ struct el *queue;
73
+ struct el *pool;
74
+ };
75
+ typedef struct btx_message_iterator_s btx_message_iterator_t;
76
+
77
+ enum btx_source_status_e {
78
+ BTX_SOURCE_END,
79
+ BTX_SOURCE_OK,
80
+ };
81
+ typedef enum btx_source_status_e btx_source_status_t;
82
+
83
+ <% elsif options[:component_type] == 'FILTER' %>
84
+ struct common_data_s {
85
+ name_to_dispatcher_t *name_to_dispatcher;
86
+ void *usr_data;
87
+ const bt_value *params;
88
+ btx_params_t *btx_params;
89
+ /* Component's input port (weak) */
90
+ const bt_component_filter *component;
91
+ bt_trace *downstream_trace;
92
+ /* Used by downstream.c */
93
+ bt_self_message_iterator *self_message_iterator;
94
+ };
95
+
96
+ enum btx_filter_state_e {
97
+ BTX_FILTER_STATE_INITIALIZING,
98
+ BTX_FILTER_STATE_PROCESSING,
99
+ BTX_FILTER_STATE_FINALIZING,
100
+ BTX_FILTER_STATE_FINISHED,
101
+ BTX_FILTER_STATE_ERROR,
102
+ };
103
+ typedef enum btx_filter_state_e btx_filter_state_t;
104
+
105
+ enum btx_filter_processing_state_e {
106
+ BTX_FILTER_PROCESSING_STATE_READING,
107
+ BTX_FILTER_PROCESSING_STATE_SENDING,
108
+ BTX_FILTER_PROCESSING_STATE_FINISHED,
109
+ };
110
+ typedef enum btx_filter_processing_state_e btx_filter_processing_state_t;
111
+
112
+ typedef struct el_mi {
113
+ /* Upstream message iterator (owned by this) */
114
+ bt_message_iterator *message_iterator;
115
+ struct el_mi *next, *prev;
116
+ } el_mi;
117
+
118
+ /* Message iterator's private data */
119
+ struct btx_message_iterator_s {
120
+ /* (Weak) link to the component's private data */
121
+ common_data_t *common_data;
122
+ /* Upstream messages iterator */
123
+ el_mi *head_mi;
124
+ btx_filter_state_t state;
125
+ btx_filter_processing_state_t processing_state;
126
+ /* Handling the downstream message queue */
127
+ struct el *queue;
128
+ struct el *pool;
129
+ };
130
+ typedef struct btx_message_iterator_s btx_message_iterator_t;
131
+
132
+ <% elsif options[:component_type] == 'SINK' %>
133
+ struct common_data_s {
134
+ name_to_dispatcher_t *name_to_dispatcher;
135
+ void *usr_data;
136
+ const bt_value *params;
137
+ btx_params_t *btx_params;
138
+ /* Upstream message iterator (owned by this) */
139
+ bt_message_iterator *message_iterator;
140
+ };
141
+ <% end %>
142
+
143
+ void btx_register_usr_callbacks(void *btx_handle);
144
+
145
+ <% component_dispatchers.each do |e| %>
146
+ <%# The signature type of callbacks %>
147
+ typedef void
148
+ <%= e.name_sanitized %>_callback_f(void *btx_handle,
149
+ <%= e.args.map{ |s| s.type }.join(", ") %>);
150
+ <%# The Function who register the callbacks to the dispatcher %>
151
+ void btx_register_callbacks_<%= e.name_sanitized %>(
152
+ void *btx_handle, <%= e.name_sanitized %>_callback_f *callback);
153
+
154
+ void btx_call_callbacks_<%= e.name_sanitized %>(
155
+ common_data_t *common_data, <%= e.args.map{ |s| s.type }.join(", ") %>);
156
+ <% end %>
157
+
158
+ #ifdef __cplusplus
159
+ }
160
+ #endif
@@ -0,0 +1,119 @@
1
+ #include "utlist.h"
2
+ #include <babeltrace2/babeltrace.h>
3
+ #include <metababel/btx_component.h>
4
+ #include <metababel/btx_downstream.h>
5
+ #include <stdlib.h>
6
+
7
+ void btx_downstream_move_messages(
8
+ btx_message_iterator_t *message_iterator_private_data,
9
+ bt_message_array_const messages, uint64_t capacity, uint64_t *count) {
10
+
11
+ // Set count to min(capacity, common_data->queue.size())
12
+ // Count time, pop the head of the queue and put it in messages
13
+ for (*count = 0; *count < capacity && message_iterator_private_data->queue;
14
+ (*count)++) {
15
+ struct el *elt = message_iterator_private_data->queue;
16
+ messages[*count] = elt->message;
17
+ DL_DELETE(message_iterator_private_data->queue, elt);
18
+ // Put it back to the bool of chain for reuse
19
+ DL_APPEND(message_iterator_private_data->pool, elt);
20
+ }
21
+ }
22
+
23
+ void btx_downstream_push_message(
24
+ btx_message_iterator_t *message_iterator_private_data,
25
+ const bt_message *message) {
26
+
27
+ struct el *elt;
28
+ if (message_iterator_private_data->pool) {
29
+ elt = message_iterator_private_data->pool;
30
+ DL_DELETE(message_iterator_private_data->pool, elt);
31
+ } else {
32
+ elt = (struct el *)malloc(sizeof *elt);
33
+ }
34
+ elt->message = message;
35
+ DL_APPEND(message_iterator_private_data->queue, elt);
36
+ }
37
+
38
+ bt_trace_class *
39
+ btx_downstream_trace_class_create_rec(bt_self_component *self_component) {
40
+ bt_trace_class *trace_class = bt_trace_class_create(self_component);
41
+ <%= body_declarator_classes %>
42
+ return trace_class;
43
+ }
44
+
45
+ bt_trace *btx_downstream_trace_create_rec(bt_trace_class *trace_class) {
46
+ bt_trace *trace = bt_trace_create(trace_class);
47
+ <% stream_classes.each_with_index do |_,i| %>
48
+ {
49
+ bt_stream_class *stream_class =
50
+ bt_trace_class_borrow_stream_class_by_index(trace_class, <%= i %>);
51
+ bt_stream_create(stream_class, trace);
52
+ }
53
+ <% end %>
54
+ return trace;
55
+ }
56
+
57
+ void btx_push_messages_stream_beginning(
58
+ bt_self_message_iterator *self_message_iterator,
59
+ btx_message_iterator_t *message_iterator_private_data) {
60
+
61
+ bt_trace *trace =
62
+ message_iterator_private_data->common_data->downstream_trace;
63
+ <% stream_classes.each_with_index do |_,i| %>
64
+ {
65
+ bt_stream *stream = bt_trace_borrow_stream_by_index(trace, <%= i %>);
66
+ bt_message *message =
67
+ bt_message_stream_beginning_create(self_message_iterator, stream);
68
+ btx_downstream_push_message(message_iterator_private_data, message);
69
+ }
70
+ <% end %>
71
+ }
72
+
73
+ void btx_push_messages_stream_end(
74
+ bt_self_message_iterator *self_message_iterator,
75
+ btx_message_iterator_t *message_iterator_private_data) {
76
+
77
+ bt_trace *trace =
78
+ message_iterator_private_data->common_data->downstream_trace;
79
+ <% stream_classes.each_with_index do |_,i| %>
80
+ {
81
+ bt_stream *stream = bt_trace_borrow_stream_by_index(trace, <%= i %>);
82
+ bt_message *message =
83
+ bt_message_stream_end_create(self_message_iterator, stream);
84
+ btx_downstream_push_message(message_iterator_private_data, message);
85
+ }
86
+ <% end %>
87
+ }
88
+
89
+ <% downstream_events.each do |e| %>
90
+ static void btx_set_message_<%= e.name_sanitized %>(
91
+ bt_event *<%= event_name %>,
92
+ <%= e.args.map{ |s| "#{s.type} #{s.name}" }.join(", ") %>) {
93
+ <%= e.body %>
94
+ }
95
+
96
+ void btx_push_message_<%= e.name_sanitized %>(
97
+ void *btx_handle,
98
+ <%= e.args.map{ |s| "#{s.type} #{s.name}" }.join(", ") %>) {
99
+
100
+ common_data_t *common_data = (common_data_t *)btx_handle;
101
+
102
+ bt_stream *stream = bt_trace_borrow_stream_by_index(
103
+ common_data->downstream_trace, <%= e.index_stream_class %>);
104
+ bt_stream_class *stream_class = bt_stream_borrow_class(stream);
105
+ bt_event_class *event_class = bt_stream_class_borrow_event_class_by_index(
106
+ stream_class, <%= e.index_event_class %>);
107
+
108
+ bt_message *message = bt_message_event_create(
109
+ common_data->self_message_iterator, event_class, stream);
110
+ bt_event *downstream_event = bt_message_event_borrow_event(message);
111
+
112
+ btx_set_message_<%= e.name_sanitized %>(
113
+ downstream_event, <%= e.args.map{ |s| s.name }.join(", ") %>);
114
+
115
+ btx_message_iterator_t *message_iterator_private_data =
116
+ bt_self_message_iterator_get_data(common_data->self_message_iterator);
117
+ btx_downstream_push_message(message_iterator_private_data, message);
118
+ }
119
+ <% end %>
@@ -0,0 +1,34 @@
1
+ #pragma once
2
+ #include <babeltrace2/babeltrace.h>
3
+ #include <metababel/btx_component.h>
4
+ #ifdef __cplusplus
5
+ extern "C" {
6
+ #endif
7
+ void btx_downstream_move_messages(
8
+ btx_message_iterator_t *message_iterator_private_data,
9
+ bt_message_array_const messages, uint64_t capacity, uint64_t *count);
10
+
11
+ void btx_downstream_push_message(
12
+ btx_message_iterator_t *message_iterator_private_data,
13
+ const bt_message *message);
14
+
15
+ bt_trace_class *
16
+ btx_downstream_trace_class_create_rec(bt_self_component *self_component);
17
+
18
+ bt_trace *btx_downstream_trace_create_rec(bt_trace_class *trace_class);
19
+
20
+ void btx_push_messages_stream_beginning(
21
+ bt_self_message_iterator *self_message_iterator,
22
+ btx_message_iterator_t *message_iterator_private_data);
23
+ void btx_push_messages_stream_end(
24
+ bt_self_message_iterator *self_message_iterator,
25
+ btx_message_iterator_t *message_iterator_private_data);
26
+
27
+ <% downstream_events.each do |e| %>
28
+ void btx_push_message_<%= e.name_sanitized %>(
29
+ void *btx_handle,
30
+ <%= e.args.map{ |s| "#{s.type} #{s.name}" }.join(", ") %>);
31
+ <% end %>
32
+ #ifdef __cplusplus
33
+ }
34
+ #endif