1 /*
2  * Copyright (C) 2014 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 android.app;
18 
19 import android.annotation.Nullable;
20 import android.os.Parcel;
21 import android.os.ParcelFileDescriptor;
22 import android.os.Parcelable;
23 import android.util.Slog;
24 import android.util.proto.ProtoOutputStream;
25 
26 import java.io.IOException;
27 import java.util.Objects;
28 
29 /**
30  * System private API for passing profiler settings.
31  *
32  * {@hide}
33  */
34 public class ProfilerInfo implements Parcelable {
35     // Version of the profiler output
36     public static final int OUTPUT_VERSION_DEFAULT = 1;
37     // CLOCK_TYPE_DEFAULT chooses the default used by ART. ART uses CLOCK_TYPE_DUAL by default (see
38     // kDefaultTraceClockSource in art/runtime/runtime_globals.h).
39     public static final int CLOCK_TYPE_DEFAULT = 0x000;
40     // The values of these constants are chosen such that they correspond to the flags passed to
41     // VMDebug.startMethodTracing to choose the corresponding clock type (see
42     // core/java/android/app/ActivityThread.java).
43     // The flag values are defined in ART (see TraceFlag in art/runtime/trace.h).
44     public static final int CLOCK_TYPE_WALL = 0x010;
45     public static final int CLOCK_TYPE_THREAD_CPU = 0x100;
46     public static final int CLOCK_TYPE_DUAL = 0x110;
47     // The second and third bits of the flags field specify the trace format version. This should
48     // match with kTraceFormatVersionShift defined in art/runtime/trace.h.
49     public static final int TRACE_FORMAT_VERSION_SHIFT = 1;
50 
51     private static final String TAG = "ProfilerInfo";
52 
53     /* Name of profile output file. */
54     public final String profileFile;
55 
56     /* File descriptor for profile output file, can be null. */
57     public ParcelFileDescriptor profileFd;
58 
59     /* Indicates sample profiling when nonzero, interval in microseconds. */
60     public final int samplingInterval;
61 
62     /* Automatically stop the profiler when the app goes idle. */
63     public final boolean autoStopProfiler;
64 
65     /*
66      * Indicates whether to stream the profiling info to the out file continuously.
67      */
68     public final boolean streamingOutput;
69 
70     /**
71      * Denotes an agent (and its parameters) to attach for profiling.
72      */
73     public final String agent;
74 
75     /**
76      * Whether the {@link agent} should be attached early (before bind-application) or during
77      * bind-application. Agents attached prior to binding cannot be loaded from the app's APK
78      * directly and must be given as an absolute path (or available in the default LD_LIBRARY_PATH).
79      * Agents attached during bind-application will miss early setup (e.g., resource initialization
80      * and classloader generation), but are searched in the app's library search path.
81      */
82     public final boolean attachAgentDuringBind;
83 
84     /**
85      * Indicates the clock source to be used for profiling. The source could be wallclock, thread
86      * cpu or both
87      */
88     public final int clockType;
89 
90     /**
91      * Indicates the version of profiler output.
92      */
93     public final int profilerOutputVersion;
94 
ProfilerInfo(String filename, ParcelFileDescriptor fd, int interval, boolean autoStop, boolean streaming, String agent, boolean attachAgentDuringBind, int clockType, int profilerOutputVersion)95     public ProfilerInfo(String filename, ParcelFileDescriptor fd, int interval, boolean autoStop,
96             boolean streaming, String agent, boolean attachAgentDuringBind, int clockType,
97             int profilerOutputVersion) {
98         profileFile = filename;
99         profileFd = fd;
100         samplingInterval = interval;
101         autoStopProfiler = autoStop;
102         streamingOutput = streaming;
103         this.clockType = clockType;
104         this.agent = agent;
105         this.attachAgentDuringBind = attachAgentDuringBind;
106         this.profilerOutputVersion = profilerOutputVersion;
107     }
108 
ProfilerInfo(ProfilerInfo in)109     public ProfilerInfo(ProfilerInfo in) {
110         profileFile = in.profileFile;
111         profileFd = in.profileFd;
112         samplingInterval = in.samplingInterval;
113         autoStopProfiler = in.autoStopProfiler;
114         streamingOutput = in.streamingOutput;
115         agent = in.agent;
116         attachAgentDuringBind = in.attachAgentDuringBind;
117         clockType = in.clockType;
118         profilerOutputVersion = in.profilerOutputVersion;
119     }
120 
121     /**
122      * Get the value for the clock type corresponding to the option string passed to the activity
123      * manager. am profile start / am start-activity start-profiler commands accept clock-type
124      * option to choose the source of timestamps when profiling. This function maps the option
125      * string to the value of flags that is used when calling VMDebug.startMethodTracing
126      */
getClockTypeFromString(String type)127     public static int getClockTypeFromString(String type) {
128         if ("thread-cpu".equals(type)) {
129             return CLOCK_TYPE_THREAD_CPU;
130         } else if ("wall".equals(type)) {
131             return CLOCK_TYPE_WALL;
132         } else if ("dual".equals(type)) {
133             return CLOCK_TYPE_DUAL;
134         } else {
135             return CLOCK_TYPE_DEFAULT;
136         }
137     }
138 
139     /**
140      * Get the flags that need to be passed to VMDebug.startMethodTracing to specify the desired
141      * output format.
142      */
getFlagsForOutputVersion(int version)143     public static int getFlagsForOutputVersion(int version) {
144         // Only two version 1 and version 2 are supported. Just use the default if we see an unknown
145         // version.
146         if (version != 1 || version != 2) {
147             version = OUTPUT_VERSION_DEFAULT;
148         }
149 
150         // The encoded version in the flags starts from 0, where as the version that we read from
151         // user starts from 1. So, subtract one before encoding it in the flags.
152         return (version - 1) << TRACE_FORMAT_VERSION_SHIFT;
153     }
154 
155     /**
156      * Return a new ProfilerInfo instance, with fields populated from this object,
157      * and {@link agent} and {@link attachAgentDuringBind} as given.
158      */
setAgent(String agent, boolean attachAgentDuringBind)159     public ProfilerInfo setAgent(String agent, boolean attachAgentDuringBind) {
160         return new ProfilerInfo(this.profileFile, this.profileFd, this.samplingInterval,
161                 this.autoStopProfiler, this.streamingOutput, agent, attachAgentDuringBind,
162                 this.clockType, this.profilerOutputVersion);
163     }
164 
165     /**
166      * Close profileFd, if it is open. The field will be null after a call to this function.
167      */
closeFd()168     public void closeFd() {
169         if (profileFd != null) {
170             try {
171                 profileFd.close();
172             } catch (IOException e) {
173                 Slog.w(TAG, "Failure closing profile fd", e);
174             }
175             profileFd = null;
176         }
177     }
178 
179     @Override
describeContents()180     public int describeContents() {
181         if (profileFd != null) {
182             return profileFd.describeContents();
183         } else {
184             return 0;
185         }
186     }
187 
188     @Override
writeToParcel(Parcel out, int flags)189     public void writeToParcel(Parcel out, int flags) {
190         out.writeString(profileFile);
191         if (profileFd != null) {
192             out.writeInt(1);
193             profileFd.writeToParcel(out, flags);
194         } else {
195             out.writeInt(0);
196         }
197         out.writeInt(samplingInterval);
198         out.writeInt(autoStopProfiler ? 1 : 0);
199         out.writeInt(streamingOutput ? 1 : 0);
200         out.writeString(agent);
201         out.writeBoolean(attachAgentDuringBind);
202         out.writeInt(clockType);
203         out.writeInt(profilerOutputVersion);
204     }
205 
206     /** @hide */
dumpDebug(ProtoOutputStream proto, long fieldId)207     public void dumpDebug(ProtoOutputStream proto, long fieldId) {
208         final long token = proto.start(fieldId);
209         proto.write(ProfilerInfoProto.PROFILE_FILE, profileFile);
210         if (profileFd != null) {
211             proto.write(ProfilerInfoProto.PROFILE_FD, profileFd.getFd());
212         }
213         proto.write(ProfilerInfoProto.SAMPLING_INTERVAL, samplingInterval);
214         proto.write(ProfilerInfoProto.AUTO_STOP_PROFILER, autoStopProfiler);
215         proto.write(ProfilerInfoProto.STREAMING_OUTPUT, streamingOutput);
216         proto.write(ProfilerInfoProto.AGENT, agent);
217         proto.write(ProfilerInfoProto.CLOCK_TYPE, clockType);
218         proto.write(ProfilerInfoProto.PROFILER_OUTPUT_VERSION, profilerOutputVersion);
219         proto.end(token);
220     }
221 
222     public static final @android.annotation.NonNull Parcelable.Creator<ProfilerInfo> CREATOR =
223             new Parcelable.Creator<ProfilerInfo>() {
224                 @Override
225                 public ProfilerInfo createFromParcel(Parcel in) {
226                     return new ProfilerInfo(in);
227                 }
228 
229                 @Override
230                 public ProfilerInfo[] newArray(int size) {
231                     return new ProfilerInfo[size];
232                 }
233             };
234 
ProfilerInfo(Parcel in)235     private ProfilerInfo(Parcel in) {
236         profileFile = in.readString();
237         profileFd = in.readInt() != 0 ? ParcelFileDescriptor.CREATOR.createFromParcel(in) : null;
238         samplingInterval = in.readInt();
239         autoStopProfiler = in.readInt() != 0;
240         streamingOutput = in.readInt() != 0;
241         agent = in.readString();
242         attachAgentDuringBind = in.readBoolean();
243         clockType = in.readInt();
244         profilerOutputVersion = in.readInt();
245     }
246 
247     @Override
equals(@ullable Object o)248     public boolean equals(@Nullable Object o) {
249         if (this == o) {
250             return true;
251         }
252         if (o == null || getClass() != o.getClass()) {
253             return false;
254         }
255         final ProfilerInfo other = (ProfilerInfo) o;
256         // TODO: Also check #profileFd for equality.
257         return Objects.equals(profileFile, other.profileFile)
258                 && autoStopProfiler == other.autoStopProfiler
259                 && samplingInterval == other.samplingInterval
260                 && streamingOutput == other.streamingOutput && Objects.equals(agent, other.agent)
261                 && clockType == other.clockType
262                 && profilerOutputVersion == other.profilerOutputVersion;
263     }
264 
265     @Override
hashCode()266     public int hashCode() {
267         int result = 17;
268         result = 31 * result + Objects.hashCode(profileFile);
269         result = 31 * result + samplingInterval;
270         result = 31 * result + (autoStopProfiler ? 1 : 0);
271         result = 31 * result + (streamingOutput ? 1 : 0);
272         result = 31 * result + Objects.hashCode(agent);
273         result = 31 * result + clockType;
274         result = 31 * result + profilerOutputVersion;
275         return result;
276     }
277 }
278