blob: 57828071bab8df7614a13f27785592fe938dddcb [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 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
17import java.io.Serializable;
18import java.util.ArrayList;
19import java.util.Arrays;
20import java.util.List;
21import java.util.Set;
22
23/**
24 * A loaded class.
25 */
26class LoadedClass implements Serializable, Comparable<LoadedClass> {
27
28 private static final long serialVersionUID = 0;
29
30 /** Class name. */
31 final String name;
32
33 /** Load operations. */
34 final List<Operation> loads = new ArrayList<Operation>();
35
36 /** Static initialization operations. */
37 final List<Operation> initializations = new ArrayList<Operation>();
38
39 /** Memory usage gathered by loading only this class in its own VM. */
40 MemoryUsage memoryUsage = MemoryUsage.NOT_AVAILABLE;
41
42 /**
43 * Whether or not this class was loaded in the system class loader.
44 */
45 final boolean systemClass;
46
47 /** Whether or not this class will be preloaded. */
48 boolean preloaded;
49
50 /** Constructs a new class. */
51 LoadedClass(String name, boolean systemClass) {
52 this.name = name;
53 this.systemClass = systemClass;
54 }
55
56 void measureMemoryUsage() {
57 this.memoryUsage = MemoryUsage.forClass(name);
58 }
59
60 int mlt = -1;
61
62 /** Median time to load this class. */
63 int medianLoadTimeMicros() {
64 if (mlt != -1) {
65 return mlt;
66 }
67
68 return mlt = calculateMedian(loads);
69 }
70
71 int mit = -1;
72
73 /** Median time to initialize this class. */
74 int medianInitTimeMicros() {
75 if (mit != -1) {
76 return mit;
77 }
78
79 return mit = calculateMedian(initializations);
80 }
81
82 /** Calculates the median duration for a list of operations. */
83 private static int calculateMedian(List<Operation> operations) {
84 int size = operations.size();
85 if (size == 0) {
86 return 0;
87 }
88
89 int[] times = new int[size];
90 for (int i = 0; i < size; i++) {
91 times[i] = operations.get(i).exclusiveTimeMicros();
92 }
93
94 Arrays.sort(times);
95 int middle = size / 2;
96 if (size % 2 == 1) {
97 // Odd
98 return times[middle];
99 } else {
100 // Even -- average the two.
101 return (times[middle - 1] + times[middle]) / 2;
102 }
103 }
104
105 /**
106 * Counts loads by apps.
107 */
108 int appLoads() {
109 return operationsByApps(loads);
110 }
111
112 /**
113 * Counts inits by apps.
114 */
115 int appInits() {
116 return operationsByApps(initializations);
117 }
118
119 /**
120 * Counts number of app operations in the given list.
121 */
122 private static int operationsByApps(List<Operation> operations) {
123 int byApps = 0;
124 for (Operation operation : operations) {
125 if (operation.process.isApplication()) {
126 byApps++;
127 }
128 }
129 return byApps;
130 }
131
132 public int compareTo(LoadedClass o) {
133 return name.compareTo(o.name);
134 }
135
136 @Override
137 public String toString() {
138 return name;
139 }
140
141 /**
142 * Returns true if this class's initialization causes the given class to
143 * initialize.
144 */
145 public boolean initializes(LoadedClass clazz, Set<LoadedClass> visited) {
146 // Avoid infinite recursion.
147 if (!visited.add(this)) {
148 return false;
149 }
150
151 if (clazz == this) {
152 return true;
153 }
154
155 for (Operation initialization : initializations) {
156 if (initialization.loadedClass.initializes(clazz, visited)) {
157 return true;
158 }
159 }
160
161 return false;
162 }
163}