driver_unix.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // +build !windows
  2. package execdriver
  3. import (
  4. "encoding/json"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/docker/docker/daemon/execdriver/native/template"
  12. "github.com/opencontainers/runc/libcontainer"
  13. "github.com/opencontainers/runc/libcontainer/cgroups/fs"
  14. "github.com/opencontainers/runc/libcontainer/configs"
  15. )
  16. // Network settings of the container
  17. type Network struct {
  18. Mtu int `json:"mtu"`
  19. ContainerID string `json:"container_id"` // id of the container to join network.
  20. NamespacePath string `json:"namespace_path"`
  21. HostNetworking bool `json:"host_networking"`
  22. }
  23. // InitContainer is the initialization of a container config.
  24. // It returns the initial configs for a container. It's mostly
  25. // defined by the default template.
  26. func InitContainer(c *Command) *configs.Config {
  27. container := template.New()
  28. container.Hostname = getEnv("HOSTNAME", c.ProcessConfig.Env)
  29. container.Cgroups.Name = c.ID
  30. container.Cgroups.AllowedDevices = c.AllowedDevices
  31. container.Devices = c.AutoCreatedDevices
  32. container.Rootfs = c.Rootfs
  33. container.Readonlyfs = c.ReadonlyRootfs
  34. container.Privatefs = true
  35. // check to see if we are running in ramdisk to disable pivot root
  36. container.NoPivotRoot = os.Getenv("DOCKER_RAMDISK") != ""
  37. // Default parent cgroup is "docker". Override if required.
  38. if c.CgroupParent != "" {
  39. container.Cgroups.Parent = c.CgroupParent
  40. }
  41. return container
  42. }
  43. func getEnv(key string, env []string) string {
  44. for _, pair := range env {
  45. parts := strings.SplitN(pair, "=", 2)
  46. if parts[0] == key {
  47. return parts[1]
  48. }
  49. }
  50. return ""
  51. }
  52. // SetupCgroups setups cgroup resources for a container.
  53. func SetupCgroups(container *configs.Config, c *Command) error {
  54. if c.Resources != nil {
  55. container.Cgroups.CpuShares = c.Resources.CPUShares
  56. container.Cgroups.Memory = c.Resources.Memory
  57. container.Cgroups.MemoryReservation = c.Resources.MemoryReservation
  58. container.Cgroups.MemorySwap = c.Resources.MemorySwap
  59. container.Cgroups.CpusetCpus = c.Resources.CpusetCpus
  60. container.Cgroups.CpusetMems = c.Resources.CpusetMems
  61. container.Cgroups.CpuPeriod = c.Resources.CPUPeriod
  62. container.Cgroups.CpuQuota = c.Resources.CPUQuota
  63. container.Cgroups.BlkioWeight = c.Resources.BlkioWeight
  64. container.Cgroups.OomKillDisable = c.Resources.OomKillDisable
  65. container.Cgroups.MemorySwappiness = c.Resources.MemorySwappiness
  66. }
  67. return nil
  68. }
  69. // Returns the network statistics for the network interfaces represented by the NetworkRuntimeInfo.
  70. func getNetworkInterfaceStats(interfaceName string) (*libcontainer.NetworkInterface, error) {
  71. out := &libcontainer.NetworkInterface{Name: interfaceName}
  72. // This can happen if the network runtime information is missing - possible if the
  73. // container was created by an old version of libcontainer.
  74. if interfaceName == "" {
  75. return out, nil
  76. }
  77. type netStatsPair struct {
  78. // Where to write the output.
  79. Out *uint64
  80. // The network stats file to read.
  81. File string
  82. }
  83. // Ingress for host veth is from the container. Hence tx_bytes stat on the host veth is actually number of bytes received by the container.
  84. netStats := []netStatsPair{
  85. {Out: &out.RxBytes, File: "tx_bytes"},
  86. {Out: &out.RxPackets, File: "tx_packets"},
  87. {Out: &out.RxErrors, File: "tx_errors"},
  88. {Out: &out.RxDropped, File: "tx_dropped"},
  89. {Out: &out.TxBytes, File: "rx_bytes"},
  90. {Out: &out.TxPackets, File: "rx_packets"},
  91. {Out: &out.TxErrors, File: "rx_errors"},
  92. {Out: &out.TxDropped, File: "rx_dropped"},
  93. }
  94. for _, netStat := range netStats {
  95. data, err := readSysfsNetworkStats(interfaceName, netStat.File)
  96. if err != nil {
  97. return nil, err
  98. }
  99. *(netStat.Out) = data
  100. }
  101. return out, nil
  102. }
  103. // Reads the specified statistics available under /sys/class/net/<EthInterface>/statistics
  104. func readSysfsNetworkStats(ethInterface, statsFile string) (uint64, error) {
  105. data, err := ioutil.ReadFile(filepath.Join("/sys/class/net", ethInterface, "statistics", statsFile))
  106. if err != nil {
  107. return 0, err
  108. }
  109. return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
  110. }
  111. // Stats collects all the resource usage information from a container.
  112. func Stats(containerDir string, containerMemoryLimit int64, machineMemory int64) (*ResourceStats, error) {
  113. f, err := os.Open(filepath.Join(containerDir, "state.json"))
  114. if err != nil {
  115. return nil, err
  116. }
  117. defer f.Close()
  118. type network struct {
  119. Type string
  120. HostInterfaceName string
  121. }
  122. state := struct {
  123. CgroupPaths map[string]string `json:"cgroup_paths"`
  124. Networks []network
  125. }{}
  126. if err := json.NewDecoder(f).Decode(&state); err != nil {
  127. return nil, err
  128. }
  129. now := time.Now()
  130. mgr := fs.Manager{Paths: state.CgroupPaths}
  131. cstats, err := mgr.GetStats()
  132. if err != nil {
  133. return nil, err
  134. }
  135. stats := &libcontainer.Stats{CgroupStats: cstats}
  136. // if the container does not have any memory limit specified set the
  137. // limit to the machines memory
  138. memoryLimit := containerMemoryLimit
  139. if memoryLimit == 0 {
  140. memoryLimit = machineMemory
  141. }
  142. for _, iface := range state.Networks {
  143. switch iface.Type {
  144. case "veth":
  145. istats, err := getNetworkInterfaceStats(iface.HostInterfaceName)
  146. if err != nil {
  147. return nil, err
  148. }
  149. stats.Interfaces = append(stats.Interfaces, istats)
  150. }
  151. }
  152. return &ResourceStats{
  153. Stats: stats,
  154. Read: now,
  155. MemoryLimit: memoryLimit,
  156. }, nil
  157. }