libvirt_ffi 0.1.0 → 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: 54cfaffc6e2d9a5efb229e0c47cf0222fd99521ff5fa5ec0af12fdcffe338248
4
- data.tar.gz: 46b545e97737a7c189a601939d211107a77adc92c197e163f6fa3066930b71a4
3
+ metadata.gz: 8a956033faefb88d11d76587ed18a9ef066f15ecf12321d97611f53469fc8e55
4
+ data.tar.gz: f16da9c9e1311edccf72c539591a0b0892f70f36aca7fa7718ce396ab2cc5826
5
5
  SHA512:
6
- metadata.gz: e0f41521b95ae1c940b4da9bd2ebc229d86b220afc508b66bb74de8cdc7404a7078559d3f4d3a9fdae9cf30c67c402557e811b353d91fc34f6e4465aeb1eb3cd
7
- data.tar.gz: 45e726be8f9049df8e87bfb768b60058bbd4dd3be55b1808e0a4a60fb9014188853d4af5f123de6baeded56006e371e863e11e7fe38b9ad3ef4643b27829d0b1
6
+ metadata.gz: 39539a7d0868f78843d388fefc3b398406a6cbf3890dd7667e8484280098c9ba2c3af45659280b338ee1f0c93ee9c45a4ffdf6246db64e7c5571d0b43abd0c39
7
+ data.tar.gz: 5a8502473cde37b1538ec2ead69d872f989b66052d2cc7c93f766557591b4b5b807f7a45e31927a7b07b9b1361ecd9f8b9d827b3b7768f19f1694e46ef8a5e42
@@ -1,10 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'objspace'
4
- require 'libvirt/ffi/connection'
5
- require 'libvirt/ffi/domain'
6
- require 'libvirt/util'
7
-
8
3
  module Libvirt
9
4
  class Connection
10
5
  def initialize(uri)
@@ -98,6 +93,44 @@ module Libvirt
98
93
  true
99
94
  end
100
95
 
96
+ def version
97
+ version_ptr = ::FFI::MemoryPointer.new(:ulong)
98
+ result = FFI::Connection.virConnectGetVersion(@conn_ptr, version_ptr)
99
+ raise Error, "Couldn't get connection version" if result < 0
100
+ version_number = version_ptr.get_ulong(0)
101
+ # version_number = FFI::Connection.virConnectGetVersion(@conn_ptr)
102
+ Libvirt::Util.parse_version(version_number)
103
+ end
104
+
105
+ def lib_version
106
+ version_ptr = ::FFI::MemoryPointer.new(:ulong)
107
+ result = FFI::Connection.virConnectGetLibVersion(@conn_ptr, version_ptr)
108
+ raise Error, "Couldn't get connection lib version" if result < 0
109
+ version_number = version_ptr.get_ulong(0)
110
+ # version_number = FFI::Connection.virConnectGetLibVersion(@conn_ptr)
111
+ Libvirt::Util.parse_version(version_number)
112
+ end
113
+
114
+ def hostname
115
+ FFI::Connection.virConnectGetHostname(@conn_ptr)
116
+ end
117
+
118
+ # @param type [String,NilClass]
119
+ def max_vcpus(type = nil)
120
+ FFI::Connection.virConnectGetMaxVcpus(@conn_ptr, type)
121
+ end
122
+
123
+ def capabilities
124
+ FFI::Connection.virConnectGetCapabilities(@conn_ptr)
125
+ end
126
+
127
+ def node_info
128
+ node_info_ptr = ::FFI::MemoryPointer.new(FFI::NodeInfo::Struct.by_value)
129
+ result = FFI::NodeInfo.virNodeGetInfo(@conn_ptr, node_info_ptr)
130
+ raise Error, "Couldn't get connection node info" if result < 0
131
+ NodeInfo.new(node_info_ptr)
132
+ end
133
+
101
134
  private
102
135
 
103
136
  def check_open!
@@ -1,9 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'objspace'
4
- require 'libvirt/ffi/domain'
5
- require 'libvirt/util'
6
-
7
3
  module Libvirt
