k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/util/gatherers/resource_gather_worker.go (about) 1 /* 2 Copyright 2018 The Kubernetes Authors. 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 package gatherers 18 19 import ( 20 "sync" 21 "time" 22 23 utilruntime "k8s.io/apimachinery/pkg/util/runtime" 24 clientset "k8s.io/client-go/kubernetes" 25 "k8s.io/klog/v2" 26 "k8s.io/perf-tests/clusterloader2/pkg/measurement/util" 27 "k8s.io/perf-tests/clusterloader2/pkg/measurement/util/kubelet" 28 "k8s.io/perf-tests/clusterloader2/pkg/measurement/util/kubemark" 29 "k8s.io/perf-tests/clusterloader2/pkg/provider" 30 ) 31 32 type resourceGatherWorker struct { 33 c clientset.Interface 34 nodeName string 35 wg *sync.WaitGroup 36 containerIDs []string 37 stopCh chan struct{} 38 dataSeries []util.ResourceUsagePerContainer 39 finished bool 40 inKubemark bool 41 resourceDataGatheringPeriod time.Duration 42 host string 43 port int 44 provider provider.Provider 45 } 46 47 func (w *resourceGatherWorker) singleProbe() { 48 data := make(util.ResourceUsagePerContainer) 49 if w.inKubemark { 50 kubemarkData := kubemark.GetKubemarkMasterComponentsResourceUsage(w.host, w.provider) 51 if data == nil { 52 return 53 } 54 for k, v := range kubemarkData { 55 data[k] = &util.ContainerResourceUsage{ 56 Name: v.Name, 57 MemoryWorkingSetInBytes: v.MemoryWorkingSetInBytes, 58 CPUUsageInCores: v.CPUUsageInCores, 59 } 60 } 61 } else { 62 nodeUsage, err := kubelet.GetOneTimeResourceUsageOnNode(w.c, w.nodeName, w.port, func() []string { return w.containerIDs }) 63 if err != nil { 64 klog.Errorf("error while reading data from %v: %v", w.nodeName, err) 65 return 66 } 67 for k, v := range nodeUsage { 68 data[k] = v 69 klog.V(3).Infof("Get container %v usage on node %v. CPUUsageInCores: %v, MemoryUsageInBytes: %v, MemoryWorkingSetInBytes: %v", k, w.nodeName, v.CPUUsageInCores, v.MemoryUsageInBytes, v.MemoryWorkingSetInBytes) 70 } 71 } 72 w.dataSeries = append(w.dataSeries, data) 73 } 74 75 func (w *resourceGatherWorker) gather(initialSleep time.Duration) { 76 defer utilruntime.HandleCrash() 77 defer w.wg.Done() 78 defer klog.V(2).Infof("Closing worker for %v", w.nodeName) 79 defer func() { w.finished = true }() 80 select { 81 case <-time.After(initialSleep): 82 w.singleProbe() 83 for { 84 select { 85 case <-time.After(w.resourceDataGatheringPeriod): 86 w.singleProbe() 87 case <-w.stopCh: 88 return 89 } 90 } 91 case <-w.stopCh: 92 return 93 } 94 }