k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/measurement/util/kubemark/kubemark.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 kubemark
    18  
    19  import (
    20  	"bufio"
    21  	"fmt"
    22  	"strings"
    23  
    24  	"k8s.io/klog/v2"
    25  	"k8s.io/perf-tests/clusterloader2/pkg/measurement/util"
    26  	"k8s.io/perf-tests/clusterloader2/pkg/provider"
    27  )
    28  
    29  // ResourceUsage represents resources used by the kubemark.
    30  type ResourceUsage struct {
    31  	Name                    string
    32  	MemoryWorkingSetInBytes uint64
    33  	CPUUsageInCores         float64
    34  }
    35  
    36  func getMasterUsageByPrefix(host string, provider provider.Provider, prefix string) (string, error) {
    37  	sshResult, err := util.SSH(fmt.Sprintf("ps ax -o %%cpu,rss,command | tail -n +2 | grep %v | sed 's/\\s+/ /g'", prefix), host+":22", provider)
    38  	if err != nil {
    39  		return "", err
    40  	}
    41  	return sshResult.Stdout, nil
    42  }
    43  
    44  // GetKubemarkMasterComponentsResourceUsage returns resource usage of the kubemark components.
    45  // TODO: figure out how to move this to kubemark directory (need to factor test SSH out of e2e framework)
    46  func GetKubemarkMasterComponentsResourceUsage(host string, provider provider.Provider) map[string]*ResourceUsage {
    47  	result := make(map[string]*ResourceUsage)
    48  	// Get kubernetes component resource usage
    49  	sshResult, err := getMasterUsageByPrefix(host, provider, "kube")
    50  	if err != nil {
    51  		klog.Errorf("error when trying to SSH to master machine. Skipping probe. %v", err)
    52  		return nil
    53  	}
    54  	scanner := bufio.NewScanner(strings.NewReader(sshResult))
    55  	for scanner.Scan() {
    56  		var cpu float64
    57  		var mem uint64
    58  		var name string
    59  		fmt.Sscanf(strings.TrimSpace(scanner.Text()), "%f %d /usr/local/bin/kube-%s", &cpu, &mem, &name)
    60  		if name != "" {
    61  			// Gatherer expects pod_name/container_name format
    62  			fullName := name + "/" + name
    63  			result[fullName] = &ResourceUsage{Name: fullName, MemoryWorkingSetInBytes: mem * 1024, CPUUsageInCores: cpu / 100}
    64  		}
    65  	}
    66  	// Get etcd resource usage
    67  	sshResult, err = getMasterUsageByPrefix(host, provider, "bin/etcd")
    68  	if err != nil {
    69  		klog.Errorf("error when trying to SSH to master machine. Skipping probe")
    70  		return nil
    71  	}
    72  	scanner = bufio.NewScanner(strings.NewReader(sshResult))
    73  	for scanner.Scan() {
    74  		var cpu float64
    75  		var mem uint64
    76  		var etcdKind string
    77  		fmt.Sscanf(strings.TrimSpace(scanner.Text()), "%f %d /usr/local/bin/etcd", &cpu, &mem)
    78  		dataDirStart := strings.Index(scanner.Text(), "--data-dir")
    79  		if dataDirStart < 0 {
    80  			continue
    81  		}
    82  		fmt.Sscanf(scanner.Text()[dataDirStart:], "--data-dir /var/%s", &etcdKind)
    83  		if etcdKind != "" {
    84  			// Gatherer expects pod_name/container_name format
    85  			fullName := "etcd/" + etcdKind
    86  			result[fullName] = &ResourceUsage{Name: fullName, MemoryWorkingSetInBytes: mem * 1024, CPUUsageInCores: cpu / 100}
    87  		}
    88  	}
    89  	return result
    90  }