blob: fbf1aa4b8db06631cfc87d5e2c3b494b5cc3c4cc [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
Jeff Sharkeyeb4cc4922012-04-26 18:17:29 -070042 private final Context mContext;
Sen Hubde75702010-05-28 01:54:03 -070043 private FileObserver snapshotObserver;
44
45 public SamplingProfilerService(Context context) {
Jeff Sharkeyeb4cc4922012-04-26 18:17:29 -070046 mContext = context;
Sen Hubde75702010-05-28 01:54:03 -070047 registerSettingObserver(context);
48 startWorking(context);
49 }
50
51 private void startWorking(Context context) {
52 if (LOCAL_LOGV) Slog.v(TAG, "starting SamplingProfilerService!");
53
54 final DropBoxManager dropbox =
55 (DropBoxManager) context.getSystemService(Context.DROPBOX_SERVICE);
56
57 // before FileObserver is ready, there could have already been some snapshots
58 // in the directory, we don't want to miss them
59 File[] snapshotFiles = new File(SNAPSHOT_DIR).listFiles();
60 for (int i = 0; snapshotFiles != null && i < snapshotFiles.length; i++) {
61 handleSnapshotFile(snapshotFiles[i], dropbox);
62 }
63
64 // detect new snapshot and put it in dropbox
65 // delete it afterwards no matter what happened before
66 // Note: needs listening at event ATTRIB rather than CLOSE_WRITE, because we set the
67 // readability of snapshot files after writing them!
68 snapshotObserver = new FileObserver(SNAPSHOT_DIR, FileObserver.ATTRIB) {
69 @Override
70 public void onEvent(int event, String path) {
71 handleSnapshotFile(new File(SNAPSHOT_DIR, path), dropbox);
72 }
73 };
74 snapshotObserver.startWatching();
75
76 if (LOCAL_LOGV) Slog.v(TAG, "SamplingProfilerService activated");
77 }
78
79 private void handleSnapshotFile(File file, DropBoxManager dropbox) {
80 try {
81 dropbox.addFile(TAG, file, 0);
82 if (LOCAL_LOGV) Slog.v(TAG, file.getPath() + " added to dropbox");
83 } catch (IOException e) {
84 Slog.e(TAG, "Can't add " + file.getPath() + " to dropbox", e);
85 } finally {
86 file.delete();
87 }
88 }
89
90 private void registerSettingObserver(Context context) {
91 ContentResolver contentResolver = context.getContentResolver();
92 contentResolver.registerContentObserver(
Jeff Brownbf6f6f92012-09-25 15:03:20 -070093 Settings.Global.getUriFor(Settings.Global.SAMPLING_PROFILER_MS),
Sen Hubde75702010-05-28 01:54:03 -070094 false, new SamplingProfilerSettingsObserver(contentResolver));
95 }
96
97 @Override
98 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Jeff Sharkeyeb4cc4922012-04-26 18:17:29 -070099 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
100
Sen Hubde75702010-05-28 01:54:03 -0700101 pw.println("SamplingProfilerService:");
102 pw.println("Watching directory: " + SNAPSHOT_DIR);
103 }
104
105 private class SamplingProfilerSettingsObserver extends ContentObserver {
106 private ContentResolver mContentResolver;
107 public SamplingProfilerSettingsObserver(ContentResolver contentResolver) {
108 super(null);
109 mContentResolver = contentResolver;
110 onChange(false);
111 }
112 @Override
113 public void onChange(boolean selfChange) {
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700114 Integer samplingProfilerMs = Settings.Global.getInt(
115 mContentResolver, Settings.Global.SAMPLING_PROFILER_MS, 0);
Sen Hubde75702010-05-28 01:54:03 -0700116 // setting this secure property will start or stop sampling profiler,
Brad Fitzpatrickd7ad0d22010-12-02 15:30:23 -0800117 // as well as adjust the the time between taking snapshots.
118 SystemProperties.set("persist.sys.profiler_ms", samplingProfilerMs.toString());
Sen Hubde75702010-05-28 01:54:03 -0700119 }
120 }
121}