1
0
Fork 0

Remove sprintf where it's not required

This commit is contained in:
Gregory Eremin 2014-07-06 20:49:20 +07:00
parent 07579de372
commit 6d8e80e5c2
5 changed files with 10 additions and 13 deletions

View File

@ -26,15 +26,14 @@ type ProcLoadavg struct {
} }
func NewProcLoadavg() ProcLoadavg { func NewProcLoadavg() ProcLoadavg {
file := fmt.Sprintf("/proc/loadavg") b, err := ioutil.ReadFile("/proc/loadavg")
bytes, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
panic(err) panic(err)
} }
p := ProcLoadavg{} p := ProcLoadavg{}
parsed, err := fmt.Sscanf(string(bytes), "%f %f %f %d/%d %d", parsed, err := fmt.Sscanf(string(b), "%f %f %f %d/%d %d",
&p.Avg1Min, &p.Avg5Min, &p.Avg15Min, &p.RunnableEntities, &p.Avg1Min, &p.Avg5Min, &p.Avg15Min, &p.RunnableEntities,
&p.TotalEntities, &p.LastPid) &p.TotalEntities, &p.LastPid)
if parsed < 6 { if parsed < 6 {

View File

@ -212,14 +212,14 @@ type ProcPidStat struct {
func NewProcPidStat(pid int) ProcPidStat { func NewProcPidStat(pid int) ProcPidStat {
file := fmt.Sprintf("/proc/%d/stat", pid) file := fmt.Sprintf("/proc/%d/stat", pid)
bytes, err := ioutil.ReadFile(file) b, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
panic(err) panic(err)
} }
p := ProcPidStat{} p := ProcPidStat{}
parsed, err := fmt.Sscanf(string(bytes), parsed, err := fmt.Sscanf(string(b),
"%d %s %s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d "+ "%d %s %s %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d "+
"%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d", "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
&p.Pid, &p.Comm, &p.State, &p.Ppid, &p.Pgrp, &p.Session, &p.TtyNr, &p.Pid, &p.Comm, &p.State, &p.Ppid, &p.Pgrp, &p.Session, &p.TtyNr,

View File

@ -19,14 +19,14 @@ type ProcPidStatm struct {
func NewProcPidStatm(pid int) ProcPidStatm { func NewProcPidStatm(pid int) ProcPidStatm {
file := fmt.Sprintf("/proc/%d/statm", pid) file := fmt.Sprintf("/proc/%d/statm", pid)
bytes, err := ioutil.ReadFile(file) b, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
panic(err) panic(err)
} }
p := ProcPidStatm{} p := ProcPidStatm{}
parsed, err := fmt.Sscanf(string(bytes), "%d %d %d %d %d %d %d", parsed, err := fmt.Sscanf(string(b), "%d %d %d %d %d %d %d",
&p.Size, &p.Resident, &p.Share, &p.Text, &p.Lib, &p.Data, &p.Dt) &p.Size, &p.Resident, &p.Share, &p.Text, &p.Lib, &p.Data, &p.Dt)
if parsed < 7 { if parsed < 7 {

View File

@ -30,15 +30,14 @@ type ProcStat struct {
} }
func NewProcStat() ProcStat { func NewProcStat() ProcStat {
file := fmt.Sprintf("/proc/stat") b, err := ioutil.ReadFile("/proc/stat")
bytes, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
panic(err) panic(err)
} }
p := ProcStat{} p := ProcStat{}
parsed, err := fmt.Sscanf(string(bytes), "cpu %d %d %d %d %d %d %d %d %d %d", parsed, err := fmt.Sscanf(string(b), "cpu %d %d %d %d %d %d %d %d %d %d",
&p.Cpu.User, &p.Cpu.Nice, &p.Cpu.System, &p.Cpu.Idle, &p.Cpu.Iowait, &p.Cpu.User, &p.Cpu.Nice, &p.Cpu.System, &p.Cpu.Idle, &p.Cpu.Iowait,
&p.Cpu.Irq, &p.Cpu.Softirq, &p.Cpu.Steal, &p.Cpu.Guest, &p.Cpu.Irq, &p.Cpu.Softirq, &p.Cpu.Steal, &p.Cpu.Guest,
&p.Cpu.GuestNice) &p.Cpu.GuestNice)

View File

@ -13,15 +13,14 @@ type ProcUptime struct {
} }
func NewProcUptime() ProcUptime { func NewProcUptime() ProcUptime {
file := fmt.Sprintf("/proc/uptime") b, err := ioutil.ReadFile("/proc/uptime")
bytes, err := ioutil.ReadFile(file)
if err != nil { if err != nil {
panic(err) panic(err)
} }
p := ProcUptime{} p := ProcUptime{}
parsed, err := fmt.Sscanf(string(bytes), "%f %f", &p.Uptime, &p.Idle) parsed, err := fmt.Sscanf(string(b), "%f %f", &p.Uptime, &p.Idle)
if parsed < 2 { if parsed < 2 {
fmt.Println("Managed to parse only", parsed, "fields out of 2") fmt.Println("Managed to parse only", parsed, "fields out of 2")
} }