OLD | NEW |
---|---|
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 11 matching lines...) Expand all Loading... | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 #include "v8.h" | 28 #include "v8.h" |
29 | 29 |
30 #include "heap-snapshot-generator-inl.h" | 30 #include "heap-snapshot-generator-inl.h" |
31 | 31 |
32 #include "allocation-tracker.h" | |
32 #include "heap-profiler.h" | 33 #include "heap-profiler.h" |
33 #include "debug.h" | 34 #include "debug.h" |
34 #include "types.h" | 35 #include "types.h" |
35 | 36 |
36 namespace v8 { | 37 namespace v8 { |
37 namespace internal { | 38 namespace internal { |
38 | 39 |
39 | 40 |
40 HeapGraphEdge::HeapGraphEdge(Type type, const char* name, int from, int to) | 41 HeapGraphEdge::HeapGraphEdge(Type type, const char* name, int from, int to) |
41 : type_(type), | 42 : type_(type), |
(...skipping 585 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
627 ids_(heap) { | 628 ids_(heap) { |
628 } | 629 } |
629 | 630 |
630 | 631 |
631 static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) { | 632 static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) { |
632 delete *snapshot_ptr; | 633 delete *snapshot_ptr; |
633 } | 634 } |
634 | 635 |
635 | 636 |
636 HeapSnapshotsCollection::~HeapSnapshotsCollection() { | 637 HeapSnapshotsCollection::~HeapSnapshotsCollection() { |
638 delete allocation_tracker_; | |
637 snapshots_.Iterate(DeleteHeapSnapshot); | 639 snapshots_.Iterate(DeleteHeapSnapshot); |
638 } | 640 } |
639 | 641 |
640 | 642 |
643 void HeapSnapshotsCollection::StartHeapObjectsTracking() { | |
644 ids_.UpdateHeapObjectsMap(); | |
645 if (allocation_tracker_ == NULL) { | |
646 allocation_tracker_ = new AllocationTracker(&ids_, names()); | |
647 } | |
648 is_tracking_objects_ = true; | |
649 } | |
650 | |
651 | |
652 void HeapSnapshotsCollection::StopHeapObjectsTracking() { | |
653 ids_.StopHeapObjectsTracking(); | |
654 if (allocation_tracker_ != NULL) { | |
655 delete allocation_tracker_; | |
656 allocation_tracker_ = NULL; | |
657 } | |
658 } | |
659 | |
660 | |
641 HeapSnapshot* HeapSnapshotsCollection::NewSnapshot(const char* name, | 661 HeapSnapshot* HeapSnapshotsCollection::NewSnapshot(const char* name, |
642 unsigned uid) { | 662 unsigned uid) { |
643 is_tracking_objects_ = true; // Start watching for heap objects moves. | 663 is_tracking_objects_ = true; // Start watching for heap objects moves. |
644 return new HeapSnapshot(this, name, uid); | 664 return new HeapSnapshot(this, name, uid); |
645 } | 665 } |
646 | 666 |
647 | 667 |
648 void HeapSnapshotsCollection::SnapshotGenerationFinished( | 668 void HeapSnapshotsCollection::SnapshotGenerationFinished( |
649 HeapSnapshot* snapshot) { | 669 HeapSnapshot* snapshot) { |
650 ids_.SnapshotGenerationFinished(); | 670 ids_.SnapshotGenerationFinished(); |
(...skipping 23 matching lines...) Expand all Loading... | |
674 if (ids_.FindEntry(obj->address()) == id) { | 694 if (ids_.FindEntry(obj->address()) == id) { |
675 ASSERT(object == NULL); | 695 ASSERT(object == NULL); |
676 object = obj; | 696 object = obj; |
677 // Can't break -- kFilterUnreachable requires full heap traversal. | 697 // Can't break -- kFilterUnreachable requires full heap traversal. |
678 } | 698 } |
679 } | 699 } |
680 return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>(); | 700 return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>(); |
681 } | 701 } |
682 | 702 |
683 | 703 |
704 void HeapSnapshotsCollection::NewObjectEvent(Address addr, int size) { | |
705 DisallowHeapAllocation no_allocation; | |
706 ids_.NewObject(addr, size); | |
707 if (allocation_tracker_ != NULL) { | |
708 allocation_tracker_->NewObjectEvent(addr, size); | |
709 } | |
710 } | |
711 | |
712 | |
684 size_t HeapSnapshotsCollection::GetUsedMemorySize() const { | 713 size_t HeapSnapshotsCollection::GetUsedMemorySize() const { |
685 size_t size = sizeof(*this); | 714 size_t size = sizeof(*this); |
686 size += names_.GetUsedMemorySize(); | 715 size += names_.GetUsedMemorySize(); |
687 size += ids_.GetUsedMemorySize(); | 716 size += ids_.GetUsedMemorySize(); |
688 size += GetMemoryUsedByList(snapshots_); | 717 size += GetMemoryUsedByList(snapshots_); |
689 for (int i = 0; i < snapshots_.length(); ++i) { | 718 for (int i = 0; i < snapshots_.length(); ++i) { |
690 size += snapshots_[i]->RawSnapshotSize(); | 719 size += snapshots_[i]->RawSnapshotSize(); |
691 } | 720 } |
692 return size; | 721 return size; |
693 } | 722 } |
(...skipping 1778 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2472 bool aborted_; | 2501 bool aborted_; |
2473 }; | 2502 }; |
2474 | 2503 |
2475 | 2504 |
2476 // type, name|index, to_node. | 2505 // type, name|index, to_node. |
2477 const int HeapSnapshotJSONSerializer::kEdgeFieldsCount = 3; | 2506 const int HeapSnapshotJSONSerializer::kEdgeFieldsCount = 3; |
2478 // type, name, id, self_size, children_index. | 2507 // type, name, id, self_size, children_index. |
2479 const int HeapSnapshotJSONSerializer::kNodeFieldsCount = 5; | 2508 const int HeapSnapshotJSONSerializer::kNodeFieldsCount = 5; |
2480 | 2509 |
2481 void HeapSnapshotJSONSerializer::Serialize(v8::OutputStream* stream) { | 2510 void HeapSnapshotJSONSerializer::Serialize(v8::OutputStream* stream) { |
2511 if (AllocationTracker* allocation_tracker = | |
2512 snapshot_->collection()->allocation_tracker()) { | |
2513 allocation_tracker->PrepareForSerialization(); | |
2514 } | |
2482 ASSERT(writer_ == NULL); | 2515 ASSERT(writer_ == NULL); |
2483 writer_ = new OutputStreamWriter(stream); | 2516 writer_ = new OutputStreamWriter(stream); |
2484 SerializeImpl(); | 2517 SerializeImpl(); |
2485 delete writer_; | 2518 delete writer_; |
2486 writer_ = NULL; | 2519 writer_ = NULL; |
2487 } | 2520 } |
2488 | 2521 |
2489 | 2522 |
2490 void HeapSnapshotJSONSerializer::SerializeImpl() { | 2523 void HeapSnapshotJSONSerializer::SerializeImpl() { |
2491 ASSERT(0 == snapshot_->root()->index()); | 2524 ASSERT(0 == snapshot_->root()->index()); |
2492 writer_->AddCharacter('{'); | 2525 writer_->AddCharacter('{'); |
2493 writer_->AddString("\"snapshot\":{"); | 2526 writer_->AddString("\"snapshot\":{"); |
2494 SerializeSnapshot(); | 2527 SerializeSnapshot(); |
2495 if (writer_->aborted()) return; | 2528 if (writer_->aborted()) return; |
2496 writer_->AddString("},\n"); | 2529 writer_->AddString("},\n"); |
2497 writer_->AddString("\"nodes\":["); | 2530 writer_->AddString("\"nodes\":["); |
2498 SerializeNodes(); | 2531 SerializeNodes(); |
2499 if (writer_->aborted()) return; | 2532 if (writer_->aborted()) return; |
2500 writer_->AddString("],\n"); | 2533 writer_->AddString("],\n"); |
2501 writer_->AddString("\"edges\":["); | 2534 writer_->AddString("\"edges\":["); |
2502 SerializeEdges(); | 2535 SerializeEdges(); |
2503 if (writer_->aborted()) return; | 2536 if (writer_->aborted()) return; |
2504 writer_->AddString("],\n"); | 2537 writer_->AddString("],\n"); |
2538 | |
2539 writer_->AddString("\"trace_function_infos\":["); | |
2540 SerializeTraceNodeInfos(); | |
2541 if (writer_->aborted()) return; | |
2542 writer_->AddString("],\n"); | |
2543 writer_->AddString("\"trace_tree\":["); | |
2544 SerializeTraceTree(); | |
2545 if (writer_->aborted()) return; | |
2546 writer_->AddString("],\n"); | |
2547 | |
2505 writer_->AddString("\"strings\":["); | 2548 writer_->AddString("\"strings\":["); |
2506 SerializeStrings(); | 2549 SerializeStrings(); |
2507 if (writer_->aborted()) return; | 2550 if (writer_->aborted()) return; |
2508 writer_->AddCharacter(']'); | 2551 writer_->AddCharacter(']'); |
2509 writer_->AddCharacter('}'); | 2552 writer_->AddCharacter('}'); |
2510 writer_->Finalize(); | 2553 writer_->Finalize(); |
2511 } | 2554 } |
2512 | 2555 |
2513 | 2556 |
2514 int HeapSnapshotJSONSerializer::GetStringId(const char* s) { | 2557 int HeapSnapshotJSONSerializer::GetStringId(const char* s) { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2655 JSON_S("edge_types") ":" JSON_A( | 2698 JSON_S("edge_types") ":" JSON_A( |
2656 JSON_A( | 2699 JSON_A( |
2657 JSON_S("context") "," | 2700 JSON_S("context") "," |
2658 JSON_S("element") "," | 2701 JSON_S("element") "," |
2659 JSON_S("property") "," | 2702 JSON_S("property") "," |
2660 JSON_S("internal") "," | 2703 JSON_S("internal") "," |
2661 JSON_S("hidden") "," | 2704 JSON_S("hidden") "," |
2662 JSON_S("shortcut") "," | 2705 JSON_S("shortcut") "," |
2663 JSON_S("weak")) "," | 2706 JSON_S("weak")) "," |
2664 JSON_S("string_or_number") "," | 2707 JSON_S("string_or_number") "," |
2665 JSON_S("node")))); | 2708 JSON_S("node")) "," |
2709 JSON_S("trace_function_info_fields") ":" JSON_A( | |
2710 JSON_S("function_id") "," | |
2711 JSON_S("name") "," | |
2712 JSON_S("script_name") "," | |
2713 JSON_S("script_id") "," | |
2714 JSON_S("line") "," | |
2715 JSON_S("column")) "," | |
2716 JSON_S("trace_node_fields") ":" JSON_A( | |
2717 JSON_S("id") "," | |
2718 JSON_S("function_id") "," | |
2719 JSON_S("count") "," | |
2720 JSON_S("size") "," | |
2721 JSON_S("children")))); | |
2666 #undef JSON_S | 2722 #undef JSON_S |
2667 #undef JSON_O | 2723 #undef JSON_O |
2668 #undef JSON_A | 2724 #undef JSON_A |
2669 writer_->AddString(",\"node_count\":"); | 2725 writer_->AddString(",\"node_count\":"); |
2670 writer_->AddNumber(snapshot_->entries().length()); | 2726 writer_->AddNumber(snapshot_->entries().length()); |
2671 writer_->AddString(",\"edge_count\":"); | 2727 writer_->AddString(",\"edge_count\":"); |
2672 writer_->AddNumber(snapshot_->edges().length()); | 2728 writer_->AddNumber(snapshot_->edges().length()); |
2729 writer_->AddString(",\"trace_function_count\":"); | |
2730 uint32_t count = 0; | |
2731 AllocationTracker* tracker = snapshot_->collection()->allocation_tracker(); | |
2732 if (tracker) { | |
2733 count = tracker->id_to_function_info()->occupancy(); | |
2734 } | |
2735 writer_->AddNumber(count); | |
2673 } | 2736 } |
2674 | 2737 |
2675 | 2738 |
2676 static void WriteUChar(OutputStreamWriter* w, unibrow::uchar u) { | 2739 static void WriteUChar(OutputStreamWriter* w, unibrow::uchar u) { |
2677 static const char hex_chars[] = "0123456789ABCDEF"; | 2740 static const char hex_chars[] = "0123456789ABCDEF"; |
2678 w->AddString("\\u"); | 2741 w->AddString("\\u"); |
2679 w->AddCharacter(hex_chars[(u >> 12) & 0xf]); | 2742 w->AddCharacter(hex_chars[(u >> 12) & 0xf]); |
2680 w->AddCharacter(hex_chars[(u >> 8) & 0xf]); | 2743 w->AddCharacter(hex_chars[(u >> 8) & 0xf]); |
2681 w->AddCharacter(hex_chars[(u >> 4) & 0xf]); | 2744 w->AddCharacter(hex_chars[(u >> 4) & 0xf]); |
2682 w->AddCharacter(hex_chars[u & 0xf]); | 2745 w->AddCharacter(hex_chars[u & 0xf]); |
2683 } | 2746 } |
2684 | 2747 |
2685 | 2748 |
2749 void HeapSnapshotJSONSerializer::SerializeTraceTree() { | |
2750 AllocationTracker* tracker = snapshot_->collection()->allocation_tracker(); | |
2751 if (!tracker) return; | |
2752 AllocationTraceTree* traces = tracker->trace_tree(); | |
2753 SerializeTraceNode(traces->root()); | |
2754 } | |
2755 | |
2756 | |
2757 void HeapSnapshotJSONSerializer::SerializeTraceNode(AllocationTraceNode* node) { | |
2758 // The buffer needs space for 4 unsigned ints, 4 commas, [ and \0 | |
2759 const int kBufferSize = | |
2760 4 * MaxDecimalDigitsIn<sizeof(unsigned)>::kUnsigned // NOLINT | |
2761 + 4 + 1 + 1; | |
Hannes Payer (out of office)
2013/10/21 09:57:22
Why don't you define this constant in the header a
yurys
2013/10/21 15:07:56
Not sure I follow you here, this constant is used
| |
2762 EmbeddedVector<char, kBufferSize> buffer; | |
2763 int buffer_pos = 0; | |
2764 buffer_pos = utoa(node->id(), buffer, buffer_pos); | |
2765 buffer[buffer_pos++] = ','; | |
2766 buffer_pos = utoa(node->function_id(), buffer, buffer_pos); | |
2767 buffer[buffer_pos++] = ','; | |
2768 buffer_pos = utoa(node->allocation_count(), buffer, buffer_pos); | |
2769 buffer[buffer_pos++] = ','; | |
2770 buffer_pos = utoa(node->allocation_size(), buffer, buffer_pos); | |
2771 buffer[buffer_pos++] = ','; | |
2772 buffer[buffer_pos++] = '['; | |
2773 buffer[buffer_pos++] = '\0'; | |
2774 writer_->AddString(buffer.start()); | |
2775 | |
2776 Vector<AllocationTraceNode*> children = node->children(); | |
2777 for (int i = 0; i < children.length(); i++) { | |
2778 if (i > 0) { | |
2779 writer_->AddCharacter(','); | |
2780 } | |
2781 SerializeTraceNode(children[i]); | |
2782 } | |
2783 writer_->AddCharacter(']'); | |
2784 } | |
2785 | |
2786 | |
2787 // 0-based position is converted to 1-based during the serialization. | |
2788 static int SerializePosition(int position, const Vector<char>& buffer, | |
2789 int buffer_pos) { | |
2790 if (position == -1) { | |
2791 buffer[buffer_pos++] = '0'; | |
2792 } else { | |
2793 ASSERT(position >= 0); | |
2794 buffer_pos = utoa(static_cast<unsigned>(position + 1), buffer, buffer_pos); | |
2795 } | |
2796 return buffer_pos; | |
2797 } | |
2798 | |
2799 | |
2800 void HeapSnapshotJSONSerializer::SerializeTraceNodeInfos() { | |
2801 AllocationTracker* tracker = snapshot_->collection()->allocation_tracker(); | |
2802 if (!tracker) return; | |
2803 // The buffer needs space for 6 unsigned ints, 6 commas, \n and \0 | |
2804 const int kBufferSize = | |
2805 6 * MaxDecimalDigitsIn<sizeof(unsigned)>::kUnsigned // NOLINT | |
2806 + 6 + 1 + 1; | |
Hannes Payer (out of office)
2013/10/21 09:57:22
Same here.
| |
2807 EmbeddedVector<char, kBufferSize> buffer; | |
2808 HashMap* id_to_function_info = tracker->id_to_function_info(); | |
2809 bool first_entry = true; | |
2810 for (HashMap::Entry* p = id_to_function_info->Start(); | |
2811 p != NULL; | |
2812 p = id_to_function_info->Next(p)) { | |
2813 SnapshotObjectId id = | |
2814 static_cast<SnapshotObjectId>(reinterpret_cast<intptr_t>(p->key)); | |
2815 AllocationTracker::FunctionInfo* info = | |
2816 reinterpret_cast<AllocationTracker::FunctionInfo* >(p->value); | |
2817 int buffer_pos = 0; | |
2818 if (first_entry) { | |
2819 first_entry = false; | |
2820 } else { | |
2821 buffer[buffer_pos++] = ','; | |
2822 } | |
2823 buffer_pos = utoa(id, buffer, buffer_pos); | |
2824 buffer[buffer_pos++] = ','; | |
2825 buffer_pos = utoa(GetStringId(info->name), buffer, buffer_pos); | |
2826 buffer[buffer_pos++] = ','; | |
2827 buffer_pos = utoa(GetStringId(info->script_name), buffer, buffer_pos); | |
2828 buffer[buffer_pos++] = ','; | |
2829 // The cast is safe because script id is a non-negative Smi. | |
2830 buffer_pos = utoa(static_cast<unsigned>(info->script_id), buffer, | |
2831 buffer_pos); | |
2832 buffer[buffer_pos++] = ','; | |
2833 buffer_pos = SerializePosition(info->line, buffer, buffer_pos); | |
2834 buffer[buffer_pos++] = ','; | |
2835 buffer_pos = SerializePosition(info->column, buffer, buffer_pos); | |
2836 buffer[buffer_pos++] = '\n'; | |
2837 buffer[buffer_pos++] = '\0'; | |
2838 writer_->AddString(buffer.start()); | |
2839 } | |
2840 } | |
2841 | |
2842 | |
2686 void HeapSnapshotJSONSerializer::SerializeString(const unsigned char* s) { | 2843 void HeapSnapshotJSONSerializer::SerializeString(const unsigned char* s) { |
2687 writer_->AddCharacter('\n'); | 2844 writer_->AddCharacter('\n'); |
2688 writer_->AddCharacter('\"'); | 2845 writer_->AddCharacter('\"'); |
2689 for ( ; *s != '\0'; ++s) { | 2846 for ( ; *s != '\0'; ++s) { |
2690 switch (*s) { | 2847 switch (*s) { |
2691 case '\b': | 2848 case '\b': |
2692 writer_->AddString("\\b"); | 2849 writer_->AddString("\\b"); |
2693 continue; | 2850 continue; |
2694 case '\f': | 2851 case '\f': |
2695 writer_->AddString("\\f"); | 2852 writer_->AddString("\\f"); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2745 writer_->AddString("\"<dummy>\""); | 2902 writer_->AddString("\"<dummy>\""); |
2746 for (int i = 1; i < sorted_strings.length(); ++i) { | 2903 for (int i = 1; i < sorted_strings.length(); ++i) { |
2747 writer_->AddCharacter(','); | 2904 writer_->AddCharacter(','); |
2748 SerializeString(sorted_strings[i]); | 2905 SerializeString(sorted_strings[i]); |
2749 if (writer_->aborted()) return; | 2906 if (writer_->aborted()) return; |
2750 } | 2907 } |
2751 } | 2908 } |
2752 | 2909 |
2753 | 2910 |
2754 } } // namespace v8::internal | 2911 } } // namespace v8::internal |
OLD | NEW |