Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 29f495b

Browse files
authored
Merge pull request #2332 from ZombineDev/gc-runtime-profile-stats
Add access to GC runtime profile stats merged-on-behalf-of: Nicholas Wilson <thewilsonator@users.noreply.github.com>
2 parents d945061 + 81732af commit 29f495b

File tree

10 files changed

+184
-41
lines changed

10 files changed

+184
-41
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Added `GC.profileStats()` to `core.memory`
2+
3+
Allows access to current GC profiling information.
4+
See $(REF GC.ProfileStats, core,memory) for a list of profile stats.

mak/COPY

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,18 @@ COPY=\
8181
\
8282
$(IMPDIR)\core\sys\freebsd\netinet\in_.d \
8383
\
84-
$(IMPDIR)\core\sys\freebsd\sys\cdefs.d \
8584
$(IMPDIR)\core\sys\freebsd\pthread_np.d \
8685
$(IMPDIR)\core\sys\freebsd\sys\_bitset.d \
8786
$(IMPDIR)\core\sys\freebsd\sys\_cpuset.d \
87+
$(IMPDIR)\core\sys\freebsd\sys\cdefs.d \
88+
$(IMPDIR)\core\sys\freebsd\sys\elf_common.d \
8889
$(IMPDIR)\core\sys\freebsd\sys\elf.d \
8990
$(IMPDIR)\core\sys\freebsd\sys\elf32.d \
9091
$(IMPDIR)\core\sys\freebsd\sys\elf64.d \
91-
$(IMPDIR)\core\sys\freebsd\sys\elf_common.d \
9292
$(IMPDIR)\core\sys\freebsd\sys\event.d \
9393
$(IMPDIR)\core\sys\freebsd\sys\link_elf.d \
94-
$(IMPDIR)\core\sys\freebsd\sys\mount.d \
9594
$(IMPDIR)\core\sys\freebsd\sys\mman.d \
95+
$(IMPDIR)\core\sys\freebsd\sys\mount.d \
9696
$(IMPDIR)\core\sys\freebsd\time.d \
9797
$(IMPDIR)\core\sys\freebsd\unistd.d \
9898
\
@@ -101,14 +101,14 @@ COPY=\
101101
\
102102
$(IMPDIR)\core\sys\dragonflybsd\netinet\in_.d \
103103
\
104-
$(IMPDIR)\core\sys\dragonflybsd\sys\cdefs.d \
105104
$(IMPDIR)\core\sys\dragonflybsd\pthread_np.d \
106105
$(IMPDIR)\core\sys\dragonflybsd\sys\_bitset.d \
107106
$(IMPDIR)\core\sys\dragonflybsd\sys\_cpuset.d \
107+
$(IMPDIR)\core\sys\dragonflybsd\sys\cdefs.d \
108+
$(IMPDIR)\core\sys\dragonflybsd\sys\elf_common.d \
108109
$(IMPDIR)\core\sys\dragonflybsd\sys\elf.d \
109110
$(IMPDIR)\core\sys\dragonflybsd\sys\elf32.d \
110111
$(IMPDIR)\core\sys\dragonflybsd\sys\elf64.d \
111-
$(IMPDIR)\core\sys\dragonflybsd\sys\elf_common.d \
112112
$(IMPDIR)\core\sys\dragonflybsd\sys\event.d \
113113
$(IMPDIR)\core\sys\dragonflybsd\sys\link_elf.d \
114114
$(IMPDIR)\core\sys\dragonflybsd\sys\mman.d \
@@ -197,8 +197,8 @@ COPY=\
197197
$(IMPDIR)\core\sys\posix\sys\types.d \
198198
$(IMPDIR)\core\sys\posix\sys\uio.d \
199199
$(IMPDIR)\core\sys\posix\sys\un.d \
200-
$(IMPDIR)\core\sys\posix\sys\wait.d \
201200
$(IMPDIR)\core\sys\posix\sys\utsname.d \
201+
$(IMPDIR)\core\sys\posix\sys\wait.d \
202202
\
203203
$(IMPDIR)\core\sys\solaris\dlfcn.d \
204204
$(IMPDIR)\core\sys\solaris\elf.d \
@@ -213,9 +213,9 @@ COPY=\
213213
$(IMPDIR)\core\sys\solaris\sys\elf_SPARC.d \
214214
$(IMPDIR)\core\sys\solaris\sys\elftypes.d \
215215
$(IMPDIR)\core\sys\solaris\sys\link.d \
216+
$(IMPDIR)\core\sys\solaris\sys\priocntl.d \
216217
$(IMPDIR)\core\sys\solaris\sys\procset.d \
217218
$(IMPDIR)\core\sys\solaris\sys\types.d \
218-
$(IMPDIR)\core\sys\solaris\sys\priocntl.d \
219219
\
220220
$(IMPDIR)\core\sys\windows\accctrl.d \
221221
$(IMPDIR)\core\sys\windows\aclapi.d \

