How to obtain the length of the memory statistics array when calling virDomainMemoryStats

Up to ‘nr_stats’ elements of ‘stats’ will be populated with memory statistics from the domain. Only statistics supported by the domain, the driver, and this version of libvirt will be returned.

What does the above API description even mean by nr_stats? How do you determine the number of elements that need to be populated?

For the second part of Project 1 my advanced operating systems course, I need to collect memory statistics for each guest operating system (i.e. domain) running on the hypervisor. In order to query each domain for its memory usage, I need to call the virDomainStats function offered by libvrt and pass in a structure with a length of n elements. But how do you determine the number of elements?

Determining length of virDomainMemoryStats

The enum value VIR_DOMAIN_MEMORY_STAT_NR can be used to determine the length of the statistics array. That value, part of the enum virDomainMemoryStatTags1, represents the last type of memory statistic offered by the libvrt library.

I figured this out after searching on Google and stumbling on an open source project called collectd2the source code making use of the libvrt library.