-
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.
WIP patch to demonstrate the possible API TBD: add tests TBD: provide rationale in the commit message Signed-off-by: Francesco Romani <fromani@redhat.com>
- Loading branch information
Showing
11 changed files
with
419 additions
and
18 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" | ||
) | ||
|
||
// sriovCmd represents the install command | ||
var sriovCmd = &cobra.Command{ | ||
Use: "sriov", | ||
Short: "Show SRIOV devices information for the host system", | ||
RunE: showSRIOV, | ||
} | ||
|
||
// showSRIOV show graphics/GPU information for the host system. | ||
func showSRIOV(cmd *cobra.Command, args []string) error { | ||
sriov, err := ghw.SRIOV() | ||
if err != nil { | ||
return errors.Wrap(err, "error getting GPU info") | ||
} | ||
|
||
switch outputFormat { | ||
case outputFormatHuman: | ||
fmt.Printf("%v\n", sriov) | ||
|
||
for _, dev := range sriov.PhysicalFunctions { | ||
fmt.Printf(" %v\n", dev) | ||
} | ||
case outputFormatJSON: | ||
fmt.Printf("%s\n", sriov.JSONString(pretty)) | ||
case outputFormatYAML: | ||
fmt.Printf("%s", sriov.YAMLString()) | ||
} | ||
return nil | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(sriovCmd) | ||
} |
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,117 @@ | ||
// | ||
// Use and distribution licensed under the Apache license version 2. | ||
// | ||
// See the COPYING file in the root project directory for full text. | ||
// | ||
|
||
package sriov | ||
|
||
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" | ||
pciaddr "github.com/jaypipes/ghw/pkg/pci/address" | ||
) | ||
|
||
type Device struct { | ||
Driver string `json:"driver"` | ||
Interfaces []string `json:"interfaces"` | ||
// the PCI address where the SRIOV instance can be found | ||
Address *pciaddr.Address `json:"address"` | ||
PCI *pci.Device `json:"pci"` | ||
} | ||
|
||
type PhysicalFunction struct { | ||
Device | ||
MaxVFNum int `json:"max_vf_num,omitempty"` | ||
VFs []VirtualFunction `json:"vfs,omitempty"` | ||
} | ||
|
||
type VirtualFunction struct { | ||
Device | ||
ID int `json:"id"` | ||
// Address of the (parent) Physical Function this Virtual Function pertains to. | ||
ParentAddress *pciaddr.Address `json:"parent_address,omitempty"` | ||
} | ||
|
||
func (pf *PhysicalFunction) String() string { | ||
deviceStr := pf.Address.String() | ||
nodeStr := "" | ||
if pf.PCI != nil { | ||
deviceStr = pf.PCI.String() | ||
if pf.PCI.Node != nil { | ||
nodeStr = fmt.Sprintf(" [affined to NUMA node %d]", pf.PCI.Node.ID) | ||
} | ||
} | ||
return fmt.Sprintf( | ||
"physical function %s@%s with %d/%d virtual functions", | ||
nodeStr, | ||
deviceStr, | ||
len(pf.VFs), | ||
pf.MaxVFNum, | ||
) | ||
} | ||
|
||
func (vf *VirtualFunction) String() string { | ||
deviceStr := vf.Address.String() | ||
nodeStr := "" | ||
if vf.PCI != nil { | ||
deviceStr = vf.PCI.String() | ||
if vf.PCI.Node != nil { | ||
nodeStr = fmt.Sprintf(" [affined to NUMA node %d]", vf.PCI.Node.ID) | ||
} | ||
} | ||
return fmt.Sprintf( | ||
"virtual function %d %s@%s from %s", | ||
vf.ID, | ||
nodeStr, | ||
deviceStr, | ||
vf.ParentAddress, | ||
) | ||
} | ||
|
||
type Info struct { | ||
ctx *context.Context | ||
PhysicalFunctions []*PhysicalFunction `json:"physical_functions,omitempty"` | ||
VirtualFunctions []*VirtualFunction `json:"virtual_functions,omitempty"` | ||
} | ||
|
||
// New returns a pointer to an Info struct that contains information about the | ||
// SRIOV 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 { | ||
return fmt.Sprintf( | ||
"sriov (%d phsyical %d virtual devices)", | ||
len(i.PhysicalFunctions), | ||
len(i.VirtualFunctions), | ||
) | ||
} | ||
|
||
// simple private struct used to encapsulate gpu information in a top-level | ||
// "sriov" YAML/JSON map/object key | ||
type sriovPrinter struct { | ||
Info *Info `json:"sriov,omitempty"` | ||
} | ||
|
||
// YAMLString returns a string with the gpu information formatted as YAML | ||
// under a top-level "gpu:" key | ||
func (i *Info) YAMLString() string { | ||
return marshal.SafeYAML(i.ctx, sriovPrinter{i}) | ||
} | ||
|
||
// JSONString returns a string with the gpu information formatted as JSON | ||
// under a top-level "gpu:" key | ||
func (i *Info) JSONString(indent bool) string { | ||
return marshal.SafeJSON(i.ctx, sriovPrinter{i}, indent) | ||
} |
Oops, something went wrong.