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    /**
53     * Denotes an agent (and its parameters) to attach for profiling.
54     */
55    public final String agent;
56
57    public ProfilerInfo(String filename, ParcelFileDescriptor fd, int interval, boolean autoStop,
58            boolean streaming, String agent) {
59        profileFile = filename;
60        profileFd = fd;
61        samplingInterval = interval;
62        autoStopProfiler = autoStop;
63        streamingOutput = streaming;
64        this.agent = agent;
65    }
66
67    public ProfilerInfo(ProfilerInfo in) {
68        profileFile = in.profileFile;
69        profileFd = in.profileFd;
70        samplingInterval = in.samplingInterval;
71        autoStopProfiler = in.autoStopProfiler;
72        streamingOutput = in.streamingOutput;
73        agent = in.agent;
74    }
75
76    /**
77     * Close profileFd, if it is open. The field will be null after a call to this function.
78     */
79    public void closeFd() {
80        if (profileFd != null) {
81            try {
82                profileFd.close();
83            } catch (IOException e) {
84                Slog.w(TAG, "Failure closing profile fd", e);
85            }
86            profileFd = null;
87        }
88    }
89
90    @Override
91    public int describeContents() {
92        if (profileFd != null) {
93            return profileFd.describeContents();
94        } else {
95            return 0;
96        }
97    }
98
99    @Override
100    public void writeToParcel(Parcel out, int flags) {
101        out.writeString(profileFile);
102        if (profileFd != null) {
103            out.writeInt(1);
104            profileFd.writeToParcel(out, flags);
105        } else {
106            out.writeInt(0);
107        }
108        out.writeInt(samplingInterval);
109        out.writeInt(autoStopProfiler ? 1 : 0);
110        out.writeInt(streamingOutput ? 1 : 0);
111        out.writeString(agent);
112    }
113
114    public static final Parcelable.Creator<ProfilerInfo> CREATOR =
115            new Parcelable.Creator<ProfilerInfo>() {
116                @Override
117                public ProfilerInfo createFromParcel(Parcel in) {
118                    return new ProfilerInfo(in);
119                }
120
121                @Override
122                public ProfilerInfo[] newArray(int size) {
123                    return new ProfilerInfo[size];
124                }
125            };
126
127    private ProfilerInfo(Parcel in) {
128        profileFile = in.readString();
129        profileFd = in.readInt() != 0 ? ParcelFileDescriptor.CREATOR.createFromParcel(in) : null;
130        samplingInterval = in.readInt();
131        autoStopProfiler = in.readInt() != 0;
132        streamingOutput = in.readInt() != 0;
133        agent = in.readString();
134    }
135}
136