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

Skip to content

Commit ea88e72

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.concurrency` - current thread limit * `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 3206143 commit ea88e72

File tree

3 files changed

+154
-15
lines changed

3 files changed

+154
-15
lines changed

Rakefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
require "bundler/gem_tasks"
22

3+
# Disable stderr warning output
4+
ENV["VIPS_WARNING"] = "1"
5+
36
begin
47
Bundler.setup(:default, :development)
58
rescue Bundler::BundlerError => e

lib/vips.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,9 @@ 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
630+
attach_function :vips_concurrency_get, [], :int
629631

630632
# vips_foreign_get_suffixes was added in libvips 8.8
631633
begin
@@ -640,40 +642,113 @@ def self.leak_set leak
640642
vips_leak_set((leak ? 1 : 0))
641643
end
642644

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

647693
# Set the maximum number of operations that libvips should cache. Set 0 to
648694
# disable the operation cache. The default is 1000.
649695
def self.cache_set_max size
650696
vips_cache_set_max size
697+
cache_max
651698
end
652699

653700
# Set the maximum amount of memory that libvips should use for the operation
654701
# cache. Set 0 to disable the operation cache. The default is 100mb.
655702
def self.cache_set_max_mem size
656703
vips_cache_set_max_mem size
704+
cache_max_mem
657705
end
658706

659707
# Set the maximum number of files libvips should keep open in the
660708
# operation cache. Set 0 to disable the operation cache. The default is
661709
# 100.
662710
def self.cache_set_max_files size
663711
vips_cache_set_max_files size
712+
cache_max_files
713+
end
714+
715+
# Print the libvips operation cache to stdout. Handy for debugging.
716+
def self.cache_print # :nodoc:
717+
vips_cache_print
718+
end
719+
720+
# Drop the libvips operation cache. Handy for leak tracking.
721+
def self.cache_drop_all # :nodoc:
722+
vips_cache_drop_all
723+
end
724+
725+
# Get the size of the libvips worker pool. This defaults to the number of
726+
# hardware threads on your computer.
727+
def self.concurrency
728+
vips_concurrency_get
664729
end
665730

666731
# Set the size of the libvips worker pool. This defaults to the number of
667732
# hardware threads on your computer. Set to 1 to disable threading.
733+
# Set to 0 to use the default.
668734
def self.concurrency_set n
669735
vips_concurrency_set n
736+
concurrency
737+
end
738+
739+
# Whether SIMD and the run-time compiler are enabled. This can give a nice
740+
# speed-up, but can also be unstable on some systems or with some versions
741+
# of the run-time compiler.
742+
def self.vector?
743+
vips_vector_isenabled == 1
670744
end
671745

672746
# Enable or disable SIMD and the run-time compiler. This can give a nice
673747
# speed-up, but can also be unstable on some systems or with some versions
674748
# of the run-time compiler.
675749
def self.vector_set enabled
676750
vips_vector_set_enabled(enabled ? 1 : 0)
751+
vector?
677752
end
678753

679754
# Deprecated compatibility function.

spec/vips_spec.rb

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,99 @@
11
require "spec_helper"
22

33
RSpec.describe Vips do
4-
describe "Vips" do
5-
it "can set concurrency" do
6-
Vips.concurrency_set 12
4+
describe "concurrency" do
5+
it "sets concurrency" do
6+
default = rand(7)
7+
ENV["VIPS_CONCURRENCY"] = default.to_s
8+
9+
expect(Vips.concurrency_set(12)).to eq 12
10+
expect(Vips.concurrency).to eq 12
11+
12+
expect(Vips.concurrency_set(0)).to eq default
13+
expect(Vips.concurrency).to eq default
14+
end
15+
16+
it "clips concurrency" do
17+
default = rand(7)
18+
ENV["VIPS_CONCURRENCY"] = default.to_s
19+
20+
expect(Vips.concurrency_set(1025)).to eq 1024
21+
expect(Vips.concurrency).to eq 1024
22+
23+
expect(Vips.concurrency_set(0)).to eq default
24+
expect(Vips.concurrency).to eq default
725
end
826

9-
it "can set SIMD" do
10-
Vips.vector_set true
27+
it "sets SIMD" do
28+
default = Vips.vector?
29+
30+
expect(Vips.vector_set(true)).to be true
31+
expect(Vips.vector?).to be true
32+
33+
expect(Vips.vector_set(false)).to be false
34+
expect(Vips.vector?).to be false
35+
36+
Vips.vector_set default
1137
end
1238

1339
it "can enable leak testing" do
1440
Vips.leak_set true
1541
Vips.leak_set false
1642
end
1743

18-
it "can set the operation cache size" do
19-
Vips.cache_set_max 0
20-
Vips.cache_set_max 100
44+
it "can get a set of filename suffixes" do
45+
suffs = Vips.get_suffixes
46+
expect(suffs.length > 10).to be true unless suffs.empty?
47+
end
48+
end
49+
50+
describe "cache" do
51+
it "can get and set the operation cache size" do
52+
default = Vips.cache_max
53+
54+
expect(Vips.cache_set_max(0)).to be 0
55+
expect(Vips.cache_max).to be 0
56+
57+
expect(Vips.cache_set_max(default)).to be default
58+
expect(Vips.cache_max).to be default
2159
end
2260

2361
it "can set the operation cache memory limit" do
24-
Vips.cache_set_max_mem 0
25-
Vips.cache_set_max_mem 10000000
62+
default = Vips.cache_max_mem
63+
64+
expect(Vips.cache_set_max_mem(0)).to be 0
65+
expect(Vips.cache_max_mem).to be 0
66+
67+
expect(Vips.cache_set_max_mem(default)).to be default
68+
expect(Vips.cache_max_mem).to be default
2669
end
2770

2871
it "can set the operation cache file limit" do
29-
Vips.cache_set_max_files 0
30-
Vips.cache_set_max_files 100
72+
default = Vips.cache_max_files
73+
74+
expect(Vips.cache_set_max_files(0)).to be 0
75+
expect(Vips.cache_max_files).to be 0
76+
77+
expect(Vips.cache_set_max_files(default)).to be default
78+
expect(Vips.cache_max_files).to be default
3179
end
80+
end
3281

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?
82+
describe "#tracked_*" do
83+
it "can get allocated bytes" do
84+
expect(Vips.tracked_mem).to be >= 0
85+
end
86+
87+
it "can get allocated bytes high-water mark" do
88+
expect(Vips.tracked_mem_highwater).to be >= 0
89+
end
90+
91+
it "can get allocation count" do
92+
expect(Vips.tracked_allocs).to be >= 0
93+
end
94+
95+
it "can get open file count" do
96+
expect(Vips.tracked_files).to be >= 0
3697
end
3798
end
3899

0 commit comments

Comments
 (0)
0