static Bool GuestInfoProcData(const char *pathName, // IN: path name GuestInfoCollector *collector) // IN: { char line[4096]; FILE *fp = Posix_Fopen(pathName, "r"); if (fp == NULL) { g_warning("%s: Error opening %s.\n", __FUNCTION__, pathName); return FALSE; } while (fgets(line, sizeof line, fp) != NULL) { uint64 value = 0; char *fieldName = strtok(line, " \t"); char *fieldData = strtok(NULL, " \t"); if (fieldName == NULL) { continue; } if ((fieldData == NULL) || (sscanf(fieldData, "%"FMT64"u", &value) != 1)) { continue; } GuestInfoCollectStat(pathName, collector, fieldName, value); } fclose(fp); return TRUE; }
static Bool GuestInfoProcMemInfoData(GuestInfoCollector *collector) // IN: { char line[512]; FILE *fp = Posix_Fopen(MEMINFO_FILE, "r"); if (fp == NULL) { g_warning("%s: Error opening " MEMINFO_FILE ".\n", __FUNCTION__); return FALSE; } while (fgets(line, sizeof line, fp) == line) { char *p; uint64 value = 0; char *fieldName = strtok(line, " \t"); char *fieldData = strtok(NULL, " \t"); if (fieldName == NULL) { continue; } p = strrchr(fieldName, ':'); if (p == NULL) { continue; } else { *p = '\0'; } if ((fieldData == NULL) || (sscanf(fieldData, "%"FMT64"u", &value) != 1)) { continue; } GuestInfoCollectStat(MEMINFO_FILE, collector, fieldName, value); } fclose(fp); return TRUE; }
static Bool GuestInfoGetUpTime(double *now) // OUT: { char line[512]; Bool result = FALSE; FILE *fp = Posix_Fopen(UPTIME_FILE, "r"); if (fp == NULL) { return result; } if (fgets(line, sizeof line, fp) != NULL) { double idle; if (sscanf(line, "%lf %lf", now, &idle) == 2) { result = TRUE; } } fclose(fp); return result; }
uint64 System_Uptime(void) { uint64 uptime = -1; #ifdef USERWORLD { VmkuserStatus_Code status; uint64 sysUptime; status = VmkuserUptime_GetUptime(&sysUptime); if (VmkuserStatus_IsOK(status)) { uptime = sysUptime / 10000; } } #elif defined(__linux__) { FILE *procStream; char *buf = NULL; size_t bufSize; uint64 sec; unsigned int csec; if (((procStream = Posix_Fopen("/proc/uptime", "r")) != NULL) && (StdIO_ReadNextLine(procStream, &buf, 80, &bufSize) == StdIO_Success) && (sscanf(buf, "%"FMT64"u.%2u", &sec, &csec) == 2)) { uptime = sec * 100 + csec; } else { Warning("%s: Unable to parse /proc/uptime.\n", __func__); } free(buf); if (procStream) { fclose(procStream); } } #elif defined sun || defined __APPLE__ { struct utmpx *boot, tmp; tmp.ut_type = BOOT_TIME; if ((boot = getutxid(&tmp)) != NULL) { struct timeval now; struct timeval *boottime = &boot->ut_tv; gettimeofday(&now, NULL); uptime = (now.tv_sec * 100 + now.tv_usec / 10000) - (boottime->tv_sec * 100 + boottime->tv_usec / 10000); } else { Warning("%s: Unable to determine boot time.\n", __func__); } endutxent(); } #else // FreeBSD { /* * FreeBSD: src/usr.bin/w/w.c rev 1.59: * "Obtain true uptime through clock_gettime(CLOCK_MONOTONIC, * struct *timespec) instead of subtracting 'bootime' from 'now'." */ struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts) != -1) { uptime = ts.tv_sec * 100 + ts.tv_nsec / 10000000; } else { Warning("%s: clock_gettime: %d\n", __func__, errno); } } #endif return uptime; }