Skip to content

Commit

Permalink
- Adding Compiler/runtime/rtclock.c; a part of the System module
Browse files Browse the repository at this point in the history
  - Contains functions for _accurate_ timing (the Linux timer reports a 1-nanosecond resolution)
- Added extra fields to the SimulationResult record returned by simulate()
  - totalTime, timeFrontend, timeBackend, timeCodegen, timeCompile, timeSimulation
  - The fields are disabled when running omc through rtest
- Added new flag to omc, --running-testsuite flag (note that you need to use -- --running-testsuite in order to prevent the RML runtime from eating the flag)


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@6075 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Sep 9, 2010
1 parent f83cc58 commit 7a43d4b
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 32 deletions.
92 changes: 66 additions & 26 deletions Compiler/CevalScript.mo
Expand Up @@ -94,34 +94,60 @@ protected import Util;
protected import ValuesUtil;
protected import XMLDump;

public constant Integer RT_CLOCK_SIMULATE_TOTAL = 8;
public constant Integer RT_CLOCK_SIMULATE_SIMULATION = 9;
public constant Integer RT_CLOCK_BUILD_MODEL = 10;

protected constant DAE.Type simulationResultType_rtest = (DAE.T_COMPLEX(ClassInf.RECORD(Absyn.IDENT("SimulationResult")),{
DAE.TYPES_VAR("resultFile",DAE.ATTR(false,false,SCode.RO(),SCode.VAR(),Absyn.BIDIR(),Absyn.UNSPECIFIED()),false,DAE.T_STRING_DEFAULT,DAE.UNBOUND(),NONE()),
DAE.TYPES_VAR("messages",DAE.ATTR(false,false,SCode.RO(),SCode.VAR(),Absyn.BIDIR(),Absyn.UNSPECIFIED()),false,DAE.T_STRING_DEFAULT,DAE.UNBOUND(),NONE())
},NONE,NONE),NONE);

protected constant DAE.Type simulationResultType_full = (DAE.T_COMPLEX(ClassInf.RECORD(Absyn.IDENT("SimulationResult")),{
DAE.TYPES_VAR("resultFile",DAE.ATTR(false,false,SCode.RO(),SCode.VAR(),Absyn.BIDIR(),Absyn.UNSPECIFIED()),false,DAE.T_STRING_DEFAULT,DAE.UNBOUND(),NONE()),
DAE.TYPES_VAR("messages",DAE.ATTR(false,false,SCode.RO(),SCode.VAR(),Absyn.BIDIR(),Absyn.UNSPECIFIED()),false,DAE.T_STRING_DEFAULT,DAE.UNBOUND(),NONE()),
DAE.TYPES_VAR("totalTime",DAE.ATTR(false,false,SCode.RO(),SCode.VAR(),Absyn.BIDIR(),Absyn.UNSPECIFIED()),false,DAE.T_REAL_DEFAULT,DAE.UNBOUND(),NONE())
},NONE,NONE),NONE);

public function getSimulationResultType
output DAE.Type t;
algorithm
t := simulationResultType_rtest;
t := Util.if_(RTOpts.getRunningTestsuite(), simulationResultType_rtest, simulationResultType_full);
end getSimulationResultType;

public function createSimulationResult
input String resultFile;
input String message;
input Real totalTime;
input Real timeFrontend;
input Real timeBackend;
input Real timeCodegen;
input Real timeCompile;
input Real timeSimulation;
output Values.Value res;
protected
list<Values.Value> vals;
list<String> fields;
algorithm
vals := Util.if_(RTOpts.getRunningTestsuite(), {}, {Values.REAL(totalTime),Values.REAL(timeFrontend),Values.REAL(timeBackend),Values.REAL(timeCodegen),Values.REAL(timeCompile),Values.REAL(timeSimulation)});
fields := Util.if_(RTOpts.getRunningTestsuite(), {}, {"totalTime","timeFrontend","timeBackend","timeCodegen","timeCompile","timeSimulation"});
res := Values.RECORD(Absyn.IDENT("SimulationResult"),
{Values.STRING(resultFile),Values.STRING(message)},
{"resultFile","messages"},-1);
Values.STRING(resultFile)::Values.STRING(message)::vals,
"resultFile"::"messages"::fields,-1);
end createSimulationResult;