mak/SRCS

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ SRCS=\
4545
src\core\stdc\stdio.d \
4646
src\core\stdc\stdlib.d \
4747
src\core\stdc\string.d \
48+
src\core\stdc\tgmath.d \
4849
src\core\stdc\time.d \
4950
src\core\stdc\wchar_.d \
51+
src\core\stdc\wctype.d \
5052
\
5153
src\core\stdcpp\array.d \
5254
src\core\stdcpp\string_view.d \
@@ -74,9 +76,14 @@ SRCS=\
7476
src\core\sys\darwin\mach\thread_act.d \
7577
src\core\sys\darwin\netinet\in_.d \
7678
\
79+
src\core\sys\darwin\sys\cdefs.d \
80+
src\core\sys\darwin\sys\event.d \
81+
src\core\sys\darwin\sys\mman.d \
82+
\
7783
src\core\sys\freebsd\dlfcn.d \
7884
src\core\sys\freebsd\execinfo.d \
7985
src\core\sys\freebsd\netinet\in_.d \
86+
src\core\sys\freebsd\pthread_np.d \
8087
src\core\sys\freebsd\sys\_bitset.d \
8188
src\core\sys\freebsd\sys\_cpuset.d \
8289
src\core\sys\freebsd\sys\cdefs.d \
@@ -94,6 +101,7 @@ SRCS=\
94101
src\core\sys\dragonflybsd\dlfcn.d \
95102
src\core\sys\dragonflybsd\execinfo.d \
96103
src\core\sys\dragonflybsd\netinet\in_.d \
104+
src\core\sys\dragonflybsd\pthread_np.d \
97105
src\core\sys\dragonflybsd\sys\_bitset.d \
98106
src\core\sys\dragonflybsd\sys\_cpuset.d \
99107
src\core\sys\dragonflybsd\sys\cdefs.d \
@@ -106,25 +114,75 @@ SRCS=\
106114
src\core\sys\dragonflybsd\sys\mman.d \
107115
src\core\sys\dragonflybsd\time.d \
108116
\
117+
src\core\sys\linux\config.d \
118+
src\core\sys\linux\dlfcn.d \
119+
src\core\sys\linux\elf.d \
120+
src\core\sys\linux\epoll.d \
121+
src\core\sys\linux\errno.d \
122+
src\core\sys\linux\execinfo.d \
123+
src\core\sys\linux\fcntl.d \
124+
src\core\sys\linux\ifaddrs.d \
125+
src\core\sys\linux\link.d \
126+
src\core\sys\linux\sched.d \
127+
src\core\sys\linux\termios.d \
128+
src\core\sys\linux\time.d \
129+
src\core\sys\linux\timerfd.d \
130+
src\core\sys\linux\tipc.d \
131+
src\core\sys\linux\unistd.d \
132+
\
109133
src\core\sys\linux\netinet\in_.d \
110134
src\core\sys\linux\netinet\tcp.d \
111-
src\core\sys\linux\stdio.d \
112-
src\core\sys\linux\tipc.d \
135+
src\core\sys\linux\sys\netinet\tcp.d \
136+
\
137+
src\core\sys\linux\sys\auxv.d \
138+
src\core\sys\linux\sys\eventfd.d \
139+
src\core\sys\linux\sys\file.d \
113140
src\core\sys\linux\sys\inotify.d \
114141
src\core\sys\linux\sys\mman.d \
115142
src\core\sys\linux\sys\signalfd.d \
116143
src\core\sys\linux\sys\socket.d \
117144
src\core\sys\linux\sys\sysinfo.d \
118-
src\core\sys\linux\sys\time.d \
119145
src\core\sys\linux\sys\xattr.d \
146+
src\core\sys\linux\sys\time.d \
147+
src\core\sys\linux\sys\prctl.d \
148+
\
149+
src\core\sys\openbsd\dlfcn.d \
120150
\
151+
src\core\sys\posix\arpa\inet.d \
152+
src\core\sys\posix\aio.d \
153+
src\core\sys\posix\config.d \
121154
src\core\sys\posix\dirent.d \
155+
src\core\sys\posix\dlfcn.d \
156+
src\core\sys\posix\fcntl.d \
157+
src\core\sys\posix\grp.d \
158+
src\core\sys\posix\iconv.d \
159+
src\core\sys\posix\inttypes.d \
160+
src\core\sys\posix\libgen.d \
161+
src\core\sys\posix\netdb.d \
162+
src\core\sys\posix\poll.d \
163+
src\core\sys\posix\pthread.d \
164+
src\core\sys\posix\pwd.d \
165+
src\core\sys\posix\sched.d \
166+
src\core\sys\posix\semaphore.d \
167+
src\core\sys\posix\setjmp.d \
122168
src\core\sys\posix\signal.d \
123169
src\core\sys\posix\spawn.d \
124-
src\core\sys\posix\netdb.d \
170+
src\core\sys\posix\stdio.d \
171+
src\core\sys\posix\stdlib.d \
172+
src\core\sys\posix\syslog.d \
173+
src\core\sys\posix\termios.d \
174+
src\core\sys\posix\time.d \
175+
src\core\sys\posix\ucontext.d \
176+
src\core\sys\posix\unistd.d \
177+
src\core\sys\posix\utime.d \
178+
\
179+
src\core\sys\posix\net\if_.d \
180+
\
125181
src\core\sys\posix\netinet\in_.d \
126-
src\core\sys\posix\arpa\inet.d \
182+
src\core\sys\posix\netinet\tcp.d \
127183
\
184+
src\core\sys\posix\sys\filio.d \
185+
src\core\sys\posix\sys\ioccom.d \
128186
src\core\sys\posix\sys\ioctl.d \
129187
src\core\sys\posix\sys\ipc.d \
130188
src\core\sys\posix\sys\mman.d \
@@ -135,15 +193,29 @@ SRCS=\
135193
src\core\sys\posix\sys\stat.d \
136194
src\core\sys\posix\sys\statvfs.d \
137195
src\core\sys\posix\sys\time.d \
196+
src\core\sys\posix\sys\ttycom.d \
138197
src\core\sys\posix\sys\types.d \
139198
src\core\sys\posix\sys\uio.d \
140199
src\core\sys\posix\sys\un.d \
141200
src\core\sys\posix\sys\utsname.d \
142201
src\core\sys\posix\sys\wait.d \
143202
\
203+
src\core\sys\solaris\dlfcn.d \
204+
src\core\sys\solaris\elf.d \
205+
src\core\sys\solaris\execinfo.d \
206+
src\core\sys\solaris\libelf.d \
207+
src\core\sys\solaris\link.d \
208+
src\core\sys\solaris\time.d \
209+
src\core\sys\solaris\sys\elf.d \
210+
src\core\sys\solaris\sys\elf_386.d \
211+
src\core\sys\solaris\sys\elf_amd64.d \
212+
src\core\sys\solaris\sys\elf_notes.d \
213+
src\core\sys\solaris\sys\elf_SPARC.d \
214+
src\core\sys\solaris\sys\elftypes.d \
215+
src\core\sys\solaris\sys\link.d \
144216
src\core\sys\solaris\sys\priocntl.d \
145-
src\core\sys\solaris\sys\types.d \
146217
src\core\sys\solaris\sys\procset.d \
218+
src\core\sys\solaris\sys\types.d \
147219
\
148220
src\core\sys\windows\accctrl.d \
149221
src\core\sys\windows\aclapi.d \

