Skip to content

Commit df3662d

Browse files
authored
Merge pull request #57 from adrianreber/2021-10-29-stats-functions
Move statistics read funtion to stats/
2 parents 636cac7 + 130a18c commit df3662d

File tree

6 files changed

+73
-34
lines changed

6 files changed

+73
-34
lines changed

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ phaul-test: $(TEST_BINARIES)
4040
clean:
4141
@rm -f $(TEST_BINARIES)
4242
@rm -rf image
43-
@rm -f rpc/rpc.proto stats/stats.proto
4443

4544
rpc/rpc.proto:
4645
curl -sSL https://raw.githubusercontent.com/checkpoint-restore/criu/master/images/rpc.proto -o $@

magic/types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package magic
2+
3+
const (
4+
ImgCommonMagic = 0x54564319 /* Sarov (a.k.a. Arzamas-16) */
5+
ImgServiceMagic = 0x55105940 /* Zlatoust */
6+
StatsMagic = 0x57093306 /* Ostashkov */
7+
8+
PrimaryMagicOffset = 0x0
9+
SecondaryMagicOffset = 0x4
10+
SizeOffset = 0x8
11+
PayloadOffset = 0xC
12+
)

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/types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package stats
2+
3+
const (
4+
StatsDump = "stats-dump"
5+
StatsRestore = "stats-restore"
6+
)

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)