| | 1 | | #pragma rtGlobals=3 |
| | 2 | | #pragma TextEncoding="UTF-8" |
| | 3 | | #pragma rtFunctionErrors=1 |
| | 4 | | #pragma version=1.10 |
| | 5 | | #pragma ModuleName=IUTF_Tracing_Analytics |
| | 6 | |
|
| | 7 | | #undef UTF_ALLOW_TRACING |
| | 8 | | #if Exists("TUFXOP_Version") |
| | 9 | |
|
| | 10 | | #if IgorVersion() >= 10.00 |
| | 11 | | #define UTF_ALLOW_TRACING |
| | 12 | | #elif (IgorVersion() >= 9.00) && (NumberByKey("BUILD", IgorInfo(0)) >= 38812) |
| | 13 | | #define UTF_ALLOW_TRACING |
| | 14 | | #endif |
| | 15 | |
|
| | 16 | | #endif |
| | 17 | |
|
| | 18 | | #ifdef UTF_ALLOW_TRACING |
| | 19 | |
|
| | 20 | | static Structure CollectionResult |
| | 21 | | WAVE/T functions |
| | 22 | | WAVE lines |
| | 23 | | WAVE calls |
| | 24 | | WAVE sums |
| | 25 | | variable count |
| | 26 | | EndStructure |
| | 27 | |
|
| 0 | 28 | | static Function GetMaxFuncCount() |
| 0 | 29 | | WAVE/WAVE funcLocations = IUTF_Tracing#GetFuncLocations() |
| 0 | 30 | | variable size = DimSize(funcLocations, UTF_ROW) |
| | 31 | |
|
| 0 | 32 | | Make/FREE=1/N=(size) helper = DimSize(funcLocations[p][%FUNCLIST], UTF_ROW) |
| 0 | 33 | | return WaveMax(helper) |
| 0 | 34 | | End |
| | 35 | |
|
| 0 | 36 | | static Function GetMaxProcLineCount() |
| 0 | 37 | | WAVE wv = IUTF_Tracing#GetProcSizes() |
| | 38 | |
|
| 0 | 39 | | return WaveMax(wv) |
| 0 | 40 | | End |
| | 41 | |
|
| 6 | 42 | | static Function/WAVE GetTotals() |
| 6 | 43 | | variable i, numWaves |
| | 44 | |
|
| 6 | 45 | | TUFXOP_GetStorage/N="IUTF_TestRun" storage |
| 6 | 46 | | numWaves = DimSize(storage, UTF_ROW) |
| 6 | 47 | | WAVE/ZZ totals |
| 6 | 48 | | for(i = 0; i < numWaves; i++) |
| 6 | 49 | | WAVE/Z/WAVE entryOuter = storage[i] |
| 6 | 50 | | if(!WaveExists(entryOuter)) |
| 0 | 51 | | continue |
| 6 | 52 | | endif |
| 6 | 53 | | WAVE entry = entryOuter[0] |
| 6 | 54 | | if(WaveExists(totals)) |
| 0 | 55 | | FastOp totals += entry |
| 6 | 56 | | else |
| 6 | 57 | | Duplicate/FREE=1 entry, totals |
| 0 | 58 | | endif |
| 0 | 59 | | endfor |
| | 60 | |
|
| 0 | 61 | | if(!WaveExists(totals)) |
| 0 | 62 | | Make/N=0/FREE=1 totals |
| 0 | 63 | | endif |
| | 64 | |
|
| 0 | 65 | | return totals |
| 6 | 66 | | End |
| | 67 | |
|
| 0 | 68 | | static Function CollectFunctions(WAVE totals, WAVE/T procs, STRUCT CollectionResult &result) |
| 0 | 69 | | variable i, j, startIndex, endIndex, funcCount |
| 0 | 70 | | variable procCount = DimSize(procs, UTF_ROW) |
| 0 | 71 | | variable maxFuncCount = GetMaxFuncCount() |
| 0 | 72 | | DFREF dfr = GetPackageFolder() |
| 0 | 73 | | WAVE/WAVE funcLocations = IUTF_Tracing#GetFuncLocations() |
| 0 | 74 | | variable lbFuncList = FindDimLabel(funcLocations, UTF_COLUMN, "FUNCLIST") |
| 0 | 75 | | variable lbFuncStart = FindDimLabel(funcLocations, UTF_COLUMN, "FUNCSTART") |
| | 76 | |
|
| 0 | 77 | | Make/FREE=1/N=(procCount, maxFuncCount)/T result.functions |
| 0 | 78 | | Make/FREE=1/N=(procCount, maxFuncCount) result.lines, result.calls, result.sums |
| | 79 | |
|
| 0 | 80 | | for(i = 0; i < procCount; i++) |
| 0 | 81 | | WAVE/T procFuncNames = funcLocations[i][lbFuncList] |
| 0 | 82 | | WAVE procFuncLines = funcLocations[i][lbFuncStart] |
| 0 | 83 | | funcCount = DimSize(procFuncNames, UTF_ROW) |
| 0 | 84 | | if(!funcCount) |
| 0 | 85 | | continue |
| 0 | 86 | | endif |
| 0 | 87 | | result.count += funcCount |
| | 88 | |
|
| 0 | 89 | | result.functions[i][0, funcCount - 1] = procFuncNames[q] |
| 0 | 90 | | result.lines[i][0, funcCount - 1] = procFuncLines[q] |
| 0 | 91 | | result.calls[i][0, funcCount - 1] = totals[procFuncLines[q]][0][i] |
| 0 | 92 | | for(j = 0; j < funcCount; j++) |
| 0 | 93 | | startIndex = procFuncLines[j] |
| 0 | 94 | | endIndex = j + 1 < funcCount ? procFuncLines[j + 1] : DimSize(totals, UTF_ROW) |
| 0 | 95 | | WaveStats/M=1/Q/RMD=[startIndex, endIndex - 1][0, 0][i, i] totals |
| 0 | 96 | | result.sums[i][j] = V_sum |
| 0 | 97 | | endfor |
| 0 | 98 | | endfor |
| 0 | 99 | | End |
| | 100 | |
|
| 0 | 101 | | static Function CollectLines(WAVE totals, WAVE/T procs, STRUCT CollectionResult &result) |
| 0 | 102 | | variable i, j, funcCount |
| 0 | 103 | | string name |
| 0 | 104 | | variable procCount = DimSize(procs, UTF_ROW) |
| 0 | 105 | | variable lineCount = GetMaxProcLineCount() |
| 0 | 106 | | DFREF dfr = GetPackageFolder() |
| 0 | 107 | | WAVE/WAVE funcLocations = IUTF_Tracing#GetFuncLocations() |
| 0 | 108 | | variable lbFuncList = FindDimLabel(funcLocations, UTF_COLUMN, "FUNCLIST") |
| 0 | 109 | | variable lbFuncStart = FindDimLabel(funcLocations, UTF_COLUMN, "FUNCSTART") |
| | 110 | |
|
| 0 | 111 | | Make/FREE=1/N=(procCount, lineCount)/T result.functions = "" |
| 0 | 112 | | Make/FREE=1/N=(procCount, lineCount) result.lines = q |
| 0 | 113 | | Make/FREE=1/N=(procCount, lineCount) result.calls = totals[q][0][p] |
| | 114 | |
|
| 0 | 115 | | WAVE procSizes = IUTF_Tracing#GetProcSizes() |
| 0 | 116 | | for(i = 0; i < procCount; i++) |
| 0 | 117 | | WAVE/T procFuncNames = funcLocations[i][lbFuncList] |
| 0 | 118 | | WAVE procFuncLines = funcLocations[i][lbFuncStart] |
| 0 | 119 | | lineCount = procSizes[i] |
| 0 | 120 | | result.count += lineCount |
| | 121 | |
|
| 0 | 122 | | // insert the function names at the lines of their declaration. |
| 0 | 123 | | funcCount = DimSize(procFuncNames, UTF_ROW) |
| 0 | 124 | | for(j = 0; j < funcCount; j++) |
| 0 | 125 | | result.functions[i][procFuncLines[j]] = procFuncNames[j] |
| 0 | 126 | | endfor |
| | 127 | |
|
| 0 | 128 | | // fill in the gaps between the function declaration with the name |
| 0 | 129 | | // of the previous function declaration |
| 0 | 130 | | name = "" |
| 0 | 131 | | for(j = 0; j < lineCount; j++) |
| 0 | 132 | | if(IUTF_Utils#IsEmpty(result.functions[i][j])) |
| 0 | 133 | | result.functions[i][j] = name |
| 0 | 134 | | else |
| 0 | 135 | | name = result.functions[i][j] |
| 0 | 136 | | endif |
| 0 | 137 | | endfor |
| 0 | 138 | | endfor |
| 0 | 139 | | End |
| | 140 | |
|
| 0 | 141 | | static Function/WAVE SearchHighestWithMeta(WAVE/T procs, STRUCT CollectionResult &collectionResult, variable sorting) |
| 0 | 142 | | variable i, searchIndex, metaIndex |
| 0 | 143 | | string msg |
| | 144 | |
|
| 0 | 145 | | Make/FREE=1/N=(collectionResult.count, 5)/T result |
| 0 | 146 | | SetDimLabel UTF_COLUMN, 0, 'Function Calls', result |
| 0 | 147 | | SetDimLabel UTF_COLUMN, 1, 'Sum of called Lines', result |
| 0 | 148 | | SetDimLabel UTF_COLUMN, 2, Procedure, result |
| 0 | 149 | | SetDimLabel UTF_COLUMN, 3, Function, result |
| 0 | 150 | | SetDimLabel UTF_COLUMN, 4, Line, result |
| | 151 | |
|
| 0 | 152 | | if(sorting == UTF_ANALYTICS_CALLS) |
| 0 | 153 | | WAVE searchWave = collectionResult.calls |
| 0 | 154 | | searchIndex = 0 |
| 0 | 155 | | WAVE metaWave = collectionResult.sums |
| 0 | 156 | | metaIndex = 1 |
| 0 | 157 | | elseif(sorting == UTF_ANALYTICS_SUM) |
| 0 | 158 | | WAVE searchWave = collectionResult.sums |
| 0 | 159 | | searchIndex = 1 |
| 0 | 160 | | WAVE metaWave = collectionResult.calls |
| 0 | 161 | | metaIndex = 0 |
| 0 | 162 | | else |
| 0 | 163 | | sprintf msg, "Bug: Sorting %d is not supported", sorting |
| 0 | 164 | | IUTF_Reporting#IUTF_PrintStatusMessage(msg) |
| 0 | 165 | | return result |
| 0 | 166 | | endif |
| | 167 | |
|
| 0 | 168 | | for(i = 0; i < collectionResult.count; i++) |
| 0 | 169 | | WaveStats/M=1/Q searchWave |
| 0 | 170 | | if(V_max <= 0) |
| 0 | 171 | | Redimension/N=(i, -1) result |
| 0 | 172 | | return result |
| 0 | 173 | | endif |
| 0 | 174 | | result[i][searchIndex] = num2istr(V_max) |
| 0 | 175 | | result[i][metaIndex] = num2istr(metaWave[V_maxRowLoc][V_maxColLoc]) |
| 0 | 176 | | result[i][2] = procs[V_maxRowLoc] |
| 0 | 177 | | result[i][3] = collectionResult.functions[V_maxRowLoc][V_maxColLoc] |
| 0 | 178 | | result[i][4] = num2istr(collectionResult.lines[V_maxRowLoc][V_maxColLoc]) |
| 0 | 179 | | searchWave[V_maxRowLoc][V_maxColLoc] = NaN |
| 0 | 180 | | endfor |
| | 181 | |
|
| 0 | 182 | | return result |
| 0 | 183 | | End |
| | 184 | |
|
| 0 | 185 | | static Function/WAVE SearchHighest(WAVE/T procs, STRUCT CollectionResult &collectionResult, variable sorting) |
| 0 | 186 | | variable i |
| 0 | 187 | | string msg |
| | 188 | |
|
| 0 | 189 | | Make/FREE=1/N=(collectionResult.count, 4)/T result |
| 0 | 190 | | SetDimLabel UTF_COLUMN, 0, Calls, result |
| 0 | 191 | | SetDimLabel UTF_COLUMN, 1, Procedure, result |
| 0 | 192 | | SetDimLabel UTF_COLUMN, 2, Function, result |
| 0 | 193 | | SetDimLabel UTF_COLUMN, 3, Line, result |
| | 194 | |
|
| 0 | 195 | | if(sorting == UTF_ANALYTICS_CALLS) |
| 0 | 196 | | WAVE searchWave = collectionResult.calls |
| 0 | 197 | | else |
| 0 | 198 | | sprintf msg, "Bug: Sorting %d is not supported", sorting |
| 0 | 199 | | IUTF_Reporting#IUTF_PrintStatusMessage(msg) |
| 0 | 200 | | return result |
| 0 | 201 | | endif |
| | 202 | |
|
| 0 | 203 | | for(i = 0; i < collectionResult.count; i++) |
| 0 | 204 | | WaveStats/M=1/Q searchWave |
| 0 | 205 | | if(V_max <= 0) |
| 0 | 206 | | Redimension/N=(i, -1) result |
| 0 | 207 | | return result |
| 0 | 208 | | endif |
| 0 | 209 | | result[i][0] = num2istr(V_max) |
| 0 | 210 | | result[i][1] = procs[V_maxRowLoc] |
| 0 | 211 | | result[i][2] = collectionResult.functions[V_maxRowLoc][V_maxColLoc] |
| 0 | 212 | | result[i][3] = num2istr(collectionResult.lines[V_maxRowLoc][V_maxColLoc]) |
| 0 | 213 | | searchWave[V_maxRowLoc][V_maxColLoc] = NaN |
| 0 | 214 | | endfor |
| | 215 | |
|
| 0 | 216 | | return result |
| 0 | 217 | | End |
| | 218 | |
|
| 0 | 219 | | static Function/S GetWaveHeader(WAVE wv) |
| 0 | 220 | | variable i |
| 0 | 221 | | string header = "" |
| 0 | 222 | | variable size = DimSize(wv, UTF_COLUMN) |
| | 223 | |
|
| 0 | 224 | | for(i = size - 1; i >= 0; i--) |
| 0 | 225 | | header = AddListItem(GetDimLabel(wv, UTF_COLUMN, i), header) |
| 0 | 226 | | endfor |
| | 227 | |
|
| 0 | 228 | | return header |
| 0 | 229 | | End |
| | 230 | |
|
| 6 | 231 | | static Function HasTracingData() |
| 6 | 232 | | DFREF dfr = GetPackageFolder() |
| | 233 | |
|
| 6 | 234 | | WAVE/Z/SDFR=dfr FuncLocations |
| 6 | 235 | | WAVE/Z/SDFR=dfr ProcSizes |
| 6 | 236 | | if(!WaveExists(FuncLocations) || !WaveExists(ProcSizes)) |
| 0 | 237 | | return 0 |
| 6 | 238 | | endif |
| | 239 | |
|
| 6 | 240 | | TUFXOP_GetStorage/Z/N="IUTF_TestRun" storage |
| 6 | 241 | | if(V_flag) |
| 0 | 242 | | return 0 |
| 6 | 243 | | endif |
| | 244 | |
|
| 6 | 245 | | return 1 |
| 6 | 246 | | End |
| | 247 | |
|
| | 248 | | /// @brief Show the top functions after a tracing run in the history area |
| | 249 | | /// @param count The maximum number of items that should be output. |
| | 250 | | /// @param mode (optional, default UTF_ANALYTICS_FUNCTIONS) defines the data selection. |
| | 251 | | /// Can be UTF_ANALYTICS_FUNCTIONS or UTF_ANALYTICS_LINES. |
| | 252 | | /// @param sorting (optional, default UTF_ANALYTICS_CALLS) defines the metric for sorting. |
| | 253 | | /// Can be UTF_ANALYTICS_CALLS or UTF_ANALYTICS_SUM. UTF_ANALYTICS_SUM is only |
| | 254 | | /// supported for the mode UTF_ANALYTICS_FUNCTIONS. |
| 0 | 255 | | Function ShowTopFunctions(variable count, [variable mode, variable sorting]) |
| 0 | 256 | | STRUCT CollectionResult collectionResult |
| 0 | 257 | | string msg, header |
| 0 | 258 | | WAVE/T procs = IUTF_Tracing#GetTracedProcedureNames() |
| 0 | 259 | | DFREF dfr = GetPackageFolder() |
| | 260 | |
|
| 0 | 261 | | mode = ParamIsDefault(mode) ? UTF_ANALYTICS_FUNCTIONS : mode |
| 0 | 262 | | sorting = ParamIsDefault(sorting) ? UTF_ANALYTICS_CALLS : sorting |
| | 263 | |
|
| 0 | 264 | | if(mode != UTF_ANALYTICS_FUNCTIONS && mode != UTF_ANALYTICS_LINES) |
| 0 | 265 | | sprintf msg, "Mode %d is an unsupported mode", mode |
| 0 | 266 | | IUTF_Reporting#IUTF_PrintStatusMessage(msg) |
| 0 | 267 | | return NaN |
| 0 | 268 | | endif |
| 0 | 269 | | if(sorting != UTF_ANALYTICS_CALLS && sorting != UTF_ANALYTICS_SUM) |
| 0 | 270 | | sprintf msg, "Sorting %d is an unsupported sorting", sorting |
| 0 | 271 | | IUTF_Reporting#IUTF_PrintStatusMessage(msg) |
| 0 | 272 | | return NaN |
| 0 | 273 | | endif |
| 0 | 274 | | if(sorting == UTF_ANALYTICS_SUM && mode != UTF_ANALYTICS_FUNCTIONS) |
| 0 | 275 | | IUTF_Reporting#IUTF_PrintStatusMessage("Sum sorting is only available for the functions mode") |
| 0 | 276 | | return NaN |
| 0 | 277 | | endif |
| 0 | 278 | | if(count < 0 || IUTF_Utils#IsNaN(count)) |
| 0 | 279 | | sprintf msg, "Invalid count: %d", count |
| 0 | 280 | | IUTF_Reporting#IUTF_PrintStatusMessage(msg) |
| 0 | 281 | | return NaN |
| 0 | 282 | | endif |
| 0 | 283 | | if(!HasTracingData()) |
| 0 | 284 | | IUTF_Reporting#IUTF_PrintStatusMessage("No Tracing data exists. Try to run tracing first.") |
| 0 | 285 | | return NaN |
| 0 | 286 | | endif |
| | 287 | |
|
| 0 | 288 | | WAVE totals = GetTotals() |
| 0 | 289 | | if(!DimSize(totals, UTF_ROW)) |
| 0 | 290 | | // this can happen after stored Experiment is loaded to a fresh instance of Igor |
| 0 | 291 | | IUTF_Reporting#IUTF_PrintStatusMessage("TUFXOP has no data. Try to rerun tracing to get new data.") |
| 0 | 292 | | return NaN |
| 0 | 293 | | endif |
| | 294 | |
|
| 0 | 295 | | if(mode == UTF_ANALYTICS_FUNCTIONS) |
| 0 | 296 | | CollectFunctions(totals, procs, collectionResult) |
| 0 | 297 | | elseif(mode == UTF_ANALYTICS_LINES) |
| 0 | 298 | | CollectLines(totals, procs, collectionResult) |
| 0 | 299 | | else |
| 0 | 300 | | sprintf msg, "Bug: Unknown mode %d for collection", mode |
| 0 | 301 | | IUTF_Reporting#IUTF_PrintStatusMessage(msg) |
| 0 | 302 | | return NaN |
| 0 | 303 | | endif |
| | 304 | |
|
| 0 | 305 | | if(count < collectionResult.count) |
| 0 | 306 | | collectionResult.count = count |
| 0 | 307 | | endif |
| | 308 | |
|
| 0 | 309 | | if(mode == UTF_ANALYTICS_FUNCTIONS) |
| 0 | 310 | | WAVE/T result = SearchHighestWithMeta(procs, collectionResult, sorting) |
| 0 | 311 | | elseif(mode == UTF_ANALYTICS_LINES) |
| 0 | 312 | | WAVE/T result = SearchHighest(procs, collectionResult, sorting) |
| 0 | 313 | | else |
| 0 | 314 | | sprintf msg, "Bug: Unknown mode %d for sorting", mode |
| 0 | 315 | | IUTF_Reporting#IUTF_PrintStatusMessage(msg) |
| 0 | 316 | | endif |
| | 317 | |
|
| 0 | 318 | | Duplicate/O result, dfr:TracingAnalyticResult |
| | 319 | |
|
| 0 | 320 | | header = GetWaveHeader(result) |
| 0 | 321 | | msg = IUTF_Utils#NicifyTableText(result, header) |
| 0 | 322 | | IUTF_Reporting#IUTF_PrintStatusMessage(msg) |
| 0 | 323 | | End |
| | 324 | |
|
| | 325 | | #endif // UTF_ALLOW_TRACING |