18

为什么 OS X 10.6.8 中的 host_statistics64() (我不知道其他版本是否有这个问题)返回的免费、活动、非活动和有线内存的计数加起来不等于 ram 的总量?为什么它缺少不一致的页数?

以下输出表示十秒内未分类为空闲、活动、非活动或有线的页面数(大约每秒采样一次)。

458
243
153
199
357
140
304
93
181
224

产生上述数字的代码是:

#include <stdio.h>
#include <mach/mach.h>
#include <mach/vm_statistics.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char** argv) {
        struct vm_statistics64 stats;
        mach_port_t host    = mach_host_self();
        natural_t   count   = HOST_VM_INFO64_COUNT;
        natural_t   missing = 0;
        int         debug   = argc == 2 ? !strcmp(argv[1], "-v") : 0;
        kern_return_t ret;
        int           mib[2];
        long          ram;
        natural_t     pages;
        size_t        length;
        int           i;

        mib[0] = CTL_HW;
        mib[1] = HW_MEMSIZE;
        length = sizeof(long);
        sysctl(mib, 2, &ram, &length, NULL, 0);
        pages  = ram / getpagesize();

        for (i = 0; i < 10; i++) {
                if ((ret = host_statistics64(host, HOST_VM_INFO64, (host_info64_t)&stats, &count)) != KERN_SUCCESS) {
                        printf("oops\n");
                        return 1;
                }

                /* updated for 10.9 */
                missing = pages - (
                        stats.free_count     +
                        stats.active_count   +
                        stats.inactive_count +
                        stats.wire_count     +
                        stats.compressor_page_count
                );

                if (debug) {
                        printf(
                                "%11d pages (# of pages)\n"
                                "%11d free_count (# of pages free) \n"
                                "%11d active_count (# of pages active) \n"
                                "%11d inactive_count (# of pages inactive) \n"
                                "%11d wire_count (# of pages wired down) \n"
                                "%11lld zero_fill_count (# of zero fill pages) \n"
                                "%11lld reactivations (# of pages reactivated) \n"
                                "%11lld pageins (# of pageins) \n"
                                "%11lld pageouts (# of pageouts) \n"
                                "%11lld faults (# of faults) \n"
                                "%11lld cow_faults (# of copy-on-writes) \n"
                                "%11lld lookups (object cache lookups) \n"
                                "%11lld hits (object cache hits) \n"
                                "%11lld purges (# of pages purged) \n"
                                "%11d purgeable_count (# of pages purgeable) \n"
                                "%11d speculative_count (# of pages speculative (also counted in free_count)) \n"
                                "%11lld decompressions (# of pages decompressed) \n"
                                "%11lld compressions (# of pages compressed) \n"
                                "%11lld swapins (# of pages swapped in (via compression segments)) \n"
                                "%11lld swapouts (# of pages swapped out (via compression segments)) \n"
                                "%11d compressor_page_count (# of pages used by the compressed pager to hold all the compressed data) \n"
                                "%11d throttled_count (# of pages throttled) \n"
                                "%11d external_page_count (# of pages that are file-backed (non-swap)) \n"
                                "%11d internal_page_count (# of pages that are anonymous) \n"
                                "%11lld total_uncompressed_pages_in_compressor (# of pages (uncompressed) held within the compressor.) \n",
                                pages, stats.free_count, stats.active_count, stats.inactive_count,
                                stats.wire_count, stats.zero_fill_count, stats.reactivations,
                                stats.pageins, stats.pageouts, stats.faults, stats.cow_faults,
                                stats.lookups, stats.hits, stats.purges, stats.purgeable_count,
                                stats.speculative_count, stats.decompressions, stats.compressions,
                                stats.swapins, stats.swapouts, stats.compressor_page_count,
                                stats.throttled_count, stats.external_page_count,
                                stats.internal_page_count, stats.total_uncompressed_pages_in_compressor
                        );
                }

                printf("%i\n", missing);
                sleep(1);
        }

        return 0;
}
4

2 回答 2

14

