memsize_linux.go 667 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. "os"
  10. "strconv"
  11. "strings"
  12. )
  13. func memorySize() int64 {
  14. f, err := os.Open("/proc/meminfo")
  15. if err != nil {
  16. return 0
  17. }
  18. defer f.Close()
  19. s := bufio.NewScanner(f)
  20. if !s.Scan() {
  21. return 0
  22. }
  23. l := s.Text()
  24. fs := strings.Fields(l)
  25. if len(fs) != 3 || fs[2] != "kB" {
  26. return 0
  27. }
  28. kb, err := strconv.ParseInt(fs[1], 10, 64)
  29. if err != nil {
  30. return 0
  31. }
  32. return kb * 1024
  33. }