public function createSimulationResultFailure
input String errorMessage;
input String message;
output Values.Value res;
protected
list<Values.Value> vals;
list<String> fields;
algorithm
vals := Util.if_(RTOpts.getRunningTestsuite(), {}, {Values.REAL(0.0)});
fields := Util.if_(RTOpts.getRunningTestsuite(), {}, {"totalTime"});
res := Values.RECORD(Absyn.IDENT("SimulationResult"),
{Values.STRING(""),Values.STRING(errorMessage)},
{"resultFile","messages"},-1);
Values.STRING("")::Values.STRING(message)::vals,
"resultFile"::"messages"::fields,-1);
end createSimulationResultFailure;

public function cevalInteractiveFunctions
Expand Down Expand Up @@ -170,7 +196,7 @@ algorithm
Absyn.ComponentRef cr_1;
Integer size,length,rest;
list<String> vars_1,vars_2,args;
Real t1,t2,time;
Real t1,t2,time,timeFrontend,timeBackend,timeCodegen,timeCompile;
Interactive.InteractiveStmts istmts;
Boolean bval;
Env.Cache cache;
Expand Down Expand Up @@ -307,7 +333,7 @@ algorithm
lstVarVal = iv,
compiledFunctions = cf)),msg)
equation
(cache,ret_val,st_1,_,_,_) = translateModel(cache,env, className, st, msg, filenameprefix,true);
(cache,ret_val,st_1,_,_,_,_,_) = translateModel(cache,env, className, st, msg, filenameprefix,true);
then
(cache,ret_val,st_1);

Expand Down Expand Up @@ -511,7 +537,7 @@ algorithm
lstVarVal = iv,
compiledFunctions = cf)),msg)
equation
(cache,executable,method_str,outputFormat_str,st,initfilename) = buildModel(cache,env, exp, st_1, msg);
(cache,executable,method_str,outputFormat_str,st,initfilename,_,_,_,_) = buildModel(cache,env, exp, st_1, msg);
then
(cache,ValuesUtil.makeArray({Values.STRING(executable),Values.STRING(initfilename)}),st);

Expand Down Expand Up @@ -564,22 +590,26 @@ algorithm
compiledFunctions = cf)),msg)
local String s1; Absyn.ComponentRef cr_name;
equation
(cache,executable,method_str,outputFormat_str,st,_) = buildModel(cache,env, exp, st_1, msg) "Build and simulate model" ;
System.realtimeTick(RT_CLOCK_SIMULATE_TOTAL);

(cache,executable,method_str,outputFormat_str,st,_,timeFrontend,timeBackend,timeCodegen,timeCompile) = buildModel(cache,env, exp, st_1, msg);

cit = winCitation();
pwd = System.pwd();
pd = System.pathDelimiter();
executableSuffixedExe = stringAppend(executable, System.getExeExt());
sim_call = Util.stringAppendList({cit,pwd,pd,executableSuffixedExe,cit," > output.log 2>&1"});
/* MathCore Version
executableSuffixedExe = Util.linuxDotSlash() +& executable +& ".exe";
cr_name = Absyn.pathToCref(className);
s1 = Absyn.componentRefStr(cr_name);
sim_call = Util.stringAppendList({cit,executableSuffixedExe,cit," > output_" , s1 , ".log 2>&1"});
*/
0 = System.systemCall(sim_call);
System.realtimeTick(RT_CLOCK_SIMULATE_SIMULATION);
0 = System.systemCall(sim_call);

result_file = Util.stringAppendList({executable,"_res.",outputFormat_str});
simValue = createSimulationResult(result_file, System.readFile("output.log"));
simValue = createSimulationResult(result_file, System.readFile("output.log"),
System.realtimeTock(RT_CLOCK_SIMULATE_TOTAL),
timeFrontend,
timeBackend,
timeCodegen,
timeCompile,
System.realtimeTock(RT_CLOCK_SIMULATE_SIMULATION));
newst = Interactive.addVarToSymboltable("currentSimulationResult", Values.STRING(result_file), DAE.T_STRING_DEFAULT, st);
then
(cache,simValue,newst);
Expand Down Expand Up @@ -2317,6 +2347,8 @@ public function translateModel "function translateModel
output DAELow.DAELow outDAELow;
output list<String> outStringLst;
output String outString;
output Real timeFrontend;
output Real timeBackend;
algorithm
(outCache,outValue,outInteractiveSymbolTable,outDAELow,outStringLst,outString):=
matchcontinue (inCache,inEnv,className,inInteractiveSymbolTable,inMsg,inExp,addDummy)
Expand All @@ -2332,10 +2364,10 @@ algorithm
String file_dir;
case (cache,env,className,st,msg,fileprefix,addDummy) /* mo file directory */
equation
(cache, outValMsg, st, indexed_dlow, libs, file_dir) =
(cache, outValMsg, st, indexed_dlow, libs, file_dir, timeFrontend, timeBackend) =
SimCode.translateModel(cache,env,className,st,msg,fileprefix,addDummy);
then
(cache,outValMsg,st,indexed_dlow,libs,file_dir);
(cache,outValMsg,st,indexed_dlow,libs,file_dir,timeFrontend,timeBackend);
end matchcontinue;
end translateModel;