TL;博士:

  • host_statistics64()从不同来源获取信息,这可能会花费时间并可能产生不一致的结果。
  • host_statistics64()通过名称类似的变量获取一些信息vm_page_foo_count。但并非所有这些变量都被考虑在内,例如vm_page_stolen_count不是。
  • 众所周知,/usr/bin/top被盗页面添加到有线页面的数量中。这是一个指标,表明在计算页数时应考虑这些页数。

笔记

  • 我正在使用Darwin Kernel Version 16.5.0 xnu-3789.51.2~3/RELEASE_X86_64 x86_64 开发macOS 10.12,但所有行为都是完全可重现的。
  • 我将链接很多我在我的机器上使用的 XNU 版本的源代码。可以在这里找到:xnu-3789.51.2
  • 您编写的程序与(and )/usr/bin/vm_stat的包装器基本相同。相应的源代码可以在这里找到:system_cmds-496/vm_stat.tproj/vm_stat.chost_statistics64()host_statistics()

如何host_statistics64()适应 XNU 以及它是如何工作的?

正如 Widley 所知,OS X 内核被称为XNU(X NU IS N OT U NIX)并且“是一个混合内核,将卡内基梅隆大学开发的 Mach 内核与来自 FreeBSD 的组件和用于编写称为 IOKit 的驱动程序的 C++ API 相结合。” https://github.com/opensource-apple/xnu/blob/10.12/README.md

虚拟内存管理 (VM) 是Mach的一部分,因此host_statistics64()位于此处。让我们仔细看看它的实现,它包含在xnu-3789.51.2/osfmk/kern/host.c中。

函数签名是

kern_return_t
host_statistics64(host_t host, host_flavor_t flavor, host_info64_t info, mach_msg_type_number_t * count);

第一个相关行是

[...]
processor_t processor;
vm_statistics64_t stat;
vm_statistics64_data_t host_vm_stat;
mach_msg_type_number_t original_count;
unsigned int local_q_internal_count;
unsigned int local_q_external_count;
[...]
processor = processor_list;
stat = &PROCESSOR_DATA(processor, vm_stat);
host_vm_stat = *stat;

if (processor_count > 1) {
    simple_lock(&processor_list_lock);

    while ((processor = processor->processor_list) != NULL) {
        stat = &PROCESSOR_DATA(processor, vm_stat);

        host_vm_stat.zero_fill_count += stat->zero_fill_count;
        host_vm_stat.reactivations += stat->reactivations;
        host_vm_stat.pageins += stat->pageins;
        host_vm_stat.pageouts += stat->pageouts;
        host_vm_stat.faults += stat->faults;
        host_vm_stat.cow_faults += stat->cow_faults;
        host_vm_stat.lookups += stat->lookups;
        host_vm_stat.hits += stat->hits;
        host_vm_stat.compressions += stat->compressions;
        host_vm_stat.decompressions += stat->decompressions;
        host_vm_stat.swapins += stat->swapins;
        host_vm_stat.swapouts += stat->swapouts;
    }

    simple_unlock(&processor_list_lock);
}
[...]

我们得到host_vm_statwhich 是 type vm_statistics64_data_t。正如typedef struct vm_statistics64您在xnu-3789.51.2/osfmk/mach/vm_statistics.h中看到的那样。我们从xnu-3789.51.2/osfmk/kern/processor_data.hPROCESSOR_DATA()中定义的 makro 获取处理器信息。我们通过简单地将相关数字相加来填充所有处理器。host_vm_stat

如您所见,我们发现了一些众所周知的统计数据,例如zero_fill_countcompressions但并非全部包含在host_statistics64().

接下来的相关行是:

stat = (vm_statistics64_t)info;

stat->free_count = vm_page_free_count + vm_page_speculative_count;
stat->active_count = vm_page_active_count;
[...]
stat->inactive_count = vm_page_inactive_count;
stat->wire_count = vm_page_wire_count + vm_page_throttled_count + vm_lopage_free_count;
stat->zero_fill_count = host_vm_stat.zero_fill_count;
stat->reactivations = host_vm_stat.reactivations;
stat->pageins = host_vm_stat.pageins;
stat->pageouts = host_vm_stat.pageouts;
stat->faults = host_vm_stat.faults;
stat->cow_faults = host_vm_stat.cow_faults;
stat->lookups = host_vm_stat.lookups;
stat->hits = host_vm_stat.hits;

