Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(FACT-3149) Fix memory usage reporting on FreeBSD #2523

Merged
merged 1 commit into from
Nov 4, 2023
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
22 changes: 15 additions & 7 deletions lib/facter/resolvers/freebsd/system_memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,33 @@ def post_resolve(fact_name, _options)

def calculate_system_memory(fact_name)
read_total_memory_in_bytes
read_available_memory_in_bytes
read_used_memory_in_bytes

@fact_list[:used_bytes] = @fact_list[:total_bytes] - @fact_list[:available_bytes]
@fact_list[:available_bytes] = @fact_list[:total_bytes] - @fact_list[:used_bytes]
@fact_list[:capacity] = Facter::Util::Resolvers::FilesystemHelper
.compute_capacity(@fact_list[:used_bytes], @fact_list[:total_bytes])

@fact_list[fact_name]
end

def read_available_memory_in_bytes
output = Facter::Core::Execution.execute('vmstat -H --libxo json', logger: log)
data = JSON.parse(output)
@fact_list[:available_bytes] = data['memory']['free-memory'] * 1024
def pagesize
@pagesize ||= Facter::Freebsd::FfiHelper.sysctl_by_name(:long, 'vm.stats.vm.v_page_size')
end

def read_used_memory_in_bytes
require_relative 'ffi/ffi_helper'

@fact_list[:used_bytes] = pagesize * (
Facter::Freebsd::FfiHelper.sysctl_by_name(:long, 'vm.stats.vm.v_active_count') +
Facter::Freebsd::FfiHelper.sysctl_by_name(:long, 'vm.stats.vm.v_wire_count')
)
end

def read_total_memory_in_bytes
require_relative 'ffi/ffi_helper'

@fact_list[:total_bytes] = Facter::Freebsd::FfiHelper.sysctl_by_name(:long, 'hw.physmem')
@fact_list[:total_bytes] = pagesize *
Facter::Freebsd::FfiHelper.sysctl_by_name(:long, 'vm.stats.vm.v_page_count')
end
end
end
Expand Down
17 changes: 11 additions & 6 deletions spec/facter/resolvers/freebsd/system_memory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
before do
system_memory.instance_variable_set(:@log, log_spy)
allow(Facter::Freebsd::FfiHelper).to receive(:sysctl_by_name)
.with(:long, 'hw.physmem')
.and_return(17_043_554_304)

allow(Facter::Core::Execution).to receive(:execute)
.with('vmstat -H --libxo json', { logger: log_spy })
.and_return(load_fixture('freebsd_vmstat').read)
.with(:long, 'vm.stats.vm.v_page_size')
.and_return(4096)
allow(Facter::Freebsd::FfiHelper).to receive(:sysctl_by_name)
.with(:long, 'vm.stats.vm.v_page_count')
.and_return(4_161_024)
allow(Facter::Freebsd::FfiHelper).to receive(:sysctl_by_name)
.with(:long, 'vm.stats.vm.v_active_count')
.and_return(2_335_139)
allow(Facter::Freebsd::FfiHelper).to receive(:sysctl_by_name)
.with(:long, 'vm.stats.vm.v_wire_count')
.and_return(1_167_569)
end

it 'returns available system memory in bytes' do
Expand Down
6 changes: 0 additions & 6 deletions spec/fixtures/freebsd_vmstat

This file was deleted.