Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 | 2 |
| 3 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 import unittest | 7 import unittest |
| 8 | 8 import logging |
| 9 from systrace import systrace | 9 from systrace import run_systrace |
| 10 from systrace.agents import ftrace_agent | 10 from systrace.tracing_agents import ftrace_agent |
| 11 | 11 |
| 12 SYSTRACE_HOST_CMD_DEFAULT = ['./systrace.py', '--target=linux'] | 12 SYSTRACE_HOST_CMD_DEFAULT = ['./systrace.py', '--target=linux'] |
| 13 FT_DIR = "/sys/kernel/debug/tracing/" | 13 FT_DIR = "/sys/kernel/debug/tracing/" |
| 14 FT_EVENT_DIR = FT_DIR + "events/" | 14 FT_EVENT_DIR = FT_DIR + "events/" |
| 15 FT_TRACE_ON = FT_DIR + "tracing_on" | 15 FT_TRACE_ON = FT_DIR + "tracing_on" |
| 16 FT_TRACE = FT_DIR + "trace" | 16 FT_TRACE = FT_DIR + "trace" |
| 17 FT_BUFFER_SIZE = FT_DIR + "buffer_size_kb" | 17 FT_BUFFER_SIZE = FT_DIR + "buffer_size_kb" |
| 18 | 18 |
| 19 | 19 |
| 20 def make_test_io_interface(permitted_files): | 20 def make_test_io_interface(permitted_files): |
| 21 class TestIoImpl(object): | 21 class TestIoImpl(object): |
| 22 | 22 |
| 23 @staticmethod | 23 @staticmethod |
| 24 def writeFile(path, data): | 24 def writeFile(path, data): |
| 25 permitted_files[path] = data | 25 permitted_files[path] = data |
| 26 | 26 |
| 27 @staticmethod | 27 @staticmethod |
| 28 def readFile(path): | 28 def readFile(path): |
| 29 if path in permitted_files: | 29 if path in permitted_files: |
| 30 return permitted_files[path] | 30 return permitted_files[path] |
| 31 else: | 31 else: |
| 32 return "" | 32 return "" |
| 33 | 33 |
| 34 @staticmethod | 34 @staticmethod |
| 35 def haveWritePermissions(path): | 35 def haveWritePermissions(path): |
| 36 return path in permitted_files | 36 return path in permitted_files |
| 37 | |
| 38 @staticmethod | |
| 39 def checkedWriteFile(path, data): | |
|
Zhen Wang
2016/03/30 16:14:08
nit: checkAndWriteFile
alexandermont
2016/03/30 19:33:25
Done
| |
| 40 permitted_files[path] = data | |
| 41 | |
| 37 return TestIoImpl | 42 return TestIoImpl |
| 38 | 43 |
| 39 | 44 |
| 40 class FtraceAgentTest(unittest.TestCase): | 45 class FtraceAgentTest(unittest.TestCase): |
| 41 | 46 |
| 42 def test_avail_categories(self): | 47 def test_avail_categories(self): |
| 43 # sched only has required events | 48 # sched only has required events |
| 44 permitted_files = { | 49 permitted_files = { |
| 45 FT_EVENT_DIR + "sched/sched_switch/enable": "0", | 50 FT_EVENT_DIR + "sched/sched_switch/enable": "0", |
| 46 FT_EVENT_DIR + "sched/sched_wakeup/enable": "0" | 51 FT_EVENT_DIR + "sched/sched_wakeup/enable": "0" |
| 47 } | 52 } |
| 48 io_interface = make_test_io_interface(permitted_files) | 53 io_interface = make_test_io_interface(permitted_files) |
| 49 options, categories = systrace.parse_options(SYSTRACE_HOST_CMD_DEFAULT) | 54 agent = ftrace_agent.FtraceAgent(io_interface) |
| 50 agent = ftrace_agent.FtraceAgent(options, categories, io_interface) | |
| 51 self.assertEqual(['sched'], agent._avail_categories()) | 55 self.assertEqual(['sched'], agent._avail_categories()) |
| 52 | 56 |
| 53 # check for no available categories | 57 # check for no available categories |
| 54 permitted_files = {} | 58 permitted_files = {} |
| 55 io_interface = make_test_io_interface(permitted_files) | 59 io_interface = make_test_io_interface(permitted_files) |
| 56 options, categories = systrace.parse_options(SYSTRACE_HOST_CMD_DEFAULT) | 60 agent = ftrace_agent.FtraceAgent(io_interface) |
| 57 agent = ftrace_agent.FtraceAgent(options, categories, io_interface) | |
| 58 self.assertEqual([], agent._avail_categories()) | 61 self.assertEqual([], agent._avail_categories()) |
| 59 | 62 |
| 60 # block has some required, some optional events | 63 # block has some required, some optional events |
| 61 permitted_files = { | 64 permitted_files = { |
| 62 FT_EVENT_DIR + "block/block_rq_complete/enable": "0", | 65 FT_EVENT_DIR + "block/block_rq_complete/enable": "0", |
| 63 FT_EVENT_DIR + "block/block_rq_issue/enable": "0" | 66 FT_EVENT_DIR + "block/block_rq_issue/enable": "0" |
| 64 } | 67 } |
| 65 io_interface = make_test_io_interface(permitted_files) | 68 io_interface = make_test_io_interface(permitted_files) |
| 66 options, categories = systrace.parse_options(SYSTRACE_HOST_CMD_DEFAULT) | 69 agent = ftrace_agent.FtraceAgent(io_interface) |
| 67 agent = ftrace_agent.FtraceAgent(options, categories, io_interface) | |
| 68 self.assertEqual(['disk'], agent._avail_categories()) | 70 self.assertEqual(['disk'], agent._avail_categories()) |
| 69 | 71 |
| 70 def test_tracing_bootstrap(self): | 72 def test_tracing_bootstrap(self): |
| 71 workq_event_path = FT_EVENT_DIR + "workqueue/enable" | 73 workq_event_path = FT_EVENT_DIR + "workqueue/enable" |
| 72 permitted_files = { | 74 permitted_files = { |
| 73 workq_event_path: "0", | 75 workq_event_path: "0", |
| 74 FT_TRACE: "x" | 76 FT_TRACE: "x" |
| 75 } | 77 } |
| 76 io_interface = make_test_io_interface(permitted_files) | 78 io_interface = make_test_io_interface(permitted_files) |
| 77 systrace_cmd = SYSTRACE_HOST_CMD_DEFAULT + ["workq"] | 79 systrace_cmd = SYSTRACE_HOST_CMD_DEFAULT + ["workq"] |
| 78 options, categories = systrace.parse_options(systrace_cmd) | 80 options, categories = run_systrace.parse_options(systrace_cmd) |
| 79 agent = ftrace_agent.FtraceAgent(options, categories, io_interface) | 81 agent = ftrace_agent.FtraceAgent(io_interface) |
| 80 self.assertEqual(['workq'], agent._avail_categories()) | 82 self.assertEqual(['workq'], agent._avail_categories()) |
| 81 | 83 |
| 82 # confirm tracing is enabled, buffer is cleared | 84 # confirm tracing is enabled, buffer is cleared |
| 83 agent.start() | 85 agent.StartAgentTracing(options, categories) |
| 84 self.assertEqual(permitted_files[FT_TRACE_ON], "1") | 86 self.assertEqual(permitted_files[FT_TRACE_ON], "1") |
| 85 self.assertEqual(permitted_files[FT_TRACE], "") | 87 self.assertEqual(permitted_files[FT_TRACE], "") |
| 86 | 88 |
| 87 # fill in file with dummy contents | 89 # fill in file with dummy contents |
| 88 dummy_trace = "trace_contents" | 90 dummy_trace = "trace_contents" |
| 89 permitted_files[FT_TRACE] = dummy_trace | 91 permitted_files[FT_TRACE] = dummy_trace |
| 90 | 92 |
| 91 # confirm tracing is disabled | 93 # confirm tracing is disabled |
| 92 agent.collect_result() | 94 agent.StopAgentTracing() |
| 93 self.assertEqual(permitted_files[FT_TRACE_ON], "0") | 95 self.assertEqual(permitted_files[FT_TRACE_ON], "0") |
| 94 | 96 |
| 95 # confirm trace is expected, and read from fs | 97 # confirm trace is expected, and read from fs |
| 96 self.assertTrue(agent.expect_trace()) | 98 self.assertEqual(agent.GetResults().trace_data, dummy_trace) |
| 97 self.assertEqual(agent.get_trace_data(), dummy_trace) | |
| 98 | 99 |
| 99 # confirm buffer size is reset to 1 | 100 # confirm buffer size is reset to 1 |
| 100 self.assertEqual(permitted_files[FT_BUFFER_SIZE], "1") | 101 self.assertEqual(permitted_files[FT_BUFFER_SIZE], "1") |
| 101 | 102 |
| 102 def test_tracing_event_enable_disable(self): | 103 def test_tracing_event_enable_disable(self): |
| 103 # turn on irq tracing | 104 # turn on irq tracing |
| 104 ipi_event_path = FT_EVENT_DIR + "ipi/enable" | 105 ipi_event_path = FT_EVENT_DIR + "ipi/enable" |
| 105 irq_event_path = FT_EVENT_DIR + "irq/enable" | 106 irq_event_path = FT_EVENT_DIR + "irq/enable" |
| 106 permitted_files = { | 107 permitted_files = { |
| 107 ipi_event_path: "0", | 108 ipi_event_path: "0", |
| 108 irq_event_path: "0" | 109 irq_event_path: "0" |
| 109 } | 110 } |
| 110 io_interface = make_test_io_interface(permitted_files) | 111 io_interface = make_test_io_interface(permitted_files) |
| 111 systrace_cmd = SYSTRACE_HOST_CMD_DEFAULT + ["irq"] | 112 systrace_cmd = SYSTRACE_HOST_CMD_DEFAULT + ["irq"] |
| 112 options, categories = systrace.parse_options(systrace_cmd) | 113 options, categories = run_systrace.parse_options(systrace_cmd) |
| 113 agent = ftrace_agent.FtraceAgent(options, categories, io_interface) | 114 agent = ftrace_agent.FtraceAgent(io_interface) |
| 114 self.assertEqual(['irq'], agent._avail_categories()) | 115 self.assertEqual(['irq'], agent._avail_categories()) |
| 115 | 116 |
| 116 # confirm all the event nodes are turned on during tracing | 117 # confirm all the event nodes are turned on during tracing |
| 117 agent.start() | 118 agent.StartAgentTracing(options, categories) |
| 118 self.assertEqual(permitted_files[irq_event_path], "1") | 119 self.assertEqual(permitted_files[irq_event_path], "1") |
| 119 self.assertEqual(permitted_files[ipi_event_path], "1") | 120 self.assertEqual(permitted_files[ipi_event_path], "1") |
| 120 | 121 |
| 121 # and then turned off when completed. | 122 # and then turned off when completed. |
| 122 agent.collect_result() | 123 agent.StopAgentTracing() |
| 123 self.assertEqual(permitted_files[irq_event_path], "0") | 124 self.assertEqual(permitted_files[irq_event_path], "0") |
| 124 self.assertEqual(permitted_files[ipi_event_path], "0") | 125 self.assertEqual(permitted_files[ipi_event_path], "0") |
| 125 | 126 |
| 126 def test_trace_time(self): | |
| 127 systrace_cmd = SYSTRACE_HOST_CMD_DEFAULT + ['-t', '10'] | |
| 128 options, categories = systrace.parse_options(systrace_cmd) | |
| 129 agent = ftrace_agent.FtraceAgent(options, categories) | |
| 130 self.assertEqual(agent._get_trace_time(), 10) | |
| 131 | |
| 132 def test_buffer_size(self): | 127 def test_buffer_size(self): |
| 133 systrace_cmd = SYSTRACE_HOST_CMD_DEFAULT + ['-b', '16000'] | 128 systrace_cmd = SYSTRACE_HOST_CMD_DEFAULT + ['-b', '16000'] |
| 134 options, categories = systrace.parse_options(systrace_cmd) | 129 options, _ = run_systrace.parse_options(systrace_cmd) |
| 135 agent = ftrace_agent.FtraceAgent(options, categories) | 130 self.assertEqual(ftrace_agent._get_trace_buffer_size(options), 16000) |
| 136 self.assertEqual(agent._get_trace_buffer_size(), 16000) | 131 |
| 132 if __name__ == "__main__": | |
| 133 logging.getLogger().setLevel(logging.DEBUG) | |
| 134 unittest.main(verbosity=2) | |
| OLD | NEW |