OMCompiler/SimulationRuntime/cpp/Core/Utils/extension/measure_time.hpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * This file belongs to the OpenModelica Run-Time System | ||
| 3 | * | ||
| 4 | * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), c/o Linköpings | ||
| 5 | * universitet, Department of Computer and Information Science, SE-58183 Linköping, Sweden. All rights | ||
| 6 | * reserved. | ||
| 7 | * | ||
| 8 | * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE | ||
| 9 | * AGPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. ANY | ||
| 10 | * USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S | ||
| 11 | * ACCEPTANCE OF THE BSD NEW LICENSE OR THE OSMC PUBLIC LICENSE OR THE AGPL | ||
| 12 | * VERSION 3, ACCORDING TO RECIPIENTS CHOICE. | ||
| 13 | * | ||
| 14 | * The OpenModelica software and the OSMC (Open Source Modelica Consortium) Public License | ||
| 15 | * (OSMC-PL) are obtained from OSMC, either from the above address, from the URLs: | ||
| 16 | * http://www.openmodelica.org or https://github.com/OpenModelica/ or | ||
| 17 | * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica distribution. GNU | ||
| 18 | * AGPL version 3 is obtained from: https://www.gnu.org/licenses/licenses.html#GPL. The BSD NEW | ||
| 19 | * License is obtained from: http://www.opensource.org/licenses/BSD-3-Clause. | ||
| 20 | * | ||
| 21 | * This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY | ||
| 23 | * SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF | ||
| 24 | * OSMC-PL. | ||
| 25 | * | ||
| 26 | */ | ||
| 27 | |||
| 28 | #ifndef MEASURE_TIME_HPP | ||
| 29 | #define MEASURE_TIME_HPP | ||
| 30 | |||
| 31 | #if defined(_MSC_VER) | ||
| 32 | #include <intrin.h> | ||
| 33 | #endif | ||
| 34 | |||
| 35 | #ifdef USE_SCOREP | ||
| 36 | #define MEASURETIME_REGION_DEFINE(handlerName, regionName) SCOREP_USER_REGION_DEFINE( handlerName ) | ||
| 37 | #define MEASURETIME_START(valStart, handlerName, regionName) SCOREP_USER_REGION_BEGIN( handlerName, regionName, SCOREP_USER_REGION_TYPE_COMMON ) | ||
| 38 | #define MEASURETIME_END(valStart, valEnd, valRes, handlerName) SCOREP_USER_REGION_END( handlerName ) | ||
| 39 | #include <scorep/SCOREP_User.h> | ||
| 40 | #else | ||
| 41 | #define MEASURETIME_REGION_DEFINE(handlerName, regionName) | ||
| 42 | #define MEASURETIME_START(valStart, handlerName, regionName) MeasureTime::getTimeValuesStart(valStart) | ||
| 43 | #define MEASURETIME_END(valStart, valEnd, valRes, handlerName) { MeasureTime::getTimeValuesEnd(valEnd); valEnd->sub(valStart); valEnd->sub(MeasureTime::getOverhead()); valRes->_sumMeasuredValues->add(valEnd); ++(valRes->_sumMeasuredValues->_numCalcs); } | ||
| 44 | #endif | ||
| 45 | |||
| 46 | #include <fstream> | ||
| 47 | #include <sstream> | ||
| 48 | #include <vector> | ||
| 49 | #include <map> | ||
| 50 | #include <ctime> | ||
| 51 | #include <iostream> | ||
| 52 | #include <Core/Modelica.h> | ||
| 53 | |||
| 54 | ✗ | class BOOST_EXTENSION_EXPORT_DECL MeasureTimeValues | |
| 55 | { | ||
| 56 | public: | ||
| 57 | unsigned int _numCalcs; | ||
| 58 | |||
| 59 | MeasureTimeValues(); | ||
| 60 | virtual ~MeasureTimeValues(); | ||
| 61 | |||
| 62 | virtual std::string serializeToJson() const = 0; | ||
| 63 | |||
| 64 | virtual void add(MeasureTimeValues *values) = 0; | ||
| 65 | virtual void sub(MeasureTimeValues *values) = 0; | ||
| 66 | virtual void div(int counter) = 0; | ||
| 67 | virtual MeasureTimeValues* clone() const = 0; | ||
| 68 | virtual void reset(); | ||
| 69 | }; | ||
| 70 | |||
| 71 | class BOOST_EXTENSION_EXPORT_DECL MeasureTimeValuesSolver : public MeasureTimeValues | ||
| 72 | { | ||
| 73 | public: | ||
| 74 | MeasureTimeValuesSolver(); | ||
| 75 | MeasureTimeValuesSolver(unsigned long long functionEvaluations, unsigned long long errorTestFailures); | ||
| 76 | MeasureTimeValuesSolver(const MeasureTimeValuesSolver &timeValues); | ||
| 77 | virtual ~MeasureTimeValuesSolver(); | ||
| 78 | |||
| 79 | virtual std::string serializeToJson() const; | ||
| 80 | |||
| 81 | virtual void add(MeasureTimeValues *values); | ||
| 82 | virtual void sub(MeasureTimeValues *values); | ||
| 83 | virtual void div(int counter); | ||
| 84 | |||
| 85 | virtual MeasureTimeValues* clone() const; | ||
| 86 | virtual void reset(); | ||
| 87 | |||
| 88 | unsigned long long _functionEvaluations; | ||
| 89 | unsigned long long _errorTestFailures; | ||
| 90 | }; | ||
| 91 | |||
| 92 | class BOOST_EXTENSION_EXPORT_DECL MeasureTimeData | ||
| 93 | { | ||
| 94 | public: | ||
| 95 | std::string _id; | ||
| 96 | MeasureTimeValues *_sumMeasuredValues; | ||
| 97 | |||
| 98 | MeasureTimeData(); | ||
| 99 | MeasureTimeData(const MeasureTimeData &data); | ||
| 100 | MeasureTimeData(std::string id); | ||
| 101 | virtual ~MeasureTimeData(); | ||
| 102 | |||
| 103 | std::string serializeToJson() const; | ||
| 104 | |||
| 105 | void addValuesToSum(MeasureTimeValues *values); | ||
| 106 | }; | ||
| 107 | |||
| 108 | class BOOST_EXTENSION_EXPORT_DECL MeasureTime | ||
| 109 | { | ||
| 110 | public: | ||
| 111 | typedef std::map<std::string, std::vector<MeasureTimeData*> *> block_map; | ||
| 112 | typedef std::map<std::string, block_map> file_map; | ||
| 113 | |||
| 114 | virtual ~MeasureTime(); | ||
| 115 | |||
| 116 | static MeasureTime* getInstance(); | ||
| 117 | |||
| 118 | static MeasureTimeValues* getOverhead(); | ||
| 119 | |||
| 120 | /** | ||
| 121 | * Applied overhead minimization: | ||
| 122 | * - stick thread to core 1 -> no effect | ||
| 123 | * - always inline -> effect | ||
| 124 | * - measure overhead and sub the values -> effect | ||
| 125 | */ | ||
| 126 | static inline void getTimeValuesStart(MeasureTimeValues *res) | ||
| 127 | { | ||
| 128 | ✗ | getInstance()->getTimeValuesStartP(res); | |
| 129 | } | ||
| 130 | |||
| 131 | static inline void getTimeValuesEnd(MeasureTimeValues *res) | ||
| 132 | { | ||
| 133 | ✗ | getInstance()->getTimeValuesEndP(res); | |
| 134 | } | ||
| 135 | |||
| 136 | static MeasureTimeValues* getZeroValues(); | ||
| 137 | |||
| 138 | static void deinitialize(); | ||
| 139 | |||
| 140 | static void addResultContentBlock(std::string modelName, std::string blockName, std::vector<MeasureTimeData*> *data); | ||
| 141 | |||
| 142 | static void writeToJson(); | ||
| 143 | |||
| 144 | virtual void benchOverhead(); | ||
| 145 | |||
| 146 | virtual void setOverheadToZero(); | ||
| 147 | |||
| 148 | virtual void initializeThread(unsigned long int threadNumber) = 0; | ||
| 149 | virtual void deinitializeThread() = 0; | ||
| 150 | |||
| 151 | protected: | ||
| 152 | static MeasureTime * _instance; | ||
| 153 | static file_map _valuesToWrite; | ||
| 154 | |||
| 155 | MeasureTimeValues * _measuredOverhead; | ||
| 156 | |||
| 157 | MeasureTime(); | ||
| 158 | |||
| 159 | virtual MeasureTimeValues* getZeroValuesP() const = 0; | ||
| 160 | |||
| 161 | virtual void getTimeValuesStartP(MeasureTimeValues *res) const = 0; | ||
| 162 | virtual void getTimeValuesEndP(MeasureTimeValues *res) const = 0; | ||
| 163 | }; | ||
| 164 | |||
| 165 | #endif // MEASURE_TIME_HPP | ||
| 166 |