From deba5665d961789a8c43b0f563d68da835018fe0 Mon Sep 17 00:00:00 2001
From: Boming Zhang <bomingzh@sjtu.edu.cn>
Date: Mon, 4 Mar 2024 14:33:44 -0500
Subject: [PATCH] refactor: rename variable & add log

---
 README.md                              | 29 ++++++++++++++------------
 internal/executors/sandbox/executor.go | 17 +++++++--------
 internal/stage/run.go                  | 16 ++++++++------
 3 files changed, 33 insertions(+), 29 deletions(-)

diff --git a/README.md b/README.md
index 1a304db..cffd35c 100644
--- a/README.md
+++ b/README.md
@@ -13,19 +13,22 @@ go build -o ./build/joj3 ./cmd/joj3
 + DIRNAME=./_example/simple
 + cd ./_example/simple
 + ./../../build/joj3
-2024/03/04 02:57:50 INFO stage start name=compile
-2024/03/04 02:57:50 INFO sandbox run cmd="{Args:[/usr/bin/g++ a.cc -o a] Env:[PATH=/usr/bin:/bin] Files:[0xc00007f540 0xc00007f580 0xc00007f5c0] CPULimit:10000000000 RealCPULimit:0 ClockLimit:0 MemoryLimit:104857600 StackLimit:0 ProcLimit:50 CPURateLimit:0 CPUSetLimit: CopyIn:map[] CopyInCached:map[] CopyInCwd:true CopyOut:[stdout stderr] CopyOutCached:[a] CopyOutMax:0 CopyOutDir: TTY:false StrictMemoryLimit:false DataSegmentLimit:false AddressSpaceLimit:false}"
-2024/03/04 02:57:50 INFO sandbox run ret="results:{status:Accepted  time:298002000  runTime:298694146  memory:57880576  files:{key:\"stderr\"  value:\"\"}  files:{key:\"stdout\"  value:\"\"}  fileIDs:{key:\"a\"  value:\"DMTRJR3V\"}}"
-2024/03/04 02:57:50 INFO executor done result="{Status:Accepted ExitStatus:0 Error: Time:298.002ms RunTime:298.694146ms Memory:55.2 MiB Files:map[stderr:len:0 stdout:len:0] FileIDs:map[a:DMTRJR3V] FileError:[]}"
-2024/03/04 02:57:50 INFO parser done result="&{Score:100 Comment:compile done, executor status: run time: 298694146 ns, memory: 57880576 bytes}"
-2024/03/04 02:57:50 INFO stage start name=run
-2024/03/04 02:57:50 INFO sandbox run cmd="{Args:[./a] Env:[PATH=/usr/bin:/bin] Files:[0xc00007f600 0xc00007f640 0xc00007f680] CPULimit:10000000000 RealCPULimit:0 ClockLimit:0 MemoryLimit:104857600 StackLimit:0 ProcLimit:50 CPURateLimit:0 CPUSetLimit: CopyIn:map[] CopyInCached:map[a:a] CopyInCwd:false CopyOut:[stdout stderr] CopyOutCached:[] CopyOutMax:0 CopyOutDir: TTY:false StrictMemoryLimit:false DataSegmentLimit:false AddressSpaceLimit:false}"
-2024/03/04 02:57:50 INFO sandbox run ret="results:{status:Accepted  time:1122000  runTime:1723994  memory:15384576  files:{key:\"stderr\"  value:\"\"}  files:{key:\"stdout\"  value:\"2\\n\"}}"
-2024/03/04 02:57:50 INFO executor done result="{Status:Accepted ExitStatus:0 Error: Time:1.122ms RunTime:1.723994ms Memory:14.7 MiB Files:map[stderr:len:0 stdout:len:2] FileIDs:map[] FileError:[]}"
-2024/03/04 02:57:50 INFO parser done result="&{Score:100 Comment:}"
-2024/03/04 02:57:50 INFO stage result name=compile score=100 comment="compile done, executor status: run time: 298694146 ns, memory: 57880576 bytes"
-2024/03/04 02:57:50 INFO stage result name=run score=100 comment=""
-2024/03/04 02:57:50 INFO sandbox cleanup
+2024/03/04 14:33:27 INFO stage start name=compile
+2024/03/04 14:33:27 INFO executor run start cmd="{Args:[/usr/bin/g++ a.cc -o a] Env:[PATH=/usr/bin:/bin] Files:[0xc000114340 0xc000114380 0xc0001143c0] CPULimit:10000000000 RealCPULimit:0 ClockLimit:0 MemoryLimit:104857600 StackLimit:0 ProcLimit:50 CPURateLimit:0 CPUSetLimit: CopyIn:map[] CopyInCached:map[] CopyInCwd:true CopyOut:[stdout stderr] CopyOutCached:[a] CopyOutMax:0 CopyOutDir: TTY:false StrictMemoryLimit:false DataSegmentLimit:false AddressSpaceLimit:false}"
+2024/03/04 14:33:27 INFO executor run done result="{Status:Accepted ExitStatus:0 Error: Time:355.171ms RunTime:356.069198ms Memory:53.9 MiB Files:map[stderr:len:0 stdout:len:0] FileIDs:map[a:VNQ3A3QC] FileError:[]}"
+2024/03/04 14:33:27 INFO parser run start config="map[comment:compile done score:100]"
+2024/03/04 14:33:27 INFO parser run done result="&{Score:100 Comment:compile done, executor status: run time: 356069198 ns, memory: 56561664 bytes}"
+2024/03/04 14:33:27 INFO stage start name=run
+2024/03/04 14:33:27 INFO executor run start cmd="{Args:[./a] Env:[PATH=/usr/bin:/bin] Files:[0xc000114400 0xc000114440 0xc000114480] CPULimit:10000000000 RealCPULimit:0 ClockLimit:0 MemoryLimit:104857600 StackLimit:0 ProcLimit:50 CPURateLimit:0 CPUSetLimit: CopyIn:map[] CopyInCached:map[a:a] CopyInCwd:false CopyOut:[stdout stderr] CopyOutCached:[] CopyOutMax:0 CopyOutDir: TTY:false StrictMemoryLimit:false DataSegmentLimit:false AddressSpaceLimit:false}"
+2024/03/04 14:33:27 INFO executor run done result="{Status:Accepted ExitStatus:0 Error: Time:1.393ms RunTime:2.2294ms Memory:12.8 MiB Files:map[stderr:len:0 stdout:len:2] FileIDs:map[] FileError:[]}"
+2024/03/04 14:33:27 INFO parser run start config="map[score:100 stdoutPath:1.stdout]"
+2024/03/04 14:33:27 INFO parser run done result="&{Score:100 Comment:}"
+2024/03/04 14:33:27 INFO stage result name=compile score=100 comment="compile done, executor status: run time: 356069198 ns, memory: 56561664 bytes"
+2024/03/04 14:33:27 INFO stage result name=run score=100 comment=""
+2024/03/04 14:33:27 INFO executor cleanup start name=dummy
+2024/03/04 14:33:27 INFO executor cleanup done name=dummy
+2024/03/04 14:33:27 INFO executor cleanup start name=sandbox
+2024/03/04 14:33:27 INFO executor cleanup done name=sandbox
 + cd -
 ```
 
diff --git a/internal/executors/sandbox/executor.go b/internal/executors/sandbox/executor.go
index 66c867f..d93f93f 100644
--- a/internal/executors/sandbox/executor.go
+++ b/internal/executors/sandbox/executor.go
@@ -15,7 +15,6 @@ type Sandbox struct {
 }
 
 func (e *Sandbox) Run(cmd stage.Cmd) (*stage.ExecutorResult, error) {
-	slog.Info("sandbox run", "cmd", cmd)
 	if cmd.CopyIn == nil {
 		cmd.CopyIn = make(map[string]stage.CmdFile)
 	}
@@ -24,24 +23,22 @@ func (e *Sandbox) Run(cmd stage.Cmd) (*stage.ExecutorResult, error) {
 			cmd.CopyIn[k] = stage.CmdFile{FileID: &fileID}
 		}
 	}
-	req := &pb.Request{Cmd: convertPBCmd([]stage.Cmd{cmd})}
-	ret, err := e.execClient.Exec(context.TODO(), req)
+	pbReq := &pb.Request{Cmd: convertPBCmd([]stage.Cmd{cmd})}
+	pbRet, err := e.execClient.Exec(context.TODO(), pbReq)
 	if err != nil {
 		return nil, err
 	}
-	if ret.Error != "" {
-		return nil, fmt.Errorf("compile error: %s", ret.Error)
+	if pbRet.Error != "" {
+		return nil, fmt.Errorf("sandbox execute error: %s", pbRet.Error)
 	}
-	slog.Info("sandbox run", "ret", ret)
-	res := &convertPBResult(ret.Results)[0]
-	for fileName, fileID := range res.FileIDs {
+	executorRes := &convertPBResult(pbRet.Results)[0]
+	for fileName, fileID := range executorRes.FileIDs {
 		e.cachedMap[fileName] = fileID
 	}
-	return res, nil
+	return executorRes, nil
 }
 
 func (e *Sandbox) Cleanup() error {
-	slog.Info("sandbox cleanup")
 	for _, fileID := range e.cachedMap {
 		_, err := e.execClient.FileDelete(context.TODO(), &pb.FileID{
 			FileID: fileID,
diff --git a/internal/stage/run.go b/internal/stage/run.go
index c9a7290..0b81920 100644
--- a/internal/stage/run.go
+++ b/internal/stage/run.go
@@ -33,18 +33,20 @@ func Run(stages []Stage) []StageResult {
 	var parserResults []StageResult
 	for _, stage := range stages {
 		slog.Info("stage start", "name", stage.Name)
+		slog.Info("executor run start", "cmd", stage.ExecutorCmd)
 		executorResult, err := stage.Executor.Run(stage.ExecutorCmd)
 		if err != nil {
-			slog.Error("executor error", "name", stage.ExecutorName, "error", err)
+			slog.Error("executor run error", "name", stage.ExecutorName, "error", err)
 			break
 		}
-		slog.Info("executor done", "result", executorResult)
+		slog.Info("executor run done", "result", executorResult)
+		slog.Info("parser run start", "config", stage.ParserConfig)
 		parserResult, err := stage.Parser.Run(executorResult, stage.ParserConfig)
 		if err != nil {
-			slog.Error("parser error", "name", stage.ExecutorName, "error", err)
+			slog.Error("parser run error", "name", stage.ExecutorName, "error", err)
 			break
 		}
-		slog.Info("parser done", "result", parserResult)
+		slog.Info("parser run done", "result", parserResult)
 		parserResults = append(parserResults, StageResult{
 			Name:         stage.Name,
 			ParserResult: parserResult,
@@ -54,10 +56,12 @@ func Run(stages []Stage) []StageResult {
 }
 
 func Cleanup() {
-	for _, executor := range executorMap {
+	for name, executor := range executorMap {
+		slog.Info("executor cleanup start", "name", name)
 		err := executor.Cleanup()
 		if err != nil {
-			slog.Error("executor cleanup error", "error", err)
+			slog.Error("executor cleanup error", "name", name, "error", err)
 		}
+		slog.Info("executor cleanup done", "name", name)
 	}
 }