def _create_trace(self):
        trace = btw.Writer(self._trace_path)
        clock = btw.Clock('test_clock')
        trace.add_clock(clock)

        integer_field_type = btw.IntegerFieldDeclaration(32)

        event_class = btw.EventClass('simple_event')
        event_class.add_field(integer_field_type, 'int_field')

        stream_class = btw.StreamClass('empty_packet_stream')
        stream_class.add_event_class(event_class)
        stream_class.clock = clock

        stream = trace.create_stream(stream_class)

        for i in range(self._expected_event_count):
            event = btw.Event(event_class)
            event.payload('int_field').value = i
            stream.append_event(event)
        stream.flush()

        # The CTF writer will not be able to populate the packet context's
        # timestamp_begin and timestamp_end fields if it is asked to flush
        # without any queued events.
        with self.assertRaises(ValueError):
            stream.flush()

        packet_context = stream.packet_context
        packet_context.field('timestamp_begin').value = 1
        packet_context.field('timestamp_end').value = 123456

        stream.flush()
Example #2
0
    def create_event_types(self):
        int32_field_decl = btw.IntegerFieldDeclaration(32)
        struct_field_decl = btw.StructureFieldDeclaration()
        struct_field_decl.add_field(int32_field_decl, "pid")
        struct_field_decl.add_field(int32_field_decl, "cpu_id")
        self.stream_class.packet_context_type = struct_field_decl
        self.stream_context_decl = struct_field_decl

        event_class = btw.EventClass("task_create")
        int32_type = btw.IntegerFieldDeclaration(32)
        event_class.add_field(int32_type, "tid")
        self.stream_class.add_event_class(event_class)
        self.tcreateEventClass = event_class

        event_class = btw.EventClass("apex_START")
        int32_type = btw.IntegerFieldDeclaration(32)
        event_class.add_field(int32_type, "tid")
        self.stream_class.add_event_class(event_class)
        self.apexStart = event_class

        self.schedswitchEventClass = btw.EventClass("task_switch")
        int32_type = btw.IntegerFieldDeclaration(32)
        self.schedswitchEventClass.add_field(int32_type, "next_tid")
        self.stream_class.add_event_class(self.schedswitchEventClass)

        self.irqEntryEventClass = btw.EventClass("irq_handler_entry")
        int32_type = btw.IntegerFieldDeclaration(32)
        self.irqEntryEventClass.add_field(int32_type, "irq")
        self.stream_class.add_event_class(self.irqEntryEventClass)

        self.irqExitEventClass = btw.EventClass("irq_handler_exit")
        int32_type = btw.IntegerFieldDeclaration(32)
        self.irqExitEventClass.add_field(int32_type, "irq")
        self.stream_class.add_event_class(self.irqExitEventClass)

        self.syscallEntryEventClass = btw.EventClass("sys_raw_entry")
        int32_type = btw.IntegerFieldDeclaration(32)
        self.syscallEntryEventClass.add_field(int32_type, "id")
        self.stream_class.add_event_class(self.syscallEntryEventClass)

        self.syscallExitEventClass = btw.EventClass("sys_raw_exit")
        int32_type = btw.IntegerFieldDeclaration(32)
        self.syscallExitEventClass.add_field(int32_type, "id")
        self.stream_class.add_event_class(self.syscallExitEventClass)

        self.partSwitchEventClass = btw.EventClass("part_switch")
        int32_type = btw.IntegerFieldDeclaration(32)
        self.partSwitchEventClass.add_field(int32_type, "pid")
        self.stream_class.add_event_class(self.partSwitchEventClass)
