]> git.saurik.com Git - apple/javascriptcore.git/blob - profiler/HeavyProfile.cpp
5ea9d3b9ede17281655f7496d0a19da1f3f5ddfa
[apple/javascriptcore.git] / profiler / HeavyProfile.cpp
1 /*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "HeavyProfile.h"
28
29 #include "TreeProfile.h"
30
31 namespace JSC {
32
33 HeavyProfile::HeavyProfile(TreeProfile* treeProfile)
34 : Profile(treeProfile->title(), treeProfile->uid())
35 {
36 m_treeProfile = treeProfile;
37 head()->setTotalTime(m_treeProfile->head()->actualTotalTime());
38 head()->setSelfTime(m_treeProfile->head()->actualSelfTime());
39 generateHeavyStructure();
40 }
41
42 void HeavyProfile::generateHeavyStructure()
43 {
44 ProfileNode* treeHead = m_treeProfile->head();
45 ProfileNode* currentNode = treeHead->firstChild();
46 for (ProfileNode* nextNode = currentNode; nextNode; nextNode = nextNode->firstChild())
47 currentNode = nextNode;
48
49 // For each node
50 HashMap<CallIdentifier, ProfileNode*> foundChildren;
51 while (currentNode && currentNode != treeHead) {
52 ProfileNode* child = foundChildren.get(currentNode->callIdentifier());
53 if (child) // currentNode is in the set already
54 mergeProfiles(child, currentNode);
55 else { // currentNode is not in the set
56 child = addNode(currentNode);
57 foundChildren.set(currentNode->callIdentifier(), child);
58 }
59
60 currentNode = currentNode->traverseNextNodePostOrder();
61 }
62 }
63
64 ProfileNode* HeavyProfile::addNode(ProfileNode* currentNode)
65 {
66 RefPtr<ProfileNode> node = ProfileNode::create(head(), currentNode);
67 head()->addChild(node);
68
69 addAncestorsAsChildren(currentNode->parent(), node.get());
70 return node.get();
71 }
72
73 void HeavyProfile::mergeProfiles(ProfileNode* heavyProfileHead, ProfileNode* treeProfileHead)
74 {
75 ASSERT_ARG(heavyProfileHead, heavyProfileHead);
76 ASSERT_ARG(treeProfileHead, treeProfileHead);
77
78 ProfileNode* currentTreeNode = treeProfileHead;
79 ProfileNode* currentHeavyNode = heavyProfileHead;
80 ProfileNode* previousHeavyNode = 0;
81
82 while (currentHeavyNode) {
83 previousHeavyNode = currentHeavyNode;
84
85 currentHeavyNode->setTotalTime(currentHeavyNode->actualTotalTime() + currentTreeNode->actualTotalTime());
86 currentHeavyNode->setSelfTime(currentHeavyNode->actualSelfTime() + currentTreeNode->actualSelfTime());
87 currentHeavyNode->setNumberOfCalls(currentHeavyNode->numberOfCalls() + currentTreeNode->numberOfCalls());
88
89 currentTreeNode = currentTreeNode->parent();
90 currentHeavyNode = currentHeavyNode->findChild(currentTreeNode);
91 }
92
93 // If currentTreeNode is null then we already have the whole tree we wanted to copy.
94 // If not we need to copy the subset of the tree that remains different between the two.
95 if (currentTreeNode)
96 addAncestorsAsChildren(currentTreeNode, previousHeavyNode);
97 }
98
99 void HeavyProfile::addAncestorsAsChildren(ProfileNode* getFrom, ProfileNode* addTo)
100 {
101 ASSERT_ARG(getFrom, getFrom);
102 ASSERT_ARG(addTo, addTo);
103
104 if (!getFrom->head())
105 return;
106
107 RefPtr<ProfileNode> currentNode = addTo;
108 for (ProfileNode* treeAncestor = getFrom; treeAncestor && treeAncestor != getFrom->head(); treeAncestor = treeAncestor->parent()) {
109 RefPtr<ProfileNode> newChild = ProfileNode::create(currentNode->head(), treeAncestor);
110 currentNode->addChild(newChild);
111 currentNode = newChild.release();
112 }
113 }
114
115 } // namespace JSC