Home | History | Annotate | Download | only in tests
      1 #    Copyright 2016-2016 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 import trappy
     17 
     18 import utils_tests
     19 
     20 class TestSchedFunctions(utils_tests.SetupDirectory):
     21     def __init__(self, *args, **kwargs):
     22         super(TestSchedFunctions, self).__init__([], *args, **kwargs)
     23 
     24     def test_get_pids_for_processes_no_sched_switch(self):
     25         """get_pids_for_processes() raises an exception if the trace doesn't have a sched_switch event"""
     26         from bart.sched.functions import get_pids_for_process
     27 
     28         trace_file = "trace.txt"
     29         raw_trace_file = "trace.raw.txt"
     30 
     31         with open(trace_file, "w") as fout:
     32             fout.write("")
     33 
     34         with open(raw_trace_file, "w") as fout:
     35             fout.write("")
     36 
     37         trace = trappy.FTrace(trace_file)
     38         with self.assertRaises(ValueError):
     39             get_pids_for_process(trace, "foo")
     40 
     41     def test_get_pids_for_process_funny_process_names(self):
     42         """get_pids_for_process() works when a process name is a substring of another"""
     43         from bart.sched.functions import get_pids_for_process
     44 
     45         trace_file = "trace.txt"
     46         raw_trace_file = "trace.raw.txt"
     47         in_data = """          <idle>-0     [001] 10826.894644: sched_switch:          prev_comm=swapper/1 prev_pid=0 prev_prio=120 prev_state=0 next_comm=rt-app next_pid=3268 next_prio=120
     48             wmig-3268  [001] 10826.894778: sched_switch:          prev_comm=wmig prev_pid=3268 prev_prio=120 prev_state=1 next_comm=rt-app next_pid=3269 next_prio=120
     49            wmig1-3269  [001] 10826.905152: sched_switch:          prev_comm=wmig1 prev_pid=3269 prev_prio=120 prev_state=1 next_comm=wmig next_pid=3268 next_prio=120
     50             wmig-3268  [001] 10826.915384: sched_switch:          prev_comm=wmig prev_pid=3268 prev_prio=120 prev_state=1 next_comm=swapper/1 next_pid=0 next_prio=120
     51           <idle>-0     [005] 10826.995169: sched_switch:          prev_comm=swapper/5 prev_pid=0 prev_prio=120 prev_state=0 next_comm=wmig1 next_pid=3269 next_prio=120
     52            wmig1-3269  [005] 10827.007064: sched_switch:          prev_comm=wmig1 prev_pid=3269 prev_prio=120 prev_state=0 next_comm=wmig next_pid=3268 next_prio=120
     53             wmig-3268  [005] 10827.019061: sched_switch:          prev_comm=wmig prev_pid=3268 prev_prio=120 prev_state=0 next_comm=wmig1 next_pid=3269 next_prio=120
     54            wmig1-3269  [005] 10827.031061: sched_switch:          prev_comm=wmig1 prev_pid=3269 prev_prio=120 prev_state=0 next_comm=wmig next_pid=3268 next_prio=120
     55             wmig-3268  [005] 10827.050645: sched_switch:          prev_comm=wmig prev_pid=3268 prev_prio=120 prev_state=1 next_comm=swapper/5 next_pid=0 next_prio=120
     56 """
     57 
     58         # We create an empty trace.txt to please trappy ...
     59         with open(trace_file, "w") as fout:
     60             fout.write("")
     61 
     62         # ... but we only put the sched_switch events in the raw trace
     63         # file because that's where trappy is going to look for
     64         with open(raw_trace_file, "w") as fout:
     65             fout.write(in_data)
     66 
     67         trace = trappy.FTrace(trace_file)
     68 
     69         self.assertEquals(get_pids_for_process(trace, "wmig"), [3268])
     70