driver_linux.go 4.5 KB

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