src/core/memory.d

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ private
139139

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

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

174+
/**
175+
* Aggregation of current profile information
176+
*/
177+
static struct ProfileStats
178+
{
179+
import core.time : Duration;
180+
/// total number of GC cycles
181+
size_t numCollections;
182+
/// total time spent doing GC
183+
Duration totalCollectionTime;
184+
/// total time threads were paused doing GC
185+
Duration totalPauseTime;
186+
/// largest time threads were paused during one GC cycle
187+
Duration maxPauseTime;
188+
/// largest time spent doing one GC cycle
189+
Duration maxCollectionTime;
190+
}
191+
173192
/**
174193
* Enables automatic garbage collection behavior if collections have
175194
* previously been suspended by a call to disable. This function is
@@ -689,6 +708,15 @@ struct GC
689708
return gc_stats();
690709
}
691710

711+
/**
712+
* Returns runtime profile stats for currently active GC implementation
713+
* See `core.memory.GC.ProfileStats` for list of available metrics.
714+
*/
715+
static ProfileStats profileStats() nothrow @nogc @safe
716+
{
717+
return gc_profileStats();
718+
}
719+
692720
/**
693721
* Adds an internal root pointing to the GC memory block referenced by p.
694722
* As a result, the block referenced by p itself and any blocks accessible
@@ -1265,3 +1293,12 @@ unittest
12651293
void* p = GC.malloc(100);
12661294
assert(GC.realloc(&p, 50) == null); // non-GC pointer
12671295
}
1296+
1297+
// test GC.profileStats
1298+
unittest
1299+
{
1300+
auto stats = GC.profileStats();
1301+
GC.collect();
1302+
auto nstats = GC.profileStats();
1303+
assert(nstats.numCollections > stats.numCollections);
1304+
}

src/core/sys/freebsd/pthread_np.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Authors: Martin Nowak
55
*/
6-
module core.sys.freebsd.pthread;
6+
module core.sys.freebsd.pthread_np;
77

88
version (FreeBSD):
99
extern (C) nothrow @nogc:

src/gc/gcinterface.d

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ interface GC
148148
*/
149149
core.memory.GC.Stats stats() nothrow;
150150

151+
/**
152+
* Retrieve profile statistics about garbage collection.
153+
* Useful for debugging and tuning.
154+
*/
155+
core.memory.GC.ProfileStats profileStats() nothrow;
156+
151157
/**
152158
* add p to list of roots
153159
*/

src/gc/impl/conservative/gc.d

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ __gshared Duration prepTime;
100100
__gshared Duration markTime;
101101
__gshared Duration sweepTime;
102102
__gshared Duration recoverTime;
103+
__gshared Duration pauseTime;
103104
__gshared Duration maxPauseTime;
105+
__gshared Duration maxCollectionTime;
104106
__gshared size_t numCollections;
105107
__gshared size_t maxPoolMemory;
106108

@@ -1184,6 +1186,19 @@ class ConservativeGC : GC
11841186
}
11851187

11861188

1189+
core.memory.GC.ProfileStats profileStats() nothrow
1190+
{
1191+
typeof(return) ret;
1192+
1193+
ret.numCollections = numCollections;
1194+
ret.totalCollectionTime = prepTime + markTime + sweepTime + recoverTime;
1195+
ret.totalPauseTime = pauseTime;
1196+
ret.maxCollectionTime = maxCollectionTime;
1197+
ret.maxPauseTime = maxPauseTime;
1198+
1199+
return ret;
1200+
}
1201+
11871202
//
11881203
//
11891204
//
@@ -2405,28 +2420,23 @@ struct Gcx
24052420

24062421
prepare();
24072422

2408-
if (config.profile)
2409-
{
2410-
stop = currTime;
2411-
prepTime += (stop - start);
2412-
start = stop;
2413-
}
2423+
stop = currTime;
2424+
prepTime += (stop - start);
2425+
start = stop;
24142426

24152427
markAll(nostack);
24162428

24172429
thread_processGCMarks(&isMarked);
24182430
thread_resumeAll();
24192431
}
24202432

2421-
if (config.profile)
2422-
{
2423-
stop = currTime;
2424-
markTime += (stop - start);
2425-
Duration pause = stop - begin;
2426-
if (pause > maxPauseTime)
2427-
maxPauseTime = pause;
2428-
start = stop;
2429-
}
2433+
stop = currTime;
2434+
markTime += (stop - start);
2435+
Duration pause = stop - begin;
2436+
if (pause > maxPauseTime)
2437+
maxPauseTime = pause;
2438+
pauseTime += pause;
2439+
start = stop;
24302440

24312441
ConservativeGC._inFinalizer = true;
24322442
size_t freedLargePages=void;
@@ -2436,21 +2446,19 @@ struct Gcx
24362446
ConservativeGC._inFinalizer = false;
24372447
}
24382448

2439-
if (config.profile)
2440-
{
2441-
stop = currTime;
2442-
sweepTime += (stop - start);
2443-
start = stop;
2444-
}
2449+
stop = currTime;
2450+
sweepTime += (stop - start);
2451+
start = stop;
24452452

24462453
immutable freedSmallPages = recover();
24472454

2448-
if (config.profile)
2449-
{
2450-
stop = currTime;
2451-
recoverTime += (stop - start);
2452-
++numCollections;
2453-
}
2455+
stop = currTime;
2456+
recoverTime += (stop - start);
2457+
Duration collectionTime = stop - begin;
2458+
if (collectionTime > maxCollectionTime)
2459+
maxCollectionTime = collectionTime;
2460+
2461+
++numCollections;
24542462

24552463
updateCollectThresholds();
24562464

0 commit comments

Comments
 (0)