• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 org.json.JSONException;
19 import org.json.JSONObject;
20 
21 import java.util.Arrays;
22 import java.util.HashSet;
23 import java.util.Map;
24 
25 /**
26  * Contains a list of processes and which type of memory they are using, both in foreground and
27  * background.
28  */
29 public class MemoryHealthItem extends GenericItem {
30     private static final String BACKGROUND = "background";
31     private static final String FOREGROUND = "foreground";
32 
33     public static final String DALVIK_AVG = "dalvik_avg";
34     public static final String NATIVE_AVG = "native_avg";
35     public static final String  PSS_AVG = "pss_avg";
36     public static final String  DALVIK_PEAK = "dalvik_peak";
37     public static final String NATIVE_PEAK = "native_peak";
38     public static final String PSS_PEAK = "pss_peak";
39 
40     public static final String SUMMARY_JAVA_HEAP_AVG = "summary_java_heap_avg";
41     public static final String SUMMARY_NATIVE_HEAP_AVG = "summary_native_heap_avg";
42     public static final String SUMMARY_CODE_AVG = "summary_code_avg";
43     public static final String SUMMARY_STACK_AVG = "summary_stack_avg";
44     public static final String SUMMARY_GRAPHICS_AVG = "summary_graphics_avg";
45     public static final String SUMMARY_OTHER_AVG = "summary_other_avg";
46     public static final String SUMMARY_SYSTEM_AVG = "summary_system_avg";
47     public static final String SUMMARY_OVERALL_PSS_AVG = "summary_overall_pss_avg";
48 
MemoryHealthItem(Map<String, Map<String, Long>> foreground, Map<String, Map<String, Long>> background)49     public MemoryHealthItem(Map<String, Map<String, Long>> foreground,
50             Map<String, Map<String, Long>> background) {
51         super(new HashSet<String>(Arrays.asList(FOREGROUND, BACKGROUND)));
52         super.setAttribute(FOREGROUND, foreground);
53         super.setAttribute(BACKGROUND, background);
54     }
55 
56     /**
57      * Returns breakdown of memory usage of foreground processes.
58      * @return Map that stores breakdown of memory usage
59      */
60     @SuppressWarnings("unchecked")
getForeground()61     public Map<String, Map<String, Long>> getForeground() {
62         return (Map<String, Map<String, Long>>) super.getAttribute(FOREGROUND);
63     }
64 
65     /**
66      * Returns breakdown of memory usage of foreground processes.
67      * @return Map that stores breakdown of memory usage
68      */
69     @SuppressWarnings("unchecked")
getBackground()70     public Map<String, Map<String, Long>> getBackground() {
71         return (Map<String, Map<String, Long>>) super.getAttribute(BACKGROUND);
72     }
73 
74     @Override
toJson()75     public JSONObject toJson() {
76         JSONObject memoryHealth = new JSONObject();
77         try {
78             memoryHealth.put(FOREGROUND, mapToJson(getForeground()));
79             memoryHealth.put(BACKGROUND, mapToJson(getBackground()));
80         } catch (JSONException e) {
81             // ignore
82         }
83         return memoryHealth;
84     }
85 
mapToJson(Map<String, Map<String, Long>> map)86     private JSONObject mapToJson(Map<String, Map<String, Long>> map) {
87         JSONObject out = new JSONObject();
88         for (Map.Entry<String, Map<String, Long>> entry : map.entrySet()) {
89             try {
90                 out.put(entry.getKey(), processToJson(entry.getValue()));
91             } catch (JSONException e) {
92                 // ignore
93             }
94         }
95         return out;
96     }
97 
processToJson(Map<String, Long> map)98     private JSONObject processToJson(Map<String, Long> map) {
99         JSONObject out = new JSONObject();
100         for (Map.Entry<String, Long> entry : map.entrySet()) {
101             try {
102                 out.put(entry.getKey(), entry.getValue());
103             } catch (JSONException e) {
104                 // ignore
105             }
106         }
107         return out;
108     }
109 }
110