k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/third_party/forked/vishhstress/stress.go (about) 1 /* 2 MIT License 3 4 Copyright (c) 2024 Vish Kannan 5 6 Permission is hereby granted, free of charge, to any person obtaining a copy 7 of this software and associated documentation files (the "Software"), to deal 8 in the Software without restriction, including without limitation the rights 9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 copies of the Software, and to permit persons to whom the Software is 11 furnished to do so, subject to the following conditions: 12 13 The above copyright notice and this permission notice shall be included in all 14 copies or substantial portions of the Software. 15 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 SOFTWARE. 23 */ 24 25 // Package vishhstress was forked from <https://github.com/vishh/stress/tree/eab4e3384bcad9899b8b801b4a1917a758e97d96> 26 // so that it can be consumed from agnhost. 27 package vishhstress 28 29 import ( 30 "io" 31 "io/ioutil" 32 "os" 33 "time" 34 35 "github.com/spf13/cobra" 36 "k8s.io/apimachinery/pkg/api/resource" 37 "k8s.io/klog/v2" 38 ) 39 40 var ( 41 argMemTotal string 42 argMemStepSize string 43 argMemSleepDuration time.Duration 44 argCpus int 45 buffer [][]byte 46 ) 47 48 // CmdStress is used by agnhost Cobra. 49 var CmdStress = &cobra.Command{ 50 Use: "stress", 51 Short: "Lightweight compute resource stress utlity", 52 Args: cobra.NoArgs, 53 Run: main, 54 } 55 56 func init() { 57 flags := CmdStress.Flags() 58 flags.StringVar(&argMemTotal, "mem-total", "0", "total memory to be consumed. Memory will be consumed via multiple allocations.") 59 flags.StringVar(&argMemStepSize, "mem-alloc-size", "4Ki", "amount of memory to be consumed in each allocation") 60 flags.DurationVar(&argMemSleepDuration, "mem-alloc-sleep", time.Millisecond, "duration to sleep between allocations") 61 flags.IntVar(&argCpus, "cpus", 0, "total number of CPUs to utilize") 62 } 63 64 func main(cmd *cobra.Command, _ []string) { 65 total := resource.MustParse(argMemTotal) 66 stepSize := resource.MustParse(argMemStepSize) 67 klog.Infof("Allocating %q memory, in %q chunks, with a %v sleep between allocations", total.String(), stepSize.String(), argMemSleepDuration) 68 burnCPU() 69 allocateMemory(total, stepSize) 70 klog.Infof("Allocated %q memory", total.String()) 71 select {} 72 } 73 74 func burnCPU() { 75 src, err := os.Open("/dev/zero") 76 if err != nil { 77 klog.Fatalf("failed to open /dev/zero") 78 } 79 for i := 0; i < argCpus; i++ { 80 klog.Infof("Spawning a thread to consume CPU") 81 go func() { 82 _, err := io.Copy(ioutil.Discard, src) 83 if err != nil { 84 klog.Fatalf("failed to copy from /dev/zero to /dev/null: %v", err) 85 } 86 }() 87 } 88 } 89 90 func allocateMemory(total, stepSize resource.Quantity) { 91 for i := int64(1); i*stepSize.Value() <= total.Value(); i++ { 92 newBuffer := make([]byte, stepSize.Value()) 93 for i := range newBuffer { 94 newBuffer[i] = 0 95 } 96 buffer = append(buffer, newBuffer) 97 time.Sleep(argMemSleepDuration) 98 } 99 }