def test_avail_categories(self):
        # sched only has required events
        permitted_files = {
            FT_EVENT_DIR + "sched/sched_switch/enable": "0",
            FT_EVENT_DIR + "sched/sched_wakeup/enable": "0",
        }
        io_interface = make_test_io_interface(permitted_files)
        options, categories = systrace.parse_options(SYSTRACE_HOST_CMD_DEFAULT)
        agent = ftrace_agent.FtraceAgent(options, categories, io_interface)
        self.assertEqual(["sched"], agent._avail_categories())

        # check for no available categories
        permitted_files = {}
        io_interface = make_test_io_interface(permitted_files)
        options, categories = systrace.parse_options(SYSTRACE_HOST_CMD_DEFAULT)
        agent = ftrace_agent.FtraceAgent(options, categories, io_interface)
        self.assertEqual([], agent._avail_categories())

        # block has some required, some optional events
        permitted_files = {
            FT_EVENT_DIR + "block/block_rq_complete/enable": "0",
            FT_EVENT_DIR + "block/block_rq_issue/enable": "0",
        }
        io_interface = make_test_io_interface(permitted_files)
        options, categories = systrace.parse_options(SYSTRACE_HOST_CMD_DEFAULT)
        agent = ftrace_agent.FtraceAgent(options, categories, io_interface)
        self.assertEqual(["disk"], agent._avail_categories())
    def test_avail_categories(self):
        # sched only has required events
        permitted_files = {
            FT_EVENT_DIR + "sched/sched_switch/enable": "0",
            FT_EVENT_DIR + "sched/sched_wakeup/enable": "0"
        }
        io_interface = make_test_io_interface(permitted_files)
        options, categories = systrace.parse_options(SYSTRACE_HOST_CMD_DEFAULT)
        agent = ftrace_agent.FtraceAgent(options, categories, io_interface)
        self.assertEqual(['sched'], agent._avail_categories())

        # check for no available categories
        permitted_files = {}
        io_interface = make_test_io_interface(permitted_files)
        options, categories = systrace.parse_options(SYSTRACE_HOST_CMD_DEFAULT)
        agent = ftrace_agent.FtraceAgent(options, categories, io_interface)
        self.assertEqual([], agent._avail_categories())

        # block has some required, some optional events
        permitted_files = {
            FT_EVENT_DIR + "block/block_rq_complete/enable": "0",
            FT_EVENT_DIR + "block/block_rq_issue/enable": "0"
        }
        io_interface = make_test_io_interface(permitted_files)
        options, categories = systrace.parse_options(SYSTRACE_HOST_CMD_DEFAULT)
        agent = ftrace_agent.FtraceAgent(options, categories, io_interface)
        self.assertEqual(['disk'], agent._avail_categories())
    def test_tracing_bootstrap(self):
        workq_event_path = FT_EVENT_DIR + "workqueue/enable"
        permitted_files = {workq_event_path: "0", FT_TRACE: "x"}
        io_interface = make_test_io_interface(permitted_files)
        systrace_cmd = SYSTRACE_HOST_CMD_DEFAULT + ["workq"]
        options, categories = systrace.parse_options(systrace_cmd)
        agent = ftrace_agent.FtraceAgent(options, categories, io_interface)
        self.assertEqual(["workq"], agent._avail_categories())

        # confirm tracing is enabled, buffer is cleared
        agent.start()
        self.assertEqual(permitted_files[FT_TRACE_ON], "1")
        self.assertEqual(permitted_files[FT_TRACE], "")

        # fill in file with dummy contents
        dummy_trace = "trace_contents"
        permitted_files[FT_TRACE] = dummy_trace

        # confirm tracing is disabled
        agent.collect_result()
        self.assertEqual(permitted_files[FT_TRACE_ON], "0")

        # confirm trace is expected, and read from fs
        self.assertTrue(agent.expect_trace())
        self.assertEqual(agent.get_trace_data(), dummy_trace)

        # confirm buffer size is reset to 1
        self.assertEqual(permitted_files[FT_BUFFER_SIZE], "1")
    def test_tracing_bootstrap(self):
        workq_event_path = FT_EVENT_DIR + "workqueue/enable"
        permitted_files = {workq_event_path: "0", FT_TRACE: "x"}
        io_interface = make_test_io_interface(permitted_files)
        systrace_cmd = SYSTRACE_HOST_CMD_DEFAULT + ["workq"]
        options, categories = systrace.parse_options(systrace_cmd)
        agent = ftrace_agent.FtraceAgent(options, categories, io_interface)
        self.assertEqual(['workq'], agent._avail_categories())

        # confirm tracing is enabled, buffer is cleared
        agent.start()
        self.assertEqual(permitted_files[FT_TRACE_ON], "1")
        self.assertEqual(permitted_files[FT_TRACE], "")

        # fill in file with dummy contents
        dummy_trace = "trace_contents"
        permitted_files[FT_TRACE] = dummy_trace

        # confirm tracing is disabled
        agent.collect_result()
        self.assertEqual(permitted_files[FT_TRACE_ON], "0")

        # confirm trace is expected, and read from fs
        self.assertTrue(agent.expect_trace())
        self.assertEqual(agent.get_trace_data(), dummy_trace)

        # confirm buffer size is reset to 1
        self.assertEqual(permitted_files[FT_BUFFER_SIZE], "1")
 def test_list_categories(self):
     options, categories = systrace.parse_options(
         SYSTRACE_LIST_CATEGORIES_CMD)
     agent = atrace_agent.AtraceAgent(options, categories)
     tracer_args = agent._construct_trace_command()
     self.assertEqual(' '.join(TRACE_LIST_CATEGORIES_CMD),
                      ' '.join(tracer_args))
     self.assertEqual(False, agent.expect_trace())
 def test_construct_trace_from_file_command(self):
   options, categories = systrace.parse_options(['systrace.py',
       '--from-file', ATRACE_DATA_RAW_FROM_FILE])
   agent = atrace_agent.try_create_agent(options, categories)
   tracer_args = agent._construct_trace_command()
   self.assertEqual(' '.join(['cat', ATRACE_DATA_RAW_FROM_FILE]),
           ' '.join(tracer_args))
   self.assertEqual(True, agent.expect_trace())
    def test_preprocess_trace_data(self):
        with contextlib.nested(open(ATRACE_DATA_WITH_THREAD_LIST, 'r'),
                               open(ATRACE_DATA, 'r')) as (f1, f2):
            atrace_data_with_thread_list = f1.read()
            atrace_data = f2.read()

            options, categories = systrace.parse_options([])
            agent = atrace_agent.AtraceAgent(options, categories)
            trace_data = agent._preprocess_trace_data(
                atrace_data_with_thread_list)
            self.assertEqual(atrace_data, trace_data)
  def test_preprocess_trace_data(self):
    with contextlib.nested(open(ATRACE_DATA_STRIPPED, 'r'),
                           open(ATRACE_DATA_RAW, 'r')) as (f1, f2):
      atrace_data = f1.read()
      atrace_data_raw = f2.read();

      options, categories = systrace.parse_options(STOP_FIX_UPS)
      agent = atrace_agent.AtraceAgent(options, categories)
      trace_data = agent._preprocess_trace_data(atrace_data_raw)

      self.assertEqual(atrace_data, trace_data)
    def test_tracing_event_enable_disable(self):
        # turn on irq tracing
        ipi_event_path = FT_EVENT_DIR + "ipi/enable"
        irq_event_path = FT_EVENT_DIR + "irq/enable"
        permitted_files = {ipi_event_path: "0", irq_event_path: "0"}
        io_interface = make_test_io_interface(permitted_files)
        systrace_cmd = SYSTRACE_HOST_CMD_DEFAULT + ["irq"]
        options, categories = systrace.parse_options(systrace_cmd)
        agent = ftrace_agent.FtraceAgent(options, categories, io_interface)
        self.assertEqual(["irq"], agent._avail_categories())

        # confirm all the event nodes are turned on during tracing
        agent.start()
        self.assertEqual(permitted_files[irq_event_path], "1")
        self.assertEqual(permitted_files[ipi_event_path], "1")

        # and then turned off when completed.
        agent.collect_result()
        self.assertEqual(permitted_files[irq_event_path], "0")
        self.assertEqual(permitted_files[ipi_event_path], "0")
    def test_tracing_event_enable_disable(self):
        # turn on irq tracing
        ipi_event_path = FT_EVENT_DIR + "ipi/enable"
        irq_event_path = FT_EVENT_DIR + "irq/enable"
        permitted_files = {ipi_event_path: "0", irq_event_path: "0"}
        io_interface = make_test_io_interface(permitted_files)
        systrace_cmd = SYSTRACE_HOST_CMD_DEFAULT + ["irq"]
        options, categories = systrace.parse_options(systrace_cmd)
        agent = ftrace_agent.FtraceAgent(options, categories, io_interface)
        self.assertEqual(['irq'], agent._avail_categories())

        # confirm all the event nodes are turned on during tracing
        agent.start()
        self.assertEqual(permitted_files[irq_event_path], "1")
        self.assertEqual(permitted_files[ipi_event_path], "1")

        # and then turned off when completed.
        agent.collect_result()
        self.assertEqual(permitted_files[irq_event_path], "0")
        self.assertEqual(permitted_files[ipi_event_path], "0")
 def test_construct_trace_command(self):
     options, categories = systrace.parse_options(SYSTRACE_CMD)
     agent = atrace_agent.AtraceLegacyAgent(options, categories)
     tracer_args = agent._construct_trace_command()
     self.assertEqual(' '.join(LEGACY_TRACE_CMD), ' '.join(tracer_args))
     self.assertEqual(True, agent.expect_trace())
 def test_buffer_size(self):
     systrace_cmd = SYSTRACE_HOST_CMD_DEFAULT + ["-b", "16000"]
     options, categories = systrace.parse_options(systrace_cmd)
     agent = ftrace_agent.FtraceAgent(options, categories)
     self.assertEqual(agent._get_trace_buffer_size(), 16000)
 def test_buffer_size(self):
     systrace_cmd = SYSTRACE_HOST_CMD_DEFAULT + ['-b', '16000']
     options, categories = systrace.parse_options(systrace_cmd)
     agent = ftrace_agent.FtraceAgent(options, categories)
     self.assertEqual(agent._get_trace_buffer_size(), 16000)
 def test_construct_trace_command(self):
   options, categories = systrace.parse_options(SYSTRACE_CMD)
   agent = atrace_agent.AtraceAgent(options, categories)
   tracer_args = agent._construct_trace_command()
   self.assertEqual(' '.join(TRACE_CMD), ' '.join(tracer_args))
   self.assertEqual(True, agent.expect_trace())
 def test_boot(self):
   options, categories = systrace.parse_options(SYSTRACE_BOOT_CMD)
   agent = atrace_agent.BootAgent(options, categories)
   tracer_args = agent._construct_trace_command()
   self.assertEqual(' '.join(TRACE_BOOT_CMD), ' '.join(tracer_args))
   self.assertEqual(True, agent.expect_trace())
 def test_trace_time(self):
   systrace_cmd = SYSTRACE_HOST_CMD_DEFAULT + ['-t', '10']
   options, categories = systrace.parse_options(systrace_cmd)
   agent = ftrace_agent.FtraceAgent(options, categories)
   self.assertEqual(agent._get_trace_time(), 10)