8000 Expose runtime state for metrics scraping and diagnostics · libvips/ruby-vips@a30fc5b · GitHub
[go: up one dir, main page]

Skip to content

Commit a30fc5b

Browse files
committed
Expose runtime state for metrics scraping and diagnostics
Metrics for allocations and open files: * `Vips.tracked_mem` - bytes currently allocated * `Vips.tracked_mem_highwater` - max bytes allocated * `Vips.tracked_allocs` - current allocation count * `Vips.tracked_files` - current open file count Expose global settings for inspection: * `Vips.vector?` - whether SIMD vector support is enabled * `Vips.cache_max` - max operations to cache * `Vips.cache_max_mem` - max bytes to cache * `Vips.cache_max_files` - max open files to cache
1 parent 53d279b commit a30fc5b

File tree

2 files changed

+121
-12
lines changed

2 files changed

+121
-12
lines changed

lib/vips.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ def to_s
625625

626626
attach_function :vips_leak_set, [:int], :void
627627
attach_function :vips_vector_set_enabled, [:int], :void
628+
attach_function :vips_vector_isenabled, [], :int
628629
attach_function :vips_concurrency_set, [:int], :void
629630

630631
# vips_foreign_get_suffixes was added in libvips 8.8
@@ -640,27 +641,84 @@ def self.leak_set leak
640641
vips_leak_set((leak ? 1 : 0))
641642
end
642643

644+
attach_function :vips_tracked_get_mem, [], :int
645+
attach_function :vips_tracked_get_mem_highwater, [], :int
646+
attach_function :vips_tracked_get_allocs, [], :int
647+
attach_function :vips_tracked_get_files, [], :int
648+
attach_function :vips_cache_get_max, [], :int
649+
attach_function :vips_cache_get_max_mem, [], :int
650+
attach_function :vips_cache_get_max_files, [], :int
643651
attach_function :vips_cache_set_max, [:int], :void
644652
attach_function :vips_cache_set_max_mem, [:int], :void
645653
attach_function :vips_cache_set_max_files, [:int], :void
654+
attach_function :vips_cache_print, [], :void
655+
attach_function :vips_cache_drop_all, [], :void
656+
657+
# Get the number of bytes currently allocated via vips_malloc.
658+
def self.tracked_mem
659+
vips_tracked_get_mem
660+
end
661+
662+
# Get the greatest number of bytes ever actively allocated via vips_malloc.
663+
def self.tracked_mem_highwater
664+
vips_tracked_get_mem_highwater
665+
end
666+
667+
# Get the number of active allocations.
668+
def self.tracked_allocs
669+
vips_tracked_get_allocs
670+
end
671+
672+
# Get the number of open files.
673+
def self.tracked_files
674+
vips_tracked_get_files
675+
end
676+
677+
# Get the maximum number of operations that libvips should cache.
678+
def self.cache_max
679+
vips_cache_get_max
680+
end
681+
682+
# Get the maximum amount of memory that libvips uses for the operation cache.
683+
def self.cache_max_mem
684+
vips_cache_get_max_mem
685+
end
686+
687+
# Get the maximum number of files libvips keeps open in the operation cache.
688+
def self.cache_max_files
689+
vips_cache_get_max_files
690+
end
646691

647692
# Set the maximum number of operations that libvips should cache. Set 0 to
648693
# disable the operation cache. The default is 1000.
649694
def self.cache_set_max size
650695
vips_cache_set_max size
696+
cache_max
651697
end
652698

653699
# Set the maximum amount of memory that libvips should use for the operation
654700
# cache. Set 0 to disable the operation cache. The default is 100mb.
655701
def self.cache_set_max_mem size
656702
vips_cache_set_max_mem size
703+
cache_max_mem
657704
end
658705

659706
# Set the maximum number of files libvips should keep open in the
660707
# operation cache. Set 0 to disable the operation cache. The default is
661708
# 100.
662709
def self.cache_set_max_files size
663710
vips_cache_set_max_files size
711+
cache_max_files
712+
end
713+
714+
# Print the libvips operation cache to stdout. Handy for debugging.
715+
def self.cache_print # :nodoc:
716+
vips_cache_print
717+
end
718+
719+
# Drop the libvips operation cache. Handy for leak tracking.
720+
def self.cache_drop_all # :nodoc:
721+
vips_cache_drop_all
664722
end
665723

