aboutsummaryrefslogtreecommitdiff
path: root/tests/test_base.py
blob: e1fec6d1e1cf8f44b632ee2f5663e23ac3c8c91d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#    Copyright 2015-2017 ARM Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


import os
import sys
import unittest
import utils_tests
import trappy
from trappy.base import trace_parser_explode_array

sys.path.append(os.path.join(utils_tests.TESTS_DIRECTORY, "..", "trappy"))

class TestBaseMethods(unittest.TestCase):
    """Test simple methods that don't need to set up a directory"""
    def test_trace_parser_explode_array(self):
        """TestBaseMethods: Basic test of trace_parser_explode_array()"""

        line = "cpus=0000000f freq=1400000 raw_cpu_power=189 load={3 2 12 2} power=14"
        expected = "cpus=0000000f freq=1400000 raw_cpu_power=189 load0=3 load1=2 load2=12 load3=2 power=14"
        array_lengths = {"load": 4}

        result = trace_parser_explode_array(line, array_lengths)
        self.assertEquals(result, expected)

    def test_trace_parser_explode_array_nop(self):
        """TestBaseMethods: trace_parser_explode_array() returns the same string if there's no array in it"""

        line = "cpus=0000000f freq=1400000 raw_cpu_power=189 load0=3 load1=2 load2=12 load3=2 power=14"
        array_lengths = {"load": 0}

        result = trace_parser_explode_array(line, array_lengths)
        self.assertEquals(result, line)

    def test_trace_parser_explode_array_2(self):
        """TestBaseMethods: trace_parser_explode_array() works if there's two arrays in the string"""

        line = "cpus=0000000f freq=1400000 load={3 2 12 2} power=14 req_power={10 7 2 34}"
        expected = "cpus=0000000f freq=1400000 load0=3 load1=2 load2=12 load3=2 power=14 req_power0=10 req_power1=7 req_power2=2 req_power3=34"
        array_lengths = {'load': 4, 'req_power': 4}

        result = trace_parser_explode_array(line, array_lengths)
        self.assertEquals(result, expected)

    def test_trace_parser_explode_array_diff_lengths(self):
        """TestBaseMethods: trace_parser_explode_array() expands arrays that are shorter than the expected length

        trace_parser_explode_array() has to be able to deal with an array of
        size 2 if we tell it in other parts of the trace it is four.

        """

        line = "cpus=0000000f freq=1400000 load={3 2} power=14"
        expected = "cpus=0000000f freq=1400000 load0=3 load1=2 load2=0 load3=0 power=14"
        array_lengths = {'load': 4}

        result = trace_parser_explode_array(line, array_lengths)
        self.assertEquals(result, expected)

