Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 6 | #include <fstream> |
| 7 | #include <iostream> |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "class_linker.h" |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 12 | #include "file.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 13 | #include "image.h" |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 14 | #include "os.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 15 | #include "runtime.h" |
| 16 | #include "space.h" |
| 17 | #include "stringpiece.h" |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 18 | #include "unordered_map.h" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 19 | |
| 20 | namespace art { |
| 21 | |
| 22 | static void usage() { |
| 23 | fprintf(stderr, |
| 24 | "Usage: oatdump [options] ...\n" |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 25 | " Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art --host-prefix=$ANDROID_PRODUCT_OUT\n" |
| 26 | " Example: adb shell oatdump --image=/system/framework/boot.art\n" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 27 | "\n"); |
| 28 | fprintf(stderr, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 29 | " --image=<file.art>: specifies the required input image filename.\n" |
| 30 | " Example: --image=/system/framework/boot.art\n" |
| 31 | "\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 32 | fprintf(stderr, |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 33 | " --boot-image=<file.art>: provide the image file for the boot class path.\n" |
| 34 | " Example: --boot-image=/system/framework/boot.art\n" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 35 | "\n"); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 36 | fprintf(stderr, |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 37 | " --host-prefix may be used to translate host paths to target paths during\n" |
| 38 | " cross compilation.\n" |
| 39 | " Example: --host-prefix=out/target/product/crespo\n" |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 40 | "\n"); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 41 | fprintf(stderr, |
| 42 | " --output=<file> may be used to send the output to a file.\n" |
| 43 | " Example: --output=/tmp/oatdump.txt\n" |
| 44 | "\n"); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 45 | exit(EXIT_FAILURE); |
| 46 | } |
| 47 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 48 | const char* image_roots_descriptions_[] = { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 49 | "kJniStubArray", |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 50 | "kAbstractMethodErrorStubArray", |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 51 | "kInstanceResolutionStubArray", |
| 52 | "kStaticResolutionStubArray", |
Ian Rogers | 1cb0a1d | 2011-10-06 15:24:35 -0700 | [diff] [blame^] | 53 | "kUnknownMethodResolutionStubArray", |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 54 | "kCalleeSaveMethod", |
| 55 | "kOatLocation", |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 56 | "kDexCaches", |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 57 | "kClassRoots", |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 60 | class OatDump { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 61 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 62 | public: |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 63 | static void Dump(const std::string& image_filename, |
| 64 | std::ostream& os, |
| 65 | Space& image_space, |
| 66 | const ImageHeader& image_header) { |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 67 | os << "MAGIC:\n"; |
| 68 | os << image_header.GetMagic() << "\n\n"; |
| 69 | |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 70 | os << "IMAGE BASE:\n"; |
| 71 | os << reinterpret_cast<void*>(image_header.GetImageBaseAddr()) << "\n\n"; |
| 72 | |
| 73 | os << "OAT BASE:\n"; |
| 74 | os << reinterpret_cast<void*>(image_header.GetOatBaseAddr()) << "\n\n"; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 75 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 76 | os << "ROOTS:\n"; |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 77 | CHECK_EQ(arraysize(image_roots_descriptions_), size_t(ImageHeader::kImageRootsMax)); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 78 | for (int i = 0; i < ImageHeader::kImageRootsMax; i++) { |
| 79 | ImageHeader::ImageRoot image_root = static_cast<ImageHeader::ImageRoot>(i); |
Brian Carlstrom | 34f426c | 2011-10-04 12:58:02 -0700 | [diff] [blame] | 80 | const char* image_root_description = image_roots_descriptions_[i]; |
| 81 | Object* image_root_object = image_header.GetImageRoot(image_root); |
| 82 | os << StringPrintf("%s: %p\n", image_root_description, image_root_object); |
| 83 | if (image_root_object->IsObjectArray()) { |
| 84 | // TODO: replace down_cast with AsObjectArray (g++ currently has a problem with this) |
| 85 | ObjectArray<Object>* image_root_object_array |
| 86 | = down_cast<ObjectArray<Object>*>(image_root_object); |
| 87 | // = image_root_object->AsObjectArray<Object>(); |
| 88 | for (int i = 0; i < image_root_object_array->GetLength(); i++) { |
| 89 | os << StringPrintf("\t%d: %p\n", i, image_root_object_array->Get(i)); |
| 90 | } |
| 91 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 92 | } |
| 93 | os << "\n"; |
| 94 | |
| 95 | os << "OBJECTS:\n" << std::flush; |
| 96 | OatDump state(image_space, os); |
| 97 | HeapBitmap* heap_bitmap = Heap::GetLiveBits(); |
| 98 | DCHECK(heap_bitmap != NULL); |
| 99 | heap_bitmap->Walk(OatDump::Callback, &state); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 100 | os << "\n"; |
| 101 | |
| 102 | os << "STATS:\n" << std::flush; |
| 103 | UniquePtr<File> file(OS::OpenFile(image_filename.c_str(), false)); |
| 104 | state.stats_.file_bytes = file->Length(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 105 | size_t header_bytes = sizeof(ImageHeader); |
| 106 | state.stats_.header_bytes = header_bytes; |
| 107 | size_t alignment_bytes = RoundUp(header_bytes, kObjectAlignment) - header_bytes; |
| 108 | state.stats_.alignment_bytes += alignment_bytes; |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 109 | state.stats_.Dump(os); |
| 110 | |
| 111 | os << std::flush; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 114 | private: |
| 115 | |
Elliott Hughes | d1bb4f6 | 2011-09-23 14:09:45 -0700 | [diff] [blame] | 116 | OatDump(const Space& dump_space, std::ostream& os) : dump_space_(dump_space), os_(os) { |
| 117 | } |
| 118 | |
| 119 | ~OatDump() { |
| 120 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 121 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 122 | static void Callback(Object* obj, void* arg) { |
| 123 | DCHECK(obj != NULL); |
| 124 | DCHECK(arg != NULL); |
| 125 | OatDump* state = reinterpret_cast<OatDump*>(arg); |
| 126 | if (!state->InDumpSpace(obj)) { |
| 127 | return; |
| 128 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 129 | |
| 130 | size_t object_bytes = obj->SizeOf(); |
| 131 | size_t alignment_bytes = RoundUp(object_bytes, kObjectAlignment) - object_bytes; |
| 132 | state->stats_.object_bytes += object_bytes; |
| 133 | state->stats_.alignment_bytes += alignment_bytes; |
| 134 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 135 | std::string summary; |
| 136 | StringAppendF(&summary, "%p: ", obj); |
| 137 | if (obj->IsClass()) { |
| 138 | Class* klass = obj->AsClass(); |
| 139 | StringAppendF(&summary, "CLASS %s", klass->GetDescriptor()->ToModifiedUtf8().c_str()); |
Brian Carlstrom | e10b697 | 2011-09-26 13:49:03 -0700 | [diff] [blame] | 140 | std::stringstream ss; |
| 141 | ss << " (" << klass->GetStatus() << ")"; |
| 142 | summary += ss.str(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 143 | } else if (obj->IsMethod()) { |
| 144 | Method* method = obj->AsMethod(); |
| 145 | StringAppendF(&summary, "METHOD %s", PrettyMethod(method).c_str()); |
| 146 | } else if (obj->IsField()) { |
| 147 | Field* field = obj->AsField(); |
| 148 | Class* type = field->GetType(); |
| 149 | std::string type_string; |
| 150 | type_string += (type == NULL) ? "<UNKNOWN>" : type->GetDescriptor()->ToModifiedUtf8(); |
| 151 | StringAppendF(&summary, "FIELD %s", PrettyField(field).c_str()); |
| 152 | } else if (obj->IsArrayInstance()) { |
| 153 | StringAppendF(&summary, "ARRAY %d", obj->AsArray()->GetLength()); |
| 154 | } else if (obj->IsString()) { |
| 155 | StringAppendF(&summary, "STRING %s", obj->AsString()->ToModifiedUtf8().c_str()); |
| 156 | } else { |
| 157 | StringAppendF(&summary, "OBJECT"); |
| 158 | } |
| 159 | StringAppendF(&summary, "\n"); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 160 | std::string descriptor = obj->GetClass()->GetDescriptor()->ToModifiedUtf8(); |
| 161 | StringAppendF(&summary, "\tclass %p: %s\n", obj->GetClass(), descriptor.c_str()); |
| 162 | state->stats_.descriptor_to_bytes[descriptor] += object_bytes; |
| 163 | state->stats_.descriptor_to_count[descriptor] += 1; |
| 164 | // StringAppendF(&summary, "\tsize %d (alignment padding %d)\n", |
| 165 | // object_bytes, RoundUp(object_bytes, kObjectAlignment) - object_bytes); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 166 | if (obj->IsMethod()) { |
| 167 | Method* method = obj->AsMethod(); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 168 | if (!method->IsPhony()) { |
Brian Carlstrom | e10b697 | 2011-09-26 13:49:03 -0700 | [diff] [blame] | 169 | const ByteArray* code = method->GetCodeArray(); |
| 170 | const int8_t* code_base = NULL; |
| 171 | const int8_t* code_limit = NULL; |
| 172 | if (code != NULL) { |
| 173 | size_t code_bytes = code->GetLength(); |
| 174 | code_base = code->GetData(); |
| 175 | code_limit = code_base + code_bytes; |
| 176 | if (method->IsNative()) { |
| 177 | state->stats_.managed_to_native_code_bytes += code_bytes; |
| 178 | } else { |
| 179 | state->stats_.managed_code_bytes += code_bytes; |
| 180 | } |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 181 | } else { |
| 182 | code_base = reinterpret_cast<const int8_t*>(method->GetCode()); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 183 | } |
Brian Carlstrom | e10b697 | 2011-09-26 13:49:03 -0700 | [diff] [blame] | 184 | StringAppendF(&summary, "\tCODE %p-%p\n", code_base, code_limit); |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 185 | |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 186 | const ByteArray* invoke = method->GetInvokeStubArray(); |
Brian Carlstrom | e10b697 | 2011-09-26 13:49:03 -0700 | [diff] [blame] | 187 | const int8_t* invoke_base = NULL; |
| 188 | const int8_t* invoke_limit = NULL; |
| 189 | if (invoke != NULL) { |
| 190 | size_t native_to_managed_code_bytes = invoke->GetLength(); |
| 191 | invoke_base = invoke->GetData(); |
| 192 | invoke_limit = invoke_base + native_to_managed_code_bytes; |
| 193 | state->stats_.native_to_managed_code_bytes += native_to_managed_code_bytes; |
| 194 | StringAppendF(&summary, "\tJNI STUB %p-%p\n", invoke_base, invoke_limit); |
| 195 | } |
Ian Rogers | ff1ed47 | 2011-09-20 13:46:24 -0700 | [diff] [blame] | 196 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 197 | if (method->IsNative()) { |
| 198 | if (method->IsRegistered()) { |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 199 | StringAppendF(&summary, "\tNATIVE REGISTERED %p\n", method->GetNativeMethod()); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 200 | } else { |
| 201 | StringAppendF(&summary, "\tNATIVE UNREGISTERED\n"); |
| 202 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 203 | DCHECK(method->GetRegisterMapHeader() == NULL); |
| 204 | DCHECK(method->GetRegisterMapData() == NULL); |
| 205 | DCHECK(method->GetMappingTable() == NULL); |
| 206 | } else if (method->IsAbstract()) { |
| 207 | StringAppendF(&summary, "\tABSTRACT\n"); |
| 208 | DCHECK(method->GetRegisterMapHeader() == NULL); |
| 209 | DCHECK(method->GetRegisterMapData() == NULL); |
| 210 | DCHECK(method->GetMappingTable() == NULL); |
| 211 | } else if (method->IsPhony()) { |
| 212 | StringAppendF(&summary, "\tPHONY\n"); |
| 213 | DCHECK(method->GetRegisterMapHeader() == NULL); |
| 214 | DCHECK(method->GetRegisterMapData() == NULL); |
| 215 | DCHECK(method->GetMappingTable() == NULL); |
| 216 | } else { |
| 217 | size_t register_map_bytes = (method->GetRegisterMapHeader()->GetLength() + |
| 218 | method->GetRegisterMapData()->GetLength()); |
| 219 | state->stats_.register_map_bytes += register_map_bytes; |
| 220 | |
Brian Carlstrom | e10b697 | 2011-09-26 13:49:03 -0700 | [diff] [blame] | 221 | if (method->GetMappingTable() != NULL) { |
| 222 | size_t pc_mapping_table_bytes = method->GetMappingTable()->GetLength(); |
| 223 | state->stats_.pc_mapping_table_bytes += pc_mapping_table_bytes; |
| 224 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 225 | |
| 226 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 227 | class DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache(); |
| 228 | const DexFile& dex_file = class_linker->FindDexFile(dex_cache); |
| 229 | const DexFile::CodeItem* code_item = dex_file.GetCodeItem(method->GetCodeItemOffset()); |
| 230 | size_t dex_instruction_bytes = code_item->insns_size_ * 2; |
| 231 | state->stats_.dex_instruction_bytes += dex_instruction_bytes; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 234 | state->os_ << summary << std::flush; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 235 | } |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 236 | |
| 237 | bool InDumpSpace(const Object* object) { |
| 238 | const byte* o = reinterpret_cast<const byte*>(object); |
| 239 | return (o >= dump_space_.GetBase() && o < dump_space_.GetLimit()); |
| 240 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 241 | |
| 242 | public: |
| 243 | struct Stats { |
| 244 | size_t file_bytes; |
| 245 | |
| 246 | size_t header_bytes; |
| 247 | size_t object_bytes; |
| 248 | size_t alignment_bytes; |
| 249 | |
| 250 | size_t managed_code_bytes; |
| 251 | size_t managed_to_native_code_bytes; |
| 252 | size_t native_to_managed_code_bytes; |
| 253 | |
| 254 | size_t register_map_bytes; |
| 255 | size_t pc_mapping_table_bytes; |
| 256 | |
| 257 | size_t dex_instruction_bytes; |
| 258 | |
| 259 | Stats() |
| 260 | : file_bytes(0), |
| 261 | header_bytes(0), |
| 262 | object_bytes(0), |
| 263 | alignment_bytes(0), |
| 264 | managed_code_bytes(0), |
| 265 | managed_to_native_code_bytes(0), |
| 266 | native_to_managed_code_bytes(0), |
| 267 | register_map_bytes(0), |
| 268 | pc_mapping_table_bytes(0), |
| 269 | dex_instruction_bytes(0) {} |
| 270 | |
| 271 | typedef std::tr1::unordered_map<std::string,size_t> TableBytes; |
| 272 | TableBytes descriptor_to_bytes; |
| 273 | |
| 274 | typedef std::tr1::unordered_map<std::string,size_t> TableCount; |
| 275 | TableCount descriptor_to_count; |
| 276 | |
| 277 | double PercentOfFileBytes(size_t size) { |
| 278 | return (static_cast<double>(size) / static_cast<double>(file_bytes)) * 100; |
| 279 | } |
| 280 | |
| 281 | double PercentOfObjectBytes(size_t size) { |
| 282 | return (static_cast<double>(size) / static_cast<double>(object_bytes)) * 100; |
| 283 | } |
| 284 | |
| 285 | void Dump(std::ostream& os) { |
| 286 | os << StringPrintf("\tfile_bytes = %d\n", file_bytes); |
| 287 | os << "\n"; |
| 288 | |
| 289 | os << "\tfile_bytes = header_bytes + object_bytes + alignment_bytes\n"; |
| 290 | os << StringPrintf("\theader_bytes = %10d (%2.0f%% of file_bytes)\n", |
| 291 | header_bytes, PercentOfFileBytes(header_bytes)); |
| 292 | os << StringPrintf("\tobject_bytes = %10d (%2.0f%% of file_bytes)\n", |
| 293 | object_bytes, PercentOfFileBytes(object_bytes)); |
| 294 | os << StringPrintf("\talignment_bytes = %10d (%2.0f%% of file_bytes)\n", |
| 295 | alignment_bytes, PercentOfFileBytes(alignment_bytes)); |
| 296 | os << "\n"; |
| 297 | os << std::flush; |
| 298 | CHECK_EQ(file_bytes, header_bytes + object_bytes + alignment_bytes); |
| 299 | |
| 300 | os << "\tobject_bytes = sum of descriptor_to_bytes values below:\n"; |
| 301 | size_t object_bytes_total = 0; |
| 302 | typedef TableBytes::const_iterator It; // TODO: C++0x auto |
| 303 | for (It it = descriptor_to_bytes.begin(), end = descriptor_to_bytes.end(); it != end; ++it) { |
| 304 | const std::string& descriptor = it->first; |
| 305 | size_t bytes = it->second; |
| 306 | size_t count = descriptor_to_count[descriptor]; |
| 307 | double average = static_cast<double>(bytes) / static_cast<double>(count); |
| 308 | double percent = PercentOfObjectBytes(bytes); |
Brian Carlstrom | f153fe1 | 2011-09-27 16:27:12 -0700 | [diff] [blame] | 309 | os << StringPrintf("\t%32s %8d bytes %6d instances " |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 310 | "(%3.0f bytes/instance) %2.0f%% of object_bytes\n", |
| 311 | descriptor.c_str(), bytes, count, |
| 312 | average, percent); |
| 313 | |
| 314 | object_bytes_total += bytes; |
| 315 | } |
| 316 | os << "\n"; |
| 317 | os << std::flush; |
| 318 | CHECK_EQ(object_bytes, object_bytes_total); |
| 319 | |
| 320 | os << StringPrintf("\tmanaged_code_bytes = %8d (%2.0f%% of object_bytes)\n", |
| 321 | managed_code_bytes, PercentOfObjectBytes(managed_code_bytes)); |
| 322 | os << StringPrintf("\tmanaged_to_native_code_bytes = %8d (%2.0f%% of object_bytes)\n", |
| 323 | managed_to_native_code_bytes, |
| 324 | PercentOfObjectBytes(managed_to_native_code_bytes)); |
| 325 | os << StringPrintf("\tnative_to_managed_code_bytes = %8d (%2.0f%% of object_bytes)\n", |
| 326 | native_to_managed_code_bytes, |
| 327 | PercentOfObjectBytes(native_to_managed_code_bytes)); |
| 328 | os << "\n"; |
| 329 | os << std::flush; |
| 330 | |
| 331 | os << StringPrintf("\tregister_map_bytes = %7d (%2.0f%% of object_bytes)\n", |
| 332 | register_map_bytes, PercentOfObjectBytes(register_map_bytes)); |
| 333 | os << StringPrintf("\tpc_mapping_table_bytes = %7d (%2.0f%% of object_bytes)\n", |
| 334 | pc_mapping_table_bytes, PercentOfObjectBytes(pc_mapping_table_bytes)); |
| 335 | os << "\n"; |
| 336 | os << std::flush; |
| 337 | |
| 338 | os << StringPrintf("\tdex_instruction_bytes = %d\n", dex_instruction_bytes); |
| 339 | os << StringPrintf("\tmanaged_code_bytes expansion = %.2f\n", |
| 340 | static_cast<double>(managed_code_bytes) |
| 341 | / static_cast<double>(dex_instruction_bytes)); |
| 342 | os << "\n"; |
| 343 | os << std::flush; |
| 344 | |
| 345 | } |
| 346 | } stats_; |
| 347 | |
| 348 | private: |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 349 | const Space& dump_space_; |
| 350 | std::ostream& os_; |
Elliott Hughes | d1bb4f6 | 2011-09-23 14:09:45 -0700 | [diff] [blame] | 351 | |
| 352 | DISALLOW_COPY_AND_ASSIGN(OatDump); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | int oatdump(int argc, char** argv) { |
| 356 | // Skip over argv[0]. |
| 357 | argv++; |
| 358 | argc--; |
| 359 | |
| 360 | if (argc == 0) { |
| 361 | fprintf(stderr, "no arguments specified\n"); |
| 362 | usage(); |
| 363 | } |
| 364 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 365 | const char* image_filename = NULL; |
| 366 | const char* boot_image_filename = NULL; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 367 | std::string host_prefix; |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 368 | std::ostream* os = &std::cout; |
| 369 | UniquePtr<std::ofstream> out; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 370 | |
| 371 | for (int i = 0; i < argc; i++) { |
| 372 | const StringPiece option(argv[i]); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 373 | if (option.starts_with("--image=")) { |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 374 | image_filename = option.substr(strlen("--image=")).data(); |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 375 | } else if (option.starts_with("--boot-image=")) { |
| 376 | boot_image_filename = option.substr(strlen("--boot-image=")).data(); |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 377 | } else if (option.starts_with("--host-prefix=")) { |
| 378 | host_prefix = option.substr(strlen("--host-prefix=")).data(); |
Brian Carlstrom | 27ec961 | 2011-09-19 20:20:38 -0700 | [diff] [blame] | 379 | } else if (option.starts_with("--output=")) { |
| 380 | const char* filename = option.substr(strlen("--output=")).data(); |
| 381 | out.reset(new std::ofstream(filename)); |
| 382 | if (!out->good()) { |
| 383 | fprintf(stderr, "failed to open output filename %s\n", filename); |
| 384 | usage(); |
| 385 | } |
| 386 | os = out.get(); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 387 | } else { |
| 388 | fprintf(stderr, "unknown argument %s\n", option.data()); |
| 389 | usage(); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | if (image_filename == NULL) { |
| 394 | fprintf(stderr, "--image file name not specified\n"); |
| 395 | return EXIT_FAILURE; |
| 396 | } |
| 397 | |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 398 | Runtime::Options options; |
| 399 | std::string image_option; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 400 | std::string oat_option; |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 401 | std::string boot_image_option; |
Brian Carlstrom | e24fa61 | 2011-09-29 00:53:55 -0700 | [diff] [blame] | 402 | std::string boot_oat_option; |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 403 | if (boot_image_filename != NULL) { |
| 404 | boot_image_option += "-Ximage:"; |
| 405 | boot_image_option += boot_image_filename; |
| 406 | options.push_back(std::make_pair(boot_image_option.c_str(), reinterpret_cast<void*>(NULL))); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 407 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 408 | image_option += "-Ximage:"; |
| 409 | image_option += image_filename; |
| 410 | options.push_back(std::make_pair(image_option.c_str(), reinterpret_cast<void*>(NULL))); |
| 411 | |
| 412 | if (!host_prefix.empty()) { |
| 413 | options.push_back(std::make_pair("host-prefix", host_prefix.c_str())); |
| 414 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 415 | |
| 416 | UniquePtr<Runtime> runtime(Runtime::Create(options, false)); |
| 417 | if (runtime.get() == NULL) { |
| 418 | fprintf(stderr, "could not create runtime\n"); |
| 419 | return EXIT_FAILURE; |
| 420 | } |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 421 | |
| 422 | Space* image_space = Heap::GetSpaces()[Heap::GetSpaces().size()-2]; |
| 423 | CHECK(image_space != NULL); |
| 424 | const ImageHeader& image_header = image_space->GetImageHeader(); |
| 425 | if (!image_header.IsValid()) { |
| 426 | fprintf(stderr, "invalid image header %s\n", image_filename); |
| 427 | return EXIT_FAILURE; |
| 428 | } |
Brian Carlstrom | 916e74e | 2011-09-23 11:42:01 -0700 | [diff] [blame] | 429 | OatDump::Dump(image_filename, *os, *image_space, image_header); |
Brian Carlstrom | 78128a6 | 2011-09-15 17:21:19 -0700 | [diff] [blame] | 430 | return EXIT_SUCCESS; |
| 431 | } |
| 432 | |
| 433 | } // namespace art |
| 434 | |
| 435 | int main(int argc, char** argv) { |
| 436 | return art::oatdump(argc, argv); |
| 437 | } |