sys-memory 0.1.3 → 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: ad8db02636f7f2e60a3cc6a56365b10eecfa117825aa4ce7992f8f8bf2a9ae61
4
- data.tar.gz: b336bd31a553a7b2e3a3ca24848152c5f73bf913f0bfdce402ae7164717050ba
3
+ metadata.gz: 9c4673911e1763e8282380312020206b04a9fb268572d3b64e8fc0a631befa03
4
+ data.tar.gz: 28e0b52dd179b98a4896dab5442bac98beb538fea493d0db5ea113b6148ed70d
5
5
  SHA512:
6
- metadata.gz: 6634473e9c7ca86e19bbd341e88beff8d8094be5e64a7228049599171f31d486a52db9d0ef2c1e0dc2ffe040639936f3a0b1f639e0960cdc03760ef26ba64ec7
7
- data.tar.gz: e23244aacf479c8936a443458c6b584f9013fe8882bf23e7a18647ebda78fb79d9ea2f1006dc8e5760a524c64dd89a9b84f6bca428f155e966ba350a5fdc5f8f
6
+ metadata.gz: f6b1f2cf5a71c94be2dc997fb7e4c3d91a2d32c83af4b7399cba169c8ef36d20382decb1d35e020d7c7b7f7651957d1a1deb105e748db66087f071aae20879a8
7
+ data.tar.gz: 0c4f87e99e7b69148c0abcbfed29217a2fac229eff5237a3f632bfd2994dc89d6b432c63169a9e5bef1ab1f432f81330617bddef72f6a3092770fbcaaac49eb6
checksums.yaml.gz.sig CHANGED
Binary file
data/CHANGES.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.2.0 - 12-Jun-2024
2
+ * Added support for DragonflyBSD. Currently the default implementation
3
+ for all BSD platforms.
4
+
1
5
  ## 0.1.3 - 12-Dec-2023
2
6
  * Fix "used" calculations on Linux. Thanks go to Splendide-Imaginarius
3
7
  for the spot and the patch.
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
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.3'
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.3')
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.3'
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.3
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-12-12 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