stat->purgeable_count = vm_page_purgeable_count;
stat->purges = vm_page_purged_count;

stat->speculative_count = vm_page_speculative_count;

我们重用stat并使其成为我们的输出结构。然后我们用两个被称为和free_count的总和来填充。我们以相同的方式收集其他剩余数据(通过使用名为 的变量)或通过获取我们在上面填写的统计数据。unsigned longvm_page_free_countvm_page_speculative_countvm_page_foo_counthost_vm_stat

1. 结论我们从不同来源收集数据。来自处理器信息或来自名为vm_page_foo_count. 这会花费时间,并且可能会导致一些不一致的问题,因为 VM 是一个非常快速且连续的过程。

让我们仔细看看已经提到的变量vm_page_foo_count。它们在xnu-3789.51.2/osfmk/vm/vm_page.h中定义如下:

extern
unsigned int    vm_page_free_count; /* How many pages are free? (sum of all colors) */
extern
unsigned int    vm_page_active_count;   /* How many pages are active? */
extern
unsigned int    vm_page_inactive_count; /* How many pages are inactive? */
#if CONFIG_SECLUDED_MEMORY
extern
unsigned int    vm_page_secluded_count; /* How many pages are secluded? */
extern
unsigned int    vm_page_secluded_count_free;
extern
unsigned int    vm_page_secluded_count_inuse;
#endif /* CONFIG_SECLUDED_MEMORY */
extern
unsigned int    vm_page_cleaned_count; /* How many pages are in the clean queue? */
extern
unsigned int    vm_page_throttled_count;/* How many inactives are throttled */
extern
unsigned int    vm_page_speculative_count;  /* How many speculative pages are unclaimed? */
extern unsigned int vm_page_pageable_internal_count;
extern unsigned int vm_page_pageable_external_count;
extern
unsigned int    vm_page_xpmapped_external_count;    /* How many pages are mapped executable? */
extern
unsigned int    vm_page_external_count; /* How many pages are file-backed? */
extern
unsigned int    vm_page_internal_count; /* How many pages are anonymous? */
extern
unsigned int    vm_page_wire_count;     /* How many pages are wired? */
extern
unsigned int    vm_page_wire_count_initial; /* How many pages wired at startup */
extern
unsigned int    vm_page_free_target;    /* How many do we want free? */
extern
unsigned int    vm_page_free_min;   /* When to wakeup pageout */
extern
unsigned int    vm_page_throttle_limit; /* When to throttle new page creation */
extern
uint32_t    vm_page_creation_throttle;  /* When to throttle new page creation */
extern
unsigned int    vm_page_inactive_target;/* How many do we want inactive? */
#if CONFIG_SECLUDED_MEMORY
extern
unsigned int    vm_page_secluded_target;/* How many do we want secluded? */
#endif /* CONFIG_SECLUDED_MEMORY */
extern
unsigned int    vm_page_anonymous_min;  /* When it's ok to pre-clean */
extern
unsigned int    vm_page_inactive_min;   /* When to wakeup pageout */
extern
unsigned int    vm_page_free_reserved;  /* How many pages reserved to do pageout */
extern
unsigned int    vm_page_throttle_count; /* Count of page allocations throttled */
extern
unsigned int    vm_page_gobble_count;
extern
unsigned int    vm_page_stolen_count;   /* Count of stolen pages not acccounted in zones */
[...]
extern
unsigned int    vm_page_purgeable_count;/* How many pages are purgeable now ? */
extern
unsigned int    vm_page_purgeable_wired_count;/* How many purgeable pages are wired now ? */
extern
uint64_t    vm_page_purged_count;   /* How many pages got purged so far ? */

这是很多关于我们只能使用host_statistics64(). 这些统计数据中的大部分都在xnu-3789.51.2/osfmk/vm/vm_resident.c中更新。例如,此函数将页面释放到空闲页面列表:

/*
*   vm_page_release:
*
*   Return a page to the free list.
*/

void
vm_page_release(
    vm_page_t   mem,
    boolean_t   page_queues_locked)
{
    [...]
    vm_page_free_count++;
    [...]
}

