X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=bindings%2Fpython%2Fexamples%2Fbabeltrace_and_lttng.py;h=651fa2da982a81ec06fc0c0aaeb92f074a884935;hb=5df9e303fa9d81b3a6d989612c7a98039ec76fd1;hp=cfd611f68b24417de8e3f5e2033913146127e711;hpb=550461940505dbeae425d1b87d145549800a5341;p=babeltrace.git diff --git a/bindings/python/examples/babeltrace_and_lttng.py b/bindings/python/examples/babeltrace_and_lttng.py index cfd611f6..651fa2da 100644 --- a/bindings/python/examples/babeltrace_and_lttng.py +++ b/bindings/python/examples/babeltrace_and_lttng.py @@ -1,21 +1,29 @@ #!/usr/bin/env python3 # babeltrace_and_lttng.py -# +# # Babeltrace and LTTng example script -# +# # Copyright 2012 EfficiOS Inc. -# +# # Author: Danny Serres -# +# # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: -# +# # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. # This script uses both lttng-tools and babeltrace @@ -29,25 +37,28 @@ # ------------------------------------------------------ -ses_name = "babeltrace-lttng-test" -trace_path = "/lttng-traces/babeltrace-lttng-trace/" -out_file = "babeltrace-lttng-trace-text-output.txt" +ses_name = "babeltrace-lttng-test" +trace_path = "/lttng-traces/babeltrace-lttng-trace/" +out_file = "babeltrace-lttng-trace-text-output.txt" # ------------------------------------------------------ import time try: - import babeltrace, lttng + import babeltrace.reader + import lttng except ImportError: - raise ImportError( "both babeltrace and lttng-tools " - "python modules must be installed" ) + raise ImportError( "both babeltrace and lttng-tools " + "python modules must be installed") # Errors to raise if something goes wrong class LTTngError(Exception): - pass + pass + + class BabeltraceError(Exception): - pass + pass # LTTNG-TOOLS @@ -56,9 +67,9 @@ class BabeltraceError(Exception): lttng.destroy(ses_name) # Creating a new session and handle -ret = lttng.create(ses_name,trace_path) +ret = lttng.create(ses_name, trace_path) if ret < 0: - raise LTTngError(lttng.strerror(ret)) + raise LTTngError(lttng.strerror(ret)) domain = lttng.Domain() domain.type = lttng.DOMAIN_KERNEL @@ -66,7 +77,7 @@ domain.type = lttng.DOMAIN_KERNEL han = None han = lttng.Handle(ses_name, domain) if han is None: - raise LTTngError("Handle not created") + raise LTTngError("Handle not created") # Enabling all events @@ -75,37 +86,33 @@ event.type = lttng.EVENT_ALL event.loglevel_type = lttng.EVENT_LOGLEVEL_ALL ret = lttng.enable_event(han, event, None) if ret < 0: - raise LTTngError(lttng.strerror(ret)) + raise LTTngError(lttng.strerror(ret)) # Start, wait, stop ret = lttng.start(ses_name) if ret < 0: - raise LTTngError(lttng.strerror(ret)) + raise LTTngError(lttng.strerror(ret)) print("Tracing...") time.sleep(2) print("Stopped.") ret = lttng.stop(ses_name) if ret < 0: - raise LTTngError(lttng.strerror(ret)) + raise LTTngError(lttng.strerror(ret)) # Destroying tracing session ret = lttng.destroy(ses_name) if ret < 0: - raise LTTngError(lttng.strerror(ret)) + raise LTTngError(lttng.strerror(ret)) # BABELTRACE -# Create context and add trace: -ctx = babeltrace.Context() -ret = ctx.add_trace(trace_path + "/kernel", "ctf") +# Create TraceCollecion and add trace: +traces = babeltrace.reader.TraceCollection() +ret = traces.add_trace(trace_path + "/kernel", "ctf") if ret is None: - raise BabeltraceError("Error adding trace") - -# Iterator setup -bp = babeltrace.IterPos(babeltrace.SEEK_BEGIN) -ctf_it = babeltrace.ctf.Iterator(ctx,bp) + raise BabeltraceError("Error adding trace") # Reading events from trace # and outputting timestamps and event names @@ -113,20 +120,11 @@ ctf_it = babeltrace.ctf.Iterator(ctx,bp) print("Writing trace file...") output = open(out_file, "wt") -event = ctf_it.read_event() -while(event is not None): - output.write("TS: {}, {} : {}\n".format(event.get_timestamp(), - event.get_cycles(), event.get_name())) - - # Next event - ret = ctf_it.next() - if ret < 0: - break - event = ctf_it.read_event() +for event in traces.events: + output.write("TS: {}, {} : {}\n".format( + event.timestamp, event.cycles, event.name)) # Closing file output.close() -# Destroying dynamic elements -del ctf_it, han print("Done.")