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-2454) fix how used memory is calculated #2038

Merged
merged 1 commit into from
Aug 31, 2020
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
9 changes: 8 additions & 1 deletion lib/facter/resolvers/memory_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def read_meminfo_file(fact_name)
def read_system(output)
@fact_list[:total] = kilobytes_to_bytes(output.match(/MemTotal:\s+(\d+)\s/)[1])
@fact_list[:memfree] = kilobytes_to_bytes(output.match(/MemFree:\s+(\d+)\s/)[1])
@fact_list[:used_bytes] = compute_used(@fact_list[:total], @fact_list[:memfree])
@fact_list[:used_bytes] = compute_used(@fact_list[:total], reclaimable_memory(output))
@fact_list[:capacity] = compute_capacity(@fact_list[:used_bytes], @fact_list[:total])
end

Expand All @@ -45,6 +45,13 @@ def kilobytes_to_bytes(quantity)
quantity.to_i * 1024
end

def reclaimable_memory(output)
buffers = kilobytes_to_bytes(output.match(/Buffers:\s+(\d+)\s/)[1])
cached = kilobytes_to_bytes(output.match(/Cached:\s+(\d+)\s/)[1])
s_reclaimable = kilobytes_to_bytes(output.match(/SReclaimable:\s+(\d+)\s/)[1])
@fact_list[:memfree] + buffers + cached + s_reclaimable
end

def compute_capacity(used, total)
format('%<computed_capacity>.2f', computed_capacity: (used / total.to_f * 100)) + '%'
end
Expand Down
10 changes: 8 additions & 2 deletions spec/facter/resolvers/memory_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
context 'when there is swap memory' do
let(:total) { 4_036_680 * 1024 }
let(:free) { 3_547_792 * 1024 }
let(:used) { total - free }
let(:buffers) { 4_288 * 1024 }
let(:cached) { 298_624 * 1024 }
let(:s_reclaimable) { 29_072 * 1024 }
let(:used) { total - free - buffers - cached - s_reclaimable }
let(:swap_total) { 2_097_148 * 1024 }
let(:swap_free) { 2_097_148 * 1024 }
let(:swap_used) { swap_total - swap_free }
Expand Down Expand Up @@ -63,7 +66,10 @@
context 'when there is not swap memory' do
let(:total) { 4_134_510_592 }
let(:free) { 3_465_723_904 }
let(:used) { total - free }
let(:buffers) { 2_088 * 1024 }
let(:cached) { 445_204 * 1024 }
let(:s_reclaimable) { 71_384 * 1024 }
let(:used) { total - free - buffers - cached - s_reclaimable }
let(:fixture_name) { 'meminfo2' }

it 'returns total memory' do
Expand Down