Hi libvirt comuninty’s developers. I will try the fast explain. We’re working on kvm for a project and we tried get current machine’s memory usages. The code we tried to use for this operation is as shown in the below.
irb(main):002:0> conn = Libvirt::open("qemu:///system")
=> #<Libvirt::Connect:0x000055d1c66de398>
irb(main):003:0> domain = conn.lookup_domain_by_name("2_2").memory_stats
=> [#<Libvirt::Domain::MemoryStats:0x000055d1c6621a68 @tag=6, @val=1048576>, #<Libvirt::Domain::MemoryStats:0x000055d1c6621978 @tag=7, @val=1180796>]
irb(main):004:0> domain.first.val
Traceback (most recent call last):
16: from /root/.rbenv/versions/2.5.1/bin/bundle:23:in `load'
15: from /root/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/exe/bundle:22:in `<top (required)>'
14: from /root/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/friendly_errors.rb:124:in `with_friendly_errors'
13: from /root/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/exe/bundle:30:in `block in <top (required)>'
12: from /root/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/cli.rb:18:in `start'
11: from /root/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
10: from /root/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/cli.rb:27:in `dispatch'
9: from /root/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
8: from /root/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
7: from /root/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
6: from /root/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/cli.rb:424:in `exec'
5: from /root/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/cli/exec.rb:28:in `run'
4: from /root/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/cli/exec.rb:74:in `kernel_load'
3: from /root/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/bundler-1.16.6/lib/bundler/cli/exec.rb:74:in `load'
2: from bin/console:13:in `<top (required)>'
1: from (irb):4
NoMethodError (undefined method `val' for #<Libvirt::Domain::MemoryStats:0x000055d1c6621a68>)
Did you mean? value
irb(main):005:0> domain.first.value
=> nil
As you see, when we want the use memory_usage’s val parameter but Libvirt throwing up undefined method error. Our fix proffer is memory_stats function's return @val parameter enough be update as shown in the below.
diff --git a/ext/libvirt/domain.c b/ext/libvirt/domain.c
index d665907..d458e98 100644
--- a/ext/libvirt/domain.c
+++ b/ext/libvirt/domain.c
@@ -675,7 +675,7 @@ static VALUE libvirt_domain_memory_stats(int argc, VALUE *argv, VALUE d)
for (i = 0; i < r; i++) {
tmp = rb_class_new_instance(0, NULL, c_domain_memory_stats);
rb_iv_set(tmp, "@tag", INT2NUM(stats[i].tag));
- rb_iv_set(tmp, "@val", ULL2NUM(stats[i].val));
+ rb_iv_set(tmp, "@value", ULL2NUM(stats[i].val));
rb_ary_store(result, i, tmp);
}
After that change memory_stats method’s val error will solved as shown in the below.
irb(main):002:0> conn = Libvirt::open("qemu:///system")
=> #<Libvirt::Connect:0x0000562ecf943320>
irb(main):003:0> memory_stats = conn.lookup_domain_by_name("2_2").memory_stats
=> [#<Libvirt::Domain::MemoryStats:0x0000562ecf976f68 @tag=6, @value=1048576>, #<Libvirt::Domain::MemoryStats:0x0000562ecf976f40 @tag=7, @value=1182952>]
irb(main):004:0> memory_stats.first.value
=> 1048576