#1 2019-02-19 12:29:38

witya
Member
Registered: 2015-09-25
Posts: 5

[patch] SynFPCCMemAligned for support jemalloc,tbbmalloc on windows

--- SynFPCCMemAligned.pa_    2019-02-19 17:09:00.794997800 +0700
+++ SynFPCCMemAligned.pas    2019-02-19 19:24:42.010596200 +0700
@@ -117,11 +117,14 @@

{$else}

+{$ifndef MSWINDOWS}
uses
   {$ifdef LINUXNOTBSD}
   cthreads, // as required by libraries - will also link needed glibc
   {$endif LINUXNOTBSD}
   dl;
+{$endif}
+

// late-binding API calls to the external malloc libraries

@@ -198,7 +201,11 @@
var
   OldMM: TMemoryManager;
   {$ifndef FPC_SYNCMEM}
-  lib: pointer;
+    {$ifdef MSWINDOWS}
+      lib:THandle;
+    {$else}
+      lib: pointer;
+    {$endif}
   {$endif FPC_SYNCMEM}

{$I-}
@@ -208,30 +215,59 @@
   //writeln('using glibc');
   {$else}
   {$ifdef FPC_SYNJEMALLOC} // jemalloc 3.6 seems slower, but maybe less fragmented
-  lib := dlopen('libjemalloc.so.1', RTLD_LAZY);
-  if lib <> nil then begin
-    pointer(@malloc)  := dlsym(lib, 'malloc');
-    pointer(@calloc)  := dlsym(lib, 'calloc');
-    pointer(@free)    := dlsym(lib, 'free');
-    pointer(@realloc) := dlsym(lib, 'realloc');
-    pointer(@msize)   := dlsym(lib, 'malloc_usable_size');
-    //writeln('using jemalloc');
-  end else
-    writeln(StdErr, dlerror, '  [apt-get install libjemalloc1]');
+    {$ifdef MSWINDOWS}
+    lib:=LoadLibrary('jemalloc.dll');
+    if lib = 0 then
+      writeln(StdErr, dlerror, '  [install jemalloc.dll]')
+    else begin
+      pointer(@malloc)  := GetProcAddress(lib, 'malloc');
+      pointer(@calloc)  := GetProcAddress(lib, 'calloc');
+      pointer(@free)    := GetProcAddress(lib, 'free');
+      pointer(@realloc) := GetProcAddress(lib, 'realloc');
+      pointer(@msize)   := GetProcAddress(lib, 'malloc_usable_size');
+      //writeln('using jemalloc');
+    end;
+    {$else}
+    lib := dlopen('libjemalloc.so.1', RTLD_LAZY);
+    if lib = nil then
+      writeln(StdErr, dlerror, '  [apt-get install libjemalloc1]')
+    else begin
+      pointer(@malloc)  := dlsym(lib, 'malloc');
+      pointer(@calloc)  := dlsym(lib, 'calloc');
+      pointer(@free)    := dlsym(lib, 'free');
+      pointer(@realloc) := dlsym(lib, 'realloc');
+      pointer(@msize)   := dlsym(lib, 'malloc_usable_size');
+      //writeln('using jemalloc');
+    end;
+    {$endif}
   {$else}
-  lib := dlopen('libtbbmalloc.so.2', RTLD_LAZY);
-  if lib = nil then
-   lib := dlopen('libtbbmalloc.so', RTLD_LAZY);
-  if lib = nil then
-    writeln(StdErr, dlerror, '  [apt-get install libtbb2]')
-  else begin
-    pointer(@malloc)  := dlsym(lib, 'scalable_malloc');
-    pointer(@calloc)  := dlsym(lib, 'scalable_calloc');
-    pointer(@free)    := dlsym(lib, 'scalable_free');
-    pointer(@realloc) := dlsym(lib, 'scalable_realloc');
-    pointer(@msize)   := dlsym(lib, 'scalable_msize');
+    {$ifdef MSWINDOWS}
+     lib:=LoadLibrary('tbbmalloc.dll');
+     if lib = 0 then
+       writeln(StdErr, dlerror, '  [install tbbmalloc.dll]')
+     else begin
+       pointer(@malloc)  := GetProcAddress(lib, 'scalable_malloc');
+       pointer(@calloc)  := GetProcAddress(lib, 'scalable_calloc');
+       pointer(@free)    := GetProcAddress(lib, 'scalable_free');
+       pointer(@realloc) := GetProcAddress(lib, 'scalable_realloc');
+       pointer(@msize)   := GetProcAddress(lib, 'scalable_msize');
+       //writeln('using Intel TBB');
+     end;
+    {$else}
+    lib := dlopen('libtbbmalloc.so.2', RTLD_LAZY);
+    if lib = nil then
+     lib := dlopen('libtbbmalloc.so', RTLD_LAZY);
+    if lib = nil then
+      writeln(StdErr, dlerror, '  [apt-get install libtbb2]')
+    else begin
+      pointer(@malloc)  := dlsym(lib, 'scalable_malloc');
+      pointer(@calloc)  := dlsym(lib, 'scalable_calloc');
+      pointer(@free)    := dlsym(lib, 'scalable_free');
+      pointer(@realloc) := dlsym(lib, 'scalable_realloc');
+      pointer(@msize)   := dlsym(lib, 'scalable_msize');
     //writeln('using Intel TBB');
-  end;
+    end;
+    {$endif}
   {$endif FPC_SYNJEMALLOC}
   {$endif FPC_SYNCMEM}
   if pointer(@msize) <> nil then begin
@@ -253,8 +289,13 @@
   if pointer(@msize) <> nil then begin
     SetMemoryManager(OldMM);
     {$ifndef FPC_SYNCMEM}
-    if lib <> nil then
-      dlclose(lib);
+      {$ifdef MSWINDOWS}
+      if lib <> 0 then
+        FreeLibrary(lib);
+      {$else}
+      if lib <> nil then
+        dlclose(lib);
+      {$endif}
     {$endif FPC_SYNCMEM}
   end;
end.

Offline

#2 2019-02-19 15:22:37

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: [patch] SynFPCCMemAligned for support jemalloc,tbbmalloc on windows

Please don't post code in the forum.
See https://synopse.info/forum/misc.php?action=rules

Please use pull requests in https://github.com/synopse/mORMot/pulls

Offline

#3 2019-02-19 15:24:34

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,242
Website

Re: [patch] SynFPCCMemAligned for support jemalloc,tbbmalloc on windows

BTW, from our tests Intel TBB is very fast, especially on micro benchmarks, but on our typical high-loaded servers, it consumes too much memory.

For instance, it is only 20% faster than glibc but RSS process is 70 times more - unusable in practice.
Yes, 70 times is no typo, meaning for 1GB of typical RSS RAM consumed, it is 70GB...

Offline

#4 2019-02-20 00:16:54

witya
Member
Registered: 2015-09-25
Posts: 5

Re: [patch] SynFPCCMemAligned for support jemalloc,tbbmalloc on windows

OK ,Thank you.

Offline

Board footer

Powered by FluxBB