rdar://problem/21469556
Make sure that the memory report is correct for 64-bit devices.

llvm-svn: 240728
diff --git a/lldb/tools/debugserver/source/MacOSX/MachTask.mm b/lldb/tools/debugserver/source/MacOSX/MachTask.mm
index 4cb53af..23d6e70 100644
--- a/lldb/tools/debugserver/source/MacOSX/MachTask.mm
+++ b/lldb/tools/debugserver/source/MacOSX/MachTask.mm
@@ -382,7 +382,11 @@
         get_threads_profile_data(scanType, task, pid, threads_id, threads_name, threads_used_usec);
     }
     
-    struct vm_statistics vm_stats;
+#if defined (HOST_VM_INFO64_COUNT)
+    vm_statistics64_data_t vminfo;
+#elif
+    struct vm_statistics vminfo;
+#endif
     uint64_t physical_memory;
     mach_vm_size_t rprvt = 0;
     mach_vm_size_t rsize = 0;
@@ -391,7 +395,7 @@
     mach_vm_size_t dirty_size = 0;
     mach_vm_size_t purgeable = 0;
     mach_vm_size_t anonymous = 0;
-    if (m_vm_memory.GetMemoryProfile(scanType, task, task_info, m_process->GetCPUType(), pid, vm_stats, physical_memory, rprvt, rsize, vprvt, vsize, dirty_size, purgeable, anonymous))
+    if (m_vm_memory.GetMemoryProfile(scanType, task, task_info, m_process->GetCPUType(), pid, vminfo, physical_memory, rprvt, rsize, vprvt, vsize, dirty_size, purgeable, anonymous))
     {
         std::ostringstream profile_data_stream;
         
@@ -444,6 +448,9 @@
         
         if (scanType & eProfileMemory)
         {
+#if defined (HOST_VM_INFO64_COUNT)
+            static vm_size_t pagesize = vm_kernel_page_size;
+#elif
             static vm_size_t pagesize;
             static bool calculated = false;
             if (!calculated)
@@ -451,16 +458,22 @@
                 calculated = true;
                 pagesize = PageSize();
             }
+#endif
             
             /* Unused values. Optimized out for transfer performance.
-            profile_data_stream << "wired:" << vm_stats.wire_count * pagesize << ';';
-            profile_data_stream << "active:" << vm_stats.active_count * pagesize << ';';
-            profile_data_stream << "inactive:" << vm_stats.inactive_count * pagesize << ';';
+            profile_data_stream << "wired:" << vminfo.wire_count * pagesize << ';';
+            profile_data_stream << "active:" << vminfo.active_count * pagesize << ';';
+            profile_data_stream << "inactive:" << vminfo.inactive_count * pagesize << ';';
              */
-            uint64_t total_used_count = vm_stats.wire_count + vm_stats.inactive_count + vm_stats.active_count;
+#if defined (HOST_VM_INFO64_COUNT)
+            // This mimicks Activity Monitor.
+            uint64_t total_used_count = (physical_memory / vm_kernel_page_size) - (vminfo.free_count - vminfo.speculative_count) - vminfo.external_page_count - vminfo.purgeable_count;
+#elif
+            uint64_t total_used_count = vminfo.wire_count + vminfo.inactive_count + vminfo.active_count;
+#endif
             profile_data_stream << "used:" << total_used_count * pagesize << ';';
             /* Unused values. Optimized out for transfer performance.
-            profile_data_stream << "free:" << vm_stats.free_count * pagesize << ';';
+            profile_data_stream << "free:" << vminfo.free_count * pagesize << ';';
              */
             
             profile_data_stream << "rprvt:" << rprvt << ';';
diff --git a/lldb/tools/debugserver/source/MacOSX/MachVMMemory.cpp b/lldb/tools/debugserver/source/MacOSX/MachVMMemory.cpp
index ea36ea2..b6ef891 100644
--- a/lldb/tools/debugserver/source/MacOSX/MachVMMemory.cpp
+++ b/lldb/tools/debugserver/source/MacOSX/MachVMMemory.cpp
@@ -236,11 +236,10 @@
     static bool calculated = false;
     if (calculated) return physical_memory;
     
-    int mib[2];
-    mib[0] = CTL_HW;
-    mib[1] = HW_MEMSIZE;
     size_t len = sizeof(physical_memory);
-    sysctl(mib, 2, &physical_memory, &len, NULL, 0);
+    sysctlbyname("hw.memsize", &physical_memory, &len, NULL, 0);
+    
+    calculated = true;
     return physical_memory;
 }
 
@@ -418,8 +417,13 @@
 #endif
 }
 
+#if defined (HOST_VM_INFO64_COUNT)
 nub_bool_t
