driver_unix.go 5.6 KB

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