driver_linux.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. }
  50. return nil
  51. }
  52. // Returns the network statistics for the network interfaces represented by the NetworkRuntimeInfo.
  53. func getNetworkInterfaceStats(interfaceName string) (*libcontainer.NetworkInterface, error) {
  54. out := &libcontainer.NetworkInterface{Name: interfaceName}
  55. // This can happen if the network runtime information is missing - possible if the
  56. // container was created by an old version of libcontainer.
  57. if interfaceName == "" {
  58. return out, nil
  59. }
  60. type netStatsPair struct {
  61. // Where to write the output.
  62. Out *uint64
  63. // The network stats file to read.
  64. File string
  65. }
  66. // 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.
  67. netStats := []netStatsPair{
  68. {Out: &out.RxBytes, File: "tx_bytes"},
  69. {Out: &out.RxPackets, File: "tx_packets"},
  70. {Out: &out.RxErrors, File: "tx_errors"},
  71. {Out: &out.RxDropped, File: "tx_dropped"},
  72. {Out: &out.TxBytes, File: "rx_bytes"},
  73. {Out: &out.TxPackets, File: "rx_packets"},
  74. {Out: &out.TxErrors, File: "rx_errors"},
  75. {Out: &out.TxDropped, File: "rx_dropped"},
  76. }
  77. for _, netStat := range netStats {
  78. data, err := readSysfsNetworkStats(interfaceName, netStat.File)
  79. if err != nil {
  80. return nil, err
  81. }
  82. *(netStat.Out) = data
  83. }
  84. return out, nil
  85. }
  86. // Reads the specified statistics available under /sys/class/net/<EthInterface>/statistics
  87. func readSysfsNetworkStats(ethInterface, statsFile string) (uint64, error) {
  88. data, err := ioutil.ReadFile(filepath.Join("/sys/class/net", ethInterface, "statistics", statsFile))
  89. if err != nil {
  90. return 0, err
  91. }
  92. return strconv.ParseUint(strings.TrimSpace(string(data)), 10, 64)
  93. }
  94. func Stats(containerDir string, containerMemoryLimit int64, machineMemory int64) (*ResourceStats, error) {
  95. f, err := os.Open(filepath.Join(containerDir, "state.json"))
  96. if err != nil {
  97. return nil, err
  98. }
  99. defer f.Close()
  100. type network struct {
  101. Type string
  102. HostInterfaceName string
  103. }
  104. state := struct {
  105. CgroupPaths map[string]string `json:"cgroup_paths"`
  106. Networks []network
  107. }{}
  108. if err := json.NewDecoder(f).Decode(&state); err != nil {
  109. return nil, err
  110. }
  111. now := time.Now()
  112. mgr := fs.Manager{Paths: state.CgroupPaths}
  113. cstats, err := mgr.GetStats()
  114. if err != nil {
  115. return nil, err
  116. }
  117. stats := &libcontainer.Stats{CgroupStats: cstats}
  118. // if the container does not have any memory limit specified set the
  119. // limit to the machines memory
  120. memoryLimit := containerMemoryLimit
  121. if memoryLimit == 0 {
  122. memoryLimit = machineMemory
  123. }
  124. for _, iface := range state.Networks {
  125. switch iface.Type {
  126. case "veth":
  127. istats, err := getNetworkInterfaceStats(iface.HostInterfaceName)
  128. if err != nil {
  129. return nil, err
  130. }
  131. stats.Interfaces = append(stats.Interfaces, istats)
  132. }
  133. }
  134. return &ResourceStats{
  135. Stats: stats,
  136. Read: now,
  137. MemoryLimit: memoryLimit,
  138. }, nil
  139. }