void output(struct vg_data *now, struct vg_data *prev, unsigned int flag) { long long ms_now, ms_prev; struct tm *tm; char timestamp[BUFSIZE]; if (now == NULL) { printf("output: BUG: now is NULL\n"); exit(1); } ms_now = (long long)now->ts.tv_sec * 1000000 + (long long)now->ts.tv_usec; ms_prev = (long long)prev->ts.tv_sec * 1000000 + (long long)prev->ts.tv_usec; if (flag & FLAG_UNIXTIME) { printf("%-24ld ", now->ts.tv_sec); } else { tm = localtime((time_t*) &(now->ts.tv_sec)); strftime(timestamp, BUFSIZE, "%FT%T%z", tm); printf("%24s ", timestamp); } if (IS_RAWOUTPUT(flag)) { printf("%lu ", now->id); printf("%u ", now->hostmhz); /* MHz */ printf("%u ", now->reservemhz); /* MHz */ printf("%u ", now->limitmhz); /* MHz */ printf("%u ", now->cpushares); printf("%lu ", now->elapsedms); /* ms */ printf("%lu ", now->usedms); /* ms */ printf("%lu ", now->stolenms); /* ms */ printf("%u ", now->reservemb); /* MB */ printf("%u ", now->limitmb); /* MB */ printf("%u ", now->memshares); printf("%u ", now->mappedsize); /* MB */ printf("%u ", now->active); /* MB */ printf("%u ", now->overhead); /* MB */ printf("%u ", now->ballooned); /* MB */ printf("%u ", now->swapped); /* MB */ printf("%u ", now->sharedmb); /* MB */ printf("%u ", now->sharedsavedmb); /* MB */ printf("%u ", now->usedmb); /* MB */ printf("%lld ", (ms_now - ms_prev) / 1000); /* guest clock */ printf("%8.2f ", (double)100 * (now->usedms - prev->usedms) / (double)(now->elapsedms - prev->elapsedms)); /* cpu usage rate */ printf("%8.2f", (double)100 * (now->stolenms - prev->stolenms) / (double)(now->elapsedms - prev->elapsedms)); /* %ready rate */ } else { if (prev == NULL) { printf("output: BUG: prev is NULL for non-raw mode!\n"); exit(1); } printf("%8lld ", (ms_now - ms_prev) / 1000); /* guest clock */ printf("%8lu ", now->elapsedms - prev->elapsedms); printf("%8lu ", now->usedms - prev->usedms); printf("%8lu ", now->stolenms - prev->stolenms); printf("%8.2f ", (double)100 * (now->usedms - prev->usedms) / (double)(now->elapsedms - prev->elapsedms)); /* cpu usage rate */ printf("%8.2f", (double)100 * (now->stolenms - prev->stolenms) / (double)(now->elapsedms - prev->elapsedms)); /* %ready rate */ } printf("\n"); return; }
int main (int argc, char **argv) { char c; unsigned int flag = 0; int interval = 1, count = 0, max_count = 1; struct vg_data vg_now, vg_prev; VMGuestLibError ret; while ((c = getopt(argc, argv, "i:c:hvru")) != -1) { switch(c) { case 'i': interval = atoi(optarg); break; case 'c': max_count = atoi(optarg); break; case 'h': usage(); return 0; break; case 'r': /* raw output */ flag |= FLAG_RAWOUTPUT; break; case 'v': /* verbose mode */ flag |= FLAG_VERBOSE; break; case 'u': flag |= FLAG_UNIXTIME; break; default: printf("Unkown option '%c'\n", c); } } memset(&vg_now, 0x0, sizeof(struct vg_data)); ret = VMGuestLib_OpenHandle(&vg_now.handle); if (ret != VMGUESTLIB_ERROR_SUCCESS) { if (IS_VERBOSE(flag)) { printf("VMGuestLib_OpenHandle: %d (%s)\n", ret, VMGuestLib_GetErrorText(ret)); } return 1; } if (sample(&vg_now, flag) != 0) { goto bailout; } if (IS_RAWOUTPUT(flag)) { printf("Timestamp " "SessionId " "HostProcessorSpeed " "CpuReservationMHz CpuLimitMHz CpuShares " "ElapsedMs CpuUsedMs CpuStolenMs " "MemReservationMB MemLimitMB MemShares MemMappedMB " "MemActiveMB MemOverheadMB MemBalloonedMB MemSwappedMB " "MemSharedMB MemSharedSavedMB MemUsedMB\n" ); } else { printf("%-24s %-8s %-8s %8s %8s %8s %8s\n", "Timestamp", "intvl(g)", "intvl(h)", "used", "stolen", "%used", "%ready"); } for (count = 0; count < max_count; count++) { vg_prev = vg_now; sleep(interval); if (sample(&vg_now, flag) != 0) { goto bailout; } output(&vg_now, &vg_prev, flag); } bailout: ret = VMGuestLib_CloseHandle(vg_now.handle); if (ret != VMGUESTLIB_ERROR_SUCCESS) { if (IS_VERBOSE(flag)) { printf("VMGuestLib_CloseHandle: %d (%s)\n", ret, VMGuestLib_GetErrorText(ret)); } return 1; } return 0; }
void output(struct vg_data *now, struct vg_data *prev, unsigned int flag) { long long ms_now, ms_prev; struct tm *tm; if (now == NULL) { printf("output: BUG: now is NULL\n"); exit(1); } ms_now = (long long)now->ts.tv_sec * 1000000 + (long long)now->ts.tv_usec; ms_prev = (long long)prev->ts.tv_sec * 1000000 + (long long)prev->ts.tv_usec; if (flag & FLAG_UNIXTIME) { printf("%ld ", now->ts.tv_sec); } else { tm = localtime((time_t*) &(now->ts.tv_sec)); printf("%04d/%02d/%02d %02d:%02d:%02d ", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec); } if (IS_RAWOUTPUT(flag)) { printf("%llu ", now->id); printf("%u ", now->hostmhz); /* MHz */ printf("%u ", now->reservemhz); /* MHz */ printf("%u ", now->limitmhz); /* MHz */ printf("%u ", now->cpushares); printf("%llu ", now->elapsedms); /* ms */ printf("%llu ", now->usedms); /* ms */ printf("%u ", now->reservemb); /* MB */ printf("%u ", now->limitmb); /* MB */ printf("%u ", now->memshares); printf("%u ", now->mappedsize); /* MB */ printf("%u ", now->active); /* MB */ printf("%u ", now->overhead); /* MB */ printf("%u ", now->ballooned); /* MB */ printf("%u ", now->swapped); /* MB */ printf("%u ", now->sharedmb); /* MB */ printf("%u ", now->sharedsavedmb); /* MB */ printf("%u ", now->usedmb); /* MB */ printf("%lld ", (ms_now - ms_prev) / 1000); /* guest clock */ printf("%6.2f", (double)100 * (now->usedms - prev->usedms) / (double)(now->elapsedms - prev->elapsedms)); /* cpu usage rate */ } else { if (prev == NULL) { printf("output: BUG: prev is NULL for non-raw mode!\n"); exit(1); } printf("%lld ", (ms_now - ms_prev) / 1000); /* guest clock */ printf("%llu ", now->elapsedms - prev->elapsedms); printf("%llu ", now->usedms - prev->usedms); printf("%6.2f", (double)100 * (now->usedms - prev->usedms) / (double)(now->elapsedms - prev->elapsedms)); /* cpu usage rate */ } printf("\n"); return; }