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