Tests: Add event packet header accessors test
[deliverable/babeltrace.git] / bindings / python / examples / babeltrace_and_lttng.py
... / ...
CommitLineData
1#!/usr/bin/env python3
2# babeltrace_and_lttng.py
3#
4# Babeltrace and LTTng example script
5#
6# Copyright 2012 EfficiOS Inc.
7#
8# Author: Danny Serres <danny.serres@efficios.com>
9#
10# Permission is hereby granted, free of charge, to any person obtaining a copy
11# of this software and associated documentation files (the "Software"), to deal
12# in the Software without restriction, including without limitation the rights
13# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14# copies of the Software, and to permit persons to whom the Software is
15# furnished to do so, subject to the following conditions:
16#
17# The above copyright notice and this permission notice shall be included in
18# all copies or substantial portions of the Software.
19
20
21# This script uses both lttng-tools and babeltrace
22# python modules. It creates a session, enables
23# events, starts tracing for 2 seconds, stops tracing,
24# destroys the session and outputs the trace in the
25# specified output file.
26#
27# WARNING: will destroy any existing trace having
28# the same name as ses_name
29
30
31# ------------------------------------------------------
32ses_name = "babeltrace-lttng-test"
33trace_path = "/lttng-traces/babeltrace-lttng-trace/"
34out_file = "babeltrace-lttng-trace-text-output.txt"
35# ------------------------------------------------------
36
37
38import time
39try:
40 import babeltrace
41 import lttng
42except ImportError:
43 raise ImportError( "both babeltrace and lttng-tools "
44 "python modules must be installed")
45
46
47# Errors to raise if something goes wrong
48class LTTngError(Exception):
49 pass
50
51
52class BabeltraceError(Exception):
53 pass
54
55
56# LTTNG-TOOLS
57
58# Making sure session does not already exist
59lttng.destroy(ses_name)
60
61# Creating a new session and handle
62ret = lttng.create(ses_name, trace_path)
63if ret < 0:
64 raise LTTngError(lttng.strerror(ret))
65
66domain = lttng.Domain()
67domain.type = lttng.DOMAIN_KERNEL
68
69han = None
70han = lttng.Handle(ses_name, domain)
71if han is None:
72 raise LTTngError("Handle not created")
73
74
75# Enabling all events
76event = lttng.Event()
77event.type = lttng.EVENT_ALL
78event.loglevel_type = lttng.EVENT_LOGLEVEL_ALL
79ret = lttng.enable_event(han, event, None)
80if ret < 0:
81 raise LTTngError(lttng.strerror(ret))
82
83# Start, wait, stop
84ret = lttng.start(ses_name)
85if ret < 0:
86 raise LTTngError(lttng.strerror(ret))
87print("Tracing...")
88time.sleep(2)
89print("Stopped.")
90ret = lttng.stop(ses_name)
91if ret < 0:
92 raise LTTngError(lttng.strerror(ret))
93
94
95# Destroying tracing session
96ret = lttng.destroy(ses_name)
97if ret < 0:
98 raise LTTngError(lttng.strerror(ret))
99
100
101# BABELTRACE
102
103# Create TraceCollecion and add trace:
104traces = babeltrace.TraceCollection()
105ret = traces.add_trace(trace_path + "/kernel", "ctf")
106if ret is None:
107 raise BabeltraceError("Error adding trace")
108
109# Reading events from trace
110# and outputting timestamps and event names
111# in out_file
112print("Writing trace file...")
113output = open(out_file, "wt")
114
115for event in traces.events:
116 output.write("TS: {}, {} : {}\n".format(
117 event.timestamp, event.cycles, event.name))
118
119# Closing file
120output.close()
121
122print("Done.")
This page took 0.023392 seconds and 5 git commands to generate.