Sha256: 11bdc164b29bc25ccd9e3c0e2bb455dee127560177b653397c6636a2a54c9307

Contents?: true

Size: 1.36 KB

Versions: 14

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

module Facter
  module Resolvers
    module Macosx
      class SystemMemory < BaseResolver
        @semaphore = Mutex.new
        @fact_list ||= {}
        @log = Facter::Log.new(self)
        class << self
          private

          def post_resolve(fact_name)
            @fact_list.fetch(fact_name) { calculate_system_memory(fact_name) }
          end

          def calculate_system_memory(fact_name)
            read_total_memory_in_bytes
            read_available_memory_in_bytes

            @fact_list[:used_bytes] = @fact_list[:total_bytes] - @fact_list[:available_bytes]
            @fact_list[:capacity] = compute_capacity(@fact_list[:used_bytes], @fact_list[:total_bytes])

            @fact_list[fact_name]
          end

          def read_available_memory_in_bytes
            output, _status = Open3.capture2('vm_stat')
            page_size = output.match(/page size of (\d+) bytes/)[1].to_i
            pages_free = output.match(/Pages free:\s+(\d+)/)[1].to_i

            @fact_list[:available_bytes] = page_size * pages_free
          end

          def read_total_memory_in_bytes
            @fact_list[:total_bytes] = Open3.capture2('sysctl -n hw.memsize').first.to_i
          end

          def compute_capacity(used, total)
            format('%.2f', (used / total.to_f * 100)) + '%'
          end
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
facter-4.0.20 lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.19 lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.18 lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.17 lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.16 lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.15 lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.14 lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.13 lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.12 lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.11 lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.10.pre lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.9.pre lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.8.pre lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.7.pre lib/resolvers/macosx/system_memory_resolver.rb