| 1 | #ifdef __MACH__ |
|---|
| 2 | |
|---|
| 3 | #include <memory.h> |
|---|
| 4 | #include <stdlib.h> |
|---|
| 5 | #include <stdio.h> |
|---|
| 6 | #include <unistd.h> |
|---|
| 7 | #include <sys/types.h> |
|---|
| 8 | #include <sys/sysctl.h> |
|---|
| 9 | #include <sys/socket.h> |
|---|
| 10 | #include <sys/gmon.h> |
|---|
| 11 | #include <mach/vm_param.h> |
|---|
| 12 | #include <netinet/in.h> |
|---|
| 13 | #include <netinet/icmp6.h> |
|---|
| 14 | #include <sys/vmmeter.h> |
|---|
| 15 | #include <sys/proc.h> |
|---|
| 16 | #include <mach/task_info.h> |
|---|
| 17 | #include <mach/task.h> |
|---|
| 18 | #include <mach/mach_init.h> |
|---|
| 19 | #include <mach/vm_statistics.h> |
|---|
| 20 | |
|---|
| 21 | const double DEFAULT_RAM = 1e9; |
|---|
| 22 | const double DEFAULT_MEM_USE = 1e6; |
|---|
| 23 | |
|---|
| 24 | double GetNAN() |
|---|
| 25 | { |
|---|
| 26 | static unsigned long nan[2]={0xffffffff, 0x7fffffff}; |
|---|
| 27 | double dNAN = *( double* )nan; |
|---|
| 28 | return dNAN; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | double g_dNAN = GetNAN(); |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | double GetRAMSize() |
|---|
| 35 | { |
|---|
| 36 | static double CACHED_RAM = 0; |
|---|
| 37 | if (CACHED_RAM != 0) |
|---|
| 38 | return CACHED_RAM; |
|---|
| 39 | |
|---|
| 40 | uint64_t MemPages = 0; |
|---|
| 41 | size_t Len = sizeof(MemPages); |
|---|
| 42 | if (sysctlbyname("hw.memsize", &MemPages, &Len, NULL, 0) < 0) |
|---|
| 43 | return DEFAULT_RAM; |
|---|
| 44 | return (double) MemPages; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | double GetRAMSizeMB() |
|---|
| 48 | { |
|---|
| 49 | return GetRAMSize()/1e6; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | static double g_uPeakMemUseBytes; |
|---|
| 53 | |
|---|
| 54 | double GetMaxMemUseBytes() |
|---|
| 55 | { |
|---|
| 56 | return g_uPeakMemUseBytes; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | double GetPeakMemUseBytes() |
|---|
| 60 | { |
|---|
| 61 | return GetMaxMemUseBytes(); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | double GetMemUseBytes() |
|---|
| 65 | { |
|---|
| 66 | task_t mytask = mach_task_self(); |
|---|
| 67 | struct task_basic_info ti; |
|---|
| 68 | memset((void *) &ti, 0, sizeof(ti)); |
|---|
| 69 | mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT; |
|---|
| 70 | kern_return_t ok = task_info(mytask, TASK_BASIC_INFO, (task_info_t) &ti, &count); |
|---|
| 71 | if (ok == KERN_INVALID_ARGUMENT) |
|---|
| 72 | return DEFAULT_MEM_USE; |
|---|
| 73 | |
|---|
| 74 | if (ok != KERN_SUCCESS) |
|---|
| 75 | return DEFAULT_MEM_USE; |
|---|
| 76 | |
|---|
| 77 | double uBytes = (double ) ti.resident_size; |
|---|
| 78 | if (uBytes > g_uPeakMemUseBytes) |
|---|
| 79 | g_uPeakMemUseBytes = uBytes; |
|---|
| 80 | return uBytes; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | double GetMemUseMB() |
|---|
| 84 | { |
|---|
| 85 | return GetMemUseBytes()/1e6; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | void OSInit() |
|---|
| 89 | { |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | #endif // __MACH__ |
|---|