sys-memory 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/CHANGES.md +4 -0
- data/README.md +4 -1
- data/lib/sys/bsd/memory.rb +96 -0
- data/lib/sys/memory.rb +2 -0
- data/lib/sys/version.rb +1 -1
- data/spec/sys_memory_spec.rb +1 -1
- data/sys-memory.gemspec +1 -1
- data.tar.gz.sig +0 -0
- metadata +3 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c4673911e1763e8282380312020206b04a9fb268572d3b64e8fc0a631befa03
|
4
|
+
data.tar.gz: 28e0b52dd179b98a4896dab5442bac98beb538fea493d0db5ea113b6148ed70d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f6b1f2cf5a71c94be2dc997fb7e4c3d91a2d32c83af4b7399cba169c8ef36d20382decb1d35e020d7c7b7f7651957d1a1deb105e748db66087f071aae20879a8
|
7
|
+
data.tar.gz: 0c4f87e99e7b69148c0abcbfed29217a2fac229eff5237a3f632bfd2994dc89d6b432c63169a9e5bef1ab1f432f81330617bddef72f6a3092770fbcaaac49eb6
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/CHANGES.md
CHANGED
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-
|
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
data/lib/sys/version.rb
CHANGED
data/spec/sys_memory_spec.rb
CHANGED
@@ -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.
|
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
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.
|
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:
|
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
|