Skip to content

Commit

Permalink
Merge pull request #159 from lukego/hugetlb_check
Browse files Browse the repository at this point in the history
memory: Inibit use_hugetlb if not available.
  • Loading branch information
lukego committed Apr 30, 2014
2 parents 8d58cf5 + 669bab6 commit a1d22be
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
23 changes: 16 additions & 7 deletions src/core/memory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dma_min_addr, dma_max_addr = false, false
-- Allocate DMA-friendly memory.
-- Return virtual memory pointer, physical address, and actual size.
function dma_alloc (bytes)
assert(bytes <= huge_page_size)
if use_hugetlb then assert(bytes <= huge_page_size) end
bytes = lib.align(bytes, 128)
if #chunks == 0 or bytes + chunks[#chunks].used > chunks[#chunks].size then
allocate_next_chunk()
Expand Down Expand Up @@ -70,9 +70,14 @@ end
function get_huge_page_size ()
local meminfo = lib.readfile("/proc/meminfo", "*a")
local _,_,hugesize = meminfo:find("Hugepagesize: +([0-9]+) kB")
return hugesize
and tonumber(hugesize) * 1024
or base_page_size -- use base page size as default value
if true or hugesize == nil then
-- Huge pages not available.
-- Use a reasonable default value, but inhibit HugeTLB allocation.
use_hugetlb = false
return 2048*1024
else
return tonumber(hugesize) * 1024
end
end

base_page_size = 4096
Expand All @@ -97,6 +102,10 @@ end
function selftest (options)
print("selftest: memory")
require("lib.hardware.bus")
if not use_hugetlb then
print("Skipping test because use_hugetlb = false.")
os.exit(43)
end
print("HugeTLB pages (/proc/sys/vm/nr_hugepages): " .. get_hugepages())
for i = 1, 4 do
io.write(" Allocating a "..(huge_page_size/1024/1024).."MB HugeTLB: ")
Expand All @@ -116,13 +125,13 @@ end
--- This module requires a stable physical-virtual mapping so this is
--- enforced automatically at load-time.

function set_use_physical_memory()
function set_use_physical_memory ()
ram_to_io_addr = virtual_to_physical
assert(C.lock_memory() == 0) -- let's hope it's not needed anymore
end

function set_default_allocator(use_hugetlb)
if use_hugetlb and lib.can_write("/proc/sys/vm/nr_hugepages") then
function set_default_allocator ()
if use_hugetlb and huge_page_size and lib.can_write("/proc/sys/vm/nr_hugepages") then
allocate_RAM = function(size)
for i =1, 3 do
local page = C.allocate_huge_page(size)
Expand Down
2 changes: 1 addition & 1 deletion src/lib/hardware/bus.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ if host_has_vfio() then
else
memory.set_use_physical_memory()
end
memory.set_default_allocator(true)
memory.set_default_allocator()

0 comments on commit a1d22be

Please sign in to comment.