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" | |
33 #include "heap-profiler.h" | 32 #include "heap-profiler.h" |
34 #include "debug.h" | 33 #include "debug.h" |
35 #include "types.h" | 34 #include "types.h" |
36 | 35 |
37 namespace v8 { | 36 namespace v8 { |
38 namespace internal { | 37 namespace internal { |
39 | 38 |
40 | 39 |
41 HeapGraphEdge::HeapGraphEdge(Type type, const char* name, int from, int to) | 40 HeapGraphEdge::HeapGraphEdge(Type type, const char* name, int from, int to) |
42 : type_(type), | 41 : type_(type), |
(...skipping 698 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
741 sizeof(*this) + | 740 sizeof(*this) + |
742 sizeof(HashMap::Entry) * entries_map_.capacity() + | 741 sizeof(HashMap::Entry) * entries_map_.capacity() + |
743 GetMemoryUsedByList(entries_) + | 742 GetMemoryUsedByList(entries_) + |
744 GetMemoryUsedByList(time_intervals_); | 743 GetMemoryUsedByList(time_intervals_); |
745 } | 744 } |
746 | 745 |
747 | 746 |
748 HeapSnapshotsCollection::HeapSnapshotsCollection(Heap* heap) | 747 HeapSnapshotsCollection::HeapSnapshotsCollection(Heap* heap) |
749 : is_tracking_objects_(false), | 748 : is_tracking_objects_(false), |
750 names_(heap), | 749 names_(heap), |
751 ids_(heap), | 750 ids_(heap) { |
752 allocation_tracker_(NULL) { | |
753 } | 751 } |
754 | 752 |
755 | 753 |
756 static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) { | 754 static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) { |
757 delete *snapshot_ptr; | 755 delete *snapshot_ptr; |
758 } | 756 } |
759 | 757 |
760 | 758 |
761 HeapSnapshotsCollection::~HeapSnapshotsCollection() { | 759 HeapSnapshotsCollection::~HeapSnapshotsCollection() { |
762 delete allocation_tracker_; | |
763 snapshots_.Iterate(DeleteHeapSnapshot); | 760 snapshots_.Iterate(DeleteHeapSnapshot); |
764 } | 761 } |
765 | 762 |
766 | 763 |
767 void HeapSnapshotsCollection::StartHeapObjectsTracking() { | |
768 ids_.UpdateHeapObjectsMap(); | |
769 if (allocation_tracker_ == NULL) { | |
770 allocation_tracker_ = new AllocationTracker(&ids_, names()); | |
771 } | |
772 is_tracking_objects_ = true; | |
773 } | |
774 | |
775 | |
776 void HeapSnapshotsCollection::StopHeapObjectsTracking() { | |
777 ids_.StopHeapObjectsTracking(); | |
778 if (allocation_tracker_ != NULL) { | |
779 delete allocation_tracker_; | |
780 allocation_tracker_ = NULL; | |
781 } | |
782 } | |
783 | |
784 | |
785 HeapSnapshot* HeapSnapshotsCollection::NewSnapshot(const char* name, | 764 HeapSnapshot* HeapSnapshotsCollection::NewSnapshot(const char* name, |
786 unsigned uid) { | 765 unsigned uid) { |
787 is_tracking_objects_ = true; // Start watching for heap objects moves. | 766 is_tracking_objects_ = true; // Start watching for heap objects moves. |
788 return new HeapSnapshot(this, name, uid); | 767 return new HeapSnapshot(this, name, uid); |
789 } | 768 } |
790 | 769 |
791 | 770 |
792 void HeapSnapshotsCollection::SnapshotGenerationFinished( | 771 void HeapSnapshotsCollection::SnapshotGenerationFinished( |
793 HeapSnapshot* snapshot) { | 772 HeapSnapshot* snapshot) { |
794 ids_.SnapshotGenerationFinished(); | 773 ids_.SnapshotGenerationFinished(); |
(...skipping 23 matching lines...) Expand all Loading... |
818 if (ids_.FindEntry(obj->address()) == id) { | 797 if (ids_.FindEntry(obj->address()) == id) { |
819 ASSERT(object == NULL); | 798 ASSERT(object == NULL); |
820 object = obj; | 799 object = obj; |
821 // Can't break -- kFilterUnreachable requires full heap traversal. | 800 // Can't break -- kFilterUnreachable requires full heap traversal. |
822 } | 801 } |
823 } | 802 } |
824 return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>(); | 803 return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>(); |
825 } | 804 } |
826 | 805 |
827 | 806 |
828 void HeapSnapshotsCollection::NewObjectEvent(Address addr, int size) { | |
829 DisallowHeapAllocation no_allocation; | |
830 ids_.NewObject(addr, size); | |
831 if (allocation_tracker_ != NULL) { | |
832 allocation_tracker_->NewObjectEvent(addr, size); | |
833 } | |
834 } | |
835 | |
836 | |
837 size_t HeapSnapshotsCollection::GetUsedMemorySize() const { | 807 size_t HeapSnapshotsCollection::GetUsedMemorySize() const { |
838 size_t size = sizeof(*this); | 808 size_t size = sizeof(*this); |
839 size += names_.GetUsedMemorySize(); | 809 size += names_.GetUsedMemorySize(); |
840 size += ids_.GetUsedMemorySize(); | 810 size += ids_.GetUsedMemorySize(); |
841 size += GetMemoryUsedByList(snapshots_); | 811 size += GetMemoryUsedByList(snapshots_); |
842 for (int i = 0; i < snapshots_.length(); ++i) { | 812 for (int i = 0; i < snapshots_.length(); ++i) { |
843 size += snapshots_[i]->RawSnapshotSize(); | 813 size += snapshots_[i]->RawSnapshotSize(); |
844 } | 814 } |
845 return size; | 815 return size; |
846 } | 816 } |
(...skipping 1778 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2625 bool aborted_; | 2595 bool aborted_; |
2626 }; | 2596 }; |
2627 | 2597 |
2628 | 2598 |
2629 // type, name|index, to_node. | 2599 // type, name|index, to_node. |
2630 const int HeapSnapshotJSONSerializer::kEdgeFieldsCount = 3; | 2600 const int HeapSnapshotJSONSerializer::kEdgeFieldsCount = 3; |
2631 // type, name, id, self_size, children_index. | 2601 // type, name, id, self_size, children_index. |
2632 const int HeapSnapshotJSONSerializer::kNodeFieldsCount = 5; | 2602 const int HeapSnapshotJSONSerializer::kNodeFieldsCount = 5; |
2633 | 2603 |
2634 void HeapSnapshotJSONSerializer::Serialize(v8::OutputStream* stream) { | 2604 void HeapSnapshotJSONSerializer::Serialize(v8::OutputStream* stream) { |
2635 if (AllocationTracker* allocation_tracker = | |
2636 snapshot_->collection()->allocation_tracker()) { | |
2637 allocation_tracker->PrepareForSerialization(); | |
2638 } | |
2639 ASSERT(writer_ == NULL); | 2605 ASSERT(writer_ == NULL); |
2640 writer_ = new OutputStreamWriter(stream); | 2606 writer_ = new OutputStreamWriter(stream); |
2641 SerializeImpl(); | 2607 SerializeImpl(); |
2642 delete writer_; | 2608 delete writer_; |
2643 writer_ = NULL; | 2609 writer_ = NULL; |
2644 } | 2610 } |
2645 | 2611 |
2646 | 2612 |
2647 void HeapSnapshotJSONSerializer::SerializeImpl() { | 2613 void HeapSnapshotJSONSerializer::SerializeImpl() { |
2648 ASSERT(0 == snapshot_->root()->index()); | 2614 ASSERT(0 == snapshot_->root()->index()); |
2649 writer_->AddCharacter('{'); | 2615 writer_->AddCharacter('{'); |
2650 writer_->AddString("\"snapshot\":{"); | 2616 writer_->AddString("\"snapshot\":{"); |
2651 SerializeSnapshot(); | 2617 SerializeSnapshot(); |
2652 if (writer_->aborted()) return; | 2618 if (writer_->aborted()) return; |
2653 writer_->AddString("},\n"); | 2619 writer_->AddString("},\n"); |
2654 writer_->AddString("\"nodes\":["); | 2620 writer_->AddString("\"nodes\":["); |
2655 SerializeNodes(); | 2621 SerializeNodes(); |
2656 if (writer_->aborted()) return; | 2622 if (writer_->aborted()) return; |
2657 writer_->AddString("],\n"); | 2623 writer_->AddString("],\n"); |
2658 writer_->AddString("\"edges\":["); | 2624 writer_->AddString("\"edges\":["); |
2659 SerializeEdges(); | 2625 SerializeEdges(); |
2660 if (writer_->aborted()) return; | 2626 if (writer_->aborted()) return; |
2661 writer_->AddString("],\n"); | 2627 writer_->AddString("],\n"); |
2662 | |
2663 writer_->AddString("\"trace_function_infos\":["); | |
2664 SerializeTraceNodeInfos(); | |
2665 if (writer_->aborted()) return; | |
2666 writer_->AddString("],\n"); | |
2667 writer_->AddString("\"trace_tree\":["); | |
2668 SerializeTraceTree(); | |
2669 if (writer_->aborted()) return; | |
2670 writer_->AddString("],\n"); | |
2671 | |
2672 writer_->AddString("\"strings\":["); | 2628 writer_->AddString("\"strings\":["); |
2673 SerializeStrings(); | 2629 SerializeStrings(); |
2674 if (writer_->aborted()) return; | 2630 if (writer_->aborted()) return; |
2675 writer_->AddCharacter(']'); | 2631 writer_->AddCharacter(']'); |
2676 writer_->AddCharacter('}'); | 2632 writer_->AddCharacter('}'); |
2677 writer_->Finalize(); | 2633 writer_->Finalize(); |
2678 } | 2634 } |
2679 | 2635 |
2680 | 2636 |
2681 int HeapSnapshotJSONSerializer::GetStringId(const char* s) { | 2637 int HeapSnapshotJSONSerializer::GetStringId(const char* s) { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2822 JSON_S("edge_types") ":" JSON_A( | 2778 JSON_S("edge_types") ":" JSON_A( |
2823 JSON_A( | 2779 JSON_A( |
2824 JSON_S("context") "," | 2780 JSON_S("context") "," |
2825 JSON_S("element") "," | 2781 JSON_S("element") "," |
2826 JSON_S("property") "," | 2782 JSON_S("property") "," |
2827 JSON_S("internal") "," | 2783 JSON_S("internal") "," |
2828 JSON_S("hidden") "," | 2784 JSON_S("hidden") "," |
2829 JSON_S("shortcut") "," | 2785 JSON_S("shortcut") "," |
2830 JSON_S("weak")) "," | 2786 JSON_S("weak")) "," |
2831 JSON_S("string_or_number") "," | 2787 JSON_S("string_or_number") "," |
2832 JSON_S("node")) "," | 2788 JSON_S("node")))); |
2833 JSON_S("trace_function_info_fields") ":" JSON_A( | |
2834 JSON_S("function_id") "," | |
2835 JSON_S("name") "," | |
2836 JSON_S("script_name") "," | |
2837 JSON_S("script_id") "," | |
2838 JSON_S("line") "," | |
2839 JSON_S("column")) "," | |
2840 JSON_S("trace_node_fields") ":" JSON_A( | |
2841 JSON_S("id") "," | |
2842 JSON_S("function_id") "," | |
2843 JSON_S("count") "," | |
2844 JSON_S("size") "," | |
2845 JSON_S("children")))); | |
2846 #undef JSON_S | 2789 #undef JSON_S |
2847 #undef JSON_O | 2790 #undef JSON_O |
2848 #undef JSON_A | 2791 #undef JSON_A |
2849 writer_->AddString(",\"node_count\":"); | 2792 writer_->AddString(",\"node_count\":"); |
2850 writer_->AddNumber(snapshot_->entries().length()); | 2793 writer_->AddNumber(snapshot_->entries().length()); |
2851 writer_->AddString(",\"edge_count\":"); | 2794 writer_->AddString(",\"edge_count\":"); |
2852 writer_->AddNumber(snapshot_->edges().length()); | 2795 writer_->AddNumber(snapshot_->edges().length()); |
2853 writer_->AddString(",\"trace_function_count\":"); | |
2854 uint32_t count = 0; | |
2855 AllocationTracker* tracker = snapshot_->collection()->allocation_tracker(); | |
2856 if (tracker) { | |
2857 count = tracker->id_to_function_info()->occupancy(); | |
2858 } | |
2859 writer_->AddNumber(count); | |
2860 } | 2796 } |
2861 | 2797 |
2862 | 2798 |
2863 static void WriteUChar(OutputStreamWriter* w, unibrow::uchar u) { | 2799 static void WriteUChar(OutputStreamWriter* w, unibrow::uchar u) { |
2864 static const char hex_chars[] = "0123456789ABCDEF"; | 2800 static const char hex_chars[] = "0123456789ABCDEF"; |
2865 w->AddString("\\u"); | 2801 w->AddString("\\u"); |
2866 w->AddCharacter(hex_chars[(u >> 12) & 0xf]); | 2802 w->AddCharacter(hex_chars[(u >> 12) & 0xf]); |
2867 w->AddCharacter(hex_chars[(u >> 8) & 0xf]); | 2803 w->AddCharacter(hex_chars[(u >> 8) & 0xf]); |
2868 w->AddCharacter(hex_chars[(u >> 4) & 0xf]); | 2804 w->AddCharacter(hex_chars[(u >> 4) & 0xf]); |
2869 w->AddCharacter(hex_chars[u & 0xf]); | 2805 w->AddCharacter(hex_chars[u & 0xf]); |
2870 } | 2806 } |
2871 | 2807 |
2872 | 2808 |
2873 void HeapSnapshotJSONSerializer::SerializeTraceTree() { | |
2874 AllocationTracker* tracker = snapshot_->collection()->allocation_tracker(); | |
2875 if (!tracker) return; | |
2876 AllocationTraceTree* traces = tracker->trace_tree(); | |
2877 SerializeTraceNode(traces->root()); | |
2878 } | |
2879 | |
2880 | |
2881 void HeapSnapshotJSONSerializer::SerializeTraceNode(AllocationTraceNode* node) { | |
2882 // The buffer needs space for 4 unsigned ints, 4 commas, [ and \0 | |
2883 const int kBufferSize = | |
2884 4 * MaxDecimalDigitsIn<sizeof(unsigned)>::kUnsigned // NOLINT | |
2885 + 4 + 1 + 1; | |
2886 EmbeddedVector<char, kBufferSize> buffer; | |
2887 int buffer_pos = 0; | |
2888 buffer_pos = utoa(node->id(), buffer, buffer_pos); | |
2889 buffer[buffer_pos++] = ','; | |
2890 buffer_pos = utoa(node->function_id(), buffer, buffer_pos); | |
2891 buffer[buffer_pos++] = ','; | |
2892 buffer_pos = utoa(node->allocation_count(), buffer, buffer_pos); | |
2893 buffer[buffer_pos++] = ','; | |
2894 buffer_pos = utoa(node->allocation_size(), buffer, buffer_pos); | |
2895 buffer[buffer_pos++] = ','; | |
2896 buffer[buffer_pos++] = '['; | |
2897 buffer[buffer_pos++] = '\0'; | |
2898 writer_->AddString(buffer.start()); | |
2899 | |
2900 Vector<AllocationTraceNode*> children = node->children(); | |
2901 for (int i = 0; i < children.length(); i++) { | |
2902 if (i > 0) { | |
2903 writer_->AddCharacter(','); | |
2904 } | |
2905 SerializeTraceNode(children[i]); | |
2906 } | |
2907 writer_->AddCharacter(']'); | |
2908 } | |
2909 | |
2910 | |
2911 // 0-based position is converted to 1-based during the serialization. | |
2912 static int SerializePosition(int position, const Vector<char>& buffer, | |
2913 int buffer_pos) { | |
2914 if (position == -1) { | |
2915 buffer[buffer_pos++] = '0'; | |
2916 } else { | |
2917 ASSERT(position >= 0); | |
2918 buffer_pos = utoa(static_cast<unsigned>(position + 1), buffer, buffer_pos); | |
2919 } | |
2920 return buffer_pos; | |
2921 } | |
2922 | |
2923 | |
2924 void HeapSnapshotJSONSerializer::SerializeTraceNodeInfos() { | |
2925 AllocationTracker* tracker = snapshot_->collection()->allocation_tracker(); | |
2926 if (!tracker) return; | |
2927 // The buffer needs space for 6 unsigned ints, 6 commas, \n and \0 | |
2928 const int kBufferSize = | |
2929 6 * MaxDecimalDigitsIn<sizeof(unsigned)>::kUnsigned // NOLINT | |
2930 + 6 + 1 + 1; | |
2931 EmbeddedVector<char, kBufferSize> buffer; | |
2932 HashMap* id_to_function_info = tracker->id_to_function_info(); | |
2933 bool first_entry = true; | |
2934 for (HashMap::Entry* p = id_to_function_info->Start(); | |
2935 p != NULL; | |
2936 p = id_to_function_info->Next(p)) { | |
2937 SnapshotObjectId id = | |
2938 static_cast<SnapshotObjectId>(reinterpret_cast<intptr_t>(p->key)); | |
2939 AllocationTracker::FunctionInfo* info = | |
2940 reinterpret_cast<AllocationTracker::FunctionInfo* >(p->value); | |
2941 int buffer_pos = 0; | |
2942 if (first_entry) { | |
2943 first_entry = false; | |
2944 } else { | |
2945 buffer[buffer_pos++] = ','; | |
2946 } | |
2947 buffer_pos = utoa(id, buffer, buffer_pos); | |
2948 buffer[buffer_pos++] = ','; | |
2949 buffer_pos = utoa(GetStringId(info->name), buffer, buffer_pos); | |
2950 buffer[buffer_pos++] = ','; | |
2951 buffer_pos = utoa(GetStringId(info->script_name), buffer, buffer_pos); | |
2952 buffer[buffer_pos++] = ','; | |
2953 // The cast is safe because script id is a non-negative Smi. | |
2954 buffer_pos = utoa(static_cast<unsigned>(info->script_id), buffer, | |
2955 buffer_pos); | |
2956 buffer[buffer_pos++] = ','; | |
2957 buffer_pos = SerializePosition(info->line, buffer, buffer_pos); | |
2958 buffer[buffer_pos++] = ','; | |
2959 buffer_pos = SerializePosition(info->column, buffer, buffer_pos); | |
2960 buffer[buffer_pos++] = '\n'; | |
2961 buffer[buffer_pos++] = '\0'; | |
2962 writer_->AddString(buffer.start()); | |
2963 } | |
2964 } | |
2965 | |
2966 | |
2967 void HeapSnapshotJSONSerializer::SerializeString(const unsigned char* s) { | 2809 void HeapSnapshotJSONSerializer::SerializeString(const unsigned char* s) { |
2968 writer_->AddCharacter('\n'); | 2810 writer_->AddCharacter('\n'); |
2969 writer_->AddCharacter('\"'); | 2811 writer_->AddCharacter('\"'); |
2970 for ( ; *s != '\0'; ++s) { | 2812 for ( ; *s != '\0'; ++s) { |
2971 switch (*s) { | 2813 switch (*s) { |
2972 case '\b': | 2814 case '\b': |
2973 writer_->AddString("\\b"); | 2815 writer_->AddString("\\b"); |
2974 continue; | 2816 continue; |
2975 case '\f': | 2817 case '\f': |
2976 writer_->AddString("\\f"); | 2818 writer_->AddString("\\f"); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3026 writer_->AddString("\"<dummy>\""); | 2868 writer_->AddString("\"<dummy>\""); |
3027 for (int i = 1; i < sorted_strings.length(); ++i) { | 2869 for (int i = 1; i < sorted_strings.length(); ++i) { |
3028 writer_->AddCharacter(','); | 2870 writer_->AddCharacter(','); |
3029 SerializeString(sorted_strings[i]); | 2871 SerializeString(sorted_strings[i]); |
3030 if (writer_->aborted()) return; | 2872 if (writer_->aborted()) return; |
3031 } | 2873 } |
3032 } | 2874 } |
3033 | 2875 |
3034 | 2876 |
3035 } } // namespace v8::internal | 2877 } } // namespace v8::internal |
OLD | NEW |