2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 import java.io.Serializable;
23 class LoadedClass implements Serializable, Comparable<LoadedClass> {
25 private static final long serialVersionUID = 0;
30 /** Load operations. */
31 final List<Operation> loads = new ArrayList<Operation>();
33 /** Static initialization operations. */
34 final List<Operation> initializations = new ArrayList<Operation>();
36 /** Memory usage gathered by loading only this class in its own VM. */
37 MemoryUsage memoryUsage = MemoryUsage.NOT_AVAILABLE;
40 * Whether or not this class was loaded in the system class loader.
42 final boolean systemClass;
44 /** Whether or not this class will be preloaded. */
47 /** Constructs a new class. */
48 LoadedClass(String name, boolean systemClass) {
50 this.systemClass = systemClass;
53 void measureMemoryUsage() {
54 this.memoryUsage = MemoryUsage.forClass(name);
59 /** Median time to load this class. */
60 int medianLoadTimeMicros() {
65 return mlt = calculateMedian(loads);
70 /** Median time to initialize this class. */
71 int medianInitTimeMicros() {
76 return mit = calculateMedian(initializations);
79 int medianTimeMicros() {
80 return medianInitTimeMicros() + medianLoadTimeMicros();
83 /** Calculates the median duration for a list of operations. */
84 private static int calculateMedian(List<Operation> operations) {
85 int size = operations.size();
90 int[] times = new int[size];
91 for (int i = 0; i < size; i++) {
92 times[i] = operations.get(i).exclusiveTimeMicros();
96 int middle = size / 2;
101 // Even -- average the two.
102 return (times[middle - 1] + times[middle]) / 2;
106 /** Returns names of processes that loaded this class. */
107 Set<String> processNames() {
108 Set<String> names = new HashSet<String>();
109 addProcessNames(loads, names);
110 addProcessNames(initializations, names);
114 private void addProcessNames(List<Operation> ops, Set<String> names) {
115 for (Operation operation : ops) {
116 if (operation.process.fromZygote()) {
117 names.add(operation.process.name);
122 public int compareTo(LoadedClass o) {
123 return name.compareTo(o.name);
127 public String toString() {