blob: fc93c3a0094e373b1580bd294dcfe202eafcc2b4 [file] [log] [blame]
Joe Onorato4eb64fd2016-03-21 15:30:09 -07001/*
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
17package com.android.internal.app.procstats;
18
Dianne Hackborne17b4452018-01-10 13:15:40 -080019import static com.android.internal.app.procstats.ProcessStats.PSS_RSS_AVERAGE;
20import static com.android.internal.app.procstats.ProcessStats.PSS_RSS_MAXIMUM;
21import static com.android.internal.app.procstats.ProcessStats.PSS_RSS_MINIMUM;
Joe Onorato4eb64fd2016-03-21 15:30:09 -070022import static com.android.internal.app.procstats.ProcessStats.PSS_SAMPLE_COUNT;
23import static com.android.internal.app.procstats.ProcessStats.PSS_MINIMUM;
24import static com.android.internal.app.procstats.ProcessStats.PSS_AVERAGE;
25import static com.android.internal.app.procstats.ProcessStats.PSS_MAXIMUM;
26import static com.android.internal.app.procstats.ProcessStats.PSS_USS_MINIMUM;
27import static com.android.internal.app.procstats.ProcessStats.PSS_USS_AVERAGE;
28import static com.android.internal.app.procstats.ProcessStats.PSS_USS_MAXIMUM;
29import static com.android.internal.app.procstats.ProcessStats.PSS_COUNT;
30
Dianne Hackborneaed0ba2018-08-08 17:10:17 -070031import android.service.procstats.ProcessStatsStateProto;
Dianne Hackborn99aeba82018-07-31 13:29:33 -070032import android.util.proto.ProtoOutputStream;
33import android.util.proto.ProtoUtils;
34
Joe Onorato4eb64fd2016-03-21 15:30:09 -070035/**
36 * Class to accumulate PSS data.
37 */
38public class PssTable extends SparseMappingTable.Table {
39 /**
40 * Construct the PssTable with 'tableData' as backing store
41 * for the longs data.
42 */
43 public PssTable(SparseMappingTable tableData) {
44 super(tableData);
45 }
46
47 /**
48 * Merge the the values from the other table into this one.
49 */
50 public void mergeStats(PssTable that) {
51 final int N = that.getKeyCount();
52 for (int i=0; i<N; i++) {
Dianne Hackborn99aeba82018-07-31 13:29:33 -070053 final int thatKey = that.getKeyAt(i);
54 final int state = SparseMappingTable.getIdFromKey(thatKey);
55
56 final int key = getOrAddKey((byte)state, PSS_COUNT);
57 final long[] stats = getArrayForKey(key);
58 final int statsIndex = SparseMappingTable.getIndexFromKey(key);
59
60 final long[] thatStats = that.getArrayForKey(thatKey);
61 final int thatStatsIndex = SparseMappingTable.getIndexFromKey(thatKey);
62
63 mergeStats(stats, statsIndex, thatStats, thatStatsIndex);
Joe Onorato4eb64fd2016-03-21 15:30:09 -070064 }
65 }
66
67 /**
68 * Merge the supplied PSS data in. The new min pss will be the minimum of the existing
69 * one and the new one, the average will now incorporate the new average, etc.
70 */
71 public void mergeStats(int state, int inCount, long minPss, long avgPss, long maxPss,
Dianne Hackborne17b4452018-01-10 13:15:40 -080072 long minUss, long avgUss, long maxUss, long minRss, long avgRss, long maxRss) {
Joe Onorato4eb64fd2016-03-21 15:30:09 -070073 final int key = getOrAddKey((byte)state, PSS_COUNT);
Dianne Hackborn99aeba82018-07-31 13:29:33 -070074 final long[] stats = getArrayForKey(key);
75 final int statsIndex = SparseMappingTable.getIndexFromKey(key);
76 mergeStats(stats, statsIndex, inCount, minPss, avgPss, maxPss, minUss, avgUss, maxUss,
77 minRss, avgRss, maxRss);
78 }
79
80 public static void mergeStats(final long[] stats, final int statsIndex,
81 final long[] thatStats, int thatStatsIndex) {
82 mergeStats(stats, statsIndex, (int)thatStats[thatStatsIndex + PSS_SAMPLE_COUNT],
83 thatStats[thatStatsIndex + PSS_MINIMUM],
84 thatStats[thatStatsIndex + PSS_AVERAGE],
85 thatStats[thatStatsIndex + PSS_MAXIMUM],
86 thatStats[thatStatsIndex + PSS_USS_MINIMUM],
87 thatStats[thatStatsIndex + PSS_USS_AVERAGE],
88 thatStats[thatStatsIndex + PSS_USS_MAXIMUM],
89 thatStats[thatStatsIndex + PSS_RSS_MINIMUM],
90 thatStats[thatStatsIndex + PSS_RSS_AVERAGE],
91 thatStats[thatStatsIndex + PSS_RSS_MAXIMUM]);
92 }
93
94 public static void mergeStats(final long[] stats, final int statsIndex, final int inCount,
95 final long minPss, final long avgPss, final long maxPss,
96 final long minUss, final long avgUss, final long maxUss,
97 final long minRss, final long avgRss, final long maxRss) {
98 final long count = stats[statsIndex + PSS_SAMPLE_COUNT];
Joe Onorato4eb64fd2016-03-21 15:30:09 -070099 if (count == 0) {
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700100 stats[statsIndex + PSS_SAMPLE_COUNT] = inCount;
101 stats[statsIndex + PSS_MINIMUM] = minPss;
102 stats[statsIndex + PSS_AVERAGE] = avgPss;
103 stats[statsIndex + PSS_MAXIMUM] = maxPss;
104 stats[statsIndex + PSS_USS_MINIMUM] = minUss;
105 stats[statsIndex + PSS_USS_AVERAGE] = avgUss;
106 stats[statsIndex + PSS_USS_MAXIMUM] = maxUss;
107 stats[statsIndex + PSS_RSS_MINIMUM] = minRss;
108 stats[statsIndex + PSS_RSS_AVERAGE] = avgRss;
109 stats[statsIndex + PSS_RSS_MAXIMUM] = maxRss;
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700110 } else {
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700111 stats[statsIndex + PSS_SAMPLE_COUNT] = count + inCount;
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700112
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700113 if (stats[statsIndex + PSS_MINIMUM] > minPss) {
114 stats[statsIndex + PSS_MINIMUM] = minPss;
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700115 }
116
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700117 stats[statsIndex + PSS_AVERAGE] = (long)(((stats[statsIndex + PSS_AVERAGE]
118 * (double)count) + (avgPss * (double)inCount)) / (count + inCount));
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700119
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700120 if (stats[statsIndex + PSS_MAXIMUM] < maxPss) {
121 stats[statsIndex + PSS_MAXIMUM] = maxPss;
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700122 }
123
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700124 if (stats[statsIndex + PSS_USS_MINIMUM] > minUss) {
125 stats[statsIndex + PSS_USS_MINIMUM] = minUss;
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700126 }
127
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700128 stats[statsIndex + PSS_USS_AVERAGE] = (long)(((stats[statsIndex + PSS_USS_AVERAGE]
129 * (double)count) + (avgUss * (double)inCount)) / (count + inCount));
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700130
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700131 if (stats[statsIndex + PSS_USS_MAXIMUM] < maxUss) {
132 stats[statsIndex + PSS_USS_MAXIMUM] = maxUss;
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700133 }
Dianne Hackborne17b4452018-01-10 13:15:40 -0800134
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700135 if (stats[statsIndex + PSS_RSS_MINIMUM] > minRss) {
136 stats[statsIndex + PSS_RSS_MINIMUM] = minRss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800137 }
138
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700139 stats[statsIndex + PSS_RSS_AVERAGE] = (long)(((stats[statsIndex + PSS_RSS_AVERAGE]
140 * (double)count) + (avgRss * (double)inCount)) / (count + inCount));
Dianne Hackborne17b4452018-01-10 13:15:40 -0800141
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700142 if (stats[statsIndex + PSS_RSS_MAXIMUM] < maxRss) {
143 stats[statsIndex + PSS_RSS_MAXIMUM] = maxRss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800144 }
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700145 }
146 }
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700147
148 public void writeStatsToProtoForKey(ProtoOutputStream proto, int key) {
149 final long[] stats = getArrayForKey(key);
150 final int statsIndex = SparseMappingTable.getIndexFromKey(key);
151 writeStatsToProto(proto, stats, statsIndex);
152 }
153
154 public static void writeStatsToProto(ProtoOutputStream proto, final long[] stats,
155 final int statsIndex) {
Dianne Hackborneaed0ba2018-08-08 17:10:17 -0700156 proto.write(ProcessStatsStateProto.SAMPLE_SIZE, stats[statsIndex + PSS_SAMPLE_COUNT]);
157 ProtoUtils.toAggStatsProto(proto, ProcessStatsStateProto.PSS,
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700158 stats[statsIndex + PSS_MINIMUM],
159 stats[statsIndex + PSS_AVERAGE],
160 stats[statsIndex + PSS_MAXIMUM]);
Dianne Hackborneaed0ba2018-08-08 17:10:17 -0700161 ProtoUtils.toAggStatsProto(proto, ProcessStatsStateProto.USS,
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700162 stats[statsIndex + PSS_USS_MINIMUM],
163 stats[statsIndex + PSS_USS_AVERAGE],
164 stats[statsIndex + PSS_USS_MAXIMUM]);
Dianne Hackborneaed0ba2018-08-08 17:10:17 -0700165 ProtoUtils.toAggStatsProto(proto, ProcessStatsStateProto.RSS,
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700166 stats[statsIndex + PSS_RSS_MINIMUM],
167 stats[statsIndex + PSS_RSS_AVERAGE],
168 stats[statsIndex + PSS_RSS_MAXIMUM]);
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700169 }
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700170}