8
4
  class Domain
9
5
  def initialize(dom_ptr, conn)
@@ -25,5 +21,40 @@ module Libvirt
25
21
  def to_ptr
26
22
  @dom_ptr
27
23
  end
24
+
25
+ def uuid
26
+ buff = ::FFI::MemoryPointer.new(:char, FFI::Domain::UUID_STRING_BUFLEN)
27
+ result = FFI::Domain.virDomainGetUUIDString(@dom_ptr, buff)
28
+ raise Error, "Couldn't get domain uuid" if result < 0
29
+ buff.read_string
30
+ end
31
+
32
+ def name
33
+ FFI::Domain.virDomainGetName(@dom_ptr)
34
+ end
35
+
36
+ def max_vcpus
37
+ FFI::Domain.virDomainGetMaxVcpus(@dom_ptr)
38
+ end
39
+
40
+ # def vcpus
41
+ # # https://github.com/libvirt/ruby-libvirt/blob/9f71ff5add1f57ffef7cf513b72638d92d9fd84f/ext/libvirt/domain.c#L787
42
+ # # dominfo = virDomainGetInfo
43
+ # # dominfo.nrVirtCpu
44
+ # # maxcpus = ruby_libvirt_get_maxcpus(ruby_libvirt_connect_get(d));
45
+ # # vcpu_infos_ptr
46
+ # FFI::Domain.virDomainGetVcpus(@dom_ptr, vcpu_infos_ptr, maxinfo, cpumaps, maplen)
47
+ # end
48
+ def vcpus
49
+ OpenStruct.new(count: max_vcpus)
50
+ end
51
+
52
+ def max_memory
53
+ FFI::Domain.virDomainGetMaxMemory(@dom_ptr)
54
+ end
55
+
56
+ def xml_desc(flags = 0)
57
+ FFI::Domain.virDomainGetXMLDesc(@dom_ptr, flags)
58
+ end
28
59
  end
29
60
  end
data/lib/libvirt/event.rb CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  require 'singleton'
4
4
  require 'forwardable'
5
- require 'libvirt/ffi/event'
6
5
 
7
6
  module Libvirt
8
7
  class Event
@@ -1,14 +1,44 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'ffi'
4
- require 'libvirt/util'
5
-
6
3
  module Libvirt
7
4
  module FFI
8
5
  module Connection
9
6
  extend ::FFI::Library
10
7
  ffi_lib Util.library_path
11
8
 
9
+ # struct virNodeInfo {
10
+ #
11
+ # char model[32] model - string indicating the CPU model
12
+ # unsigned long memory - memory size in kilobytes
13
+ # unsigned int cpus - the number of active CPUs
14
+ # unsigned int mhz - expected CPU frequency, 0 if not known or on unusual architectures
15
+ # unsigned int nodes - the number of NUMA cell, 1 for unusual NUMA topologies or uniform memory access; check capabilities XML for the actual NUMA topology
16
+ # unsigned int sockets - number of CPU sockets per node if nodes > 1, 1 in case of unusual NUMA topology
17
+ # unsigned int cores - number of cores per socket, total number of processors in case of unusual NUMA topolog
18
+ # unsigned int threads - number of threads per core, 1 in case of unusual numa topology
19
+ # }
20
+ class NodeInfoStruct < ::FFI::Struct
21
+ layout :model, [:char, 32],
22
+ :memory, :ulong,
23
+ :cpus, :ulong,
24
+ :mhz, :ulong,
25
+ :nodes, :ulong,
26
+ :sockets, :ulong,
27
+ :cores, :ulong,
28
+ :threads, :ulong
29
+ end
30
+
31
+ class NodeInfo
32
+ def initialize(node_info_ptr, node_info_struct)
33
+ @node_info_ptr = node_info_ptr
34
+ @node_info_struct = node_info_struct
35
+ end
36
+
37
+ def [](attr)
38
+ @node_info_struct[attr]
39
+ end
40
+ end
41
+
12
42
  # virConnectPtr virConnectOpen (const char * name)
13
43
  attach_function :virConnectOpen, [:string], :pointer
