/* Clean up any open resources */ static void cleanup(void) { if(cwin != NULL && !isendwin()) endwin(); if(prev_node != NULL) xenstat_free_node(prev_node); if(cur_node != NULL) xenstat_free_node(cur_node); if(xhandle != NULL) xenstat_uninit(xhandle); }
static void top(void) { xenstat_domain **domains; unsigned int i, num_domains = 0; /* Now get the node information */ if (prev_node != NULL) xenstat_free_node(prev_node); prev_node = cur_node; cur_node = xenstat_get_node(xhandle, XENSTAT_ALL); if (cur_node == NULL) fail("Failed to retrieve statistics from libxenstat\n"); /* dump summary top information */ if (!batch) do_summary(); /* Count the number of domains for which to report data */ num_domains = xenstat_node_num_domains(cur_node); domains = calloc(num_domains, sizeof(xenstat_domain *)); if(domains == NULL) fail("Failed to allocate memory\n"); for (i=0; i < num_domains; i++) domains[i] = xenstat_node_domain_by_index(cur_node, i); /* Sort */ qsort(domains, num_domains, sizeof(xenstat_domain *), (int(*)(const void *, const void *))compare_domains); if(first_domain_index >= num_domains) first_domain_index = num_domains-1; for (i = first_domain_index; i < num_domains; i++) { if(!batch && current_row() == lines()-1) break; if (i == first_domain_index || repeat_header) do_header(); do_domain(domains[i]); if (show_vcpus) do_vcpu(domains[i]); if (show_networks) do_network(domains[i]); if (show_vbds) do_vbd(domains[i]); if (show_tmem) do_tmem(domains[i]); } if (!batch) do_bottom_line(); free(domains); }
xenstat_node *xenstat_get_node(xenstat_handle * handle, unsigned int flags) { #define DOMAIN_CHUNK_SIZE 256 xenstat_node *node; xc_physinfo_t physinfo = { 0 }; xc_domaininfo_t domaininfo[DOMAIN_CHUNK_SIZE]; unsigned int new_domains; unsigned int i; /* Create the node */ node = (xenstat_node *) calloc(1, sizeof(xenstat_node)); if (node == NULL) return NULL; /* Store the handle in the node for later access */ node->handle = handle; /* Get information about the physical system */ if (xc_physinfo(handle->xc_handle, &physinfo) < 0) { free(node); return NULL; } node->cpu_hz = ((unsigned long long)physinfo.cpu_khz) * 1000ULL; node->num_cpus = physinfo.nr_cpus; node->tot_mem = ((unsigned long long)physinfo.total_pages) * handle->page_size; node->free_mem = ((unsigned long long)physinfo.free_pages) * handle->page_size; /* malloc(0) is not portable, so allocate a single domain. This will * be resized below. */ node->domains = malloc(sizeof(xenstat_domain)); if (node->domains == NULL) { free(node); return NULL; } node->num_domains = 0; do { xenstat_domain *domain, *tmp; new_domains = xc_domain_getinfolist(handle->xc_handle, node->num_domains, DOMAIN_CHUNK_SIZE, domaininfo); tmp = realloc(node->domains, (node->num_domains + new_domains) * sizeof(xenstat_domain)); if (tmp == NULL) { free(node->domains); free(node); return NULL; } node->domains = tmp; domain = node->domains + node->num_domains; /* zero out newly allocated memory in case error occurs below */ memset(domain, 0, new_domains * sizeof(xenstat_domain)); for (i = 0; i < new_domains; i++) { /* Fill in domain using domaininfo[i] */ domain->id = domaininfo[i].domain; domain->name = xenstat_get_domain_name(handle, domain->id); if (domain->name == NULL) { if (errno == ENOMEM) { /* fatal error */ xenstat_free_node(node); return NULL; } else { /* failed to get name -- this means the domain is being destroyed so simply ignore this entry */ continue; } } domain->state = domaininfo[i].flags; domain->cpu_ns = domaininfo[i].cpu_time; domain->num_vcpus = (domaininfo[i].max_vcpu_id+1); domain->vcpus = NULL; domain->cur_mem = ((unsigned long long)domaininfo[i].tot_pages) * handle->page_size; domain->max_mem = domaininfo[i].max_pages == UINT_MAX ? (unsigned long long)-1 : (unsigned long long)(domaininfo[i].max_pages * handle->page_size); domain->ssid = domaininfo[i].ssidref; domain->num_networks = 0; domain->networks = NULL; domain->num_vbds = 0; domain->vbds = NULL; domain++; node->num_domains++; } } while (new_domains == DOMAIN_CHUNK_SIZE); /* Run all the extra data collectors requested */ node->flags = 0; for (i = 0; i < NUM_COLLECTORS; i++) { if ((flags & collectors[i].flag) == collectors[i].flag) { node->flags |= collectors[i].flag; if(collectors[i].collect(node) == 0) { xenstat_free_node(node); return NULL; } } } return node; }