James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 1 | #include "profiletablemodel.h" |
James Benton | 902626c | 2012-08-22 12:18:09 +0100 | [diff] [blame] | 2 | #include "profiledialog.h" |
James Benton | 0b65a2b | 2012-09-07 18:38:15 +0100 | [diff] [blame] | 3 | #include "profiling.h" |
James Benton | 902626c | 2012-08-22 12:18:09 +0100 | [diff] [blame] | 4 | |
| 5 | #include <QLocale> |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 6 | |
James Benton | 4c4896f | 2012-08-22 12:11:37 +0100 | [diff] [blame] | 7 | typedef trace::Profile::Call Call; |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 8 | typedef trace::Profile::Frame Frame; |
James Benton | 56ad11c | 2012-08-16 13:44:19 +0100 | [diff] [blame] | 9 | typedef trace::Profile::Program Program; |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 10 | |
| 11 | enum { |
| 12 | COLUMN_PROGRAM, |
| 13 | COLUMN_USAGES, |
| 14 | COLUMN_GPU_TIME, |
| 15 | COLUMN_CPU_TIME, |
| 16 | COLUMN_PIXELS_DRAWN, |
| 17 | COLUMN_GPU_AVERAGE, |
| 18 | COLUMN_CPU_AVERAGE, |
| 19 | COLUMN_PIXELS_AVERAGE, |
| 20 | MAX_COLUMN |
| 21 | }; |
| 22 | |
| 23 | static QString columnNames[] = { |
| 24 | QString("Program"), |
| 25 | QString("Calls"), |
| 26 | QString("Total GPU Time"), |
| 27 | QString("Total CPU Time"), |
| 28 | QString("Total Pixels Drawn"), |
| 29 | QString("Avg GPU Time"), |
| 30 | QString("Avg CPU Time"), |
| 31 | QString("Avg Pixels Drawn") |
| 32 | }; |
| 33 | |
| 34 | ProfileTableModel::ProfileTableModel(QObject *parent) |
| 35 | : QAbstractTableModel(parent), |
| 36 | m_profile(0), |
| 37 | m_sortColumn(COLUMN_GPU_TIME), |
| 38 | m_sortOrder(Qt::DescendingOrder) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | |
| 43 | void ProfileTableModel::setProfile(trace::Profile* profile) |
| 44 | { |
| 45 | m_profile = profile; |
James Benton | 56ad11c | 2012-08-16 13:44:19 +0100 | [diff] [blame] | 46 | m_timeMin = m_profile->frames.front().cpuStart; |
| 47 | m_timeMax = m_profile->frames.back().cpuStart + m_profile->frames.back().cpuDuration; |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 48 | updateModel(); |
| 49 | } |
| 50 | |
| 51 | |
James Benton | b70a86a | 2012-08-28 18:41:43 +0100 | [diff] [blame] | 52 | /** |
| 53 | * Set selection to nothing |
| 54 | */ |
| 55 | void ProfileTableModel::selectNone() |
| 56 | { |
| 57 | m_timeMin = m_timeMax = 0; |
| 58 | updateModel(); |
| 59 | sort(m_sortColumn, m_sortOrder); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * Set selection to program |
| 65 | */ |
| 66 | void ProfileTableModel::selectProgram(unsigned /*program*/) |
| 67 | { |
| 68 | /* We have no program based selection for table */ |
| 69 | selectNone(); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * Set selection to a period of time |
| 75 | */ |
| 76 | void ProfileTableModel::selectTime(int64_t start, int64_t end) |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 77 | { |
| 78 | m_timeMin = start; |
| 79 | m_timeMax = end; |
James Benton | b70a86a | 2012-08-28 18:41:43 +0100 | [diff] [blame] | 80 | |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 81 | updateModel(); |
| 82 | sort(m_sortColumn, m_sortOrder); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /** |
| 87 | * Creates the row data from trace profile |
| 88 | */ |
| 89 | void ProfileTableModel::updateModel() |
| 90 | { |
| 91 | if (m_timeMin == m_timeMax) { |
James Benton | 56ad11c | 2012-08-16 13:44:19 +0100 | [diff] [blame] | 92 | m_timeMin = m_profile->frames.front().cpuStart; |
| 93 | m_timeMax = m_profile->frames.back().cpuStart + m_profile->frames.back().cpuDuration; |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 94 | } |
| 95 | |
Jose Fonseca | 010f996 | 2016-03-05 14:45:41 +0000 | [diff] [blame] | 96 | for (auto & row : m_rowData) { |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 97 | |
| 98 | row.uses = 0; |
| 99 | row.pixels = 0; |
| 100 | row.gpuTime = 0; |
| 101 | row.cpuTime = 0; |
| 102 | row.longestCpu = NULL; |
| 103 | row.longestGpu = NULL; |
| 104 | row.longestPixel = NULL; |
| 105 | } |
| 106 | |
James Benton | 56ad11c | 2012-08-16 13:44:19 +0100 | [diff] [blame] | 107 | for (std::vector<Program>::const_iterator itr = m_profile->programs.begin(); itr != m_profile->programs.end(); ++itr) { |
James Benton | 4c4896f | 2012-08-22 12:11:37 +0100 | [diff] [blame] | 108 | ProfileTableRow* row = NULL; |
James Benton | 56ad11c | 2012-08-16 13:44:19 +0100 | [diff] [blame] | 109 | const Program& program = *itr; |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 110 | |
Jose Fonseca | 010f996 | 2016-03-05 14:45:41 +0000 | [diff] [blame] | 111 | for (const auto & callNo : program.calls) { |
| 112 | const Call& call = m_profile->calls[callNo]; |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 113 | |
James Benton | 56ad11c | 2012-08-16 13:44:19 +0100 | [diff] [blame] | 114 | if (call.cpuStart > m_timeMax) { |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 115 | break; |
| 116 | } |
| 117 | |
James Benton | 56ad11c | 2012-08-16 13:44:19 +0100 | [diff] [blame] | 118 | if (call.cpuStart + call.cpuDuration < m_timeMin) { |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 119 | continue; |
| 120 | } |
| 121 | |
James Benton | 4c4896f | 2012-08-22 12:11:37 +0100 | [diff] [blame] | 122 | if (!row) { |
| 123 | row = getRow(itr - m_profile->programs.begin()); |
| 124 | } |
| 125 | |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 126 | row->uses++; |
James Benton | 56ad11c | 2012-08-16 13:44:19 +0100 | [diff] [blame] | 127 | row->pixels += call.pixels; |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 128 | row->gpuTime += call.gpuDuration; |
| 129 | row->cpuTime += call.cpuDuration; |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 130 | |
| 131 | if (!row->longestGpu || row->longestGpu->gpuDuration < call.gpuDuration) { |
| 132 | row->longestGpu = &call; |
| 133 | } |
| 134 | |
| 135 | if (!row->longestCpu || row->longestCpu->cpuDuration < call.cpuDuration) { |
| 136 | row->longestCpu = &call; |
| 137 | } |
| 138 | |
| 139 | if (!row->longestPixel || row->longestPixel->pixels < call.pixels) { |
| 140 | row->longestPixel = &call; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /** |
| 148 | * Get the appropriate call associated with an item in the table |
| 149 | */ |
James Benton | 4c4896f | 2012-08-22 12:11:37 +0100 | [diff] [blame] | 150 | const trace::Profile::Call *ProfileTableModel::getJumpCall(const QModelIndex & index) const { |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 151 | const ProfileTableRow& row = m_rowData[index.row()]; |
| 152 | |
| 153 | switch(index.column()) { |
| 154 | case COLUMN_GPU_TIME: |
| 155 | case COLUMN_GPU_AVERAGE: |
| 156 | return row.longestGpu; |
| 157 | case COLUMN_CPU_TIME: |
| 158 | case COLUMN_CPU_AVERAGE: |
| 159 | return row.longestCpu; |
| 160 | case COLUMN_PIXELS_DRAWN: |
| 161 | case COLUMN_PIXELS_AVERAGE: |
| 162 | return row.longestPixel; |
| 163 | } |
| 164 | |
| 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | |
James Benton | b70a86a | 2012-08-28 18:41:43 +0100 | [diff] [blame] | 169 | /** |
| 170 | * Get the shader program associated with an item in the table |
| 171 | */ |
| 172 | unsigned ProfileTableModel::getProgram(const QModelIndex & index) const |
| 173 | { |
| 174 | const ProfileTableRow& row = m_rowData[index.row()]; |
| 175 | return row.program; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | /** |
| 180 | * Get the row index for a shader program |
| 181 | */ |
| 182 | int ProfileTableModel::getRowIndex(unsigned program) const |
| 183 | { |
| 184 | for (int i = 0; i < m_rowData.size(); ++i) { |
| 185 | if (m_rowData[i].program == program) |
| 186 | return i; |
| 187 | } |
| 188 | |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /** |
| 194 | * Get the row data for a shader program |
| 195 | */ |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 196 | ProfileTableRow* ProfileTableModel::getRow(unsigned program) { |
Jose Fonseca | 010f996 | 2016-03-05 14:45:41 +0000 | [diff] [blame] | 197 | for (auto & row : m_rowData) { |
| 198 | if (row.program == program) |
| 199 | return &row; |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 200 | } |
| 201 | |
James Benton | 56ad11c | 2012-08-16 13:44:19 +0100 | [diff] [blame] | 202 | m_rowData.append(ProfileTableRow(program)); |
| 203 | return &m_rowData.back(); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | |
| 207 | int ProfileTableModel::rowCount(const QModelIndex & parent) const |
| 208 | { |
| 209 | if (!parent.isValid()) { |
| 210 | return m_rowData.size(); |
| 211 | } else { |
| 212 | return 0; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | |
| 217 | int ProfileTableModel::columnCount(const QModelIndex & /*parent*/) const |
| 218 | { |
| 219 | return MAX_COLUMN; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | QVariant ProfileTableModel::data(const QModelIndex &index, int role) const |
| 224 | { |
| 225 | if (!index.isValid()) { |
| 226 | return QVariant(); |
| 227 | } |
| 228 | |
| 229 | if (role == Qt::DisplayRole) { |
| 230 | const ProfileTableRow& row = m_rowData[index.row()]; |
| 231 | |
| 232 | switch(index.column()) { |
| 233 | case COLUMN_PROGRAM: |
| 234 | return row.program; |
| 235 | case COLUMN_USAGES: |
James Benton | 902626c | 2012-08-22 12:18:09 +0100 | [diff] [blame] | 236 | return QLocale::system().toString(row.uses); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 237 | case COLUMN_GPU_TIME: |
James Benton | 0b65a2b | 2012-09-07 18:38:15 +0100 | [diff] [blame] | 238 | return Profiling::getTimeString(row.gpuTime); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 239 | case COLUMN_CPU_TIME: |
James Benton | 0b65a2b | 2012-09-07 18:38:15 +0100 | [diff] [blame] | 240 | return Profiling::getTimeString(row.cpuTime); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 241 | case COLUMN_PIXELS_DRAWN: |
James Benton | 902626c | 2012-08-22 12:18:09 +0100 | [diff] [blame] | 242 | return QLocale::system().toString((qlonglong)row.pixels); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 243 | case COLUMN_GPU_AVERAGE: |
James Benton | 0b65a2b | 2012-09-07 18:38:15 +0100 | [diff] [blame] | 244 | return Profiling::getTimeString((row.uses <= 0) ? 0 : (row.gpuTime / row.uses)); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 245 | case COLUMN_CPU_AVERAGE: |
James Benton | 0b65a2b | 2012-09-07 18:38:15 +0100 | [diff] [blame] | 246 | return Profiling::getTimeString((row.uses <= 0) ? 0 : (row.cpuTime / row.uses)); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 247 | case COLUMN_PIXELS_AVERAGE: |
James Benton | 902626c | 2012-08-22 12:18:09 +0100 | [diff] [blame] | 248 | return QLocale::system().toString((row.uses <= 0) ? 0 : (row.pixels / row.uses)); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 249 | } |
| 250 | } else if (role == Qt::TextAlignmentRole) { |
| 251 | return Qt::AlignRight; |
| 252 | } |
| 253 | |
| 254 | return QVariant(); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | QVariant ProfileTableModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 259 | { |
| 260 | if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { |
| 261 | if (section >= 0 && section < MAX_COLUMN) { |
| 262 | return columnNames[section]; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | return QVariant(); |
| 267 | } |
| 268 | |
| 269 | |
| 270 | class ProgramSorter { |
| 271 | public: |
| 272 | ProgramSorter(int column, Qt::SortOrder order) |
| 273 | : mSortColumn(column), |
| 274 | mSortOrder(order) |
| 275 | { |
| 276 | } |
| 277 | |
| 278 | bool operator()(const ProfileTableRow &p1, const ProfileTableRow &p2) const |
| 279 | { |
| 280 | bool result = true; |
| 281 | |
| 282 | switch(mSortColumn) { |
| 283 | case COLUMN_PROGRAM: |
| 284 | result = p1.program < p2.program; |
| 285 | break; |
| 286 | case COLUMN_USAGES: |
| 287 | result = p1.uses < p2.uses; |
| 288 | break; |
| 289 | case COLUMN_GPU_TIME: |
| 290 | result = p1.gpuTime < p2.gpuTime; |
| 291 | break; |
| 292 | case COLUMN_CPU_TIME: |
| 293 | result = p1.cpuTime < p2.cpuTime; |
| 294 | break; |
| 295 | case COLUMN_PIXELS_DRAWN: |
| 296 | result = p1.pixels < p2.pixels; |
| 297 | break; |
| 298 | case COLUMN_GPU_AVERAGE: |
James Benton | 0871705 | 2012-08-08 17:43:52 +0100 | [diff] [blame] | 299 | result = ((p1.uses <= 0) ? 0 : (p1.gpuTime / p1.uses)) < ((p2.uses <= 0) ? 0 : (p2.gpuTime / p2.uses)); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 300 | break; |
| 301 | case COLUMN_CPU_AVERAGE: |
James Benton | 0871705 | 2012-08-08 17:43:52 +0100 | [diff] [blame] | 302 | result = ((p1.uses <= 0) ? 0 : (p1.cpuTime / p1.uses)) < ((p2.uses <= 0) ? 0 : (p2.cpuTime / p2.uses)); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 303 | break; |
| 304 | case COLUMN_PIXELS_AVERAGE: |
James Benton | 0871705 | 2012-08-08 17:43:52 +0100 | [diff] [blame] | 305 | result = ((p1.uses <= 0) ? 0 : (p1.pixels / p1.uses)) < ((p2.uses <= 0) ? 0 : (p2.pixels / p2.uses)); |
James Benton | fc4f55a | 2012-08-08 17:09:07 +0100 | [diff] [blame] | 306 | break; |
| 307 | } |
| 308 | |
| 309 | if (mSortOrder == Qt::DescendingOrder) { |
| 310 | return !result; |
| 311 | } else { |
| 312 | return result; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | private: |
| 317 | int mSortColumn; |
| 318 | Qt::SortOrder mSortOrder; |
| 319 | }; |
| 320 | |
| 321 | |
| 322 | void ProfileTableModel::sort(int column, Qt::SortOrder order) { |
| 323 | m_sortColumn = column; |
| 324 | m_sortOrder = order; |
| 325 | qSort(m_rowData.begin(), m_rowData.end(), ProgramSorter(column, order)); |
| 326 | emit dataChanged(createIndex(0, 0), createIndex(m_rowData.size(), MAX_COLUMN)); |
| 327 | } |
| 328 | |
| 329 | |
| 330 | #include "profiletablemodel.moc" |