github.com/hhrutter/nomad@v0.6.0-rc2.0.20170723054333-80c4b03f0705/client/structs/structs.go (about)

     1  package structs
     2  
     3  import (
     4  	"crypto/md5"
     5  	"io"
     6  	"strconv"
     7  )
     8  
     9  // MemoryStats holds memory usage related stats
    10  type MemoryStats struct {
    11  	RSS            uint64
    12  	Cache          uint64
    13  	Swap           uint64
    14  	MaxUsage       uint64
    15  	KernelUsage    uint64
    16  	KernelMaxUsage uint64
    17  
    18  	// A list of fields whose values were actually sampled
    19  	Measured []string
    20  }
    21  
    22  func (ms *MemoryStats) Add(other *MemoryStats) {
    23  	ms.RSS += other.RSS
    24  	ms.Cache += other.Cache
    25  	ms.Swap += other.Swap
    26  	ms.MaxUsage += other.MaxUsage
    27  	ms.KernelUsage += other.KernelUsage
    28  	ms.KernelMaxUsage += other.KernelMaxUsage
    29  	ms.Measured = joinStringSet(ms.Measured, other.Measured)
    30  }
    31  
    32  // CpuStats holds cpu usage related stats
    33  type CpuStats struct {
    34  	SystemMode       float64
    35  	UserMode         float64
    36  	TotalTicks       float64
    37  	ThrottledPeriods uint64
    38  	ThrottledTime    uint64
    39  	Percent          float64
    40  
    41  	// A list of fields whose values were actually sampled
    42  	Measured []string
    43  }
    44  
    45  func (cs *CpuStats) Add(other *CpuStats) {
    46  	cs.SystemMode += other.SystemMode
    47  	cs.UserMode += other.UserMode
    48  	cs.TotalTicks += other.TotalTicks
    49  	cs.ThrottledPeriods += other.ThrottledPeriods
    50  	cs.ThrottledTime += other.ThrottledTime
    51  	cs.Percent += other.Percent
    52  	cs.Measured = joinStringSet(cs.Measured, other.Measured)
    53  }
    54  
    55  // ResourceUsage holds information related to cpu and memory stats
    56  type ResourceUsage struct {
    57  	MemoryStats *MemoryStats
    58  	CpuStats    *CpuStats
    59  }
    60  
    61  func (ru *ResourceUsage) Add(other *ResourceUsage) {
    62  	ru.MemoryStats.Add(other.MemoryStats)
    63  	ru.CpuStats.Add(other.CpuStats)
    64  }
    65  
    66  // TaskResourceUsage holds aggregated resource usage of all processes in a Task
    67  // and the resource usage of the individual pids
    68  type TaskResourceUsage struct {
    69  	ResourceUsage *ResourceUsage
    70  	Timestamp     int64
    71  	Pids          map[string]*ResourceUsage
    72  }
    73  
    74  // AllocResourceUsage holds the aggregated task resource usage of the
    75  // allocation.
    76  type AllocResourceUsage struct {
    77  	// ResourceUsage is the summation of the task resources
    78  	ResourceUsage *ResourceUsage
    79  
    80  	// Tasks contains the resource usage of each task
    81  	Tasks map[string]*TaskResourceUsage
    82  
    83  	// The max timestamp of all the Tasks
    84  	Timestamp int64
    85  }
    86  
    87  // joinStringSet takes two slices of strings and joins them
    88  func joinStringSet(s1, s2 []string) []string {
    89  	lookup := make(map[string]struct{}, len(s1))
    90  	j := make([]string, 0, len(s1))
    91  	for _, s := range s1 {
    92  		j = append(j, s)
    93  		lookup[s] = struct{}{}
    94  	}
    95  
    96  	for _, s := range s2 {
    97  		if _, ok := lookup[s]; !ok {
    98  			j = append(j, s)
    99  		}
   100  	}
   101  
   102  	return j
   103  }
   104  
   105  // FSIsolation is an enumeration to describe what kind of filesystem isolation
   106  // a driver supports.
   107  type FSIsolation int
   108  
   109  const (
   110  	// FSIsolationNone means no isolation. The host filesystem is used.
   111  	FSIsolationNone FSIsolation = 0
   112  
   113  	// FSIsolationChroot means the driver will use a chroot on the host
   114  	// filesystem.
   115  	FSIsolationChroot FSIsolation = 1
   116  
   117  	// FSIsolationImage means the driver uses an image.
   118  	FSIsolationImage FSIsolation = 2
   119  )
   120  
   121  func (f FSIsolation) String() string {
   122  	switch f {
   123  	case 0:
   124  		return "none"
   125  	case 1:
   126  		return "chroot"
   127  	case 2:
   128  		return "image"
   129  	default:
   130  		return "INVALID"
   131  	}
   132  }
   133  
   134  // DriverNetwork is the network created by driver's (eg Docker's bridge
   135  // network) during Prestart.
   136  type DriverNetwork struct {
   137  	// PortMap can be set by drivers to replace ports in environment
   138  	// variables with driver-specific mappings.
   139  	PortMap map[string]int
   140  
   141  	// IP is the IP address for the task created by the driver.
   142  	IP string
   143  
   144  	// AutoAdvertise indicates whether the driver thinks services that
   145  	// choose to auto-advertise-addresses should use this IP instead of the
   146  	// host's. eg If a Docker network plugin is used
   147  	AutoAdvertise bool
   148  }
   149  
   150  // Advertise returns true if the driver suggests using the IP set. May be
   151  // called on a nil Network in which case it returns false.
   152  func (d *DriverNetwork) Advertise() bool {
   153  	return d != nil && d.AutoAdvertise
   154  }
   155  
   156  // Copy a DriverNetwork struct. If it is nil, nil is returned.
   157  func (d *DriverNetwork) Copy() *DriverNetwork {
   158  	if d == nil {
   159  		return nil
   160  	}
   161  	pm := make(map[string]int, len(d.PortMap))
   162  	for k, v := range d.PortMap {
   163  		pm[k] = v
   164  	}
   165  	return &DriverNetwork{
   166  		PortMap:       pm,
   167  		IP:            d.IP,
   168  		AutoAdvertise: d.AutoAdvertise,
   169  	}
   170  }
   171  
   172  // Hash the contents of a DriverNetwork struct to detect changes. If it is nil,
   173  // an empty slice is returned.
   174  func (d *DriverNetwork) Hash() []byte {
   175  	if d == nil {
   176  		return []byte{}
   177  	}
   178  	h := md5.New()
   179  	io.WriteString(h, d.IP)
   180  	io.WriteString(h, strconv.FormatBool(d.AutoAdvertise))
   181  	for k, v := range d.PortMap {
   182  		io.WriteString(h, k)
   183  		io.WriteString(h, strconv.Itoa(v))
   184  	}
   185  	return h.Sum(nil)
   186  }