sys-memory 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a0f9fcf7bd91e41d398ad63d7aa5d62bf34a14a377700cd380696b9ba7fa6a48
4
- data.tar.gz: 95deeb86473d5350521df7149a2c5ccc6343d72cbac104e28d72e8cecfe7b850
3
+ metadata.gz: 9c4673911e1763e8282380312020206b04a9fb268572d3b64e8fc0a631befa03
4
+ data.tar.gz: 28e0b52dd179b98a4896dab5442bac98beb538fea493d0db5ea113b6148ed70d
5
5
  SHA512:
6
- metadata.gz: 7d461e046325569a7f9c3ddd25a285ed192c28512a777f7e2f3d4e44db4e9eb2768275162b7db8b41af61fdece3e1049f75554362b43e0b85b47a9c3063d3a63
7
- data.tar.gz: ec8e318290a6c405240269323f0f3a97a0e1c0a650041006d899f58fd4d0760871df32d02b9634b0288284d8579ef38003d706ae1290675f7a7c556a93f67639
6
+ metadata.gz: f6b1f2cf5a71c94be2dc997fb7e4c3d91a2d32c83af4b7399cba169c8ef36d20382decb1d35e020d7c7b7f7651957d1a1deb105e748db66087f071aae20879a8
7
+ data.tar.gz: 0c4f87e99e7b69148c0abcbfed29217a2fac229eff5237a3f632bfd2994dc89d6b432c63169a9e5bef1ab1f432f81330617bddef72f6a3092770fbcaaac49eb6
checksums.yaml.gz.sig CHANGED
Binary file
data/CHANGES.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 0.2.0 - 12-Jun-2024
2
+ * Added support for DragonflyBSD. Currently the default implementation
3
+ for all BSD platforms.
4
+
5
+ ## 0.1.3 - 12-Dec-2023
6
+ * Fix "used" calculations on Linux. Thanks go to Splendide-Imaginarius
7
+ for the spot and the patch.
8
+
1
9
  ## 0.1.2 - 9-Apr-2023
2
10
  * Added rubocop and rubocop-rspec as development dependencies.
3
11
  * Minor updates based on rubocop suggestions.
data/README.md CHANGED
@@ -7,6 +7,9 @@ A Ruby interface for getting memory information.
7
7
  * Linux
8
8
  * Windows
9
9
  * OSX
10
+ * BSD
11
+
12
+ Note that only DragonflyBSD has been tested. I am not sure about other flavors yet.
10
13
 
11
14
  ## Installation
12
15
  `gem install sys-memory`
@@ -44,7 +47,7 @@ https://github.com/djberg96/sys-memory
44
47
  Apache-2.0
45
48
 
46
49
  ## Copyright
47
- (C) 2021-2023 Daniel J. Berger, All Rights Reserved
50
+ (C) 2021-2024 Daniel J. Berger, All Rights Reserved
48
51
 
49
52
  ## Warranty
50
53
  This package is provided "as is" and without any express or
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ffi'
4
+
5
+ # The Sys module serves only as a namespace.
6
+ module Sys
7
+ # The Memory module is a house for memory related singleton methods that don't require state.
8
+ module Memory
9
+ extend FFI::Library
10
+ ffi_lib FFI::Library::LIBC
11
+
12
+ attach_function :sysctlbyname, %i[string pointer pointer pointer size_t], :int
13
+
14
+ # Obtain detailed memory information about your host in the form of a hash.
15
+ # Note that the exact nature of this hash is largely dependent on your
16
+ # operating system.
17
+ #
18
+ def memory
19
+ page_size = get_by_name('hw.pagesize')
20
+
21
+ hash = {}
22
+ hash[:total] = get_by_name('hw.physmem')
23
+ hash[:active] = get_by_name('vm.stats.vm.v_active_count') * page_size
24
+ hash[:all] = get_by_name('vm.stats.vm.v_page_count') * page_size
25
+ hash[:cache] = get_by_name('vm.stats.vm.v_cache_count') * page_size
26
+ hash[:free] = get_by_name('vm.stats.vm.v_free_count') * page_size
27
+ hash[:inactive] = get_by_name('vm.stats.vm.v_inactive_count') * page_size
28
+ hash[:wire] = get_by_name('vm.stats.vm.v_wire_count') * page_size
29
+ hash[:swap_size] = get_by_name('vm.swap_size')
30
+ hash[:swap_free] = get_by_name('vm.swap_free')
31
+
32
+ hash
33
+ end
34
+
35
+ # Total memory in bytes. By default this is only physical memory, but
36
+ # if the +extended+ option is set to true, then swap memory is included as
37
+ # part of the total.
38
+ #
39
+ def total(extended: false)
40
+ hash = memory
41
+ extended ? hash[:total] + hash[:swap_size] : hash[:total]
42
+ end
43
+
44
+ # The memory currently available, in bytes. By default this is only
45
+ # physical memory, but if the +extended+ option is set to true, then free
46
+ # swap memory is also included.
47
+ #
48
+ def free(extended: false)
49
+ hash = memory
50
+ extended ? hash[:free] + hash[:swap_free] : hash[:free]
51
+ end
52
+
53
+ # The memory, in bytes, currently in use. By default this is only
54
+ # physical memory, but if the +extended+ option is set to true then
55
+ # swap is included in the calculation.
56
+ #
57
+ def used(extended: false)
58
+ total(extended: extended) - free(extended: extended)
59
+ end
60
+
61
+ # A number between 0 and 100 that specifies the approximate percentage of
62
+ # memory that is in use. If the +extended+ option is set to true then
63
+ # swap memory is included in the calculation.
64
+ #
65
+ def load(extended: false)
66
+ (used(extended: extended) / total(extended: extended).to_f).round(2) * 100
67
+ end
68
+
69
+ module_function :memory, :total, :free, :load, :used
70
+
71
+ private
72
+
73
+ def get_by_name(mib)
74
+ value = nil
75
+
76
+ begin
77
+ optr = FFI::MemoryPointer.new(:uint64_t)
78
+ size = FFI::MemoryPointer.new(:size_t)
79
+ size.write_int(optr.size)
80
+
81
+ if sysctlbyname(mib, optr, size, nil, 0) < 0
82
+ raise SystemCallError.new('sysctlbyname', FFI.errno)
83
+ end
84
+
85
+ value = optr.read_uint64
86
+ ensure
87
+ optr.free if optr && !optr.null?
88
+ size.free if size && !size.null?
89
+ end
90
+
91
+ value
92
+ end
93
+
94
+ module_function :get_by_name
95
+ end
96
+ end
@@ -50,7 +50,7 @@ module Sys
50
50
  #
