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 "code-stubs.h" | 32 #include "code-stubs.h" |
34 #include "heap-profiler.h" | 33 #include "heap-profiler.h" |
35 #include "debug.h" | 34 #include "debug.h" |
36 #include "types.h" | 35 #include "types.h" |
37 | 36 |
38 namespace v8 { | 37 namespace v8 { |
39 namespace internal { | 38 namespace internal { |
40 | 39 |
41 | 40 |
42 HeapGraphEdge::HeapGraphEdge(Type type, const char* name, int from, int to) | 41 HeapGraphEdge::HeapGraphEdge(Type type, const char* name, int from, int to) |
(...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
742 sizeof(*this) + | 741 sizeof(*this) + |
743 sizeof(HashMap::Entry) * entries_map_.capacity() + | 742 sizeof(HashMap::Entry) * entries_map_.capacity() + |
744 GetMemoryUsedByList(entries_) + | 743 GetMemoryUsedByList(entries_) + |
745 GetMemoryUsedByList(time_intervals_); | 744 GetMemoryUsedByList(time_intervals_); |
746 } | 745 } |
747 | 746 |
748 | 747 |
749 HeapSnapshotsCollection::HeapSnapshotsCollection(Heap* heap) | 748 HeapSnapshotsCollection::HeapSnapshotsCollection(Heap* heap) |
750 : is_tracking_objects_(false), | 749 : is_tracking_objects_(false), |
751 names_(heap), | 750 names_(heap), |
752 ids_(heap), | 751 ids_(heap) { |
753 allocation_tracker_(NULL) { | |
754 } | 752 } |
755 | 753 |
756 | 754 |
757 static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) { | 755 static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) { |
758 delete *snapshot_ptr; | 756 delete *snapshot_ptr; |
759 } | 757 } |
760 | 758 |
761 | 759 |
762 HeapSnapshotsCollection::~HeapSnapshotsCollection() { | 760 HeapSnapshotsCollection::~HeapSnapshotsCollection() { |
763 delete allocation_tracker_; | |
764 snapshots_.Iterate(DeleteHeapSnapshot); | 761 snapshots_.Iterate(DeleteHeapSnapshot); |
765 } | 762 } |
766 | 763 |
767 | 764 |
768 void HeapSnapshotsCollection::StartHeapObjectsTracking() { | |
769 ids_.UpdateHeapObjectsMap(); | |
770 if (allocation_tracker_ == NULL) { | |
771 allocation_tracker_ = new AllocationTracker(&ids_, names()); | |
772 } | |
773 is_tracking_objects_ = true; | |
774 } | |
775 | |
776 | |
777 void HeapSnapshotsCollection::StopHeapObjectsTracking() { | |
778 ids_.StopHeapObjectsTracking(); | |
779 if (allocation_tracker_ != NULL) { | |
780 delete allocation_tracker_; | |
781 allocation_tracker_ = NULL; | |
782 } | |
783 } | |
784 | |
785 | |
786 HeapSnapshot* HeapSnapshotsCollection::NewSnapshot(const char* name, | 765 HeapSnapshot* HeapSnapshotsCollection::NewSnapshot(const char* name, |
787 unsigned uid) { | 766 unsigned uid) { |
788 is_tracking_objects_ = true; // Start watching for heap objects moves. | 767 is_tracking_objects_ = true; // Start watching for heap objects moves. |
789 return new HeapSnapshot(this, name, uid); | 768 return new HeapSnapshot(this, name, uid); |
790 } | 769 } |
791 | 770 |
792 | 771 |
793 void HeapSnapshotsCollection::SnapshotGenerationFinished( | 772 void HeapSnapshotsCollection::SnapshotGenerationFinished( |
794 HeapSnapshot* snapshot) { | 773 HeapSnapshot* snapshot) { |
795 ids_.SnapshotGenerationFinished(); | 774 ids_.SnapshotGenerationFinished(); |
(...skipping 23 matching lines...) Expand all Loading... |
819 if (ids_.FindEntry(obj->address()) == id) { | 798 if (ids_.FindEntry(obj->address()) == id) { |
820 ASSERT(object == NULL); | 799 ASSERT(object == NULL); |
821 object = obj; | 800 object = obj; |
822 // Can't break -- kFilterUnreachable requires full heap traversal. | 801 // Can't break -- kFilterUnreachable requires full heap traversal. |
823 } | 802 } |
824 } | 803 } |
825 return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>(); | 804 return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>(); |
826 } | 805 } |
827 | 806 |
828 | 807 |
829 void HeapSnapshotsCollection::NewObjectEvent(Address addr, int size) { | |
830 DisallowHeapAllocation no_allocation; | |
831 ids_.NewObject(addr, size); | |
832 if (allocation_tracker_ != NULL) { | |
833 allocation_tracker_->NewObjectEvent(addr, size); | |
834 } | |
835 } | |
836 | |
837 | |
838 size_t HeapSnapshotsCollection::GetUsedMemorySize() const { | 808 size_t HeapSnapshotsCollection::GetUsedMemorySize() const { |
839 size_t size = sizeof(*this); | 809 size_t size = sizeof(*this); |
840 size += names_.GetUsedMemorySize(); | 810 size += names_.GetUsedMemorySize(); |
841 size += ids_.GetUsedMemorySize(); | 811 size += ids_.GetUsedMemorySize(); |
842 size += GetMemoryUsedByList(snapshots_); | 812 size += GetMemoryUsedByList(snapshots_); |
843 for (int i = 0; i < snapshots_.length(); ++i) { | 813 for (int i = 0; i < snapshots_.length(); ++i) { |
844 size += snapshots_[i]->RawSnapshotSize(); | 814 size += snapshots_[i]->RawSnapshotSize(); |
845 } | 815 } |
846 return size; | 816 return size; |
847 } | 817 } |
(...skipping 1820 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2668 bool aborted_; | 2638 bool aborted_; |
2669 }; | 2639 }; |
2670 | 2640 |
2671 | 2641 |
2672 // type, name|index, to_node. | 2642 // type, name|index, to_node. |
2673 const int HeapSnapshotJSONSerializer::kEdgeFieldsCount = 3; | 2643 const int HeapSnapshotJSONSerializer::kEdgeFieldsCount = 3; |
2674 // type, name, id, self_size, children_index. | 2644 // type, name, id, self_size, children_index. |
2675 const int HeapSnapshotJSONSerializer::kNodeFieldsCount = 5; | 2645 const int HeapSnapshotJSONSerializer::kNodeFieldsCount = 5; |
2676 | 2646 |
2677 void HeapSnapshotJSONSerializer::Serialize(v8::OutputStream* stream) { | 2647 void HeapSnapshotJSONSerializer::Serialize(v8::OutputStream* stream) { |
2678 if (AllocationTracker* allocation_tracker = | |
2679 snapshot_->collection()->allocation_tracker()) { | |
2680 allocation_tracker->PrepareForSerialization(); | |
2681 } | |
2682 ASSERT(writer_ == NULL); | 2648 ASSERT(writer_ == NULL); |
2683 writer_ = new OutputStreamWriter(stream); | 2649 writer_ = new OutputStreamWriter(stream); |
2684 SerializeImpl(); | 2650 SerializeImpl(); |
2685 delete writer_; | 2651 delete writer_; |
2686 writer_ = NULL; | 2652 writer_ = NULL; |
2687 } | 2653 } |
2688 | 2654 |
2689 | 2655 |
2690 void HeapSnapshotJSONSerializer::SerializeImpl() { | 2656 void HeapSnapshotJSONSerializer::SerializeImpl() { |
2691 ASSERT(0 == snapshot_->root()->index()); | 2657 ASSERT(0 == snapshot_->root()->index()); |
2692 writer_->AddCharacter('{'); | 2658 writer_->AddCharacter('{'); |
2693 writer_->AddString("\"snapshot\":{"); | 2659 writer_->AddString("\"snapshot\":{"); |
2694 SerializeSnapshot(); | 2660 SerializeSnapshot(); |
2695 if (writer_->aborted()) return; | 2661 if (writer_->aborted()) return; |
2696 writer_->AddString("},\n"); | 2662 writer_->AddString("},\n"); |
2697 writer_->AddString("\"nodes\":["); | 2663 writer_->AddString("\"nodes\":["); |
2698 SerializeNodes(); | 2664 SerializeNodes(); |
2699 if (writer_->aborted()) return; | 2665 if (writer_->aborted()) return; |
2700 writer_->AddString("],\n"); | 2666 writer_->AddString("],\n"); |
2701 writer_->AddString("\"edges\":["); | 2667 writer_->AddString("\"edges\":["); |
2702 SerializeEdges(); | 2668 SerializeEdges(); |
2703 if (writer_->aborted()) return; | 2669 if (writer_->aborted()) return; |
2704 writer_->AddString("],\n"); | 2670 writer_->AddString("],\n"); |
2705 | |
2706 writer_->AddString("\"trace_function_infos\":["); | |
2707 SerializeTraceNodeInfos(); | |
2708 if (writer_->aborted()) return; | |
2709 writer_->AddString("],\n"); | |
2710 writer_->AddString("\"trace_tree\":["); | |
2711 SerializeTraceTree(); | |
2712 if (writer_->aborted()) return; | |
2713 writer_->AddString("],\n"); | |
2714 | |
2715 writer_->AddString("\"strings\":["); | 2671 writer_->AddString("\"strings\":["); |
2716 SerializeStrings(); | 2672 SerializeStrings(); |
2717 if (writer_->aborted()) return; | 2673 if (writer_->aborted()) return; |
2718 writer_->AddCharacter(']'); | 2674 writer_->AddCharacter(']'); |
2719 writer_->AddCharacter('}'); | 2675 writer_->AddCharacter('}'); |
2720 writer_->Finalize(); | 2676 writer_->Finalize(); |
2721 } | 2677 } |
2722 | 2678 |
2723 | 2679 |
2724 int HeapSnapshotJSONSerializer::GetStringId(const char* s) { | 2680 int HeapSnapshotJSONSerializer::GetStringId(const char* s) { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2865 JSON_S("edge_types") ":" JSON_A( | 2821 JSON_S("edge_types") ":" JSON_A( |
2866 JSON_A( | 2822 JSON_A( |
2867 JSON_S("context") "," | 2823 JSON_S("context") "," |
2868 JSON_S("element") "," | 2824 JSON_S("element") "," |
2869 JSON_S("property") "," | 2825 JSON_S("property") "," |
2870 JSON_S("internal") "," | 2826 JSON_S("internal") "," |
2871 JSON_S("hidden") "," | 2827 JSON_S("hidden") "," |
2872 JSON_S("shortcut") "," | 2828 JSON_S("shortcut") "," |
2873 JSON_S("weak")) "," | 2829 JSON_S("weak")) "," |
2874 JSON_S("string_or_number") "," | 2830 JSON_S("string_or_number") "," |
2875 JSON_S("node")) "," | 2831 JSON_S("node")))); |
2876 JSON_S("trace_function_info_fields") ":" JSON_A( | |
2877 JSON_S("function_id") "," | |
2878 JSON_S("name") "," | |
2879 JSON_S("script_name") "," | |
2880 JSON_S("script_id") "," | |
2881 JSON_S("line") "," | |
2882 JSON_S("column")) "," | |
2883 JSON_S("trace_node_fields") ":" JSON_A( | |
2884 JSON_S("id") "," | |
2885 JSON_S("function_id") "," | |
2886 JSON_S("count") "," | |
2887 JSON_S("size") "," | |
2888 JSON_S("children")))); | |
2889 #undef JSON_S | 2832 #undef JSON_S |
2890 #undef JSON_O | 2833 #undef JSON_O |
2891 #undef JSON_A | 2834 #undef JSON_A |
2892 writer_->AddString(",\"node_count\":"); | 2835 writer_->AddString(",\"node_count\":"); |
2893 writer_->AddNumber(snapshot_->entries().length()); | 2836 writer_->AddNumber(snapshot_->entries().length()); |
2894 writer_->AddString(",\"edge_count\":"); | 2837 writer_->AddString(",\"edge_count\":"); |
2895 writer_->AddNumber(snapshot_->edges().length()); | 2838 writer_->AddNumber(snapshot_->edges().length()); |
2896 writer_->AddString(",\"trace_function_count\":"); | |
2897 uint32_t count = 0; | |
2898 AllocationTracker* tracker = snapshot_->collection()->allocation_tracker(); | |
2899 if (tracker) { | |
2900 count = tracker->id_to_function_info()->occupancy(); | |
2901 } | |
2902 writer_->AddNumber(count); | |
2903 } | 2839 } |
2904 | 2840 |
2905 | 2841 |
2906 static void WriteUChar(OutputStreamWriter* w, unibrow::uchar u) { | 2842 static void WriteUChar(OutputStreamWriter* w, unibrow::uchar u) { |
2907 static const char hex_chars[] = "0123456789ABCDEF"; | 2843 static const char hex_chars[] = "0123456789ABCDEF"; |
2908 w->AddString("\\u"); | 2844 w->AddString("\\u"); |
2909 w->AddCharacter(hex_chars[(u >> 12) & 0xf]); | 2845 w->AddCharacter(hex_chars[(u >> 12) & 0xf]); |
2910 w->AddCharacter(hex_chars[(u >> 8) & 0xf]); | 2846 w->AddCharacter(hex_chars[(u >> 8) & 0xf]); |
2911 w->AddCharacter(hex_chars[(u >> 4) & 0xf]); | 2847 w->AddCharacter(hex_chars[(u >> 4) & 0xf]); |
2912 w->AddCharacter(hex_chars[u & 0xf]); | 2848 w->AddCharacter(hex_chars[u & 0xf]); |
2913 } | 2849 } |
2914 | 2850 |
2915 | 2851 |
2916 void HeapSnapshotJSONSerializer::SerializeTraceTree() { | |
2917 AllocationTracker* tracker = snapshot_->collection()->allocation_tracker(); | |
2918 if (!tracker) return; | |
2919 AllocationTraceTree* traces = tracker->trace_tree(); | |
2920 SerializeTraceNode(traces->root()); | |
2921 } | |
2922 | |
2923 | |
2924 void HeapSnapshotJSONSerializer::SerializeTraceNode(AllocationTraceNode* node) { | |
2925 // The buffer needs space for 4 unsigned ints, 4 commas, [ and \0 | |
2926 const int kBufferSize = | |
2927 4 * MaxDecimalDigitsIn<sizeof(unsigned)>::kUnsigned // NOLINT | |
2928 + 4 + 1 + 1; | |
2929 EmbeddedVector<char, kBufferSize> buffer; | |
2930 int buffer_pos = 0; | |
2931 buffer_pos = utoa(node->id(), buffer, buffer_pos); | |
2932 buffer[buffer_pos++] = ','; | |
2933 buffer_pos = utoa(node->function_id(), buffer, buffer_pos); | |
2934 buffer[buffer_pos++] = ','; | |
2935 buffer_pos = utoa(node->allocation_count(), buffer, buffer_pos); | |
2936 buffer[buffer_pos++] = ','; | |
2937 buffer_pos = utoa(node->allocation_size(), buffer, buffer_pos); | |
2938 buffer[buffer_pos++] = ','; | |
2939 buffer[buffer_pos++] = '['; | |
2940 buffer[buffer_pos++] = '\0'; | |
2941 writer_->AddString(buffer.start()); | |
2942 | |
2943 Vector<AllocationTraceNode*> children = node->children(); | |
2944 for (int i = 0; i < children.length(); i++) { | |
2945 if (i > 0) { | |
2946 writer_->AddCharacter(','); | |
2947 } | |
2948 SerializeTraceNode(children[i]); | |
2949 } | |
2950 writer_->AddCharacter(']'); | |
2951 } | |
2952 | |
2953 | |
2954 // 0-based position is converted to 1-based during the serialization. | |
2955 static int SerializePosition(int position, const Vector<char>& buffer, | |
2956 int buffer_pos) { | |
2957 if (position == -1) { | |
2958 buffer[buffer_pos++] = '0'; | |
2959 } else { | |
2960 ASSERT(position >= 0); | |
2961 buffer_pos = utoa(static_cast<unsigned>(position + 1), buffer, buffer_pos); | |
2962 } | |
2963 return buffer_pos; | |
2964 } | |
2965 | |
2966 | |
2967 void HeapSnapshotJSONSerializer::SerializeTraceNodeInfos() { | |
2968 AllocationTracker* tracker = snapshot_->collection()->allocation_tracker(); | |
2969 if (!tracker) return; | |
2970 // The buffer needs space for 6 unsigned ints, 6 commas, \n and \0 | |
2971 const int kBufferSize = | |
2972 6 * MaxDecimalDigitsIn<sizeof(unsigned)>::kUnsigned // NOLINT | |
2973 + 6 + 1 + 1; | |
2974 EmbeddedVector<char, kBufferSize> buffer; | |
2975 HashMap* id_to_function_info = tracker->id_to_function_info(); | |
2976 bool first_entry = true; | |
2977 for (HashMap::Entry* p = id_to_function_info->Start(); | |
2978 p != NULL; | |
2979 p = id_to_function_info->Next(p)) { | |
2980 SnapshotObjectId id = | |
2981 static_cast<SnapshotObjectId>(reinterpret_cast<intptr_t>(p->key)); | |
2982 AllocationTracker::FunctionInfo* info = | |
2983 reinterpret_cast<AllocationTracker::FunctionInfo* >(p->value); | |
2984 int buffer_pos = 0; | |
2985 if (first_entry) { | |
2986 first_entry = false; | |
2987 } else { | |
2988 buffer[buffer_pos++] = ','; | |
2989 } | |
2990 buffer_pos = utoa(id, buffer, buffer_pos); | |
2991 buffer[buffer_pos++] = ','; | |
2992 buffer_pos = utoa(GetStringId(info->name), buffer, buffer_pos); | |
2993 buffer[buffer_pos++] = ','; | |
2994 buffer_pos = utoa(GetStringId(info->script_name), buffer, buffer_pos); | |
2995 buffer[buffer_pos++] = ','; | |
2996 // The cast is safe because script id is a non-negative Smi. | |
2997 buffer_pos = utoa(static_cast<unsigned>(info->script_id), buffer, | |
2998 buffer_pos); | |
2999 buffer[buffer_pos++] = ','; | |
3000 buffer_pos = SerializePosition(info->line, buffer, buffer_pos); | |
3001 buffer[buffer_pos++] = ','; | |
3002 buffer_pos = SerializePosition(info->column, buffer, buffer_pos); | |
3003 buffer[buffer_pos++] = '\n'; | |
3004 buffer[buffer_pos++] = '\0'; | |
3005 writer_->AddString(buffer.start()); | |
3006 } | |
3007 } | |
3008 | |
3009 | |
3010 void HeapSnapshotJSONSerializer::SerializeString(const unsigned char* s) { | 2852 void HeapSnapshotJSONSerializer::SerializeString(const unsigned char* s) { |
3011 writer_->AddCharacter('\n'); | 2853 writer_->AddCharacter('\n'); |
3012 writer_->AddCharacter('\"'); | 2854 writer_->AddCharacter('\"'); |
3013 for ( ; *s != '\0'; ++s) { | 2855 for ( ; *s != '\0'; ++s) { |
3014 switch (*s) { | 2856 switch (*s) { |
3015 case '\b': | 2857 case '\b': |
3016 writer_->AddString("\\b"); | 2858 writer_->AddString("\\b"); |
3017 continue; | 2859 continue; |
3018 case '\f': | 2860 case '\f': |
3019 writer_->AddString("\\f"); | 2861 writer_->AddString("\\f"); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3069 writer_->AddString("\"<dummy>\""); | 2911 writer_->AddString("\"<dummy>\""); |
3070 for (int i = 1; i < sorted_strings.length(); ++i) { | 2912 for (int i = 1; i < sorted_strings.length(); ++i) { |
3071 writer_->AddCharacter(','); | 2913 writer_->AddCharacter(','); |
3072 SerializeString(sorted_strings[i]); | 2914 SerializeString(sorted_strings[i]); |
3073 if (writer_->aborted()) return; | 2915 if (writer_->aborted()) return; |
3074 } | 2916 } |
3075 } | 2917 } |
3076 | 2918 |
3077 | 2919 |
3078 } } // namespace v8::internal | 2920 } } // namespace v8::internal |
OLD | NEW |