Skip to content

Commit

Permalink
Add functionality to record all mmc_alloc_words
Browse files Browse the repository at this point in the history
To use it: configure with CPPFLAGS=-DOMC_RECORD_ALLOC_WORDS and run
make OMC_EXTRA_FLAGS=-d=metaModelicaRecordAllocWords

Then in your mos-script, finish the script by calling exit(0) since we
usually call _exit(0), skipping the atexit handler. Note that your mos-
script must run with -n=1 since the code is not thread-safe.
  • Loading branch information
sjoelund authored and OpenModelica-Hudson committed Mar 24, 2016
1 parent ffcb0f7 commit 9b7aa44
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 9 deletions.
8 changes: 4 additions & 4 deletions Compiler/BackEnd/Tearing.mo
Expand Up @@ -2812,10 +2812,10 @@ algorithm

(_,potentialTVars,_) := List.intersection1OnTrue(potentialTVars,discreteVars,intEq);
(_,potentialTVars,_) := List.intersection1OnTrue(potentialTVars,tSel_never,intEq);
if listEmpty(potentialTVars) then
Error.addCompilerError("It is not possible to select a new tearing variable, because all left variables are discrete or have the attribute tearingSelect=never");
fail();
end if;
if listEmpty(potentialTVars) then
Error.addCompilerError("It is not possible to select a new tearing variable, because all left variables are discrete or have the attribute tearingSelect=never");
fail();
end if;
if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("\nNone of the variables from (3rd) is able to causalize an equation in the next step.\nNow consider ALL unassigned non-discrete variables without attribute tearingSelect=never as potential tVars.\n\n");
print("\n4th: "+ stringDelimitList(List.map(potentialTVars,intString),",")+"\n(All unassigned non-discrete variables without attribute 'never')\n\n");
Expand Down
6 changes: 5 additions & 1 deletion Compiler/Template/CodegenCFunctions.tpl
Expand Up @@ -4090,7 +4090,11 @@ end addRootsTempArray;

template modelicaLine(builtin.SourceInfo info)
::=
if boolOr(acceptMetaModelicaGrammar(), Flags.isSet(Flags.GEN_DEBUG_SYMBOLS)) then '/*#modelicaLine <%infoStr(info)%>*/<%\n%>'
if boolOr(acceptMetaModelicaGrammar(), Flags.isSet(Flags.GEN_DEBUG_SYMBOLS))
then (if Flags.isSet(OMC_RECORD_ALLOC_WORDS)
then '/*#modelicaLine <%infoStr(info)%>*/<%\n%>mmc_set_current_pos("<%infoStr(info)%>");<%\n%>'
else '/*#modelicaLine <%infoStr(info)%>*/<%\n%>'
)
end modelicaLine;

template endModelicaLine()
Expand Down
1 change: 1 addition & 0 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -3255,6 +3255,7 @@ package Flags
constant DebugFlag USEMPI;
constant DebugFlag RUNTIME_STATIC_LINKING;
constant DebugFlag HARDCODED_START_VALUES;
constant DebugFlag OMC_RECORD_ALLOC_WORDS;
constant ConfigFlag NUM_PROC;
constant ConfigFlag HPCOM_CODE;
constant ConfigFlag PROFILING_LEVEL;
Expand Down
5 changes: 4 additions & 1 deletion Compiler/Util/Flags.mo
Expand Up @@ -484,6 +484,8 @@ constant DebugFlag DUMP_LOOPS = DEBUG_FLAG(157, "dumpLoops", false,
Util.gettext("Dumps loop equation."));
constant DebugFlag SKIP_INPUT_OUTPUT_SYNTACTIC_SUGAR = DEBUG_FLAG(158, "skipInputOutputSyntacticSugar", false,
Util.gettext("Used when bootstrapping to preserve the input output parsing of the code output by the list command."));
constant DebugFlag OMC_RECORD_ALLOC_WORDS = DEBUG_FLAG(159, "metaModelicaRecordAllocWords", false,
Util.gettext("Instrument the source code to record memory allocations (requires run-time and generated files compiled with -DOMC_RECORD_ALLOC_WORDS)."));

// This is a list of all debug flags, to keep track of which flags are used. A
// flag can not be used unless it's in this list, and the list is checked at
Expand Down Expand Up @@ -648,7 +650,8 @@ constant list<DebugFlag> allDebugFlags = {
DISABLE_JACSCC,
FORCE_NLS_ANALYTIC_JACOBIAN,
DUMP_LOOPS,
SKIP_INPUT_OUTPUT_SYNTACTIC_SUGAR
SKIP_INPUT_OUTPUT_SYNTACTIC_SUGAR,
OMC_RECORD_ALLOC_WORDS
};

