class Roby::DRoby::Timepoints::CTF

Constants

ID_GROUP_END
ID_GROUP_START
ID_TIMEPOINT

Attributes

clock_base[R]
name_to_addr_mapping[R]
packet_contents[R]
packet_timestamp_begin[R]
packet_timestamp_end[R]
thread_ids[R]
uuid[R]

Public Class Methods

generate_metadata(path, _uuid, _clock_base) click to toggle source
# File lib/roby/droby/timepoints_ctf.rb, line 68
def self.generate_metadata(path, _uuid, _clock_base)
    _uuid_s = _uuid.map { |v| "%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
new(clock_base: 0) click to toggle source
# File lib/roby/droby/timepoints_ctf.rb, line 17
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 = Hash.new

    @thread_ids = Hash.new
end

Public Instance Methods

add(time, thread_id, thread_name, name) click to toggle source
# File lib/roby/droby/timepoints_ctf.rb, line 64
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) click to toggle source
# File lib/roby/droby/timepoints_ctf.rb, line 33
def addr_from_name(name)
    name_to_addr_mapping[name] ||= name_to_addr_mapping.size
end
generate_metadata() click to toggle source
# File lib/roby/droby/timepoints_ctf.rb, line 78
def generate_metadata
    self.class.generate_metadata(metadata_template_path, uuid, clock_base)
end
group_end(time, thread_id, thread_name, name) click to toggle source
# File lib/roby/droby/timepoints_ctf.rb, line 60
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) click to toggle source
# File lib/roby/droby/timepoints_ctf.rb, line 55
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) click to toggle source
# File lib/roby/droby/timepoints_ctf.rb, line 29
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) click to toggle source
# File lib/roby/droby/timepoints_ctf.rb, line 82
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_packet() click to toggle source
# File lib/roby/droby/timepoints_ctf.rb, line 90
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_path() click to toggle source
# File lib/roby/droby/timepoints_ctf.rb, line 74
def metadata_template_path
    Pathname.new(__FILE__).dirname + "timepoints_ctf.metadata.erb"
end
save(path) click to toggle source
# File lib/roby/droby/timepoints_ctf.rb, line 108
def save(path)
    (path + "metadata").open('w') do |io|
        io.write generate_metadata
    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("%016x T %s" % [id, name.gsub(/[^\w]/, '_')])
        end
    end
end
thread_id_of(thread_id) click to toggle source
# File lib/roby/droby/timepoints_ctf.rb, line 47
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) click to toggle source
# File lib/roby/droby/timepoints_ctf.rb, line 41
def update_packet(time, marshalled_event)
    @packet_timestamp_begin ||= time
    @packet_timestamp_end = time
    packet_contents << marshalled_event
end