utils.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright The containerd 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 runc
  14. import (
  15. "bytes"
  16. "os"
  17. "strconv"
  18. "strings"
  19. "sync"
  20. )
  21. // ReadPidFile reads the pid file at the provided path and returns
  22. // the pid or an error if the read and conversion is unsuccessful
  23. func ReadPidFile(path string) (int, error) {
  24. data, err := os.ReadFile(path)
  25. if err != nil {
  26. return -1, err
  27. }
  28. return strconv.Atoi(string(data))
  29. }
  30. var bytesBufferPool = sync.Pool{
  31. New: func() interface{} {
  32. return bytes.NewBuffer(nil)
  33. },
  34. }
  35. func getBuf() *bytes.Buffer {
  36. return bytesBufferPool.Get().(*bytes.Buffer)
  37. }
  38. func putBuf(b *bytes.Buffer) {
  39. if b == nil {
  40. return
  41. }
  42. b.Reset()
  43. bytesBufferPool.Put(b)
  44. }
  45. // fieldsASCII is similar to strings.Fields but only allows ASCII whitespaces
  46. func fieldsASCII(s string) []string {
  47. fn := func(r rune) bool {
  48. switch r {
  49. case '\t', '\n', '\f', '\r', ' ':
  50. return true
  51. }
  52. return false
  53. }
  54. return strings.FieldsFunc(s, fn)
  55. }
  56. // ParsePSOutput parses the runtime's ps raw output and returns a TopResults
  57. func ParsePSOutput(output []byte) (*TopResults, error) {
  58. topResults := &TopResults{}
  59. lines := strings.Split(string(output), "\n")
  60. topResults.Headers = fieldsASCII(lines[0])
  61. pidIndex := -1
  62. for i, name := range topResults.Headers {
  63. if name == "PID" {
  64. pidIndex = i
  65. }
  66. }
  67. for _, line := range lines[1:] {
  68. if len(line) == 0 {
  69. continue
  70. }
  71. fields := fieldsASCII(line)
  72. if fields[pidIndex] == "-" {
  73. continue
  74. }
  75. process := fields[:len(topResults.Headers)-1]
  76. process = append(process, strings.Join(fields[len(topResults.Headers)-1:], " "))
  77. topResults.Processes = append(topResults.Processes, process)
  78. }
  79. return topResults, nil
  80. }