Jose Fonseca | 9653f95 | 2015-05-19 16:32:43 +0100 | [diff] [blame] | 1 | #pragma once |
James Benton | 0b65a2b | 2012-09-07 18:38:15 +0100 | [diff] [blame] | 2 | |
| 3 | #include <QString> |
| 4 | #include <QLocale> |
| 5 | #include "trace_profiler.hpp" |
| 6 | |
| 7 | class Profiling { |
| 8 | public: |
| 9 | /** |
| 10 | * Select and show the call in main window. |
| 11 | */ |
| 12 | static void jumpToCall(int index); |
| 13 | |
| 14 | /** |
| 15 | * Convert a CPU / GPU time to a textual representation. |
| 16 | * This includes automatic unit selection. |
| 17 | */ |
| 18 | static QString getTimeString(int64_t time, int64_t unitTime = 0) |
| 19 | { |
| 20 | QString text; |
| 21 | QString unit = " ns"; |
| 22 | double unitScale = 1; |
| 23 | |
| 24 | if (unitTime == 0) { |
| 25 | unitTime = time; |
| 26 | } |
| 27 | |
| 28 | if (unitTime >= 60e9) { |
| 29 | int64_t mins = time / 60e9; |
| 30 | text += QString("%1 m ").arg(mins); |
| 31 | |
| 32 | time -= mins * 60e9; |
| 33 | unit = " s"; |
| 34 | unitScale = 1e9; |
| 35 | } else if (unitTime >= 1e9) { |
| 36 | unit = " s"; |
| 37 | unitScale = 1e9; |
| 38 | } else if (unitTime >= 1e6) { |
| 39 | unit = " ms"; |
| 40 | unitScale = 1e6; |
| 41 | } else if (unitTime >= 1e3) { |
| 42 | unit = QString::fromUtf8(" µs"); |
| 43 | unitScale = 1e3; |
| 44 | } |
| 45 | |
| 46 | /* 3 decimal places */ |
| 47 | text += QString("%1").arg(time / unitScale, 0, 'f', 3); |
| 48 | |
| 49 | /* Remove trailing 0 */ |
| 50 | while(text.endsWith('0')) |
| 51 | text.truncate(text.length() - 1); |
| 52 | |
| 53 | /* Remove trailing decimal point */ |
| 54 | if (text.endsWith(QLocale::system().decimalPoint())) |
| 55 | text.truncate(text.length() - 1); |
| 56 | |
| 57 | return text + unit; |
| 58 | } |
| 59 | |
| 60 | template<typename val_ty, int64_t val_ty::* mem_ptr_start, int64_t val_ty::* mem_ptr_dura> |
| 61 | static typename std::vector<val_ty>::const_iterator binarySearchTimespan( |
| 62 | typename std::vector<val_ty>::const_iterator begin, |
| 63 | typename std::vector<val_ty>::const_iterator end, |
| 64 | int64_t time, |
| 65 | bool nearest = false) |
| 66 | { |
| 67 | int lower = 0; |
| 68 | int upper = end - begin; |
| 69 | int pos = (lower + upper) / 2; |
| 70 | typename std::vector<val_ty>::const_iterator itr = begin + pos; |
| 71 | |
| 72 | while (!((*itr).*mem_ptr_start <= time && (*itr).*mem_ptr_start + (*itr).*mem_ptr_dura > time) && (lower <= upper)) { |
| 73 | if ((*itr).*mem_ptr_start > time) { |
| 74 | upper = pos - 1; |
| 75 | } else { |
| 76 | lower = pos + 1; |
| 77 | } |
| 78 | |
| 79 | pos = (lower + upper) / 2; |
| 80 | itr = begin + pos; |
| 81 | } |
| 82 | |
| 83 | if (nearest || lower <= upper) { |
| 84 | return itr; |
| 85 | } else { |
| 86 | return end; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static std::vector<unsigned>::const_iterator binarySearchTimespanIndexed( |
| 91 | const std::vector<trace::Profile::Call>& calls, |
| 92 | std::vector<unsigned>::const_iterator begin, |
| 93 | std::vector<unsigned>::const_iterator end, |
| 94 | int64_t time, |
| 95 | bool nearest = false) |
| 96 | { |
| 97 | int lower = 0; |
| 98 | int upper = end - begin - 1; |
| 99 | int pos = (lower + upper) / 2; |
| 100 | std::vector<unsigned>::const_iterator itr = begin + pos; |
| 101 | |
| 102 | while (lower <= upper) { |
| 103 | const trace::Profile::Call& call = calls[*itr]; |
| 104 | |
| 105 | if (call.gpuStart <= time && call.gpuStart + call.gpuDuration > time) { |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | if (call.gpuStart > time) { |
| 110 | upper = pos - 1; |
| 111 | } else { |
| 112 | lower = pos + 1; |
| 113 | } |
| 114 | |
| 115 | pos = (lower + upper) / 2; |
| 116 | itr = begin + pos; |
| 117 | } |
| 118 | |
| 119 | if (nearest || lower <= upper) { |
| 120 | return itr; |
| 121 | } else { |
| 122 | return end; |
| 123 | } |
| 124 | } |
| 125 | }; |