The Android Open Source Project | 54b6cfa | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | import java.util.Comparator; |
| 18 | |
| 19 | /** |
| 20 | * Ranks classes for preloading based on how long their operations took |
| 21 | * and how early the operations happened. Higher ranked classes come first. |
| 22 | */ |
| 23 | class ClassRank implements Comparator<Operation> { |
| 24 | |
| 25 | /** |
| 26 | * Increase this number to add more weight to classes which were loaded |
| 27 | * earlier. |
| 28 | */ |
| 29 | static final int SEQUENCE_WEIGHT = 500; // 5 ms |
| 30 | |
| 31 | static final int BUCKET_SIZE = 5; |
| 32 | |
| 33 | public int compare(Operation a, Operation b) { |
| 34 | // Higher ranked operations should come first. |
| 35 | int result = rankOf(b) - rankOf(a); |
| 36 | if (result != 0) { |
| 37 | return result; |
| 38 | } |
| 39 | |
| 40 | // Make sure we don't drop one of two classes w/ the same rank. |
| 41 | // If a load and an initialization have the same rank, it's OK |
| 42 | // to treat the operations equally. |
| 43 | return a.loadedClass.name.compareTo(b.loadedClass.name); |
| 44 | } |
| 45 | |
| 46 | /** Ranks the given operation. */ |
| 47 | private static int rankOf(Operation o) { |
| 48 | return o.medianExclusiveTimeMicros() |
| 49 | + SEQUENCE_WEIGHT / (o.index / BUCKET_SIZE + 1); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |