1# Copyright 2015-2017 ARM Limited 2# 3# 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 16 17import unittest 18import matplotlib 19from test_sched import BaseTestSched 20from trappy.base import Base 21import trappy 22 23 24class DynamicEvent(Base): 25 26 """Test the ability to register 27 specific classes to trappy""" 28 29 unique_word = "dynamic_test_key" 30 name = "dynamic_event" 31 32 33class TestDynamicEvents(BaseTestSched): 34 35 def __init__(self, *args, **kwargs): 36 super(TestDynamicEvents, self).__init__(*args, **kwargs) 37 38 def test_dynamic_data_frame(self): 39 """ 40 Test if the dynamic events are populated 41 in the data frame 42 """ 43 parse_class = trappy.register_dynamic_ftrace("DynamicEvent", "dynamic_test_key") 44 t = trappy.FTrace(name="first") 45 self.assertTrue(len(t.dynamic_event.data_frame) == 1) 46 47 trappy.unregister_dynamic_ftrace(parse_class) 48 49 def test_dynamic_class_attr(self): 50 """ 51 Test the attibutes of the dynamically 52 generated class 53 """ 54 cls = trappy.register_dynamic_ftrace("DynamicEvent", "dynamic_test_key", 55 pivot="test_pivot") 56 self.assertEquals(cls.__name__, "DynamicEvent") 57 self.assertEquals(cls.name, "dynamic_event") 58 self.assertEquals(cls.unique_word, "dynamic_test_key") 59 self.assertEquals(cls.pivot, "test_pivot") 60 61 trappy.unregister_dynamic_ftrace(cls) 62 63 def test_dynamic_event_plot(self): 64 """Test if plotter can accept a dynamic class 65 for a template argument""" 66 67 cls = trappy.register_dynamic_ftrace("DynamicEvent", "dynamic_test_key") 68 t = trappy.FTrace(name="first") 69 l = trappy.LinePlot(t, cls, column="load") 70 l.view(test=True) 71 72 trappy.unregister_dynamic_ftrace(cls) 73 74 def test_dynamic_event_scope(self): 75 """Test the case when an "all" scope class is 76 registered. it should appear in both thermal and sched 77 ftrace class definitions when scoped ftrace objects are created 78 """ 79 cls = trappy.register_dynamic_ftrace("DynamicEvent", "dynamic_test_key") 80 t1 = trappy.FTrace(name="first") 81 self.assertTrue(t1.class_definitions.has_key(cls.name)) 82 83 trappy.unregister_dynamic_ftrace(cls) 84 85 def test_register_ftrace_parser(self): 86 trappy.register_ftrace_parser(DynamicEvent) 87 t = trappy.FTrace(name="first") 88 self.assertTrue(len(t.dynamic_event.data_frame) == 1) 89 90 trappy.unregister_ftrace_parser(DynamicEvent) 91 92 def test_no_none_pivot(self): 93 """register_dynamic_ftrace() with default value for pivot doesn't create a class with a pivot=None""" 94 cls = trappy.register_dynamic_ftrace("MyEvent", "my_dyn_test_key") 95 self.assertFalse(hasattr(cls, "pivot")) 96 97 trappy.unregister_dynamic_ftrace(cls) 98 99 def test_unregister_dynamic_ftrace(self): 100 """Test that dynamic events can be unregistered""" 101 dyn_event = trappy.register_dynamic_ftrace("DynamicEvent", 102 "dynamic_test_key") 103 trace = trappy.FTrace(name="first") 104 self.assertTrue(len(trace.dynamic_event.data_frame) == 1) 105 106 trappy.unregister_dynamic_ftrace(dyn_event) 107 trace = trappy.FTrace(name="first") 108 109 self.assertFalse(hasattr(trace, "dynamic_event")) 110 111 dyn_event = trappy.register_dynamic_ftrace("DynamicEvent", 112 "dynamic_test_key", 113 scope="sched") 114 trace = trappy.FTrace(name="first") 115 self.assertTrue(len(trace.dynamic_event.data_frame) == 1) 116 117 trappy.unregister_dynamic_ftrace(dyn_event) 118 trace = trappy.FTrace(name="first") 119 120 self.assertFalse(hasattr(trace, "dynamic_event")) 121 122 def test_unregister_ftrace_parser(self): 123 """unregister_ftrace_parser() works""" 124 trappy.register_ftrace_parser(DynamicEvent) 125 trappy.unregister_ftrace_parser(DynamicEvent) 126 trace = trappy.FTrace() 127 self.assertFalse(hasattr(trace, "dynamic_event")) 128