github.com/intel/goresctrl@v0.5.0/cmd/blockio/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  	"encoding/json"
    23  	"flag"
    24  	"fmt"
    25  	"os"
    26  
    27  	"github.com/intel/goresctrl/pkg/blockio"
    28  	goresctrlpath "github.com/intel/goresctrl/pkg/path"
    29  )
    30  
    31  var examples string = `Examples:
    32      # Inspect OCI blockio structure
    33      $ blockio -config sample.cfg -class slowread | jq
    34  
    35      # Apply read throttling to a cgroup
    36      $ blockio -config sample.cfg -class slowread -cgroup user.slice/mygroup
    37      $ cat /sys/fs/cgroup/blkio/user.slice/mygroup/blkio.throttle.read_bps_device
    38  
    39      # Remove throttling from a cgroup
    40      $ blockio -config sample.cfg -class nolimit -cgroup user.slice/mygroup
    41  `
    42  
    43  func usage() {
    44  	flag.CommandLine.SetOutput(os.Stdout)
    45  	fmt.Fprintln(flag.CommandLine.Output(), "blockio - demo application for goresctrl/pkg/blockio API")
    46  	fmt.Fprintln(flag.CommandLine.Output(), "Usage: blockio -config=FILE -class=NAME [-cgroup=CGROUP]")
    47  	flag.PrintDefaults()
    48  	fmt.Fprint(flag.CommandLine.Output(), examples)
    49  }
    50  
    51  func errorExit(format string, args ...interface{}) {
    52  	fmt.Fprintln(os.Stderr, fmt.Sprintf(format, args...))
    53  	os.Exit(1)
    54  }
    55  
    56  func main() {
    57  	// Parse commandline arguments
    58  	flag.Usage = usage
    59  	flag.Func("prefix", "set mount prefix for system directories", func(s string) error {
    60  		goresctrlpath.SetPrefix(s)
    61  		return nil
    62  	})
    63  	optConfig := flag.String("config", "", "load class configuration from FILE")
    64  	optClass := flag.String("class", "", "use configuration of the blockio class NAME")
    65  	optCgroup := flag.String("cgroup", "", "apply class to CGROUP, otherwise print it as OCI BlockIO structure")
    66  	flag.Parse()
    67  
    68  	if optConfig == nil || *optConfig == "" {
    69  		errorExit("missing -config=FILE")
    70  	}
    71  
    72  	if optClass == nil || *optClass == "" {
    73  		errorExit("missing -class=NAME")
    74  	}
    75  
    76  	// Read blockio class configuration.
    77  	if err := blockio.SetConfigFromFile(*optConfig, true); err != nil {
    78  		errorExit("%v", err)
    79  	}
    80  
    81  	if optCgroup == nil || *optCgroup == "" {
    82  		// If -cgroup=CGROUP is missing, print OCI spec.
    83  		oci, err := blockio.OciLinuxBlockIO(*optClass)
    84  		if err != nil {
    85  			errorExit("%v", err)
    86  		}
    87  		ociBytes, err := json.Marshal(oci)
    88  		if err != nil {
    89  			errorExit("%v", err)
    90  		}
    91  		fmt.Printf("%s\n", ociBytes)
    92  	} else {
    93  		// If -cgroup=CGROUP is given, apply class configuration to it.
    94  		err := blockio.SetCgroupClass(*optCgroup, *optClass)
    95  		if err != nil {
    96  			errorExit("%v", err)
    97  		}
    98  		fmt.Printf("cgroup %s configured to blockio class %q\n", *optCgroup, *optClass)
    99  	}
   100  }