Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
mount: Fix hugepages support
Browse files Browse the repository at this point in the history
Signed-off-by: Pradipta Banerjee <[email protected]>
  • Loading branch information
bpradipt committed Jan 27, 2021
1 parent 25d7471 commit 7a0a500
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
9 changes: 9 additions & 0 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1162,6 +1162,15 @@ func (a *agentGRPC) UpdateContainer(ctx context.Context, req *pb.UpdateContainer
resources.MemorySwap = req.Resources.Memory.Swap
}

if len(req.Resources.HugepageLimits) > 0 {
for _, l := range req.Resources.HugepageLimits {
resources.HugetlbLimit = append(resources.HugetlbLimit, &configs.HugepageLimit{
Pagesize: l.Pagesize,
Limit: l.Limit,
})
}
}

if req.Resources.Pids != nil {
resources.PidsLimit = req.Resources.Pids.Limit
}
Expand Down
52 changes: 46 additions & 6 deletions mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"bufio"
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand All @@ -26,12 +27,14 @@ import (
)

const (
type9pFs = "9p"
typeVirtioFS = "virtiofs"
typeRootfs = "rootfs"
typeTmpFs = "tmpfs"
procMountStats = "/proc/self/mountstats"
mountPerm = os.FileMode(0755)
type9pFs = "9p"
typeVirtioFS = "virtiofs"
typeRootfs = "rootfs"
typeTmpFs = "tmpfs"
typeHugeTlbfs = "hugetlbfs"
procMountStats = "/proc/self/mountstats"
mountPerm = os.FileMode(0755)
sysfsHugepagesPrefix = "/sys/kernel/mm/hugepages/"
)

var flagList = map[string]int{
Expand Down Expand Up @@ -111,6 +114,13 @@ func mount(source, destination, fsType string, flags int, options string) error
absSource = source
case typeTmpFs:
absSource = source
case typeHugeTlbfs:
absSource = source
//Allocate hugepages before mount
///sys/kernel/mm/hugepages/hugepages-1048576kB/nr_hugepages
///sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages
//options eg "pagesize=2048,size=107374182"
allocateHugePages(options)
default:
absSource, err = filepath.EvalSymlinks(source)
if err != nil {
Expand All @@ -132,6 +142,36 @@ func mount(source, destination, fsType string, flags int, options string) error
return nil
}

// Allocate hugepages by writing to sysfs
func allocateHugePages(options string) error {

agentLog.WithField("HugePagesOption", options).Info("HugePages option string")

//options eg "pagesize=2048,size=107374182"
opt := strings.Split(options, ",")
pageSizeStr := strings.TrimPrefix(opt[0], "pagesize=")
pageSize, err := strconv.ParseInt(pageSizeStr, 10, 64)
if err != nil {
return grpcStatus.Errorf(codes.Internal, "Incorrect hugepage size %v", pageSizeStr)
}
//sysfs entry is always of the form hugepages-${size}kB
//Ref: https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt
pageSizeKbStr := strconv.FormatInt((pageSize/1024), 10) + "kB"
sysfsHugepagesPath := sysfsHugepagesPrefix + "hugepages-" + pageSizeKbStr + "/" + "nr_hugepages"

sizeStr := strings.TrimPrefix(opt[1], "size=")
size, err := strconv.ParseInt(sizeStr, 10, 64)
if err != nil {
return grpcStatus.Errorf(codes.Internal, "Incorrect total size of hugepages %v", sizeStr)
}
numpages := strconv.FormatInt((size / pageSize), 10)
if err := ioutil.WriteFile(sysfsHugepagesPath, []byte(numpages), 0644); err != nil {
return grpcStatus.Errorf(codes.Internal, "Could not allocate hugepages")
}

return nil
}

// ensureDestinationExists will recursively create a given mountpoint. If directories
// are created, their permissions are initialized to mountPerm
func ensureDestinationExists(source, destination string, fsType string) error {
Expand Down

0 comments on commit 7a0a500

Please sign in to comment.