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