blob: 9ef17f5643f5ee463e3a069c5887087ee7c56527 [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;
Bob Lee2e93f652009-08-11 01:16:03 -070018import java.util.*;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019
20/**
21 * A loaded class.
22 */
23class LoadedClass implements Serializable, Comparable<LoadedClass> {
24
25 private static final long serialVersionUID = 0;
26
27 /** Class name. */
28 final String name;
29
30 /** Load operations. */
31 final List<Operation> loads = new ArrayList<Operation>();
32
33 /** Static initialization operations. */
34 final List<Operation> initializations = new ArrayList<Operation>();
35
36 /** Memory usage gathered by loading only this class in its own VM. */
37 MemoryUsage memoryUsage = MemoryUsage.NOT_AVAILABLE;
38
39 /**
40 * Whether or not this class was loaded in the system class loader.
41 */
42 final boolean systemClass;
43
44 /** Whether or not this class will be preloaded. */
45 boolean preloaded;
46
47 /** Constructs a new class. */
48 LoadedClass(String name, boolean systemClass) {
49 this.name = name;
50 this.systemClass = systemClass;
51 }
52
53 void measureMemoryUsage() {
Bob Lee2e93f652009-08-11 01:16:03 -070054// this.memoryUsage = MemoryUsage.forClass(name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 }
56
57 int mlt = -1;
58
59 /** Median time to load this class. */
60 int medianLoadTimeMicros() {
61 if (mlt != -1) {
62 return mlt;
63 }
64
65 return mlt = calculateMedian(loads);
66 }
67
68 int mit = -1;
69
70 /** Median time to initialize this class. */
71 int medianInitTimeMicros() {
72 if (mit != -1) {
73 return mit;
74 }
75
76 return mit = calculateMedian(initializations);
77 }
78
79 /** Calculates the median duration for a list of operations. */
80 private static int calculateMedian(List<Operation> operations) {
81 int size = operations.size();
82 if (size == 0) {
83 return 0;
84 }
85
86 int[] times = new int[size];
87 for (int i = 0; i < size; i++) {
88 times[i] = operations.get(i).exclusiveTimeMicros();
89 }
90
91 Arrays.sort(times);
92 int middle = size / 2;
93 if (size % 2 == 1) {
94 // Odd
95 return times[middle];
96 } else {
97 // Even -- average the two.
98 return (times[middle - 1] + times[middle]) / 2;
99 }
100 }
101
Bob Lee2e93f652009-08-11 01:16:03 -0700102 /** Returns names of apps that loaded this class. */
103 Set<String> applicationNames() {
104 Set<String> appNames = new HashSet<String>();
105 addProcessNames(loads, appNames);
106 addProcessNames(initializations, appNames);
107 return appNames;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 }
109
Bob Lee2e93f652009-08-11 01:16:03 -0700110 private void addProcessNames(List<Operation> ops, Set<String> appNames) {
111 for (Operation operation : ops) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 if (operation.process.isApplication()) {
Bob Lee2e93f652009-08-11 01:16:03 -0700113 appNames.add(operation.process.name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 }
115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 }
117
118 public int compareTo(LoadedClass o) {
119 return name.compareTo(o.name);
120 }
121
122 @Override
123 public String toString() {
124 return name;
125 }
126
127 /**
128 * Returns true if this class's initialization causes the given class to
129 * initialize.
130 */
131 public boolean initializes(LoadedClass clazz, Set<LoadedClass> visited) {
132 // Avoid infinite recursion.
133 if (!visited.add(this)) {
134 return false;
135 }
136
137 if (clazz == this) {
138 return true;
139 }
140
141 for (Operation initialization : initializations) {
142 if (initialization.loadedClass.initializes(clazz, visited)) {
143 return true;
144 }
145 }
146
147 return false;
148 }
Bob Lee2e93f652009-08-11 01:16:03 -0700149
150 public boolean isPreloadable() {
151 return systemClass && Policy.isPreloadableClass(name);
152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153}