-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// +build !windows | ||
// +build !linux | ||
|
||
package phymem | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |