1
0
mirror of https://github.com/ossrs/srs.git synced 2026-10-24 09:59:20 +03:00
Files
srs/cmd/perf/sampler.go
Winlin 7b737e64df Perf: Load and profile tools, and the first SRS and Go proxy fixes. v8.0.63 (#4785)
SRS gets performance tools that load and profile SRS in Docker for every
protocol: a lean load tool, an internal skill that runs an improvement
round, and a measurement method that holds up to a 10% verdict. The same
tools then measure the Go proxy. The first runs found one SRS fix and
three Go proxy fixes.

The load tool, a standalone Go module in cmd/perf (bin/srs-perf):

- Per-core worker processes shard the clients and report counters each
second. Lean clients for every protocol: RTMP publish and play,
HTTP-FLV, HTTP-TS, HLS, DASH, RTSP, SRT, WHIP and WHEP. No decode;
frames and lag come from timestamps.
- 17 scenarios: each protocol's fan-out, publishers (rtmp-pub,
whip-pub), many-streams, cross-protocol bridges (srt-rtmp, srt-whep,
whip-rtmp, whip-flv), and delay probes for the RTMP pause, forward and
edge.
- run ramps by doubling to a capacity, then holds at 50% and 90% of it.
compare checks that the setups match and reports capacity and CPU per
client at the 10% line.
- make perf-image builds an optimized SRS with symbols and frame
pointers, with perf, FlameGraph and FFmpeg. SRS runs on one pinned core
and is configured only by env files, one per scenario.

The measurement method:

- A flat CBR source, checked to ±3% per second, since SRS CPU follows
the bitrate.
- SRS CPU from its cgroup once a second. Holds have no profiler in the
measuring window; each count gets its own separate profile hold.
- perf-ab.sh builds two revisions and runs them side by side on separate
cpusets. compare gives the 95% CI of the paired per-second difference.
- perf-verify.sh checks every run: capacity, healthy steps, profiles and
symbols, noise, and what bound the run.

The internal-perf-for-srs skill:

- SKILL.md holds the rules, the shared tools and the round workflow. A
Target Router loads references/cpp-server.md for the C++ media server or
references/go-server.md for the Go proxy.
- Each reference covers its tools, topology, scenarios, measuring, how
to read its profiles (Linux perf, or Go pprof and trace), A/B, the
verified scenarios with their capacity and top bottleneck, and how to
start a round.
- srs-develop routes performance, benchmark, load test and profile tasks
to it. perf-skill-test.sh checks the text against the scenario registry.
- Reports are git-ignored, since their numbers hold only for the machine
that made them.
- The skills now name each server by its job: the C++ media server and
the Go proxy, with no "generation".

C++ media server, one core, Linux arm64 in Docker:

- Fix: an empty SrsSharedPtr holds no reference count, so each fan-out
copy of a message does one malloc instead of three. Covered by
srs_utest_ai05.cpp.
- Capacity: rtmp-rtmp, rtmp-flv, rtmp-hls, whip-rtmp and whip-flv
12,800; rtmp-dash and srt-rtmp 6,400; rtmp-ts and rtmp-rtsp 3,200;
whip-whep 2,000 (cap); rtmp-whep, srt-whep, srt-srt and rtmp-pub 1,280;
whip-pub 1,000 (cap); many-streams 640 streams.
- Top bottlenecks found, not fixed: the RTMP fan-out copy,
_st_epoll_dispatch with EAGAIN reads, 4 KB reads and writes for HLS and
DASH segments, one write per message for HTTP-TS and two per packet for
RTSP, the SRTP replay window in libsrtp, and libsrt for SRT.
- Delays: the paused RTMP player's 500 ms sleep, and the forward's and
edge publish's 500 ms receive timeout, each adding about 220 ms of lag.

The Go proxy, cmd/proxy, on one core in front of 1 to 4 SRS origins:

- perf-up.sh -target proxy, run -target proxy, which samples the proxy
and each origin, Go profiles in each profile hold (CPU, allocations,
mutex, block, trace, GC), and proxy A/B.
- Fix: HTTP-FLV, HTTP-TS and HLS relay the origin's error status,
headers and body, rather than a 500 with the origin's address.
- Fix: a WebRTC STUN binding routes by its ufrag, not a cached client
address. WHIP and WHEP failed from 160 clients on three origins.
- Capacity on three origins: rtmp-flv and rtmp-hls 12,800; rtmp-rtmp and
rtmp-ts 6,400; whip-whep 2,000 (cap); srt-srt and rtmp-pub 1,280;
whip-pub 1,000 (cap); many-streams 1,280 streams.

