450
#elif defined(__linux__)
467
f = Posix_Fopen("/proc/uptime", "r");
469
Warning(LGPFX" Failed to open /proc/uptime: %s\n", Msg_ErrString());
456
static Atomic_Int fdStorage = { -1 };
458
fd = Atomic_ReadInt(&fdStorage);
460
/* Do we need to open the file the first time through? */
461
if (UNLIKELY(fd == -1)) {
462
fd = open("/proc/uptime", O_RDONLY);
465
Warning(LGPFX" Failed to open /proc/uptime: %s\n", Msg_ErrString());
469
/* Try to swap ours in. If we lose the race, close our fd */
470
if (Atomic_ReadIfEqualWriteInt(&fdStorage, -1, fd) != -1) {
474
/* Get the winning fd - either ours or theirs, doesn't matter anymore */
475
fd = Atomic_ReadInt(&fdStorage);
480
res = pread(fd, buf, sizeof buf - 1, 0);
482
Warning(LGPFX" Failed to pread /proc/uptime: %s\n", Msg_ErrString());
485
ASSERT(res < sizeof buf);
473
res = fscanf(f, "%lf", &uptime);
488
if (sscanf(buf, "%lf", &uptime) != 1) {
476
489
Warning(LGPFX" Failed to parse /proc/uptime\n");
479
493
return uptime * 1000 * 1000;
486
501
*-----------------------------------------------------------------------------
762
787
Hostinfo_GetRatedCpuMhz(int32 cpuNumber, // IN
763
788
uint32 *mHz) // OUT
765
#if defined(__APPLE__)
790
#if defined(__APPLE__) || defined(__FreeBSD__)
792
# if defined(__APPLE__)
793
# define CPUMHZ_SYSCTL_NAME "hw.cpufrequency_max"
794
# elif __FreeBSD__version >= 50011
795
# define CPUMHZ_SYSCTL_NAME "hw.clockrate"
798
# if defined(CPUMHZ_SYSCTL_NAME)
767
800
size_t hzSize = sizeof hz;
769
802
// 'cpuNumber' is ignored: Intel Macs are always perfectly symetric.
771
if (sysctlbyname("hw.cpufrequency_max", &hz, &hzSize, NULL, 0) == -1) {
804
if (sysctlbyname(CPUMHZ_SYSCTL_NAME, &hz, &hzSize, NULL, 0) == -1) {
775
808
*mHz = hz / 1000000;
779
815
char *readVal = HostinfoGetCpuInfo(cpuNumber, "cpu MHz");
812
848
Hostinfo_GetCpuDescription(uint32 cpuNumber) // IN
814
#if defined(__APPLE__)
850
#if defined(__APPLE__) || defined(__FreeBSD__)
851
# if defined(__APPLE__)
852
# define CPUDESC_SYSCTL_NAME "machdep.cpu.brand_string"
854
# define CPUDESC_SYSCTL_NAME "hw.model"
818
860
// 'cpuNumber' is ignored: Intel Macs are always perfectly symetric.
820
if (sysctlbyname("machdep.cpu.brand_string", NULL, &descSize, NULL, 0)
862
if (sysctlbyname(CPUDESC_SYSCTL_NAME, NULL, &descSize, NULL, 0)
1071
1119
*freeRam = (uint64)si.freeram * si.mem_unit;
1122
*totalSwap = (uint64)si.totalswap * si.mem_unit;
1125
*freeSwap = (uint64)si.freeswap * si.mem_unit;
1075
1129
#else // ifdef HAVE_SYSINFO
1076
1130
NOT_IMPLEMENTED();
1077
1131
#endif // ifdef HAVE_SYSINFO
1079
#endif // ifndef VMX86_SERVER
1080
1133
#endif // ifndef __APPLE__
1136
#if defined(__linux__) || defined(__FreeBSD__) || defined(sun)
1084
1138
*-----------------------------------------------------------------------------
1086
* Hostinfo_GetMemoryInfoInPages --
1140
* HostinfoGetLinuxMemoryInfoInPages --
1088
1142
* Obtain the minimum memory to be maintained, total memory available, and
1089
* free memory available on the host in pages.
1143
* free memory available on the host (Linux or COS) in pages.
1092
1146
* TRUE on success: '*minSize', '*maxSize' and '*currentSize' are set
1102
Hostinfo_GetMemoryInfoInPages(unsigned int *minSize, // OUT
1103
unsigned int *maxSize, // OUT
1104
unsigned int *currentSize) // OUT
1156
HostinfoGetLinuxMemoryInfoInPages(unsigned int *minSize, // OUT
1157
unsigned int *maxSize, // OUT
1158
unsigned int *currentSize) // OUT
1106
#if defined(__APPLE__)
1107
mach_msg_type_number_t count;
1108
vm_statistics_data_t stat;
1109
kern_return_t error;
1112
* Largely inspired by
1113
* darwinsource-10.4.5/top-15/libtop.c::libtop_p_vm_sample().
1116
count = HOST_VM_INFO_COUNT;
1117
error = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&stat,
1119
if (error != KERN_SUCCESS || count != HOST_VM_INFO_COUNT) {
1120
Warning("%s: Unable to retrieve host vm stats.\n", __FUNCTION__);
1124
// XXX Figure out this value.
1127
// XXX Hopefully this includes cached memory as well. We should check.
1128
*currentSize = stat.free_count;
1131
* Includes kernel memory. This is the same amount as reported by the
1132
* "hw.physmem" sysctl, which is itself slightly lower than the amount
1133
* reported by the "hw.memsize" sysctl, which is the physical RAM size.
1135
*maxSize = stat.free_count
1136
+ stat.wire_count + stat.active_count + stat.inactive_count;
1139
#elif defined(VMX86_SERVER)
1142
VMK_ReturnStatus status;
1144
if (VmkSyscall_Init(FALSE, NULL, 0)) {
1145
status = CosVmnix_GetMemSize(&total, &free);
1146
if (status == VMK_OK) {
1148
*maxSize = total / PAGE_SIZE;
1149
*currentSize = free / PAGE_SIZE;
1159
1162
unsigned int cached = 0;
1208
*-----------------------------------------------------------------------------
1210
* HostinfoGetSwapInfoInPages --
1212
* Obtain the total swap and free swap on the host (Linux or COS) in pages.
1215
* TRUE on success: '*totalSwap' and '*freeSwap' are set if not NULL
1221
*-----------------------------------------------------------------------------
1225
Hostinfo_GetSwapInfoInPages(unsigned int *totalSwap, // OUT
1226
unsigned int *freeSwap) // OUT
1231
if (HostinfoSysinfo(NULL, NULL, &total, &free) == FALSE) {
1235
if (totalSwap != NULL) {
1236
*totalSwap = total / PAGE_SIZE;
1239
if (freeSwap != NULL) {
1240
*freeSwap = free / PAGE_SIZE;
1249
*-----------------------------------------------------------------------------
1251
* Hostinfo_GetMemoryInfoInPages --
1253
* Obtain the minimum memory to be maintained, total memory available, and
1254
* free memory available on the host in pages.
1257
* TRUE on success: '*minSize', '*maxSize' and '*currentSize' are set
1263
*-----------------------------------------------------------------------------
1267
Hostinfo_GetMemoryInfoInPages(unsigned int *minSize, // OUT
1268
unsigned int *maxSize, // OUT
1269
unsigned int *currentSize) // OUT
1271
#if defined(__APPLE__)
1272
mach_msg_type_number_t count;
1273
vm_statistics_data_t stat;
1274
kern_return_t error;
1276
size_t memsizeSize = sizeof memsize;
1279
* Largely inspired by
1280
* darwinsource-10.4.5/top-15/libtop.c::libtop_p_vm_sample().
1283
count = HOST_VM_INFO_COUNT;
1284
error = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&stat,
1286
if (error != KERN_SUCCESS || count != HOST_VM_INFO_COUNT) {
1287
Warning("%s: Unable to retrieve host vm stats.\n", __FUNCTION__);
1291
// XXX Figure out this value.
1295
* XXX Hopefully this includes cached memory as well. We should check.
1296
* No. It returns only completely used pages.
1298
*currentSize = stat.free_count;
1301
* Adding up the stat values does not sum to 100% of physical memory.
1302
* The correct value is available from sysctl so we do that instead.
1304
if (sysctlbyname("hw.memsize", &memsize, &memsizeSize, NULL, 0) == -1) {
1305
Warning("%s: Unable to retrieve host vm hw.memsize.\n", __FUNCTION__);
1309
*maxSize = memsize / PAGE_SIZE;
1311
#elif defined(VMX86_SERVER)
1314
VMK_ReturnStatus status;
1316
if (VmkSyscall_Init(FALSE, NULL, 0)) {
1317
status = CosVmnix_GetMemSize(&total, &free);
1318
if (status == VMK_OK) {
1320
*maxSize = total / PAGE_SIZE;
1321
*currentSize = free / PAGE_SIZE;
1329
return HostinfoGetLinuxMemoryInfoInPages(minSize, maxSize, currentSize);
1336
*-----------------------------------------------------------------------------
1338
* Hostinfo_GetCOSMemoryInfoInPages --
1340
* Obtain the minimum memory to be maintained, total memory available, and
1341
* free memory available on the COS in pages.
1344
* TRUE on success: '*minSize', '*maxSize' and '*currentSize' are set
1350
*-----------------------------------------------------------------------------
1354
Hostinfo_GetCOSMemoryInfoInPages(unsigned int *minSize, // OUT
1355
unsigned int *maxSize, // OUT
1356
unsigned int *currentSize) // OUT
1358
if (HostType_OSIsPureVMK()) {
1361
return HostinfoGetLinuxMemoryInfoInPages(minSize, maxSize, currentSize);