Changeset: cb0ef4ef40f9 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=cb0ef4ef40f9
Modified Files:
        gdk/gdk_system.mx
        gdk/gdk_utils.mx
Branch: default
Log Message:

Moved some functions around.
gdk_system is now completely independent from the rest of GDK, and the
only thing it contains is the low-level thread interface plus the
low-level timer interface.


diffs (truncated from 381 to 300 lines):

diff --git a/gdk/gdk_system.mx b/gdk/gdk_system.mx
--- a/gdk/gdk_system.mx
+++ b/gdk/gdk_system.mx
@@ -133,19 +133,9 @@
 
 #define MT_log(_impl, _object, _action, _caller, _fp) do { if (GDKdebug & 
1024) { fprintf(_fp, "%s: " _action "(" PTRFMT ")\n", _caller, 
PTRFMTCAST(void*) _object); fflush(_fp); } _impl; } while (0)
 
-/* virtual memory defines */
-gdk_export size_t _MT_npages;
-gdk_export size_t _MT_pagesize;
-
-#define MT_pagesize()  _MT_pagesize
-#define MT_npages()    _MT_npages
-@h
 /* API */
-gdk_export void MT_init(void); /*  init the package. */
 gdk_export int MT_alive(int pid);      /* OS independent way to check if some 
process is still alive. */
 
-#define MT_initialized() (MT_pagesize() > 0)
-
 @- MT Thread Api
 @h
 typedef size_t MT_Id;          /* thread number. will not be zero */
@@ -257,16 +247,10 @@
 
 gdk_export int MT_check_nr_cores(void);
 
-#endif /*_GDK_SYSTEM_H_*/
-
-
 @- Mthreads Routine implementations
 @c
 #include "monetdb_config.h"
-#include "gdk.h"
-
-size_t _MT_pagesize = 0;       /* variable holding memory size */
-size_t _MT_npages = 0;         /* variable holding page size */
+#include "gdk_system.h"
 
 MT_Lock MT_system_lock
 #ifdef PTHREAD_MUTEX_INITIALIZER
@@ -342,80 +326,6 @@
 }
 #endif /* MT_LOCK_TRACE */
 
-void
-MT_init(void)
-{
-#ifdef HAVE_GETSYSTEMINFO
-       {
-               SYSTEM_INFO sysInfo;
-
-               GetSystemInfo(&sysInfo);
-               _MT_pagesize = sysInfo.dwPageSize;
-       }
-#else
-# if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
-       _MT_pagesize = sysconf(_SC_PAGESIZE);
-# endif
-#endif
-       if (_MT_pagesize <= 0)
-               _MT_pagesize = 4096;    /* default */
-
-#ifdef HAVE_GLOBALMEMORYSTATUSEX
-       {
-               MEMORYSTATUSEX memStatEx;
-
-               memStatEx.dwLength = sizeof(memStatEx);
-               if (GlobalMemoryStatusEx(&memStatEx))
-                       _MT_npages = (size_t) (memStatEx.ullTotalPhys / 
_MT_pagesize);
-       }
-#else
-# ifdef HAVE_GLOBALMEMORYSTATUS
-       if (_MT_npages <= 0) {
-               MEMORYSTATUS memStat;
-
-               GlobalMemoryStatus(&memStat);
-               _MT_npages = memStat.dwTotalPhys / _MT_pagesize;
-       }
-# else
-#  if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES)
-       _MT_npages = sysconf(_SC_PHYS_PAGES);
-#  else
-#   if defined(HAVE_SYS_SYSCTL_H) && defined(HW_USERMEM)
-       {
-               uint64_t size;
-               size_t len = sizeof(size);
-
-               /* This is the 'non-kernel memory', e.g. what's left to a user
-                * process to allocate. */
-               sysctlbyname("hw.usermem", &size, &len, NULL, 0);
-               _MT_npages = size / _MT_pagesize;
-       }
-#   else
-#    error "don't know how to get the amount of physical memory for your OS"
-#   endif /* sysctl */
-#  endif /* sysconf */
-# endif        /* GlobalMemoryStatus */
-#endif /* GlobalMemoryStatusEx */
-#ifdef HAVE_GETRLIMIT
-       {
-               struct rlimit rl;
-               size_t memlim;
-
-               /* The environment can be limited memory wise.  In such case the
-                * physically available memory, is not necessarily what we can
-                * also use. */
-               getrlimit(RLIMIT_DATA, &rl);
-               if (rl.rlim_cur != (rlim_t) RLIM_INFINITY) {
-                       /* rlimit returns in bytes, recalculate */
-                       memlim = rl.rlim_cur / _MT_pagesize;
-                       /* if it's more restrictive, take that as value */
-                       if (memlim < _MT_npages)
-                               _MT_npages = memlim;
-               }
-       }
-#endif
-}
-
 #if !defined(HAVE_PTHREAD_H) && defined(_MSC_VER)
 static struct winthread {
        struct winthread *next;
@@ -900,4 +810,68 @@
        return ncpus;
 }
 
