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 17 package com.android.internal.app.procstats; 18 19 import static com.android.internal.app.procstats.ProcessStats.PSS_SAMPLE_COUNT; 20 import static com.android.internal.app.procstats.ProcessStats.PSS_MINIMUM; 21 import static com.android.internal.app.procstats.ProcessStats.PSS_AVERAGE; 22 import static com.android.internal.app.procstats.ProcessStats.PSS_MAXIMUM; 23 import static com.android.internal.app.procstats.ProcessStats.PSS_USS_MINIMUM; 24 import static com.android.internal.app.procstats.ProcessStats.PSS_USS_AVERAGE; 25 import static com.android.internal.app.procstats.ProcessStats.PSS_USS_MAXIMUM; 26 import static com.android.internal.app.procstats.ProcessStats.PSS_COUNT; 27 28 /** 29 * Class to accumulate PSS data. 30 */ 31 public class PssTable extends SparseMappingTable.Table { 32 /** 33 * Construct the PssTable with 'tableData' as backing store 34 * for the longs data. 35 */ 36 public PssTable(SparseMappingTable tableData) { 37 super(tableData); 38 } 39 40 /** 41 * Merge the the values from the other table into this one. 42 */ 43 public void mergeStats(PssTable that) { 44 final int N = that.getKeyCount(); 45 for (int i=0; i<N; i++) { 46 final int key = that.getKeyAt(i); 47 final int state = SparseMappingTable.getIdFromKey(key); 48 mergeStats(state, (int)that.getValue(key, PSS_SAMPLE_COUNT), 49 that.getValue(key, PSS_MINIMUM), 50 that.getValue(key, PSS_AVERAGE), 51 that.getValue(key, PSS_MAXIMUM), 52 that.getValue(key, PSS_USS_MINIMUM), 53 that.getValue(key, PSS_USS_AVERAGE), 54 that.getValue(key, PSS_USS_MAXIMUM)); 55 } 56 } 57 58 /** 59 * Merge the supplied PSS data in. The new min pss will be the minimum of the existing 60 * one and the new one, the average will now incorporate the new average, etc. 61 */ 62 public void mergeStats(int state, int inCount, long minPss, long avgPss, long maxPss, 63 long minUss, long avgUss, long maxUss) { 64 final int key = getOrAddKey((byte)state, PSS_COUNT); 65 final long count = getValue(key, PSS_SAMPLE_COUNT); 66 if (count == 0) { 67 setValue(key, PSS_SAMPLE_COUNT, inCount); 68 setValue(key, PSS_MINIMUM, minPss); 69 setValue(key, PSS_AVERAGE, avgPss); 70 setValue(key, PSS_MAXIMUM, maxPss); 71 setValue(key, PSS_USS_MINIMUM, minUss); 72 setValue(key, PSS_USS_AVERAGE, avgUss); 73 setValue(key, PSS_USS_MAXIMUM, maxUss); 74 } else { 75 setValue(key, PSS_SAMPLE_COUNT, count + inCount); 76 77 long val; 78 79 val = getValue(key, PSS_MINIMUM); 80 if (val > minPss) { 81 setValue(key, PSS_MINIMUM, minPss); 82 } 83 84 val = getValue(key, PSS_AVERAGE); 85 setValue(key, PSS_AVERAGE, 86 (long)(((val*(double)count)+(avgPss*(double)inCount)) / (count+inCount))); 87 88 val = getValue(key, PSS_MAXIMUM); 89 if (val < maxPss) { 90 setValue(key, PSS_MAXIMUM, maxPss); 91 } 92 93 val = getValue(key, PSS_USS_MINIMUM); 94 if (val > minUss) { 95 setValue(key, PSS_USS_MINIMUM, minUss); 96 } 97 98 val = getValue(key, PSS_USS_AVERAGE); 99 setValue(key, PSS_AVERAGE, 100 (long)(((val*(double)count)+(avgUss*(double)inCount)) / (count+inCount))); 101 102 val = getValue(key, PSS_USS_MAXIMUM); 103 if (val < maxUss) { 104 setValue(key, PSS_USS_MAXIMUM, maxUss); 105 } 106 } 107 } 108 } 109