1 /* 2 * Copyright (C) 2013 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 package com.android.loganalysis.item; 17 18 import com.android.loganalysis.parser.KernelLogParser; 19 20 import org.json.JSONArray; 21 import org.json.JSONException; 22 import org.json.JSONObject; 23 24 import java.util.Arrays; 25 import java.util.HashSet; 26 import java.util.LinkedList; 27 import java.util.List; 28 import java.util.Set; 29 30 /** 31 * A {@link IItem} used to store kernel log info. 32 */ 33 public class KernelLogItem extends GenericItem { 34 35 /** Constant for JSON output */ 36 public static final String START_TIME = "START_TIME"; 37 /** Constant for JSON output */ 38 public static final String STOP_TIME = "STOP_TIME"; 39 /** Constant for JSON output */ 40 public static final String EVENTS = "EVENTS"; 41 42 private static final Set<String> ATTRIBUTES = new HashSet<String>(Arrays.asList( 43 START_TIME, STOP_TIME, EVENTS)); 44 45 @SuppressWarnings("serial") 46 private class ItemList extends LinkedList<MiscKernelLogItem> {} 47 48 /** 49 * The constructor for {@link KernelLogItem}. 50 */ KernelLogItem()51 public KernelLogItem() { 52 super(ATTRIBUTES); 53 54 setAttribute(START_TIME, Double.valueOf(0.0)); 55 setAttribute(STOP_TIME, Double.valueOf(0.0)); 56 setAttribute(EVENTS, new ItemList()); 57 } 58 59 /** 60 * Get the start time of the kernel log. 61 */ getStartTime()62 public Double getStartTime() { 63 return (Double) getAttribute(START_TIME); 64 } 65 66 /** 67 * Set the start time of the kernel log. 68 */ setStartTime(Double time)69 public void setStartTime(Double time) { 70 setAttribute(START_TIME, time); 71 } 72 73 /** 74 * Get the stop time of the kernel log. 75 */ getStopTime()76 public Double getStopTime() { 77 return (Double) getAttribute(STOP_TIME); 78 } 79 80 /** 81 * Set the stop time of the kernel log. 82 */ setStopTime(Double time)83 public void setStopTime(Double time) { 84 setAttribute(STOP_TIME, time); 85 } 86 87 /** 88 * Get the list of all {@link MiscKernelLogItem} events. 89 */ getEvents()90 public List<MiscKernelLogItem> getEvents() { 91 return (ItemList) getAttribute(EVENTS); 92 } 93 94 /** 95 * Add an {@link MiscKernelLogItem} event to the end of the list of events. 96 */ addEvent(MiscKernelLogItem event)97 public void addEvent(MiscKernelLogItem event) { 98 // Only take the first kernel reset 99 if (KernelLogParser.KERNEL_RESET.equals(event.getCategory()) && 100 !getMiscEvents(KernelLogParser.KERNEL_RESET).isEmpty()) { 101 return; 102 } 103 ((ItemList) getAttribute(EVENTS)).add(event); 104 } 105 106 /** 107 * Get the list of all {@link MiscKernelLogItem} events for a category. 108 */ getMiscEvents(String category)109 public List<MiscKernelLogItem> getMiscEvents(String category) { 110 List<MiscKernelLogItem> items = new LinkedList<MiscKernelLogItem>(); 111 for (MiscKernelLogItem item : getEvents()) { 112 if (item.getCategory().equals(category)) { 113 items.add(item); 114 } 115 } 116 return items; 117 } 118 119 /** 120 * Get the list of all {@link SELinuxItem} events. 121 */ getSELinuxEvents()122 public List<SELinuxItem> getSELinuxEvents() { 123 List<SELinuxItem> items = new LinkedList<SELinuxItem>(); 124 for (MiscKernelLogItem item : getEvents()) { 125 if (item instanceof SELinuxItem) { 126 items.add((SELinuxItem)item); 127 } 128 } 129 return items; 130 } 131 132 /** 133 * Get the list of all {@link LowMemoryKillerItem} events. 134 */ getLowMemoryKillerEvents()135 public List<LowMemoryKillerItem> getLowMemoryKillerEvents() { 136 List<LowMemoryKillerItem> items = new LinkedList<LowMemoryKillerItem>(); 137 for (MiscKernelLogItem item : getEvents()) { 138 if (item instanceof LowMemoryKillerItem) { 139 items.add((LowMemoryKillerItem)item); 140 } 141 } 142 return items; 143 } 144 145 /** 146 * Get the list of all {@link PageAllocationFailureItem} events. 147 */ getPageAllocationFailureEvents()148 public List<PageAllocationFailureItem> getPageAllocationFailureEvents() { 149 List<PageAllocationFailureItem> items = new LinkedList<PageAllocationFailureItem>(); 150 for (MiscKernelLogItem item : getEvents()) { 151 if (item instanceof PageAllocationFailureItem) { 152 items.add((PageAllocationFailureItem)item); 153 } 154 } 155 return items; 156 } 157 158 /** 159 * {@inheritDoc} 160 */ 161 @Override toJson()162 public JSONObject toJson() { 163 JSONObject output = super.toJson(); 164 JSONArray events = new JSONArray(); 165 for (MiscKernelLogItem event : getEvents()) { 166 events.put(event.toJson()); 167 } 168 169 try { 170 output.put(EVENTS, events); 171 } catch (JSONException e) { 172 // Ignore 173 } 174 return output; 175 } 176 } 177