51
51
  def used(extended: false)
52
52
  hash = memory
53
- (total(extended: extended) - free(extended: extended) - hash['Buffers'] - hash['Cached'] - hash['Slab']) * 1024
53
+ total(extended: extended) - free(extended: extended) - (hash['Buffers'] + hash['Cached'] + hash['Slab']) * 1024
54
54
  end
55
55
 
56
56
  # A number between 0 and 100 that specifies the approximate percentage of
data/lib/sys/memory.rb CHANGED
@@ -8,6 +8,8 @@ case RbConfig::CONFIG['host_os']
8
8
  require_relative 'linux/memory'
9
9
  when /darwin|macos/i
10
10
  require_relative 'osx/memory'
11
+ when /bsd|dragonfly/i
12
+ require_relative 'bsd/memory'
11
13
  when /windows|win32|mingw/i
12
14
  require_relative 'windows/memory'
13
15
  end
data/lib/sys/version.rb CHANGED
@@ -3,6 +3,6 @@
3
3
  module Sys
4
4
  module Memory
5
5
  # The version of the sys-memory library.
6
- VERSION = '0.1.2'
6
+ VERSION = '0.2.0'
7
7
  end
8
8
  end
@@ -6,7 +6,7 @@ require 'sys-memory'
6
6
  RSpec.describe Sys::Memory do
7
7
  context 'Sys::Memory::VERSION' do
8
8
  example 'the version constant is set to the expected value' do
9
- expect(described_class::VERSION).to eq('0.1.2')
9
+ expect(described_class::VERSION).to eq('0.2.0')
10
10
  expect(described_class::VERSION).to be_frozen
11
11
  end
12
12
  end
data/sys-memory.gemspec CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'sys-memory'
5
- spec.version = '0.1.2'
5
+ spec.version = '0.2.0'
6
6
  spec.author = 'Daniel J. Berger'
7
7
  spec.email = 'djberg96@gmail.com'
8
8
  spec.license = 'Apache-2.0'
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sys-memory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel J. Berger
@@ -35,7 +35,7 @@ cert_chain:
35
35
  ORVCZpRuCPpmC8qmqxUnARDArzucjaclkxjLWvCVHeFa9UP7K3Nl9oTjJNv+7/jM
36
36
  WZs4eecIcUc4tKdHxcAJ0MO/Dkqq7hGaiHpwKY76wQ1+8xAh
37
37
  -----END CERTIFICATE-----
38
- date: 2023-04-10 00:00:00.000000000 Z
38
+ date: 2024-06-12 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: ffi
@@ -138,6 +138,7 @@ files:
138
138
  - Rakefile
139
139
  - certs/djberg96_pub.pem
140
140
  - lib/sys-memory.rb
141
+ - lib/sys/bsd/memory.rb
141
142
  - lib/sys/linux/memory.rb
142
143
  - lib/sys/memory.rb
143
144
  - lib/sys/osx/memory.rb
metadata.gz.sig CHANGED
Binary file