Home | History | Annotate | Download | only in rule
      1 /*
      2  * Copyright (C) 2016 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.loganalysis.rule;
     18 
     19 import com.android.loganalysis.item.BugreportItem;
     20 import com.android.loganalysis.item.InterruptItem;
     21 import com.android.loganalysis.item.InterruptItem.InterruptInfoItem;
     22 import com.android.loganalysis.item.InterruptItem.InterruptCategory;
     23 
     24 import java.util.concurrent.TimeUnit;
     25 import java.util.ArrayList;
     26 import java.util.List;
     27 
     28 import org.json.JSONException;
     29 import org.json.JSONObject;
     30 
     31 /**
     32  * Rules definition for Interrupt rule
     33  */
     34 public class InterruptRule extends AbstractPowerRule {
     35 
     36     private static final String INTERRUPT_ANALYSIS = "INTERRUPT_ANALYSIS";
     37     private static final long INTERRUPT_THRESHOLD_MS = 120000;
     38 
     39     private List<InterruptInfoItem> mOffendingInterruptsList;
     40 
     41     public InterruptRule (BugreportItem bugreportItem) {
     42         super(bugreportItem);
     43     }
     44 
     45     @Override
     46     public void applyRule() {
     47         mOffendingInterruptsList = new ArrayList<InterruptInfoItem>();
     48         InterruptItem interruptItem = getDetailedAnalysisItem().getInterruptItem();
     49         if (interruptItem == null || getTimeOnBattery() < 0) {
     50             return;
     51         }
     52         for (InterruptInfoItem interrupts : interruptItem.getInterrupts()) {
     53             final long interruptsPerMs = getTimeOnBattery()/interrupts.getInterruptCount();
     54             if (interruptsPerMs < INTERRUPT_THRESHOLD_MS) {
     55                 mOffendingInterruptsList.add(interrupts);
     56             }
     57         }
     58     }
     59 
     60     @Override
     61     public JSONObject getAnalysis() {
     62         JSONObject interruptAnalysis = new JSONObject();
     63         StringBuilder analysis = new StringBuilder();
     64         if (mOffendingInterruptsList == null || mOffendingInterruptsList.size() <= 0) {
     65             analysis.append(String.format(
     66                     "No interrupts woke up device more frequent than %d secs.",
     67                     TimeUnit.MILLISECONDS.toSeconds(INTERRUPT_THRESHOLD_MS)));
     68         } else {
     69             for (InterruptInfoItem interrupts : mOffendingInterruptsList) {
     70                 if (interrupts.getCategory() != InterruptCategory.UNKNOWN_INTERRUPT) {
     71                     analysis.append(String.format("Frequent interrupts from %s (%s). ",
     72                             interrupts.getCategory(),
     73                             interrupts.getName()));
     74                 } else {
     75                     analysis.append(String.format("Frequent interrupts from %s. ",
     76                             interrupts.getName()));
     77                 }
     78             }
     79         }
     80         try {
     81             interruptAnalysis.put(INTERRUPT_ANALYSIS, analysis.toString().trim());
     82         } catch (JSONException e) {
     83             // do nothing
     84         }
     85         return interruptAnalysis;
     86     }
     87 }
     88