Skip to content

[Task] Profile sysbench CPU/Memory/Mutex contention and analyze bottlenecks #25472

Open

Description

@XuPeng-SH

Performance Task: Profile current sysbench workload (CPU/Memory/Mutex)

Is there an existing issue for performance?

  • I have checked existing issues.

Environment

  • Branch: main
  • Deployment: dev standalone / multi-CN as applicable

Details of Performance

建立一个专用的 sysbench profiling 任务,聚焦当前问题场景下的 CPU、Memory、Mutex 瓶颈:

  • 覆盖 go runtime CPU profile、heap、mutex profile;
  • 形成可复现、可对比的采集流程;
  • 输出 top/火焰图等可定位结论,给出优化方向。

Reproduction Steps (sysbench)

  1. 以低干扰方式启动 MO(示例):
    ./mo-service -debug-http=:6060 -block-profile-rate=100 -mutex-profile-fraction=100 -launch ./etc/launch/launch.toml
  2. 确认实际运行的 sysbench 脚本参数(threads、duration、events、sql)
  3. 压测时采集 profile(至少持续 30s):
    curl -s "http://127.0.0.1:6060/debug/pprof/profile?seconds=30" > cpu.pprof
    curl -s http://127.0.0.1:6060/debug/pprof/heap > heap.pprof
    curl -s http://127.0.0.1:6060/debug/pprof/mutex > mutex.pprof
    curl -s http://127.0.0.1:6060/debug/pprof/block > block.pprof
    curl -s http://127.0.0.1:6060/debug/pprof/goroutine > goroutine.pprof
  4. 多轮采集:压测前、压测中、压测后。
  5. 分析(示例):
    go tool pprof -top cpu.pprof
    go tool pprof -web cpu.pprof  # 如安装 graphviz
    go tool pprof -top heap.pprof
    go tool pprof -top mutex.pprof
    go tool pprof -top block.pprof

Expected Analysis Output

  • CPU: 确认是否存在明显热点算子、锁、序列化/反序列化或调度路径;
  • Memory: 定位高分配点,确认是否有持续增长(可疑泄漏)或短时暴涨;
  • Mutex: 定位高竞争对象及其调用链,给出是否为代码路径、并发参数或引擎交互导致。

Acceptance

  • 采集步骤对同一 workload 可复现
  • 形成每类 profile 的结论与可落地优化建议
  • 输出下一步修复建议清单(含优先级)