Expand Down Expand Up @@ -2486,8 +2518,12 @@ public function buildModel "function buildModel
output String outputFormat_str;
output Interactive.InteractiveSymbolTable outInteractiveSymbolTable3;
output String outString4 "initFileName";
output Real timeFrontend;
output Real timeBackend;
output Real timeCodegen;
output Real timeCompile;
algorithm
(outCache,outString1,outString2,outputFormat_str,outInteractiveSymbolTable3,outString4):=
(outCache,outString1,outString2,outputFormat_str,outInteractiveSymbolTable3,outString4,timeFrontend,timeBackend,timeCodegen,timeCompile):=
matchcontinue (inCache,inEnv,inExp,inInteractiveSymbolTable,inMsg)
local
Values.Value ret_val;
Expand Down Expand Up @@ -2538,7 +2574,7 @@ algorithm
_ = System.cd(oldDir);
true = existFile;
then
(cache,filenameprefix,method_str,outputFormat_str,st2,init_filename);
(cache,filenameprefix,method_str,outputFormat_str,st2,init_filename,0.0,0.0,0.0,0.0);
// compile the model
case (cache,env,(exp as DAE.CALL(path = Absyn.IDENT(name = _),expLst = ({DAE.CODE(Absyn.C_TYPENAME(classname),_),starttime,stoptime,interval,tolerance,method,fileprefix,storeInTemp,noClean,options,outputFormat}))),(st_1 as Interactive.SYMBOLTABLE(ast = p,explodedAst = sp,instClsLst = ic,lstVarVal = iv,compiledFunctions = cf)),msg)
local Absyn.TimeStamp ts,ts2;
Expand All @@ -2552,7 +2588,7 @@ algorithm
(cache,Values.BOOL(cdToTemp),SOME(st)) = Ceval.ceval(cache,env, storeInTemp, true, SOME(st_1), NONE, msg);
oldDir = System.pwd();
changeToTempDirectory(cdToTemp);
(cache,ret_val,st,indexed_dlow_1,libs,file_dir) = translateModel(cache,env, classname, st_1, msg, fileprefix,true);
(cache,ret_val,st,indexed_dlow_1,libs,file_dir,timeFrontend,timeBackend) = translateModel(cache,env, classname, st_1, msg, fileprefix,true);
cname_str = Absyn.pathString(classname);
(cache,init_filename,starttime_r,stoptime_r,interval_r,tolerance_r,method_str,options_str,outputFormat_str) = calculateSimulationSettings(cache,env, exp, st, msg, cname_str);
(cache,filenameprefix) = extractFilePrefix(cache,env, fileprefix, st, msg);
Expand All @@ -2561,14 +2597,18 @@ algorithm
win1 = getWithinStatement(classname);
s3 = extractNoCleanCommand(noClean);
makefilename = generateMakefilename(filenameprefix);

