Como o FreeBSD aloca memória?

8

Estou ciente de que isso é uma explicação simplificada / generalizada, mas top(1) utility divide a memória no FreeBSD em seis pools - Active , Inactive , Wired , Cache , Buffers e Free . Exemplo de top(1) output:

Mem: 130M Active, 42M Inact, 51M Wired, 14M Cache, 34M Buf, 648K Free
Swap: 512M Total, 512M Free

Active é usado pelos processos em execução e Wired é usado principalmente para o kernel. Inactive é a memória de processos fechados que ainda são armazenados em cache caso precisem ser reutilizados, Cache é dados em cache, Buffers são buffers de disco (eu acho que é semelhante a cached no Linux free(1) output ( ?)) e Free é uma memória completamente não utilizada. Estou certo de que o kernel do FreeBSD aloca espaço automaticamente de Inactive , Cache e Buffers pools para Active ou Wired , se necessário?

    
por Martin 07.10.2015 / 09:31

1 resposta

12

Para resumir, active e wired é usada memória que não deve ou não pode ser trocada para liberar memória. Enquanto inativo pode ser corretamente trocado, mas ainda é possuído (não liberado) por um processo ou pelo kernel, então esta não é uma memória muito usada, mas ainda é usada.

Novo é lavanderia , que é uma lista de páginas com memória suja, que pode precisar ser gravada no dispositivo de troca. Se a memória suja precisar ser trocada ou não, ela será incluída novamente na fila inativa.

Não é suposto que a memória de Wired seja trocada, por segurança (no caso do kernel) ou por otimização do processo de usuário (como o ZFS). A memória com fio é usada para caches de sistemas de arquivos, que podem ser liberados pelo kernel. Pelo menos para o ZFS, isso pode ser visto como a maior parte da memória livre.

Livre memória é definitivamente livre.

Em cache (agora obsoleto, eu acho) está pronto para ser liberado, já que ele já foi trocado e só está disponível para possível realocação.

Buffer é usado como um cache pela maioria dos sistemas de arquivos (UFS, FAT, ...) e é a quantidade de memória usada pelos sistemas de arquivos. Pode ser ativo, inativo ou com fio.

ARC (Adaptive Replacement Cache) é o cache usado pelo ZFS e é a memória que pode ser liberada quando necessário.

Do Wiki do FreeBSD sobre memória

Memory Classes

Active

  • Contains pages "actively" (recently) referenced by userland
  • Contains a mix of clean and dirty pages
  • Pages are regularly scanned by the page daemon (each page is visited once every vm.pageout_update_period seconds)
  • Scans check to see if the page has been referenced since the last scan
  • If enough scans complete without seeing a reference, the page is moved to the inactive queue
  • Implements pseudo-LRU

Inactive

  • Contains pages aged out of the active queue
  • Contains pages evicted from the buffer cache
  • Contains a mix of clean and dirty pages
  • Pages are scanned by the page daemon (starting from the head of the queue) when there is a memory shortage:
    • Pages which have been referenced are moved back to the active queue or the tail of the inactive queue
    • Pages which are dirty are moved to the tail of the laundry queue
    • Unreferenced, clean pages may be freed and reused immediately
  • Implements second-chance LRU

Laundry

  • Queue for managing dirty inactive pages, which must be cleaned ("laundered") before they can be reused
  • Managed by a separate thread, the laundry thread, instead of the page daemon
  • Laundry thread launders a small number of pages to balance the inactive and laundry queues
  • Frequency of laundering depends on:
    • How many clean pages the page daemon is freeing; more frees contributes to a higher frequency of laundering
    • The size of the laundry queue relative to the inactive queue; if the laundry queue is growing, we will launder more frequently
  • Pages are scanned by the laundry thread (starting from the head of the queue):
    • Pages which have been referenced are moved back to the active queue or the tail of the laundry queue
    • Dirty pages are laundered and then moved close to the head of the inactive queue

Free

  • Memory available for use by the rest of the system.

Wired

  • Non-pageable memory: cannot be freed until explicitly released by the owner
  • Userland memory can be wired by mlock(2) (subject to system and per-user limits)
  • Kernel memory allocators return wired memory
  • Contents of the ARC and the buffer cache are wired
  • Some memory is permanently wired and is never freed (e.g., the kernel file itself)

De O design e implementação do sistema operacional FreeBSD capítulo 6.12 Substituição de Página (Não é mais totalmente preciso, mas aqui para referência da velha questão):

The kernel divides the main memory into five lists:

  1. Wired: Wired pages are locked in memory and cannot be paged out. Typically these pages are being used by the kernel or the physical-memory pager, or they have been locked down with mlock. In addition, all the pages being used to hold the thread stacks of loaded (i.e. not swapped-out) processes are also wired.
  2. Active: Active pages are being used by one or more regions of virtual memory. Although the kernel can page them out, doing so is likely to cause an active process to fault them back again.
  3. Inactive: Inactive pages may be dirty and have contents that are still known, but they are not usually part of any active region. If the contents of the page are dirty, the contents must be written to backing store before the page can be reused. Once the page has been cleaned, it is moved to the cache list. If the system becomes short of memory, the pageout daemon may try to move active pages to the inactive list in the hopes of finding pages that are not really in use. The selection criteria that are used by the pageout daemon to select pages to move from the active list to the inactive list are described later in this section. When the free-memory and cache lists drop to low, pageout daemon traverses the inactive list to create more cache and free pages.
  4. Cache: Cache pages have contents that are still known, but they are not part of an mapping. If they are faulted into active region, they are not part of any mapping. If they are faulted int an active region, they will be moved from the cache list to the active list. If they are used for a read or a write, they will be moved from the cache list first to the buffer cache and eventually released to the inactive list. An mlock system call can reclaim a page from the cache list and wire it. Pages on the cache list are similar to inactive pages except that they are not dirty, either because they are unmodified since they were paged in or because they have been written to their backing store. They can be claimed for a new use when a page is needed.
  5. Free: Free pages have no useful contents and will be used to fulfill new pagefault requests.

Para responder à sua pergunta original

Am I correct that FreeBSD kernel automatically allocates space from Inactive, Cache and Buffers pools to Active or Wired if needed?

As páginas

Ativas podem se tornar inativas se não forem usadas por algum tempo. Se o kernel trocar uma página inativa , esta página é movida para a lista cache . A página na lista cache não faz parte do mapeamento virtual de nenhum processo, mas pode ser facilmente recuperada, como ativa ou com fio . Ou quando necessário para E / S como um cache buffer .

A memória com fio não pode ser trocada da memória principal. Se ele estiver conectado por um processo, ele precisa ser desconectado com a chamada munlock para se tornar a memória ativa novamente.

Ativo , inativo e wired memória pode ser liberada pelo processo ou kernel e adicionada ao livre lista.

    
por 09.10.2015 / 10:44