Activity

  1. added
    severity/s0
    and removed on Jul 6, 2026
  2. added theissue type on Jul 6, 2026
  3. added this to the MO-202607 milestone on Jul 6, 2026
  4. VioletQwQ-0 commented on Jul 8, 2026

    @VioletQwQ-0
    Collaborator

    point_select

    配置

    TPS / p95

    模式 TPS p95
    --db-ps-mode=disable 64,861 84ms
    --db-ps-mode=auto (prepared) 139,218 35ms

    Profile(auto 1000vu,8GB cache)

    CPU(53 核满载):Syscall6 5.19% + sync.Mutex.Lock 2.65% + gcmarknewobject 2.48% + context.value 2.27% + prometheus.observe 2.19%。无单点热点(cache 命中,无 lz4/memmove/crc32)。

    Mutex(总等待 617562s):sync.Mutex.Unlock 88.75% + runtime.unlock 10.98% + RWMutex.Unlock 31.38% cum。调用链 frontend.handleConn → doComQuery → executeStmtWithTxn,139k QPS 下 frontend 路径分布式锁(Session.mu/Routine/planCache 等),非单把锁,架构层天花板。

    Block(总阻塞 377106s):selectgo 61.17% + sync.Mutex.Lock 30.91% + fdMutex.rwlock 5.57%。

    子项结论

    closeTxn 锁竞争(#25489 + PR #25490):closeTxn 第一次 client.mu.Lock 是废锁——markAllActiveTxnAborted 只发 abortC channel 信号不碰锁,每条 autocommit 关闭 txn 都白排一次队。删除后 mutex 等待 -80%、p95 -18%。详见 #25489。

    plan DeepCopy(P0 第二项):planCache 用 SQL 文本做 key,--db-ps-mode=disable 下随机 id 字面量导致命中率 0,每条 SQL 重新 build plan + DeepCopy。auto/prepared 命中后 sql/plan cum 12.79%→0.61%。非 MO bug,优化方向是 SQL normalize(feature)。

    SQLFlusher trace 分配(#25505):trace/ETL 导出路径 SQLFlusher.FlushBuffer 每次新建 10MB buffer(DefaultWriterBufferSize),30s 分配 156GB,CPU 0.006% 但推高 GC。sync.Pool 和按 buf.Len() 预分配两方案均 TPS 退化(-18%/-20%),放弃。详见 #25505。

    Profile 位置

    • mo-srv-129:/data/mo-prof/pprof/{1000vu_bigcache_disable,1000vu_bigcache_auto}/
  5. VioletQwQ-0 commented on Jul 8, 2026

    @VioletQwQ-0
    Collaborator

    read_write 摸底

    配置

    TPS

    vusers TPS 备注
    1 17.8
    10 193
    50 542
    100 622
    500 799 饱和点
    1000 2,278 8GB cache(大 cache 对写路径也提升 ~2.9x)

    read_write 比 point_select 早饱和(500vu 撞墙),写路径瓶颈更重。

    Profile(1000vu)

    CPU:Syscall6 13.33% + memmove 11.53% + lz4.decodeBlock 5.08% + GC ~16%(scanSpan 7.56% + tryDeferToSpanScan 5.20% + fpTracebackPartialExpand 3.25%)。写 + raft + trace 产生大量垃圾,GC 压力高于 point_select。

    Mutex(总等待 9292s):runtime.unlock 82.52% + sync.Mutex.Unlock 14.56% + RWMutex.Unlock 12.18% cum。和 point_select 不同——read_write 锁竞争主导是 runtime 内部锁(GC/调度),非应用层 sync.Mutex。

    Block(总阻塞 18722 hrs,point_select 的 54 倍):selectgo 92.99% + fdMutex.rwlock 5.61% + txn/rpc.handleTxnRequest 45.61% cum(事务 RPC worker 等待)+ morpc.remoteBackend.writeLoop 1.72%。

    Alloc(1580GB/30s):SQLFlusher 156GB + reflect.unsafe_New 111GB + dragonboat raft 74GB + plan.DeepCopyExpr 58GB + time.newTimer 30GB。

    子项结论

    SQLFlusher trace 分配(#25505):read_write 1000vu 下 SQLFlusher 156GB alloc,跨 workload 复现(point_select auto 151GB)。详见 #25505。

    handleTxnRequest block(8539 hrs / 45.61%):line-level 归因确认是 worker pool 空闲等待,非瓶颈。block 全在 server.go:305 的 select 等 s.queue 来请求(8539 hrs),实际处理 req.exec() 仅 535s(0.002%)。多 worker goroutine 累计空闲,不是排队等待,无 runnable starvation,不建 issue。

    内存泄漏检测(8GB cache,1000vu,压测前/中/后 heap)

    时间点 inuse 大头
    压测前 fifocache 154MB + txn/trace 92MB + logtail 38MB
    压测中峰值 vector.NewVec 586MB + Vector.Unmarshal 134MB + HandleRowsDelete 122MB
    压测后 30s fifocache 154MB + txn/trace 92MB + logtail 64MB

    无明显泄漏:压测中峰值分配在压测后被 GC 回收,before/after 稳定占用持平。after 略高(bytes.growSlice 72MB)属后台 ETL/trace 正常波动。注:单次 1000vu 无法触发长时间泄漏(如 #25198 系列 CN 事务泄漏),需长时间多轮压测验证。

    Profile 位置

    • mo-srv-129:/data/mo-prof/pprof_rw/1000vu/(默认 cache 6 档 profile)+ /data/mo-prof/pprof/rw_leak/(8GB cache 泄漏检测)
    • 火焰图见下方附件
  6. VioletQwQ-0 commented on Jul 8, 2026

    @VioletQwQ-0
    Collaborator

    阶段更新:clean point_select + SQLFlusher 跨 workload 复现

    point_select clean rer跑

    之前 point_select 表被 read_write 的 delete/tombstone 污染,IsRowDeleted 进入读取路径,影响了 point_select 归因。重新使用 clean 数据后结果提升:

    模式 TPS 变化
    --db-ps-mode=disable 67,439 +4%
    --db-ps-mode=auto (prepared) 150,942 +8%

    clean auto 1000vu:avg 6.61ms,p95 27.17ms,CPU profile 1602.14s/30s ≈ 53.4 cores(64 CPU 机器上不是完全打满)。

    clean auto 1000vu 当前瓶颈

    不是单一 closeTxn 锁。

    CPU 侧:

    • 网络返回 syscall:linux.Syscall6 95.06s / 5.93%,主要是结果包写回客户端。
    • GC/分配:mallocgc 14.56% cum,gcBgMarkWorker 9.8% cum。
    • 读路径:TableScan/readutil/blockio 有明显 cum 成本。

    Mutex 侧:

    • total wait:486.38hrs
    • IsMOCtledSpan:338.65hrs / 69.63%
    • trace.Start:308.91hrs / 63.51%
    • txnClient.closeTxn:35.88hrs / 7.38%
    • txnOperator.closeLocked:36.16hrs / 7.44%

    这说明 #25490 之前的 closeTxn 废锁已经不是主导;clean auto 的最大锁等待来自 trace control 读路径的全局 mutex。该发现已建 #25539,但由于 trace 正在整体删除,#25539 已关闭,仅保留 profile 证据作为背景。

    SQLFlusher 分配(#25505)跨 workload 复现

    SQLFlusher.FlushBuffer 的固定 10MB buffer 分配在 clean point_select、read_only、insert 中都复现:

    workload total alloc SQLFlusher.FlushBuffer share
    point_select clean auto 1000vu 4.62TB 223.18GB flat / 225.06GB cum 4.87%
    read_only 500vu 7.65TB ~0.23TB cum 3.05%
    read_only 1000vu 7.07TB 230.25GB flat / 236.91GB cum 3.27%
    insert 500vu 8.67TB ~0.23TB cum 2.71%
    insert 1000vu 9.09TB 232.46GB flat / 240.73GB cum 2.59%

    line-level 均指向:

    pkg/util/export/etl/content.go:196
    sqlCsv := bytes.NewBuffer(make([]byte, 0, table.DefaultWriterBufferSize))
    

    read_only/insert 1000vu 中,fmt.Sprintf 只有 4-6GB,conn.Exec 只有 2-3GB,说明主因是每次 flush 固定申请 10MB escape buffer。

    下一步

    1. [Performance] SQLFlusher trace/ETL path allocates heavily under high-concurrency sysbench workloads #25505 使用当前 patch 重跑 point_select clean auto 1000vu。验收:SQLFlusher.FlushBuffer 从 ~223GB 降到 GB 级或退出 top20,TPS/CPU 不回退。
    2. 重跑必须使用 clean point_select 数据,不能复用跑过 read_write/delete 的表,避免 tombstone 污染 TPS/CPU/profile。
    3. read_only/insert 结果可作为 [Performance] SQLFlusher trace/ETL path allocates heavily under high-concurrency sysbench workloads #25505 的跨 workload 证据,不直接混入 point_select patch 验收基线。
  7. VioletQwQ-0 commented on Jul 15, 2026

    @VioletQwQ-0
    Collaborator

    P1 allocation optimization update: PR #25701 / #25710

    在 clean data、1000vu point_select auto、disable-trace=true、8GB memory + 50GB disk SHARED cache 条件下,已完成两个相互独立的高频分配优化验证。

    PR #25701:复用 SELECT result columns

    普通 SELECT 返回路径原先会构造两次 result column:一次用于 MySQL protocol columns,一次用于 Session.rs。该 PR 改为只构造一次并复用同一份 snapshot;SHOW COLUMNS、其他 computation wrapper 和非 SELECT 路径保持原逻辑。

    本地 microbenchmark:

    路径 B/op allocs/op
    duplicate construction 736 6
    reused construction 440 4

    即单次列构造路径约 -40.2% B/op、-33.3% allocs/op。

    mo-srv-129 fresh-data A/B/A:

    指标 baseline mean patched 变化
    TPS 177,857 178,904 +0.6%
    alloc/query 25.0 KB 24.7 KB 约 -1.2%
    malloc/query 317.1 314.8 -0.7%

    GetResultColumnsFromPlan.func1 alloc space 从 3.02 GB/30s 降到 1.44 GB/30.7s,按时间归一化约下降 53%。TPS 和平均延迟保持中性,符合该 PR “减少 SELECT response allocation churn,不改变执行计划和查询语义”的目标。

    PR #25710:避免默认 transaction callback map/slice 分配

    原实现每个新事务都会为两个 client-owned ClosedEvent callback 创建 map/slice。该 PR 将 immutable default callbacks 存在 txnClient,operator 只在添加 custom callback 时通过 copy-on-write 创建私有 map。

    本地 benchmark:

    场景 Before After 变化
    New + rollback 2544 B / 17 allocs 2000 B / 12 allocs -21.4% B,-5 allocs
    New + one custom callback 2664 B / 19 allocs 2528 B / 17 allocs -5.1% B,-2 allocs
    New + 14 trace callbacks 5976 B / 50 allocs 4088 B / 46 allocs -31.6% B,-4 allocs

    mo-srv-129 clean A/B/A:

    • baseline A:180,508.66 TPS
    • patched:188,905.01 TPS
    • baseline B:178,377.58 TPS
    • baseline mean:179,443.12 TPS,patched 相对提升约 5.3%
    • 独立 profile 对照:186,083.03 vs 180,208.74 TPS,提升约 3.3%

    AppendEventCallback 原约 2.65 GB/30s 的热点在 patched alloc profile 中消失,未观察到等量分配迁移。default/custom 顺序、首错短路、commit/rollback close-once、operator 隔离、RestartTxn 清理和 race 生命周期均有测试覆盖。

    当前结论

  8. VioletQwQ-0 commented on Jul 23, 2026

    @VioletQwQ-0
    Collaborator

    Post-merge update for #26026:

    • Merged on 2026-07-22. Final PR head: 713e2c52d2faf4638d3d579e00d1717eb6b44a75; squash merge commit: 56f2d4d234cb0dbc434d3464752d5b43a61c25a1.
    • The retired single-KindOption path now avoids the escaping SpanConfig allocation while preserving custom options, multi-option ordering, nil-context normalization, trace-context behavior, and the alternate provider path.
    • Fresh-data A/B/A measured TPS +0.45%, CPU/query -0.56%, alloc/query -3.86%, malloc/query -2.35%, and GC cycles/query -3.98%. The profile removed approximately 4.018 GB / 60s and 29.25M objects / 60s from NonRecordingTracer.Start without an equivalent allocation migration.
    • The throughput result is treated as neutral; the confirmed benefit is reduced allocation and GC churn.
    • Required PR CI passed, and the post-merge TKE sysbench check passed. The failed merge-update-moc / Update MOC check is a post-merge sync workflow and is not a code-regression signal for this PR.

    This closes only the retired trace KindOption allocation sub-item. #25472 remains open as the umbrella profiling/optimization task for the remaining observability and workload-specific hotspots.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area/observabilityseverity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions