1 Star 0 Fork 0

橙子/androidutils

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
procinfo.go 1.85 KB
一键复制 编辑 原始数据 按行查看 历史
shengxiang 提交于 2017-12-22 15:08 . add memory and cpuinfo parse
package androidutils
import (
"errors"
"io/ioutil"
"regexp"
"strconv"
"strings"
)
// MemoryInfo read from /proc/meminfo, unit kB
func MemoryInfo() (info map[string]int, err error) {
data, err := ioutil.ReadFile("/proc/meminfo")
if err != nil {
return
}
return parseMemoryInfo(data)
}
func parseMemoryInfo(data []byte) (info map[string]int, err error) {
re := regexp.MustCompile(`([\w_\(\)]+):\s*(\d+) kB`)
matches := re.FindAllStringSubmatch(string(data), -1)
if len(matches) == 0 {
return nil, errors.New("Invalid memory info data")
}
info = make(map[string]int)
for _, m := range matches {
var key = m[1]
val, _ := strconv.Atoi(m[2])
info[key] = val
}
return
}
type Processor struct {
Index int
BogoMIPS string
Features []string
}
// ProcessorInfo read from /proc/cpuinfo
func ProcessorInfo() (hardware string, processors []Processor, err error) {
data, err := ioutil.ReadFile("/proc/cpuinfo")
if err != nil {
return
}
return parseCpuInfo(data)
}
func parseCpuInfo(data []byte) (hardware string, processors []Processor, err error) {
re := regexp.MustCompile(`([\w ]+\w)\s*:\s*(.+)`)
ms := re.FindAllStringSubmatch(string(data), -1)
processors = make([]Processor, 0, 8)
var processor = Processor{Index: -1}
for _, m := range ms {
var key = m[1]
var val = m[2]
if key == "Hardware" { // Hardware occur at last
hardware = val
processors = append(processors, processor)
break
}
if key == "processor" {
idx, _ := strconv.Atoi(val)
if idx != processor.Index && processor.Index != -1 {
processors = append(processors, processor)
}
processor.Index = idx
continue
}
switch key {
case "BogoMIPS":
processor.BogoMIPS = val
case "Features":
processor.Features = strings.Split(val, " ")
default:
// ignore
}
}
if len(processors) == 0 {
err = errors.New("Invalid cpuinfo data")
}
return
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xuchengzhi/androidutils.git
[email protected]:xuchengzhi/androidutils.git
xuchengzhi
androidutils
androidutils
master

搜索帮助