github.com/intel/goresctrl@v0.5.0/cmd/rdt/main.go (about)

     1  /*
     2  Copyright 2021 Intel Corporation
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // This application demonstrates using the blockio API.
    18  
    19  package main
    20  
    21  import (
    22  	"flag"
    23  	"fmt"
    24  	"os"
    25  	"sort"
    26  	"strings"
    27  
    28  	"github.com/intel/goresctrl/pkg/rdt"
    29  )
    30  
    31  var (
    32  	// Global command line flags
    33  	groupPrefix string
    34  )
    35  
    36  type subCmd func([]string) error
    37  
    38  var subCmds = map[string]subCmd{
    39  	"info":      subCmdInfo,
    40  	"configure": subCmdConfigure,
    41  }
    42  
    43  func main() {
    44  	cmds := make([]string, 0, len(subCmds))
    45  	for c := range subCmds {
    46  		cmds = append(cmds, c)
    47  	}
    48  	sort.Strings(cmds)
    49  	allCmds := strings.Join(cmds, ", ")
    50  
    51  	if len(os.Args) < 2 {
    52  		exitError("missing sub-command, must be one of: %s\n", allCmds)
    53  	}
    54  
    55  	// Run sub-command
    56  	cmd, ok := subCmds[os.Args[1]]
    57  	if !ok {
    58  		exitError("unknown sub-command %q, must be of: %s\n", os.Args[1], allCmds)
    59  	}
    60  
    61  	if err := cmd(os.Args[2:]); err != nil {
    62  		exitError("sub-command %q failed: %v\n", os.Args[1], err)
    63  	}
    64  }
    65  
    66  func addGlobalFlags(flagset *flag.FlagSet) {
    67  	flagset.StringVar(&groupPrefix, "group-prefix", "", "prefix to use for resctrl groups")
    68  }
    69  
    70  func subCmdInfo(args []string) error {
    71  	// Parse command line args
    72  	flags := flag.NewFlagSet("info", flag.ExitOnError)
    73  	addGlobalFlags(flags)
    74  	if err := flags.Parse(args); err != nil {
    75  		return err
    76  	}
    77  
    78  	// Run sub-command
    79  	if err := rdt.Initialize(groupPrefix); err != nil {
    80  		fmt.Printf("RDT is not enabled: %v\n", err)
    81  		return nil
    82  	}
    83  
    84  	fmt.Printf("Monitoring supported: %v\n", rdt.MonSupported())
    85  	if rdt.MonSupported() {
    86  		mon := rdt.GetMonFeatures()
    87  
    88  		fmt.Println("Monitoring features:")
    89  		for r, f := range mon {
    90  			fmt.Printf("  - %s: %v\n", r, strings.Join(f, ", "))
    91  		}
    92  	}
    93  	fmt.Println("Classes:")
    94  	for _, cls := range rdt.GetClasses() {
    95  		fmt.Printf("  - %s\n", cls.Name())
    96  
    97  		mon := cls.GetMonGroups()
    98  		if len(mon) > 0 {
    99  			fmt.Println("    Monitoring groups:")
   100  			for _, grp := range mon {
   101  				fmt.Printf("      - %s\n", grp.Name())
   102  			}
   103  		}
   104  	}
   105  
   106  	return nil
   107  }
   108  
   109  func subCmdConfigure(args []string) error {
   110  	// Parse command line args
   111  	flags := flag.NewFlagSet("configure", flag.ExitOnError)
   112  	addGlobalFlags(flags)
   113  
   114  	configFile := flags.String("config-file", "", "path to rdt configuration file")
   115  	force := flags.Bool("force", false, "force configuration, delete non-empty resctrl groups")
   116  
   117  	if err := flags.Parse(args); err != nil {
   118  		return err
   119  	}
   120  
   121  	if *configFile == "" {
   122  		return fmt.Errorf("-config-file must be specified")
   123  	}
   124  
   125  	// Run sub-command
   126  	if err := rdt.Initialize(groupPrefix); err != nil {
   127  		return fmt.Errorf("RDT is not enabled: %v", err)
   128  	}
   129  
   130  	fmt.Println("Configuring resctrl filesystem...")
   131  	if err := rdt.SetConfigFromFile(*configFile, *force); err != nil {
   132  		return err
   133  	}
   134  
   135  	fmt.Println("Done!")
   136  
   137  	return nil
   138  }
   139  
   140  func exitError(format string, args ...interface{}) {
   141  	fmt.Printf("ERROR: "+format+"\n", args...)
   142  	os.Exit(1)
   143  }