-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for kernel lock analysis (#114)
* Add support for kernel lock analysis * Fix code format issue by make format * Fix staticcheck issue * address the reviewer's comment, except for the html output * Add html render * Modify README for perf lock
- Loading branch information
Showing
7 changed files
with
241 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Package lock is a subcommand of the root command. It is used to collect kernel lock related perf information from target(s). | ||
package lock | ||
|
||
// Copyright (C) 2021-2024 Intel Corporation | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"perfspect/internal/common" | ||
"perfspect/internal/report" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
const cmdName = "lock" | ||
|
||
var examples = []string{ | ||
fmt.Sprintf(" Lock inspect from local host: $ %s %s", common.AppName, cmdName), | ||
fmt.Sprintf(" Lock inspect from remote target: $ %s %s --target 192.168.1.1 --user fred --key fred_key", common.AppName, cmdName), | ||
fmt.Sprintf(" Lock inspect from multiple targets: $ %s %s --targets targets.yaml", common.AppName, cmdName), | ||
} | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: cmdName, | ||
Short: "Collect system information for kernel lock analysis from target(s)", | ||
Long: "", | ||
Example: strings.Join(examples, "\n"), | ||
RunE: runCmd, | ||
PreRunE: validateFlags, | ||
GroupID: "primary", | ||
Args: cobra.NoArgs, | ||
SilenceErrors: true, | ||
} | ||
|
||
var ( | ||
flagDuration int | ||
flagFrequency int | ||
) | ||
|
||
const ( | ||
flagDurationName = "duration" | ||
flagFrequencyName = "frequency" | ||
) | ||
|
||
func init() { | ||
Cmd.Flags().StringVar(&common.FlagInput, common.FlagInputName, "", "") | ||
Cmd.Flags().StringSliceVar(&common.FlagFormat, common.FlagFormatName, []string{report.FormatHtml}, "") | ||
Cmd.Flags().IntVar(&flagDuration, flagDurationName, 10, "") | ||
Cmd.Flags().IntVar(&flagFrequency, flagFrequencyName, 11, "") | ||
|
||
common.AddTargetFlags(Cmd) | ||
|
||
Cmd.SetUsageFunc(usageFunc) | ||
} | ||
|
||
func usageFunc(cmd *cobra.Command) error { | ||
cmd.Printf("Usage: %s [flags]\n\n", cmd.CommandPath()) | ||
cmd.Printf("Examples:\n%s\n\n", cmd.Example) | ||
cmd.Println("Flags:") | ||
for _, group := range getFlagGroups() { | ||
cmd.Printf(" %s:\n", group.GroupName) | ||
for _, flag := range group.Flags { | ||
flagDefault := "" | ||
if cmd.Flags().Lookup(flag.Name).DefValue != "" { | ||
flagDefault = fmt.Sprintf(" (default: %s)", cmd.Flags().Lookup(flag.Name).DefValue) | ||
} | ||
cmd.Printf(" --%-20s %s%s\n", flag.Name, flag.Help, flagDefault) | ||
} | ||
} | ||
cmd.Println("\nGlobal Flags:") | ||
cmd.Parent().PersistentFlags().VisitAll(func(pf *pflag.Flag) { | ||
flagDefault := "" | ||
if cmd.Parent().PersistentFlags().Lookup(pf.Name).DefValue != "" { | ||
flagDefault = fmt.Sprintf(" (default: %s)", cmd.Flags().Lookup(pf.Name).DefValue) | ||
} | ||
cmd.Printf(" --%-20s %s%s\n", pf.Name, pf.Usage, flagDefault) | ||
}) | ||
return nil | ||
} | ||
|
||
func getFlagGroups() []common.FlagGroup { | ||
var groups []common.FlagGroup | ||
flags := []common.Flag{ | ||
{ | ||
Name: flagDurationName, | ||
Help: "number of seconds to run the collection", | ||
}, | ||
{ | ||
Name: flagFrequencyName, | ||
Help: "number of samples taken per second", | ||
}, | ||
} | ||
groups = append(groups, common.FlagGroup{ | ||
GroupName: "Options", | ||
Flags: flags, | ||
}) | ||
groups = append(groups, common.GetTargetFlagGroup()) | ||
|
||
return groups | ||
} | ||
|
||
func validateFlags(cmd *cobra.Command, args []string) error { | ||
if flagDuration <= 0 { | ||
err := fmt.Errorf("duration must be greater than 0") | ||
fmt.Fprintf(os.Stderr, "Error: %v\n", err) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func runCmd(cmd *cobra.Command, args []string) error { | ||
reportingCommand := common.ReportingCommand{ | ||
Cmd: cmd, | ||
ReportNamePost: "lock", | ||
Frequency: flagFrequency, | ||
Duration: flagDuration, | ||
TableNames: []string{report.KernelLockAnalysisTableName}, | ||
} | ||
return reportingCommand.Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters