int main(void) { xenstat_handle *handle = xenstat_init(); if(!handle) { fputs("Internal error in xenstats, failed to initialize handle\n", stderr); exit(1); } xenstat_node *node = xenstat_get_node(handle, XENSTAT_ALL); if(!node) { fputs("Failed to get statistics from libxenlight\n", stderr); exit(2); } printf("<<<xen_info>>>\n"); printf("version\t%s\n", xenstat_node_xen_version(node)); printf("total_memory\t%llu\n", xenstat_node_tot_mem(node)); printf("free_memory\t%llu\n", xenstat_node_free_mem(node)); // printf("freeable_memory\t%li\n", xenstat_node_freeable_mb(node)); printf("cpu_amount\t%i\n", xenstat_node_num_cpus(node)); printf("cpu_speed\t%llu\n", xenstat_node_cpu_hz(node)); int domains_amount = xenstat_node_num_domains(node); if(!domains_amount) exit(0); printf("domains\t%i\n", domains_amount); xenstat_domain** domains = malloc(sizeof(xenstat_domain*) * domains_amount); for(int i=0; i<domains_amount; i++) { xenstat_domain* domain = xenstat_node_domain_by_index(node, i); if(xenstat_domain_id(domain)) { printf("<<<<%s>>>>\n", xenstat_domain_name(domain)); } else{ printf("<<<<>>>>\n"); } printf("<<<xen_domain>>>\n"); printf("id\t%u\n", xenstat_domain_id(domain)); } return 0; }
/* Computes the CPU percentage used for a specified domain */ static double get_cpu_pct(xenstat_domain *domain) { xenstat_domain *old_domain; double us_elapsed; /* Can't calculate CPU percentage without a previous sample. */ if(prev_node == NULL) return 0.0; old_domain = xenstat_node_domain(prev_node, xenstat_domain_id(domain)); if(old_domain == NULL) return 0.0; /* Calculate the time elapsed in microseconds */ us_elapsed = ((curtime.tv_sec-oldtime.tv_sec)*1000000.0 +(curtime.tv_usec - oldtime.tv_usec)); /* In the following, nanoseconds must be multiplied by 1000.0 to * convert to microseconds, then divided by 100.0 to get a percentage, * resulting in a multiplication by 10.0 */ return ((xenstat_domain_cpu_ns(domain) -xenstat_domain_cpu_ns(old_domain))/10.0)/us_elapsed; }