-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #385 from mlorenzofr/accelerators
Add support for processing accelerators hardware
- Loading branch information
Showing
14 changed files
with
409 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,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.