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 "Profile.h"
28
29 #include "ProfileNode.h"
30 #include <stdio.h>
31
32 #if PLATFORM(ANDROID)
33 typedef bool (* Comparator)(const void*, const void*);
34 namespace std {
35 extern void sort(const void** start, const void** end, Comparator comp);
36 }
37 #endif
38
39 namespace JSC {
40
create(const UString & title,unsigned uid)41 PassRefPtr<Profile> Profile::create(const UString& title, unsigned uid)
42 {
43 return adoptRef(new Profile(title, uid));
44 }
45
Profile(const UString & title,unsigned uid)46 Profile::Profile(const UString& title, unsigned uid)
47 : m_title(title)
48 , m_uid(uid)
49 {
50 // FIXME: When multi-threading is supported this will be a vector and calls
51 // into the profiler will need to know which thread it is executing on.
52 m_head = ProfileNode::create(CallIdentifier("Thread_1", 0, 0), 0, 0);
53 }
54
~Profile()55 Profile::~Profile()
56 {
57 }
58
forEach(void (ProfileNode::* function)())59 void Profile::forEach(void (ProfileNode::*function)())
60 {
61 ProfileNode* currentNode = m_head->firstChild();
62 for (ProfileNode* nextNode = currentNode; nextNode; nextNode = nextNode->firstChild())
63 currentNode = nextNode;
64
65 if (!currentNode)
66 currentNode = m_head.get();
67
68 ProfileNode* endNode = m_head->traverseNextNodePostOrder();
69 while (currentNode && currentNode != endNode) {
70 (currentNode->*function)();
71 currentNode = currentNode->traverseNextNodePostOrder();
72 }
73 }
74
focus(const ProfileNode * profileNode)75 void Profile::focus(const ProfileNode* profileNode)
76 {
77 if (!profileNode || !m_head)
78 return;
79
80 bool processChildren;
81 const CallIdentifier& callIdentifier = profileNode->callIdentifier();
82 for (ProfileNode* currentNode = m_head.get(); currentNode; currentNode = currentNode->traverseNextNodePreOrder(processChildren))
83 processChildren = currentNode->focus(callIdentifier);
84
85 // Set the visible time of all nodes so that the %s display correctly.
86 forEach(&ProfileNode::calculateVisibleTotalTime);
87 }
88
exclude(const ProfileNode * profileNode)89 void Profile::exclude(const ProfileNode* profileNode)
90 {
91 if (!profileNode || !m_head)
92 return;
93
94 const CallIdentifier& callIdentifier = profileNode->callIdentifier();
95
96 for (ProfileNode* currentNode = m_head.get(); currentNode; currentNode = currentNode->traverseNextNodePreOrder())
97 currentNode->exclude(callIdentifier);
98
99 // Set the visible time of the head so the %s display correctly.
100 m_head->setVisibleTotalTime(m_head->totalTime() - m_head->selfTime());
101 m_head->setVisibleSelfTime(0.0);
102 }
103
restoreAll()104 void Profile::restoreAll()
105 {
106 forEach(&ProfileNode::restore);
107 }
108
109 #ifndef NDEBUG
debugPrintData() const110 void Profile::debugPrintData() const
111 {
112 printf("Call graph:\n");
113 m_head->debugPrintData(0);
114 }
115
116 typedef pair<UString::Rep*, unsigned> NameCountPair;
117
118 #if PLATFORM(ANDROID)
119 typedef bool (* NameCountPairComparator)(const NameCountPair&, const NameCountPair&);
120
_sort(NameCountPair * start,NameCountPair * end,NameCountPairComparator comp)121 inline void _sort(NameCountPair* start, NameCountPair* end, NameCountPairComparator comp)
122 {
123 std::sort((const void**) start, (const void**) end, (Comparator) comp);
124 }
125 #endif
126
functionNameCountPairComparator(const NameCountPair & a,const NameCountPair & b)127 static inline bool functionNameCountPairComparator(const NameCountPair& a, const NameCountPair& b)
128 {
129 return a.second > b.second;
130 }
131
debugPrintDataSampleStyle() const132 void Profile::debugPrintDataSampleStyle() const
133 {
134 typedef Vector<NameCountPair> NameCountPairVector;
135
136 FunctionCallHashCount countedFunctions;
137 printf("Call graph:\n");
138 m_head->debugPrintDataSampleStyle(0, countedFunctions);
139
140 printf("\nTotal number in stack:\n");
141 NameCountPairVector sortedFunctions(countedFunctions.size());
142 copyToVector(countedFunctions, sortedFunctions);
143
144 #if PLATFORM(ANDROID)
145 _sort(sortedFunctions.begin(), sortedFunctions.end(), functionNameCountPairComparator);
146 #else
147 std::sort(sortedFunctions.begin(), sortedFunctions.end(), functionNameCountPairComparator);
148 #endif
149 for (NameCountPairVector::iterator it = sortedFunctions.begin(); it != sortedFunctions.end(); ++it)
150 printf(" %-12d%s\n", (*it).second, UString((*it).first).UTF8String().c_str());
151
152 printf("\nSort by top of stack, same collapsed (when >= 5):\n");
153 }
154 #endif
155
156 } // namespace JSC
157