Skip to content

Commit 130a18c

Browse files
committed
Move stats related function to stats/
This also adds a function to read restore stats and verifies the magic numbers in the stats files. Signed-off-by: Adrian Reber <[email protected]>
1 parent a8c94b2 commit 130a18c

File tree

3 files changed

+55
-33
lines changed

3 files changed

+55
-33
lines changed

phaul/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func (pc *Client) Migrate() error {
110110
return err
111111
}
112112

113-
st, err := criuGetDumpStats(imgDir)
113+
st, err := stats.CriuGetDumpStats(imgDir)
114114
if err != nil {
115115
return err
116116
}

phaul/stats.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

stats/utils.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package stats
2+
3+
import (
4+
"encoding/binary"
5+
"errors"
6+
"io/ioutil"
7+
"os"
8+
"path/filepath"
9+
10+
"github.com/checkpoint-restore/go-criu/v5/magic"
11+
"google.golang.org/protobuf/proto"
12+
)
13+
14+
func readStatisticsFile(imgDir *os.File, fileName string) (*StatsEntry, error) {
15+
buf, err := ioutil.ReadFile(filepath.Join(imgDir.Name(), fileName))
16+
if err != nil {
17+
return nil, err
18+
}
19+
20+
if binary.LittleEndian.Uint32(buf[magic.PrimaryMagicOffset:magic.SecondaryMagicOffset]) != magic.ImgServiceMagic {
21+
return nil, errors.New("Primary magic not found")
22+
}
23+
24+
if binary.LittleEndian.Uint32(buf[magic.SecondaryMagicOffset:magic.SizeOffset]) != magic.StatsMagic {
25+
return nil, errors.New("Secondary magic not found")
26+
}
27+
28+
payloadSize := binary.LittleEndian.Uint32(buf[magic.SizeOffset:magic.PayloadOffset])
29+
30+
st := &StatsEntry{}
31+
if err := proto.Unmarshal(buf[magic.PayloadOffset:magic.PayloadOffset+payloadSize], st); err != nil {
32+
return nil, err
33+
}
34+
35+
return st, nil
36+
}
37+
38+
func CriuGetDumpStats(imgDir *os.File) (*DumpStatsEntry, error) {
39+
st, err := readStatisticsFile(imgDir, StatsDump)
40+
if err != nil {
41+
return nil, err
42+
}
43+
44+
return st.GetDump(), nil
45+
}
46+
47+
func CriuGetRestoreStats(imgDir *os.File) (*RestoreStatsEntry, error) {
48+
st, err := readStatisticsFile(imgDir, StatsRestore)
49+
if err != nil {
50+
return nil, err
51+
}
52+
53+
return st.GetRestore(), nil
54+
}

0 commit comments

Comments
 (0)