检索虚拟机统计信息时在64位iOS设备上的奇怪行为 [英] Strange behavior on 64bit iOS devices when retrieving vm statistics

查看:54
本文介绍了检索虚拟机统计信息时在64位iOS设备上的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个调整,显示了钩子SpringBoard方法中的可用内存.我正在使用此代码:

I made a tweak that shows free ram inside hooked SpringBoard method. I am using this code:

    mach_port_t host_port;
    mach_msg_type_number_t host_size;
    vm_size_t pagesize;

    host_port = mach_host_self();
    host_size = sizeof(vm_statistics_data_t) / sizeof(integer_t);
    host_page_size(host_port, &pagesize);

    vm_statistics_data_t vm_stat;

    if (host_statistics(host_port, HOST_VM_INFO, (host_info_t)&vm_stat, &host_size) != KERN_SUCCESS){
        ram = @"N/A";
    }else{  
        natural_t bytes = (vm_stat.free_count * pagesize);
    }

在5s之前的设备和Air上运行正常.但是64位设备用户报告说,他们获得的可用RAM数量大于设备上的最大RAM数量.我用相同的代码制作了命令行实用程序,并要求从终端以root身份运行它,然后该命令行实用程序显示正确的值.我检查了为什么会这样,发现在64位设备上的SpringBoard内部host_page_size(host_port, &pagesize);返回的pagesize = 16384实际上比命令行实用程序中显示的大4倍.同样,它仅影响64位设备,而在其他设备上,无论在何处,它都显示pagesize = 4096(正确值).可以使用硬编码的pagesize = 4096来解决此问题,但我想知道为什么会这样,也许我错过了一些重要的事情.

On devices prior 5s and Air it works fine. But 64bit device users reported that they getting amount of free ram larger than max amount of RAM on device. I made command-line utility with the same code and asked to run it as root from terminal, and the command-line utility showed correct values. I checked why it is happening and found out that inside SpringBoard on 64bit devices host_page_size(host_port, &pagesize); returns pagesize = 16384 that is actually 4 times bigger than it shows in command-line utility. Again, it affects only 64 bit devices, on other devices it shows pagesize = 4096 (correct value) no matter where. It can be fixed with hardcoded pagesize = 4096 but I want to know why this is happening, maybe I am missing something important.

推荐答案

#import <mach/mach.h>之后 您可以访问vm_page_sizevm_kernel_page_size(仅自OS X 10.9 + iOS 7起)

after #import <mach/mach.h> you can access to vm_page_size and to vm_kernel_page_size (only since OS X 10.9 + iOS 7)

vm_kernel_page_size = 4096

vm_kernel_page_size = 4096

vm_page_size = 16384

vm_page_size = 16384

已弃用的呼叫getpagesize()返回我们16384

deprecated call getpagesize() return us 16384

host_page_size(mach_host_self(), &pagesize)返回4096

下一个代码,返回16384:

next code, return 16384:

vm_size_t pagesize = 0;
int mib[] = { CTL_HW, HW_PAGESIZE };
size_t length = sizeof(pagesize);
const int sysctlResult = sysctl(mib, 2, &pagesize, &length, NULL, 0);

在arm64 + iOS 9.0.2上测试

Tested on arm64 + iOS 9.0.2

这篇关于检索虚拟机统计信息时在64位iOS设备上的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