diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-06 16:04:43 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-06 16:04:43 +0200 |
| commit | cc4edee087143d656fc74067297f3f540b61362e (patch) | |
| tree | 7bd4b8bf841037b2fb19779d52cff6219c4fa070 /internal | |
| parent | a2d253f1e92578ccea95f962bbd1a1aebf190de1 (diff) | |
refactor: share collapse field validation lists (task 387)
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/collapse/fields.go | 39 | ||||
| -rw-r--r-- | internal/flags/flags.go | 30 | ||||
| -rw-r--r-- | internal/flamegraph/livetrie.go | 15 |
3 files changed, 50 insertions, 34 deletions
diff --git a/internal/collapse/fields.go b/internal/collapse/fields.go new file mode 100644 index 0000000..b87fe11 --- /dev/null +++ b/internal/collapse/fields.go @@ -0,0 +1,39 @@ +package collapse + +import "slices" + +var validFields = []string{ + "path", + "comm", + "tracepoint", + "pid", + "tid", + "flags", +} + +var validCountFields = []string{ + "count", + "duration", + "durationToPrev", + "bytes", +} + +// ValidFields returns a copy of supported collapse fields. +func ValidFields() []string { + return slices.Clone(validFields) +} + +// ValidCountFields returns a copy of supported collapse count fields. +func ValidCountFields() []string { + return slices.Clone(validCountFields) +} + +// IsValidField reports whether a collapse field is supported. +func IsValidField(field string) bool { + return slices.Contains(validFields, field) +} + +// IsValidCountField reports whether a collapse count field is supported. +func IsValidCountField(field string) bool { + return slices.Contains(validCountFields, field) +} diff --git a/internal/flags/flags.go b/internal/flags/flags.go index cc6e70a..67842ab 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -10,6 +10,8 @@ import ( "sync" "sync/atomic" "time" + + "ior/internal/collapse" ) var ( @@ -23,24 +25,6 @@ func init() { current.Store(&defaults) } -var ( - validCollapsedFields = []string{ - "path", - "comm", - "tracepoint", - "pid", - "tid", - "flags", - } - - validCollapsedCounts = []string{ - "count", - "duration", - "durationToPrev", - "bytes", - } -) - type Flags struct { PidFilter int TidFilter int @@ -159,6 +143,8 @@ func Parse() error { func parse() error { cfg := NewFlags() + validFields := collapse.ValidFields() + validCounts := collapse.ValidCountFields() flag.IntVar(&cfg.PidFilter, "pid", cfg.PidFilter, "Filter for processes ID") flag.IntVar(&cfg.TidFilter, "tid", cfg.TidFilter, "Filter for thread ID") @@ -179,9 +165,9 @@ func parse() error { flag.DurationVar(&cfg.LiveInterval, "live-interval", cfg.LiveInterval, "Synthetic live flamegraph refresh interval for --testliveflames") flag.BoolVar(&cfg.TUIExportEnable, "tuiExport", cfg.TUIExportEnable, "Enable writing TUI snapshot export files") fields := flag.String("fields", "", - fmt.Sprintf("Comma separated list of fields to collapse, valid are: %v", validCollapsedFields)) + fmt.Sprintf("Comma separated list of fields to collapse, valid are: %v", validFields)) flag.StringVar(&cfg.CountField, "count", cfg.CountField, - fmt.Sprintf("Count field to collapse, valid are: %v", validCollapsedCounts)) + fmt.Sprintf("Count field to collapse, valid are: %v", validCounts)) if err := flag.CommandLine.Parse(os.Args[1:]); err != nil { return err } @@ -208,12 +194,12 @@ func parse() error { } for _, field := range cfg.CollapsedFields { - if !slices.Contains(validCollapsedFields, field) { + if !collapse.IsValidField(field) { return fmt.Errorf("invalid field for collapse: %s", field) } } - if !slices.Contains(validCollapsedCounts, cfg.CountField) { + if !collapse.IsValidCountField(cfg.CountField) { return fmt.Errorf("invalid count field: %s", cfg.CountField) } diff --git a/internal/flamegraph/livetrie.go b/internal/flamegraph/livetrie.go index 9f1fd91..04137bf 100644 --- a/internal/flamegraph/livetrie.go +++ b/internal/flamegraph/livetrie.go @@ -9,6 +9,7 @@ import ( "sync" "sync/atomic" + "ior/internal/collapse" "ior/internal/event" ) @@ -261,21 +262,11 @@ func normalizeLiveTrieFields(fields []string) ([]string, error) { } func isLiveTrieField(field string) bool { - switch field { - case "path", "comm", "tracepoint", "pid", "tid", "flags": - return true - default: - return false - } + return collapse.IsValidField(field) } func isLiveTrieCountField(field string) bool { - switch field { - case "count", "duration", "durationToPrev", "bytes": - return true - default: - return false - } + return collapse.IsValidCountField(field) } func subtreeTotal(node *trieNode) uint64 { |