class TestBase(utils_tests.SetupDirectory):
    """Incomplete tests for the Base class"""

    def __init__(self, *args, **kwargs):
        super(TestBase, self).__init__(
             [("../doc/trace.txt", "trace.txt"),
              ("trace_equals.txt", "trace_equals.txt")],
             *args,
             **kwargs)

    def test_parse_empty_array(self):
        """TestBase: Trace with empty array creates a valid DataFrame"""

        in_data = """     kworker/4:1-397   [004]   720.741315: thermal_power_cpu_get: cpus=000000f0 freq=1900000 raw_cpu_power=1259 load={} power=61
     kworker/4:1-397   [004]   720.741349: thermal_power_cpu_get: cpus=0000000f freq=1400000 raw_cpu_power=189 load={} power=14"""

        expected_columns = set(["__comm", "__pid", "__tgid", "__cpu", "cpus", "freq",
                                "raw_cpu_power", "power"])

        with open("trace.txt", "w") as fout:
            fout.write(in_data)

        trace = trappy.FTrace()
        dfr = trace.cpu_in_power.data_frame

        self.assertEquals(set(dfr.columns), expected_columns)
        self.assertEquals(dfr["power"].iloc[0], 61)

    def test_parse_special_fields(self):
        """TestBase: Task name, PID, CPU and timestamp are properly paresed """

        events = {
                1001.456789 : { 'task': 'rcu_preempt',       'pid': 1123, 'cpu': 001 },
                1002.456789 : { 'task': 'rs:main',           'pid': 2123, 'cpu': 002 },
                1003.456789 : { 'task': 'AsyncTask #1',      'pid': 3123, 'cpu': 003 },
                1004.456789 : { 'task': 'kworker/1:1H',      'pid': 4123, 'cpu': 004 },
                1005.456789 : { 'task': 'jbd2/sda2-8',       'pid': 5123, 'cpu': 005 },
                1006.456789 : { 'task': 'IntentService[',    'pid': 6123, 'cpu': 005 },
                1006.456789 : { 'task': r'/system/bin/.s$_?.u- \a]}c\./ef[.12]*[[l]in]ger',
                                'pid': 1234, 'cpu': 666 },
        }

        in_data = """"""
        for timestamp in sorted(events):
            in_data+="{0:>16s}-{1:d} [{2:04d}] {3}: event0:   tag=value\n".\
                    format(
                        events[timestamp]['task'],
                        events[timestamp]['pid'],
                        events[timestamp]['cpu'],
                        timestamp
                        )

        expected_columns = set(["__comm", "__pid", "__tgid", "__cpu", "tag"])

        with open("trace.txt", "w") as fout:
            fout.write(in_data)

        ftrace_parser = trappy.register_dynamic_ftrace("Event0", "event0", scope="sched")
        trace = trappy.FTrace(normalize_time=False)
        dfr = trace.event0.data_frame

        self.assertEquals(set(dfr.columns), expected_columns)

        for timestamp, event in events.iteritems():
            self.assertEquals(dfr["__comm"].loc[timestamp], event['task'])
            self.assertEquals(dfr["__pid"].loc[timestamp],  event['pid'])
            self.assertEquals(dfr["__cpu"].loc[timestamp],  event['cpu'])

        trappy.unregister_dynamic_ftrace(ftrace_parser)


    def test_parse_values_concatenation(self):
        """TestBase: Trace with space separated values created a valid DataFrame"""

        in_data = """     rcu_preempt-7     [000]    73.604532: my_sched_stat_runtime:   comm=Space separated taskname pid=7 runtime=262875 [ns] vruntime=17096359856 [ns]"""

        expected_columns = set(["__comm", "__pid", "__tgid", "__cpu", "comm", "pid", "runtime", "vruntime"])

        with open("trace.txt", "w") as fout:
            fout.write(in_data)

        ftrace_parser = trappy.register_dynamic_ftrace("sched_stat_runtime",
                                       "my_sched_stat_runtime", scope="sched")
        trace = trappy.FTrace()
        dfr = trace.sched_stat_runtime.data_frame

        self.assertEquals(set(dfr.columns), expected_columns)
        self.assertEquals(dfr["comm"].iloc[0], "Space separated taskname")
        self.assertEquals(dfr["pid"].iloc[0], 7)
        self.assertEquals(dfr["runtime"].iloc[0], 262875)
        self.assertEquals(dfr["vruntime"].iloc[0], 17096359856)

        trappy.unregister_dynamic_ftrace(ftrace_parser)

    def test_get_dataframe(self):
        """TestBase: Thermal.data_frame["thermal_zone"] exists and
           it contains a known value"""
        dfr = trappy.FTrace().thermal.data_frame

        self.assertTrue("thermal_zone" in dfr.columns)
        self.assertEquals(dfr["temp"].iloc[0], 68786)

    def test_write_csv(self):
        """TestBase: Base::write_csv() creates a valid csv"""
        from csv import DictReader

        fname = "thermal.csv"
        trappy.FTrace().thermal.write_csv(fname)

        with open(fname) as fin:
            csv_reader = DictReader(fin)

            self.assertTrue("Time" in csv_reader.fieldnames)
            self.assertTrue("temp" in csv_reader.fieldnames)

            first_data = csv_reader.next()
            self.assertEquals(first_data["Time"], "0.0")
            self.assertEquals(first_data["temp"], "68786")

    def test_normalize_time(self):
        """TestBase: Base::normalize_time() normalizes the time of the trace"""
        thrm = trappy.FTrace().thermal

        last_prev_time = thrm.data_frame.index[-1]

        basetime = thrm.data_frame.index[0]
        thrm.normalize_time(basetime)

        last_time = thrm.data_frame.index[-1]
        expected_last_time = last_prev_time - basetime

        self.assertEquals(round(thrm.data_frame.index[0], 7), 0)
        self.assertEquals(round(last_time - expected_last_time, 7), 0)

    def test_equals_in_field_value(self):
        """TestBase: Can parse events with fields with values containing '='"""
        trace = trappy.FTrace("trace_equals.txt", events=['equals_event'])

        df = trace.equals_event.data_frame
        self.assertSetEqual(set(df.columns),
                            set(["__comm", "__pid", "__tgid", "__cpu", "my_field"]))
        self.assertListEqual(df["my_field"].tolist(),
                             ["foo", "foo=bar", "foo=bar=baz", 1,
                              "1=2", "1=foo", "1foo=2"])