ProfilerInfo.java revision 2b073a0e897a7f48be6fc4e915e4171b7bdfb2b9
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
17package android.app;
18
19import android.os.Parcel;
20import android.os.ParcelFileDescriptor;
21import android.os.Parcelable;
22import android.util.Slog;
23
24import java.io.IOException;
25
26/**
27 * System private API for passing profiler settings.
28 *
29 * {@hide}
30 */
31public class ProfilerInfo implements Parcelable {
32
33    private static final String TAG = "ProfilerInfo";
34
35    /* Name of profile output file. */
36    public final String profileFile;
37
38    /* File descriptor for profile output file, can be null. */
39    public ParcelFileDescriptor profileFd;
40
41    /* Indicates sample profiling when nonzero, interval in microseconds. */
42    public final int samplingInterval;
43
44    /* Automatically stop the profiler when the app goes idle. */
45    public final boolean autoStopProfiler;
46
47    /*
48     * Indicates whether to stream the profiling info to the out file continuously.
49     */
50    public final boolean streamingOutput;
51
52    public ProfilerInfo(String filename, ParcelFileDescriptor fd, int interval, boolean autoStop,
53            boolean streaming) {
54        profileFile = filename;
55        profileFd = fd;
56        samplingInterval = interval;
57        autoStopProfiler = autoStop;
58        streamingOutput = streaming;
59    }
60
61    public ProfilerInfo(ProfilerInfo in) {
62        profileFile = in.profileFile;
63        profileFd = in.profileFd;
64        samplingInterval = in.samplingInterval;
65        autoStopProfiler = in.autoStopProfiler;
66        streamingOutput = in.streamingOutput;
67    }
68
69    /**
70     * Close profileFd, if it is open. The field will be null after a call to this function.
71     */
72    public void closeFd() {
73        if (profileFd != null) {
74            try {
75                profileFd.close();
76            } catch (IOException e) {
77                Slog.w(TAG, "Failure closing profile fd", e);
78            }
79            profileFd = null;
80        }
81    }
82
83    @Override
84    public int describeContents() {
85        if (profileFd != null) {
86            return profileFd.describeContents();
87        } else {
88            return 0;
89        }
90    }
91
92    @Override
93    public void writeToParcel(Parcel out, int flags) {
94        out.writeString(profileFile);
95        if (profileFd != null) {
96            out.writeInt(1);
97            profileFd.writeToParcel(out, flags);
98        } else {
99            out.writeInt(0);
100        }
101        out.writeInt(samplingInterval);
102        out.writeInt(autoStopProfiler ? 1 : 0);
103        out.writeInt(streamingOutput ? 1 : 0);
104    }
105
106    public static final Parcelable.Creator<ProfilerInfo> CREATOR =
107            new Parcelable.Creator<ProfilerInfo>() {
108                @Override
109                public ProfilerInfo createFromParcel(Parcel in) {
110                    return new ProfilerInfo(in);
111                }
112
113                @Override
114                public ProfilerInfo[] newArray(int size) {
115                    return new ProfilerInfo[size];
116                }
117            };
118
119    private ProfilerInfo(Parcel in) {
120        profileFile = in.readString();
121        profileFd = in.readInt() != 0 ? ParcelFileDescriptor.CREATOR.createFromParcel(in) : null;
122        samplingInterval = in.readInt();
123        autoStopProfiler = in.readInt() != 0;
124        streamingOutput = in.readInt() != 0;
125    }
126}
127