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 698 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
740 sizeof(*this) + | 741 sizeof(*this) + |
741 sizeof(HashMap::Entry) * entries_map_.capacity() + | 742 sizeof(HashMap::Entry) * entries_map_.capacity() + |
742 GetMemoryUsedByList(entries_) + | 743 GetMemoryUsedByList(entries_) + |
743 GetMemoryUsedByList(time_intervals_); | 744 GetMemoryUsedByList(time_intervals_); |
744 } | 745 } |
745 | 746 |
746 | 747 |
747 HeapSnapshotsCollection::HeapSnapshotsCollection(Heap* heap) | 748 HeapSnapshotsCollection::HeapSnapshotsCollection(Heap* heap) |
748 : is_tracking_objects_(false), | 749 : is_tracking_objects_(false), |
749 names_(heap), | 750 names_(heap), |
750 ids_(heap) { | 751 ids_(heap), |
| 752 allocation_tracker_(NULL) { |
751 } | 753 } |
752 | 754 |
753 | 755 |
754 static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) { | 756 static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) { |
755 delete *snapshot_ptr; | 757 delete *snapshot_ptr; |
756 } | 758 } |
757 | 759 |
758 | 760 |
759 HeapSnapshotsCollection::~HeapSnapshotsCollection() { | 761 HeapSnapshotsCollection::~HeapSnapshotsCollection() { |
| 762 delete allocation_tracker_; |
760 snapshots_.Iterate(DeleteHeapSnapshot); | 763 snapshots_.Iterate(DeleteHeapSnapshot); |
761 } | 764 } |
762 | 765 |
763 | 766 |
| 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 |
764 HeapSnapshot* HeapSnapshotsCollection::NewSnapshot(const char* name, | 785 HeapSnapshot* HeapSnapshotsCollection::NewSnapshot(const char* name, |
765 unsigned uid) { | 786 unsigned uid) { |
766 is_tracking_objects_ = true; // Start watching for heap objects moves. | 787 is_tracking_objects_ = true; // Start watching for heap objects moves. |
767 return new HeapSnapshot(this, name, uid); | 788 return new HeapSnapshot(this, name, uid); |
768 } | 789 } |
769 | 790 |
770 | 791 |
771 void HeapSnapshotsCollection::SnapshotGenerationFinished( | 792 void HeapSnapshotsCollection::SnapshotGenerationFinished( |
772 HeapSnapshot* snapshot) { | 793 HeapSnapshot* snapshot) { |
773 ids_.SnapshotGenerationFinished(); | 794 ids_.SnapshotGenerationFinished(); |
(...skipping 23 matching lines...) Expand all Loading... |
797 if (ids_.FindEntry(obj->address()) == id) { | 818 if (ids_.FindEntry(obj->address()) == id) { |
798 ASSERT(object == NULL); | 819 ASSERT(object == NULL); |
799 object = obj; | 820 object = obj; |
800 // Can't break -- kFilterUnreachable requires full heap traversal. | 821 // Can't break -- kFilterUnreachable requires full heap traversal. |
801 } | 822 } |
802 } | 823 } |
803 return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>(); | 824 return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>(); |
804 } | 825 } |
805 | 826 |
806 | 827 |
| 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 |
807 size_t HeapSnapshotsCollection::GetUsedMemorySize() const { | 837 size_t HeapSnapshotsCollection::GetUsedMemorySize() const { |
808 size_t size = sizeof(*this); | 838 size_t size = sizeof(*this); |
809 size += names_.GetUsedMemorySize(); | 839 size += names_.GetUsedMemorySize(); |
810 size += ids_.GetUsedMemorySize(); | 840 size += ids_.GetUsedMemorySize(); |
811 size += GetMemoryUsedByList(snapshots_); | 841 size += GetMemoryUsedByList(snapshots_); |
812 for (int i = 0; i < snapshots_.length(); ++i) { | 842 for (int i = 0; i < snapshots_.length(); ++i) { |
813 size += snapshots_[i]->RawSnapshotSize(); | 843 size += snapshots_[i]->RawSnapshotSize(); |
814 } | 844 } |
815 return size; | 845 return size; |
816 } | 846 } |
(...skipping 1778 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2595 bool aborted_; | 2625 bool aborted_; |
2596 }; | 2626 }; |
2597 | 2627 |
2598 | 2628 |
2599 // type, name|index, to_node. | 2629 // type, name|index, to_node. |
2600 const int HeapSnapshotJSONSerializer::kEdgeFieldsCount = 3; | 2630 const int HeapSnapshotJSONSerializer::kEdgeFieldsCount = 3; |
2601 // type, name, id, self_size, children_index. | 2631 // type, name, id, self_size, children_index. |
2602 const int HeapSnapshotJSONSerializer::kNodeFieldsCount = 5; | 2632 const int HeapSnapshotJSONSerializer::kNodeFieldsCount = 5; |
2603 | 2633 |
2604 void HeapSnapshotJSONSerializer::Serialize(v8::OutputStream* stream) { | 2634 void HeapSnapshotJSONSerializer::Serialize(v8::OutputStream* stream) { |
| 2635 if (AllocationTracker* allocation_tracker = |
| 2636 snapshot_->collection()->allocation_tracker()) { |
| 2637 allocation_tracker->PrepareForSerialization(); |
| 2638 } |
2605 ASSERT(writer_ == NULL); | 2639 ASSERT(writer_ == NULL); |
2606 writer_ = new OutputStreamWriter(stream); | 2640 writer_ = new OutputStreamWriter(stream); |
2607 SerializeImpl(); | 2641 SerializeImpl(); |
2608 delete writer_; | 2642 delete writer_; |
2609 writer_ = NULL; | 2643 writer_ = NULL; |
2610 } | 2644 } |
2611 | 2645 |
2612 | 2646 |
2613 void HeapSnapshotJSONSerializer::SerializeImpl() { | 2647 void HeapSnapshotJSONSerializer::SerializeImpl() { |
2614 ASSERT(0 == snapshot_->root()->index()); | 2648 ASSERT(0 == snapshot_->root()->index()); |
2615 writer_->AddCharacter('{'); | 2649 writer_->AddCharacter('{'); |
2616 writer_->AddString("\"snapshot\":{"); | 2650 writer_->AddString("\"snapshot\":{"); |
2617 SerializeSnapshot(); | 2651 SerializeSnapshot(); |
2618 if (writer_->aborted()) return; | 2652 if (writer_->aborted()) return; |
2619 writer_->AddString("},\n"); | 2653 writer_->AddString("},\n"); |
2620 writer_->AddString("\"nodes\":["); | 2654 writer_->AddString("\"nodes\":["); |
2621 SerializeNodes(); | 2655 SerializeNodes(); |
2622 if (writer_->aborted()) return; | 2656 if (writer_->aborted()) return; |
2623 writer_->AddString("],\n"); | 2657 writer_->AddString("],\n"); |
2624 writer_->AddString("\"edges\":["); | 2658 writer_->AddString("\"edges\":["); |
2625 SerializeEdges(); | 2659 SerializeEdges(); |
2626 if (writer_->aborted()) return; | 2660 if (writer_->aborted()) return; |
2627 writer_->AddString("],\n"); | 2661 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 |
2628 writer_->AddString("\"strings\":["); | 2672 writer_->AddString("\"strings\":["); |
2629 SerializeStrings(); | 2673 SerializeStrings(); |
2630 if (writer_->aborted()) return; | 2674 if (writer_->aborted()) return; |
2631 writer_->AddCharacter(']'); | 2675 writer_->AddCharacter(']'); |
2632 writer_->AddCharacter('}'); | 2676 writer_->AddCharacter('}'); |
2633 writer_->Finalize(); | 2677 writer_->Finalize(); |
2634 } | 2678 } |
2635 | 2679 |
2636 | 2680 |
2637 int HeapSnapshotJSONSerializer::GetStringId(const char* s) { | 2681 int HeapSnapshotJSONSerializer::GetStringId(const char* s) { |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2778 JSON_S("edge_types") ":" JSON_A( | 2822 JSON_S("edge_types") ":" JSON_A( |
2779 JSON_A( | 2823 JSON_A( |
2780 JSON_S("context") "," | 2824 JSON_S("context") "," |
2781 JSON_S("element") "," | 2825 JSON_S("element") "," |
2782 JSON_S("property") "," | 2826 JSON_S("property") "," |
2783 JSON_S("internal") "," | 2827 JSON_S("internal") "," |
2784 JSON_S("hidden") "," | 2828 JSON_S("hidden") "," |
2785 JSON_S("shortcut") "," | 2829 JSON_S("shortcut") "," |
2786 JSON_S("weak")) "," | 2830 JSON_S("weak")) "," |
2787 JSON_S("string_or_number") "," | 2831 JSON_S("string_or_number") "," |
2788 JSON_S("node")))); | 2832 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")))); |
2789 #undef JSON_S | 2846 #undef JSON_S |
2790 #undef JSON_O | 2847 #undef JSON_O |
2791 #undef JSON_A | 2848 #undef JSON_A |
2792 writer_->AddString(",\"node_count\":"); | 2849 writer_->AddString(",\"node_count\":"); |
2793 writer_->AddNumber(snapshot_->entries().length()); | 2850 writer_->AddNumber(snapshot_->entries().length()); |
2794 writer_->AddString(",\"edge_count\":"); | 2851 writer_->AddString(",\"edge_count\":"); |
2795 writer_->AddNumber(snapshot_->edges().length()); | 2852 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); |
2796 } | 2860 } |
2797 | 2861 |
2798 | 2862 |
2799 static void WriteUChar(OutputStreamWriter* w, unibrow::uchar u) { | 2863 static void WriteUChar(OutputStreamWriter* w, unibrow::uchar u) { |
2800 static const char hex_chars[] = "0123456789ABCDEF"; | 2864 static const char hex_chars[] = "0123456789ABCDEF"; |
2801 w->AddString("\\u"); | 2865 w->AddString("\\u"); |
2802 w->AddCharacter(hex_chars[(u >> 12) & 0xf]); | 2866 w->AddCharacter(hex_chars[(u >> 12) & 0xf]); |
2803 w->AddCharacter(hex_chars[(u >> 8) & 0xf]); | 2867 w->AddCharacter(hex_chars[(u >> 8) & 0xf]); |
2804 w->AddCharacter(hex_chars[(u >> 4) & 0xf]); | 2868 w->AddCharacter(hex_chars[(u >> 4) & 0xf]); |
2805 w->AddCharacter(hex_chars[u & 0xf]); | 2869 w->AddCharacter(hex_chars[u & 0xf]); |
2806 } | 2870 } |
2807 | 2871 |
2808 | 2872 |
| 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 |
2809 void HeapSnapshotJSONSerializer::SerializeString(const unsigned char* s) { | 2967 void HeapSnapshotJSONSerializer::SerializeString(const unsigned char* s) { |
2810 writer_->AddCharacter('\n'); | 2968 writer_->AddCharacter('\n'); |
2811 writer_->AddCharacter('\"'); | 2969 writer_->AddCharacter('\"'); |
2812 for ( ; *s != '\0'; ++s) { | 2970 for ( ; *s != '\0'; ++s) { |
2813 switch (*s) { | 2971 switch (*s) { |
2814 case '\b': | 2972 case '\b': |
2815 writer_->AddString("\\b"); | 2973 writer_->AddString("\\b"); |
2816 continue; | 2974 continue; |
2817 case '\f': | 2975 case '\f': |
2818 writer_->AddString("\\f"); | 2976 writer_->AddString("\\f"); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2868 writer_->AddString("\"<dummy>\""); | 3026 writer_->AddString("\"<dummy>\""); |
2869 for (int i = 1; i < sorted_strings.length(); ++i) { | 3027 for (int i = 1; i < sorted_strings.length(); ++i) { |
2870 writer_->AddCharacter(','); | 3028 writer_->AddCharacter(','); |
2871 SerializeString(sorted_strings[i]); | 3029 SerializeString(sorted_strings[i]); |
2872 if (writer_->aborted()) return; | 3030 if (writer_->aborted()) return; |
2873 } | 3031 } |
2874 } | 3032 } |
2875 | 3033 |
2876 | 3034 |
2877 } } // namespace v8::internal | 3035 } } // namespace v8::internal |
OLD | NEW |