3 # The MIT License (MIT)
5 # Copyright (C) 2015 - Julien Desfossez <jdesfossez@efficios.com>
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 from babeltrace
import TraceCollection
, CTFScope
33 # quick fix for debian-based distros
34 sys
.path
.append("/usr/local/lib/python%d.%d/site-packages" %
35 (sys
.version_info
.major
, sys
.version_info
.minor
))
36 from babeltrace
import TraceCollection
, CTFScope
38 preambule
= """#!/usr/bin/env python3
44 NSEC_PER_SEC = 1000000000
47 from babeltrace import TraceCollection
49 # quick fix for debian-based distros
50 sys.path.append("/usr/local/lib/python%d.%d/site-packages" %
51 (sys.version_info.major, sys.version_info.minor))
52 from babeltrace import TraceCollection
56 def __init__(self, trace):
60 def ns_to_hour_nsec(self, ns):
61 d = time.localtime(ns/NSEC_PER_SEC)
62 return "%02d:%02d:%02d.%09d" % (d.tm_hour, d.tm_min, d.tm_sec,
66 # iterate over all the events
67 for event in self.trace.events:
68 if not event.name in self.event_count.keys():
69 self.event_count[event.name] = 0
70 method_name = "handle_%s" % \
71 event.name.replace(":", "_").replace("+", "_")
72 # call the function to handle each event individually
73 if hasattr(TraceParser, method_name):
74 func = getattr(TraceParser, method_name)
76 # print statistics after parsing the trace
77 print("Total event count:")
78 for e in self.event_count.keys():
79 print("- %s: %d" % (e, self.event_count[e]))
84 if __name__ == "__main__":
85 parser = argparse.ArgumentParser(description='Trace parser')
86 parser.add_argument('path', metavar="<path/to/trace>", help='Trace path')
87 args = parser.parse_args()
89 traces = TraceCollection()
90 handle = traces.add_traces_recursive(args.path, "ctf")
94 t = TraceParser(traces)
97 for h in handle.values():
98 traces.remove_trace(h)
102 def gen_parser(handle
, fd
, args
):
103 for h
in handle
.values():
104 for event
in h
.events
:
105 fmt_str
= "[%s] %s: { cpu_id = %s }, { "
106 fmt_fields
= "self.ns_to_hour_nsec(timestamp), event.name, " \
108 name
= event
.name
.replace(":", "_").replace("+", "_")
109 fd
.write(" def handle_%s(self, event):\n" % (name
))
110 fd
.write(" timestamp = event.timestamp\n")
111 fd
.write(" cpu_id = event[\"cpu_id\"]\n")
112 for field
in event
.fields
:
113 if field
.scope
== CTFScope
.EVENT_FIELDS
:
115 # some field names are reserved keywords/variables
122 fd
.write(" %s = event[\"%s\"]\n" % (fname
,
124 fmt_str
= fmt_str
+ field
.name
+ " = %s, "
125 fmt_fields
= fmt_fields
+ "%s, " % (fname
)
126 fd
.write("\n self.event_count[event.name] += 1\n")
128 fd
.write(" print(\"%s }\" %% (%s))\n\n" %
129 (fmt_str
[0:-2], fmt_fields
[0:-1]))
132 if __name__
== "__main__":
133 parser
= argparse
.ArgumentParser(description
='Trace parser generator')
134 parser
.add_argument('path', metavar
="<path/to/trace>", help='Trace path')
135 parser
.add_argument('-o', '--output', type=str,
136 metavar
="<output-script-name>",
137 help='Output script name')
138 parser
.add_argument('-q', '--quiet', action
="store_true",
139 help='Generate a quiet parser (no print)')
140 args
= parser
.parse_args()
142 traces
= TraceCollection()
143 handle
= traces
.add_traces_recursive(args
.path
, "ctf")
148 output
= "generated-parser.py"
152 fd
= open(output
, "w")
154 gen_parser(handle
, fd
, args
)
156 for h
in handle
.values():
157 traces
.remove_trace(h
)
160 os
.chmod(output
, stat
.S_IRUSR | stat
.S_IWUSR | stat
.S_IXUSR |
161 stat
.S_IRGRP | stat
.S_IXGRP |
162 stat
.S_IROTH | stat
.S_IXOTH
)
163 print("A trace parser for this trace has been written in", output
)
This page took 0.035 seconds and 6 git commands to generate.