Skip to content

Commit

Permalink
Merge pull request #113 from myuon/memory
Browse files Browse the repository at this point in the history
  • Loading branch information
myuon authored Sep 18, 2023
2 parents 5a5cf0b + 62302a1 commit fb29209
Show file tree
Hide file tree
Showing 13 changed files with 714 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ quartz-debugger.json
# already existing elements were commented out

#/target

*.ir
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ build_current_compiler:
run file options="":
@just build_current_compiler
MODE=run-wat WAT_FILE=./build/quartz-current.wat cargo run --release -- compile {{options}} -o ./build/quartz-compiled.wat {{file}}
MODE=run-wat WAT_FILE=./build/quartz-compiled.wat cargo run --release
MEMORY_DUMP_FILE=./build/memory/memory.bin MODE=run-wat WAT_FILE=./build/quartz-compiled.wat cargo run --release

test file options="":
@just build_current_compiler
Expand Down
3 changes: 3 additions & 0 deletions memorydump/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/myuon/quartz/memorydump

go 1.21.1
198 changes: 198 additions & 0 deletions memorydump/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package main

import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"sync"
)

func appendByteAsHex(bs []byte, b byte) []byte {
if b < 16 {
bs = append(bs, '0')
} else {
h := b >> 4
if h < 10 {
bs = append(bs, '0'+h)
} else {
bs = append(bs, 'a'+h-10)
}
}

h2 := b & 0x0f
if h2 < 10 {
bs = append(bs, '0'+h2)
} else {
bs = append(bs, 'a'+h2-10)
}

return bs
}

func hexdump(data []byte, offset int64, writer io.Writer) {
skip := false

dataOffset := 0
unitSize := 16 * 1000
for dataOffset < len(data) {
unit := data[dataOffset:min(dataOffset+unitSize, len(data))]

for i := 0; i < len(unit)/16; i += 1 {
unitIndex := i * 16
chunk := unit[unitIndex:min(unitIndex+16, len(unit))]
hexVals := make([]byte, 0, 48)
asciiVals := make([]byte, 0, 16)
nonZeroFlag := false
for _, b := range chunk {
hexVals = appendByteAsHex(hexVals, b)
hexVals = append(hexVals, ' ')

if 32 <= b && b <= 126 {
asciiVals = append(asciiVals, b)
} else {
asciiVals = append(asciiVals, '.')
}

if b != 0 {
nonZeroFlag = true
}
}

address := int64(unitIndex) + int64(dataOffset) + offset

if nonZeroFlag {
if skip {
fmt.Fprintf(writer, "%08x 00\n", address)
}

fmt.Fprintf(writer, "%08x %s |%s|\n", address, string(hexVals), string(asciiVals))
skip = false
} else {
if !skip {
fmt.Fprintf(writer, "%08x 00\n", address)
fmt.Fprint(writer, "...\n")

skip = true
}
}
}

dataOffset += unitSize
}

if skip {
fmt.Fprintf(writer, "%08x 00\n", int64(dataOffset)+offset)
}
}

func deletePreviousFiles(pattern string) {
files, err := filepath.Glob(pattern)
if err != nil {
panic(err)
}

for _, f := range files {
err := os.Remove(f)
if err != nil {
panic(err)
}
fmt.Printf("Deleted previous file: %s\n", f)
}
}

