summaryrefslogtreecommitdiffstats
path: root/lib/ur/memsize_linux.go
blob: 3ea8d066a7c8d8a809832b0d4755957a4560c6dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Copyright (C) 2014 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.

package ur

import (
	"bufio"
	"os"
	"strconv"
	"strings"
)

func memorySize() int64 {
	f, err := os.Open("/proc/meminfo")
	if err != nil {
		return 0
	}
	defer f.Close()

	s := bufio.NewScanner(f)
	if !s.Scan() {
		return 0
	}

	l := s.Text()
	fs := strings.Fields(l)
	if len(fs) != 3 || fs[2] != "kB" {
		return 0
	}

	kb, err := strconv.ParseInt(fs[1], 10, 64)
	if err != nil {
		return 0
	}
	return kb * 1024
}