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()
Beispiel #2
0
    def sched_switch(self, tid):
        ClockManager().sample()
        e = btw.Event(self.schedswitchEventClass)
        e.payload("next_tid").value = tid

        #ctx = btw.StructureField(self.stream_context_decl)
        #ctx.field("pid").value = self.id
        #e.stream_context = ctx

        self.stream.append_event(e)
Beispiel #3
0
    def apex_START(self, tid):
        ClockManager().sample()
        e = btw.Event(self.apexStart)
        e.payload("tid").value = tid

        #ctx = btw.StructureField(self.stream_context_decl)
        #ctx.field("pid").value = self.id
        #e.stream_context = ctx

        self.stream.append_event(e)
Beispiel #4
0
    def task_create(self, tid):
        ClockManager().sample()
        e = btw.Event(self.tcreateEventClass)
        e.payload("tid").value = tid

        #ctx = btw.StructureField(self.stream_context_decl)
        #ctx.field("pid").value = self.id
        #e.stream_context = ctx

        self.stream.append_event(e)
Beispiel #5
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()
Beispiel #6
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
init_time = 0
pool_memory_allocate_events = {}

# OpenCL times to synchronize GPU times with CPU times
opencl_device_events = []
queued_times = []
end_times = []
enqueueNDRangeKernel_times = []
callback_times = []

for r_event in collection.events:
    name = r_event.name
    event_time = r_event.timestamp
    fields = r_event.field_list_with_scope(
        babeltrace.common.CTFScope.EVENT_FIELDS)
    w_event = btw.Event(event_classes[name])

    for f in fields:
        # print(name, f, r_event[f])
        if r_event[f] == "hsa_init":
            init_time = event_time

        # if hcTracer:kernel_* : fill the grid and groupworker arrays
        if f == "workgroup_size" or f == "grid_size":
            for i in range(3):
                tmp = w_event.payload(f).field(i)
                tmp.value = r_event[f][i]
            continue

        w_event.payload(f).value = r_event[f]
for event_class in event_classes.values():
    main_stream_class.add_event_class(event_class)
main_stream = writer.create_stream(main_stream_class)

# Create events, based on event classes

init_time = None
cntol = 0
events = defaultdict(list)

driver_dic, runtime_dic = correlation_cbid()

for r_event in collection.events:
    name = r_event.name
    event_time = r_event.timestamp
    w_event = btw.Event(event_classes[name])

    fields = r_event.field_list_with_scope(
        babeltrace.common.CTFScope.EVENT_FIELDS)
    w_event = btw.Event(event_classes[name])

    for f in fields:
        # print(name, f, r_event[f])
        w_event.payload(f).value = r_event[f]

    if "queue" not in name and ("cudaTracer:kernel" in name
                                or "cudaTracer:memcpy" in name):
        event_time = r_event["timestamp"] * 1000

    # organize threads
    threadId = r_event.field_with_scope(
event_class.add_field(enumeration_type, "enum_field")

# Create an array type
array_type = btw.ArrayFieldDeclaration(int10_type, 5)
event_class.add_field(array_type, "array_field")

# Create a sequence type
sequence_type = btw.SequenceFieldDeclaration(int32_type, "sequence_len")
event_class.add_field(uint16_type, "sequence_len")
event_class.add_field(sequence_type, "sequence_field")

stream_class.add_event_class(event_class)
stream = writer.create_stream(stream_class)

for i in range(100):
    event = btw.Event(event_class)

    clock.time = i * 1000
    structure_field = event.payload("structure_field")
    integer_field = structure_field.field("an_integer")
    integer_field.value = i

    string_field = structure_field.field("a_string_field")
    string_field.value = "Test string."

    float_field = event.payload("float_field")
    float_field.value = float(i) + (float(i) / 100.0)

    array_field = event.payload("array_field")
    for j in range(5):
        element = array_field.field(j)
Beispiel #10
0
 def partSwitch(self, pid):
     ClockManager().sample()
     e = btw.Event(self.partSwitchEventClass)
     e.payload("pid").value = pid
     self.stream.append_event(e)
Beispiel #11
0
 def syscall_entry(self, irq):
     ClockManager().sample()
     e = btw.Event(self.syscallEntryEventClass)
     e.payload("id").value = irq
     self.stream.append_event(e)
Beispiel #12
0
 def irq_exit(self, irq):
     ClockManager().sample()
     e = btw.Event(self.irqExitEventClass)
     e.payload("irq").value = irq
     self.stream.append_event(e)
Beispiel #13
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()