2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef DALVIK_HPROF_HPROF_H_
17 #define DALVIK_HPROF_HPROF_H_
21 #define HPROF_ID_SIZE (sizeof (u4))
23 #define UNIQUE_ERROR() \
24 -((((uintptr_t)__func__) << 16 | __LINE__) & (0x7fffffff))
27 #define HPROF_NULL_STACK_TRACE 0
28 #define HPROF_NULL_THREAD 0
31 typedef hprof_id hprof_string_id;
32 typedef hprof_id hprof_object_id;
33 typedef hprof_id hprof_class_object_id;
35 enum hprof_basic_type {
36 hprof_basic_object = 2,
37 hprof_basic_boolean = 4,
39 hprof_basic_float = 6,
40 hprof_basic_double = 7,
42 hprof_basic_short = 9,
44 hprof_basic_long = 11,
48 HPROF_TAG_STRING = 0x01,
49 HPROF_TAG_LOAD_CLASS = 0x02,
50 HPROF_TAG_UNLOAD_CLASS = 0x03,
51 HPROF_TAG_STACK_FRAME = 0x04,
52 HPROF_TAG_STACK_TRACE = 0x05,
53 HPROF_TAG_ALLOC_SITES = 0x06,
54 HPROF_TAG_HEAP_SUMMARY = 0x07,
55 HPROF_TAG_START_THREAD = 0x0A,
56 HPROF_TAG_END_THREAD = 0x0B,
57 HPROF_TAG_HEAP_DUMP = 0x0C,
58 HPROF_TAG_HEAP_DUMP_SEGMENT = 0x1C,
59 HPROF_TAG_HEAP_DUMP_END = 0x2C,
60 HPROF_TAG_CPU_SAMPLES = 0x0D,
61 HPROF_TAG_CONTROL_SETTINGS = 0x0E,
64 /* Values for the first byte of
65 * HEAP_DUMP and HEAP_DUMP_SEGMENT
68 enum hprof_heap_tag_t {
70 HPROF_ROOT_UNKNOWN = 0xFF,
71 HPROF_ROOT_JNI_GLOBAL = 0x01,
72 HPROF_ROOT_JNI_LOCAL = 0x02,
73 HPROF_ROOT_JAVA_FRAME = 0x03,
74 HPROF_ROOT_NATIVE_STACK = 0x04,
75 HPROF_ROOT_STICKY_CLASS = 0x05,
76 HPROF_ROOT_THREAD_BLOCK = 0x06,
77 HPROF_ROOT_MONITOR_USED = 0x07,
78 HPROF_ROOT_THREAD_OBJECT = 0x08,
79 HPROF_CLASS_DUMP = 0x20,
80 HPROF_INSTANCE_DUMP = 0x21,
81 HPROF_OBJECT_ARRAY_DUMP = 0x22,
82 HPROF_PRIMITIVE_ARRAY_DUMP = 0x23,
85 HPROF_HEAP_DUMP_INFO = 0xfe,
86 HPROF_ROOT_INTERNED_STRING = 0x89,
87 HPROF_ROOT_FINALIZING = 0x8a, /* obsolete */
88 HPROF_ROOT_DEBUGGER = 0x8b,
89 HPROF_ROOT_REFERENCE_CLEANUP = 0x8c, /* obsolete */
90 HPROF_ROOT_VM_INTERNAL = 0x8d,
91 HPROF_ROOT_JNI_MONITOR = 0x8e,
92 HPROF_UNREACHABLE = 0x90, /* obsolete */
93 HPROF_PRIMITIVE_ARRAY_NODATA_DUMP = 0xc3,
96 /* Represents a top-level hprof record, whose serialized
99 * u1 TAG: denoting the type of the record
100 * u4 TIME: number of microseconds since the time stamp in the header
101 * u4 LENGTH: number of bytes that follow this u4 field
102 * and belong to this record
103 * [u1]* BODY: as many bytes as specified in the above u4 field
105 struct hprof_record_t {
115 HPROF_HEAP_DEFAULT = 0,
116 HPROF_HEAP_ZYGOTE = 'Z',
120 struct hprof_context_t {
121 /* curRec *must* be first so that we
122 * can cast from a context to a record.
124 hprof_record_t curRec;
126 u4 gcThreadSerialNumber;
128 HprofHeapId currentHeap; // which heap we're currently emitting
129 u4 stackTraceSerialNumber;
130 size_t objectsInSegment;
133 * If directToDdms is set, "fileName" and "fd" will be ignored.
134 * Otherwise, "fileName" must be valid, though if "fd" >= 0 it will
135 * only be used for debug messages.
139 char *fileDataPtr; // for open_memstream
140 size_t fileDataSize; // for open_memstream
147 * HprofString.cpp functions
150 hprof_string_id hprofLookupStringId(const char *str);
152 int hprofDumpStrings(hprof_context_t *ctx);
154 int hprofStartup_String(void);
155 int hprofShutdown_String(void);
159 * HprofClass.cpp functions
162 hprof_class_object_id hprofLookupClassId(const ClassObject *clazz);
164 int hprofDumpClasses(hprof_context_t *ctx);
166 int hprofStartup_Class(void);
167 int hprofShutdown_Class(void);
171 * HprofHeap.cpp functions
174 int hprofStartHeapDump(hprof_context_t *ctx);
175 int hprofFinishHeapDump(hprof_context_t *ctx);
177 int hprofSetGcScanState(hprof_context_t *ctx,
178 hprof_heap_tag_t state, u4 threadSerialNumber);
179 int hprofMarkRootObject(hprof_context_t *ctx,
180 const Object *obj, jobject jniObj);
182 int hprofDumpHeapObject(hprof_context_t *ctx, const Object *obj);
185 * HprofOutput.cpp functions
188 void hprofContextInit(hprof_context_t *ctx, char *fileName, int fd,
189 bool writeHeader, bool directToDdms);
191 int hprofFlushRecord(hprof_record_t *rec, FILE *fp);
192 int hprofFlushCurrentRecord(hprof_context_t *ctx);
193 int hprofStartNewRecord(hprof_context_t *ctx, u1 tag, u4 time);
195 int hprofAddU1ToRecord(hprof_record_t *rec, u1 value);
196 int hprofAddU1ListToRecord(hprof_record_t *rec,
197 const u1 *values, size_t numValues);
199 int hprofAddUtf8StringToRecord(hprof_record_t *rec, const char *str);
201 int hprofAddU2ToRecord(hprof_record_t *rec, u2 value);
202 int hprofAddU2ListToRecord(hprof_record_t *rec,
203 const u2 *values, size_t numValues);
205 int hprofAddU4ToRecord(hprof_record_t *rec, u4 value);
206 int hprofAddU4ListToRecord(hprof_record_t *rec,
207 const u4 *values, size_t numValues);
209 int hprofAddU8ToRecord(hprof_record_t *rec, u8 value);
210 int hprofAddU8ListToRecord(hprof_record_t *rec,
211 const u8 *values, size_t numValues);
213 #define hprofAddIdToRecord(rec, id) hprofAddU4ToRecord((rec), (u4)(id))
214 #define hprofAddIdListToRecord(rec, values, numValues) \
215 hprofAddU4ListToRecord((rec), (const u4 *)(values), (numValues))
218 * Hprof.cpp functions
221 hprof_context_t* hprofStartup(const char *outputFileName, int fd,
223 bool hprofShutdown(hprof_context_t *ctx);
224 void hprofFreeContext(hprof_context_t *ctx);
225 int hprofDumpHeap(const char* fileName, int fd, bool directToDdms);
227 #endif // DALVIK_HPROF_HPROF_H_