Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 526b822

Browse files
sjoelundOpenModelica-Hudson
authored andcommitted
Improve reporting memory usage
Disabled Serializer.mo and wrote a dedicated memory usage counter. It also tries to account for GC overhead (allocating 24 bytes actually results in 32 bytes being allocated). Added -d=execstatGCcollect, which adds a GC.gcollect() and an extra notification for each execstat event so we can track how much memory is in use for each phase. This has a roughly 3x performance penalty as full GC is expensive. -d=reportSerializedSize is even slower though, and together they cause a 10x hit to performance. Belonging to [master]: - #2218
1 parent 9c759d9 commit 526b822

File tree

10 files changed

+130
-53
lines changed

10 files changed

+130
-53
lines changed

Compiler/SimCode/SimCodeMain.mo

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ import HpcOmTaskGraph;
9494
import SerializeModelInfo;
9595
import TaskSystemDump;
9696
import SerializeInitXML;
97-
import Serializer;
9897
import SimCodeDump;
9998
import SimCodeUtil;
10099
import StackOverflow;
@@ -276,7 +275,7 @@ algorithm
276275
ExecStat.execStat("SimCode");
277276

278277
if Flags.isSet(Flags.SERIALIZED_SIZE) then
279-
serializeNotify(simCode, filenamePrefix, "simCode");
278+
serializeNotify(simCode, "SimCode");
280279
ExecStat.execStat("Serialize simCode");
281280
end if;
282281

@@ -741,6 +740,7 @@ public function translateModel "
741740
output String outFileDir;
742741
output list<tuple<String, Values.Value>> resultValues;
743742
protected
743+
FCore.Cache inCache = cache;
744744
Boolean generateFunctions = false;
745745
Real timeSimCode=0.0, timeTemplates=0.0, timeBackend=0.0, timeFrontend=0.0;
746746
type State = enumeration(frontend, backend, templates, simcode);
@@ -777,10 +777,13 @@ algorithm
777777
SOME(dae) := odae;
778778

779779
if Flags.isSet(Flags.SERIALIZED_SIZE) then
780-
serializeNotify(dae, filenameprefix, "dae");
781-
serializeNotify(graph, filenameprefix, "graph");
782-
serializeNotify(cache, filenameprefix, "cache");
783-
serializeNotify(SymbolTable.get(), filenameprefix, "st");
780+
serializeNotify(dae, "FrontEnd DAE");
781+
serializeNotify(graph, "FCore.Graph");
782+
serializeNotify((graph,inEnv), "FCore.Graph + Old graph");
783+
serializeNotify(cache, "FCore.Cache");
784+
serializeNotify((cache,inCache), "FCore.Cache + Old cache");
785+
serializeNotify(SymbolTable.get(), "Symbol Table (Absyn and SCode)");
786+
serializeNotify((SymbolTable.get(),dae,graph,inEnv,cache,inCache), "Symbol Table, DAE, Graph, OldGraph, Cache, OldCache");
784787
ExecStat.execStat("Serialize FrontEnd");
785788
end if;
786789

@@ -792,7 +795,7 @@ algorithm
792795
ExecStat.execStat("Transformations before backend");
793796

794797
if Flags.isSet(Flags.SERIALIZED_SIZE) then
795-
serializeNotify(dae, filenameprefix, "dae2");
798+
serializeNotify(dae, "FrontEnd DAE after transformations");
796799
ExecStat.execStat("Serialize DAE (2)");
797800
end if;
798801

@@ -809,7 +812,7 @@ algorithm
809812
GC.free(dae);
810813

811814
if Flags.isSet(Flags.SERIALIZED_SIZE) then
812-
serializeNotify(dlow, filenameprefix, "dlow");
815+
serializeNotify(dlow, "BackendDAECreate.lower");
813816
ExecStat.execStat("Serialize dlow");
814817
end if;
815818

@@ -836,9 +839,11 @@ algorithm
836839
state := State.simcode;
837840

