1
0

memsize_linux.go 770 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package ur
  7. import (
  8. "bufio"
  9. "errors"
  10. "os"
  11. "strconv"
  12. "strings"
  13. )
  14. func memorySize() (int64, error) {
  15. f, err := os.Open("/proc/meminfo")
  16. if err != nil {
  17. return 0, err
  18. }
  19. s := bufio.NewScanner(f)
  20. if !s.Scan() {
  21. return 0, errors.New("/proc/meminfo parse error 1")
  22. }
  23. l := s.Text()
  24. fs := strings.Fields(l)
  25. if len(fs) != 3 || fs[2] != "kB" {
  26. return 0, errors.New("/proc/meminfo parse error 2")
  27. }
  28. kb, err := strconv.ParseInt(fs[1], 10, 64)
  29. if err != nil {
  30. return 0, err
  31. }
  32. return kb * 1024, nil
  33. }