666724
# Set the size of the libvips worker pool. This defaults to the number of
@@ -669,11 +727,19 @@ def self.concurrency_set n
669727
vips_concurrency_set n
670728
end
671729

730+
# Whether SIMD and the run-time compiler are enabled. This can give a nice
731+
# speed-up, but can also be unstable on some systems or with some versions
732+
# of the run-time compiler.
733+
def self.vector?
734+
vips_vector_isenabled == 1
735+
end
736+
672737
# Enable or disable SIMD and the run-time compiler. This can give a nice
673738
# speed-up, but can also be unstable on some systems or with some versions
674739
# of the run-time compiler.
675740
def self.vector_set enabled
676741
vips_vector_set_enabled(enabled ? 1 : 0)
742+
vector?
677743
end
678744

679745
# Deprecated compatibility function.

spec/vips_spec.rb

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,76 @@
66
Vips.concurrency_set 12
77
end
88

9-
it "can set SIMD" do
10-
Vips.vector_set true
9+
it "sets SIMD" do
10+
default = Vips.vector?
11+
12+
expect(Vips.vector_set(true)).to be true
13+
expect(Vips.vector?).to be true
14+
15+
expect(Vips.vector_set(false)).to be false
16+
expect(Vips.vector?).to be false
17+
18+
Vips.vector_set default
1119
end
1220

1321
it "can enable leak testing" do
1422
Vips.leak_set true
1523
Vips.leak_set false
1624
end
1725

18-
it "can set the operation cache size" do
19-
Vips.cache_set_max 0
20-
Vips.cache_set_max 100
26+
it "can get a set of filename suffixes" do
27+
suffs = Vips.get_suffixes
28+
expect(suffs.length > 10).to be true unless suffs.empty?
29+
end
30+
end
31+
32+
describe "cache" do
33+
it "can get and set the operation cache size" do
34+
default = Vips.cache_max
35+
36+
expect(Vips.cache_set_max(0)).to be 0
37+
expect(Vips.cache_max).to be 0
38+
39+
expect(Vips.cache_set_max(default)).to be default
40+
expect(Vips.cache_max).to be default
2141
end
2242

2343
it "can set the operation cache memory limit" do
24-
Vips.cache_set_max_mem 0
25-
Vips.cache_set_max_mem 10000000
44+
default = Vips.cache_max_mem
45+
46+
expect(Vips.cache_set_max_mem(0)).to be 0
47+
expect(Vips.cache_max_mem).to be 0
48+
49+
expect(Vips.cache_set_max_mem(default)).to be default
50+
expect(Vips.cache_max_mem).to be default
2651
end
2752

2853
it "can set the operation cache file limit" do
29-
Vips.cache_set_max_files 0
30-
Vips.cache_set_max_files 100
54+
default = Vips.cache_max_files
55+
56+
expect(Vips.cache_set_max_files(0)).to be 0
57+
expect(Vips.cache_max_files).to be 0
58+
59+
expect(Vips.cache_set_max_files(default)).to be default
60+
expect(Vips.cache_max_files).to be default
3161
end
62+
end
3263

33-
it "can get a set of filename suffixes" do
34-
suffs = Vips.get_suffixes
35-
expect(suffs.length > 10).to be true unless suffs.empty?
64+
describe "#tracked_*" do
65+
it "can get allocated bytes" do
66+
expect(Vips.tracked_mem).to be >= 0
67+
end
68+
69+
it "can get allocated bytes high-water mark" do
70+
expect(Vips.tracked_mem_highwater).to be >= 0
71+
end
72+
73+
it "can get allocation count" do
74+
expect(Vips.tracked_allocs).to be >= 0
75+
end
76+
77+
it "can get open file count" do
78+
expect(Vips.tracked_files).to be >= 0
3679
end
3780
end
3881

0 commit comments

Comments
 (0)
0