Class: Roby::DRoby::Timepoints::CTF

Inherits:
Object
  • Object
show all
Defined in:
lib/roby/droby/timepoints_ctf.rb

Constant Summary collapse

ID_TIMEPOINT =
1
ID_GROUP_START =
2
ID_GROUP_END =
3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clock_base: 0) ⇒ CTF

Returns a new instance of CTF.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/roby/droby/timepoints_ctf.rb', line 11

def initialize(clock_base: 0)
    @uuid = SecureRandom.random_bytes(16).unpack("C*")
    @clock_base = clock_base

    @packet_timestamp_begin = nil
    @packet_timestamp_end = nil
    @packet_contents = String.new
    @name_to_addr_mapping = {}

    @thread_ids = {}
end

Instance Attribute Details

#clock_baseObject (readonly)

Returns the value of attribute clock_base.



9
10
11
# File 'lib/roby/droby/timepoints_ctf.rb', line 9

def clock_base
  @clock_base
end

#name_to_addr_mappingObject (readonly)

Returns the value of attribute name_to_addr_mapping.



9
10
11
# File 'lib/roby/droby/timepoints_ctf.rb', line 9

def name_to_addr_mapping
  @name_to_addr_mapping
end

#packet_contentsObject (readonly)

Returns the value of attribute packet_contents.



9
10
11
# File 'lib/roby/droby/timepoints_ctf.rb', line 9

def packet_contents
  @packet_contents
end

#packet_timestamp_beginObject (readonly)

Returns the value of attribute packet_timestamp_begin.



9
10
11
# File 'lib/roby/droby/timepoints_ctf.rb', line 9

def packet_timestamp_begin
  @packet_timestamp_begin
end

#packet_timestamp_endObject (readonly)

Returns the value of attribute packet_timestamp_end.



9
10
11
# File 'lib/roby/droby/timepoints_ctf.rb', line 9

def packet_timestamp_end
  @packet_timestamp_end
end

#thread_idsObject (readonly)

Returns the value of attribute thread_ids.



9
10
11
# File 'lib/roby/droby/timepoints_ctf.rb', line 9

def thread_ids
  @thread_ids
end

#uuidObject (readonly)

Returns the value of attribute uuid.



9
10
11
# File 'lib/roby/droby/timepoints_ctf.rb', line 9

def uuid
  @uuid
end

Class Method Details

.generate_metadata(path, uuid, _clock_base) ⇒ Object



62
63
64
65
66
67
# File 'lib/roby/droby/timepoints_ctf.rb', line 62

def self.(path, uuid, _clock_base)
    uuid_s = uuid.map { |v| format("%02x", v) }.join
    uuid_s = "#{uuid_s[0, 8]}-#{uuid_s[8, 4]}-#{uuid_s[12, 4]}-"\
             "#{uuid_s[16, 4]}-#{uuid_s[20, 12]}"
    ERB.new(path.read).result(binding)
end

Instance Method Details

#add(time, thread_id, thread_name, name) ⇒ Object



58
59
60
# File 'lib/roby/droby/timepoints_ctf.rb', line 58

def add(time, thread_id, thread_name, name)
    update_packet(time, marshal_event(time, ID_TIMEPOINT, thread_id_of(thread_id), thread_name, name))
end

#addr_from_name(name) ⇒ Object



27
28
29
# File 'lib/roby/droby/timepoints_ctf.rb', line 27

def addr_from_name(name)
    name_to_addr_mapping[name] ||= name_to_addr_mapping.size
end

#generate_metadataObject



73
74
75
# File 'lib/roby/droby/timepoints_ctf.rb', line 73

def 
    self.class.(, uuid, clock_base)
end

#group_end(time, thread_id, thread_name, name) ⇒ Object



54
55
56
# File 'lib/roby/droby/timepoints_ctf.rb', line 54

def group_end(time, thread_id, thread_name, name)
    update_packet(time, marshal_event(time, ID_GROUP_END, thread_id_of(thread_id), thread_name, name))
end

#group_start(time, thread_id, thread_name, name) ⇒ Object



49
50
51
52
# File 'lib/roby/droby/timepoints_ctf.rb', line 49

def group_start(time, thread_id, thread_name, name)
    marshalled = marshal_event(time, ID_GROUP_START, thread_id_of(thread_id), thread_name, name)
    update_packet(time, marshalled + [addr_from_name(name)].pack("L<"))
end

#make_timestamp(time) ⇒ Object



23
24
25
# File 'lib/roby/droby/timepoints_ctf.rb', line 23

def make_timestamp(time)
    (time.tv_sec - clock_base) * 1_000_000 + time.tv_usec
end

#marshal_event(time, event_id, thread_id, thread_name, name) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/roby/droby/timepoints_ctf.rb', line 77

def marshal_event(time, event_id, thread_id, thread_name, name)
    timestamp = make_timestamp(time)
    event_header =
        [0xFFFF, event_id, make_timestamp(time)]
        .pack("S<L<Q<")
    thread_name ||= ""
    event_context =
        [thread_id, thread_name.size, thread_name, name.size, name]
        .pack("L<S<A#{thread_name.size}S<A#{name.size}")
    event_header + event_context
end

#marshal_packetObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/roby/droby/timepoints_ctf.rb', line 89

def marshal_packet
    header = [
        0xC1FC1FC1, # Magic
        *uuid,
        0 # Stream ID
    ].pack("L<C16L<")
    contents = packet_contents
    context = [
        make_timestamp(packet_timestamp_begin),
        make_timestamp(packet_timestamp_end),
        0
    ].pack("Q<Q<L<")

    @packet_timestamp_begin = nil
    @packet_timestamp_end = nil
    @packet_contents = String.new
    header + context + contents
end

#metadata_template_pathObject



69
70
71
# File 'lib/roby/droby/timepoints_ctf.rb', line 69

def 
    "#{Pathname.new(__FILE__).dirname}timepoints_ctf.metadata.erb"
end

#save(path) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/roby/droby/timepoints_ctf.rb', line 108

def save(path)
    "#{path}metadata".open("w") do |io|
        io.write 
    end
    "#{path}channel0_0".open("w") do |io|
        io.write marshal_packet
    end
    path.sub_ext(".ctf.names").open("w") do |io|
        name_to_addr_mapping.each do |name, id|
            io.puts(format("%016x T %s", id, name.gsub(/[^\w]/, "_")))
        end
    end
end

#thread_id_of(thread_id) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/roby/droby/timepoints_ctf.rb', line 41

def thread_id_of(thread_id)
    if id = thread_ids[thread_id]
        id
    else
        thread_ids[thread_id] = thread_ids.size
    end
end

#update_packet(time, marshalled_event) ⇒ Object



35
36
37
38
39
# File 'lib/roby/droby/timepoints_ctf.rb', line 35

def update_packet(time, marshalled_event)
    @packet_timestamp_begin ||= time
    @packet_timestamp_end = time
    packet_contents << marshalled_event
end