get-kubemark-resource-usage.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package framework
  14. import (
  15. "bufio"
  16. "fmt"
  17. "strings"
  18. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  19. e2essh "k8s.io/kubernetes/test/e2e/framework/ssh"
  20. )
  21. // KubemarkResourceUsage is a struct for tracking the resource usage of kubemark.
  22. type KubemarkResourceUsage struct {
  23. Name string
  24. MemoryWorkingSetInBytes uint64
  25. CPUUsageInCores float64
  26. }
  27. func getMasterUsageByPrefix(prefix string) (string, error) {
  28. sshResult, err := e2essh.SSH(fmt.Sprintf("ps ax -o %%cpu,rss,command | tail -n +2 | grep %v | sed 's/\\s+/ /g'", prefix), GetMasterHost()+":22", TestContext.Provider)
  29. if err != nil {
  30. return "", err
  31. }
  32. return sshResult.Stdout, nil
  33. }
  34. // GetKubemarkMasterComponentsResourceUsage returns the resource usage of kubemark which contains multiple combinations of cpu and memory usage for each pod name.
  35. // TODO: figure out how to move this to kubemark directory (need to factor test SSH out of e2e framework)
  36. func GetKubemarkMasterComponentsResourceUsage() map[string]*KubemarkResourceUsage {
  37. result := make(map[string]*KubemarkResourceUsage)
  38. // Get kubernetes component resource usage
  39. sshResult, err := getMasterUsageByPrefix("kube")
  40. if err != nil {
  41. e2elog.Logf("Error when trying to SSH to master machine. Skipping probe. %v", err)
  42. return nil
  43. }
  44. scanner := bufio.NewScanner(strings.NewReader(sshResult))
  45. for scanner.Scan() {
  46. var cpu float64
  47. var mem uint64
  48. var name string
  49. fmt.Sscanf(strings.TrimSpace(scanner.Text()), "%f %d /usr/local/bin/kube-%s", &cpu, &mem, &name)
  50. if name != "" {
  51. // Gatherer expects pod_name/container_name format
  52. fullName := name + "/" + name
  53. result[fullName] = &KubemarkResourceUsage{Name: fullName, MemoryWorkingSetInBytes: mem * 1024, CPUUsageInCores: cpu / 100}
  54. }
  55. }
  56. // Get etcd resource usage
  57. sshResult, err = getMasterUsageByPrefix("bin/etcd")
  58. if err != nil {
  59. e2elog.Logf("Error when trying to SSH to master machine. Skipping probe")
  60. return nil
  61. }
  62. scanner = bufio.NewScanner(strings.NewReader(sshResult))
  63. for scanner.Scan() {
  64. var cpu float64
  65. var mem uint64
  66. var etcdKind string
  67. fmt.Sscanf(strings.TrimSpace(scanner.Text()), "%f %d /bin/sh -c /usr/local/bin/etcd", &cpu, &mem)
  68. dataDirStart := strings.Index(scanner.Text(), "--data-dir")
  69. if dataDirStart < 0 {
  70. continue
  71. }
  72. fmt.Sscanf(scanner.Text()[dataDirStart:], "--data-dir=/var/%s", &etcdKind)
  73. if etcdKind != "" {
  74. // Gatherer expects pod_name/container_name format
  75. fullName := "etcd/" + etcdKind
  76. result[fullName] = &KubemarkResourceUsage{Name: fullName, MemoryWorkingSetInBytes: mem * 1024, CPUUsageInCores: cpu / 100}
  77. }
  78. }
  79. return result
  80. }