---------

Co-authored-by: chatgpt-codex-connector[bot] <199175422+chatgpt-codex-connector[bot]@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 18:46:30 -04:00

264 lines
7.7 KiB
Go

package main
import (
"bufio"
"context"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
type procUsage struct {
srsCPUPercent float64
srsCPUUsageUsec uint64
srsRSSBytes int64
workerCPUPercent []float64
// The origins' CPU, each from its own container's cgroup, when the
// subject is the proxy; nil for SRS.
originCPUPercent []float64
originCPUUsageUsec []uint64
}
// procSampler takes the subject's CPU (SRS, or the proxy) from its
// container's cgroup usage_usec, which counts every thread and child of the
// container to the microsecond, and RSS and each worker's CPU from /proc.
// Behind the proxy, each origin's CPU comes from its own cgroup too.
type procSampler struct {
root string
cgroupRoot string
srsPID int
srsCgroup string
workerPIDs []int
ticksPerSecond float64
previous map[int]uint64
previousUsage uint64
previousTime time.Time
originPIDs []int
originCgroups []string
previousOrigin []uint64
}
func newProcSampler(root, cgroupRoot string, srsPID int, workerPIDs []int, ticksPerSecond float64) *procSampler {
return &procSampler{root: root, cgroupRoot: cgroupRoot, srsPID: srsPID, workerPIDs: append([]int(nil), workerPIDs...), ticksPerSecond: ticksPerSecond}
}
// withOrigins samples the CPU of each origin beside the subject too.
func (s *procSampler) withOrigins(pids []int) *procSampler {
s.originPIDs = append([]int(nil), pids...)
return s
}
// originUsage reads each origin's cgroup usage_usec, finding the cgroups at
// the first call.
func (s *procSampler) originUsage() ([]uint64, error) {
if len(s.originPIDs) == 0 {
return nil, nil
}
if s.originCgroups == nil {
for _, pid := range s.originPIDs {
cgroup, err := readCgroupPath(s.root, pid)
if err != nil {
return nil, err
}
s.originCgroups = append(s.originCgroups, cgroup)
}
}
usage := make([]uint64, len(s.originCgroups))
for i, cgroup := range s.originCgroups {
v, err := readCgroupUsage(s.cgroupRoot, cgroup)
if err != nil {
return nil, err
}
usage[i] = v
}
return usage, nil
}
// readCgroupPath reads the cgroup v2 path of a process. The load container
// must share the host cgroup namespace (docker run --cgroupns host): from a
// private namespace the path of another container starts with "/..".
func readCgroupPath(root string, pid int) (string, error) {
path := filepath.Join(root, strconv.Itoa(pid), "cgroup")
data, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("read %s: %w", path, err)
}
for _, line := range strings.Split(string(data), "\n") {
if !strings.HasPrefix(line, "0::") {
continue
}
cgroup := strings.TrimPrefix(line, "0::")
if cgroup == "" || !strings.HasPrefix(cgroup, "/") || strings.HasPrefix(cgroup, "/..") {
return "", fmt.Errorf("cgroup %q of pid %d is outside this cgroup namespace; run the load container with --cgroupns host", cgroup, pid)
}
return cgroup, nil
}
return "", fmt.Errorf("no cgroup v2 entry in %s", path)
}
func readCgroupUsage(cgroupRoot, cgroup string) (uint64, error) {
path := filepath.Join(cgroupRoot, cgroup, "cpu.stat")
data, err := os.ReadFile(path)
if err != nil {
return 0, fmt.Errorf("read %s: %w", path, err)
}
for _, line := range strings.Split(string(data), "\n") {
fields := strings.Fields(line)
if len(fields) != 2 || fields[0] != "usage_usec" {
continue
}
usage, err := strconv.ParseUint(fields[1], 10, 64)
if err != nil {
return 0, fmt.Errorf("invalid usage_usec in %s: %w", path, err)
}
return usage, nil
}
return 0, fmt.Errorf("usage_usec missing in %s", path)
}
func readProcTicks(root string, pid int) (uint64, error) {
path := filepath.Join(root, strconv.Itoa(pid), "stat")
data, err := os.ReadFile(path)
if err != nil {
return 0, fmt.Errorf("read %s: %w", path, err)
}
// The comm field is parenthesized and may itself contain spaces or parentheses.
close := strings.LastIndexByte(string(data), ')')
if close < 0 {
return 0, fmt.Errorf("invalid %s: missing command terminator", path)
}
fields := strings.Fields(string(data[close+1:]))
if len(fields) < 13 {
return 0, fmt.Errorf("invalid %s: missing CPU fields", path)
}
user, err := strconv.ParseUint(fields[11], 10, 64)
if err != nil {
return 0, fmt.Errorf("invalid %s utime: %w", path, err)
}
system, err := strconv.ParseUint(fields[12], 10, 64)
if err != nil {
return 0, fmt.Errorf("invalid %s stime: %w", path, err)
}
return user + system, nil
}
func readProcRSS(root string, pid int) (int64, error) {
path := filepath.Join(root, strconv.Itoa(pid), "status")
file, err := os.Open(path)
if err != nil {
return 0, fmt.Errorf("read %s: %w", path, err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
if len(fields) == 0 || fields[0] != "VmRSS:" {
continue
}
if len(fields) != 3 || fields[2] != "kB" {
return 0, fmt.Errorf("invalid VmRSS in %s", path)
}
kb, err := strconv.ParseInt(fields[1], 10, 64)
if err != nil || kb < 0 {
return 0, fmt.Errorf("invalid VmRSS in %s", path)
}
return kb * 1024, nil
}
if err := scanner.Err(); err != nil {
return 0, fmt.Errorf("read %s: %w", path, err)
}
return 0, fmt.Errorf("VmRSS missing in %s", path)
}
func (s *procSampler) sampleAt(now time.Time) (procUsage, error) {
if s.ticksPerSecond <= 0 {
return procUsage{}, fmt.Errorf("ticks per second must be positive")
}
if s.srsCgroup == "" {
cgroup, err := readCgroupPath(s.root, s.srsPID)
if err != nil {
return procUsage{}, err
}
s.srsCgroup = cgroup
}
srsUsage, err := readCgroupUsage(s.cgroupRoot, s.srsCgroup)
if err != nil {
return procUsage{}, err
}
current := make(map[int]uint64, len(s.workerPIDs))
for _, pid := range s.workerPIDs {
ticks, err := readProcTicks(s.root, pid)
if err != nil {
return procUsage{}, err
}
current[pid] = ticks
}
rss, err := readProcRSS(s.root, s.srsPID)
if err != nil {
return procUsage{}, err
}
originUsage, err := s.originUsage()
if err != nil {
return procUsage{}, err
}
if !s.previousTime.IsZero() && !now.After(s.previousTime) {
return procUsage{}, fmt.Errorf("sample time must advance")
}
usage := procUsage{srsCPUUsageUsec: srsUsage, srsRSSBytes: rss, workerCPUPercent: make([]float64, len(s.workerPIDs))}
if originUsage != nil {
usage.originCPUUsageUsec = originUsage
usage.originCPUPercent = make([]float64, len(originUsage))
}
if !s.previousTime.IsZero() {
elapsed := now.Sub(s.previousTime).Seconds()
if srsUsage < s.previousUsage {
return procUsage{}, fmt.Errorf("cgroup usage_usec went backwards for %s", s.srsCgroup)
}
usage.srsCPUPercent = float64(srsUsage-s.previousUsage) / (elapsed * 1e6) * 100
for i, pid := range s.workerPIDs {
if current[pid] < s.previous[pid] {
return procUsage{}, fmt.Errorf("CPU ticks went backwards for pid %d", pid)
}
usage.workerCPUPercent[i] = float64(current[pid]-s.previous[pid]) / (elapsed * s.ticksPerSecond) * 100
}
for i, v := range originUsage {
if v < s.previousOrigin[i] {
return procUsage{}, fmt.Errorf("cgroup usage_usec went backwards for %s", s.originCgroups[i])
}
usage.originCPUPercent[i] = float64(v-s.previousOrigin[i]) / (elapsed * 1e6) * 100
}
}
s.previous = current
s.previousOrigin = originUsage
s.previousUsage = srsUsage
s.previousTime = now
return usage, nil
}
func (s *procSampler) sampleEverySecond(ctx context.Context, output chan<- procUsage) error {
if _, err := s.sampleAt(time.Now()); err != nil {
return err
}
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case now := <-ticker.C:
usage, err := s.sampleAt(now)
if err != nil {
return err
}
select {
case output <- usage:
case <-ctx.Done():
return ctx.Err()
}
}
}
}