838841
if Flags.isSet(Flags.SERIALIZED_SIZE) then
839-
serializeNotify(dlow, filenameprefix, "simDAE");
840-
serializeNotify(initDAE, filenameprefix, "initDAE");
841-
serializeNotify(removedInitialEquationLst, filenameprefix, "removedInitialEquationLst");
842+
serializeNotify(dlow, "BackendDAE (simulation)");
843+
serializeNotify(initDAE, "BackendDAE (initialization)");
844+
serializeNotify(initDAE_lambda0, "BackendDAE (lambda0)");
845+
serializeNotify((dlow,initDAE,initDAE_lambda0), "BackendDAE (simulation+initialization+lambda0)");
846+
serializeNotify(removedInitialEquationLst, "removedInitialEquationLst");
842847
ExecStat.execStat("Serialize solved system");
843848
end if;
844849

@@ -941,9 +946,9 @@ algorithm
941946
ExecStat.execStat("FrontEnd");
942947

943948
if Flags.isSet(Flags.SERIALIZED_SIZE) then
944-
serializeNotify(dae, filenameprefix, "dae");
945-
serializeNotify(graph, filenameprefix, "graph");
946-
serializeNotify(outCache, filenameprefix, "cache");
949+
serializeNotify(dae, "dae");
950+
serializeNotify(graph, "graph");
951+
serializeNotify(outCache, "cache");
947952
ExecStat.execStat("Serialize FrontEnd");
948953
end if;
949954

@@ -954,7 +959,7 @@ algorithm
954959
ExecStat.execStat("Transformations before backend");
955960

956961
if Flags.isSet(Flags.SERIALIZED_SIZE) then
957-
serializeNotify(dae, filenameprefix, "dae2");
962+
serializeNotify(dae, "dae2");
958963
ExecStat.execStat("Serialize DAE (2)");
959964
end if;
960965

@@ -971,7 +976,7 @@ algorithm
971976
GC.free(dae);
972977

973978
if Flags.isSet(Flags.SERIALIZED_SIZE) then
974-
serializeNotify(dlow, filenameprefix, "dlow");
979+
serializeNotify(dlow, "dlow");
975980
ExecStat.execStat("Serialize dlow");
976981
end if;
977982

@@ -982,9 +987,9 @@ algorithm
982987
timeBackend := System.realtimeTock(ClockIndexes.RT_CLOCK_BACKEND);
983988

984989
if Flags.isSet(Flags.SERIALIZED_SIZE) then
985-
serializeNotify(bdae, filenameprefix, "simDAE");
986-
serializeNotify(initDAE, filenameprefix, "initDAE");
987-
serializeNotify(removedInitialEquationLst, filenameprefix, "removedInitialEquationLst");
990+
serializeNotify(bdae, "simDAE");
991+
serializeNotify(initDAE, "initDAE");
992+
serializeNotify(removedInitialEquationLst, "removedInitialEquationLst");
988993
ExecStat.execStat("Serialize solved system");
989994
end if;
990995

@@ -1275,7 +1280,7 @@ algorithm
12751280
ExecStat.execStat("SimCode");
12761281

12771282
if Flags.isSet(Flags.SERIALIZED_SIZE) then
1278-
serializeNotify(simCode, filenamePrefix, "simCode");
1283+
serializeNotify(simCode, "SimCode");
12791284
ExecStat.execStat("Serialize simCode");
12801285
end if;
12811286

@@ -1300,14 +1305,9 @@ end generateModelCodeDAE;
13001305

13011306
protected function serializeNotify<T>
13021307
input T data;
1303-
input String prefix;
13041308
input String name;
1305-
protected
1306-
Real fsize;
13071309
algorithm
1308-
Serializer.outputFile(data, prefix + "_"+name+".bin");
1309-
(,fsize,) := System.stat(prefix + "_"+name+".bin");
1310-
Error.addMessage(Error.SERIALIZED_SIZE, {name, StringUtil.bytesToReadableUnit(fsize)});
1310+
Error.addMessage(Error.SERIALIZED_SIZE, {name, StringUtil.bytesToReadableUnit(System.getSizeOfData(data))});
13111311
end serializeNotify;
13121312

