|
| 1 | +# Copyright 2018-2019 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 os |
| 17 | +import shlex |
| 18 | +from typing import Any, Dict, Optional |
| 19 | + |
| 20 | +from devlib.host import PACKAGE_BIN_DIRECTORY |
| 21 | +from devlib.trace import TraceCollector |
| 22 | + |
| 23 | +class PerfCollector(TraceCollector): |
| 24 | + """ |
| 25 | + """ |
| 26 | + def __init__(self, |
| 27 | + target, |
| 28 | + force_install : bool = False, |
| 29 | + pre_commands : Optional[Dict[str, Any]] = None, |
| 30 | + commands : Optional[Dict[str, Any]] = None, |
| 31 | + post_commands : Optional[Dict[str, Any]] = None): |
| 32 | + super().__init__(target) |
| 33 | + self.pre_commands = pre_commands or {} |
| 34 | + self.commands = commands or {} |
| 35 | + self.post_commands = post_commands or {} |
| 36 | + |
| 37 | + self.binary = self.target.get_installed('perf') |
| 38 | + if force_install or not self.binary: |
| 39 | + host_binary = os.path.join(PACKAGE_BIN_DIRECTORY, |
| 40 | + self.target.abi, 'perf') |
| 41 | + self.binary = self.target.install(host_binary) |
| 42 | + |
| 43 | + self.kill_sleep = False |
| 44 | + |
| 45 | + # TODO: folder tree for files on device to avoid clashes |
| 46 | + # TODO: user has to figure out file classhes (e.g. between perf.data) |
| 47 | + |
| 48 | + def reset(self): |
| 49 | + super().reset() |
| 50 | + self.target.killall('perf', as_root=self.target.is_rooted) |
| 51 | + |
| 52 | + def start(self): |
| 53 | + super().start() |
| 54 | + self.cwd = f'cd {shlex.quote(self.target.working_directory)}' |
| 55 | + # TODO: why isn't target.execute running in working_directory? |
| 56 | + for label, command in self.pre_commands.items(): |
| 57 | + self.target.execute(f'{self.cwd} && {self.binary} {command}', |
| 58 | + as_root=self.target.is_rooted) |
| 59 | + for label, command in self.commands.items(): |
| 60 | + self.target.kick_off(f'{self.binary} {command}', |
| 61 | + as_root=self.target.is_rooted) |
| 62 | + if 'sleep' in command: |
| 63 | + self.kill_sleep = True |
| 64 | + |
| 65 | + def stop(self): |
| 66 | + super().stop() |
| 67 | + self.target.killall('perf', signal='SIGINT', |
| 68 | + as_root=self.target.is_rooted) |
| 69 | + if self.kill_sleep: |
| 70 | + self.target.killall('sleep', as_root=self.target.is_rooted) |
| 71 | + for label, command in self.post_commands.items(): |
| 72 | + self.target.execute(f'{self.cwd} && {self.binary} {command}', |
| 73 | + as_root=self.target.is_rooted) |
| 74 | + |
| 75 | + def get_trace(self, outfile): |
| 76 | + raise NotImplementedError # TODO |
0 commit comments