summaryrefslogtreecommitdiffstats
path: root/tools/preload/LoadedClass.java
blob: 9ef17f5643f5ee463e3a069c5887087ee7c56527 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.Serializable;
import java.util.*;

/**
 * A loaded class.
 */
class LoadedClass implements Serializable, Comparable<LoadedClass> {

    private static final long serialVersionUID = 0;

    /** Class name. */
    final String name;

    /** Load operations. */
    final List<Operation> loads = new ArrayList<Operation>();

    /** Static initialization operations. */
    final List<Operation> initializations = new ArrayList<Operation>();

    /** Memory usage gathered by loading only this class in its own VM. */
    MemoryUsage memoryUsage = MemoryUsage.NOT_AVAILABLE;

    /**
     * Whether or not this class was loaded in the system class loader.
     */
    final boolean systemClass;

    /** Whether or not this class will be preloaded. */
    boolean preloaded;

    /** Constructs a new class. */
    LoadedClass(String name, boolean systemClass) {
        this.name = name;
        this.systemClass = systemClass;
    }

    void measureMemoryUsage() {
//        this.memoryUsage = MemoryUsage.forClass(name);
    }

    int mlt = -1;

    /** Median time to load this class. */
    int medianLoadTimeMicros() {
        if (mlt != -1) {
            return mlt;
        }

        return mlt = calculateMedian(loads);
    }

    int mit = -1;

    /** Median time to initialize this class. */
    int medianInitTimeMicros() {
        if (mit != -1) {
            return mit;
        }

        return mit = calculateMedian(initializations);
    }

    /** Calculates the median duration for a list of operations. */
    private static int calculateMedian(List<Operation> operations) {
        int size = operations.size();
        if (size == 0) {
            return 0;
        }

        int[] times = new int[size];
        for (int i = 0; i < size; i++) {
            times[i] = operations.get(i).exclusiveTimeMicros();
        }

        Arrays.sort(times);
        int middle = size / 2;
        if (size % 2 == 1) {
            // Odd
            return times[middle];
        } else {
            // Even -- average the two.
            return (times[middle - 1] + times[middle]) / 2;
        }
    }

    /** Returns names of apps that loaded this class. */
    Set<String> applicationNames() {
        Set<String> appNames = new HashSet<String>();
        addProcessNames(loads, appNames);
        addProcessNames(initializations, appNames);
        return appNames;
    }

    private void addProcessNames(List<Operation> ops, Set<String> appNames) {
        for (Operation operation : ops) {
            if (operation.process.isApplication()) {
                appNames.add(operation.process.name);
            }
        }
    }

    public int compareTo(LoadedClass o) {
        return name.compareTo(o.name);
    }

    @Override
    public String toString() {
        return name;
    }

    /**
     * Returns true if this class's initialization causes the given class to
     * initialize.
     */
    public boolean initializes(LoadedClass clazz, Set<LoadedClass> visited) {
        // Avoid infinite recursion.
        if (!visited.add(this)) {
            return false;
        }

        if (clazz == this) {
            return true;
        }

        for (Operation initialization : initializations) {
            if (initialization.loadedClass.initializes(clazz, visited)) {
                return true;
            }
        }

        return false;
    }

    public boolean isPreloadable() {
        return systemClass && Policy.isPreloadableClass(name);
    }
}