adb.py revision 1a7855c14512248f7b22c8c53c3ed29068f27bed
1import subprocess
2import abc
3import os
4
5BIN_PATH = "../scripts/bin/android/%s/simpleperf"
6
7class Abi:
8    ARM    = 1
9    ARM_64 = 2
10    X86    = 3
11    X86_64 = 4
12
13    def __init__(self):
14        pass
15
16class Adb:
17
18    def __init__(self):
19        pass
20
21
22    def delete_previous_data(self):
23        err = subprocess.call(["adb", "shell", "rm", "-f", "/data/local/tmp/perf.data"])
24
25
26    def get_process_pid(self, process_name):
27        piof_output = subprocess.check_output(["adb", "shell", "pidof", process_name])
28        try:
29            process_id = int(piof_output)
30        except ValueError:
31            process_id = 0
32        return process_id
33
34
35    def pull_data(self):
36        err = subprocess.call(["adb", "pull", "/data/local/tmp/perf.data", "."])
37        return err
38
39
40    @abc.abstractmethod
41    def collect_data(self, simpleperf_command):
42        raise NotImplementedError("%s.collect_data(str) is not implemented!" % self.__class__.__name__)
43
44
45    def get_props(self):
46        props = {}
47        output = subprocess.check_output(["adb", "shell", "getprop"])
48        lines = output.split("\n")
49        for line in lines:
50            tokens = line.split(": ")
51            if len(tokens) < 2:
52                continue
53            key = tokens[0].replace("[", "").replace("]", "")
54            value = tokens[1].replace("[", "").replace("]", "")
55            props[key] = value
56        return props
57
58    def parse_abi(self, str):
59        if str.find("arm64") != -1:
60            return Abi.ARM_64
61        if str.find("arm") != -1:
62            return Abi.ARM
63        if str.find("x86_64") != -1:
64            return Abi.X86_64
65        if str.find("x86") != -1:
66            return Abi.X86
67        return Abi.ARM_64
68
69    def get_exec_path(self, abi):
70        folder_name = "arm64"
71        if abi == Abi.ARM:
72            folder_name = "arm"
73        if abi == Abi.X86:
74            folder_name = "x86"
75        if abi == Abi.X86_64:
76            folder_name = "x86_64"
77        return os.path.join(os.path.dirname(__file__), BIN_PATH % folder_name)
78
79    def push_simpleperf_binary(self):
80        # Detect the ABI of the device
81        props = self.get_props()
82        abi_raw = props["ro.product.cpu.abi"]
83        abi = self.parse_abi(abi_raw)
84        exec_path = self.get_exec_path(abi)
85
86        # Push simpleperf to the device
87        print "Pushing local '%s' to device." % exec_path
88        subprocess.call(["adb", "push", exec_path, "/data/local/tmp/simpleperf"])
89