]>
git.saurik.com Git - apple/javascriptcore.git/blob - profiler/HeavyProfile.cpp
5ea9d3b9ede17281655f7496d0a19da1f3f5ddfa
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "HeavyProfile.h"
29 #include "TreeProfile.h"
33 HeavyProfile::HeavyProfile(TreeProfile
* treeProfile
)
34 : Profile(treeProfile
->title(), treeProfile
->uid())
36 m_treeProfile
= treeProfile
;
37 head()->setTotalTime(m_treeProfile
->head()->actualTotalTime());
38 head()->setSelfTime(m_treeProfile
->head()->actualSelfTime());
39 generateHeavyStructure();
42 void HeavyProfile::generateHeavyStructure()
44 ProfileNode
* treeHead
= m_treeProfile
->head();
45 ProfileNode
* currentNode
= treeHead
->firstChild();
46 for (ProfileNode
* nextNode
= currentNode
; nextNode
; nextNode
= nextNode
->firstChild())
47 currentNode
= nextNode
;
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
);
60 currentNode
= currentNode
->traverseNextNodePostOrder();
64 ProfileNode
* HeavyProfile::addNode(ProfileNode
* currentNode
)
66 RefPtr
<ProfileNode
> node
= ProfileNode::create(head(), currentNode
);
67 head()->addChild(node
);
69 addAncestorsAsChildren(currentNode
->parent(), node
.get());
73 void HeavyProfile::mergeProfiles(ProfileNode
* heavyProfileHead
, ProfileNode
* treeProfileHead
)
75 ASSERT_ARG(heavyProfileHead
, heavyProfileHead
);
76 ASSERT_ARG(treeProfileHead
, treeProfileHead
);
78 ProfileNode
* currentTreeNode
= treeProfileHead
;
79 ProfileNode
* currentHeavyNode
= heavyProfileHead
;
80 ProfileNode
* previousHeavyNode
= 0;
82 while (currentHeavyNode
) {
83 previousHeavyNode
= currentHeavyNode
;
85 currentHeavyNode
->setTotalTime(currentHeavyNode
->actualTotalTime() + currentTreeNode
->actualTotalTime());
86 currentHeavyNode
->setSelfTime(currentHeavyNode
->actualSelfTime() + currentTreeNode
->actualSelfTime());
87 currentHeavyNode
->setNumberOfCalls(currentHeavyNode
->numberOfCalls() + currentTreeNode
->numberOfCalls());
89 currentTreeNode
= currentTreeNode
->parent();
90 currentHeavyNode
= currentHeavyNode
->findChild(currentTreeNode
);
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.
96 addAncestorsAsChildren(currentTreeNode
, previousHeavyNode
);
99 void HeavyProfile::addAncestorsAsChildren(ProfileNode
* getFrom
, ProfileNode
* addTo
)
101 ASSERT_ARG(getFrom
, getFrom
);
102 ASSERT_ARG(addTo
, addTo
);
104 if (!getFrom
->head())
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();