14
44
 
@@ -21,6 +51,34 @@ module Libvirt
21
51
  # unsigned int count
22
52
  # )
23
53
  attach_function :virConnectSetKeepAlive, [:pointer, :int, :uint], :int
54
+
55
+ # int virConnectGetVersion (
56
+ # virConnectPtr conn,
57
+ # unsigned long * hvVer
58
+ # )
59
+ attach_function :virConnectGetVersion, [:pointer, :pointer], :int
60
+
61
+ # int virConnectGetLibVersion (
62
+ # virConnectPtr conn,
63
+ # unsigned long * libVer
64
+ # )
65
+ attach_function :virConnectGetLibVersion, [:pointer, :pointer], :int
66
+
67
+ # char * virConnectGetHostname (
68
+ # virConnectPtr conn
69
+ # )
70
+ attach_function :virConnectGetHostname, [:pointer], :string # strptr ?
71
+
72
+ # int virConnectGetMaxVcpus (
73
+ # virConnectPtr conn,
74
+ # const char * type
75
+ # )
76
+ attach_function :virConnectGetMaxVcpus, [:pointer, :string], :int
77
+
78
+ # char * virConnectGetCapabilities (
79
+ # virConnectPtr conn
80
+ # )
81
+ attach_function :virConnectGetCapabilities, [:pointer], :string # strptr ?
24
82
  end
25
83
  end
26
84
  end
@@ -1,15 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'ffi'
4
- require 'libvirt/util'
5
- require 'libvirt/ffi/common'
6
-
7
3
  module Libvirt
8
4
  module FFI
9
5
  module Domain
10
6
  extend ::FFI::Library
11
7
  ffi_lib Util.library_path
12
8
 
9
+ UUID_STRING_BUFLEN = 0x80 # RFC4122
10
+
13
11
  # int virConnectDomainEventRegisterAny(
14
12
  # virConnectPtr conn,
15
13
  # virDomainPtr dom,
@@ -75,6 +73,42 @@ module Libvirt
75
73
  # )
76
74
  attach_function :virDomainGetState, [:pointer, :pointer, :pointer, :uint], :int
77
75
 
76
+ # const char *virDomainGetName (
77
+ # virDomainPtr domain
78
+ # )
79
+ attach_function :virDomainGetName, [:pointer], :string # strptr?
80
+
81
+ # int virDomainGetMaxVcpus (
82
+ # virDomainPtr domain
83
+ # )
84
+ attach_function :virDomainGetMaxVcpus, [:pointer], :int
85
+
86
+ # int virDomainGetVcpus (
87
+ # virDomainPtr domain,
88
+ # virVcpuInfoPtr info,
89
+ # int maxinfo,
90
+ # unsigned char * cpumaps,
91
+ # int maplen
92
+ # )
93
+ attach_function :virDomainGetVcpus, [:pointer, :pointer, :int, :pointer, :int], :int
94
+
95
+ # int virDomainGetUUIDString (
96
+ # virDomainPtr domain,
97
+ # char * buf
98
+ # )
99
+ attach_function :virDomainGetUUIDString, [:pointer, :pointer], :int
100
+
101
+ # unsigned long virDomainGetMaxMemory (
102
+ # virDomainPtr domain
103
+ # )
104
+ attach_function :virDomainGetMaxMemory, [:pointer], :ulong
105
+
106
+ # char *virDomainGetXMLDesc (
107
+ # virDomainPtr domain,
108
+ # unsigned int flags
109
+ # )
110
+ attach_function :virDomainGetXMLDesc, [:pointer, :uint], :string # strptr?
111
+
78
112
  # typedef int (*virConnectDomainEventCallback) (
79
113
  # virConnectPtr conn,
80
114
  # virDomainPtr dom,
@@ -1,9 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'ffi'
4
- require 'libvirt/util'
5
- require 'libvirt/ffi/common'
6
-
7
3
  module Libvirt
8
4
  module FFI
9
5
  module Event
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'ffi'
4
- require 'libvirt/util'
5
-
6
3
  module Libvirt
7
4
  module FFI
8
5
  module Libvirt
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Libvirt
4
+ module FFI
5
+ module NodeInfo
6
+ extend ::FFI::Library
7
+ ffi_lib Util.library_path
8
+
9
+ # struct virNodeInfo {
10
+ #
11
+ # char model[32] model - string indicating the CPU model
12
+ # unsigned long memory - memory size in kilobytes
13
+ # unsigned int cpus - the number of active CPUs
14
+ # unsigned int mhz - expected CPU frequency, 0 if not known or on unusual architectures
15
+ # unsigned int nodes - the number of NUMA cell, 1 for unusual NUMA topologies or uniform memory access;
16
+ # check capabilities XML for the actual NUMA topology
17
+ # unsigned int sockets - number of CPU sockets per node if nodes > 1, 1 in case of unusual NUMA topology
18
+ # unsigned int cores - number of cores per socket, total number of processors in case of unusual NUMA topolog
19
+ # unsigned int threads - number of threads per core, 1 in case of unusual numa topology
20
+ # }
21
+ class Struct < ::FFI::Struct
22
+ layout :model, [:char, 32],
23
+ :memory, :ulong,
24
+ :cpus, :uint,
25
+ :mhz, :uint,
26
+ :nodes, :uint,
27
+ :sockets, :uint,
28
+ :cores, :uint,
29
+ :threads, :uint
30
+ end
31
+
32
+ # int virNodeGetInfo (virConnectPtr conn,
33
+ # virNodeInfoPtr info)
34
+ attach_function :virNodeGetInfo, [:pointer, :pointer], :int
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Libvirt
4
+ class NodeInfo
5
+ def initialize(node_info_ptr)
6
+ @node_info_ptr = node_info_ptr
7
+ @node_info_struct = FFI::NodeInfo::Struct.new(node_info_ptr)
8
+ end
9
+
10
+ def [](attr)
11
+ @node_info_struct[attr]
12
+ end
13
+
14
+ def model
15
+ @node_info_struct[:model].to_s
16
+ end
17
+
18
+ def cpus
19
+ @node_info_struct[:cpus]
20
+ end
21
+
22
+ def mhz
23
+ @node_info_struct[:mhz]
24
+ end
25
+
26
+ def nodes
27
+ @node_info_struct[:nodes]
28
+ end
29
+
30
+ def sockets
31
+ @node_info_struct[:sockets]
32
+ end
33
+
34
+ def cores
35
+ @node_info_struct[:cores]
36
+ end
37
+
38
+ def threads
39
+ @node_info_struct[:threads]
40
+ end
41
+
42
+ def memory
43
+ @node_info_struct[:memory]
44
+ end
45
+ end
46
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Libvirt
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/libvirt.rb CHANGED
@@ -1,15 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'ffi'
4
+ require 'objspace'
4
5
  require 'libvirt/util'
5
6
  require 'libvirt/error'
6
7
  require 'libvirt/ffi/common'
8
+ require 'libvirt/ffi/libvirt'
7
9
  require 'libvirt/ffi/connection'
8
10
  require 'libvirt/ffi/domain'
9
- require 'libvirt/lib_version'
11
+ require 'libvirt/ffi/event'
12
+ require 'libvirt/ffi/node_info'
10
13
  require 'libvirt/event'
11
14
  require 'libvirt/connection'
12
15
  require 'libvirt/domain'
16
+ require 'libvirt/node_info'
13
17
  require 'libvirt/version'
14
18
 
15
19
  module Libvirt
@@ -20,6 +20,9 @@ DOMS = []
20
20
  Async do
21
21
  ASYNC_REACTOR = Async::Task.current.reactor
22
22
 
23
+ puts "Lib version #{Libvirt.lib_version}"
24
+ puts "Gem version #{Libvirt::VERSION}"
25
+
23
26
  IMPL.start
24
27
 
25
28
  c = Libvirt::Connection.new('qemu+tcp://localhost:16510/system')
@@ -28,6 +31,22 @@ Async do
28
31
  Libvirt.logger.info { "set_keep_alive #{res}" }
