Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
/ druntime Public archive
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/core-memory-gc-profile-stats.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Added `GC.profileStats()` to `core.memory`

Allows access to current GC profiling information.
See $(REF GC.ProfileStats, core,memory) for a list of profile stats.
14 changes: 7 additions & 7 deletions mak/COPY
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ COPY=\
\
$(IMPDIR)\core\sys\freebsd\netinet\in_.d \
\
$(IMPDIR)\core\sys\freebsd\sys\cdefs.d \
$(IMPDIR)\core\sys\freebsd\pthread_np.d \
$(IMPDIR)\core\sys\freebsd\sys\_bitset.d \
$(IMPDIR)\core\sys\freebsd\sys\_cpuset.d \
$(IMPDIR)\core\sys\freebsd\sys\cdefs.d \
$(IMPDIR)\core\sys\freebsd\sys\elf_common.d \
$(IMPDIR)\core\sys\freebsd\sys\elf.d \
$(IMPDIR)\core\sys\freebsd\sys\elf32.d \
$(IMPDIR)\core\sys\freebsd\sys\elf64.d \
$(IMPDIR)\core\sys\freebsd\sys\elf_common.d \
$(IMPDIR)\core\sys\freebsd\sys\event.d \
$(IMPDIR)\core\sys\freebsd\sys\link_elf.d \
$(IMPDIR)\core\sys\freebsd\sys\mount.d \
$(IMPDIR)\core\sys\freebsd\sys\mman.d \
$(IMPDIR)\core\sys\freebsd\sys\mount.d \
$(IMPDIR)\core\sys\freebsd\time.d \
$(IMPDIR)\core\sys\freebsd\unistd.d \
\
Expand All @@ -101,14 +101,14 @@ COPY=\
\
$(IMPDIR)\core\sys\dragonflybsd\netinet\in_.d \
\
$(IMPDIR)\core\sys\dragonflybsd\sys\cdefs.d \
$(IMPDIR)\core\sys\dragonflybsd\pthread_np.d \
$(IMPDIR)\core\sys\dragonflybsd\sys\_bitset.d \
$(IMPDIR)\core\sys\dragonflybsd\sys\_cpuset.d \
$(IMPDIR)\core\sys\dragonflybsd\sys\cdefs.d \
$(IMPDIR)\core\sys\dragonflybsd\sys\elf_common.d \
$(IMPDIR)\core\sys\dragonflybsd\sys\elf.d \
$(IMPDIR)\core\sys\dragonflybsd\sys\elf32.d \
$(IMPDIR)\core\sys\dragonflybsd\sys\elf64.d \
$(IMPDIR)\core\sys\dragonflybsd\sys\elf_common.d \
$(IMPDIR)\core\sys\dragonflybsd\sys\event.d \
$(IMPDIR)\core\sys\dragonflybsd\sys\link_elf.d \
$(IMPDIR)\core\sys\dragonflybsd\sys\mman.d \
Expand Down Expand Up @@ -197,8 +197,8 @@ COPY=\
$(IMPDIR)\core\sys\posix\sys\types.d \
$(IMPDIR)\core\sys\posix\sys\uio.d \
$(IMPDIR)\core\sys\posix\sys\un.d \
$(IMPDIR)\core\sys\posix\sys\wait.d \
$(IMPDIR)\core\sys\posix\sys\utsname.d \
$(IMPDIR)\core\sys\posix\sys\wait.d \
\
$(IMPDIR)\core\sys\solaris\dlfcn.d \
$(IMPDIR)\core\sys\solaris\elf.d \
Expand All @@ -213,9 +213,9 @@ COPY=\
$(IMPDIR)\core\sys\solaris\sys\elf_SPARC.d \
$(IMPDIR)\core\sys\solaris\sys\elftypes.d \
$(IMPDIR)\core\sys\solaris\sys\link.d \
$(IMPDIR)\core\sys\solaris\sys\priocntl.d \
$(IMPDIR)\core\sys\solaris\sys\procset.d \
$(IMPDIR)\core\sys\solaris\sys\types.d \
$(IMPDIR)\core\sys\solaris\sys\priocntl.d \
\
$(IMPDIR)\core\sys\windows\accctrl.d \
$(IMPDIR)\core\sys\windows\aclapi.d \
Expand Down
84 changes: 78 additions & 6 deletions mak/SRCS
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ SRCS=\
src\core\stdc\stdio.d \
src\core\stdc\stdlib.d \
src\core\stdc\string.d \
src\core\stdc\tgmath.d \
src\core\stdc\time.d \
src\core\stdc\wchar_.d \
src\core\stdc\wctype.d \
\
src\core\stdcpp\array.d \
src\core\stdcpp\string_view.d \
Expand Down Expand Up @@ -74,9 +76,14 @@ SRCS=\
src\core\sys\darwin\mach\thread_act.d \
src\core\sys\darwin\netinet\in_.d \
\
src\core\sys\darwin\sys\cdefs.d \
src\core\sys\darwin\sys\event.d \
src\core\sys\darwin\sys\mman.d \
\
src\core\sys\freebsd\dlfcn.d \
src\core\sys\freebsd\execinfo.d \
src\core\sys\freebsd\netinet\in_.d \
src\core\sys\freebsd\pthread_np.d \
src\core\sys\freebsd\sys\_bitset.d \
src\core\sys\freebsd\sys\_cpuset.d \
src\core\sys\freebsd\sys\cdefs.d \
Expand All @@ -94,6 +101,7 @@ SRCS=\
src\core\sys\dragonflybsd\dlfcn.d \
src\core\sys\dragonflybsd\execinfo.d \
src\core\sys\dragonflybsd\netinet\in_.d \
src\core\sys\dragonflybsd\pthread_np.d \
src\core\sys\dragonflybsd\sys\_bitset.d \
src\core\sys\dragonflybsd\sys\_cpuset.d \
src\core\sys\dragonflybsd\sys\cdefs.d \
Expand All @@ -106,25 +114,75 @@ SRCS=\
src\core\sys\dragonflybsd\sys\mman.d \
src\core\sys\dragonflybsd\time.d \
\
src\core\sys\linux\config.d \
src\core\sys\linux\dlfcn.d \
src\core\sys\linux\elf.d \
src\core\sys\linux\epoll.d \
src\core\sys\linux\errno.d \
src\core\sys\linux\execinfo.d \
src\core\sys\linux\fcntl.d \
src\core\sys\linux\ifaddrs.d \
src\core\sys\linux\link.d \
src\core\sys\linux\sched.d \
src\core\sys\linux\termios.d \
src\core\sys\linux\time.d \
src\core\sys\linux\timerfd.d \
src\core\sys\linux\tipc.d \
src\core\sys\linux\unistd.d \
\
src\core\sys\linux\netinet\in_.d \
src\core\sys\linux\netinet\tcp.d \
src\core\sys\linux\stdio.d \
src\core\sys\linux\tipc.d \
src\core\sys\linux\sys\netinet\tcp.d \
\
src\core\sys\linux\sys\auxv.d \
src\core\sys\linux\sys\eventfd.d \
src\core\sys\linux\sys\file.d \
src\core\sys\linux\sys\inotify.d \
src\core\sys\linux\sys\mman.d \
src\core\sys\linux\sys\signalfd.d \
src\core\sys\linux\sys\socket.d \
src\core\sys\linux\sys\sysinfo.d \
src\core\sys\linux\sys\time.d \
src\core\sys\linux\sys\xattr.d \
src\core\sys\linux\sys\time.d \
src\core\sys\linux\sys\prctl.d \
\
src\core\sys\openbsd\dlfcn.d \
\
src\core\sys\posix\arpa\inet.d \
src\core\sys\posix\aio.d \
src\core\sys\posix\config.d \
src\core\sys\posix\dirent.d \
src\core\sys\posix\dlfcn.d \
src\core\sys\posix\fcntl.d \
src\core\sys\posix\grp.d \
src\core\sys\posix\iconv.d \
src\core\sys\posix\inttypes.d \
src\core\sys\posix\libgen.d \
src\core\sys\posix\netdb.d \
src\core\sys\posix\poll.d \
src\core\sys\posix\pthread.d \
src\core\sys\posix\pwd.d \
src\core\sys\posix\sched.d \
src\core\sys\posix\semaphore.d \
src\core\sys\posix\setjmp.d \
src\core\sys\posix\signal.d \
src\core\sys\posix\spawn.d \
src\core\sys\posix\netdb.d \
src\core\sys\posix\stdio.d \
src\core\sys\posix\stdlib.d \
src\core\sys\posix\syslog.d \
src\core\sys\posix\termios.d \
src\core\sys\posix\time.d \
src\core\sys\posix\ucontext.d \
src\core\sys\posix\unistd.d \
src\core\sys\posix\utime.d \
\
src\core\sys\posix\net\if_.d \
\
src\core\sys\posix\netinet\in_.d \
src\core\sys\posix\arpa\inet.d \
src\core\sys\posix\netinet\tcp.d \
\
src\core\sys\posix\sys\filio.d \
src\core\sys\posix\sys\ioccom.d \
src\core\sys\posix\sys\ioctl.d \
src\core\sys\posix\sys\ipc.d \
src\core\sys\posix\sys\mman.d \
Expand All @@ -135,15 +193,29 @@ SRCS=\
src\core\sys\posix\sys\stat.d \
src\core\sys\posix\sys\statvfs.d \
src\core\sys\posix\sys\time.d \
src\core\sys\posix\sys\ttycom.d \
src\core\sys\posix\sys\types.d \
src\core\sys\posix\sys\uio.d \
src\core\sys\posix\sys\un.d \
src\core\sys\posix\sys\utsname.d \
src\core\sys\posix\sys\wait.d \
\
src\core\sys\solaris\dlfcn.d \
src\core\sys\solaris\elf.d \
src\core\sys\solaris\execinfo.d \
src\core\sys\solaris\libelf.d \
src\core\sys\solaris\link.d \
src\core\sys\solaris\time.d \
src\core\sys\solaris\sys\elf.d \
src\core\sys\solaris\sys\elf_386.d \
src\core\sys\solaris\sys\elf_amd64.d \
src\core\sys\solaris\sys\elf_notes.d \
src\core\sys\solaris\sys\elf_SPARC.d \
src\core\sys\solaris\sys\elftypes.d \
src\core\sys\solaris\sys\link.d \
src\core\sys\solaris\sys\priocntl.d \
src\core\sys\solaris\sys\types.d \
src\core\sys\solaris\sys\procset.d \
src\core\sys\solaris\sys\types.d \
\
src\core\sys\windows\accctrl.d \
src\core\sys\windows\aclapi.d \
Expand Down
37 changes: 37 additions & 0 deletions src/core/memory.d
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ private

