github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/drivers/shared/executor/resource_container_linux.go (about)

     1  package executor
     2  
     3  import (
     4  	"os"
     5  	"sync"
     6  
     7  	"github.com/hashicorp/nomad/client/stats"
     8  	"github.com/opencontainers/runc/libcontainer/cgroups"
     9  	cgroupConfig "github.com/opencontainers/runc/libcontainer/configs"
    10  )
    11  
    12  // resourceContainerContext is a platform-specific struct for managing a
    13  // resource container.  In the case of Linux, this is used to control Cgroups.
    14  type resourceContainerContext struct {
    15  	groups *cgroupConfig.Cgroup
    16  	cgLock sync.Mutex
    17  }
    18  
    19  // cleanup removes this host's Cgroup from within an Executor's context
    20  func (rc *resourceContainerContext) executorCleanup() error {
    21  	rc.cgLock.Lock()
    22  	defer rc.cgLock.Unlock()
    23  	if err := DestroyCgroup(rc.groups, os.Getpid()); err != nil {
    24  		return err
    25  	}
    26  	return nil
    27  }
    28  
    29  func (rc *resourceContainerContext) isEmpty() bool {
    30  	return rc.groups == nil
    31  }
    32  
    33  func (rc *resourceContainerContext) getAllPidsByCgroup() (map[int]*nomadPid, error) {
    34  	nPids := map[int]*nomadPid{}
    35  
    36  	if rc.groups == nil {
    37  		return nPids, nil
    38  	}
    39  
    40  	var path string
    41  	if p, ok := rc.groups.Paths["freezer"]; ok {
    42  		path = p
    43  	} else {
    44  		path = rc.groups.Path
    45  	}
    46  
    47  	pids, err := cgroups.GetAllPids(path)
    48  	if err != nil {
    49  		return nPids, err
    50  	}
    51  
    52  	for _, pid := range pids {
    53  		nPids[pid] = &nomadPid{
    54  			pid:           pid,
    55  			cpuStatsTotal: stats.NewCpuStats(),
    56  			cpuStatsUser:  stats.NewCpuStats(),
    57  			cpuStatsSys:   stats.NewCpuStats(),
    58  		}
    59  	}
    60  
    61  	return nPids, nil
    62  }