timeCodegen = System.realtimeTock(RT_CLOCK_BUILD_MODEL);
System.realtimeTick(RT_CLOCK_BUILD_MODEL);
Debug.fprintln("dynload", "buildModel: about to compile model " +& filenameprefix +& ", " +& file_dir);
compileModel(filenameprefix, libs, file_dir, s3, method_str);
Debug.fprintln("dynload", "buildModel: Compiling done.");
_ = System.cd(oldDir);
p = setBuildTime(p,classname);
st2 = st;// Interactive.replaceSymbolTableProgram(st,p);
timeCompile = System.realtimeTock(RT_CLOCK_BUILD_MODEL);
then
(cache,filenameprefix,method_str,outputFormat_str,st2,init_filename);
(cache,filenameprefix,method_str,outputFormat_str,st2,init_filename,timeFrontend,timeBackend,timeCodegen,timeCompile);
case (_,_,_,_,_)
then
fail();
Expand Down Expand Up @@ -4044,7 +4084,7 @@ algorithm
(cache,Values.BOOL(cdToTemp),SOME(st)) = Ceval.ceval(cache,env, storeInTemp, true, SOME(st_1), NONE, msg);
oldDir = System.pwd();
changeToTempDirectory(cdToTemp);
(cache,ret_val,st,indexed_dlow_1,libs,file_dir) = translateModel(cache,env, classname, st_1, msg, fileprefix,true);
(cache,ret_val,st,indexed_dlow_1,libs,file_dir,_,_) = translateModel(cache,env, classname, st_1, msg, fileprefix,true);
cname_str = Absyn.pathString(classname);
(cache,init_filename,starttime_r,stoptime_r,interval_r,tolerance_r,method_str,options_str,outputFormat_str) = calculateSimulationSettings(cache,env, exp, st, msg, cname_str);
(cache,filenameprefix) = extractFilePrefix(cache,env, fileprefix, st, msg);
Expand Down
3 changes: 2 additions & 1 deletion Compiler/Makefile.common
Expand Up @@ -31,7 +31,8 @@ $(CORBAOBJ) \
$(srcdir)/modpar/libmodpar.a \
$(srcdir)/runtime/settingsimpl.o \
$(srcdir)/runtime/SimulationResults.o \
$(srcdir)/runtime/IOStreamExt.o
$(srcdir)/runtime/IOStreamExt.o \
$(srcdir)/runtime/rtclock.o

# The Susan-generated MO sources
SUSANMO= \
Expand Down
4 changes: 4 additions & 0 deletions Compiler/RTOpts.mo
Expand Up @@ -220,5 +220,9 @@ public function setShowAnnotations
external "C";
end setShowAnnotations;

public function getRunningTestsuite
output Boolean runningTestsuite;
end getRunningTestsuite;

end RTOpts;

11 changes: 9 additions & 2 deletions Compiler/SimCode.mo
Expand Up @@ -598,8 +598,10 @@ public function translateModel
output DAELow.DAELow outDAELow;
output list<String> outStringLst;
output String outString;
output Real timeFrontend;
output Real timeBackend;
algorithm
(outCache,outValue,outInteractiveSymbolTable,outDAELow,outStringLst,outString):=
(outCache,outValue,outInteractiveSymbolTable,outDAELow,outStringLst,outString,timeFrontend,timeBackend):=
matchcontinue (inCache,inEnv,className,inInteractiveSymbolTable,inMsg,inExp,addDummy)
local
String filenameprefix,file_dir;
Expand All @@ -621,10 +623,13 @@ algorithm
case (cache,env,className,(st as Interactive.SYMBOLTABLE(ast = p)),msg,fileprefix,addDummy)
equation
/* calculate stuff that we need to create SimCode data structure */
System.realtimeTick(CevalScript.RT_CLOCK_BUILD_MODEL);
(cache,Values.STRING(filenameprefix),SOME(_)) = Ceval.ceval(cache,env, fileprefix, true, SOME(st), NONE, msg);
ptot = Dependency.getTotalProgram(className,p);
p_1 = SCodeUtil.translateAbsyn2SCode(ptot);
(cache,env,_,dae) = Inst.instantiateClass(cache,InnerOuter.emptyInstHierarchy,p_1,className);
timeFrontend = System.realtimeTock(CevalScript.RT_CLOCK_BUILD_MODEL);
System.realtimeTick(CevalScript.RT_CLOCK_BUILD_MODEL);
dae = DAEUtil.transformationsBeforeBackend(dae);
dlow = DAELow.lower(dae, addDummy, true);
Debug.fprint("bltdump", "Lowered DAE:\n");
Expand All @@ -645,12 +650,14 @@ algorithm
Debug.fcall("bltdump", DAELow.dumpMatching, ass1);
Debug.fcall("bltdump", DAELow.dumpComponents, comps);
Debug.fprintln("dynload", "translateModel: Generating simulation code and functions.");
timeBackend = System.realtimeTock(CevalScript.RT_CLOCK_BUILD_MODEL);
System.realtimeTick(CevalScript.RT_CLOCK_BUILD_MODEL);
a_cref = Absyn.pathToCref(className);
file_dir = CevalScript.getFileDir(a_cref, p);
(indexed_dlow_1, libs) = generateModelCode(p_1, dae, indexed_dlow_1, className, filenameprefix,
file_dir, ass1, ass2, m, mT, comps);
then
(cache,Values.STRING("SimCode: The model has been translated"),st,indexed_dlow_1,libs,file_dir);
(cache,Values.STRING("SimCode: The model has been translated"),st,indexed_dlow_1,libs,file_dir,timeFrontend,timeBackend);
end matchcontinue;
end translateModel;