13131313
annotation(__OpenModelica_Interface="backend");

Compiler/Util/Error.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,7 @@ public constant Message UNIONTYPE_MISSING_TYPEVARS = MESSAGE(5044, TRANSLATION()
10211021
public constant Message UNIONTYPE_WRONG_NUM_TYPEVARS = MESSAGE(5045, TRANSLATION(), ERROR(),
10221022
Util.gettext("Uniontype %s has %s type variables, but got %s."));
10231023
public constant Message SERIALIZED_SIZE = MESSAGE(5046, TRANSLATION(), NOTIFICATION(),
1024-
Util.gettext("%s has serialized size %s."));
1024+
Util.gettext("%s uses %s of memory."));
10251025

10261026
public constant Message COMPILER_ERROR = MESSAGE(5999, TRANSLATION(), ERROR(),
10271027
Util.notrans("%s"));

Compiler/Util/ExecStat.mo

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,33 @@ protected
3434
GC.ProfStats stats, oldStats;
3535
algorithm
3636
if Flags.isSet(Flags.EXEC_STAT) then
37-
(stats as GC.PROFSTATS(bytes_allocd_since_gc=since, allocd_bytes_before_gc=before, heapsize_full=heapsize_full, free_bytes_full=free_bytes_full)) := GC.getProfStats();
38-
memory := since+before;
39-
oldStats := getGlobalRoot(Global.gcProfilingIndex);
40-
GC.PROFSTATS(bytes_allocd_since_gc=since, allocd_bytes_before_gc=before) := oldStats;
41-
oldMemory := since+before;
42-
t := System.realtimeTock(ClockIndexes.RT_CLOCK_EXECSTAT);
43-
total := System.realtimeTock(ClockIndexes.RT_CLOCK_EXECSTAT_CUMULATIVE);
44-
timeStr := System.snprintff("%.4g", 20, t);
45-
totalTimeStr := System.snprintff("%.4g", 20, total);
46-
if Flags.isSet(Flags.GC_PROF) then
47-
gcStr := GC.profStatsStr(stats, head="", delimiter=" / ");
48-
Error.addMessage(Error.EXEC_STAT_GC, {name, timeStr, totalTimeStr, gcStr});
49-
else
50-
Error.addMessage(Error.EXEC_STAT, {name, timeStr, totalTimeStr,
51-
StringUtil.bytesToReadableUnit(memory-oldMemory, maxSizeInUnit=500, significantDigits=4),
52-
StringUtil.bytesToReadableUnit(memory, maxSizeInUnit=500, significantDigits=4),
53-
StringUtil.bytesToReadableUnit(free_bytes_full, maxSizeInUnit=500, significantDigits=4),
54-
StringUtil.bytesToReadableUnit(heapsize_full, maxSizeInUnit=500, significantDigits=4)
55-
});
56-
end if;
57-
System.realtimeTick(ClockIndexes.RT_CLOCK_EXECSTAT);
58-
setGlobalRoot(Global.gcProfilingIndex, stats);
37+
for i in if Flags.isSet(Flags.EXEC_STAT_EXTRA_GC) then {1,2} else {1} loop
38+
if i==2 then
39+
GC.gcollect();
40+
end if;
41+
(stats as GC.PROFSTATS(bytes_allocd_since_gc=since, allocd_bytes_before_gc=before, heapsize_full=heapsize_full, free_bytes_full=free_bytes_full)) := GC.getProfStats();
42+
memory := since+before;
43+
oldStats := getGlobalRoot(Global.gcProfilingIndex);
44+
GC.PROFSTATS(bytes_allocd_since_gc=since, allocd_bytes_before_gc=before) := oldStats;
45+
oldMemory := since+before;
46+
t := System.realtimeTock(ClockIndexes.RT_CLOCK_EXECSTAT);
47+
total := System.realtimeTock(ClockIndexes.RT_CLOCK_EXECSTAT_CUMULATIVE);
48+
timeStr := System.snprintff("%.4g", 20, t);
49+
totalTimeStr := System.snprintff("%.4g", 20, total);
50+
if Flags.isSet(Flags.GC_PROF) then
51+
gcStr := GC.profStatsStr(stats, head="", delimiter=" / ");
52+
Error.addMessage(Error.EXEC_STAT_GC, {name + (if i==2 then " GC" else ""), timeStr, totalTimeStr, gcStr});
53+
else
54+
Error.addMessage(Error.EXEC_STAT, {name + (if i==2 then " GC" else ""), timeStr, totalTimeStr,
55+
StringUtil.bytesToReadableUnit(memory-oldMemory, maxSizeInUnit=500, significantDigits=4),
56+
StringUtil.bytesToReadableUnit(memory, maxSizeInUnit=500, significantDigits=4),
57+
StringUtil.bytesToReadableUnit(free_bytes_full, maxSizeInUnit=500, significantDigits=4),
58+
StringUtil.bytesToReadableUnit(heapsize_full, maxSizeInUnit=500, significantDigits=4)
59+
});
60+
end if;
61+
System.realtimeTick(ClockIndexes.RT_CLOCK_EXECSTAT);
62+
setGlobalRoot(Global.gcProfilingIndex, stats);
63+
end for;
5964
end if;
6065
end execStat;
6166

