Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ Download the standalone binary from the [releases page](https://github.com/kitpr

```bash
# For Linux
sudo curl --fail --location --output /usr/local/bin/kit https://github.com/kitproj/kit/releases/download/v0.1.103/kit_v0.1.103_linux_386
sudo curl --fail --location --output /usr/local/bin/kit https://github.com/kitproj/kit/releases/download/v0.1.104/kit_v0.1.104_linux_386
sudo chmod +x /usr/local/bin/kit

# For Go users
go install github.com/kitproj/kit@v0.1.103
go install github.com/kitproj/kit@v0.1.104
```

### Basic Usage
Expand Down
2 changes: 1 addition & 1 deletion internal/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@
const formatMetrics = (metrics) => {
if (!metrics) return "";

return `${metrics.cpu.toFixed(0)}m,${(metrics.mem / 1024 / 1024).toFixed(0)}Mi`;
return `${(metrics.mem / 1024 / 1024).toFixed(0)}Mi`;
};

// Fetch the initial graph data from the server
Expand Down
35 changes: 35 additions & 0 deletions internal/metrics/procfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package metrics

import (
"fmt"
"os"
"strconv"
"strings"

"github.com/kitproj/kit/internal/types"
)

// GetProcFSCommand returns the cat command to read /proc/pid/statm
func GetProcFSCommand(pid int) []string {
return []string{"cat", fmt.Sprintf("/proc/%d/statm", pid)}
}

// ParseProcFSOutput parses /proc/pid/statm output for memory usage only
func ParseProcFSOutput(output string) (*types.Metrics, error) {
fields := strings.Fields(strings.TrimSpace(output))
if len(fields) < 2 {
return nil, fmt.Errorf("unexpected /proc/pid/statm output: insufficient fields")
}

// Field 1 (0-indexed): RSS (resident pages)
rssPages, err := strconv.ParseInt(fields[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse RSS from /proc/pid/statm: %w", err)
}

// Convert pages to bytes using actual system page size
systemPageSize := int64(os.Getpagesize())
memoryBytes := uint64(rssPages * systemPageSize)

return &types.Metrics{Mem: memoryBytes}, nil
}
40 changes: 40 additions & 0 deletions internal/metrics/ps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package metrics

import (
"fmt"
"strconv"
"strings"

"github.com/kitproj/kit/internal/types"
)

func GetPSCommand(pid int) []string {
return []string{"ps", "-o", "rss", "-p", strconv.Itoa(pid)}
}

func ParsePSOutput(output string) (*types.Metrics, error) {
lines := strings.Split(strings.TrimSpace(output), "\n")
if len(lines) < 2 {
return nil, fmt.Errorf("unexpected ps output: %s", output)
}

var totalMemory uint64

for i := 1; i < len(lines); i++ {
fields := strings.Fields(lines[i])
if len(fields) < 1 {
continue
}

rssMemoryKB, err := strconv.ParseInt(fields[0], 10, 64)
if err != nil {
continue
}

totalMemory += uint64(rssMemoryKB * 1024)
}

return &types.Metrics{
Mem: totalMemory,
}, nil
}
73 changes: 42 additions & 31 deletions internal/proc/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/docker/registry"
"github.com/docker/go-connections/nat"
"github.com/kitproj/kit/internal/metrics"
"github.com/kitproj/kit/internal/types"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
"k8s.io/utils/strings/slices"
Expand All @@ -34,6 +35,7 @@ type container struct {
name string
log *log.Logger
spec types.Spec
cli client.APIClient
types.Task
containerID string
}
Expand All @@ -49,6 +51,7 @@ func (c *container) Run(ctx context.Context, stdout, stderr io.Writer) error {
return fmt.Errorf("failed to create docker client: %w", err)
}
defer cli.Close()
c.cli = cli

dockerfile := filepath.Join(c.Image, "Dockerfile")
id, existingHash, err := c.getContainer(ctx, cli)
Expand Down Expand Up @@ -306,54 +309,62 @@ func ignoreNotExist(err error) error {
}

func (c *container) GetMetrics(ctx context.Context) (*types.Metrics, error) {
if c.containerID == "" {
return &types.Metrics{}, nil
}
// Initialize Docker client if not already done

cli, err := client.NewClientWithOpts(client.FromEnv)
command := metrics.GetProcFSCommand(1) // PID 1

// Use Docker API for exec instead of command line
output, err := c.execInContainer(ctx, command)
if err != nil {
return nil, fmt.Errorf("failed to create docker client: %w", err)
return nil, fmt.Errorf("docker exec failed for container %s: %w", c.name, err)
}
defer cli.Close()

stats, err := cli.ContainerStats(ctx, c.containerID, false) // false = single stat, not stream
metrics, err := metrics.ParseProcFSOutput(string(output))
if err != nil {
return nil, fmt.Errorf("failed to get container stats: %w", err)
return nil, fmt.Errorf("failed to parse process metrics for container %s: %w", c.name, err)
}
return metrics, nil
}

func (c *container) execInContainer(ctx context.Context, command []string) ([]byte, error) {
// Create exec configuration
execConfig := dockertypes.ExecConfig{
Cmd: command,
AttachStdout: true,
AttachStderr: true,
}
defer stats.Body.Close()

data, err := io.ReadAll(stats.Body)
// Create exec instance
execResp, err := c.cli.ContainerExecCreate(ctx, c.containerID, execConfig)
if err != nil {
return nil, fmt.Errorf("failed to read stats: %w", err)
return nil, fmt.Errorf("failed to create exec instance: %w", err)
}

var dockerStats dockertypes.StatsJSON
if err := json.Unmarshal(data, &dockerStats); err != nil {
return nil, fmt.Errorf("failed to parse stats: %w", err)
// Start exec and get response
resp, err := c.cli.ContainerExecAttach(ctx, execResp.ID, dockertypes.ExecStartCheck{})
if err != nil {
return nil, fmt.Errorf("failed to attach to exec: %w", err)
}
defer resp.Close()

// Calculate memory usage (subtract cache if available)
memoryBytes := dockerStats.MemoryStats.Usage
if dockerStats.MemoryStats.Stats["cache"] != 0 {
memoryBytes -= dockerStats.MemoryStats.Stats["cache"]
// Read the output
var stdout, stderr bytes.Buffer
_, err = stdcopy.StdCopy(&stdout, &stderr, resp.Reader)
if err != nil {
return nil, fmt.Errorf("failed to read exec output: %w", err)
}

// Calculate CPU usage in millicores
var cpuMillicores uint64
if dockerStats.PreCPUStats.CPUUsage.TotalUsage != 0 {
cpuDelta := dockerStats.CPUStats.CPUUsage.TotalUsage - dockerStats.PreCPUStats.CPUUsage.TotalUsage
systemDelta := dockerStats.CPUStats.SystemUsage - dockerStats.PreCPUStats.SystemUsage
// Check exec exit code
inspectResp, err := c.cli.ContainerExecInspect(ctx, execResp.ID)
if err != nil {
return nil, fmt.Errorf("failed to inspect exec: %w", err)
}

if systemDelta > 0 {
cpuPercent := (float64(cpuDelta) / float64(systemDelta)) * float64(len(dockerStats.CPUStats.CPUUsage.PercpuUsage))
cpuMillicores = uint64(cpuPercent * 1000) // Convert to millicores
}
if inspectResp.ExitCode != 0 {
return nil, fmt.Errorf("exec command failed with exit code %d, stderr: %s", inspectResp.ExitCode, stderr.String())
}

return &types.Metrics{
CPU: cpuMillicores,
Mem: memoryBytes,
}, nil
return stdout.Bytes(), nil
}

var _ Interface = &container{}
49 changes: 6 additions & 43 deletions internal/proc/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
"log"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"time"

"github.com/kitproj/kit/internal/metrics"
"github.com/kitproj/kit/internal/types"
)

Expand Down Expand Up @@ -90,50 +90,13 @@ func ignoreProcessFinishedErr(err error) error {
}

func (h *host) GetMetrics(ctx context.Context) (*types.Metrics, error) {
command := metrics.GetPSCommand(h.pid)
cmd := exec.CommandContext(ctx, command[0], command[1:]...)

// The `ps` command is used to get process information.
// -o %cpu,%mem specifies the desired output format: CPU and memory percentage.
// -p filters the output for the given PID.
cmd := exec.CommandContext(ctx, "ps", "-o", "%cpu,rss", "-p", strconv.Itoa(h.pid))

// Execute the command and capture its output.
output, err := cmd.Output()
if err != nil {
// This error typically occurs if the PID does not exist.
return nil, fmt.Errorf("failed to get process metrics for pid %d: %w", h.pid, err)
}

// The output from `ps` includes a header line, so we need to parse the second line.
// Example output:
// %CPU %MEM
// 0.1 0.2
lines := strings.Split(strings.TrimSpace(string(output)), "\n")
if len(lines) < 2 {
return nil, fmt.Errorf("unexpected ps output for pid %d: %s", h.pid, output)
}

// The second line contains the data. We split it by whitespace.
fields := strings.Fields(lines[1])
if len(fields) < 2 {
return nil, fmt.Errorf("unexpected ps output format for pid %d: %s", h.pid, lines[1])
}

// Parse the CPU usage from the first field.
cpuPercentage, err := strconv.ParseFloat(fields[0], 64)
output, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("failed to parse CPU usage '%s': %w", fields[0], err)
return nil, fmt.Errorf("failed to get process metrics for pid %d: %w, output: %s", h.pid, err, string(output))
}

cpuMillicores := cpuPercentage * 10 // Convert percentage to millicores (1% = 10 millicores)

rssMemoryKB, err := strconv.ParseInt(fields[1], 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse RSS memory '%s': %w", fields[1], err)
}

// Convert RSS memoryBytes from KB to bytes.
memoryBytes := uint64(rssMemoryKB * 1024)

return &types.Metrics{CPU: uint64(cpuMillicores), Mem: memoryBytes}, nil

return metrics.ParsePSOutput(string(output))
}
Loading