func main() {
// ファイルパスとチャンクサイズを指定
filePath := "./build/memory/memory.bin"
chunkSize := int64(250 * 1024 * 1024)
ext := filepath.Ext(filePath)
baseName := strings.Replace(filepath.Base(filePath), ext, "", 1)
dirPath := filepath.Dir(filePath)

// 前回作成したファイルを削除
deletePreviousFiles(filepath.Join(dirPath, fmt.Sprintf("%s_chunk_*", baseName)))

// ファイルのサイズを取得
fileInfo, err := os.Stat(filePath)
if err != nil {
panic(err)
}
fileSize := fileInfo.Size()

// ファイルを開く
file, err := os.Open(filePath)
if err != nil {
panic(err)
}
defer file.Close()

chunkFilePaths := []string{}

// チャンクごとに処理
for i := int64(0); i < fileSize; i += int64(chunkSize) {
fmt.Printf("Processing chunk starting at byte: %d\n", i)

chunkData := make([]byte, chunkSize)
n, err := file.Read(chunkData)
if err != nil && err != io.EOF {
panic(err)
}
chunkData = chunkData[:n]

chunkFilePath := filepath.Join(dirPath, fmt.Sprintf("%s_chunk_%d%s", baseName, i/int64(chunkSize), ext))
chunkFilePaths = append(chunkFilePaths, chunkFilePath)

chunkFile, err := os.Create(chunkFilePath)
if err != nil {
panic(err)
}

_, err = chunkFile.Write(chunkData)
if err != nil {
panic(err)
}

chunkFile.Close()
fmt.Printf("Created chunk file: %s\n", chunkFilePath)
}

wg := sync.WaitGroup{}
for index, chunkFilePath := range chunkFilePaths {
wg.Add(1)
go func(index int, chunkFilePath string) {
defer wg.Done()

fmt.Printf("Generating hexdump for file: %s\n", chunkFilePath)

chunkFile, err := os.Open(chunkFilePath)
if err != nil {
panic(err)
}
defer chunkFile.Close()

chunkData := make([]byte, chunkSize)
n, err := chunkFile.Read(chunkData)
if err != nil && err != io.EOF {
panic(err)
}
chunkData = chunkData[:n]

hexdumpFilePath := fmt.Sprintf("%v.hexdump", chunkFilePath)
hexdumpFile, err := os.Create(hexdumpFilePath)
if err != nil {
panic(err)
}
writer := bufio.NewWriter(hexdumpFile)

hexdump(chunkData, int64(index)*chunkSize, writer)
writer.Flush()
hexdumpFile.Close()

fmt.Printf("Created hexdump file: %s\n", hexdumpFilePath)
}(index, chunkFilePath)
}

wg.Wait()
}
7 changes: 7 additions & 0 deletions memorydump/memorydump_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "testing"

func Test_prof(t *testing.T) {
main()
}
5 changes: 4 additions & 1 deletion quartz/compiler.qz
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ module Compiler {

term = transform_let_call(term);

// file_write("quartz.ir", term.to_string());

let gen = Generator::new(validate_address);
gen.set_globals(typechecker.globals);
gen.set_strings(irgen.strings.strings);
Expand All @@ -199,7 +201,8 @@ module Compiler {

term = gen.fold_consts(term);

let code = gen.run(term, entrypoint, irgen.data_section_offset);
let offset = ((irgen.data_section_offset + 7) / 8) * 8;
let code = gen.run(term, entrypoint, offset);

return code;
}
Expand Down
9 changes: 6 additions & 3 deletions quartz/generator.qz
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,10 @@ module Generator {
self.write(format(" ;; {}", expr.t_u32!.to_string()));
}

// FIXME: i64::to_string is broken!
self.new_statement();
self.write(format("{}.const {}", Value::wasm_type(), expr.t_u32!.to_string()));
self.write_value(Value {
t_u32: expr.t_u32!,
});
} else if expr.t_nil != nil {
if MODE_READABLE_WASM {
self.new_statement();
Expand All @@ -758,7 +759,9 @@ module Generator {

// push to stack
let is_core_function = self.current_function_name.starts_with("quartz_core");
if (expr.t_let!.type_.t_address != nil || expr.t_let!.type_.t_any != nil) && !self.globals.has(expr.t_let!.name) && !is_core_function {
if (expr.t_let!.type_.t_address != nil || expr.t_let!.type_.t_any != nil) && !self.globals.has(
expr.t_let!.name,
) && !is_core_function {
self.new_statement();
self.expression(IrTerm {
t_ident: expr.t_let!.name,
Expand Down
Loading

0 comments on commit fb29209

Please sign in to comment.