1 /*
2  * Copyright (C) 2006 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 com.android.internal.telephony;
18 
19 import android.annotation.TargetApi;
20 
21 import com.android.internal.annotations.VisibleForTesting;
22 import com.android.internal.telephony.util.TelephonyUtils;
23 import com.android.telephony.Rlog;
24 
25 @TargetApi(8)
26 public class RilWakelockInfo {
27     private final String LOG_TAG = RilWakelockInfo.class.getSimpleName();
28     private int mRilRequestSent;
29     private int mTokenNumber;
30     private long mRequestTime;
31     private long mResponseTime;
32 
33     /* If there are n requests waiting for a response for time t, the time attributed to
34     each request will be t/n. If the number of outstanding requests changes at time t1,
35     then we will compute the wakelock time till t1 and store it in mWakelockTimeAttributedSoFar
36     and update mConcurrentRequests. mLastAggregatedTime will be set to t1 and used to
37     compute the time taken for this request using the new mConcurrentRequests
38      */
39     private long mWakelockTimeAttributedSoFar;
40     private long mLastAggregatedTime;
41     private int mConcurrentRequests;
42 
43     @VisibleForTesting
getConcurrentRequests()44     public int getConcurrentRequests() {
45         return mConcurrentRequests;
46     }
47 
RilWakelockInfo(int rilRequest, int tokenNumber, int concurrentRequests, long requestTime)48     RilWakelockInfo(int rilRequest, int tokenNumber, int concurrentRequests, long requestTime) {
49         concurrentRequests = validateConcurrentRequests(concurrentRequests);
50         this.mRilRequestSent = rilRequest;
51         this.mTokenNumber = tokenNumber;
52         this.mConcurrentRequests = concurrentRequests;
53         this.mRequestTime = requestTime;
54         this.mWakelockTimeAttributedSoFar = 0;
55         this.mLastAggregatedTime = requestTime;
56     }
57 
validateConcurrentRequests(int concurrentRequests)58     private int validateConcurrentRequests(int concurrentRequests) {
59         if(concurrentRequests <= 0) {
60             if (TelephonyUtils.IS_DEBUGGABLE) {
61                 IllegalArgumentException e = new IllegalArgumentException(
62                     "concurrentRequests should always be greater than 0.");
63                 Rlog.e(LOG_TAG, e.toString());
64                 throw e;
65             } else {
66                 concurrentRequests = 1;
67             }
68         }
69         return concurrentRequests;
70     }
71 
getTokenNumber()72     int getTokenNumber() {
73         return mTokenNumber;
74     }
75 
getRilRequestSent()76     int getRilRequestSent() {
77         return mRilRequestSent;
78     }
79 
setResponseTime(long responseTime)80     void setResponseTime(long responseTime) {
81         updateTime(responseTime);
82         this.mResponseTime = responseTime;
83     }
84 
updateConcurrentRequests(int concurrentRequests, long time)85     void updateConcurrentRequests(int concurrentRequests, long time) {
86         concurrentRequests = validateConcurrentRequests(concurrentRequests);
87         updateTime(time);
88         mConcurrentRequests = concurrentRequests;
89     }
90 
updateTime(long time)91     synchronized void updateTime(long time) {
92         mWakelockTimeAttributedSoFar += (time - mLastAggregatedTime) / mConcurrentRequests;
93         mLastAggregatedTime = time;
94     }
95 
getWakelockTimeAttributedToClient()96     long getWakelockTimeAttributedToClient() {
97         return mWakelockTimeAttributedSoFar;
98     }
99 
100     @Override
toString()101     public String toString() {
102         return "WakelockInfo{" +
103                 "rilRequestSent=" + mRilRequestSent +
104                 ", tokenNumber=" + mTokenNumber +
105                 ", requestTime=" + mRequestTime +
106                 ", responseTime=" + mResponseTime +
107                 ", mWakelockTimeAttributed=" + mWakelockTimeAttributedSoFar +
108                 '}';
109     }
110 }
111