1/*
2 * Copyright (C) 2008, 2014 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 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef ProfileNode_h
30#define ProfileNode_h
31
32#include "CallIdentifier.h"
33#include <wtf/HashCountedSet.h>
34#include <wtf/RefCounted.h>
35#include <wtf/RefPtr.h>
36#include <wtf/Vector.h>
37
38namespace JSC {
39
40    class ExecState;
41    class ProfileNode;
42
43    typedef HashCountedSet<StringImpl*> FunctionCallHashCount;
44
45    class ProfileNode : public RefCounted<ProfileNode> {
46    public:
47        static PassRefPtr<ProfileNode> create(ExecState* callerCallFrame, const CallIdentifier& callIdentifier, ProfileNode* headNode, ProfileNode* parentNode)
48        {
49            return adoptRef(new ProfileNode(callerCallFrame, callIdentifier, headNode, parentNode));
50        }
51
52        static PassRefPtr<ProfileNode> create(ExecState* callerCallFrame, ProfileNode* headNode, ProfileNode* node)
53        {
54            return adoptRef(new ProfileNode(callerCallFrame, headNode, node));
55        }
56
57        struct Call {
58        public:
59            Call(double startTime, double totalTime = NAN)
60                : m_startTime(startTime)
61                , m_totalTime(totalTime)
62            {
63            }
64
65            double startTime() const { return m_startTime; }
66            void setStartTime(double time) { m_startTime = time; }
67
68            double totalTime() const { return m_totalTime; }
69            void setTotalTime(double time) { m_totalTime = time; }
70
71        private:
72            double m_startTime;
73            double m_totalTime;
74        };
75
76        bool operator==(ProfileNode* node) { return m_callIdentifier == node->callIdentifier(); }
77
78        ProfileNode* willExecute(ExecState* callerCallFrame, const CallIdentifier&);
79        ProfileNode* didExecute();
80
81        void stopProfiling();
82
83        // CallIdentifier members
84        ExecState* callerCallFrame() const { return m_callerCallFrame; }
85        const CallIdentifier& callIdentifier() const { return m_callIdentifier; }
86        unsigned id() const { return m_callIdentifier.hash(); }
87        const String& functionName() const { return m_callIdentifier.functionName(); }
88        const String& url() const { return m_callIdentifier.url(); }
89        unsigned lineNumber() const { return m_callIdentifier.lineNumber(); }
90        unsigned columnNumber() const { return m_callIdentifier.columnNumber(); }
91
92        // Relationships
93        ProfileNode* head() const { return m_head; }
94        void setHead(ProfileNode* head) { m_head = head; }
95
96        ProfileNode* parent() const { return m_parent; }
97        void setParent(ProfileNode* parent) { m_parent = parent; }
98
99        ProfileNode* nextSibling() const { return m_nextSibling; }
100        void setNextSibling(ProfileNode* nextSibling) { m_nextSibling = nextSibling; }
101
102        // Time members
103        double totalTime() const { return m_totalTime; }
104        void setTotalTime(double time) { m_totalTime = time; }
105
106        double selfTime() const { return m_selfTime; }
107        void setSelfTime(double time) { m_selfTime = time; }
108
109        double totalPercent() const { return (m_totalTime / (m_head ? m_head->totalTime() : totalTime())) * 100.0; }
110        double selfPercent() const { return (m_selfTime / (m_head ? m_head->totalTime() : totalTime())) * 100.0; }
111
112        Vector<Call> calls() const { return m_calls; }
113        Call& lastCall() { ASSERT(!m_calls.isEmpty()); return m_calls.last(); }
114        size_t numberOfCalls() const { return m_calls.size(); }
115
116        // Children members
117        const Vector<RefPtr<ProfileNode>>& children() const { return m_children; }
118        ProfileNode* firstChild() const { return m_children.size() ? m_children.first().get() : 0; }
119        ProfileNode* lastChild() const { return m_children.size() ? m_children.last().get() : 0; }
120        void removeChild(ProfileNode*);
121        void addChild(PassRefPtr<ProfileNode> prpChild);
122        void insertNode(PassRefPtr<ProfileNode> prpNode);
123
124        ProfileNode* traverseNextNodePostOrder() const;
125
126#ifndef NDEBUG
127        const char* c_str() const { return m_callIdentifier; }
128        void debugPrintData(int indentLevel) const;
129        double debugPrintDataSampleStyle(int indentLevel, FunctionCallHashCount&) const;
130#endif
131
132    private:
133        typedef Vector<RefPtr<ProfileNode>>::const_iterator StackIterator;
134
135        ProfileNode(ExecState* callerCallFrame, const CallIdentifier&, ProfileNode* headNode, ProfileNode* parentNode);
136        ProfileNode(ExecState* callerCallFrame, ProfileNode* headNode, ProfileNode* nodeToCopy);
137
138        void startTimer();
139        void resetChildrensSiblings();
140        void endAndRecordCall();
141
142        ExecState* m_callerCallFrame;
143        CallIdentifier m_callIdentifier;
144        ProfileNode* m_head;
145        ProfileNode* m_parent;
146        ProfileNode* m_nextSibling;
147
148        double m_totalTime;
149        double m_selfTime;
150
151        Vector<Call, 1> m_calls;
152        Vector<RefPtr<ProfileNode>> m_children;
153    };
154
155} // namespace JSC
156
157#endif // ProfileNode_h
158