+
+@- Timers
+The following relative timers are available for inspection.
+Note that they may consume recognizable overhead.
+
+@h
+gdk_export lng GDKusec(void);
+gdk_export int GDKms(void);
+@c
+
+lng
+GDKusec(void)
+{
+       /* Return the time in milliseconds since an epoch.  The epoch
+          is roughly the time this program started. */
+#ifdef HAVE_QUERYPERFORMANCECOUNTER
+       static LARGE_INTEGER freq, start;       /* automatically initialized to 
0 */
+       LARGE_INTEGER ctr;
+
+       if (start.QuadPart == 0 &&
+           (!QueryPerformanceFrequency(&freq) ||
+            !QueryPerformanceCounter(&start)))
+               start.QuadPart = -1;
+       if (start.QuadPart > 0) {
+               QueryPerformanceCounter(&ctr);
+               return (lng) (((ctr.QuadPart - start.QuadPart) * 1000000) / 
freq.QuadPart);
+       }
+#endif
+#ifdef HAVE_GETTIMEOFDAY
+       {
+               static struct timeval tpbase;   /* automatically initialized to 
0 */
+               struct timeval tp;
+
+               if (tpbase.tv_sec == 0)
+                       gettimeofday(&tpbase, NULL);
+               gettimeofday(&tp, NULL);
+               tp.tv_sec -= tpbase.tv_sec;
+               return (lng) tp.tv_sec * 1000000 + (lng) tp.tv_usec;
+       }
+#else
+#ifdef HAVE_FTIME
+       {
+               static struct timeb tbbase;     /* automatically initialized to 
0 */
+               struct timeb tb;
+
+               if (tbbase.time == 0)
+                       ftime(&tbbase);
+               ftime(&tb);
+               tb.time -= tbbase.time;
+               return (lng) tb.time * 1000000 + (lng) tb.millitm * 1000;
+       }
+#endif
+#endif
+}
+
+
+int
+GDKms(void)
+{
+       return (int) (GDKusec() / 1000);
+}
+
+@h
+#endif /*_GDK_SYSTEM_H_*/
 @}
diff --git a/gdk/gdk_utils.mx b/gdk/gdk_utils.mx
--- a/gdk/gdk_utils.mx
+++ b/gdk/gdk_utils.mx
@@ -36,9 +36,6 @@
 gdk_export void GDKunlockHome(void);
 gdk_export int GDKgetHome(void);
 
-gdk_export lng GDKusec(void);
-gdk_export int GDKms(void);
-
 gdk_export BAT *GDKenv;
 
 @c
@@ -246,7 +243,7 @@
        int mustopen = GDKgetHome();
        time_t tm = time(0);
 
-       if (!MT_initialized())
+       if (MT_pagesize() == 0)
                return;
 
        va_start(ap, format);
@@ -453,6 +450,93 @@
 #define malloc_lock_init()
 #endif
 
+@h
+/* virtual memory defines */
+gdk_export size_t _MT_npages;
+gdk_export size_t _MT_pagesize;
+
+#define MT_pagesize()  _MT_pagesize
+#define MT_npages()    _MT_npages
+
+gdk_export void MT_init(void); /*  init the package. */
+@c
+size_t _MT_pagesize = 0;       /* variable holding memory size */
+size_t _MT_npages = 0;         /* variable holding page size */
+
+void
+MT_init(void)
+{
+#ifdef HAVE_GETSYSTEMINFO
+       {
+               SYSTEM_INFO sysInfo;
+
+               GetSystemInfo(&sysInfo);
+               _MT_pagesize = sysInfo.dwPageSize;
+       }
+#else
+# if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
+       _MT_pagesize = sysconf(_SC_PAGESIZE);
+# endif
+#endif
+       if (_MT_pagesize <= 0)
+               _MT_pagesize = 4096;    /* default */
+
+#ifdef HAVE_GLOBALMEMORYSTATUSEX
+       {
+               MEMORYSTATUSEX memStatEx;
+
+               memStatEx.dwLength = sizeof(memStatEx);
+               if (GlobalMemoryStatusEx(&memStatEx))
+                       _MT_npages = (size_t) (memStatEx.ullTotalPhys / 
_MT_pagesize);
+       }
+#else
+# ifdef HAVE_GLOBALMEMORYSTATUS
+       if (_MT_npages <= 0) {
+               MEMORYSTATUS memStat;
+
+               GlobalMemoryStatus(&memStat);
+               _MT_npages = memStat.dwTotalPhys / _MT_pagesize;
+       }
+# else
+#  if defined(HAVE_SYSCONF) && defined(_SC_PHYS_PAGES)
+       _MT_npages = sysconf(_SC_PHYS_PAGES);
+#  else
+#   if defined(HAVE_SYS_SYSCTL_H) && defined(HW_USERMEM)
+       {
+               uint64_t size;
+               size_t len = sizeof(size);
+
+               /* This is the 'non-kernel memory', e.g. what's left to a user
+                * process to allocate. */
+               sysctlbyname("hw.usermem", &size, &len, NULL, 0);
+               _MT_npages = size / _MT_pagesize;
+       }
+#   else
+#    error "don't know how to get the amount of physical memory for your OS"
+#   endif /* sysctl */
+#  endif /* sysconf */
+# endif        /* GlobalMemoryStatus */
+#endif /* GlobalMemoryStatusEx */
+#ifdef HAVE_GETRLIMIT
+       {
+               struct rlimit rl;
+               size_t memlim;
+
+               /* The environment can be limited memory wise.  In such case the
+                * physically available memory, is not necessarily what we can
+                * also use. */
+               getrlimit(RLIMIT_DATA, &rl);
+               if (rl.rlim_cur != (rlim_t) RLIM_INFINITY) {
+                       /* rlimit returns in bytes, recalculate */
+                       memlim = rl.rlim_cur / _MT_pagesize;
+                       /* if it's more restrictive, take that as value */
+                       if (memlim < _MT_npages)
+                               _MT_npages = memlim;
+               }
_______________________________________________
Checkin-list mailing list
[email protected]
http://mail.monetdb.org/mailman/listinfo/checkin-list

Reply via email to