Skip to content

Commit

Permalink
Linux support
Browse files Browse the repository at this point in the history
  • Loading branch information
koron committed Mar 15, 2020
1 parent 741dc69 commit 3fb65b7
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
37 changes: 37 additions & 0 deletions internal/procstatm/procstatm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package procstatm

import (
"fmt"
"os"
)

// Statm provides statistics of memory.
type Statm struct {
Size uint
Resident uint
Share uint
Text uint
Lib uint
Data uint
Darty uint
}

var zero = Statm{}

// Get gets Statm for process (pid).
// pid should be process id (integer) or string ("self" or so).
func Get(pid interface{}) (Statm, error) {
name := fmt.Sprintf("/proc/%s/statm", pid)
f, err := os.Open(name)
if err != nil {
return zero, err
}
defer f.Close()
m := Statm{}
_, err = fmt.Fscanf(f, "%d %d %d %d %d %d %d",
&m.Size, &m.Resident, &m.Share, &m.Text, &m.Lib, &m.Data, &m.Darty)
if err != nil {
return zero, fmt.Errorf("failed to scan %s: %w", name, err)
}
return m, nil
}
1 change: 1 addition & 0 deletions phymem.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// +build !windows
// +build !linux

package phymem

Expand Down
15 changes: 15 additions & 0 deletions phymem_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package phymem

import "github.com/koron-go/phymem/internal/procstatm"

// for test.
const providedCurrent = true

// Current get physical memory which used by current process.
func Current() (uint, error) {
m, err := procstatm.Get("self")
if err != nil {
return 0, err
}
return m.Resident * 4096, nil
}

0 comments on commit 3fb65b7

Please sign in to comment.