Compiler/Util/Flags.mo

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,8 @@ constant DebugFlag SUSAN_MATCHCONTINUE_DEBUG = DEBUG_FLAG(175, "susanDebug", fal
526526
Util.gettext("Makes Susan generate code using try/else to better debug which function broke the expected match semantics."));
527527
constant DebugFlag OLD_FE_UNITCHECK = DEBUG_FLAG(176, "oldFrontEndUnitCheck", false,
528528
Util.gettext("Checks the consistency of units in equation (for the old front-end)."));
529+
constant DebugFlag EXEC_STAT_EXTRA_GC = DEBUG_FLAG(177, "execstatGCcollect", false,
530+
Util.gettext("When running execstat, also perform an extra full garbage collection."));
529531

530532
// This is a list of all debug flags, to keep track of which flags are used. A
531533
// flag can not be used unless it's in this list, and the list is checked at
@@ -708,7 +710,8 @@ constant list<DebugFlag> allDebugFlags = {
708710
IGNORE_CYCLES,
709711
ALIAS_CONFLICTS,
710712
SUSAN_MATCHCONTINUE_DEBUG,
711-
OLD_FE_UNITCHECK
713+
OLD_FE_UNITCHECK,
714+
EXEC_STAT_EXTRA_GC
712715
};
713716

714717
public

Compiler/Util/Serializer.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ encapsulated package Serializer
4141
This package provides functions to serialize MetaModelica data.
4242
The external C implementation is in TOP/Compiler/runtime/Serializer.c"
4343

44+
// Note: Reading back the data does not work as of 2018-02-19
4445

4546
public function outputFile<T> "
4647
Prints the structure of the object."

