OSDN Git Service

simpleperf: add run_simpleperf_on_device.py.
authorYabin Cui <yabinc@google.com>
Fri, 11 Aug 2017 23:22:50 +0000 (16:22 -0700)
committerYabin Cui <yabinc@google.com>
Thu, 17 Aug 2017 19:33:40 +0000 (12:33 -0700)
It is added mainly to show in README.md how to detect whether
trace-offcpu is supported on device, by
 `python run_simpleperf_on_device.py list --show-features`.

Bug: http://b/32834638
Test: run test.py.
Change-Id: I33a6fb81b650293faec233eb6a735bac7cb5e215

simpleperf/doc/README.md
simpleperf/scripts/run_simpleperf_on_device.py [new file with mode: 0644]
simpleperf/scripts/test.py
simpleperf/scripts/utils.py

index 078d56b..a16f799 100644 (file)
@@ -93,7 +93,7 @@ Python scripts are written to help different profiling tasks.
 
 `annotate.py` is used to annotate source files based on profiling data.
 
-`app_profiler.py` is used to profile Android applications.
+`app_profiler.py` is used to profile Android applications and native programs.
 
 `binary_cache_builder.py` is used to pull libraries from Android devices.
 
@@ -103,6 +103,8 @@ Python scripts are written to help different profiling tasks.
 
 `report_sample.py` is used to generate flamegraph.
 
+`run_simpleperf_on_device.py` is a simple wrapper to run simpleperf on device.
+
 `simpleperf_report_lib.py` provides a python interface for parsing profiling data.
 
 
diff --git a/simpleperf/scripts/run_simpleperf_on_device.py b/simpleperf/scripts/run_simpleperf_on_device.py
new file mode 100644 (file)
index 0000000..37155bc
--- /dev/null
@@ -0,0 +1,37 @@
+#!/usr/bin/env python
+#
+# Copyright (C) 2017 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+"""run_simpleperf_on_device.py:
+    It downloads simpleperf to /data/local/tmp on device, and run it with all given arguments.
+    It saves the time downloading simpleperf and using `adb shell` directly.
+"""
+import subprocess
+import sys
+from utils import *
+
+def main():
+    disable_debug_log()
+    adb = AdbHelper()
+    device_arch = adb.get_device_arch()
+    simpleperf_binary = get_target_binary_path(device_arch, 'simpleperf')
+    adb.check_run(['push', simpleperf_binary, '/data/local/tmp'])
+    adb.check_run(['shell', 'chmod', 'a+x', '/data/local/tmp/simpleperf'])
+    shell_cmd = 'cd /data/local/tmp && ./simpleperf ' + ' '.join(sys.argv[1:])
+    sys.exit(subprocess.call([adb.adb_path, 'shell', shell_cmd]))
+
+if __name__ == '__main__':
+    main()
\ No newline at end of file
index 1a97ab1..69df596 100644 (file)
@@ -87,7 +87,26 @@ def build_testdata():
     for demo in copy_demo_list:
         shutil.copytree(os.path.join(from_demo_path, demo), os.path.join(testdata_path, demo))
 
-class TestExampleBase(unittest.TestCase):
+
+class TestBase(unittest.TestCase):
+    def run_cmd(self, args, return_output=False):
+        if args[0].endswith('.py'):
+            args = [sys.executable] + args
+        try:
+            if not return_output:
+                returncode = subprocess.call(args)
+            else:
+                subproc = subprocess.Popen(args, stdout=subprocess.PIPE)
+                (output_data, _) = subproc.communicate()
+                returncode = subproc.returncode
+        except:
+            returncode = None
+        self.assertEqual(returncode, 0, msg="failed to run cmd: %s" % args)
+        if return_output:
+            return output_data
+
+
+class TestExampleBase(TestBase):
     @classmethod
     def prepare(cls, example_name, package_name, activity_name, abi=None, adb_root=False):
         cls.adb = AdbHelper(enable_switch_to_root=adb_root)
@@ -127,22 +146,6 @@ class TestExampleBase(unittest.TestCase):
         remove("report.txt")
         remove("pprof.profile")
 
-    def run_cmd(self, args, return_output=False):
-        if args[0].endswith('.py'):
-            args = [sys.executable] + args
-        try:
-            if not return_output:
-                returncode = subprocess.call(args)
-            else:
-                subproc = subprocess.Popen(args, stdout=subprocess.PIPE)
-                (output_data, _) = subproc.communicate()
-                returncode = subproc.returncode
-        except:
-            returncode = None
-        self.assertEqual(returncode, 0, msg="failed to run cmd: %s" % args)
-        if return_output:
-            return output_data
-
     def run_app_profiler(self, record_arg = "-g --duration 3 -e cpu-cycles:u",
                          build_binary_cache=True, skip_compile=False, start_activity=True,
                          native_lib_dir=None):
@@ -730,6 +733,11 @@ class TestReportLib(unittest.TestCase):
         self.assertAlmostEqual(sleep_percentage, 0.4629, delta=0.0001)
 
 
+class TestRunSimpleperfOnDevice(TestBase):
+    def test_smoke(self):
+        self.run_cmd(['run_simpleperf_on_device.py', 'list', '--show-features'])
+
+
 def main():
     build_testdata()
     test_program = unittest.main(failfast=True, exit=False)
index 0570cfd..a28008e 100644 (file)
@@ -59,6 +59,9 @@ def log_fatal(msg):
 def log_exit(msg):
     sys.exit(msg)
 
+def disable_debug_log():
+    logging.getLogger().setLevel(logging.WARN)
+
 def str_to_bytes(str):
     if not is_python3():
         return str