public
Expand Down
6 changes: 3 additions & 3 deletions Compiler/boot/Makefile.common
Expand Up @@ -60,7 +60,7 @@ bootstrap-from-tarball: $(PATCHES)
@echo "Bootstrapping phase 1/3 completed"
$(MAKE) -f $(defaultMakefileTarget) --no-print-directory clean OMC=$(BOOTSTRAP_OMC)
$(MAKE) -f $(defaultMakefileTarget) --no-print-directory runtime-depends OMBUILDDIR=$(OMBUILDDIR)
OPENMODELICA_BACKEND_STUBS=1 $(MAKE) -f $(defaultMakefileTarget) generate-files-in-steps OMC=$(BOOTSTRAP_OMC)
OPENMODELICA_BACKEND_STUBS=1 $(MAKE) -f $(defaultMakefileTarget) generate-files-in-steps OMC=$(BOOTSTRAP_OMC) OMC_EXTRA_FLAGS=
# Patch _main.c to avoid a new tarball
$(PATCH_SOURCES)
echo '#include "Patternm.h"' >> build/SimCodeFunctionUtil_includes.h
Expand All @@ -71,7 +71,7 @@ bootstrap-from-tarball: $(PATCHES)
$(MAKE) -f $(defaultMakefileTarget) clean OMC=$(BOOTSTRAP_OMC)
$(MAKE) -f $(defaultMakefileTarget) --no-print-directory -C $(TOP_DIR)/Compiler/Template OMC=$(BOOTSTRAP_OMC) clean
$(MAKE) -f $(defaultMakefileTarget) --no-print-directory -C $(TOP_DIR)/Compiler/Script OMC=$(BOOTSTRAP_OMC) clean
$(MAKE) -f $(defaultMakefileTarget) generate-files-in-steps OMC=$(BOOTSTRAP_OMC)
$(MAKE) -f $(defaultMakefileTarget) generate-files-in-steps OMC=$(BOOTSTRAP_OMC) "OMC_EXTRA_FLAGS=$(OMC_EXTRA_FLAGS)"
$(MAKE) -f $(defaultMakefileTarget) install INCLUDESOURCES=1 OMC=$(BOOTSTRAP_OMC)
@echo "Bootstrapping phase 3/3 completed"

Expand Down Expand Up @@ -147,7 +147,7 @@ endif
$(GEN_DIR)%.stamp:
@test ! -z "$^" || (echo Empty dependencies for $@ ; false)
@echo 'echo(false);files:={"'`echo $^ | sed 's/ /","/g'`'"};mainClass:=$$TypeName('`basename $@ | sed "s/[.]stamp//"`');runScript("CompileFile.mos");print("Something went horribly wrong for '$@'\\n");exit(1);' > $@.mos
$(OMC) -n=1 $@.mos
$(OMC) -n=1 $(OMC_EXTRA_FLAGS) $@.mos
@touch $@

$(GEN_DIR)_main.c:
Expand Down
1 change: 1 addition & 0 deletions Compiler/boot/Makefile.in
Expand Up @@ -22,6 +22,7 @@ CORBALIBS=@CORBALIBS@
ULIMIT_CMD=true
SHREXT=@DLLEXT@
OMC=@OMC@
OMC_EXTRA_FLAGS=
RPATH=@RPATH@
STATIC=@BOOTSTRAP_STATIC@
BOOTSTRAP_OMC=@OMBUILDDIR@/bin/omc
Expand Down
56 changes: 56 additions & 0 deletions SimulationRuntime/c/meta/gc/mmc_gc.c
Expand Up @@ -41,3 +41,59 @@

static mmc_GC_state_type x_mmc_GC_state;
mmc_GC_state_type *mmc_GC_state = &x_mmc_GC_state;

#if defined(OMC_RECORD_ALLOC_WORDS)
#include <stdlib.h>
#include <stdio.h>
#include "util/uthash.h"
#include <sys/types.h>
#include <unistd.h>

typedef struct {
const char *pos; /* key */
size_t count;
UT_hash_handle hh;
} word_ht;

static const char *curPos="<nofile>:0";
static word_ht *table=NULL;
static size_t totalAlloc=0;
static void print_words()
{
word_ht *entry, *tmp;
pid_t pid = getpid();
char str[50];
sprintf(str, "omc-memory.%d.txt", pid);
FILE *fout = fopen(str, "w");
HASH_ITER(hh, table, entry, tmp) {
fprintf(fout, "%ld: %s\n", (long) entry->count, entry->pos);
}
fprintf(fout, "%ld: Total alloc\n", (long) totalAlloc);
fclose(fout);
printf("*** Printed memory consumption to file: %s\n", str);
}

void mmc_record_alloc_words(size_t n)
{
static int init=0;
word_ht *entry;
totalAlloc += n;
if (!init) {
init=1;
atexit(print_words);
}
HASH_FIND_STR(table, curPos, entry);
if (entry) {
entry->count += n;
return;
}
entry = malloc(sizeof(word_ht));
entry->pos = curPos;
entry->count = n;
HASH_ADD_KEYPTR( hh, table, entry->pos, strlen(entry->pos), entry);
}
void mmc_set_current_pos(const char *pos)
{
curPos = pos;
}
#endif
11 changes: 11 additions & 0 deletions SimulationRuntime/c/meta/gc/mmc_gc.h
Expand Up @@ -91,11 +91,22 @@ static inline void mmc_GC_init_default(void)
#define mmc_GC_clear(void)
#define mmc_GC_collect(local_GC_state)

#if defined(OMC_RECORD_ALLOC_WORDS)
void mmc_record_alloc_words(size_t n);
void mmc_set_current_pos(const char *str);
#endif

static inline void* mmc_alloc_words_atomic(unsigned int nwords) {
#if defined(OMC_RECORD_ALLOC_WORDS)
mmc_record_alloc_words((nwords) * sizeof(void*));
#endif
return GC_MALLOC_ATOMIC((nwords) * sizeof(void*));
}

static inline void* mmc_alloc_words(unsigned int nwords) {
#if defined(OMC_RECORD_ALLOC_WORDS)
mmc_record_alloc_words((nwords) * sizeof(void*));
#endif
return GC_MALLOC((nwords) * sizeof(void*));
}

Expand Down

0 comments on commit 9b7aa44

Please sign in to comment.