OMCompiler/Compiler/Util/ExecStat.mo
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file is part of OpenModelica. | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), | ||
| 5 | * c/o Linköpings universitet, Department of Computer and Information Science, | ||
| 6 | * SE-58183 Linköping, Sweden. | ||
| 7 | * | ||
| 8 | * All rights reserved. | ||
| 9 | * | ||
| 10 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF AGPL VERSION 3 LICENSE OR | ||
| 11 | * THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. | ||
| 12 | * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES | ||
| 13 | * RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GNU AGPL | ||
| 14 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 15 | * | ||
| 16 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) | ||
| 17 | * Public License (OSMC-PL) are obtained from OSMC, either from the above | ||
| 18 | * address, from the URLs: | ||
| 19 | * http://www.openmodelica.org or | ||
| 20 | * https://github.com/OpenModelica/ or | ||
| 21 | * http://www.ida.liu.se/projects/OpenModelica, | ||
| 22 | * and in the OpenModelica distribution. | ||
| 23 | * | ||
| 24 | * GNU AGPL version 3 is obtained from: | ||
| 25 | * https://www.gnu.org/licenses/licenses.html#GPL | ||
| 26 | * | ||
| 27 | * This program is distributed WITHOUT ANY WARRANTY; without | ||
| 28 | * even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 29 | * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH | ||
| 30 | * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL. | ||
| 31 | * | ||
| 32 | * See the full OSMC Public License conditions for more details. | ||
| 33 | * | ||
| 34 | */ | ||
| 35 | |||
| 36 | encapsulated package ExecStat "Timing / measuring memory for different parts of the compiler" | ||
| 37 | |||
| 38 | protected | ||
| 39 | |||
| 40 | import ClockIndexes; | ||
| 41 | import Error; | ||
| 42 | import GCExt; | ||
| 43 | import Global; | ||
| 44 | import Flags; | ||
| 45 | import System; | ||
| 46 | import StringUtil; | ||
| 47 | |||
| 48 | constant String timeFormat = "%.4g"; // Why not "%.9f"? | ||
| 49 | constant Integer timeMaxLength = 20; // Why not 21? | ||
| 50 | constant Integer memoryMaxSizeInUnit = 500; // Why not 512? | ||
| 51 | constant Integer memorySignificantDigits = 4; // Why not 3? | ||
| 52 | |||
| 53 | public | ||
| 54 | |||
| 55 | function execStatReset | ||
| 56 | algorithm | ||
| 57 | 8035 | setGlobalRoot(Global.gcProfilingIndex, GCExt.getProfStats()); | |
| 58 | 8035 | System.realtimeClear(ClockIndexes.RT_CLOCK_EXECSTAT); | |
| 59 | 8035 | System.realtimeTick(ClockIndexes.RT_CLOCK_EXECSTAT); | |
| 60 | end execStatReset; | ||
| 61 | |||
| 62 | function execStat | ||
| 63 | "Prints an execution stat on the format: | ||
| 64 | *** %name% -> time: %time%, memory %memory% | ||
| 65 | Where you provide name, and time is the time since the last call using this | ||
| 66 | index (the clock is reset after each call). The memory is the total memory | ||
| 67 | consumed by the compiler at this point in time. | ||
| 68 | " | ||
| 69 | input String name; | ||
| 70 | protected | ||
| 71 | Real t, total; | ||
| 72 | String timeStr, totalTimeStr, gcStr; | ||
| 73 | Integer memory, oldMemory, heapsize_full, free_bytes_full, since, before; | ||
| 74 | GCExt.ProfStats stats, oldStats; | ||
| 75 | function snprintff | ||
| 76 | input Real val; | ||
| 77 | output String str = System.snprintff(timeFormat, timeMaxLength, val); | ||
| 78 | end snprintff; | ||
| 79 | function bytesToReadableUnit | ||
| 80 | input Real bytes; | ||
| 81 | output String str = StringUtil.bytesToReadableUnit(bytes, memorySignificantDigits, memoryMaxSizeInUnit); | ||
| 82 | end bytesToReadableUnit; | ||
| 83 | algorithm | ||
| 84 |
1/2✓ Branch 1 taken 146784 times.
✗ Branch 2 not taken.
|
146784 | if Flags.isSet(Flags.EXEC_STAT) then |
| 85 | ✗ | for i in if Flags.isSet(Flags.EXEC_STAT_EXTRA_GC) then {1,2} else {1} loop | |
| 86 | ✗ | if i==2 then | |
| 87 | ✗ | GCExt.gcollect(); | |
| 88 | end if; | ||
| 89 | ✗ | t := System.realtimeAccumulate(ClockIndexes.RT_CLOCK_EXECSTAT); | |
| 90 | ✗ | total := System.realtimeAccumulated(ClockIndexes.RT_CLOCK_EXECSTAT); | |
| 91 | ✗ | stats as GCExt.PROFSTATS(bytes_allocd_since_gc=since, allocd_bytes_before_gc=before, heapsize_full=heapsize_full, free_bytes_full=free_bytes_full) := GCExt.getProfStats(); | |
| 92 | ✗ | memory := since+before; | |
| 93 | ✗ | oldStats := getGlobalRoot(Global.gcProfilingIndex); | |
| 94 | ✗ | GCExt.PROFSTATS(bytes_allocd_since_gc=since, allocd_bytes_before_gc=before) := oldStats; | |
| 95 | ✗ | oldMemory := since+before; | |
| 96 | ✗ | timeStr := snprintff(t); | |
| 97 | ✗ | totalTimeStr := snprintff(total); | |
| 98 | ✗ | if Flags.isSet(Flags.GC_PROF) then | |
| 99 | ✗ | gcStr := GCExt.profStatsStr(stats, head="", delimiter=" / "); | |
| 100 | ✗ | Error.addMessage(Error.EXEC_STAT_GC, {name + (if i==2 then " GC" else ""), timeStr, totalTimeStr, gcStr}); | |
| 101 | else | ||
| 102 | ✗ | Error.addMessage(Error.EXEC_STAT, {name + (if i==2 then " GC" else ""), timeStr, totalTimeStr, | |
| 103 | bytesToReadableUnit(memory-oldMemory), | ||
| 104 | bytesToReadableUnit(memory), | ||
| 105 | bytesToReadableUnit(free_bytes_full), | ||
| 106 | bytesToReadableUnit(heapsize_full) | ||
| 107 | }); | ||
| 108 | end if; | ||
| 109 | ✗ | setGlobalRoot(Global.gcProfilingIndex, stats); | |
| 110 | ✗ | System.realtimeTick(ClockIndexes.RT_CLOCK_EXECSTAT); | |
| 111 | end for; | ||
| 112 | end if; | ||
| 113 | end execStat; | ||
| 114 | |||
| 115 | annotation(__OpenModelica_Interface="util"); | ||
| 116 | end ExecStat; | ||
| 117 |