Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "ProfileModel.h"
28#include "Profile.h"
29#include <AK/StringBuilder.h>
30#include <ctype.h>
31#include <stdio.h>
32
33ProfileModel::ProfileModel(Profile& profile)
34 : m_profile(profile)
35{
36 m_user_frame_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"));
37 m_kernel_frame_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object-red.png"));
38}
39
40ProfileModel::~ProfileModel()
41{
42}
43
44GUI::ModelIndex ProfileModel::index(int row, int column, const GUI::ModelIndex& parent) const
45{
46 if (!parent.is_valid()) {
47 if (m_profile.roots().is_empty())
48 return {};
49 return create_index(row, column, m_profile.roots().at(row).ptr());
50 }
51 auto& remote_parent = *static_cast<ProfileNode*>(parent.internal_data());
52 return create_index(row, column, remote_parent.children().at(row).ptr());
53}
54
55GUI::ModelIndex ProfileModel::parent_index(const GUI::ModelIndex& index) const
56{
57 if (!index.is_valid())
58 return {};
59 auto& node = *static_cast<ProfileNode*>(index.internal_data());
60 if (!node.parent())
61 return {};
62
63 // NOTE: If the parent has no parent, it's a root, so we have to look among the roots.
64 if (!node.parent()->parent()) {
65 for (size_t row = 0; row < m_profile.roots().size(); ++row) {
66 if (m_profile.roots()[row].ptr() == node.parent()) {
67 return create_index(row, index.column(), node.parent());
68 }
69 }
70 ASSERT_NOT_REACHED();
71 return {};
72 }
73
74 for (size_t row = 0; row < node.parent()->parent()->children().size(); ++row) {
75 if (node.parent()->parent()->children()[row].ptr() == node.parent())
76 return create_index(row, index.column(), node.parent());
77 }
78
79 ASSERT_NOT_REACHED();
80 return {};
81}
82
83int ProfileModel::row_count(const GUI::ModelIndex& index) const
84{
85 if (!index.is_valid())
86 return m_profile.roots().size();
87 auto& node = *static_cast<ProfileNode*>(index.internal_data());
88 return node.children().size();
89}
90
91int ProfileModel::column_count(const GUI::ModelIndex&) const
92{
93 return Column::__Count;
94}
95
96String ProfileModel::column_name(int column) const
97{
98 switch (column) {
99 case Column::SampleCount:
100 return m_profile.show_percentages() ? "% Samples" : "# Samples";
101 case Column::SelfCount:
102 return m_profile.show_percentages() ? "% Self" : "# Self";
103 case Column::StackFrame:
104 return "Stack Frame";
105 default:
106 ASSERT_NOT_REACHED();
107 return {};
108 }
109}
110
111GUI::Model::ColumnMetadata ProfileModel::column_metadata(int column) const
112{
113 if (column == Column::SampleCount || column == Column::SelfCount)
114 return ColumnMetadata { 0, Gfx::TextAlignment::CenterRight };
115 return {};
116}
117
118GUI::Variant ProfileModel::data(const GUI::ModelIndex& index, Role role) const
119{
120 auto* node = static_cast<ProfileNode*>(index.internal_data());
121 if (role == Role::Icon) {
122 if (index.column() == Column::StackFrame) {
123 if (node->address() >= 0xc0000000)
124 return m_kernel_frame_icon;
125 return m_user_frame_icon;
126 }
127 return {};
128 }
129 if (role == Role::Display) {
130 if (index.column() == Column::SampleCount) {
131 if (m_profile.show_percentages())
132 return ((float)node->event_count() / (float)m_profile.filtered_event_count()) * 100.0f;
133 return node->event_count();
134 }
135 if (index.column() == Column::SelfCount) {
136 if (m_profile.show_percentages())
137 return ((float)node->self_count() / (float)m_profile.filtered_event_count()) * 100.0f;
138 return node->self_count();
139 }
140 if (index.column() == Column::StackFrame)
141 return node->symbol();
142 return {};
143 }
144 return {};
145}
146
147void ProfileModel::update()
148{
149 did_update();
150}