Skip to content

Commit baacf56

Browse files
authored
Merge pull request #89 from snprajwal/crit-stats
Add functions for stats
2 parents 287ed08 + 048ffcd commit baacf56

File tree

5 files changed

+97
-17
lines changed

5 files changed

+97
-17
lines changed

crit/explore.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package crit
22

33
import (
44
"fmt"
5+
"path/filepath"
56
"strconv"
67

78
"github.com/checkpoint-restore/go-criu/v6/crit/images"
@@ -20,7 +21,7 @@ type PsTree struct {
2021

2122
// ExplorePs constructs the process tree and returns the root process
2223
func (c *crit) ExplorePs() (*PsTree, error) {
23-
psTreeImg, err := getImg(fmt.Sprintf("%s/pstree.img", c.inputDirPath))
24+
psTreeImg, err := getImg(filepath.Join(c.inputDirPath, "pstree.img"))
2425
if err != nil {
2526
return nil, err
2627
}
@@ -31,7 +32,7 @@ func (c *crit) ExplorePs() (*PsTree, error) {
3132
process := entry.Message.(*images.PstreeEntry)
3233
pId := process.GetPid()
3334

34-
coreImg, err := getImg(fmt.Sprintf("%s/core-%d.img", c.inputDirPath, pId))
35+
coreImg, err := getImg(filepath.Join(c.inputDirPath, fmt.Sprintf("core-%d.img", pId)))
3536
if err != nil {
3637
return nil, err
3738
}
@@ -77,7 +78,7 @@ type File struct {
7778
// ExploreFds searches the process tree for open files
7879
// and returns a list of PIDs with the corresponding files
7980
func (c *crit) ExploreFds() ([]*Fd, error) {
80-
psTreeImg, err := getImg(fmt.Sprintf("%s/pstree.img", c.inputDirPath))
81+
psTreeImg, err := getImg(filepath.Join(c.inputDirPath, "pstree.img"))
8182
if err != nil {
8283
return nil, err
8384
}
@@ -87,13 +88,13 @@ func (c *crit) ExploreFds() ([]*Fd, error) {
8788
process := entry.Message.(*images.PstreeEntry)
8889
pId := process.GetPid()
8990
// Get file with object IDs
90-
idsImg, err := getImg(fmt.Sprintf("%s/ids-%d.img", c.inputDirPath, pId))
91+
idsImg, err := getImg(filepath.Join(c.inputDirPath, fmt.Sprintf("ids-%d.img", pId)))
9192
if err != nil {
9293
return nil, err
9394
}
9495
filesId := idsImg.Entries[0].Message.(*images.TaskKobjIdsEntry).GetFilesId()
9596
// Get open file descriptors
96-
fdInfoImg, err := getImg(fmt.Sprintf("%s/fdinfo-%d.img", c.inputDirPath, filesId))
97+
fdInfoImg, err := getImg(filepath.Join(c.inputDirPath, fmt.Sprintf("fdinfo-%d.img", filesId)))
9798
if err != nil {
9899
return nil, err
99100
}
@@ -113,7 +114,7 @@ func (c *crit) ExploreFds() ([]*Fd, error) {
113114
fdEntry.Files = append(fdEntry.Files, &file)
114115
}
115116
// Get chroot and chdir info
116-
fsImg, err := getImg(fmt.Sprintf("%s/fs-%d.img", c.inputDirPath, pId))
117+
fsImg, err := getImg(filepath.Join(c.inputDirPath, fmt.Sprintf("fs-%d.img", pId)))
117118
if err != nil {
118119
return nil, err
119120
}
@@ -161,7 +162,7 @@ type Mem struct {
161162
// ExploreMems traverses the process tree and returns a
162163
// list of processes with the corresponding memory mapping
163164
func (c *crit) ExploreMems() ([]*MemMap, error) {
164-
psTreeImg, err := getImg(fmt.Sprintf("%s/pstree.img", c.inputDirPath))
165+
psTreeImg, err := getImg(filepath.Join(c.inputDirPath, "pstree.img"))
165166
if err != nil {
166167
return nil, err
167168
}
@@ -181,7 +182,7 @@ func (c *crit) ExploreMems() ([]*MemMap, error) {
181182
process := entry.Message.(*images.PstreeEntry)
182183
pId := process.GetPid()
183184
// Get memory mappings
184-
mmImg, err := getImg(fmt.Sprintf("%s/mm-%d.img", c.inputDirPath, pId))
185+
mmImg, err := getImg(filepath.Join(c.inputDirPath, fmt.Sprintf("mm-%d.img", pId)))
185186
if err != nil {
186187
return nil, err
187188
}
@@ -288,7 +289,7 @@ type Vma struct {
288289
// ExploreRss traverses the process tree and returns
289290
// a list of processes with their RSS mappings
290291
func (c *crit) ExploreRss() ([]*RssMap, error) {
291-
psTreeImg, err := getImg(fmt.Sprintf("%s/pstree.img", c.inputDirPath))
292+
psTreeImg, err := getImg(filepath.Join(c.inputDirPath, "pstree.img"))
292293
if err != nil {
293294
return nil, err
294295
}
@@ -298,13 +299,13 @@ func (c *crit) ExploreRss() ([]*RssMap, error) {
298299
process := entry.Message.(*images.PstreeEntry)
299300
pId := process.GetPid()
300301
// Get virtual memory addresses
301-
mmImg, err := getImg(fmt.Sprintf("%s/mm-%d.img", c.inputDirPath, pId))
302+
mmImg, err := getImg(filepath.Join(c.inputDirPath, fmt.Sprintf("mm-%d.img", pId)))
302303
if err != nil {
303304
return nil, err
304305
}
305306
vmas := mmImg.Entries[0].Message.(*images.MmEntry).GetVmas()
306307
// Get physical memory addresses
307-
pagemapImg, err := getImg(fmt.Sprintf("%s/pagemap-%d.img", c.inputDirPath, pId))
308+
pagemapImg, err := getImg(filepath.Join(c.inputDirPath, fmt.Sprintf("pagemap-%d.img", pId)))
308309
if err != nil {
309310
return nil, err
310311
}

crit/stats.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package crit
2+
3+
import (
4+
"errors"
5+
"path/filepath"
6+
7+
"github.com/checkpoint-restore/go-criu/v6/crit/images"
8+
)
9+
10+
// Helper function to load stats file into Go struct
11+
func getStats(path string) (*images.StatsEntry, error) {
12+
c := New(path, "", "", false, false)
13+
statsImg, err := c.Decode()
14+
if err != nil {
15+
return nil, err
16+
}
17+
18+
stats, ok := statsImg.Entries[0].Message.(*images.StatsEntry)
19+
if !ok {
20+
return nil, errors.New("Failed to type assert stats image")
21+
}
22+
23+
return stats, nil
24+
}
25+
26+
// GetDumpStats returns the dump statistics of a checkpoint.
27+
// dir is the path to the directory with the checkpoint images.
28+
func GetDumpStats(dir string) (*images.DumpStatsEntry, error) {
29+
stats, err := getStats(filepath.Join(dir, "stats-dump"))
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
return stats.GetDump(), nil
35+
}
36+
37+
// GetRestoreStats returns the restore statistics of a checkpoint.
38+
// dir is the path to the directory with the checkpoint images.
39+
func GetRestoreStats(dir string) (*images.RestoreStatsEntry, error) {
40+
stats, err := getStats(filepath.Join(dir, "stats-restore"))
41+
if err != nil {
42+
return nil, err
43+
}
44+
45+
return stats.GetRestore(), nil
46+
}

crit/utils.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"path/filepath"
910
"strconv"
1011

1112
"github.com/checkpoint-restore/go-criu/v6/crit/images"
@@ -112,7 +113,7 @@ func getFilePath(dir string, fId uint32, fType images.FdTypes) (string, error) {
112113
var err error
113114
// Get open files
114115
if filesImg == nil {
115-
filesImg, err = getImg(fmt.Sprintf("%s/files.img", dir))
116+
filesImg, err = getImg(filepath.Join(dir, "files.img"))
116117
if err != nil {
117118
return "", err
118119
}
@@ -152,7 +153,7 @@ func getRegFilePath(dir string, file *images.FileEntry, fId uint32) (string, err
152153
}
153154

154155
if regImg == nil {
155-
regImg, err = getImg(fmt.Sprintf("%s/reg-files.img", dir))
156+
regImg, err = getImg(filepath.Join(dir, "reg-files.img"))
156157
if err != nil {
157158
return "", err
158159
}
@@ -178,7 +179,7 @@ func getPipeFilePath(dir string, file *images.FileEntry, fId uint32) (string, er
178179
}
179180

180181
if pipeImg == nil {
181-
pipeImg, err = getImg(fmt.Sprintf("%s/pipes.img", dir))
182+
pipeImg, err = getImg(filepath.Join(dir, "pipes.img"))
182183
if err != nil {
183184
return "", err
184185
}
@@ -209,7 +210,7 @@ func getUnixSkFilePath(dir string, file *images.FileEntry, fId uint32) (string,
209210
}
210211

211212
if unixSkImg == nil {
212-
unixSkImg, err = getImg(fmt.Sprintf("%s/unixsk.img", dir))
213+
unixSkImg, err = getImg(filepath.Join(dir, "unixsk.img"))
213214
if err != nil {
214215
return "", err
215216
}

test/crit/Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ CC ?= gcc
22
GO ?= go
33
CRIU ?= criu
44

5-
all: integration-test e2e-test clean
5+
all: unit-test integration-test e2e-test clean
6+
7+
unit-test: test-imgs
8+
go test -v ./...
69

710
integration-test: test-imgs crit-test
811
@echo "Running integration test"
@@ -16,6 +19,8 @@ test-imgs: ../loop/loop
1619
$(eval PID := $(shell ../loop/loop))
1720
mkdir -p $@
1821
$(CRIU) dump -v4 -o dump.log -D $@ -t $(PID)
22+
$(CRIU) restore -v4 -o restore.log -D $@ -d
23+
pkill -9 loop
1924

2025
../../crit/bin/crit:
2126
$(MAKE) -C ../../crit bin/crit
@@ -29,4 +34,4 @@ crit-test: main.go
2934
clean:
3035
@rm -rf test-imgs
3136

32-
.PHONY: all test integration-test e2e-test clean
37+
.PHONY: all test unit-test integration-test e2e-test clean

test/crit/stats_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/checkpoint-restore/go-criu/v6/crit"
7+
)
8+
9+
func TestGetDumpStats(t *testing.T) {
10+
dumpStats, err := crit.GetDumpStats("test-imgs")
11+
if err != nil {
12+
t.Error("Failed to get stats")
13+
}
14+
if dumpStats.GetPagesWritten() == 0 {
15+
t.Error("PagesWritten is 0")
16+
}
17+
}
18+
19+
func TestGetRestoreStats(t *testing.T) {
20+
restoreStats, err := crit.GetRestoreStats("test-imgs")
21+
if err != nil {
22+
t.Error("Failed to get stats")
23+
}
24+
if restoreStats.GetForkingTime() == 0 {
25+
t.Error("ForkingTime is 0")
26+
}
27+
}

0 commit comments

Comments
 (0)