Expand Down
15 changes: 15 additions & 0 deletions Compiler/System.mo
Expand Up @@ -688,5 +688,20 @@ is more generic and does not contain a date."
external "C";
end configureCommandLine;

public function realtimeTick
"Tock returns the time since the last tock; undefined if tick was never called.
The clock index is 0-15. The function fails if the number is out of range."
input Integer clockIndex;
external "C";
end realtimeTick;

public function realtimeTock
"Tock returns the time since the last tock, undefined if tick was never called.
The clock index is 0-15. The function fails if the number is out of range."
input Integer clockIndex;
output Real outTime;
external "C";
end realtimeTock;

end System;

2 changes: 1 addition & 1 deletion Compiler/runtime/Makefile.in
Expand Up @@ -32,7 +32,7 @@ IDL = @IDLCMD@
CFLAGS += @CFLAGS@
CXXFLAGS = $(CFLAGS)
CPPFLAGS = -I$(RMLINCLUDE) -I$(top_builddir)/c_runtime -I$(srcdir) -I. $(CORBAINCL) -Ilpsolve
SRC = rtopts.c socketimpl.c printimpl.c systemimpl.c settingsimpl.c dynload.c SimulationResults.c IOStreamExt.c
SRC = rtopts.c socketimpl.c printimpl.c systemimpl.c settingsimpl.c dynload.c SimulationResults.c IOStreamExt.c rtclock.c

CPPSRC = unitparser.cpp unitparserext.cpp ptolemyio.cpp daeext.cpp ErrorMessage.cpp errorext.cpp optmanager.cpp systemimplmisc.cpp $(CORBASRC)
OBJ = $(SRC:.c=.o) $(CPPSRC:.cpp=.o) $(CPPSRC:.cc=.o)
Expand Down
90 changes: 90 additions & 0 deletions Compiler/runtime/rtclock.c
@@ -0,0 +1,90 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2010, Linköpings University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
* PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköpings University, either from the above address,
* from the URL: http://www.ida.liu.se/projects/OpenModelica
* and in the OpenModelica distribution.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/

#define NUM_RT_CLOCKS 16

#if defined(__MINGW32__) || defined(_MSC_VER)

LARGE_INTEGER performance_frequency;
LARGE_INTEGER tick_tp[NUM_RT_CLOCKS];

void rt_tick(int ix) {
static int init = 0;
if (!init) {
init = 1;
QueryPerformanceFrequency(&performance_frequency);
}
QueryPerformanceCounter(&tick_tp[ix]);
}

double rt_tock(int ix) {
LARGE_INTEGER tock_tp;
QueryPerformanceCounter(&tock_tp);
return ((double)(tock_tp.QuadPart - tick_tp[ix].QuadPart)) / (performance_frequency);
}

#elif defined(__APPLE_CC__)

#include <mach/mach_time.h>
#include <time.h>

uint64_t tick_tp[NUM_RT_CLOCKS];

void rt_tick(int ix) {
tick_tp[ix] = mach_absolute_time();
}

double rt_tock(int ix) {
uint64_t tock_tp = mach_absolute_time();
uint64_t nsec;
static mach_timebase_info_data_t info = {0,0};
if (info.denom == 0)
mach_timebase_info(&info);
uint64_t elapsednano = (tock_tp-tick_tp[ix]) * (info.numer / info.denom);
return elapsednano * 1e-9;
}

#else

#include <time.h>

struct timespec tick_tp[NUM_RT_CLOCKS];

void rt_tick(int ix) {
clock_gettime(CLOCK_MONOTONIC, &tick_tp[ix]);
}

double rt_tock(int ix) {
struct timespec tock_tp = {0,0};
clock_gettime(CLOCK_MONOTONIC, &tock_tp);
return (tock_tp.tv_sec - tick_tp[ix].tv_sec) + (tock_tp.tv_nsec - tick_tp[ix].tv_nsec)*1e-9;
}

#endif

0 comments on commit 7a43d4b

Please sign in to comment.