Skip to content

Commit 048ffcd

Browse files
committed
feat(crit): add utility functions for stats
This adds the commonly used functions to fetch dump and restore statistics for a checkpoint. Signed-off-by: Prajwal S N <[email protected]>
1 parent 5b7dbf8 commit 048ffcd

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

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+
}

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)