extern (C) BlkInfo_ gc_query( void* p ) pure nothrow;
extern (C) GC.Stats gc_stats ( ) nothrow @nogc;
extern (C) GC.ProfileStats gc_profileStats ( ) nothrow @nogc @safe;

extern (C) void gc_addRoot( in void* p ) nothrow @nogc;
extern (C) void gc_addRange( in void* p, size_t sz, const TypeInfo ti = null ) nothrow @nogc;
Expand Down Expand Up @@ -170,6 +171,24 @@ struct GC
size_t freeSize;
}

/**
* Aggregation of current profile information
*/
static struct ProfileStats
{
import core.time : Duration;
/// total number of GC cycles
size_t numCollections;
/// total time spent doing GC
Duration totalCollectionTime;
/// total time threads were paused doing GC
Duration totalPauseTime;
/// largest time threads were paused during one GC cycle
Duration maxPauseTime;
/// largest time spent doing one GC cycle
Duration maxCollectionTime;
}

/**
* Enables automatic garbage collection behavior if collections have
* previously been suspended by a call to disable. This function is
Expand Down Expand Up @@ -689,6 +708,15 @@ struct GC
return gc_stats();
}

/**
* Returns runtime profile stats for currently active GC implementation
* See `core.memory.GC.ProfileStats` for list of available metrics.
*/
static ProfileStats profileStats() nothrow @nogc @safe
{
return gc_profileStats();
}

