|
| 1 | +// Package lock is a subcommand of the root command. It is used to collect kernel lock related perf information from target(s). |
| 2 | +package lock |
| 3 | + |
| 4 | +// Copyright (C) 2021-2024 Intel Corporation |
| 5 | +// SPDX-License-Identifier: BSD-3-Clause |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "perfspect/internal/common" |
| 11 | + "perfspect/internal/report" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/spf13/cobra" |
| 15 | + "github.com/spf13/pflag" |
| 16 | +) |
| 17 | + |
| 18 | +const cmdName = "lock" |
| 19 | + |
| 20 | +var examples = []string{ |
| 21 | + fmt.Sprintf(" Lock inspect from local host: $ %s %s", common.AppName, cmdName), |
| 22 | + fmt.Sprintf(" Lock inspect from remote target: $ %s %s --target 192.168.1.1 --user fred --key fred_key", common.AppName, cmdName), |
| 23 | + fmt.Sprintf(" Lock inspect from multiple targets: $ %s %s --targets targets.yaml", common.AppName, cmdName), |
| 24 | +} |
| 25 | + |
| 26 | +var Cmd = &cobra.Command{ |
| 27 | + Use: cmdName, |
| 28 | + Short: "Collect system information for kernel lock analysis from target(s)", |
| 29 | + Long: "", |
| 30 | + Example: strings.Join(examples, "\n"), |
| 31 | + RunE: runCmd, |
| 32 | + PreRunE: validateFlags, |
| 33 | + GroupID: "primary", |
| 34 | + Args: cobra.NoArgs, |
| 35 | + SilenceErrors: true, |
| 36 | +} |
| 37 | + |
| 38 | +var ( |
| 39 | + flagDuration int |
| 40 | + flagFrequency int |
| 41 | +) |
| 42 | + |
| 43 | +const ( |
| 44 | + flagDurationName = "duration" |
| 45 | + flagFrequencyName = "frequency" |
| 46 | +) |
| 47 | + |
| 48 | +func init() { |
| 49 | + Cmd.Flags().StringVar(&common.FlagInput, common.FlagInputName, "", "") |
| 50 | + Cmd.Flags().StringSliceVar(&common.FlagFormat, common.FlagFormatName, []string{report.FormatHtml}, "") |
| 51 | + Cmd.Flags().IntVar(&flagDuration, flagDurationName, 10, "") |
| 52 | + Cmd.Flags().IntVar(&flagFrequency, flagFrequencyName, 11, "") |
| 53 | + |
| 54 | + common.AddTargetFlags(Cmd) |
| 55 | + |
| 56 | + Cmd.SetUsageFunc(usageFunc) |
| 57 | +} |
| 58 | + |
| 59 | +func usageFunc(cmd *cobra.Command) error { |
| 60 | + cmd.Printf("Usage: %s [flags]\n\n", cmd.CommandPath()) |
| 61 | + cmd.Printf("Examples:\n%s\n\n", cmd.Example) |
| 62 | + cmd.Println("Flags:") |
| 63 | + for _, group := range getFlagGroups() { |
| 64 | + cmd.Printf(" %s:\n", group.GroupName) |
| 65 | + for _, flag := range group.Flags { |
| 66 | + flagDefault := "" |
| 67 | + if cmd.Flags().Lookup(flag.Name).DefValue != "" { |
| 68 | + flagDefault = fmt.Sprintf(" (default: %s)", cmd.Flags().Lookup(flag.Name).DefValue) |
| 69 | + } |
| 70 | + cmd.Printf(" --%-20s %s%s\n", flag.Name, flag.Help, flagDefault) |
| 71 | + } |
| 72 | + } |
| 73 | + cmd.Println("\nGlobal Flags:") |
| 74 | + cmd.Parent().PersistentFlags().VisitAll(func(pf *pflag.Flag) { |
| 75 | + flagDefault := "" |
| 76 | + if cmd.Parent().PersistentFlags().Lookup(pf.Name).DefValue != "" { |
| 77 | + flagDefault = fmt.Sprintf(" (default: %s)", cmd.Flags().Lookup(pf.Name).DefValue) |
| 78 | + } |
| 79 | + cmd.Printf(" --%-20s %s%s\n", pf.Name, pf.Usage, flagDefault) |
| 80 | + }) |
| 81 | + return nil |
| 82 | +} |
| 83 | + |
| 84 | +func getFlagGroups() []common.FlagGroup { |
| 85 | + var groups []common.FlagGroup |
| 86 | + flags := []common.Flag{ |
| 87 | + { |
| 88 | + Name: flagDurationName, |
| 89 | + Help: "number of seconds to run the collection", |
| 90 | + }, |
| 91 | + { |
| 92 | + Name: flagFrequencyName, |
| 93 | + Help: "number of samples taken per second", |
| 94 | + }, |
| 95 | + } |
| 96 | + groups = append(groups, common.FlagGroup{ |
| 97 | + GroupName: "Options", |
| 98 | + Flags: flags, |
| 99 | + }) |
| 100 | + groups = append(groups, common.GetTargetFlagGroup()) |
| 101 | + |
| 102 | + return groups |
| 103 | +} |
| 104 | + |
| 105 | +func validateFlags(cmd *cobra.Command, args []string) error { |
| 106 | + if flagDuration <= 0 { |
| 107 | + err := fmt.Errorf("duration must be greater than 0") |
| 108 | + fmt.Fprintf(os.Stderr, "Error: %v\n", err) |
| 109 | + return err |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +func runCmd(cmd *cobra.Command, args []string) error { |
| 115 | + reportingCommand := common.ReportingCommand{ |
| 116 | + Cmd: cmd, |
| 117 | + ReportNamePost: "lock", |
| 118 | + Frequency: flagFrequency, |
| 119 | + Duration: flagDuration, |
| 120 | + TableNames: []string{report.KernelLockAnalysisTableName}, |
| 121 | + } |
| 122 | + return reportingCommand.Run() |
| 123 | +} |
0 commit comments