blob: a6bae6e05dbc9607d4ca8176e9afa36f9640ad86 [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
Richard Gaywood57dc86a2020-03-12 17:57:10 +000019import static com.android.internal.app.procstats.ProcessStats.PSS_AVERAGE;
20import static com.android.internal.app.procstats.ProcessStats.PSS_COUNT;
21import static com.android.internal.app.procstats.ProcessStats.PSS_MAXIMUM;
22import static com.android.internal.app.procstats.ProcessStats.PSS_MINIMUM;
Dianne Hackborne17b4452018-01-10 13:15:40 -080023import static com.android.internal.app.procstats.ProcessStats.PSS_RSS_AVERAGE;
24import static com.android.internal.app.procstats.ProcessStats.PSS_RSS_MAXIMUM;
25import static com.android.internal.app.procstats.ProcessStats.PSS_RSS_MINIMUM;
Joe Onorato4eb64fd2016-03-21 15:30:09 -070026import static com.android.internal.app.procstats.ProcessStats.PSS_SAMPLE_COUNT;
Joe Onorato4eb64fd2016-03-21 15:30:09 -070027import static com.android.internal.app.procstats.ProcessStats.PSS_USS_AVERAGE;
28import static com.android.internal.app.procstats.ProcessStats.PSS_USS_MAXIMUM;
Richard Gaywood57dc86a2020-03-12 17:57:10 +000029import static com.android.internal.app.procstats.ProcessStats.PSS_USS_MINIMUM;
Joe Onorato4eb64fd2016-03-21 15:30:09 -070030
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) {
Dianne Hackborne17b4452018-01-10 13:15:40 -0800136 }
137
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700138 stats[statsIndex + PSS_RSS_AVERAGE] = (long)(((stats[statsIndex + PSS_RSS_AVERAGE]
139 * (double)count) + (avgRss * (double)inCount)) / (count + inCount));
Dianne Hackborne17b4452018-01-10 13:15:40 -0800140
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700141 if (stats[statsIndex + PSS_RSS_MAXIMUM] < maxRss) {
142 stats[statsIndex + PSS_RSS_MAXIMUM] = maxRss;
Dianne Hackborne17b4452018-01-10 13:15:40 -0800143 }
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700144 }
145 }
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700146
147 public void writeStatsToProtoForKey(ProtoOutputStream proto, int key) {
148 final long[] stats = getArrayForKey(key);
149 final int statsIndex = SparseMappingTable.getIndexFromKey(key);
150 writeStatsToProto(proto, stats, statsIndex);
151 }
152
153 public static void writeStatsToProto(ProtoOutputStream proto, final long[] stats,
154 final int statsIndex) {
Dianne Hackborneaed0ba2018-08-08 17:10:17 -0700155 proto.write(ProcessStatsStateProto.SAMPLE_SIZE, stats[statsIndex + PSS_SAMPLE_COUNT]);
156 ProtoUtils.toAggStatsProto(proto, ProcessStatsStateProto.PSS,
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700157 stats[statsIndex + PSS_MINIMUM],
158 stats[statsIndex + PSS_AVERAGE],
159 stats[statsIndex + PSS_MAXIMUM]);
Dianne Hackborneaed0ba2018-08-08 17:10:17 -0700160 ProtoUtils.toAggStatsProto(proto, ProcessStatsStateProto.USS,
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700161 stats[statsIndex + PSS_USS_MINIMUM],
162 stats[statsIndex + PSS_USS_AVERAGE],
163 stats[statsIndex + PSS_USS_MAXIMUM]);
Dianne Hackborneaed0ba2018-08-08 17:10:17 -0700164 ProtoUtils.toAggStatsProto(proto, ProcessStatsStateProto.RSS,
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700165 stats[statsIndex + PSS_RSS_MINIMUM],
166 stats[statsIndex + PSS_RSS_AVERAGE],
167 stats[statsIndex + PSS_RSS_MAXIMUM]);
Dianne Hackborn99aeba82018-07-31 13:29:33 -0700168 }
Richard Gaywood57dc86a2020-03-12 17:57:10 +0000169
170 long[] getRssMeanAndMax(int key) {
171 final long[] stats = getArrayForKey(key);
172 final int statsIndex = SparseMappingTable.getIndexFromKey(key);
173 return new long[]{stats[statsIndex + PSS_RSS_AVERAGE], stats[statsIndex + PSS_RSS_MAXIMUM]};
174 }
Joe Onorato4eb64fd2016-03-21 15:30:09 -0700175}