-MachVMMemory::GetMemoryProfile(DNBProfileDataScanType scanType, task_t task, struct task_basic_info ti, cpu_type_t cputype, nub_process_t pid, vm_statistics_data_t &vm_stats, uint64_t &physical_memory, mach_vm_size_t &rprvt, mach_vm_size_t &rsize, mach_vm_size_t &vprvt, mach_vm_size_t &vsize, mach_vm_size_t &dirty_size, mach_vm_size_t &purgeable, mach_vm_size_t &anonymous)
+MachVMMemory::GetMemoryProfile(DNBProfileDataScanType scanType, task_t task, struct task_basic_info ti, cpu_type_t cputype, nub_process_t pid, vm_statistics64_data_t &vminfo, uint64_t &physical_memory, mach_vm_size_t &rprvt, mach_vm_size_t &rsize, mach_vm_size_t &vprvt, mach_vm_size_t &vsize, mach_vm_size_t &dirty_size, mach_vm_size_t &purgeable, mach_vm_size_t &anonymous)
+#elif
+nub_bool_t
+MachVMMemory::GetMemoryProfile(DNBProfileDataScanType scanType, task_t task, struct task_basic_info ti, cpu_type_t cputype, nub_process_t pid, vm_statistics_data_t &vminfo, uint64_t &physical_memory, mach_vm_size_t &rprvt, mach_vm_size_t &rsize, mach_vm_size_t &vprvt, mach_vm_size_t &vsize, mach_vm_size_t &dirty_size, mach_vm_size_t &purgeable, mach_vm_size_t &anonymous)
+#endif
 {
     if (scanType & eProfileHostMemory)
         physical_memory = GetPhysicalMemory();
@@ -427,12 +431,17 @@
     if (scanType & eProfileMemory)
     {
         static mach_port_t localHost = mach_host_self();
+#if defined (HOST_VM_INFO64_COUNT)
+        mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
+        host_statistics64(localHost, HOST_VM_INFO64, (host_info64_t)&vminfo, &count);
+#elif
         mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
-        host_statistics(localHost, HOST_VM_INFO, (host_info_t)&vm_stats, &count);
-        vm_stats.wire_count += GetStolenPages(task);
-    
+        host_statistics(localHost, HOST_VM_INFO, (host_info_t)&vminfo, &count);
+        vminfo.wire_count += GetStolenPages(task);
+#endif
+        
+        /* We are no longer reporting these. Let's not waste time.
         GetMemorySizes(task, cputype, pid, rprvt, vprvt);
-    
         rsize = ti.resident_size;
         vsize = ti.virtual_size;
         
@@ -441,6 +450,7 @@
             // This uses vmmap strategy. We don't use the returned rsize for now. We prefer to match top's version since that's what we do for the rest of the metrics.
             GetRegionSizes(task, rsize, dirty_size);
         }
+        */
         
         if (scanType & eProfileMemoryAnonymous)
         {
diff --git a/lldb/tools/debugserver/source/MacOSX/MachVMMemory.h b/lldb/tools/debugserver/source/MacOSX/MachVMMemory.h
index 565ead0..1ec9453 100644
--- a/lldb/tools/debugserver/source/MacOSX/MachVMMemory.h
+++ b/lldb/tools/debugserver/source/MacOSX/MachVMMemory.h
@@ -28,7 +28,11 @@
     nub_size_t Write(task_t task, nub_addr_t address, const void *data, nub_size_t data_count);
     nub_size_t PageSize(task_t task);
     nub_bool_t GetMemoryRegionInfo(task_t task, nub_addr_t address, DNBRegionInfo *region_info);
-    nub_bool_t GetMemoryProfile(DNBProfileDataScanType scanType, task_t task, struct task_basic_info ti, cpu_type_t cputype, nub_process_t pid, vm_statistics_data_t &vm_stats, uint64_t &physical_memory, mach_vm_size_t &rprvt, mach_vm_size_t &rsize, mach_vm_size_t &vprvt, mach_vm_size_t &vsize, mach_vm_size_t &dirty_size, mach_vm_size_t &purgable, mach_vm_size_t &anonymous);
+#if defined (HOST_VM_INFO64_COUNT)
+    nub_bool_t GetMemoryProfile(DNBProfileDataScanType scanType, task_t task, struct task_basic_info ti, cpu_type_t cputype, nub_process_t pid, vm_statistics64_data_t &vminfo, uint64_t &physical_memory, mach_vm_size_t &rprvt, mach_vm_size_t &rsize, mach_vm_size_t &vprvt, mach_vm_size_t &vsize, mach_vm_size_t &dirty_size, mach_vm_size_t &purgeable, mach_vm_size_t &anonymous);
+#elif
+    nub_bool_t GetMemoryProfile(DNBProfileDataScanType scanType, task_t task, struct task_basic_info ti, cpu_type_t cputype, nub_process_t pid, vm_statistics_data_t &vminfo, uint64_t &physical_memory, mach_vm_size_t &rprvt, mach_vm_size_t &rsize, mach_vm_size_t &vprvt, mach_vm_size_t &vsize, mach_vm_size_t &dirty_size, mach_vm_size_t &purgeable, mach_vm_size_t &anonymous);
+#endif
 
 protected:
     nub_size_t  MaxBytesLeftInPage(task_t task, nub_addr_t addr, nub_size_t count);