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 "code-stubs.h" | 33 #include "code-stubs.h" |
33 #include "heap-profiler.h" | 34 #include "heap-profiler.h" |
34 #include "debug.h" | 35 #include "debug.h" |
35 #include "types.h" | 36 #include "types.h" |
36 | 37 |
37 namespace v8 { | 38 namespace v8 { |
38 namespace internal { | 39 namespace internal { |
39 | 40 |
40 | 41 |
41 HeapGraphEdge::HeapGraphEdge(Type type, const char* name, int from, int to) | 42 HeapGraphEdge::HeapGraphEdge(Type type, const char* name, int from, int to) |
(...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
741 sizeof(*this) + | 742 sizeof(*this) + |
742 sizeof(HashMap::Entry) * entries_map_.capacity() + | 743 sizeof(HashMap::Entry) * entries_map_.capacity() + |
743 GetMemoryUsedByList(entries_) + | 744 GetMemoryUsedByList(entries_) + |
744 GetMemoryUsedByList(time_intervals_); | 745 GetMemoryUsedByList(time_intervals_); |
745 } | 746 } |
746 | 747 |
747 | 748 |
748 HeapSnapshotsCollection::HeapSnapshotsCollection(Heap* heap) | 749 HeapSnapshotsCollection::HeapSnapshotsCollection(Heap* heap) |
749 : is_tracking_objects_(false), | 750 : is_tracking_objects_(false), |
750 names_(heap), | 751 names_(heap), |
751 ids_(heap) { | 752 ids_(heap), |
| 753 allocation_tracker_(NULL) { |
752 } | 754 } |
753 | 755 |
754 | 756 |
755 static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) { | 757 static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) { |
756 delete *snapshot_ptr; | 758 delete *snapshot_ptr; |
757 } | 759 } |
758 | 760 |
759 | 761 |
760 HeapSnapshotsCollection::~HeapSnapshotsCollection() { | 762 HeapSnapshotsCollection::~HeapSnapshotsCollection() { |
| 763 delete allocation_tracker_; |
761 snapshots_.Iterate(DeleteHeapSnapshot); | 764 snapshots_.Iterate(DeleteHeapSnapshot); |
762 } | 765 } |
763 | 766 |
764 | 767 |
| 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 |
765 HeapSnapshot* HeapSnapshotsCollection::NewSnapshot(const char* name, | 786 HeapSnapshot* HeapSnapshotsCollection::NewSnapshot(const char* name, |
766 unsigned uid) { | 787 unsigned uid) { |
767 is_tracking_objects_ = true; // Start watching for heap objects moves. | 788 is_tracking_objects_ = true; // Start watching for heap objects moves. |
768 return new HeapSnapshot(this, name, uid); | 789 return new HeapSnapshot(this, name, uid); |
769 } | 790 } |
770 | 791 |
771 | 792 |
772 void HeapSnapshotsCollection::SnapshotGenerationFinished( | 793 void HeapSnapshotsCollection::SnapshotGenerationFinished( |
773 HeapSnapshot* snapshot) { | 794 HeapSnapshot* snapshot) { |
774 ids_.SnapshotGenerationFinished(); | 795 ids_.SnapshotGenerationFinished(); |
(...skipping 23 matching lines...) Expand all Loading... |
798 if (ids_.FindEntry(obj->address()) == id) { | 819 if (ids_.FindEntry(obj->address()) == id) { |
799 ASSERT(object == NULL); | 820 ASSERT(object == NULL); |
800 object = obj; | 821 object = obj; |
801 // Can't break -- kFilterUnreachable requires full heap traversal. | 822 // Can't break -- kFilterUnreachable requires full heap traversal. |
802 } | 823 } |
803 } | 824 } |
804 return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>(); | 825 return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>(); |
805 } | 826 } |
806 | 827 |
807 | 828 |
| 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 |
808 size_t HeapSnapshotsCollection::GetUsedMemorySize() const { | 838 size_t HeapSnapshotsCollection::GetUsedMemorySize() const { |
809 size_t size = sizeof(*this); | 839 size_t size = sizeof(*this); |
810 size += names_.GetUsedMemorySize(); | 840 size += names_.GetUsedMemorySize(); |
811 size += ids_.GetUsedMemorySize(); | 841 size += ids_.GetUsedMemorySize(); |
812 size += GetMemoryUsedByList(snapshots_); | 842 size += GetMemoryUsedByList(snapshots_); |
813 for (int i = 0; i < snapshots_.length(); ++i) { | 843 for (int i = 0; i < snapshots_.length(); ++i) { |
814 size += snapshots_[i]->RawSnapshotSize(); | 844 size += snapshots_[i]->RawSnapshotSize(); |
815 } | 845 } |
816 return size; | 846 return size; |
817 } | 847 } |
(...skipping 1820 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2638 bool aborted_; | 2668 bool aborted_; |
2639 }; | 2669 }; |
2640 | 2670 |
2641 | 2671 |
2642 // type, name|index, to_node. | 2672 // type, name|index, to_node. |
2643 const int HeapSnapshotJSONSerializer::kEdgeFieldsCount = 3; | 2673 const int HeapSnapshotJSONSerializer::kEdgeFieldsCount = 3; |
2644 // type, name, id, self_size, children_index. | 2674 // type, name, id, self_size, children_index. |
2645 const int HeapSnapshotJSONSerializer::kNodeFieldsCount = 5; | 2675 const int HeapSnapshotJSONSerializer::kNodeFieldsCount = 5; |
2646 | 2676 |
2647 void HeapSnapshotJSONSerializer::Serialize(v8::OutputStream* stream) { | 2677 void HeapSnapshotJSONSerializer::Serialize(v8::OutputStream* stream) { |
| 2678 if (AllocationTracker* allocation_tracker = |
| 2679 snapshot_->collection()->allocation_tracker()) { |
| 2680 allocation_tracker->PrepareForSerialization(); |
| 2681 } |
2648 ASSERT(writer_ == NULL); | 2682 ASSERT(writer_ == NULL); |
2649 writer_ = new OutputStreamWriter(stream); | 2683 writer_ = new OutputStreamWriter(stream); |
2650 SerializeImpl(); | 2684 SerializeImpl(); |
2651 delete writer_; | 2685 delete writer_; |
2652 writer_ = NULL; | 2686 writer_ = NULL; |
2653 } | 2687 } |
2654 | 2688 |
2655 | 2689 |
2656 void HeapSnapshotJSONSerializer::SerializeImpl() { | 2690 void HeapSnapshotJSONSerializer::SerializeImpl() { |
2657 ASSERT(0 == snapshot_->root()->index()); | 2691 ASSERT(0 == snapshot_->root()->index()); |
2658 writer_->AddCharacter('{'); | 2692 writer_->AddCharacter('{'); |
2659 writer_->AddString("\"snapshot\":{"); | 2693 writer_->AddString("\"snapshot\":{"); |
2660 SerializeSnapshot(); | 2694 SerializeSnapshot(); |
2661 if (writer_->aborted()) return; | 2695 if (writer_->aborted()) return; |
2662 writer_->AddString("},\n"); | 2696 writer_->AddString("},\n"); |
2663 writer_->AddString("\"nodes\":["); | 2697 writer_->AddString("\"nodes\":["); |
2664 SerializeNodes(); | 2698 SerializeNodes(); |
2665 if (writer_->aborted()) return; | 2699 if (writer_->aborted()) return; |
2666 writer_->AddString("],\n"); | 2700 writer_->AddString("],\n"); |
2667 writer_->AddString("\"edges\":["); | 2701 writer_->AddString("\"edges\":["); |
2668 SerializeEdges(); | 2702 SerializeEdges(); |
2669 if (writer_->aborted()) return; | 2703 if (writer_->aborted()) return; |
2670 writer_->AddString("],\n"); | 2704 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 |
2671 writer_->AddString("\"strings\":["); | 2715 writer_->AddString("\"strings\":["); |
2672 SerializeStrings(); | 2716 SerializeStrings(); |
2673 if (writer_->aborted()) return; | 2717 if (writer_->aborted()) return; |
2674 writer_->AddCharacter(']'); | 2718 writer_->AddCharacter(']'); |
2675 writer_->AddCharacter('}'); | 2719 writer_->AddCharacter('}'); |
2676 writer_->Finalize(); | 2720 writer_->Finalize(); |
2677 } | 2721 } |
2678 | 2722 |
2679 | 2723 |
2680 int HeapSnapshotJSONSerializer::GetStringId(const char* s) { | 2724 int HeapSnapshotJSONSerializer::GetStringId(const char* s) { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2821 JSON_S("edge_types") ":" JSON_A( | 2865 JSON_S("edge_types") ":" JSON_A( |
2822 JSON_A( | 2866 JSON_A( |
2823 JSON_S("context") "," | 2867 JSON_S("context") "," |
2824 JSON_S("element") "," | 2868 JSON_S("element") "," |
2825 JSON_S("property") "," | 2869 JSON_S("property") "," |
2826 JSON_S("internal") "," | 2870 JSON_S("internal") "," |
2827 JSON_S("hidden") "," | 2871 JSON_S("hidden") "," |
2828 JSON_S("shortcut") "," | 2872 JSON_S("shortcut") "," |
2829 JSON_S("weak")) "," | 2873 JSON_S("weak")) "," |
2830 JSON_S("string_or_number") "," | 2874 JSON_S("string_or_number") "," |
2831 JSON_S("node")))); | 2875 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")))); |
2832 #undef JSON_S | 2889 #undef JSON_S |
2833 #undef JSON_O | 2890 #undef JSON_O |
2834 #undef JSON_A | 2891 #undef JSON_A |
2835 writer_->AddString(",\"node_count\":"); | 2892 writer_->AddString(",\"node_count\":"); |
2836 writer_->AddNumber(snapshot_->entries().length()); | 2893 writer_->AddNumber(snapshot_->entries().length()); |
2837 writer_->AddString(",\"edge_count\":"); | 2894 writer_->AddString(",\"edge_count\":"); |
2838 writer_->AddNumber(snapshot_->edges().length()); | 2895 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); |
2839 } | 2903 } |
2840 | 2904 |
2841 | 2905 |
2842 static void WriteUChar(OutputStreamWriter* w, unibrow::uchar u) { | 2906 static void WriteUChar(OutputStreamWriter* w, unibrow::uchar u) { |
2843 static const char hex_chars[] = "0123456789ABCDEF"; | 2907 static const char hex_chars[] = "0123456789ABCDEF"; |
2844 w->AddString("\\u"); | 2908 w->AddString("\\u"); |
2845 w->AddCharacter(hex_chars[(u >> 12) & 0xf]); | 2909 w->AddCharacter(hex_chars[(u >> 12) & 0xf]); |
2846 w->AddCharacter(hex_chars[(u >> 8) & 0xf]); | 2910 w->AddCharacter(hex_chars[(u >> 8) & 0xf]); |
2847 w->AddCharacter(hex_chars[(u >> 4) & 0xf]); | 2911 w->AddCharacter(hex_chars[(u >> 4) & 0xf]); |
2848 w->AddCharacter(hex_chars[u & 0xf]); | 2912 w->AddCharacter(hex_chars[u & 0xf]); |
2849 } | 2913 } |
2850 | 2914 |
2851 | 2915 |
| 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 |
2852 void HeapSnapshotJSONSerializer::SerializeString(const unsigned char* s) { | 3010 void HeapSnapshotJSONSerializer::SerializeString(const unsigned char* s) { |
2853 writer_->AddCharacter('\n'); | 3011 writer_->AddCharacter('\n'); |
2854 writer_->AddCharacter('\"'); | 3012 writer_->AddCharacter('\"'); |
2855 for ( ; *s != '\0'; ++s) { | 3013 for ( ; *s != '\0'; ++s) { |
2856 switch (*s) { | 3014 switch (*s) { |
2857 case '\b': | 3015 case '\b': |
2858 writer_->AddString("\\b"); | 3016 writer_->AddString("\\b"); |
2859 continue; | 3017 continue; |
2860 case '\f': | 3018 case '\f': |
2861 writer_->AddString("\\f"); | 3019 writer_->AddString("\\f"); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2911 writer_->AddString("\"<dummy>\""); | 3069 writer_->AddString("\"<dummy>\""); |
2912 for (int i = 1; i < sorted_strings.length(); ++i) { | 3070 for (int i = 1; i < sorted_strings.length(); ++i) { |
2913 writer_->AddCharacter(','); | 3071 writer_->AddCharacter(','); |
2914 SerializeString(sorted_strings[i]); | 3072 SerializeString(sorted_strings[i]); |
2915 if (writer_->aborted()) return; | 3073 if (writer_->aborted()) return; |
2916 } | 3074 } |
2917 } | 3075 } |
2918 | 3076 |
2919 | 3077 |
2920 } } // namespace v8::internal | 3078 } } // namespace v8::internal |
OLD | NEW |