Files
Reims-Go/internal/qmp/blockstats.go
T

124 lines
4.2 KiB
Go

package qmp
import (
"context"
"encoding/json"
"fmt"
)
// BlockLatencyHistogram mirrors QEMU's BlockLatencyHistogramInfo QMP type.
// Boundaries are in nanoseconds; len(Bins) == len(Boundaries) + 1.
type BlockLatencyHistogram struct {
Boundaries []uint64 `json:"boundaries"`
Bins []uint64 `json:"bins"`
}
// BlockDeviceStats mirrors the subset of QEMU's BlockDeviceStats returned by
// query-blockstats that is relevant to I/O performance analysis.
type BlockDeviceStats struct {
RdBytes uint64 `json:"rd_bytes"`
WrBytes uint64 `json:"wr_bytes"`
UnmapBytes uint64 `json:"unmap_bytes,omitempty"`
RdOperations uint64 `json:"rd_operations"`
WrOperations uint64 `json:"wr_operations"`
FlushOperations uint64 `json:"flush_operations"`
UnmapOperations uint64 `json:"unmap_operations,omitempty"`
RdTotalTimeNS uint64 `json:"rd_total_time_ns"`
WrTotalTimeNS uint64 `json:"wr_total_time_ns"`
FlushTotalTimeNS uint64 `json:"flush_total_time_ns"`
UnmapTotalTimeNS uint64 `json:"unmap_total_time_ns,omitempty"`
RdMerged uint64 `json:"rd_merged,omitempty"`
WrMerged uint64 `json:"wr_merged,omitempty"`
UnmapMerged uint64 `json:"unmap_merged,omitempty"`
RdLatencyHistogram *BlockLatencyHistogram `json:"rd_latency_histogram,omitempty"`
WrLatencyHistogram *BlockLatencyHistogram `json:"wr_latency_histogram,omitempty"`
FlushLatencyHistogram *BlockLatencyHistogram `json:"flush_latency_histogram,omitempty"`
}
// BlockStats mirrors one entry returned by QMP query-blockstats.
type BlockStats struct {
Device string `json:"device,omitempty"`
QDev string `json:"qdev,omitempty"`
NodeName string `json:"node-name,omitempty"`
Stats BlockDeviceStats `json:"stats"`
Parent *BlockStats `json:"parent,omitempty"`
Backing *BlockStats `json:"backing,omitempty"`
}
// DefaultLatencyHistogramBoundariesNS defines 12 log-spaced latency bucket
// edges (10 us .. 500 ms) suitable for block-latency-histogram-set.
var DefaultLatencyHistogramBoundariesNS = []uint64{
10_000, // 10 us
50_000, // 50 us
100_000, // 100 us
250_000, // 250 us
500_000, // 500 us
1_000_000, // 1 ms
2_500_000, // 2.5 ms
5_000_000, // 5 ms
10_000_000, // 10 ms
50_000_000, // 50 ms
100_000_000, // 100 ms
500_000_000, // 500 ms
}
// QueryBlockStats runs QMP `query-blockstats` on an open session.
func (c *Conn) QueryBlockStats() ([]BlockStats, error) {
raw, err := c.Query("query-blockstats", nil)
if err != nil {
return nil, err
}
var stats []BlockStats
if err := json.Unmarshal(raw, &stats); err != nil {
return nil, fmt.Errorf("decode query-blockstats: %w", err)
}
return stats, nil
}
// SetBlockLatencyHistogram configures latency histograms for a block device
// via QMP `block-latency-histogram-set`. Passing an empty boundaries slice
// disables/clears the histograms on that device.
func (c *Conn) SetBlockLatencyHistogram(id string, boundaries []uint64) error {
if id == "" {
return fmt.Errorf("block device id is required")
}
args := map[string]any{"id": id}
if len(boundaries) > 0 {
args["boundaries"] = boundaries
}
return c.Execute("block-latency-histogram-set", args)
}
// QueryBlockStatsVM performs a single short-lived QMP query-blockstats call
// against a running VM's qmp.sock and closes the connection immediately.
func QueryBlockStatsVM(ctx context.Context, vmDir string) ([]BlockStats, error) {
q, err := Dial(ctx, "unix", SocketPath(vmDir))
if err != nil {
return nil, err
}
defer q.Close()
return q.QueryBlockStats()
}
// SetBlockLatencyHistogramVM performs a single short-lived QMP
// block-latency-histogram-set call against a running VM's qmp.sock.
func SetBlockLatencyHistogramVM(ctx context.Context, vmDir, id string, boundaries []uint64) error {
q, err := Dial(ctx, "unix", SocketPath(vmDir))
if err != nil {
return err
}
defer q.Close()
return q.SetBlockLatencyHistogram(id, boundaries)
}
// FindBlockStats locates a block device entry by device name, qdev, or node-name.
func FindBlockStats(entries []BlockStats, name string) (BlockStats, bool) {
for _, e := range entries {
if e.Device == name || e.QDev == name || e.NodeName == name {
return e, true
}
}
return BlockStats{}, false
}