blob: 5b094ce5cd6a3457782e55c5dae175ea84911bec [file] [log] [blame]
Brendan Jackmane81fdcb2017-01-04 17:10:29 +00001# Copyright 2015-2017 ARM Limited
Kapileshwar Singhfb8fa1a2015-02-11 17:21:04 +00002#
Javi Merinoaace7c02015-08-10 14:10:47 +01003# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15
Kapileshwar Singhfb8fa1a2015-02-11 17:21:04 +000016
17"""The idea is to create a wrapper class that
18returns a Type of a Class dynamically created based
19on the input parameters. Similar to a factory design
20pattern
21"""
Javi Merino435457c2015-08-10 15:59:10 +010022from trappy.base import Base
Kapileshwar Singhfb8fa1a2015-02-11 17:21:04 +000023import re
Javi Merino299725a2016-03-22 12:29:11 +000024from trappy.ftrace import GenericFTrace
Kapileshwar Singhfb8fa1a2015-02-11 17:21:04 +000025
26
Javi Merino6f34d902015-02-21 11:39:09 +000027def default_init(self):
Kapileshwar Singhfb8fa1a2015-02-11 17:21:04 +000028 """Default Constructor for the
Kapileshwar Singh46b361a2015-09-09 14:51:09 +010029 Dynamic MetaClass. This is used for
30 the dynamic object creation in
31 :mod:`trappy.dynamic.DynamicTypeFactory`
Kapileshwar Singhfb8fa1a2015-02-11 17:21:04 +000032 """
33
Javi Merinoc4fbc542015-11-26 10:20:01 +000034 kwords = {}
35
36 try:
37 kwords["parse_raw"] = self.parse_raw
38 except AttributeError:
39 pass
40
41 super(type(self), self).__init__(**kwords)
Kapileshwar Singhfb8fa1a2015-02-11 17:21:04 +000042
43
44class DynamicTypeFactory(type):
45
46 """Override the type class to create
Kapileshwar Singh46b361a2015-09-09 14:51:09 +010047 a dynamic type on the fly. This Factory
48 class is used internally by
Javi Merinofecb2672015-12-21 18:34:57 +000049 :mod:`trappy.dynamic.register_dynamic_ftrace`
Kapileshwar Singhfb8fa1a2015-02-11 17:21:04 +000050 """
51
52 def __new__(mcs, name, bases, dct):
53 """Override the new method"""
54 return type.__new__(mcs, name, bases, dct)
55
56 def __init__(cls, name, bases, dct):
57 """Override the constructor"""
58 super(DynamicTypeFactory, cls).__init__(name, bases, dct)
59
60
61def _get_name(name):
62 """Internal Method to Change camelcase to
Kapileshwar Singh46b361a2015-09-09 14:51:09 +010063 underscores. CamelCase -> camel_case
Kapileshwar Singhfb8fa1a2015-02-11 17:21:04 +000064 """
65 return re.sub('(?!^)([A-Z]+)', r'_\1', name).lower()
66
67
Javi Merinofecb2672015-12-21 18:34:57 +000068def register_dynamic_ftrace(class_name, unique_word, scope="all",
69 parse_raw=False, pivot=None):
Javi Merino299725a2016-03-22 12:29:11 +000070 """Create a Dynamic FTrace parser and register it with any FTrace parsing classes
Kapileshwar Singh46b361a2015-09-09 14:51:09 +010071
72 :param class_name: The name of the class to be registered
73 (Should be in CamelCase)
74 :type class_name: str
75
76 :param unique_word: The unique_word to be matched in the
77 trace
78 :type unique_word: str
79
80 :param scope: Registry Scope (Can be used to constrain
81 the parsing of events and group them together)
82 :type scope: str
83
Chris Redpathef596e52017-06-17 17:24:12 +010084 :param parse_raw: If, true, raw trace output (-r flag)
Kapileshwar Singh46b361a2015-09-09 14:51:09 +010085 will be used
86 :type parse_raw: bool
87
Kapileshwar Singh8aab6932015-11-19 14:51:22 +000088 :param pivot: The data column about which the data can be grouped
89 :type pivot: str
90
Kapileshwar Singh46b361a2015-09-09 14:51:09 +010091 For example if a new unique word :code:`my_unique_word` has
92 to be registered with TRAPpy:
93 ::
94
95 import trappy
Javi Merinofecb2672015-12-21 18:34:57 +000096 custom_class = trappy.register_dynamic_ftrace("MyEvent", "my_unique_word")
Javi Merinoc26a3232015-12-11 18:00:30 +000097 trace = trappy.FTrace("/path/to/trace_file")
Kapileshwar Singh46b361a2015-09-09 14:51:09 +010098
Javi Merinoc26a3232015-12-11 18:00:30 +000099 # New data member created in the ftrace object
100 trace.my_event
Kapileshwar Singh46b361a2015-09-09 14:51:09 +0100101
102 .. note:: The name of the member is :code:`my_event` from **MyEvent**
103
104
105 :return: A class object of type :mod:`trappy.base.Base`
106 """
Kapileshwar Singhfb8fa1a2015-02-11 17:21:04 +0000107
Javi Merino1b4aa0c2015-12-02 12:24:18 +0000108 kwords = {
Kapileshwar Singhfb8fa1a2015-02-11 17:21:04 +0000109 "__init__": default_init,
110 "unique_word": unique_word,
Kapileshwar Singh7a709132015-03-27 17:45:22 +0000111 "name": _get_name(class_name),
Kapileshwar Singh8aab6932015-11-19 14:51:22 +0000112 "parse_raw" : parse_raw,
Kapileshwar Singhfb8fa1a2015-02-11 17:21:04 +0000113 }
Javi Merino1b4aa0c2015-12-02 12:24:18 +0000114
115 if pivot:
116 kwords["pivot"] = pivot
117
118 dyn_class = DynamicTypeFactory(class_name, (Base,), kwords)
Javi Merino299725a2016-03-22 12:29:11 +0000119 GenericFTrace.register_parser(dyn_class, scope)
Kapileshwar Singhfb8fa1a2015-02-11 17:21:04 +0000120 return dyn_class
121
122
Javi Merino094e7422016-03-22 11:53:55 +0000123def register_ftrace_parser(cls, scope="all"):
Javi Merinofecb2672015-12-21 18:34:57 +0000124 """Register a new FTrace parser class implementation
125
126 Should be used when the class has complex helper methods and does
127 not expect to use the default constructor.
Kapileshwar Singh46b361a2015-09-09 14:51:09 +0100128
129 :param cls: The class to be registered for
130 enabling the parsing of an event in trace
131 :type cls: :mod:`trappy.base.Base`
Javi Merino094e7422016-03-22 11:53:55 +0000132
133 :param scope: scope of this parser class. The scope can be used
134 to restrict the parsing done on an individual file. Currently
135 the only scopes available are "sched", "thermal" or "all"
136 :type scope: string
137
Kapileshwar Singhfb8fa1a2015-02-11 17:21:04 +0000138 """
139
140 # Check the argspec of the class
Javi Merino299725a2016-03-22 12:29:11 +0000141 GenericFTrace.register_parser(cls, scope)
Javi Merinob280b4f2015-12-22 14:53:24 +0000142
143def unregister_ftrace_parser(ftrace_parser):
144 """Unregister an ftrace parser
145
146 :param ftrace_parser: An ftrace parser class that was registered
147 with register_ftrace_parser() or register_dynamic_ftrace().
148 If done with the latter, the cls parameter is the return value
149 of register_dynamic_ftrace()
150 :type ftrace_parser: class derived from :mod:`trappy.base.Base`
151
152 """
Javi Merino299725a2016-03-22 12:29:11 +0000153 GenericFTrace.unregister_parser(ftrace_parser)