blob: 61267d07b18cdedbf1b5ca2abf4409a2bfbf9a9e [file] [log] [blame]
Sen Hubde75702010-05-28 01:54:03 -07001/*
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
17package com.android.server;
18
19import android.content.ContentResolver;
20import android.os.DropBoxManager;
21import android.os.FileObserver;
22import android.os.Binder;
23
24import android.util.Slog;
25import android.content.Context;
26import android.database.ContentObserver;
27import android.os.SystemProperties;
28import android.provider.Settings;
29import com.android.internal.os.SamplingProfilerIntegration;
30
31import java.io.File;
32import java.io.FileDescriptor;
33import java.io.IOException;
34import java.io.PrintWriter;
35
36public 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(
Brad Fitzpatrickd7ad0d22010-12-02 15:30:23 -080091 Settings.Secure.getUriFor(Settings.Secure.SAMPLING_PROFILER_MS),
Sen Hubde75702010-05-28 01:54:03 -070092 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) {
Brad Fitzpatrickd7ad0d22010-12-02 15:30:23 -0800110 Integer samplingProfilerMs = Settings.Secure.getInt(
111 mContentResolver, Settings.Secure.SAMPLING_PROFILER_MS, 0);
Sen Hubde75702010-05-28 01:54:03 -0700112 // setting this secure property will start or stop sampling profiler,
Brad Fitzpatrickd7ad0d22010-12-02 15:30:23 -0800113 // as well as adjust the the time between taking snapshots.
114 SystemProperties.set("persist.sys.profiler_ms", samplingProfilerMs.toString());
Sen Hubde75702010-05-28 01:54:03 -0700115 }
116 }
117}