Example #3
0
    def _create_trace(self, stream_descriptions):
        trace_path = tempfile.mkdtemp()
        trace = btw.Writer(trace_path)
        clock = btw.Clock('test_clock')
        clock.uuid = self._clock_uuid
        trace.add_clock(clock)

        integer_field_type = btw.IntegerFieldDeclaration(32)

        event_class = btw.EventClass('simple_event')
        event_class.add_field(integer_field_type, 'int_field')

        stream_class = btw.StreamClass('test_stream')
        stream_class.add_event_class(event_class)
        stream_class.clock = clock

        streams = []
        stream_entries = []
        for stream_id, stream_packets in enumerate(stream_descriptions):
            stream = trace.create_stream(stream_class)
            streams.append(stream)

            for packet in stream_packets:
                for timestamp in packet.timestamps:
                    stream_entries.append(Entry(stream_id, timestamp))
                # Mark the last inserted entry as the end of packet
                stream_entries[len(stream_entries) - 1].end_of_packet = True

        # Sort stream entries which will provide us with a time-ordered list of
        # events to insert in the streams.
        for entry in sorted(stream_entries, key=lambda entry: entry.timestamp):
            clock.time = entry.timestamp
            event = btw.Event(event_class)
            event.payload('int_field').value = entry.stream_id
            streams[entry.stream_id].append_event(event)
            if entry.end_of_packet is True:
                streams[entry.stream_id].flush()

        return trace_path
Example #4
0
    def _write_trace(self):
        trace = btw.Writer(self._trace_path)
        clock = btw.Clock('test_clock')
        trace.add_clock(clock)

        integer_field_type = btw.IntegerFieldDeclaration(32)

        event_class = btw.EventClass('simple_event')
        event_class.add_field(integer_field_type, 'int_field')

        stream_class = btw.StreamClass('test_stream')
        stream_class.add_event_class(event_class)
        stream_class.clock = clock

        stream_class.packet_context_type = None
        stream = trace.create_stream(stream_class)

        for i in range(self._expected_event_count):
            event = btw.Event(event_class)
            event.payload('int_field').value = i
            stream.append_event(event)
        stream.flush()

        # It is not valid for a stream to contain more than one packet
        # if it does not have content_size/packet_size info in its packet
        # context
        event = btw.Event(event_class)
        event.payload('int_field').value = 42
        stream.append_event(event)

        flush_raises = False
        try:
            stream.flush()
        except ValueError:
            flush_raises = True
        self.assertTrue(flush_raises)
        trace.flush_metadata()
import babeltrace
import babeltrace.reader as btr
import babeltrace.writer as btw

# Create field declarations
# Field declarations
uint64_fd = btw.IntegerFieldDeclaration(64)
uint64_fd.signed = False

int64_fd = btw.IntegerFieldDeclaration(64)
int64_fd.signed = True

uint32_fd = btw.IntegerFieldDeclaration(32)
uint32_fd.signed = False

uint16_fd = btw.IntegerFieldDeclaration(16)
uint16_fd.signed = False

int32_fd = btw.IntegerFieldDeclaration(32)
int32_fd.signed = True

float_fd = btw.FloatingPointFieldDeclaration()

huint64_fd = btw.IntegerFieldDeclaration(64)
huint64_fd.base = babeltrace.writer.IntegerBase.HEX
huint64_fd.signed = False

string_fd = btw.StringFieldDeclaration()

dim_array32_fd = btw.ArrayFieldDeclaration(uint32_fd, 3)
dim_array16_fd = btw.ArrayFieldDeclaration(uint16_fd, 3)
print("Clock offset_seconds is {}".format(clock.offset_seconds))
print("Clock offset is {}".format(clock.offset))
print("Clock is absolute: {}".format(clock.absolute))
print("Clock time is {}".format(clock.time))
print("Clock UUID is {}".format(clock.uuid))

writer.add_clock(clock)
writer.add_environment_field("Python_version", str(sys.version_info))

stream_class = btw.StreamClass("test_stream")
stream_class.clock = clock

event_class = btw.EventClass("SimpleEvent")

# Create a int32_t equivalent type
int32_type = btw.IntegerFieldDeclaration(32)
int32_type.signed = True

# Create a uint16_t equivalent type
uint16_type = btw.IntegerFieldDeclaration(16)
uint16_type.signed = False

# Add a custom uint16_t field in the stream's packet context
packet_context_type = stream_class.packet_context_type
print("\nFields in default packet context:")
for field in packet_context_type.fields:
    print(str(type(field[1])) + " " + field[0])
packet_context_type.add_field(uint16_type, "a_custom_packet_context_field")
stream_class.packet_context_type = packet_context_type