/**
* Adds an internal root pointing to the GC memory block referenced by p.
* As a result, the block referenced by p itself and any blocks accessible
Expand Down Expand Up @@ -1265,3 +1293,12 @@ unittest
void* p = GC.malloc(100);
assert(GC.realloc(&p, 50) == null); // non-GC pointer
}

// test GC.profileStats
unittest
{
auto stats = GC.profileStats();
GC.collect();
auto nstats = GC.profileStats();
assert(nstats.numCollections > stats.numCollections);
}
2 changes: 1 addition & 1 deletion src/core/sys/freebsd/pthread_np.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* Authors: Martin Nowak
*/
module core.sys.freebsd.pthread;
module core.sys.freebsd.pthread_np;

version (FreeBSD):
extern (C) nothrow @nogc:
Expand Down
6 changes: 6 additions & 0 deletions src/gc/gcinterface.d
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ interface GC
*/
core.memory.GC.Stats stats() nothrow;

/**
* Retrieve profile statistics about garbage collection.
* Useful for debugging and tuning.
*/
core.memory.GC.ProfileStats profileStats() nothrow;

/**
* add p to list of roots
*/
Expand Down
62 changes: 35 additions & 27 deletions src/gc/impl/conservative/gc.d
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ __gshared Duration prepTime;
__gshared Duration markTime;
__gshared Duration sweepTime;
__gshared Duration recoverTime;
__gshared Duration pauseTime;
__gshared Duration maxPauseTime;
__gshared Duration maxCollectionTime;
__gshared size_t numCollections;
__gshared size_t maxPoolMemory;

