Skip to content

Commit

Permalink
Merge pull request google#2600 from dqminh/raw-container-process-stat
Browse files Browse the repository at this point in the history
Allow raw container to retrieve process stats
  • Loading branch information
dashpole authored Jun 25, 2020
2 parents 8851fa6 + 8fdcc6d commit 6f30891
Showing 1 changed file with 48 additions and 44 deletions.
92 changes: 48 additions & 44 deletions container/libcontainer/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,56 +118,58 @@ func (h *Handler) GetStats() (*info.ContainerStats, error) {
}

// If we know the pid then get network stats from /proc/<pid>/net/dev
if h.pid == 0 {
return stats, nil
}
if h.includedMetrics.Has(container.NetworkUsageMetrics) {
netStats, err := networkStatsFromProc(h.rootFs, h.pid)
if err != nil {
klog.V(4).Infof("Unable to get network stats from pid %d: %v", h.pid, err)
} else {
stats.Network.Interfaces = append(stats.Network.Interfaces, netStats...)
}
}
if h.includedMetrics.Has(container.NetworkTcpUsageMetrics) {
t, err := tcpStatsFromProc(h.rootFs, h.pid, "net/tcp")
if err != nil {
klog.V(4).Infof("Unable to get tcp stats from pid %d: %v", h.pid, err)
} else {
stats.Network.Tcp = t
if h.pid > 0 {
if h.includedMetrics.Has(container.NetworkUsageMetrics) {
netStats, err := networkStatsFromProc(h.rootFs, h.pid)
if err != nil {
klog.V(4).Infof("Unable to get network stats from pid %d: %v", h.pid, err)
} else {
stats.Network.Interfaces = append(stats.Network.Interfaces, netStats...)
}
}
if h.includedMetrics.Has(container.NetworkTcpUsageMetrics) {
t, err := tcpStatsFromProc(h.rootFs, h.pid, "net/tcp")
if err != nil {
klog.V(4).Infof("Unable to get tcp stats from pid %d: %v", h.pid, err)
} else {
stats.Network.Tcp = t
}

t6, err := tcpStatsFromProc(h.rootFs, h.pid, "net/tcp6")
if err != nil {
klog.V(4).Infof("Unable to get tcp6 stats from pid %d: %v", h.pid, err)
} else {
stats.Network.Tcp6 = t6
}
t6, err := tcpStatsFromProc(h.rootFs, h.pid, "net/tcp6")
if err != nil {
klog.V(4).Infof("Unable to get tcp6 stats from pid %d: %v", h.pid, err)
} else {
stats.Network.Tcp6 = t6
}

}
if h.includedMetrics.Has(container.NetworkAdvancedTcpUsageMetrics) {
ta, err := advancedTCPStatsFromProc(h.rootFs, h.pid, "net/netstat", "net/snmp")
if err != nil {
klog.V(4).Infof("Unable to get advanced tcp stats from pid %d: %v", h.pid, err)
} else {
stats.Network.TcpAdvanced = ta
}
}
if h.includedMetrics.Has(container.NetworkUdpUsageMetrics) {
u, err := udpStatsFromProc(h.rootFs, h.pid, "net/udp")
if err != nil {
klog.V(4).Infof("Unable to get udp stats from pid %d: %v", h.pid, err)
} else {
stats.Network.Udp = u
if h.includedMetrics.Has(container.NetworkAdvancedTcpUsageMetrics) {
ta, err := advancedTCPStatsFromProc(h.rootFs, h.pid, "net/netstat", "net/snmp")
if err != nil {
klog.V(4).Infof("Unable to get advanced tcp stats from pid %d: %v", h.pid, err)
} else {
stats.Network.TcpAdvanced = ta
}
}
if h.includedMetrics.Has(container.NetworkUdpUsageMetrics) {
u, err := udpStatsFromProc(h.rootFs, h.pid, "net/udp")
if err != nil {
klog.V(4).Infof("Unable to get udp stats from pid %d: %v", h.pid, err)
} else {
stats.Network.Udp = u
}

u6, err := udpStatsFromProc(h.rootFs, h.pid, "net/udp6")
if err != nil {
klog.V(4).Infof("Unable to get udp6 stats from pid %d: %v", h.pid, err)
} else {
stats.Network.Udp6 = u6
u6, err := udpStatsFromProc(h.rootFs, h.pid, "net/udp6")
if err != nil {
klog.V(4).Infof("Unable to get udp6 stats from pid %d: %v", h.pid, err)
} else {
stats.Network.Udp6 = u6
}
}
}
// some process metrics are per container ( number of processes, number of
// file descriptors etc.) and not required a proper container's
// root PID (systemd services don't have the root PID atm)
if h.includedMetrics.Has(container.ProcessMetrics) {
paths := h.cgroupManager.GetPaths()
path, ok := paths["cpu"]
Expand Down Expand Up @@ -298,13 +300,15 @@ func processStatsFromProcs(rootFs string, cgroupPath string, rootPid int) (info.
}
}
}
ulimits := processRootProcUlimits(rootFs, rootPid)

processStats := info.ProcessStats{
ProcessCount: uint64(len(pids)),
FdCount: fdCount,
SocketCount: socketCount,
Ulimits: ulimits,
}

if rootPid > 0 {
processStats.Ulimits = processRootProcUlimits(rootFs, rootPid)
}

return processStats, nil
Expand Down

0 comments on commit 6f30891

Please sign in to comment.