1 /*
2  * Copyright (C) 2023 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.os;
18 
19 import android.annotation.FlaggedApi;
20 
21 import java.util.Objects;
22 
23 /**
24  * WorkDuration contains the measured time in nano seconds of the workload
25  * in each component, see
26  * {@link PerformanceHintManager.Session#reportActualWorkDuration(WorkDuration)}.
27  *
28  * All timings should be in {@link SystemClock#uptimeNanos()} and measured in wall time.
29  */
30 @FlaggedApi(Flags.FLAG_ADPF_GPU_REPORT_ACTUAL_WORK_DURATION)
31 public final class WorkDuration {
32     long mActualTotalDurationNanos = 0;
33     long mWorkPeriodStartTimestampNanos = 0;
34     long mActualCpuDurationNanos = 0;
35     long mActualGpuDurationNanos = 0;
36 
WorkDuration()37     public WorkDuration() {}
38 
39     /**
40      * Constructor for testing.
41      *
42      * @hide
43      */
WorkDuration(long workPeriodStartTimestampNanos, long actualTotalDurationNanos, long actualCpuDurationNanos, long actualGpuDurationNanos)44     public WorkDuration(long workPeriodStartTimestampNanos,
45                       long actualTotalDurationNanos,
46                       long actualCpuDurationNanos,
47                       long actualGpuDurationNanos) {
48         mActualTotalDurationNanos = actualTotalDurationNanos;
49         mWorkPeriodStartTimestampNanos = workPeriodStartTimestampNanos;
50         mActualCpuDurationNanos = actualCpuDurationNanos;
51         mActualGpuDurationNanos = actualGpuDurationNanos;
52     }
53 
54     /**
55      * Sets the actual total duration in nanoseconds.
56      *
57      * All timings should be in {@link SystemClock#uptimeNanos()}.
58      */
setActualTotalDurationNanos(long actualTotalDurationNanos)59     public void setActualTotalDurationNanos(long actualTotalDurationNanos) {
60         if (actualTotalDurationNanos <= 0) {
61             throw new IllegalArgumentException(
62                 "the actual total duration should be greater than zero.");
63         }
64         mActualTotalDurationNanos = actualTotalDurationNanos;
65     }
66 
67     /**
68      * Sets the work period start timestamp in nanoseconds.
69      *
70      * All timings should be in {@link SystemClock#uptimeNanos()}.
71      */
setWorkPeriodStartTimestampNanos(long workPeriodStartTimestampNanos)72     public void setWorkPeriodStartTimestampNanos(long workPeriodStartTimestampNanos) {
73         if (workPeriodStartTimestampNanos <= 0) {
74             throw new IllegalArgumentException(
75                 "the work period start timestamp should be greater than zero.");
76         }
77         mWorkPeriodStartTimestampNanos = workPeriodStartTimestampNanos;
78     }
79 
80     /**
81      * Sets the actual CPU duration in nanoseconds.
82      *
83      * All timings should be in {@link SystemClock#uptimeNanos()}.
84      */
setActualCpuDurationNanos(long actualCpuDurationNanos)85     public void setActualCpuDurationNanos(long actualCpuDurationNanos) {
86         if (actualCpuDurationNanos < 0) {
87             throw new IllegalArgumentException(
88                 "the actual CPU duration should be greater than or equal to zero.");
89         }
90         mActualCpuDurationNanos = actualCpuDurationNanos;
91     }
92 
93     /**
94      * Sets the actual GPU duration in nanoseconds.
95      *
96      * All timings should be in {@link SystemClock#uptimeNanos()}.
97      */
setActualGpuDurationNanos(long actualGpuDurationNanos)98     public void setActualGpuDurationNanos(long actualGpuDurationNanos) {
99         if (actualGpuDurationNanos < 0) {
100             throw new IllegalArgumentException(
101                 "the actual GPU duration should be greater than or equal to zero.");
102         }
103         mActualGpuDurationNanos = actualGpuDurationNanos;
104     }
105 
106     /**
107      * Returns the actual total duration in nanoseconds.
108      *
109      * All timings should be in {@link SystemClock#uptimeNanos()}.
110      */
getActualTotalDurationNanos()111     public long getActualTotalDurationNanos() {
112         return mActualTotalDurationNanos;
113     }
114 
115     /**
116      * Returns the work period start timestamp based in nanoseconds.
117      *
118      * All timings should be in {@link SystemClock#uptimeNanos()}.
119      */
getWorkPeriodStartTimestampNanos()120     public long getWorkPeriodStartTimestampNanos() {
121         return mWorkPeriodStartTimestampNanos;
122     }
123 
124     /**
125      * Returns the actual CPU duration in nanoseconds.
126      *
127      * All timings should be in {@link SystemClock#uptimeNanos()}.
128      */
getActualCpuDurationNanos()129     public long getActualCpuDurationNanos() {
130         return mActualCpuDurationNanos;
131     }
132 
133     /**
134      * Returns the actual GPU duration in nanoseconds.
135      *
136      * All timings should be in {@link SystemClock#uptimeNanos()}.
137      */
getActualGpuDurationNanos()138     public long getActualGpuDurationNanos() {
139         return mActualGpuDurationNanos;
140     }
141 
142     @Override
equals(Object obj)143     public boolean equals(Object obj) {
144         if (obj == this) {
145             return true;
146         }
147         if (!(obj instanceof WorkDuration)) {
148             return false;
149         }
150         WorkDuration workDuration = (WorkDuration) obj;
151         return workDuration.mActualTotalDurationNanos == this.mActualTotalDurationNanos
152             && workDuration.mWorkPeriodStartTimestampNanos == this.mWorkPeriodStartTimestampNanos
153             && workDuration.mActualCpuDurationNanos == this.mActualCpuDurationNanos
154             && workDuration.mActualGpuDurationNanos == this.mActualGpuDurationNanos;
155     }
156 
157     @Override
hashCode()158     public int hashCode() {
159         return Objects.hash(mWorkPeriodStartTimestampNanos, mActualTotalDurationNanos,
160                             mActualCpuDurationNanos, mActualGpuDurationNanos);
161     }
162 }
163