Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 17 | package com.android.internal.os; |
| 18 | |
Jesse Wilson | db35269 | 2011-01-23 11:41:15 -0800 | [diff] [blame] | 19 | import android.content.pm.PackageInfo; |
| 20 | import android.os.Build; |
| 21 | import android.os.SystemProperties; |
| 22 | import android.util.Log; |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 23 | import dalvik.system.SamplingProfiler; |
Brian Carlstrom | 1751086 | 2010-08-18 14:27:40 -0700 | [diff] [blame] | 24 | import java.io.BufferedOutputStream; |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 25 | import java.io.File; |
| 26 | import java.io.FileOutputStream; |
| 27 | import java.io.IOException; |
Brian Carlstrom | 1751086 | 2010-08-18 14:27:40 -0700 | [diff] [blame] | 28 | import java.io.PrintStream; |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 29 | import java.util.concurrent.Executor; |
| 30 | import java.util.concurrent.Executors; |
Brian Carlstrom | def41ec | 2010-12-01 15:40:38 -0800 | [diff] [blame] | 31 | import java.util.concurrent.ThreadFactory; |
Jesse Wilson | db35269 | 2011-01-23 11:41:15 -0800 | [diff] [blame] | 32 | import java.util.concurrent.atomic.AtomicBoolean; |
| 33 | import libcore.io.IoUtils; |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Integrates the framework with Dalvik's sampling profiler. |
| 37 | */ |
| 38 | public class SamplingProfilerIntegration { |
| 39 | |
| 40 | private static final String TAG = "SamplingProfilerIntegration"; |
| 41 | |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 42 | public static final String SNAPSHOT_DIR = "/data/snapshots"; |
| 43 | |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 44 | private static final boolean enabled; |
| 45 | private static final Executor snapshotWriter; |
Brian Carlstrom | def41ec | 2010-12-01 15:40:38 -0800 | [diff] [blame] | 46 | private static final int samplingProfilerMilliseconds; |
| 47 | private static final int samplingProfilerDepth; |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 48 | |
| 49 | /** Whether or not a snapshot is being persisted. */ |
| 50 | private static final AtomicBoolean pending = new AtomicBoolean(false); |
| 51 | |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 52 | static { |
Brian Carlstrom | def41ec | 2010-12-01 15:40:38 -0800 | [diff] [blame] | 53 | samplingProfilerMilliseconds = SystemProperties.getInt("persist.sys.profiler_ms", 0); |
| 54 | samplingProfilerDepth = SystemProperties.getInt("persist.sys.profiler_depth", 4); |
| 55 | if (samplingProfilerMilliseconds > 0) { |
| 56 | File dir = new File(SNAPSHOT_DIR); |
| 57 | dir.mkdirs(); |
| 58 | // the directory needs to be writable to anybody to allow file writing |
| 59 | dir.setWritable(true, false); |
| 60 | // the directory needs to be executable to anybody to allow file creation |
| 61 | dir.setExecutable(true, false); |
| 62 | if (dir.isDirectory()) { |
| 63 | snapshotWriter = Executors.newSingleThreadExecutor(new ThreadFactory() { |
| 64 | public Thread newThread(Runnable r) { |
| 65 | return new Thread(r, TAG); |
| 66 | } |
| 67 | }); |
| 68 | enabled = true; |
| 69 | Log.i(TAG, "Profiling enabled. Sampling interval ms: " |
| 70 | + samplingProfilerMilliseconds); |
| 71 | } else { |
| 72 | snapshotWriter = null; |
| 73 | enabled = true; |
| 74 | Log.w(TAG, "Profiling setup failed. Could not create " + SNAPSHOT_DIR); |
| 75 | } |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 76 | } else { |
| 77 | snapshotWriter = null; |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 78 | enabled = false; |
Brian Carlstrom | def41ec | 2010-12-01 15:40:38 -0800 | [diff] [blame] | 79 | Log.i(TAG, "Profiling disabled."); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
Brian Carlstrom | 1751086 | 2010-08-18 14:27:40 -0700 | [diff] [blame] | 83 | private static SamplingProfiler INSTANCE; |
| 84 | |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Is profiling enabled? |
| 87 | */ |
| 88 | public static boolean isEnabled() { |
| 89 | return enabled; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Starts the profiler if profiling is enabled. |
| 94 | */ |
| 95 | public static void start() { |
Brian Carlstrom | 1751086 | 2010-08-18 14:27:40 -0700 | [diff] [blame] | 96 | if (!enabled) { |
| 97 | return; |
| 98 | } |
| 99 | ThreadGroup group = Thread.currentThread().getThreadGroup(); |
| 100 | SamplingProfiler.ThreadSet threadSet = SamplingProfiler.newThreadGroupTheadSet(group); |
Brian Carlstrom | def41ec | 2010-12-01 15:40:38 -0800 | [diff] [blame] | 101 | INSTANCE = new SamplingProfiler(samplingProfilerDepth, threadSet); |
| 102 | INSTANCE.start(samplingProfilerMilliseconds); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 105 | /** |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 106 | * Writes a snapshot if profiling is enabled. |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 107 | */ |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 108 | public static void writeSnapshot(final String processName, final PackageInfo packageInfo) { |
Brian Carlstrom | 1751086 | 2010-08-18 14:27:40 -0700 | [diff] [blame] | 109 | if (!enabled) { |
| 110 | return; |
| 111 | } |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 112 | |
Bob Lee | eec2f41 | 2009-09-10 11:01:24 +0200 | [diff] [blame] | 113 | /* |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 114 | * If we're already writing a snapshot, don't bother enqueueing another |
Bob Lee | eec2f41 | 2009-09-10 11:01:24 +0200 | [diff] [blame] | 115 | * request right now. This will reduce the number of individual |
| 116 | * snapshots and in turn the total amount of memory consumed (one big |
| 117 | * snapshot is smaller than N subset snapshots). |
| 118 | */ |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 119 | if (pending.compareAndSet(false, true)) { |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 120 | snapshotWriter.execute(new Runnable() { |
| 121 | public void run() { |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 122 | try { |
Brian Carlstrom | def41ec | 2010-12-01 15:40:38 -0800 | [diff] [blame] | 123 | writeSnapshotFile(processName, packageInfo); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 124 | } finally { |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 125 | pending.set(false); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | }); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Writes the zygote's snapshot to internal storage if profiling is enabled. |
| 134 | */ |
| 135 | public static void writeZygoteSnapshot() { |
Brian Carlstrom | 1751086 | 2010-08-18 14:27:40 -0700 | [diff] [blame] | 136 | if (!enabled) { |
| 137 | return; |
| 138 | } |
Brian Carlstrom | def41ec | 2010-12-01 15:40:38 -0800 | [diff] [blame] | 139 | writeSnapshotFile("zygote", null); |
Brian Carlstrom | 1751086 | 2010-08-18 14:27:40 -0700 | [diff] [blame] | 140 | INSTANCE.shutdown(); |
| 141 | INSTANCE = null; |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 144 | /** |
| 145 | * pass in PackageInfo to retrieve various values for snapshot header |
| 146 | */ |
Brian Carlstrom | def41ec | 2010-12-01 15:40:38 -0800 | [diff] [blame] | 147 | private static void writeSnapshotFile(String processName, PackageInfo packageInfo) { |
Brian Carlstrom | 1751086 | 2010-08-18 14:27:40 -0700 | [diff] [blame] | 148 | if (!enabled) { |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 149 | return; |
| 150 | } |
Brian Carlstrom | 1751086 | 2010-08-18 14:27:40 -0700 | [diff] [blame] | 151 | INSTANCE.stop(); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * We use the current time as a unique ID. We can't use a counter |
| 155 | * because processes restart. This could result in some overlap if |
| 156 | * we capture two snapshots in rapid succession. |
| 157 | */ |
| 158 | long start = System.currentTimeMillis(); |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 159 | String name = processName.replaceAll(":", "."); |
Brian Carlstrom | def41ec | 2010-12-01 15:40:38 -0800 | [diff] [blame] | 160 | String path = SNAPSHOT_DIR + "/" + name + "-" +System.currentTimeMillis() + ".snapshot"; |
Brian Carlstrom | c9ad7c6 | 2010-09-01 12:41:36 -0700 | [diff] [blame] | 161 | PrintStream out = null; |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 162 | try { |
Brian Carlstrom | c9ad7c6 | 2010-09-01 12:41:36 -0700 | [diff] [blame] | 163 | out = new PrintStream(new BufferedOutputStream(new FileOutputStream(path))); |
Brian Carlstrom | c9ad7c6 | 2010-09-01 12:41:36 -0700 | [diff] [blame] | 164 | generateSnapshotHeader(name, packageInfo, out); |
Jesse Wilson | db35269 | 2011-01-23 11:41:15 -0800 | [diff] [blame] | 165 | new SamplingProfiler.AsciiHprofWriter(INSTANCE.getHprofData(), out).write(); |
| 166 | if (out.checkError()) { |
| 167 | throw new IOException(); |
| 168 | } |
| 169 | } catch (IOException e) { |
| 170 | Log.e(TAG, "Error writing snapshot to " + path, e); |
Brian Carlstrom | c9ad7c6 | 2010-09-01 12:41:36 -0700 | [diff] [blame] | 171 | return; |
Jesse Wilson | db35269 | 2011-01-23 11:41:15 -0800 | [diff] [blame] | 172 | } finally { |
| 173 | IoUtils.closeQuietly(out); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 174 | } |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 175 | // set file readable to the world so that SamplingProfilerService |
| 176 | // can put it to dropbox |
| 177 | new File(path).setReadable(true, false); |
| 178 | |
| 179 | long elapsed = System.currentTimeMillis() - start; |
| 180 | Log.i(TAG, "Wrote snapshot for " + name + " in " + elapsed + "ms."); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * generate header for snapshots, with the following format (like http header): |
| 185 | * |
| 186 | * Version: <version number of profiler>\n |
| 187 | * Process: <process name>\n |
| 188 | * Package: <package name, if exists>\n |
| 189 | * Package-Version: <version number of the package, if exists>\n |
| 190 | * Build: <fingerprint>\n |
| 191 | * \n |
| 192 | * <the actual snapshot content begins here...> |
| 193 | */ |
| 194 | private static void generateSnapshotHeader(String processName, PackageInfo packageInfo, |
Brian Carlstrom | c9ad7c6 | 2010-09-01 12:41:36 -0700 | [diff] [blame] | 195 | PrintStream out) { |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 196 | // profiler version |
Brian Carlstrom | c9ad7c6 | 2010-09-01 12:41:36 -0700 | [diff] [blame] | 197 | out.println("Version: 2"); |
| 198 | out.println("Process: " + processName); |
| 199 | if (packageInfo != null) { |
| 200 | out.println("Package: " + packageInfo.packageName); |
| 201 | out.println("Package-Version: " + packageInfo.versionCode); |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 202 | } |
Brian Carlstrom | c9ad7c6 | 2010-09-01 12:41:36 -0700 | [diff] [blame] | 203 | out.println("Build: " + Build.FINGERPRINT); |
Sen Hu | bde7570 | 2010-05-28 01:54:03 -0700 | [diff] [blame] | 204 | // single blank line means the end of snapshot header. |
Brian Carlstrom | c9ad7c6 | 2010-09-01 12:41:36 -0700 | [diff] [blame] | 205 | out.println(); |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 206 | } |
Bob Lee | e540833 | 2009-09-04 18:31:17 -0700 | [diff] [blame] | 207 | } |