1 /*
2  * Copyright (C) 2019 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.device.collectors;
18 
19 import android.device.collectors.annotations.OptionClass;
20 import android.os.Bundle;
21 
22 import androidx.annotation.VisibleForTesting;
23 
24 import com.android.helpers.ProcLoadHelper;
25 
26 /**
27  * A {@link ProcLoadListener} that waits until the proc/load threshold is met
28  * or timeout expires.
29  *
30  * Options:
31  * <p>-e proc-loadavg-threshold 1 : The threshold the system cpu load has
32  * to be less than or equal in last minute.
33  *
34  * <p>-e proc-loadavg-timeout 1000 :
35  * Timeout to wait before the threshold is met.
36  *
37  * <p>-e proc-loadavg-interval 100 :
38  * Interval frequency to check if the threshold is met or not.
39  *
40  */
41 @OptionClass(alias = "procload-collector")
42 public class ProcLoadListener extends BaseCollectionListener<Double> {
43 
44     private static final String TAG = ProcLoadListener.class.getSimpleName();
45     @VisibleForTesting
46     static final String PROC_LOAD_THRESHOLD = "proc-loadavg-threshold";
47     @VisibleForTesting
48     static final String PROC_THRESHOLD_TIMEOUT = "proc-loadavg-timeout";
49     @VisibleForTesting
50     static final String PROC_LOAD_INTERVAL = "proc-loadavg-interval";
51 
52     private ProcLoadHelper mProcLoadHelper = new ProcLoadHelper();
53 
ProcLoadListener()54     public ProcLoadListener() {
55         createHelperInstance(mProcLoadHelper);
56     }
57 
58     /**
59      * Constructor to simulate receiving the instrumentation arguments. Should not be used except
60      * for testing.
61      */
62     @VisibleForTesting
ProcLoadListener(Bundle args, ProcLoadHelper helper)63     public ProcLoadListener(Bundle args, ProcLoadHelper helper) {
64         super(args, helper);
65         mProcLoadHelper = helper;
66         createHelperInstance(mProcLoadHelper);
67     }
68 
69     /**
70      * Adds the options for total pss collector.
71      */
72     @Override
setupAdditionalArgs()73     public void setupAdditionalArgs() {
74         Bundle args = getArgsBundle();
75 
76         if (args.getString(PROC_LOAD_THRESHOLD) != null) {
77             mProcLoadHelper.setProcLoadThreshold(Double.parseDouble(args
78                     .getString(PROC_LOAD_THRESHOLD)));
79         }
80 
81         if (args.getString(PROC_THRESHOLD_TIMEOUT) != null) {
82             mProcLoadHelper.setProcLoadWaitTimeInMs(Long.parseLong(args
83                     .getString(PROC_THRESHOLD_TIMEOUT)));
84         }
85 
86         if (args.getString(PROC_LOAD_INTERVAL) != null) {
87             mProcLoadHelper.setProcLoadIntervalInMs(Long.parseLong(args
88                     .getString(PROC_LOAD_INTERVAL)));
89         }
90 
91     }
92 }
93