# Create a string type
Example #7
0
def create_ctf(trace):
    # temporary directory holding the CTF trace
    trace_path = tempfile.mkdtemp()

    # our writer
    writer = btw.Writer(trace_path)

    # create one default clock and register it to the writer
    clock = btw.Clock('my_clock')
    clock.description = 'this is my clock'
    #clock.frequency = 3570000000
    writer.add_clock(clock)

    # create our single stream
    streams = []
    event_types = []
    for i in range(8):
        event_types.append({})
        print("iter ", i)

        # create one default stream class and assign our clock to it
        stream_class = btw.StreamClass('stream{}'.format(i))
        stream_class.clock = clock

        # create one default event class
        event_class_cont = btw.EventClass('switch_infcont')
        event_class_prev = btw.EventClass('switch_infprev')
        event_class_next = btw.EventClass('switch_infnext')
        event_class_hypercall = btw.EventClass('hypercall')
        event_class_hypercall2 = btw.EventClass('hypercall2')
        event_class_hypercall_version = btw.EventClass('hypercall_version')
        event_class_hypercall_version_return = btw.EventClass('hypercall_version_return')
        event_class_running_runnable = btw.EventClass('running_to_runnable')
        event_class_runnable_running = btw.EventClass('runnable_to_running')
        event_class_running_block = btw.EventClass('running_to_blocked')
        event_class_blocked_runnable = btw.EventClass('blocked_to_runnable')

        # create one 32-bit signed integer field
        int32_field_decl = btw.IntegerFieldDeclaration(32)
        int32_field_decl.signed = True

        # create one 32-bit signed integer field
        int64_field_decl = btw.IntegerFieldDeclaration(64)
        int64_field_decl.signed = True

        # add this field declaration to our event class
        event_class_blocked_runnable.add_field(int32_field_decl, 'dom') 
        event_class_blocked_runnable.add_field(int32_field_decl, 'vcpu') 

        event_class_running_block.add_field(int32_field_decl, 'dom') 
        event_class_running_block.add_field(int32_field_decl, 'vcpu') 

        event_class_runnable_running.add_field(int32_field_decl, 'dom') 
        event_class_runnable_running.add_field(int32_field_decl, 'vcpu') 

        event_class_running_runnable.add_field(int32_field_decl, 'dom') 
        event_class_running_runnable.add_field(int32_field_decl, 'vcpu') 

        event_class_cont.add_field(int32_field_decl, 'dom')
        event_class_cont.add_field(int32_field_decl, 'vcpu')

        event_class_prev.add_field(int32_field_decl, 'dom')
        event_class_prev.add_field(int32_field_decl, 'vcpu')

        event_class_next.add_field(int32_field_decl, 'dom')
        event_class_next.add_field(int32_field_decl, 'vcpu')

        event_class_hypercall.add_field(int32_field_decl, 'op')
        event_class_hypercall2.add_field(int32_field_decl, 'op')

        event_class_hypercall_version.add_field(int32_field_decl, 'id')
        event_class_hypercall_version_return.add_field(int32_field_decl, 'id')

        event_types[i]['switch_infcont'] = event_class_cont
        event_types[i]['switch_infprev'] = event_class_prev
        event_types[i]['switch_infnext'] = event_class_next
        event_types[i]['hypercall'] = event_class_hypercall
        event_types[i]['hypercall2'] = event_class_hypercall2
        event_types[i]['hypercall_version'] = event_class_hypercall_version
        event_types[i]['hypercall_version_return'] = event_class_hypercall_version_return
        event_types[i]['blocked_to_runnable'] = event_class_blocked_runnable
        event_types[i]['running_to_blocked'] = event_class_running_block
        event_types[i]['runnable_to_running'] = event_class_runnable_running
        event_types[i]['running_to_runnable'] = event_class_running_runnable

        # register our event class to our stream class
        stream_class.add_event_class(event_class_cont)
        stream_class.add_event_class(event_class_prev)
        stream_class.add_event_class(event_class_next)
        stream_class.add_event_class(event_class_hypercall)
        stream_class.add_event_class(event_class_hypercall2)
        stream_class.add_event_class(event_class_hypercall_version)
        stream_class.add_event_class(event_class_hypercall_version_return)
        stream_class.add_event_class(event_class_running_block)
        stream_class.add_event_class(event_class_blocked_runnable)
        stream_class.add_event_class(event_class_running_runnable)
        stream_class.add_event_class(event_class_runnable_running)

        stream_class.packet_context_type.add_field(int32_field_decl, 'cpu_id')

        streams.append(writer.create_stream(stream_class))
        streams[-1].packet_context.field('cpu_id').value = i

    used_streams = set()

    done = False
    while not done:
        time = 2 ** 64
        e = None
        selected_s = None
        for s in trace:
            if len(s) > 0:
                temp = s[0]
                if temp["timestamp"] < time:
                    e = temp
                    time = e["timestamp"]
                    selected_s = s
        if e is None:
            done = True
            break
        else:
            e = selected_s.pop(0)
            
        event = None
        cpu = e["cpu"]
        if e["name"] == "switch_infcont":
            event = btw.Event(event_types[cpu]["switch_infcont"])
            event.payload('dom').value = e["dom"]
            event.payload('vcpu').value = e["vcpu"]
        elif e["name"] == "switch_infnext":
            event = btw.Event(event_types[cpu]["switch_infnext"])
            event.payload('dom').value = e["dom"]
            event.payload('vcpu').value = e["vcpu"]
        elif e["name"] == "switch_infprev":
            event = btw.Event(event_types[cpu]["switch_infprev"])
            event.payload('dom').value = e["dom"]
            event.payload('vcpu').value = e["vcpu"]

        elif e["name"] == "blocked_to_runnable":
            event = btw.Event(event_types[cpu]["blocked_to_runnable"])
            event.payload('dom').value = e["dom"]
            event.payload('vcpu').value = e["vcpu"]
        elif e["name"] == "running_to_blocked":
            event = btw.Event(event_types[cpu]["running_to_blocked"])
            event.payload('dom').value = e["dom"]
            event.payload('vcpu').value = e["vcpu"]
        elif e["name"] == "runnable_to_running":
            event = btw.Event(event_types[cpu]["runnable_to_running"])
            event.payload('dom').value = e["dom"]
            event.payload('vcpu').value = e["vcpu"]
        elif e["name"] == "running_to_runnable":
            event = btw.Event(event_types[cpu]["running_to_runnable"])
            event.payload('dom').value = e["dom"]
            event.payload('vcpu').value = e["vcpu"]

        elif e["name"] == "hypercall_op":
            class_ = event_types[cpu]["hypercall"]
            event = btw.Event(event_types[cpu]["hypercall"])
            #print("cpu={}' class={} ev={}".format(cpu, class_, event))
            event.payload('op').value = e["op"]
        elif e["name"] == "hypercall_op2":
            event = btw.Event(event_types[cpu]["hypercall2"])
            event.payload('op').value = e["op"]
        elif e["name"] == "hypercall_version":
            event = btw.Event(event_types[cpu]["hypercall_version"])
            event.payload('id').value = e["id"]

        used_streams.add(cpu)
        stream = streams[cpu]
        if event != None:
            clock.time = to_ns(e["timestamp"])
            stream.append_event(event)
            if e["name"] == "hypercall_version":
                event = btw.Event(event_types[cpu]["hypercall_version_return"])
                event.payload('id').value = e["id"]
                stream.append_event(event)

    # flush the stream
    print('trace path: {}'.format(trace_path))
    for s in used_streams:
        streams[s].flush()
Example #8
0
#!/usr/bin/python3

import babeltrace.reader as btr
import babeltrace.writer as btw
from collections import defaultdict

# Create field declarations

uint64_fd = btw.IntegerFieldDeclaration(64)
uint64_fd.signed = False

huint64_fd = btw.IntegerFieldDeclaration(64)
huint64_fd.base = btw.IntegerBase.HEX
huint64_fd.signed = False

string_fd = btw.StringFieldDeclaration()

# Create event classes


def event_classes_factory():
    other_event_class = btw.EventClass('hsa_runtime:other_event')
    other_event_class.add_field(string_fd, 'name')
    return other_event_class


event_classes = defaultdict(event_classes_factory)

event_classes['runtime_initialized'] = btw.EventClass(
    'hsa_runtime:runtime_initialized')
event_classes['runtime_shut_down'] = btw.EventClass(