Compiler/Util/System.mo

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,5 +1301,14 @@ external "C" OpenModelica_updateUriMapping(OpenModelica.threadData(), namesAndDi
13011301
</html>"));
13021302
end updateUriMapping;
13031303

1304+
function getSizeOfData<T>
1305+
input T data;
1306+
output Real sz;
1307+
external "C" sz=SystemImpl__getSizeOfData(data) annotation(Library = {"omcruntime"}, Documentation(info="<html>
1308+
Counts the number of bytes that were allocated to hold the given data structure.
1309+
Includes constant data and handles cycles.
1310+
</html>"));
1311+
end getSizeOfData;
1312+
13041313
annotation(__OpenModelica_Interface="util");
13051314
end System;

Compiler/boot/LoadCompilerSources.mos

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,6 @@ if true then /* Suppress output */
395395
"../Util/HashTableStringToUnit.mo",
396396
"../Util/HashTableUnitToString.mo",
397397
"../Util/PriorityQueue.mo",
398-
"../Util/Serializer.mo",
399398
"../Util/SimulationResults.mo",
400399
"../Util/TaskGraphResults.mo"
401400
};

Compiler/runtime/Makefile.common

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ OMC_OBJ_STUBS = corbaimpl_stub_omc.o
3131

3232
OMC_OBJ_BOOT = $(OMC_OBJ_SHARED) $(OMC_OBJ_STUBS)
3333

34-
OMC_OBJ = $(OMC_OBJ_SHARED) serializer.o \
34+
OMC_OBJ = $(OMC_OBJ_SHARED) \
3535
ptolemyio_omc.o SimulationResults_omc.o \
3636
$(OMCCORBASRC)
3737

38+
# serializer.o # Disabled 2018-02-19; doesn't work to read back data
3839
# Database_omc.o
3940

4041
all: install

Compiler/runtime/systemimplmisc.cpp

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44

55
#include <string>
6+
#include <unordered_set>
7+
#include <stack>
68

79
using namespace std;
810

@@ -42,6 +44,61 @@ extern "C" {
4244
return res;
4345
}
4446

45-
}
47+
#define GC_GRANULE_BYTES (2*sizeof(void*))
4648

49+
static inline size_t actualByteSize(size_t sz)
50+
{
51+
/* GC uses 2 words as the minimum allocation unit: a granule */
52+
size_t res = GC_GRANULE_BYTES*((sz+GC_GRANULE_BYTES-1) / GC_GRANULE_BYTES);
53+
return res;
54+
}
55+
#include <stdio.h>
56+
double SystemImpl__getSizeOfData(void *data)
57+
{
58+
size_t sz=0;
59+
std::unordered_set<void*> handled;
60+
std::stack<void*> work;
61+
work.push(data);
62+
while (!work.empty()) {
63+
void *item = work.top();
64+
work.pop();
65+
if (handled.find(item) != handled.end()) {
66+
continue;
67+
}
68+
handled.insert(item);
69+
if (MMC_IS_IMMEDIATE(item)) {
70+
/* Uses up zero space */
71+
continue;
72+
}
73+
mmc_uint_t hdr = MMC_GETHDR(item);
74+
if (MMC_HDR_IS_FORWARD(hdr) || hdr==MMC_NILHDR || hdr==MMC_NONEHDR) {
75+
/* Uses up zero space */
76+
continue;
77+
}
78+
if (hdr==MMC_REALHDR) {
79+
sz += actualByteSize(sizeof(void*)+sizeof(double));
80+
continue;
81+
}
82+
if (MMC_HDRISSTRING(hdr)) {
83+
sz += actualByteSize(sizeof(void*)+MMC_STRLEN(item)+1);
84+
continue;
85+
}
86+
if (MMC_HDRISSTRUCT(hdr)) {
87+
mmc_uint_t slots = MMC_HDRSLOTS(hdr);
88+
mmc_uint_t ctor = MMC_HDRCTOR(hdr);
89+
sz += actualByteSize(sizeof(void*)*(slots+1));
90+
// Push the sub-objects to the stack
91+
for (int i = (ctor>=3 && ctor != MMC_ARRAY_TAG) ? 2 /* MM record description */ : 1; i <= slots; i++) {
92+
void *ptr = (MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(item), i)));
93+
work.push(ptr);
94+
}
95+
continue;
96+
}
97+
fprintf(stderr, "abort... bytes=%d num items=%d\n", sz, handled.size());
98+
printAny(item);
99+
abort();
100+
}
101+
return sz;
102+
}
47103

104+
}

Compiler/runtime/systemimplmisc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ extern "C" {
55

66
char* _replace(const char* source_str, const char* search_str, const char* replace_str);
77

8+
int SystemImpl__getSizeOfData(void *data);
9+
810
}
911

1012
#endif

0 commit comments

Comments
 (0)