Home | History | Annotate | Download | only in server
      1 /*
      2  * Copyright (C) 2010 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.server;
     18 
     19 import android.content.ContentResolver;
     20 import android.os.DropBoxManager;
     21 import android.os.FileObserver;
     22 import android.os.Binder;
     23 
     24 import android.util.Slog;
     25 import android.content.Context;
     26 import android.database.ContentObserver;
     27 import android.os.SystemProperties;
     28 import android.provider.Settings;
     29 import com.android.internal.os.SamplingProfilerIntegration;
     30 import com.android.internal.util.DumpUtils;
     31 
     32 import java.io.File;
     33 import java.io.FileDescriptor;
     34 import java.io.IOException;
     35 import java.io.PrintWriter;
     36 
     37 public class SamplingProfilerService extends Binder {
     38 
     39     private static final String TAG = "SamplingProfilerService";
     40     private static final boolean LOCAL_LOGV = false;
     41     public static final String SNAPSHOT_DIR = SamplingProfilerIntegration.SNAPSHOT_DIR;
     42 
     43     private final Context mContext;
     44     private FileObserver snapshotObserver;
     45 
     46     public SamplingProfilerService(Context context) {
     47         mContext = context;
     48         registerSettingObserver(context);
     49         startWorking(context);
     50     }
     51 
     52     private void startWorking(Context context) {
     53         if (LOCAL_LOGV) Slog.v(TAG, "starting SamplingProfilerService!");
     54 
     55         final DropBoxManager dropbox =
     56                 (DropBoxManager) context.getSystemService(Context.DROPBOX_SERVICE);
     57 
     58         // before FileObserver is ready, there could have already been some snapshots
     59         // in the directory, we don't want to miss them
     60         File[] snapshotFiles = new File(SNAPSHOT_DIR).listFiles();
     61         for (int i = 0; snapshotFiles != null && i < snapshotFiles.length; i++) {
     62             handleSnapshotFile(snapshotFiles[i], dropbox);
     63         }
     64 
     65         // detect new snapshot and put it in dropbox
     66         // delete it afterwards no matter what happened before
     67         // Note: needs listening at event ATTRIB rather than CLOSE_WRITE, because we set the
     68         // readability of snapshot files after writing them!
     69         snapshotObserver = new FileObserver(SNAPSHOT_DIR, FileObserver.ATTRIB) {
     70             @Override
     71             public void onEvent(int event, String path) {
     72                 handleSnapshotFile(new File(SNAPSHOT_DIR, path), dropbox);
     73             }
     74         };
     75         snapshotObserver.startWatching();
     76 
     77         if (LOCAL_LOGV) Slog.v(TAG, "SamplingProfilerService activated");
     78     }
     79 
     80     private void handleSnapshotFile(File file, DropBoxManager dropbox) {
     81         try {
     82             dropbox.addFile(TAG, file, 0);
     83             if (LOCAL_LOGV) Slog.v(TAG, file.getPath() + " added to dropbox");
     84         } catch (IOException e) {
     85             Slog.e(TAG, "Can't add " + file.getPath() + " to dropbox", e);
     86         } finally {
     87             file.delete();
     88         }
     89     }
     90 
     91     private void registerSettingObserver(Context context) {
     92         ContentResolver contentResolver = context.getContentResolver();
     93         contentResolver.registerContentObserver(
     94                 Settings.Global.getUriFor(Settings.Global.SAMPLING_PROFILER_MS),
     95                 false, new SamplingProfilerSettingsObserver(contentResolver));
     96     }
     97 
     98     @Override
     99     protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
    100         if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return;
    101 
    102         pw.println("SamplingProfilerService:");
    103         pw.println("Watching directory: " + SNAPSHOT_DIR);
    104     }
    105 
    106     private class SamplingProfilerSettingsObserver extends ContentObserver {
    107         private ContentResolver mContentResolver;
    108         public SamplingProfilerSettingsObserver(ContentResolver contentResolver) {
    109             super(null);
    110             mContentResolver = contentResolver;
    111             onChange(false);
    112         }
    113         @Override
    114         public void onChange(boolean selfChange) {
    115             Integer samplingProfilerMs = Settings.Global.getInt(
    116                     mContentResolver, Settings.Global.SAMPLING_PROFILER_MS, 0);
    117             // setting this secure property will start or stop sampling profiler,
    118             // as well as adjust the the time between taking snapshots.
    119             SystemProperties.set("persist.sys.profiler_ms", samplingProfilerMs.toString());
    120         }
    121     }
    122 }
    123