-
Notifications
You must be signed in to change notification settings - Fork 182
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 processing accelerators hardware
- Loading branch information
1 parent
80d3134
commit 768e17f
Showing
10 changed files
with
513 additions
and
26 deletions.
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,50 @@ | ||
// | ||
// 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 { | ||
filter := make([]string, 0) | ||
|
||
accel, err := ghw.Accelerator(filter) | ||
if err != nil { | ||
return errors.Wrap(err, "error getting Accelerator info") | ||
} | ||
|
||
switch outputFormat { | ||
case outputFormatHuman: | ||
fmt.Printf("%v\n", accel) | ||
|
||
for _, card := range accel.AcceleratorCards { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// | ||
// 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 AcceleratorCard struct { | ||
// the PCI address where the accelerator card can be found | ||
Address string `json:"address"` | ||
// pointer to a PCIDevice struct that describes the vendor and product | ||
// model, etc | ||
Pci *pci.Device `json:"pci"` | ||
} | ||
|
||
func (card *AcceleratorCard) String() string { | ||
deviceStr := card.Address | ||
if card.Pci != nil { | ||
deviceStr = card.Pci.String() | ||
} | ||
nodeStr := "" | ||
return fmt.Sprintf( | ||
"card %s@%s", | ||
nodeStr, | ||
deviceStr, | ||
) | ||
} | ||
|
||
type Info struct { | ||
ctx *context.Context | ||
AcceleratorCards []*AcceleratorCard `json:"cards"` | ||
DiscoveryFilters []string | ||
} | ||
|
||
// New returns a pointer to an Info struct that contains information about the | ||
// accelerator cards on the host system | ||
func New(filter []string, opts ...*option.Option) (*Info, error) { | ||
ctx := context.New(opts...) | ||
|
||
info := &Info{ | ||
ctx: ctx, | ||
DiscoveryFilters: filter, | ||
} | ||
|
||
if err := ctx.Do(info.load); err != nil { | ||
Check failure on line 55 in pkg/accelerator/accelerator.go GitHub Actions / windows-2019 (1.19)
Check failure on line 55 in pkg/accelerator/accelerator.go GitHub Actions / windows-2022 (1.20)
|
||
return nil, err | ||
} | ||
return info, nil | ||
} | ||
|
||
func (i *Info) String() string { | ||
numCardsStr := "cards" | ||
if len(i.AcceleratorCards) == 1 { | ||
numCardsStr = "card" | ||
} | ||
return fmt.Sprintf( | ||
"processing accelerators (%d %s)", | ||
len(i.AcceleratorCards), | ||
numCardsStr, | ||
) | ||
} | ||
|
||
// simple private struct used to encapsulate gpu information in a top-level | ||
// "gpu" YAML/JSON map/object key | ||
type acceleratorPrinter struct { | ||
Info *Info `json:"accelerator"` | ||
} | ||
|
||
// YAMLString returns a string with the gpu 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 gpu 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.