memsize_linux.go 650 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. s := bufio.NewScanner(f)
  19. if !s.Scan() {
  20. return 0
  21. }
  22. l := s.Text()
  23. fs := strings.Fields(l)
  24. if len(fs) != 3 || fs[2] != "kB" {
  25. return 0
  26. }
  27. kb, err := strconv.ParseInt(fs[1], 10, 64)
  28. if err != nil {
  29. return 0
  30. }
  31. return kb * 1024
  32. }