1/*
2 * Copyright (C) 2012 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#pragma once
27
28namespace JSC {
29
30class ProfileTreeNode {
31 typedef HashMap<String, ProfileTreeNode> Map;
32 typedef Map::KeyValuePairType MapEntry;
33
34public:
35 ProfileTreeNode()
36 : m_count(0)
37 , m_children(0)
38 {
39 }
40
41 ~ProfileTreeNode()
42 {
43 delete m_children;
44 }
45
46 ProfileTreeNode* sampleChild(const char* name)
47 {
48 if (!m_children)
49 m_children = new Map();
50
51 ProfileTreeNode newEntry;
52 Map::AddResult result = m_children->add(String(name), newEntry);
53 ProfileTreeNode* childInMap = &result.iterator->value;
54 ++childInMap->m_count;
55 return childInMap;
56 }
57
58 void dump()
59 {
60 dumpInternal(0);
61 }
62
63 uint64_t count()
64 {
65 return m_count;
66 }
67
68 uint64_t childCount()
69 {
70 if (!m_children)
71 return 0;
72 uint64_t childCount = 0;
73 for (Map::iterator it = m_children->begin(); it != m_children->end(); ++it)
74 childCount += it->value.count();
75 return childCount;
76 }
77
78private:
79 void dumpInternal(unsigned indent)
80 {
81 if (!m_children)
82 return;
83
84 // Copy pointers to all children into a vector, and sort the vector by sample count.
85 Vector<MapEntry*> entries;
86 for (Map::iterator it = m_children->begin(); it != m_children->end(); ++it)
87 entries.append(&*it);
88 qsort(entries.begin(), entries.size(), sizeof(MapEntry*), compareEntries);
89
90 // Iterate over the children in sample-frequency order.
91 for (auto* entry : entries) {
92 // Print the number of samples, the name of this node, and the number of samples that are stack-top
93 // in this node (samples directly within this node, excluding samples in children.
94 for (unsigned i = 0; i < indent; ++i)
95 dataLogF(" ");
96 dataLogF("% 8lld: %s (%lld stack top)\n",
97 static_cast<long long>(entry->value.count()),
98 entry->key.utf8().data(),
99 static_cast<long long>(entry->value.count() - entry->value.childCount()));
100
101 // Recursively dump the child nodes.
102 entry->value.dumpInternal(indent + 1);
103 }
104 }
105
106 static int compareEntries(const void* a, const void* b)
107 {
108 uint64_t da = (*static_cast<MapEntry* const *>(a))->value.count();
109 uint64_t db = (*static_cast<MapEntry* const *>(b))->value.count();
110 return (da < db) - (da > db);
111 }
112
113 uint64_t m_count;
114 Map* m_children;
115};
116
117} // namespace JSC
118