很有趣的是extern unsigned int vm_page_stolen_count; /* Count of stolen pages not acccounted in zones */。什么是被盗页面?似乎有一些机制可以从某些列表中取出一个页面,即使它通常不会被分页。这些机制之一是推测页面列表中页面的年龄。xnu-3789.51.2/osfmk/vm/vm_page.h告诉我们

* VM_PAGE_MAX_SPECULATIVE_AGE_Q * VM_PAGE_SPECULATIVE_Q_AGE_MS
* defines the amount of time a speculative page is normally
* allowed to live in the 'protected' state (i.e. not available
* to be stolen if vm_pageout_scan is running and looking for
* pages)...  however, if the total number of speculative pages
* in the protected state exceeds our limit (defined in vm_pageout.c)
* and there are none available in VM_PAGE_SPECULATIVE_AGED_Q, then
* vm_pageout_scan is allowed to steal pages from the protected
* bucket even if they are underage.
*
* vm_pageout_scan is also allowed to pull pages from a protected
* bin if the bin has reached the "age of consent" we've set

确实void vm_pageout_scan(void)是增量vm_page_stolen_count您可以在xnu-3789.51.2/osfmk/vm/vm_pageout.c中找到相应的源代码。

我认为在计算 VM 统计信息时不会考虑被盗页面host_statistics64()

证明我是对的

证明这一点的最好方法是手动编译 XNU host_statistics64()。我没有机会这样做,但很快就会尝试。

幸运的是,我们并不是唯一对正确的 VM 统计数据感兴趣的人。因此,我们应该看看 well known( /usr/bin/topXNU 中不包含)的实现,它在此处完全可用:top-108(我刚刚选择了macOS 10.12.4 版本)。

让我们看一下top-108/libtop.c,我们在其中找到以下内容:

static int
libtop_tsamp_update_vm_stats(libtop_tsamp_t* tsamp) {
    kern_return_t kr;
    tsamp->p_vm_stat = tsamp->vm_stat;

    mach_msg_type_number_t count = sizeof(tsamp->vm_stat) / sizeof(natural_t);
    kr = host_statistics64(libtop_port, HOST_VM_INFO64, (host_info64_t)&tsamp->vm_stat, &count);
    if (kr != KERN_SUCCESS) {
        return kr;
    }

    if (tsamp->pages_stolen > 0) {
        tsamp->vm_stat.wire_count += tsamp->pages_stolen;
    }

    [...]

    return kr;
}

tsamptop-108/libtop.hlibtop_tsamp_t中定义的结构类型。除其他外,它包含和.vm_statistics64_data_t vm_statuint64_t pages_stolen

如您所见,如我们所知,static int libtop_tsamp_update_vm_stats(libtop_tsamp_t* tsamp)tsamp->vm_stat填满。host_statistics64()然后它检查是否tsamp->pages_stolen > 0并将其添加wire_counttsamp->vm_stat.

2.结论host_statistics64()如果我们只使用as in/usr/bin/vm_stat或您的示例代码,我们将无法获得这些被盗页面的数量!

为什么host_statistics64()按原样实施?

老实说,我不知道。分页是一个复杂的过程,因此实时观察是一项具有挑战性的任务。我们必须注意到,它的实现似乎没有错误。我认为,如果我们能够访问vm_page_stolen_count. /usr/bin/top如果被盗页面的数量不是很大,则执行不计算被盗页面。

另一个有趣的事情是函数上方的注释static void update_pages_stolen(libtop_tsamp_t *tsamp)/* This is for <rdar://problem/6410098>. */. Open Radar是 Apple 软件的错误报告网站,通常以评论中给出的格式对错误进行分类。我无法找到相关的错误;也许是关于丢失的页面。

我希望这些信息能对你有所帮助。如果我设法在我的机器上编译最新(和定制)版本的 XNU,我会告诉你的。也许这会带来有趣的见解。

于 2017-04-08T20:40:59.783 回答
1

刚刚注意到,如果你添加compressor_page_count到混合中,你会更接近机器中的实际 RAM 数量。

这是一个观察,而不是解释,如果有正确记录的链接,那就太好了!

于 2017-04-01T05:20:45.903 回答