forked from tobert/pcstat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcstatus.go
88 lines (75 loc) · 2.68 KB
/
pcstatus.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package pcstat
/*
* Copyright 2014 Albert P. Tobey <atobey@datastax.com> @AlTobey
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import (
"errors"
"fmt"
"os"
"time"
)
// page cache status
// Bytes: size of the file (from os.File.Stat())
// Pages: array of booleans: true if cached, false otherwise
type PcStatus struct {
Name string `json:"filename"` // file name as specified on command line
Size int64 `json:"size"` // file size in bytes
Timestamp time.Time `json:"timestamp"` // time right before calling mincore
Mtime time.Time `json:"mtime"` // last modification time of the file
Pages int `json:"pages"` // total memory pages
Cached int `json:"cached"` // number of pages that are cached
Uncached int `json:"uncached"` // number of pages that are not cached
Percent float64 `json:"percent"` // percentage of pages cached
PPStat []bool `json:"status"` // per-page status, true if cached, false otherwise
}
func GetPcStatus(fname string) (PcStatus, error) {
pcs := PcStatus{Name: fname}
f, err := os.Open(fname)
if err != nil {
return pcs, fmt.Errorf("could not open file for read: %v", err)
}
defer f.Close()
// TEST TODO: verify behavior when the file size is changing quickly
// while this function is running. I assume that the size parameter to
// mincore will prevent overruns of the output vector, but it's not clear
// what will be in there when the file is truncated between here and the
// mincore() call.
fi, err := f.Stat()
if err != nil {
return pcs, fmt.Errorf("could not stat file: %v", err)
}
if fi.IsDir() {
return pcs, errors.New("file is a directory")
}
pcs.Size = fi.Size()
pcs.Timestamp = time.Now()
pcs.Mtime = fi.ModTime()
pcs.PPStat, err = FileMincore(f, fi.Size())
if err != nil {
return pcs, err
}
// count the number of cached pages
for _, b := range pcs.PPStat {
if b {
pcs.Cached++
}
}
pcs.Pages = len(pcs.PPStat)
pcs.Uncached = pcs.Pages - pcs.Cached
// convert to float for the occasional sparsely-cached file
// see the README.md for how to produce one
pcs.Percent = (float64(pcs.Cached) / float64(pcs.Pages)) * 100.00
return pcs, nil
}