Sha256: ecf21f3c9f96e797359fa39e405f47cb97bfcd8b241a06ec0bf15ccede6d5636

Contents?: true

Size: 1.39 KB

Versions: 15

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module Facter
  module Resolvers
    module Macosx
      class SystemMemory < BaseResolver
        @semaphore = Mutex.new
        @fact_list ||= {}
        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 = Facter::Core::Execution.execute('vm_stat', logger: log)
            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] = Facter::Core::Execution.execute('sysctl -n hw.memsize', logger: log).to_i
          end

          def compute_capacity(used, total)
            "#{format('%<value>.2f', value: (used / total.to_f * 100))}%"
          end
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
facter-4.0.36 lib/facter/resolvers/macosx/system_memory_resolver.rb
facter-4.0.35 lib/facter/resolvers/macosx/system_memory_resolver.rb
facter-4.0.34 lib/facter/resolvers/macosx/system_memory_resolver.rb
facter-4.0.33 lib/facter/resolvers/macosx/system_memory_resolver.rb
facter-4.0.32 lib/facter/resolvers/macosx/system_memory_resolver.rb
facter-4.0.31 lib/facter/resolvers/macosx/system_memory_resolver.rb
facter-4.0.30 lib/facter/resolvers/macosx/system_memory_resolver.rb
facter-4.0.29 lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.28 lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.26 lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.25 lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.24 lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.23 lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.22 lib/resolvers/macosx/system_memory_resolver.rb
facter-4.0.21 lib/resolvers/macosx/system_memory_resolver.rb