Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | package com.android.ahat; |
| 18 | |
| 19 | import com.android.tools.perflib.heap.ClassObj; |
| 20 | import com.android.tools.perflib.heap.Instance; |
| 21 | import java.net.URI; |
| 22 | |
| 23 | /** |
| 24 | * Class to render an hprof value to a DocString. |
| 25 | */ |
| 26 | class Value { |
| 27 | |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 28 | // For string literals, we limit the number of characters we show to |
| 29 | // kMaxChars in case the string is really long. |
| 30 | private static int kMaxChars = 200; |
| 31 | |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Create a DocString representing a summary of the given instance. |
| 34 | */ |
| 35 | private static DocString renderInstance(Instance inst) { |
| 36 | DocString link = new DocString(); |
| 37 | if (inst == null) { |
| 38 | link.append("(null)"); |
| 39 | return link; |
| 40 | } |
| 41 | |
| 42 | // Annotate classes as classes. |
| 43 | if (inst instanceof ClassObj) { |
| 44 | link.append("class "); |
| 45 | } |
| 46 | |
| 47 | link.append(inst.toString()); |
Richard Uhler | b357730 | 2015-10-29 13:02:42 -0700 | [diff] [blame] | 48 | URI objTarget = DocString.formattedUri("object?id=%d", inst.getId()); |
| 49 | DocString formatted = DocString.link(objTarget, link); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 50 | |
| 51 | // Annotate Strings with their values. |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 52 | String stringValue = InstanceUtils.asString(inst, kMaxChars); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 53 | if (stringValue != null) { |
Richard Uhler | b357730 | 2015-10-29 13:02:42 -0700 | [diff] [blame] | 54 | formatted.appendFormat(" \"%s", stringValue); |
| 55 | formatted.append(kMaxChars == stringValue.length() ? "..." : "\""); |
| 56 | } |
| 57 | |
| 58 | // Annotate Reference with its referent |
| 59 | Instance referent = InstanceUtils.getReferent(inst); |
| 60 | if (referent != null) { |
| 61 | formatted.append(" for "); |
| 62 | |
| 63 | // It should not be possible for a referent to refer back to the |
| 64 | // reference object, even indirectly, so there shouldn't be any issues |
| 65 | // with infinite recursion here. |
| 66 | formatted.append(renderInstance(referent)); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Annotate DexCache with its location. |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 70 | String dexCacheLocation = InstanceUtils.getDexCacheLocation(inst, kMaxChars); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 71 | if (dexCacheLocation != null) { |
Richard Uhler | b357730 | 2015-10-29 13:02:42 -0700 | [diff] [blame] | 72 | formatted.appendFormat(" for %s", dexCacheLocation); |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 73 | if (kMaxChars == dexCacheLocation.length()) { |
Richard Uhler | b357730 | 2015-10-29 13:02:42 -0700 | [diff] [blame] | 74 | formatted.append("..."); |
Richard Uhler | 77ff54b | 2015-08-31 10:22:56 -0700 | [diff] [blame] | 75 | } |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 78 | |
| 79 | // Annotate bitmaps with a thumbnail. |
| 80 | Instance bitmap = InstanceUtils.getAssociatedBitmapInstance(inst); |
| 81 | String thumbnail = ""; |
| 82 | if (bitmap != null) { |
Richard Uhler | c21e4e6 | 2015-08-31 16:16:14 -0700 | [diff] [blame] | 83 | URI uri = DocString.formattedUri("bitmap?id=%d", bitmap.getId()); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 84 | formatted.appendThumbnail(uri, "bitmap image"); |
| 85 | } |
| 86 | return formatted; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Create a DocString summarizing the given value. |
| 91 | */ |
| 92 | public static DocString render(Object val) { |
| 93 | if (val instanceof Instance) { |
| 94 | return renderInstance((Instance)val); |
| 95 | } else { |
Richard Uhler | c21e4e6 | 2015-08-31 16:16:14 -0700 | [diff] [blame] | 96 | return DocString.format("%s", val); |
Richard Uhler | b730b78 | 2015-07-15 16:01:58 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | } |