Skip to content

Commit

Permalink
Bootstrapping fixes for OSX and low-memory targets
Browse files Browse the repository at this point in the history
- Added API call to find out system memory size


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@16910 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Aug 26, 2013
1 parent b7a57ca commit 05a82d6
Show file tree
Hide file tree
Showing 18 changed files with 374 additions and 157 deletions.
9 changes: 9 additions & 0 deletions Compiler/FrontEnd/ModelicaBuiltin.mo
Original file line number Diff line number Diff line change
Expand Up @@ -2840,6 +2840,15 @@ annotation(
</html>"));
end exit;

function getMemorySize
output Real memory(unit="MiB");
external "builtin";
annotation(
Documentation(info="<html>
<p>Retrieves the physical memory size available on the system in megabytes.</p>
</html>"));
end getMemorySize;

annotation(preferredView="text");
end Scripting;

Expand Down
10 changes: 9 additions & 1 deletion Compiler/Script/CevalScript.mo
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,7 @@ algorithm
Absyn.ComponentRef cr_1;
Integer size,resI,i,n, curveStyle;
Integer fmiContext, fmiInstance, fmiModelVariablesInstance, fmiLogLevel;
list<Integer> is;
list<FMI.ModelVariables> fmiModelVariablesList, fmiModelVariablesList1;
FMI.ExperimentAnnotation fmiExperimentAnnotation;
FMI.Info fmiInfo;
Expand Down Expand Up @@ -2983,7 +2984,8 @@ algorithm
equation
strs = List.map(vals,ValuesUtil.extractValueString);
strs = List.map1r(strs, stringAppend, stringAppend(Settings.getInstallationDirectoryPath(),"/bin/omc "));
v = ValuesUtil.makeArray(List.map(List.map1(System.systemCallParallel(strs),intEq,0), ValuesUtil.makeBoolean));
is = System.systemCallParallel(strs);
v = ValuesUtil.makeArray(List.map(List.map1(is,intEq,0), ValuesUtil.makeBoolean));
then (cache,v,st);

case (cache,env,"runScriptParallel",{Values.ARRAY(valueLst=vals),_},st,_)
Expand All @@ -2997,6 +2999,12 @@ algorithm
/* Cannot reach here */
then fail();

case (cache,env,"getMemorySize",{},st,_)
equation
r = System.getMemorySize();
v = Values.REAL(r);
then (cache,v,st);

end matchcontinue;
end cevalInteractiveFunctions2;

Expand Down
5 changes: 1 addition & 4 deletions Compiler/Template/CodegenC.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -9753,10 +9753,7 @@ int main(int argc, char **argv)
{
OpenModelica_ExternalC_allocation_function = mmc_mk_scon_len_ret_ptr;
init_metamodelica_segv_handler();
if (!mmc_GC_state)
{
mmc_GC_init(mmc_GC_settings_default);
}
mmc_GC_init(mmc_GC_settings_default);
{
mmc_GC_local_state_type local_GC_state = mmc_GC_save_roots_state("top"); /* push the first mark */
Expand Down
5 changes: 5 additions & 0 deletions Compiler/Util/System.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1043,4 +1043,9 @@ public function exit "Exits the compiler at this point with the given exit statu
external "C" exit(status);
end exit;

public function getMemorySize
output Real memory(unit="MB");
external "C" memory=System_getMemorySize() annotation(Library = {"omcruntime"});
end getMemorySize;

end System;
3 changes: 2 additions & 1 deletion Compiler/runtime/Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ OMC_OBJ = Error_omc.o Print_omc.o System_omc.o Settings_omc.o \
IOStreamExt_omc.o ErrorMessage.o FMI_omc.o systemimplmisc.o \
UnitParserExt_omc.o unitparser.o BackendDAEEXT_omc.o Socket_omc.o matching.o matching_cheap.o \
Database_omc.o Dynload_omc.o SimulationResults_omc.o TaskGraphResults_omc.o HpcOmBenchmarkExt_omc.o ptolemyio_omc.o \
Lapack_omc.o $(OMCCORBASRC)
Lapack_omc.o getMemorySize.o $(OMCCORBASRC)

all: runtime.a install
.PHONY: all install
Expand Down Expand Up @@ -91,6 +91,7 @@ ErrorMessage.o : ErrorMessage.cpp ErrorMessage.hpp errorext.h
FMI_rml.o : FMI_rml.c FMIImpl.c
FMI_omc.o : FMIImpl.c ../OpenModelicaBootstrappingHeader.h
ModelicaExternalC_rml.o : $(SimRuntimeCDir)/../meta/meta_modelica.h
getMemorySize_rml.o: getMemorySize_rml.c getMemorySize.c

clean:
$(RM) -rf *.a *.o omc_communication.cc omc_communication.h omc_communication-*
6 changes: 5 additions & 1 deletion Compiler/runtime/System_omc.c
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,8 @@ extern int System_fileIsNewerThan(const char *file1, const char *file2)

extern int System_forkAvailable()
{
#if defined(__MINGW32__) || defined(_MSC_VER)
/* On OSX, we fail with thread_suspend error if we fork() omc */
#if defined(__MINGW32__) || defined(_MSC_VER) || defined(__APPLE_CC__)
return 0;
#else
return 1;
Expand All @@ -719,6 +720,9 @@ extern void* System_forkCall(void *dataLst, void (*fn)(void*))
#if defined(__MINGW32__) || defined(_MSC_VER)
c_add_message(-1,ErrorType_scripting,ErrorLevel_error,gettext("Fork is not available on Windows"),NULL,0);
MMC_THROW();
#elif defined(__APPLE_CC__)
c_add_message(-1,ErrorType_scripting,ErrorLevel_error,gettext("Fork is not safe to use on OSX"),NULL,0);
MMC_THROW();
#else
int len = listLength(dataLst);
pid_t pids[len];
Expand Down
8 changes: 8 additions & 0 deletions Compiler/runtime/System_rml.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "rml.h"
#include "Config.h"
#include "systemimpl.c"
#include "getMemorySize.c"
#include <float.h>
#include <ctype.h>
#include <limits.h>
Expand Down Expand Up @@ -2215,3 +2216,10 @@ RML_BEGIN_LABEL(System__exit)
RML_TAILCALLK(rmlFC);
}
RML_END_LABEL

RML_BEGIN_LABEL(System__getMemorySize)
{
rmlA0 = mk_rcon(System_getMemorySize());
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL
104 changes: 104 additions & 0 deletions Compiler/runtime/getMemorySize.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Author: David Robert Nadeau
* Site: http://NadeauSoftware.com/
* Url: http://nadeausoftware.com/articles/2012/09/c_c_tip_how_get_physical_memory_size_system
* http://nadeausoftware.com/sites/NadeauSoftware.com/files/getMemorySize.c
* License: Creative Commons Attribution 3.0 Unported License
* http://creativecommons.org/licenses/by/3.0/deed.en_US
*/

#if defined(_WIN32)
#include <Windows.h>

#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#include <sys/types.h>
#include <sys/param.h>
#if defined(BSD)
#include <sys/sysctl.h>
#endif

#else
#error "Unable to define getMemorySize( ) for an unknown OS."
#endif



/**
* Returns the size of physical memory (RAM) in bytes.
*/
static size_t getMemorySizeBytes( )
{
#if defined(_WIN32) && (defined(__CYGWIN__) || defined(__CYGWIN32__))
/* Cygwin under Windows. ------------------------------------ */
/* New 64-bit MEMORYSTATUSEX isn't available. Use old 32.bit */
MEMORYSTATUS status;
status.dwLength = sizeof(status);
GlobalMemoryStatus( &status );
return (size_t)status.dwTotalPhys;

#elif defined(_WIN32)
/* Windows. ------------------------------------------------- */
/* Use new 64-bit MEMORYSTATUSEX, not old 32-bit MEMORYSTATUS */
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx( &status );
return (size_t)status.ullTotalPhys;

#elif defined(__unix__) || defined(__unix) || defined(unix) || (defined(__APPLE__) && defined(__MACH__))
/* UNIX variants. ------------------------------------------- */
/* Prefer sysctl() over sysconf() except sysctl() HW_REALMEM and HW_PHYSMEM */

#if defined(CTL_HW) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM64))
int mib[2];
mib[0] = CTL_HW;
#if defined(HW_MEMSIZE)
mib[1] = HW_MEMSIZE; /* OSX. --------------------- */
#elif defined(HW_PHYSMEM64)
mib[1] = HW_PHYSMEM64; /* NetBSD, OpenBSD. --------- */
#endif
int64_t size = 0; /* 64-bit */
size_t len = sizeof( size );
if ( sysctl( mib, 2, &size, &len, NULL, 0 ) == 0 )
return (size_t)size;
return 0L; /* Failed? */

#elif defined(_SC_AIX_REALMEM)
/* AIX. ----------------------------------------------------- */
return (size_t)sysconf( _SC_AIX_REALMEM ) * (size_t)1024L;

#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGESIZE)
/* FreeBSD, Linux, OpenBSD, and Solaris. -------------------- */
return (size_t)sysconf( _SC_PHYS_PAGES ) *
(size_t)sysconf( _SC_PAGESIZE );

#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGE_SIZE)
/* Legacy. -------------------------------------------------- */
return (size_t)sysconf( _SC_PHYS_PAGES ) *
(size_t)sysconf( _SC_PAGE_SIZE );

#elif defined(CTL_HW) && (defined(HW_PHYSMEM) || defined(HW_REALMEM))
/* DragonFly BSD, FreeBSD, NetBSD, OpenBSD, and OSX. -------- */
int mib[2];
mib[0] = CTL_HW;
#if defined(HW_REALMEM)
mib[1] = HW_REALMEM; /* FreeBSD. ----------------- */
#elif defined(HW_PYSMEM)
mib[1] = HW_PHYSMEM; /* Others. ------------------ */
#endif
unsigned int size = 0; /* 32-bit */
size_t len = sizeof( size );
if ( sysctl( mib, 2, &size, &len, NULL, 0 ) == 0 )
return (size_t)size;
return 0L; /* Failed? */
#endif /* sysctl and sysconf variants */

#else
return 0L; /* Unknown OS. */
#endif
}

/* Size in MB; as a double */
double System_getMemorySize() {
return getMemorySizeBytes() / (1048576.0);
}
59 changes: 27 additions & 32 deletions Compiler/runtime/systemimpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,32 @@ int SystemImpl__systemCall(const char* str)
return ret_val;
}

#ifdef WITH_HWLOC
#include <hwloc.h>
#endif

int System_numProcessors()
{
#ifdef WITH_HWLOC
hwloc_topology_t topology;
hwloc_topology_init(&topology);
hwloc_topology_load(topology);
int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_CORE);
if(depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
int res = hwloc_get_nbobjs_by_depth(topology, depth);
hwloc_topology_destroy(topology);
return res;
}
#endif
#if defined(__MINGW32__) || defined(_MSC_VER)
SYSTEM_INFO sysinfo;
GetSystemInfo( &sysinfo );
return sysinfo.dwNumberOfProcessors;
#else
return max(sysconf(_SC_NPROCESSORS_ONLN), 1);
#endif
}

void* SystemImpl__systemCallParallel(void *lst)
{
void *tmp = lst;
Expand All @@ -615,12 +641,7 @@ void* SystemImpl__systemCallParallel(void *lst)
#pragma omp parallel for private(i) schedule(dynamic)
for (i=0; i<sz; i++) {
/* fprintf(stderr, "Starting call %s\n", calls[i]); */
#if defined(__MINGW32__) || defined(_MSC_VER)
results[i] = system(calls[i]);
#else
results[i] = system(calls[i]);
results[i] = WEXITSTATUS(results[i]);
#endif
results[i] = SystemImpl__systemCall(calls[i]);
/* fprintf(stderr, "Finished call %d %s=%d\n", i, calls[i], results[i]); */
}
tmp = mk_nil();
Expand Down Expand Up @@ -2194,32 +2215,6 @@ int SystemImpl__fileIsNewerThan(const char *file1, const char *file2)
#endif
}

#ifdef WITH_HWLOC
#include <hwloc.h>
#endif

int System_numProcessors()
{
#ifdef WITH_HWLOC
hwloc_topology_t topology;
hwloc_topology_init(&topology);
hwloc_topology_load(topology);
int depth = hwloc_get_type_depth(topology, HWLOC_OBJ_CORE);
if(depth != HWLOC_TYPE_DEPTH_UNKNOWN) {
int res = hwloc_get_nbobjs_by_depth(topology, depth);
hwloc_topology_destroy(topology);
return res;
}
#endif
#if defined(__MINGW32__) || defined(_MSC_VER)
SYSTEM_INFO sysinfo;
GetSystemInfo( &sysinfo );
return sysinfo.dwNumberOfProcessors;
#else
return max(sysconf(_SC_NPROCESSORS_ONLN), 1);
#endif
}

#ifdef __cplusplus
}
#endif
39 changes: 19 additions & 20 deletions SimulationRuntime/c/meta/meta_modelica.c
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Linköping University,
* Department of Computer and Information Science,
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
* GPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the OSMC (Open Source Modelica Consortium)
* Public License (OSMC-PL) are obtained from OSMC, either from the above
* address, from the URLs: http://www.openmodelica.org or
* http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica
* distribution. GNU version 3 is obtained from:
* http://www.gnu.org/copyleft/gpl.html. The New BSD License is obtained from:
* http://www.opensource.org/licenses/BSD-3-Clause.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* 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.
* 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.
*
*/

Expand Down

0 comments on commit 05a82d6

Please sign in to comment.