-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for processing accelerators hardware #385
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,48 @@ | ||
// | ||
// Use and distribution licensed under the Apache license version 2. | ||
// | ||
// See the COPYING file in the root project directory for full text. | ||
// | ||
|
||
package commands | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/jaypipes/ghw" | ||
"github.com/pkg/errors" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// acceleratorCmd represents the install command | ||
var acceleratorCmd = &cobra.Command{ | ||
Use: "accelerator", | ||
Short: "Show processing accelerators information for the host system", | ||
RunE: showGPU, | ||
} | ||
|
||
// showAccelerator show processing accelerators information for the host system. | ||
func showAccelerator(cmd *cobra.Command, args []string) error { | ||
accel, err := ghw.Accelerator() | ||
if err != nil { | ||
return errors.Wrap(err, "error getting Accelerator info") | ||
} | ||
|
||
switch outputFormat { | ||
case outputFormatHuman: | ||
fmt.Printf("%v\n", accel) | ||
|
||
for _, card := range accel.Devices { | ||
fmt.Printf(" %v\n", card) | ||
} | ||
case outputFormatJSON: | ||
fmt.Printf("%s\n", accel.JSONString(pretty)) | ||
case outputFormatYAML: | ||
fmt.Printf("%s", accel.YAMLString()) | ||
} | ||
return nil | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(acceleratorCmd) | ||
} |
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
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,84 @@ | ||
// | ||
// Use and distribution licensed under the Apache license version 2. | ||
// | ||
// See the COPYING file in the root project directory for full text. | ||
// | ||
|
||
package accelerator | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/jaypipes/ghw/pkg/context" | ||
"github.com/jaypipes/ghw/pkg/marshal" | ||
"github.com/jaypipes/ghw/pkg/option" | ||
"github.com/jaypipes/ghw/pkg/pci" | ||
) | ||
|
||
type AcceleratorDevice struct { | ||
// the PCI address where the accelerator device can be found | ||
Address string `json:"address"` | ||
// pointer to a PCIDevice struct that describes the vendor and product | ||
// model, etc | ||
PCIDevice *pci.Device `json:"pci_device"` | ||
} | ||
|
||
func (dev *AcceleratorDevice) String() string { | ||
deviceStr := dev.Address | ||
if dev.PCIDevice != nil { | ||
deviceStr = dev.PCIDevice.String() | ||
} | ||
nodeStr := "" | ||
return fmt.Sprintf( | ||
"device %s@%s", | ||
nodeStr, | ||
deviceStr, | ||
) | ||
} | ||
|
||
type Info struct { | ||
ctx *context.Context | ||
Devices []*AcceleratorDevice `json:"devices"` | ||
} | ||
|
||
// New returns a pointer to an Info struct that contains information about the | ||
// accelerator devices on the host system | ||
func New(opts ...*option.Option) (*Info, error) { | ||
ctx := context.New(opts...) | ||
info := &Info{ctx: ctx} | ||
|
||
if err := ctx.Do(info.load); err != nil { | ||
return nil, err | ||
} | ||
return info, nil | ||
} | ||
|
||
func (i *Info) String() string { | ||
numDevsStr := "devices" | ||
if len(i.Devices) == 1 { | ||
numDevsStr = "device" | ||
} | ||
return fmt.Sprintf( | ||
"processing accelerators (%d %s)", | ||
len(i.Devices), | ||
numDevsStr, | ||
) | ||
} | ||
|
||
// simple private struct used to encapsulate processing accelerators information in a top-level | ||
// "accelerator" YAML/JSON map/object key | ||
type acceleratorPrinter struct { | ||
Info *Info `json:"accelerator"` | ||
} | ||
|
||
// YAMLString returns a string with the processing accelerators information formatted as YAML | ||
// under a top-level "accelerator:" key | ||
func (i *Info) YAMLString() string { | ||
return marshal.SafeYAML(i.ctx, acceleratorPrinter{i}) | ||
} | ||
|
||
// JSONString returns a string with the processing accelerators information formatted as JSON | ||
// under a top-level "accelerator:" key | ||
func (i *Info) JSONString(indent bool) string { | ||
return marshal.SafeJSON(i.ctx, acceleratorPrinter{i}, indent) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we want to extend (maybe later?) this API to accept additive user-provided filters?
I'm referring to the wide variety of devices out there and how they present themselves on PCI bus.
By "additive" filter I mean
ghw
is really sure it's an accelerator (this meansghw
must be conservative in the core logic, which I think it's good anyway)ghw
for whatever reasons (maybe oldghw
version, maybe rare/custom devices)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. @mlorenzofr was going to submit a PR adding that filtering functionality. He originally added it in this PR but I asked him to separate it out into a new one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perfect! I'll be on the lookout this time!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we'll extend it, but in a different PR. @jaypipes asked to separate it from the original development so he could analyze it step by step.
Welcome back, btw 😉