Skip to content

Commit

Permalink
Add GC.logging_enabled() (#51647)
Browse files Browse the repository at this point in the history
  • Loading branch information
fingolfin committed Jan 24, 2024
1 parent 919c390 commit e42ffa6
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ New library functions
* `eachrsplit(string, pattern)` iterates split substrings right to left.
* `Sys.username()` can be used to return the current user's username ([#51897]).
* `wrap(Array, m::Union{MemoryRef{T}, Memory{T}}, dims)` which is the safe counterpart to `unsafe_wrap` ([#52049]).
* `GC.logging_enabled()` can be used to test whether GC logging has been enabled via `GC.enable_logging` ([#51647]).

New library features
--------------------
Expand Down
9 changes: 9 additions & 0 deletions base/gcutils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,13 @@ function enable_logging(on::Bool=true)
ccall(:jl_enable_gc_logging, Cvoid, (Cint,), on)
end

"""
GC.logging_enabled()
Return whether GC logging has been enabled via [`GC.enable_logging`](@ref).
"""
function logging_enabled()
ccall(:jl_is_gc_logging_enabled, Cint, ()) != 0
end

end # module GC
1 change: 1 addition & 0 deletions doc/src/base/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ Base.GC.enable
Base.GC.@preserve
Base.GC.safepoint
Base.GC.enable_logging
Base.GC.logging_enabled
Meta.lower
Meta.@lower
Meta.parse(::AbstractString, ::Int)
Expand Down
4 changes: 4 additions & 0 deletions src/gc-debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,10 @@ JL_DLLEXPORT void jl_enable_gc_logging(int enable) {
gc_logging_enabled = enable;
}

JL_DLLEXPORT int jl_is_gc_logging_enabled(void) {
return gc_logging_enabled;
}

void _report_gc_finished(uint64_t pause, uint64_t freed, int full, int recollect, int64_t live_bytes) JL_NOTSAFEPOINT {
if (!gc_logging_enabled) {
return;
Expand Down
1 change: 1 addition & 0 deletions src/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ void gc_count_pool(void);
size_t jl_genericmemory_nbytes(jl_genericmemory_t *a) JL_NOTSAFEPOINT;

JL_DLLEXPORT void jl_enable_gc_logging(int enable);
JL_DLLEXPORT int jl_is_gc_logging_enabled(void);
JL_DLLEXPORT uint32_t jl_get_num_stack_mappings(void);
void _report_gc_finished(uint64_t pause, uint64_t freed, int full, int recollect, int64_t live_bytes) JL_NOTSAFEPOINT;

Expand Down
2 changes: 2 additions & 0 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1343,8 +1343,10 @@ end
open(tmppath, "w") do tmpio
redirect_stderr(tmpio) do
GC.enable_logging(true)
@test GC.logging_enabled()
GC.gc()
GC.enable_logging(false)
@test !GC.logging_enabled()
end
end
@test occursin("GC: pause", read(tmppath, String))
Expand Down

0 comments on commit e42ffa6

Please sign in to comment.