Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [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 | #ifndef HPROF_HPROF_H_ |
| 17 | #define HPROF_HPROF_H_ |
| 18 | |
| 19 | #include <stdio.h> |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 20 | |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 21 | #include <set> |
| 22 | |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 23 | #include "file.h" |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 24 | #include "globals.h" |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 25 | #include "object.h" |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 26 | #include "safe_map.h" |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 27 | #include "thread_list.h" |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | namespace hprof { |
| 32 | |
| 33 | #define HPROF_ID_SIZE (sizeof (uint32_t)) |
| 34 | |
| 35 | #define UNIQUE_ERROR() \ |
| 36 | -((((uintptr_t)__func__) << 16 | __LINE__) & (0x7fffffff)) |
| 37 | |
| 38 | #define HPROF_TIME 0 |
| 39 | #define HPROF_NULL_STACK_TRACE 0 |
| 40 | #define HPROF_NULL_THREAD 0 |
| 41 | |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 42 | #define U2_TO_BUF_BE(buf, offset, value) \ |
| 43 | do { \ |
Elliott Hughes | c1f143d | 2011-12-01 17:31:10 -0800 | [diff] [blame] | 44 | unsigned char* buf_ = (unsigned char*)(buf); \ |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 45 | int offset_ = (int)(offset); \ |
| 46 | uint16_t value_ = (uint16_t)(value); \ |
| 47 | buf_[offset_ + 0] = (unsigned char)(value_ >> 8); \ |
| 48 | buf_[offset_ + 1] = (unsigned char)(value_ ); \ |
| 49 | } while (0) |
| 50 | |
| 51 | #define U4_TO_BUF_BE(buf, offset, value) \ |
| 52 | do { \ |
Elliott Hughes | c1f143d | 2011-12-01 17:31:10 -0800 | [diff] [blame] | 53 | unsigned char* buf_ = (unsigned char*)(buf); \ |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 54 | int offset_ = (int)(offset); \ |
| 55 | uint32_t value_ = (uint32_t)(value); \ |
| 56 | buf_[offset_ + 0] = (unsigned char)(value_ >> 24); \ |
| 57 | buf_[offset_ + 1] = (unsigned char)(value_ >> 16); \ |
| 58 | buf_[offset_ + 2] = (unsigned char)(value_ >> 8); \ |
| 59 | buf_[offset_ + 3] = (unsigned char)(value_ ); \ |
| 60 | } while (0) |
| 61 | |
| 62 | #define U8_TO_BUF_BE(buf, offset, value) \ |
| 63 | do { \ |
Elliott Hughes | c1f143d | 2011-12-01 17:31:10 -0800 | [diff] [blame] | 64 | unsigned char* buf_ = (unsigned char*)(buf); \ |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 65 | int offset_ = (int)(offset); \ |
| 66 | uint64_t value_ = (uint64_t)(value); \ |
| 67 | buf_[offset_ + 0] = (unsigned char)(value_ >> 56); \ |
| 68 | buf_[offset_ + 1] = (unsigned char)(value_ >> 48); \ |
| 69 | buf_[offset_ + 2] = (unsigned char)(value_ >> 40); \ |
| 70 | buf_[offset_ + 3] = (unsigned char)(value_ >> 32); \ |
| 71 | buf_[offset_ + 4] = (unsigned char)(value_ >> 24); \ |
| 72 | buf_[offset_ + 5] = (unsigned char)(value_ >> 16); \ |
| 73 | buf_[offset_ + 6] = (unsigned char)(value_ >> 8); \ |
| 74 | buf_[offset_ + 7] = (unsigned char)(value_ ); \ |
| 75 | } while (0) |
| 76 | |
Jesse Wilson | 3aa66fd | 2011-11-08 20:53:40 -0500 | [diff] [blame] | 77 | typedef uint32_t HprofId; |
| 78 | typedef HprofId HprofStringId; |
| 79 | typedef HprofId HprofObjectId; |
| 80 | typedef HprofId HprofClassObjectId; |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 81 | typedef std::set<Class*> ClassSet; |
| 82 | typedef std::set<Class*>::iterator ClassSetIterator; |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 83 | typedef SafeMap<std::string, size_t> StringMap; |
| 84 | typedef SafeMap<std::string, size_t>::iterator StringMapIterator; |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 85 | |
Jesse Wilson | 3aa66fd | 2011-11-08 20:53:40 -0500 | [diff] [blame] | 86 | enum HprofBasicType { |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 87 | hprof_basic_object = 2, |
| 88 | hprof_basic_boolean = 4, |
| 89 | hprof_basic_char = 5, |
| 90 | hprof_basic_float = 6, |
| 91 | hprof_basic_double = 7, |
| 92 | hprof_basic_byte = 8, |
| 93 | hprof_basic_short = 9, |
| 94 | hprof_basic_int = 10, |
| 95 | hprof_basic_long = 11, |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 96 | }; |
| 97 | |
Jesse Wilson | 3aa66fd | 2011-11-08 20:53:40 -0500 | [diff] [blame] | 98 | enum HprofTag { |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 99 | HPROF_TAG_STRING = 0x01, |
| 100 | HPROF_TAG_LOAD_CLASS = 0x02, |
| 101 | HPROF_TAG_UNLOAD_CLASS = 0x03, |
| 102 | HPROF_TAG_STACK_FRAME = 0x04, |
| 103 | HPROF_TAG_STACK_TRACE = 0x05, |
| 104 | HPROF_TAG_ALLOC_SITES = 0x06, |
| 105 | HPROF_TAG_HEAP_SUMMARY = 0x07, |
| 106 | HPROF_TAG_START_THREAD = 0x0A, |
| 107 | HPROF_TAG_END_THREAD = 0x0B, |
| 108 | HPROF_TAG_HEAP_DUMP = 0x0C, |
| 109 | HPROF_TAG_HEAP_DUMP_SEGMENT = 0x1C, |
| 110 | HPROF_TAG_HEAP_DUMP_END = 0x2C, |
| 111 | HPROF_TAG_CPU_SAMPLES = 0x0D, |
| 112 | HPROF_TAG_CONTROL_SETTINGS = 0x0E, |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 113 | }; |
| 114 | |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 115 | // Values for the first byte of HEAP_DUMP and HEAP_DUMP_SEGMENT records: |
Jesse Wilson | 3aa66fd | 2011-11-08 20:53:40 -0500 | [diff] [blame] | 116 | enum HprofHeapTag { |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 117 | /* standard */ |
| 118 | HPROF_ROOT_UNKNOWN = 0xFF, |
| 119 | HPROF_ROOT_JNI_GLOBAL = 0x01, |
| 120 | HPROF_ROOT_JNI_LOCAL = 0x02, |
| 121 | HPROF_ROOT_JAVA_FRAME = 0x03, |
| 122 | HPROF_ROOT_NATIVE_STACK = 0x04, |
| 123 | HPROF_ROOT_STICKY_CLASS = 0x05, |
| 124 | HPROF_ROOT_THREAD_BLOCK = 0x06, |
| 125 | HPROF_ROOT_MONITOR_USED = 0x07, |
| 126 | HPROF_ROOT_THREAD_OBJECT = 0x08, |
| 127 | HPROF_CLASS_DUMP = 0x20, |
| 128 | HPROF_INSTANCE_DUMP = 0x21, |
| 129 | HPROF_OBJECT_ARRAY_DUMP = 0x22, |
| 130 | HPROF_PRIMITIVE_ARRAY_DUMP = 0x23, |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 131 | |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 132 | /* Android */ |
| 133 | HPROF_HEAP_DUMP_INFO = 0xfe, |
| 134 | HPROF_ROOT_INTERNED_STRING = 0x89, |
| 135 | HPROF_ROOT_FINALIZING = 0x8a, /* obsolete */ |
| 136 | HPROF_ROOT_DEBUGGER = 0x8b, |
| 137 | HPROF_ROOT_REFERENCE_CLEANUP = 0x8c, /* obsolete */ |
| 138 | HPROF_ROOT_VM_INTERNAL = 0x8d, |
| 139 | HPROF_ROOT_JNI_MONITOR = 0x8e, |
| 140 | HPROF_UNREACHABLE = 0x90, /* obsolete */ |
| 141 | HPROF_PRIMITIVE_ARRAY_NODATA_DUMP = 0xc3, |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 142 | }; |
| 143 | |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 144 | // Represents a top-level hprof record, whose serialized format is: |
| 145 | // U1 TAG: denoting the type of the record |
| 146 | // U4 TIME: number of microseconds since the time stamp in the header |
| 147 | // U4 LENGTH: number of bytes that follow this uint32_t field and belong to this record |
| 148 | // U1* BODY: as many bytes as specified in the above uint32_t field |
Jesse Wilson | 3aa66fd | 2011-11-08 20:53:40 -0500 | [diff] [blame] | 149 | class HprofRecord { |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 150 | public: |
| 151 | int Flush(FILE *fp); |
| 152 | int AddU1(uint8_t value); |
| 153 | int AddU2(uint16_t value); |
| 154 | int AddU4(uint32_t value); |
| 155 | int AddU8(uint64_t value); |
| 156 | int AddId(HprofObjectId value); |
| 157 | int AddU1List(const uint8_t *values, size_t numValues); |
| 158 | int AddU2List(const uint16_t *values, size_t numValues); |
| 159 | int AddU4List(const uint32_t *values, size_t numValues); |
| 160 | int AddU8List(const uint64_t *values, size_t numValues); |
| 161 | int AddIdList(const HprofObjectId *values, size_t numValues); |
Elliott Hughes | c1f143d | 2011-12-01 17:31:10 -0800 | [diff] [blame] | 162 | int AddUtf8String(const char* str); |
Jesse Wilson | 3aa66fd | 2011-11-08 20:53:40 -0500 | [diff] [blame] | 163 | |
Elliott Hughes | c1f143d | 2011-12-01 17:31:10 -0800 | [diff] [blame] | 164 | unsigned char* body_; |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 165 | uint32_t time_; |
| 166 | uint32_t length_; |
| 167 | size_t alloc_length_; |
| 168 | uint8_t tag_; |
| 169 | bool dirty_; |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 170 | |
| 171 | private: |
| 172 | int GuaranteeRecordAppend(size_t nmore); |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | enum HprofHeapId { |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 176 | HPROF_HEAP_DEFAULT = 0, |
| 177 | HPROF_HEAP_ZYGOTE = 'Z', |
| 178 | HPROF_HEAP_APP = 'A' |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 179 | }; |
| 180 | |
Jesse Wilson | 3aa66fd | 2011-11-08 20:53:40 -0500 | [diff] [blame] | 181 | class Hprof { |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 182 | public: |
Elliott Hughes | c1f143d | 2011-12-01 17:31:10 -0800 | [diff] [blame] | 183 | Hprof(const char* outputFileName, int fd, bool writeHeader, bool directToDdms); |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 184 | ~Hprof(); |
Jesse Wilson | 3aa66fd | 2011-11-08 20:53:40 -0500 | [diff] [blame] | 185 | |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 186 | void VisitRoot(const Object* obj); |
| 187 | int DumpHeapObject(const Object *obj); |
| 188 | bool Finish(); |
Jesse Wilson | 3aa66fd | 2011-11-08 20:53:40 -0500 | [diff] [blame] | 189 | |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 190 | private: |
| 191 | int DumpClasses(); |
| 192 | int DumpStrings(); |
| 193 | int StartNewRecord(uint8_t tag, uint32_t time); |
| 194 | int FlushCurrentRecord(); |
| 195 | int MarkRootObject(const Object *obj, jobject jniObj); |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 196 | HprofClassObjectId LookupClassId(Class* c); |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 197 | HprofStringId LookupStringId(String* string); |
| 198 | HprofStringId LookupStringId(const char* string); |
Elliott Hughes | ae80b49 | 2012-04-24 10:43:17 -0700 | [diff] [blame] | 199 | HprofStringId LookupStringId(const std::string& string); |
Elliott Hughes | e84278b | 2012-03-22 10:06:53 -0700 | [diff] [blame] | 200 | HprofStringId LookupClassNameId(Class* c); |
Elliott Hughes | c1f143d | 2011-12-01 17:31:10 -0800 | [diff] [blame] | 201 | static HprofBasicType SignatureToBasicTypeAndSize(const char* sig, size_t* sizeOut); |
| 202 | static HprofBasicType PrimitiveToBasicTypeAndSize(Primitive::Type prim, size_t* sizeOut); |
Jesse Wilson | 0c54ac1 | 2011-11-09 15:14:05 -0500 | [diff] [blame] | 203 | static int StackTraceSerialNumber(const void *obj); |
Jesse Wilson | 3aa66fd | 2011-11-08 20:53:40 -0500 | [diff] [blame] | 204 | |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 205 | // current_record_ *must* be first so that we can cast from a context to a record. |
| 206 | HprofRecord current_record_; |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 207 | |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 208 | uint32_t gc_thread_serial_number_; |
| 209 | uint8_t gc_scan_state_; |
| 210 | HprofHeapId current_heap_; // which heap we're currently emitting |
| 211 | size_t objects_in_segment_; |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 212 | |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 213 | // If direct_to_ddms_ is set, "file_name_" and "fd" will be ignored. |
| 214 | // Otherwise, "file_name_" must be valid, though if "fd" >= 0 it will |
| 215 | // only be used for debug messages. |
| 216 | bool direct_to_ddms_; |
Elliott Hughes | 7b3cdfc | 2011-12-08 21:28:17 -0800 | [diff] [blame] | 217 | std::string file_name_; |
Elliott Hughes | c1f143d | 2011-12-01 17:31:10 -0800 | [diff] [blame] | 218 | char* file_data_ptr_; // for open_memstream |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 219 | size_t file_data_size_; // for open_memstream |
| 220 | FILE *mem_fp_; |
| 221 | int fd_; |
Jesse Wilson | 3aa66fd | 2011-11-08 20:53:40 -0500 | [diff] [blame] | 222 | |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 223 | ClassSet classes_; |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 224 | size_t next_string_id_; |
Jesse Wilson | 0b075f1 | 2011-11-09 10:57:41 -0500 | [diff] [blame] | 225 | StringMap strings_; |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 226 | }; |
| 227 | |
Elliott Hughes | fbd8456 | 2011-11-07 18:56:13 -0800 | [diff] [blame] | 228 | int DumpHeap(const char* fileName, int fd, bool directToDdms); |
Jesse Wilson | c4824e6 | 2011-11-01 14:39:04 -0400 | [diff] [blame] | 229 | |
| 230 | } // namespace hprof |
| 231 | |
| 232 | } // namespace art |
| 233 | |
| 234 | #endif // HPROF_HPROF_H_ |