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