• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package simpleperf.demo.java_api;
18 
19 import android.support.v7.app.AppCompatActivity;
20 import android.os.Bundle;
21 import android.util.Log;
22 import android.widget.TextView;
23 
24 import com.android.simpleperf.ProfileSession;
25 import com.android.simpleperf.RecordOptions;
26 
27 public class MainActivity extends AppCompatActivity {
28 
29     TextView textView;
30 
31     static boolean threadsStarted = false;
32 
33     @Override
onCreate(Bundle savedInstanceState)34     protected void onCreate(Bundle savedInstanceState) {
35         super.onCreate(savedInstanceState);
36         setContentView(R.layout.activity_main);
37 
38         textView = (TextView) findViewById(R.id.textView);
39 
40         if (!threadsStarted) {
41             threadsStarted = true;
42             Thread profileThread = createProfileThread();
43             createBusyThread(profileThread);
44         }
45     }
46 
createProfileThread()47     Thread createProfileThread() {
48         Thread thread = new Thread(new Runnable() {
49             @Override
50             public void run() {
51                 RecordOptions recordOptions = new RecordOptions();
52                 recordOptions.recordDwarfCallGraph().setDuration(100).setEvent("cpu-clock");
53                 ProfileSession profileSession = new ProfileSession();
54                 try {
55                     Log.e("simpleperf", "startRecording");
56                     profileSession.startRecording(recordOptions);
57                     for (int i = 0; i < 1; i++) {
58                         Thread.sleep(1000);
59                         Log.e("simpleperf", "pauseRecording");
60                         profileSession.pauseRecording();
61                         Thread.sleep(1000);
62                         Log.e("simpleperf", "resumeRecording");
63                         profileSession.resumeRecording();
64                     }
65                     Thread.sleep(1000);
66                     Log.e("simpleperf", "stopRecording");
67                     profileSession.stopRecording();
68                     Log.e("simpleperf", "stopRecording successfully");
69                 } catch (Exception e) {
70                     Log.e("simpleperf", "exception: " + e.getMessage());
71                 }
72             }
73         }, "ProfileThread");
74         thread.start();
75         return thread;
76     }
77 
createBusyThread(final Thread profileThread)78     void createBusyThread(final Thread profileThread) {
79         new Thread(new Runnable() {
80             volatile int i = 0;
81 
82             @Override
83             public void run() {
84                 long times = 0;
85                 while (profileThread.isAlive()) {
86                     for (int i = 0; i < 1000000;) {
87                         i = callFunction(i);
88                     }
89                     try {
90                         Thread.sleep(1);
91                     } catch (InterruptedException e) {
92                     }
93                     times++;
94                     final long count = times;
95                     runOnUiThread(new Runnable() {
96                         @Override
97                         public void run() {
98                             textView.setText("count: " + count);
99                         }
100                     });
101                 }
102                 // Exit after recording. So we can build test apk, and expect profiling data file
103                 // after app exits.
104                 System.exit(0);
105             }
106 
107             private int callFunction(int a) {
108                 return a + 1;
109             }
110         }, "BusyThread").start();
111     }
112 }
113