/* 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; }
/* Collect information about VBDs */ int xenstat_collect_vbds(xenstat_node * node) { struct dirent *dp; struct priv_data *priv = get_priv_data(node->handle); if (priv == NULL) { perror("Allocation error"); return 0; } if (priv->sysfsvbd == NULL) { priv->sysfsvbd = opendir(SYSFS_VBD_PATH); if (priv->sysfsvbd == NULL) { perror("Error opening " SYSFS_VBD_PATH); return 0; } } /* Get qdisk statistics */ read_attributes_qdisk(node); rewinddir(priv->sysfsvbd); for(dp = readdir(priv->sysfsvbd); dp != NULL ; dp = readdir(priv->sysfsvbd)) { xenstat_domain *domain; xenstat_vbd vbd; unsigned int domid; int ret; char buf[256]; ret = sscanf(dp->d_name, "%3s-%u-%u", buf, &domid, &vbd.dev); if (ret != 3) continue; if (strcmp(buf,"vbd") == 0) vbd.back_type = 1; else if (strcmp(buf,"tap") == 0) vbd.back_type = 2; else continue; domain = xenstat_node_domain(node, domid); if (domain == NULL) { fprintf(stderr, "Found interface %s-%u-%u but domain %u" " does not exist.\n", buf, domid, vbd.dev, domid); continue; } if((read_attributes_vbd(dp->d_name, "statistics/oo_req", buf, 256)<=0) || ((ret = sscanf(buf, "%llu", &vbd.oo_reqs)) != 1)) { continue; } if((read_attributes_vbd(dp->d_name, "statistics/rd_req", buf, 256)<=0) || ((ret = sscanf(buf, "%llu", &vbd.rd_reqs)) != 1)) { continue; } if((read_attributes_vbd(dp->d_name, "statistics/wr_req", buf, 256)<=0) || ((ret = sscanf(buf, "%llu", &vbd.wr_reqs)) != 1)) { continue; } if((read_attributes_vbd(dp->d_name, "statistics/rd_sect", buf, 256)<=0) || ((ret = sscanf(buf, "%llu", &vbd.rd_sects)) != 1)) { continue; } if((read_attributes_vbd(dp->d_name, "statistics/wr_sect", buf, 256)<=0) || ((ret = sscanf(buf, "%llu", &vbd.wr_sects)) != 1)) { continue; } if ((xenstat_save_vbd(domain, &vbd)) == NULL) { perror("Allocation error"); return 0; } } return 1; }
/* Collect information about networks */ int xenstat_collect_networks(xenstat_node * node) { /* Helper variables for parseNetDevLine() function defined above */ int i; char line[512] = { 0 }, iface[16] = { 0 }, devBridge[16] = { 0 }, devNoBridge[16] = { 0 }; unsigned long long rxBytes, rxPackets, rxErrs, rxDrops, txBytes, txPackets, txErrs, txDrops; struct priv_data *priv = get_priv_data(node->handle); if (priv == NULL) { perror("Allocation error"); return 0; } /* Open and validate /proc/net/dev if we haven't already */ if (priv->procnetdev == NULL) { char header[sizeof(PROCNETDEV_HEADER)]; priv->procnetdev = fopen("/proc/net/dev", "r"); if (priv->procnetdev == NULL) { perror("Error opening /proc/net/dev"); return 0; } /* Validate the format of /proc/net/dev */ if (fread(header, sizeof(PROCNETDEV_HEADER) - 1, 1, priv->procnetdev) != 1) { perror("Error reading /proc/net/dev header"); return 0; } header[sizeof(PROCNETDEV_HEADER) - 1] = '\0'; if (strcmp(header, PROCNETDEV_HEADER) != 0) { fprintf(stderr, "Unexpected /proc/net/dev format\n"); return 0; } } /* Fill in networks */ /* FIXME: optimize this */ fseek(priv->procnetdev, sizeof(PROCNETDEV_HEADER) - 1, SEEK_SET); /* We get the bridge devices for use with bonding interface to get bonding interface stats */ getBridge("vir", devBridge, sizeof(devBridge)); snprintf(devNoBridge, 16, "p%s", devBridge); while (fgets(line, 512, priv->procnetdev)) { xenstat_domain *domain; xenstat_network net; unsigned int domid; parseNetDevLine(line, iface, &rxBytes, &rxPackets, &rxErrs, &rxDrops, NULL, NULL, NULL, NULL, &txBytes, &txPackets, &txErrs, &txDrops, NULL, NULL, NULL, NULL); /* If the device parsed is network bridge and both tx & rx packets are zero, we are most */ /* likely using bonding so we alter the configuration for dom0 to have bridge stats */ if ((strstr(iface, devBridge) != NULL) && (strstr(iface, devNoBridge) == NULL) && ((domain = xenstat_node_domain(node, 0)) != NULL)) { for (i = 0; i < domain->num_networks; i++) { if ((domain->networks[i].id != 0) || (domain->networks[i].tbytes != 0) || (domain->networks[i].rbytes != 0)) continue; domain->networks[i].tbytes = txBytes; domain->networks[i].tpackets = txPackets; domain->networks[i].terrs = txErrs; domain->networks[i].tdrop = txDrops; domain->networks[i].rbytes = rxBytes; domain->networks[i].rpackets = rxPackets; domain->networks[i].rerrs = rxErrs; domain->networks[i].rdrop = rxDrops; } } else /* Otherwise we need to preserve old behaviour */ if (get_iface_domid_network(iface, &domid, &net.id)) { net.tbytes = txBytes; net.tpackets = txPackets; net.terrs = txErrs; net.tdrop = txDrops; net.rbytes = rxBytes; net.rpackets = rxPackets; net.rerrs = rxErrs; net.rdrop = rxDrops; /* FIXME: this does a search for the domid */ domain = xenstat_node_domain(node, domid); if (domain == NULL) { fprintf(stderr, "Found interface vif%u.%u but domain %u" " does not exist.\n", domid, net.id, domid); continue; } if (domain->networks == NULL) { domain->num_networks = 1; domain->networks = malloc(sizeof(xenstat_network)); } else { struct xenstat_network *tmp; domain->num_networks++; tmp = realloc(domain->networks, domain->num_networks * sizeof(xenstat_network)); if (tmp == NULL) free(domain->networks); domain->networks = tmp; } if (domain->networks == NULL) return 0; domain->networks[domain->num_networks - 1] = net; } } return 1; }