Kernel VM: A few changes as per Axel in #7742

* Avoid floating point numbers in the kernel
* Warning would always show if custom swap file in use
* Don't change a custom swap file size if low space occurs
* Ram > 1GB? Don't double the memory for the automatic size
This commit is contained in:
Alexander von Gluck IV 2012-09-04 15:35:10 -05:00
parent 3e01905aca
commit 4517ef5728
1 changed files with 15 additions and 18 deletions

View File

@ -1493,7 +1493,11 @@ swap_init_post_modules()
if (swapAutomatic) { if (swapAutomatic) {
swapEnabled = true; swapEnabled = true;
swapSize = (off_t)vm_page_num_pages() * B_PAGE_SIZE * 2; swapSize = (off_t)vm_page_num_pages() * B_PAGE_SIZE;
if (swapSize <= (1024 * 1024 * 1024)) {
// Memory under 1GB? double the swap
swapSize *= 2;
}
} }
if (!swapEnabled || swapSize < B_PAGE_SIZE) if (!swapEnabled || swapSize < B_PAGE_SIZE)
@ -1557,25 +1561,18 @@ swap_init_post_modules()
const char* swapPath = path.Path(); const char* swapPath = path.Path();
// Swap size limits prevent oversized swap files // Swap size limits prevent oversized swap files
off_t existingSwapSize = 0;
struct stat existingSwapStat;
if (stat(swapPath, &existingSwapStat) == 0)
existingSwapSize = existingSwapStat.st_size;
off_t freeSpace = info.free_blocks * info.block_size + existingSwapSize;
off_t maxSwap = freeSpace;
if (swapAutomatic) { if (swapAutomatic) {
// Adjust automatic swap to a maximum of 25% of the free space off_t existingSwapSize = 0;
maxSwap = (off_t)(0.25 * freeSpace); struct stat existingSwapStat;
} else { if (stat(swapPath, &existingSwapStat) == 0)
// If user specified, leave 10% of the disk free existingSwapSize = existingSwapStat.st_size;
maxSwap = freeSpace - (off_t)(0.10 * freeSpace);
dprintf("%s: Warning: User specified swap file consumes over 90%% of "
"the available free space, limiting to 90%%\n", __func__);
}
if (swapSize > maxSwap) off_t freeSpace = info.free_blocks * info.block_size + existingSwapSize;
swapSize = maxSwap;
// Adjust automatic swap to a maximum of 25% of the free space
if (swapSize > (freeSpace / 4))
swapSize = (freeSpace / 4);
}
// Create swap file // Create swap file
int fd = open(swapPath, O_RDWR | O_CREAT | O_NOCACHE, S_IRUSR | S_IWUSR); int fd = open(swapPath, O_RDWR | O_CREAT | O_NOCACHE, S_IRUSR | S_IWUSR);