Home | History | Annotate | Download | only in performanceLaunch
      1 /*
      2  * Copyright (C) 2016 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 com.android.performanceLaunch;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.content.ComponentName;
     22 import android.content.Intent;
     23 import android.os.Bundle;
     24 import android.os.Process;
     25 import android.util.Log;
     26 import java.io.File;
     27 import java.io.InputStream;
     28 import java.io.IOException;
     29 import java.lang.ProcessBuilder;
     30 
     31 public class DispatchActivity extends Activity {
     32 
     33     private static final String TAG = "DispatchActivity";
     34     private Context mContext;
     35     private Bundle mExtras;
     36     private Intent mIntent;
     37     private String mActivityName;
     38     private String mSimpleperfBin;
     39     private String mSimpleperfEvt;
     40     private String mSimpleperfDir;
     41     private String mPackageName;
     42     private int mPid;
     43 
     44     @Override
     45     protected void onCreate(Bundle savedInstanceState) {
     46         super.onCreate(savedInstanceState);
     47         mExtras = getIntent().getExtras();
     48         if (mExtras != null) {
     49             mActivityName = mExtras.getString("ACTIVITY_NAME");
     50             mSimpleperfBin = mExtras.getString("SIMPLEPERF_BIN");
     51             mSimpleperfEvt = mExtras.getString("SIMPLEPERF_EVT");
     52             mSimpleperfDir = mExtras.getString("SIMPLEPERF_DIR");
     53             if (mSimpleperfBin == null || mSimpleperfBin.isEmpty()) {
     54                 mSimpleperfBin = "/system/xbin/simpleperf";
     55             }
     56             if (mSimpleperfEvt == null || mSimpleperfEvt.isEmpty()) {
     57                 mSimpleperfEvt = "cpu-cycles,instructions:k,instructions:u";
     58             }
     59             if (mSimpleperfDir == null || mSimpleperfDir.isEmpty()) {
     60                 mSimpleperfDir = "/sdcard/perf_simpleperf";
     61             }
     62             mPackageName = getApplicationContext().getPackageName();
     63             mContext = getApplicationContext();
     64 
     65             ComponentName cn = new ComponentName(mPackageName, mActivityName);
     66             mIntent = new Intent(Intent.ACTION_MAIN);
     67             mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     68             mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
     69             mIntent.setComponent(cn);
     70         }
     71 
     72         try {
     73             mPid = android.os.Process.myPid();
     74             ProcessBuilder simpleperf =
     75                     new ProcessBuilder(mSimpleperfBin, "stat", "--group",
     76                             mSimpleperfEvt, "-p", String.valueOf(mPid));
     77 
     78             simpleperf.redirectOutput(new File(String.format("%s/%s.%s",
     79                     mSimpleperfDir, "perf.data", String.valueOf(mPid))));
     80             simpleperf.start();
     81 
     82         } catch (Exception e) {
     83             Log.v(TAG, "simpleperf throw exception");
     84             e.printStackTrace();
     85         }
     86 
     87         startActivity(mIntent);
     88     }
     89 
     90     @Override
     91     protected void onDestroy() {
     92         super.onDestroy();
     93         try {
     94             Runtime.getRuntime().exec("pkill -l SIGINT simpleperf").waitFor();
     95         } catch (Exception e) {
     96             Log.v(TAG, "Failed to stop simpleperf");
     97             e.printStackTrace();
     98         }
     99     }
    100 }
    101