Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 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 | |
Jeff Sharkey | eb4cc492 | 2012-04-26 18:17:29 -0700 | [diff] [blame] | 42 | private final Context mContext; |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 43 | private FileObserver snapshotObserver; |
| 44 | |
| 45 | public SamplingProfilerService(Context context) { |
Jeff Sharkey | eb4cc492 | 2012-04-26 18:17:29 -0700 | [diff] [blame] | 46 | mContext = context; |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 47 | 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 Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 93 | Settings.Global.getUriFor(Settings.Global.SAMPLING_PROFILER_MS), |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 94 | false, new SamplingProfilerSettingsObserver(contentResolver)); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | eb4cc492 | 2012-04-26 18:17:29 -0700 | [diff] [blame] | 99 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
| 100 | |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 101 | 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 Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 114 | Integer samplingProfilerMs = Settings.Global.getInt( |
| 115 | mContentResolver, Settings.Global.SAMPLING_PROFILER_MS, 0); |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 116 | // setting this secure property will start or stop sampling profiler, |
Brad Fitzpatrick | d7ad0d2 | 2010-12-02 15:30:23 -0800 | [diff] [blame] | 117 | // as well as adjust the the time between taking snapshots. |
| 118 | SystemProperties.set("persist.sys.profiler_ms", samplingProfilerMs.toString()); |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |