<div xmlns="http://www.w3.org/1999/xhtml"><p align="justify" style="margin-bottom:0.28cm;line-height:108%;"><font size="2" style="font-size:10pt;">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.</font></p><p style="margin-bottom:0cm;line-height:100%;"> </p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">irb(main):002:0> conn = Libvirt::open("qemu:///system")</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">=> #<Libvirt::Connect:0x000055d1c66de398></font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">irb(main):003:0> domain = conn.lookup_domain_by_name("2_2").memory_stats</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">=> [#<Libvirt::Domain::MemoryStats:0x000055d1c6621a68 @tag=6, @val=1048576>, #<Libvirt::Domain::MemoryStats:0x000055d1c6621978 @tag=7, @val=1180796>]</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">irb(main):004:0> domain.first.val</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">Traceback (most recent call last):</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">16: from /root/.rbenv/versions/2.5.1/bin/bundle:23:in `load'</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">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)>'</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">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'</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">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)>'</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">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'</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">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'</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">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'</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">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'</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">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'</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">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'</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">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'</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">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'</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">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'</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">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'</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">2: from bin/console:13:in `<top (required)>'</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">1: from (irb):4</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">NoMethodError (undefined method `val' for #<Libvirt::Domain::MemoryStats:0x000055d1c6621a68>)</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">Did you mean? value</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">irb(main):005:0> domain.first.value</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">=> nil</font></p><p style="margin-bottom:0.28cm;line-height:108%;"><br /> </p><p style="margin-bottom:0.28cm;line-height:108%;"><font size="2" style="font-size:10pt;">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.</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">diff --git a/ext/libvirt/domain.c b/ext/libvirt/domain.c</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">index d665907..d458e98 100644</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">--- a/ext/libvirt/domain.c</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">+++ b/ext/libvirt/domain.c</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">@@ -675,7 +675,7 @@ static VALUE libvirt_domain_memory_stats(int argc, VALUE *argv, VALUE d)</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">for (i = 0; i < r; i++) {</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">tmp = rb_class_new_instance(0, NULL, c_domain_memory_stats);</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">rb_iv_set(tmp, "@tag", INT2NUM(stats[i].tag));</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">- rb_iv_set(tmp, "@val", ULL2NUM(stats[i].val));</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">+ rb_iv_set(tmp, "@value", ULL2NUM(stats[i].val));</font></p><p style="margin-bottom:0cm;line-height:100%;"> </p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">rb_ary_store(result, i, tmp);</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">}</font></p><p style="margin-bottom:0cm;line-height:100%;"> </p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">After that change memory_stats method’s val error will solved as shown in the below.</font></p><p style="margin-bottom:0cm;line-height:100%;"> </p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">irb(main):002:0> conn = Libvirt::open("qemu:///system")</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">=> #<Libvirt::Connect:0x0000562ecf943320></font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">irb(main):003:0> memory_stats = conn.lookup_domain_by_name("2_2").memory_stats</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">=> [#<Libvirt::Domain::MemoryStats:0x0000562ecf976f68 @tag=6, @value=1048576>, #<Libvirt::Domain::MemoryStats:0x0000562ecf976f40 @tag=7, @value=1182952>]</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">irb(main):004:0> memory_stats.first.value</font></p><p style="margin-bottom:0cm;line-height:100%;"><font size="2" style="font-size:10pt;">=> 1048576</font></p></div>