1 /*
2  * Copyright (C) 2024 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.ondeviceintelligence;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 
22 /**
23  * This class represents the information related to an inference event to track the resource usage
24  * as a function of inference time.
25  *
26  * @hide
27  */
28 public class InferenceInfo implements Parcelable {
29 
30     /**
31      * Uid for the caller app.
32      */
33     private final int uid;
34 
35     /**
36      * Inference start time (milliseconds from the epoch time).
37      */
38     private final long startTimeMs;
39 
40     /**
41      * Inference end time (milliseconds from the epoch time).
42      */
43     private final long endTimeMs;
44 
45     /**
46      * Suspended time in milliseconds.
47      */
48     private final long suspendedTimeMs;
49 
50     /**
51      * Constructs an InferenceInfo object with the specified parameters.
52      *
53      * @param uid             Uid for the caller app.
54      * @param startTimeMs     Inference start time (milliseconds from the epoch time).
55      * @param endTimeMs       Inference end time (milliseconds from the epoch time).
56      * @param suspendedTimeMs Suspended time in milliseconds.
57      */
InferenceInfo(int uid, long startTimeMs, long endTimeMs, long suspendedTimeMs)58     public InferenceInfo(int uid, long startTimeMs, long endTimeMs,
59             long suspendedTimeMs) {
60         this.uid = uid;
61         this.startTimeMs = startTimeMs;
62         this.endTimeMs = endTimeMs;
63         this.suspendedTimeMs = suspendedTimeMs;
64     }
65 
66     /**
67      * Constructs an InferenceInfo object from a Parcel.
68      *
69      * @param in The Parcel to read the object's data from.
70      */
InferenceInfo(Parcel in)71     protected InferenceInfo(Parcel in) {
72         uid = in.readInt();
73         startTimeMs = in.readLong();
74         endTimeMs = in.readLong();
75         suspendedTimeMs = in.readLong();
76     }
77 
78 
79     /**
80      * Writes the object's data to the provided Parcel.
81      *
82      * @param dest The Parcel to write the object's data to.
83      * @param flags Additional flags about how the object should be written.
84      */
85     @Override
writeToParcel(Parcel dest, int flags)86     public void writeToParcel(Parcel dest, int flags) {
87         dest.writeInt(uid);
88         dest.writeLong(startTimeMs);
89         dest.writeLong(endTimeMs);
90         dest.writeLong(suspendedTimeMs);
91     }
92 
93     /**
94      * Returns the UID for the caller app.
95      *
96      * @return the UID for the caller app.
97      */
getUid()98     public int getUid() {
99         return uid;
100     }
101 
102     /**
103      * Returns the inference start time in milliseconds from the epoch time.
104      *
105      * @return the inference start time in milliseconds from the epoch time.
106      */
getStartTimeMs()107     public long getStartTimeMs() {
108         return startTimeMs;
109     }
110 
111     /**
112      * Returns the inference end time in milliseconds from the epoch time.
113      *
114      * @return the inference end time in milliseconds from the epoch time.
115      */
getEndTimeMs()116     public long getEndTimeMs() {
117         return endTimeMs;
118     }
119 
120     /**
121      * Returns the suspended time in milliseconds.
122      *
123      * @return the suspended time in milliseconds.
124      */
getSuspendedTimeMs()125     public long getSuspendedTimeMs() {
126         return suspendedTimeMs;
127     }
128 
129     @Override
describeContents()130     public int describeContents() {
131         return 0;
132     }
133 
134 
135     public static final @android.annotation.NonNull Parcelable.Creator<InferenceInfo> CREATOR
136             = new Parcelable.Creator<InferenceInfo>() {
137         @Override
138         public InferenceInfo[] newArray(int size) {
139             return new InferenceInfo[size];
140         }
141 
142         @Override
143         public InferenceInfo createFromParcel(@android.annotation.NonNull Parcel in) {
144             return new InferenceInfo(in);
145         }
146     };
147 
148     /**
149      * Builder class for creating instances of {@link InferenceInfo}.
150      */
151     public static class Builder {
152         private int uid;
153         private long startTimeMs;
154         private long endTimeMs;
155         private long suspendedTimeMs;
156 
157         /**
158          * Sets the UID for the caller app.
159          *
160          * @param uid the UID for the caller app.
161          * @return the Builder instance.
162          */
setUid(int uid)163         public Builder setUid(int uid) {
164             this.uid = uid;
165             return this;
166         }
167 
168         /**
169          * Sets the inference start time in milliseconds from the epoch time.
170          *
171          * @param startTimeMs the inference start time in milliseconds from the epoch time.
172          * @return the Builder instance.
173          */
setStartTimeMs(long startTimeMs)174         public Builder setStartTimeMs(long startTimeMs) {
175             this.startTimeMs = startTimeMs;
176             return this;
177         }
178 
179         /**
180          * Sets the inference end time in milliseconds from the epoch time.
181          *
182          * @param endTimeMs the inference end time in milliseconds from the epoch time.
183          * @return the Builder instance.
184          */
setEndTimeMs(long endTimeMs)185         public Builder setEndTimeMs(long endTimeMs) {
186             this.endTimeMs = endTimeMs;
187             return this;
188         }
189 
190         /**
191          * Sets the suspended time in milliseconds.
192          *
193          * @param suspendedTimeMs the suspended time in milliseconds.
194          * @return the Builder instance.
195          */
setSuspendedTimeMs(long suspendedTimeMs)196         public Builder setSuspendedTimeMs(long suspendedTimeMs) {
197             this.suspendedTimeMs = suspendedTimeMs;
198             return this;
199         }
200 
201         /**
202          * Builds and returns an instance of {@link InferenceInfo}.
203          *
204          * @return an instance of {@link InferenceInfo}.
205          */
build()206         public InferenceInfo build() {
207             return new InferenceInfo(uid, startTimeMs, endTimeMs,
208                     suspendedTimeMs);
209         }
210     }
211 }
212