Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 3481 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3492 int HeapSnapshotJSONSerializer::GetStringId(const char* s) { | 3492 int HeapSnapshotJSONSerializer::GetStringId(const char* s) { |
| 3493 HashMap::Entry* cache_entry = strings_.Lookup( | 3493 HashMap::Entry* cache_entry = strings_.Lookup( |
| 3494 const_cast<char*>(s), ObjectHash(s), true); | 3494 const_cast<char*>(s), ObjectHash(s), true); |
| 3495 if (cache_entry->value == NULL) { | 3495 if (cache_entry->value == NULL) { |
| 3496 cache_entry->value = reinterpret_cast<void*>(next_string_id_++); | 3496 cache_entry->value = reinterpret_cast<void*>(next_string_id_++); |
| 3497 } | 3497 } |
| 3498 return static_cast<int>(reinterpret_cast<intptr_t>(cache_entry->value)); | 3498 return static_cast<int>(reinterpret_cast<intptr_t>(cache_entry->value)); |
| 3499 } | 3499 } |
| 3500 | 3500 |
| 3501 | 3501 |
| 3502 // This function won't work correctly for MIN_INT but this is not | 3502 static int utoa(unsigned value, const Vector<char>& buffer, int buffer_pos) { |
|
yurys
2012/05/12 16:26:33
Not directly related to this change but we need to
| |
| 3503 // a problem in case of heap snapshots serialization. | |
| 3504 static int itoa(int value, const Vector<char>& buffer, int buffer_pos) { | |
| 3505 if (value < 0) { | |
| 3506 buffer[buffer_pos++] = '-'; | |
| 3507 value = -value; | |
| 3508 } | |
| 3509 | |
| 3510 int number_of_digits = 0; | 3503 int number_of_digits = 0; |
| 3511 int t = value; | 3504 unsigned t = value; |
| 3512 do { | 3505 do { |
| 3513 ++number_of_digits; | 3506 ++number_of_digits; |
| 3514 } while (t /= 10); | 3507 } while (t /= 10); |
| 3515 | 3508 |
| 3516 buffer_pos += number_of_digits; | 3509 buffer_pos += number_of_digits; |
| 3517 int result = buffer_pos; | 3510 int result = buffer_pos; |
| 3518 do { | 3511 do { |
| 3519 int last_digit = value % 10; | 3512 int last_digit = value % 10; |
| 3520 buffer[--buffer_pos] = '0' + last_digit; | 3513 buffer[--buffer_pos] = '0' + last_digit; |
| 3521 value /= 10; | 3514 value /= 10; |
| 3522 } while (value); | 3515 } while (value); |
| 3523 return result; | 3516 return result; |
| 3524 } | 3517 } |
| 3525 | 3518 |
| 3526 | 3519 |
| 3527 void HeapSnapshotJSONSerializer::SerializeEdge(HeapGraphEdge* edge, | 3520 void HeapSnapshotJSONSerializer::SerializeEdge(HeapGraphEdge* edge, |
| 3528 bool first_edge) { | 3521 bool first_edge) { |
| 3529 // The buffer needs space for 3 ints, 3 commas and \0 | 3522 // The buffer needs space for 3 ints, 3 commas and \0 |
| 3530 static const int kBufferSize = | 3523 static const int kBufferSize = |
| 3531 MaxDecimalDigitsIn<sizeof(int)>::kSigned * 3 + 3 + 1; // NOLINT | 3524 MaxDecimalDigitsIn<sizeof(int)>::kSigned * 3 + 3 + 1; // NOLINT |
| 3532 EmbeddedVector<char, kBufferSize> buffer; | 3525 EmbeddedVector<char, kBufferSize> buffer; |
| 3533 int edge_name_or_index = edge->type() == HeapGraphEdge::kElement | 3526 int edge_name_or_index = edge->type() == HeapGraphEdge::kElement |
| 3534 || edge->type() == HeapGraphEdge::kHidden | 3527 || edge->type() == HeapGraphEdge::kHidden |
| 3535 || edge->type() == HeapGraphEdge::kWeak | 3528 || edge->type() == HeapGraphEdge::kWeak |
| 3536 ? edge->index() : GetStringId(edge->name()); | 3529 ? edge->index() : GetStringId(edge->name()); |
| 3537 int buffer_pos = 0; | 3530 int buffer_pos = 0; |
| 3538 if (!first_edge) { | 3531 if (!first_edge) { |
| 3539 buffer[buffer_pos++] = ','; | 3532 buffer[buffer_pos++] = ','; |
| 3540 } | 3533 } |
| 3541 buffer_pos = itoa(edge->type(), buffer, buffer_pos); | 3534 buffer_pos = utoa(edge->type(), buffer, buffer_pos); |
| 3542 buffer[buffer_pos++] = ','; | 3535 buffer[buffer_pos++] = ','; |
| 3543 buffer_pos = itoa(edge_name_or_index, buffer, buffer_pos); | 3536 buffer_pos = utoa(edge_name_or_index, buffer, buffer_pos); |
| 3544 buffer[buffer_pos++] = ','; | 3537 buffer[buffer_pos++] = ','; |
| 3545 buffer_pos = itoa(entry_index(edge->to()), buffer, buffer_pos); | 3538 buffer_pos = utoa(entry_index(edge->to()), buffer, buffer_pos); |
| 3546 buffer[buffer_pos++] = '\0'; | 3539 buffer[buffer_pos++] = '\0'; |
| 3547 writer_->AddString(buffer.start()); | 3540 writer_->AddString(buffer.start()); |
| 3548 } | 3541 } |
| 3549 | 3542 |
| 3550 | 3543 |
| 3551 void HeapSnapshotJSONSerializer::SerializeEdges(const List<HeapEntry>& nodes) { | 3544 void HeapSnapshotJSONSerializer::SerializeEdges(const List<HeapEntry>& nodes) { |
| 3552 bool first_edge = true; | 3545 bool first_edge = true; |
| 3553 for (int i = 0; i < nodes.length(); ++i) { | 3546 for (int i = 0; i < nodes.length(); ++i) { |
| 3554 HeapEntry* entry = &nodes[i]; | 3547 HeapEntry* entry = &nodes[i]; |
| 3555 Vector<HeapGraphEdge*> children = entry->children(); | 3548 Vector<HeapGraphEdge*> children = entry->children(); |
| 3556 for (int j = 0; j < children.length(); ++j) { | 3549 for (int j = 0; j < children.length(); ++j) { |
| 3557 SerializeEdge(children[j], first_edge); | 3550 SerializeEdge(children[j], first_edge); |
| 3558 first_edge = false; | 3551 first_edge = false; |
| 3559 if (writer_->aborted()) return; | 3552 if (writer_->aborted()) return; |
| 3560 } | 3553 } |
| 3561 } | 3554 } |
| 3562 } | 3555 } |
| 3563 | 3556 |
| 3564 | 3557 |
| 3565 void HeapSnapshotJSONSerializer::SerializeNode(HeapEntry* entry, | 3558 void HeapSnapshotJSONSerializer::SerializeNode(HeapEntry* entry, |
| 3566 int edges_index) { | 3559 int edges_index) { |
| 3567 // The buffer needs space for 6 ints, 1 uint32_t, 7 commas, \n and \0 | 3560 // The buffer needs space for 6 ints, 1 uint32_t, 7 commas, \n and \0 |
| 3568 static const int kBufferSize = | 3561 static const int kBufferSize = |
| 3569 6 * MaxDecimalDigitsIn<sizeof(int)>::kSigned // NOLINT | 3562 6 * MaxDecimalDigitsIn<sizeof(int)>::kSigned // NOLINT |
| 3570 + MaxDecimalDigitsIn<sizeof(uint32_t)>::kUnsigned // NOLINT | 3563 + MaxDecimalDigitsIn<sizeof(uint32_t)>::kUnsigned // NOLINT |
| 3571 + 7 + 1 + 1; | 3564 + 7 + 1 + 1; |
| 3572 EmbeddedVector<char, kBufferSize> buffer; | 3565 EmbeddedVector<char, kBufferSize> buffer; |
| 3573 int buffer_pos = 0; | 3566 int buffer_pos = 0; |
| 3574 buffer[buffer_pos++] = '\n'; | |
| 3575 if (entry_index(entry) != 0) { | 3567 if (entry_index(entry) != 0) { |
| 3576 buffer[buffer_pos++] = ','; | 3568 buffer[buffer_pos++] = ','; |
| 3577 } | 3569 } |
| 3578 buffer_pos = itoa(entry->type(), buffer, buffer_pos); | 3570 buffer_pos = utoa(entry->type(), buffer, buffer_pos); |
| 3579 buffer[buffer_pos++] = ','; | 3571 buffer[buffer_pos++] = ','; |
| 3580 buffer_pos = itoa(GetStringId(entry->name()), buffer, buffer_pos); | 3572 buffer_pos = utoa(GetStringId(entry->name()), buffer, buffer_pos); |
| 3581 buffer[buffer_pos++] = ','; | 3573 buffer[buffer_pos++] = ','; |
| 3582 buffer_pos = itoa(entry->id(), buffer, buffer_pos); | 3574 buffer_pos = utoa(entry->id(), buffer, buffer_pos); |
| 3583 buffer[buffer_pos++] = ','; | 3575 buffer[buffer_pos++] = ','; |
| 3584 buffer_pos = itoa(entry->self_size(), buffer, buffer_pos); | 3576 buffer_pos = utoa(entry->self_size(), buffer, buffer_pos); |
| 3585 buffer[buffer_pos++] = ','; | 3577 buffer[buffer_pos++] = ','; |
| 3586 buffer_pos = itoa(entry->retained_size(), buffer, buffer_pos); | 3578 buffer_pos = utoa(entry->retained_size(), buffer, buffer_pos); |
| 3587 buffer[buffer_pos++] = ','; | 3579 buffer[buffer_pos++] = ','; |
| 3588 buffer_pos = itoa(entry_index(entry->dominator()), buffer, buffer_pos); | 3580 buffer_pos = utoa(entry_index(entry->dominator()), buffer, buffer_pos); |
| 3589 buffer[buffer_pos++] = ','; | 3581 buffer[buffer_pos++] = ','; |
| 3590 buffer_pos = itoa(edges_index, buffer, buffer_pos); | 3582 buffer_pos = utoa(edges_index, buffer, buffer_pos); |
| 3583 buffer[buffer_pos++] = '\n'; | |
| 3591 buffer[buffer_pos++] = '\0'; | 3584 buffer[buffer_pos++] = '\0'; |
| 3592 writer_->AddString(buffer.start()); | 3585 writer_->AddString(buffer.start()); |
| 3593 } | 3586 } |
| 3594 | 3587 |
| 3595 | 3588 |
| 3596 void HeapSnapshotJSONSerializer::SerializeNodes(const List<HeapEntry>& nodes) { | 3589 void HeapSnapshotJSONSerializer::SerializeNodes(const List<HeapEntry>& nodes) { |
| 3597 int edges_index = 0; | 3590 int edges_index = 0; |
| 3598 for (int i = 0; i < nodes.length(); ++i) { | 3591 for (int i = 0; i < nodes.length(); ++i) { |
| 3599 HeapEntry* entry = &nodes[i]; | 3592 HeapEntry* entry = &nodes[i]; |
| 3600 SerializeNode(entry, edges_index); | 3593 SerializeNode(entry, edges_index); |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3755 | 3748 |
| 3756 | 3749 |
| 3757 void HeapSnapshotJSONSerializer::SortHashMap( | 3750 void HeapSnapshotJSONSerializer::SortHashMap( |
| 3758 HashMap* map, List<HashMap::Entry*>* sorted_entries) { | 3751 HashMap* map, List<HashMap::Entry*>* sorted_entries) { |
| 3759 for (HashMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) | 3752 for (HashMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) |
| 3760 sorted_entries->Add(p); | 3753 sorted_entries->Add(p); |
| 3761 sorted_entries->Sort(SortUsingEntryValue); | 3754 sorted_entries->Sort(SortUsingEntryValue); |
| 3762 } | 3755 } |
| 3763 | 3756 |
| 3764 } } // namespace v8::internal | 3757 } } // namespace v8::internal |
| OLD | NEW |