Expand Down Expand Up @@ -1184,6 +1186,19 @@ class ConservativeGC : GC
}


core.memory.GC.ProfileStats profileStats() nothrow
{
typeof(return) ret;

ret.numCollections = numCollections;
ret.totalCollectionTime = prepTime + markTime + sweepTime + recoverTime;
ret.totalPauseTime = pauseTime;
ret.maxCollectionTime = maxCollectionTime;
ret.maxPauseTime = maxPauseTime;

return ret;
}

//
//
//
Expand Down Expand Up @@ -2405,28 +2420,23 @@ struct Gcx

prepare();

if (config.profile)
{
stop = currTime;
prepTime += (stop - start);
start = stop;
}
stop = currTime;
prepTime += (stop - start);
start = stop;

markAll(nostack);

thread_processGCMarks(&isMarked);
thread_resumeAll();
}

if (config.profile)
{
stop = currTime;
markTime += (stop - start);
Duration pause = stop - begin;
if (pause > maxPauseTime)
maxPauseTime = pause;
start = stop;
}
stop = currTime;
markTime += (stop - start);
Duration pause = stop - begin;
if (pause > maxPauseTime)
maxPauseTime = pause;
pauseTime += pause;
start = stop;

ConservativeGC._inFinalizer = true;
size_t freedLargePages=void;
Expand All @@ -2436,21 +2446,19 @@ struct Gcx
ConservativeGC._inFinalizer = false;
}

if (config.profile)
{
stop = currTime;
sweepTime += (stop - start);
start = stop;
}
stop = currTime;
sweepTime += (stop - start);
start = stop;

immutable freedSmallPages = recover();

if (config.profile)
{
stop = currTime;
recoverTime += (stop - start);
++numCollections;
}
stop = currTime;
recoverTime += (stop - start);
Duration collectionTime = stop - begin;
if (collectionTime > maxCollectionTime)
maxCollectionTime = collectionTime;

++numCollections;

updateCollectThresholds();

Expand Down
Loading