29
32
  CONNS.push(c)
30
33
 
34
+ puts "Connection version #{c.version.inspect}"
35
+ puts "Connection lib_version #{c.lib_version.inspect}"
36
+ puts "Connection hostname #{c.hostname.inspect}"
37
+ puts "Connection max_vcpus #{c.max_vcpus.inspect}"
38
+ puts "Connection capabilities #{c.capabilities.inspect}"
39
+ node_info = c.node_info
40
+ puts "Connection nodeInfo #{node_info}"
41
+ puts "NodeInfo model #{node_info.model.inspect}"
42
+ puts "NodeInfo cpus #{node_info.cpus.inspect}"
43
+ puts "NodeInfo mhz #{node_info.mhz.inspect}"
44
+ puts "NodeInfo nodes #{node_info.nodes.inspect}"
45
+ puts "NodeInfo sockets #{node_info.sockets.inspect}"
46
+ puts "NodeInfo cores #{node_info.cores.inspect}"
47
+ puts "NodeInfo threads #{node_info.threads.inspect}"
48
+ puts "NodeInfo memory #{node_info.memory.inspect}"
49
+
31
50
  c.register_domain_event_callback(Libvirt::DOMAIN_EVENT_ID_LIFECYCLE, nil) do |dom, event, detail, opaque|
32
51
  Libvirt.logger.info { "DOMAIN_EVENT_ID_LIFECYCLE user dom=#{dom}, event=#{event}, detail=#{detail}, opaque=#{opaque}" }
33
52
  end
@@ -44,17 +63,22 @@ Async do
44
63
  end
45
64
  end
46
65
 
47
- res = domains.first.get_state
48
- Libvirt.logger.info { "Domain #{domains.first} state #{res}" }
66
+ d = domains.first
67
+ puts "Domain uuid #{d.uuid.inspect}"
68
+ puts "Domain name #{d.name.inspect}"
69
+ puts "Domain get_state #{d.get_state.inspect}"
70
+ puts "Domain get_cpus #{d.max_vcpus.inspect}"
71
+ puts "Domain max_memory #{d.max_memory.inspect}"
72
+ puts "Domain xml_desc #{d.xml_desc.inspect}"
49
73
 
50
74
  # ASYNC_REACTOR.every(10) do
51
75
  # LibvirtAsync::Util.create_task(nil, ASYNC_REACTOR) { IMPL.print_debug_info }.run
52
76
  # end
53
77
 
54
- ASYNC_REACTOR.every(5) do
55
- Libvirt.logger.info { "MEM USAGE: #{GetProcessMem.new.mb} MB" }
56
- Libvirt.logger.info { "GC.start" }
57
- GC.start
58
- Libvirt.logger.info { "MEM USAGE: #{GetProcessMem.new.mb} MB" }
59
- end
78
+ # ASYNC_REACTOR.every(5) do
79
+ # Libvirt.logger.info { "MEM USAGE: #{GetProcessMem.new.mb} MB" }
80
+ # Libvirt.logger.info { "GC.start" }
81
+ # GC.start
82
+ # Libvirt.logger.info { "MEM USAGE: #{GetProcessMem.new.mb} MB" }
83
+ # end
60
84
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libvirt_ffi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Talakevich
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-01-27 00:00:00.000000000 Z
11
+ date: 2020-01-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -52,7 +52,8 @@ files:
52
52
  - lib/libvirt/ffi/domain.rb
53
53
  - lib/libvirt/ffi/event.rb
54
54
  - lib/libvirt/ffi/libvirt.rb
55
- - lib/libvirt/lib_version.rb
55
+ - lib/libvirt/ffi/node_info.rb
56
+ - lib/libvirt/node_info.rb
56
57
  - lib/libvirt/util.rb
57
58
  - lib/libvirt/version.rb
58
59
  - lib/libvirt_ffi.rb
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'ffi'
4
- require 'libvirt/ffi/libvirt'
5
- require 'libvirt/util'
6
-
7
- module Libvirt
8
- module LibVersion
9
- def self.call
10
-
11
- end
12
- end
13
- end