Skip to content

Commit

Permalink
chore: goroutines for memorydump
Browse files Browse the repository at this point in the history
  • Loading branch information
myuon committed Sep 18, 2023
1 parent 56f9152 commit 425fc97
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 47 deletions.
57 changes: 31 additions & 26 deletions memorydump/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strings"
"sync"
)

func appendByteAsHex(bs []byte, b byte) []byte {
Expand Down Expand Up @@ -46,10 +47,6 @@ func hexdump(data []byte, offset int, writer io.Writer) {
asciiVals := make([]byte, 0, 16)
nonZeroFlag := false
for _, b := range chunk {
// if b < 16 {
// hexVals = append(hexVals, '0')
// }
// hexVals = strconv.AppendUint(hexVals, uint64(b), 16)
hexVals = appendByteAsHex(hexVals, b)
hexVals = append(hexVals, ' ')

Expand Down Expand Up @@ -150,33 +147,41 @@ func main() {
fmt.Printf("Created chunk file: %s\n", chunkFilePath)
}

wg := sync.WaitGroup{}
for index, chunkFilePath := range chunkFilePaths {
fmt.Printf("Generating hexdump for file: %s\n", chunkFilePath)
wg.Add(1)
go func(index int, chunkFilePath string) {
defer wg.Done()

chunkFile, err := os.Open(chunkFilePath)
if err != nil {
panic(err)
}
defer chunkFile.Close()
fmt.Printf("Generating hexdump for file: %s\n", chunkFilePath)

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

hexdumpFilePath := fmt.Sprintf("%v.hexdump", chunkFilePath)
hexdumpFile, err := os.Create(hexdumpFilePath)
if err != nil {
panic(err)
}
writer := bufio.NewWriter(hexdumpFile)
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, index*chunkSize, writer)
writer.Flush()
hexdumpFile.Close()
hexdump(chunkData, index*chunkSize, writer)
writer.Flush()
hexdumpFile.Close()

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

wg.Wait()
}
69 changes: 48 additions & 21 deletions quartz/std.qz
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,14 @@ module i32 {
@[test]
fun test_i32_to_string() {
assert_eq(1234.to_string(), "1234");
assert_eq((0-1234).to_string(), "-1234");
assert_eq((0 - 1234).to_string(), "-1234");
assert_eq(0.to_string(), "0");
}

@[test]
fun test_i32_parse() {
assert_eq(i32::parse("1234"), 1234);
assert_eq(i32::parse("-1234"), 0-1234);
assert_eq(i32::parse("-1234"), 0 - 1234);
assert_eq(i32::parse("0"), 0);
}

Expand Down Expand Up @@ -481,13 +481,19 @@ module i64 {
}

while n.hi != 0 || n.lo != 0 {
let r = n.mod(i64 { hi: 0, lo: 10 });
let r = n.mod(i64 {
hi: 0,
lo: 10,
});
if r.hi != 0 {
panic("i64.to_string: r.hi != 0");
}

result.push((r.lo + 48) as byte);
n = n.div(i64 { hi: 0, lo: 10 });
n = n.div(i64 {
hi: 0,
lo: 10,
});
}

return vec_byte_to_string(result).reverse();
Expand Down Expand Up @@ -828,10 +834,7 @@ fun test_split() {
assert_eq("abc".split("abc"), make[vec[string]](""));
assert_eq("abc".split("def"), make[vec[string]]("abc"));
assert_eq("abcbdbebfbg".split("b"), make[vec[string]]("a", "c", "d", "e", "f", "g"));
assert_eq(
"apple/banana/orange/lime".split("/"),
make[vec[string]]("apple", "banana", "orange", "lime"),
);
assert_eq("apple/banana/orange/lime".split("/"), make[vec[string]]("apple", "banana", "orange", "lime"));
}

@[test]
Expand Down Expand Up @@ -1262,7 +1265,7 @@ fun is_a_record(p: ptr[ptr[any]]): bool {
fun test_is_a_record() {
assert(!is_a_record(nil as ptr[ptr[any]]));
assert(!is_a_record(10 as ptr[ptr[any]]));
assert(is_a_record(make[vec[i32]](1,2,3) as ptr[ptr[any]]));
assert(is_a_record(make[vec[i32]](1, 2, 3) as ptr[ptr[any]]));
assert(is_a_record("string" as ptr[ptr[any]]));
}

Expand Down Expand Up @@ -1457,7 +1460,11 @@ module derive {
if k > 0 {
s = s.concat(", ");
}
s = s.concat(format("{}:{}", derive::to_string_internal(key, pretty, depth + 1), derive::to_string_internal(value, pretty, depth + 1)));
s = s.concat(format(
"{}:{}",
derive::to_string_internal(key, pretty, depth + 1),
derive::to_string_internal(value, pretty, depth + 1),
));
}

return s.concat(")");
Expand Down Expand Up @@ -1504,7 +1511,10 @@ fun test_derive_to_string() {
assert_eq(derive::to_string(1), "1");
assert_eq(derive::to_string(true), "true");
assert_eq(derive::to_string("abc"), "\"abc\"");
assert_eq(derive::to_string(make[vec[string]]("a", "b", "cdef\nghi")), "vec(\"a\", \"b\", \"cdef\nghi\")");
assert_eq(
derive::to_string(make[vec[string]]("a", "b", "cdef\nghi")),
"vec(\"a\", \"b\", \"cdef\nghi\")",
);
assert_eq(
derive::to_string(struct {
name: "foo",
Expand Down Expand Up @@ -1567,9 +1577,13 @@ enum WasiError {
module WasiError {
fun from_errno(code: i32): WasiError {
if code == 44 {
return WasiError { no_entry: true };
return WasiError {
no_entry: true,
};
} else {
return WasiError { unknown: code };
return WasiError {
unknown: code,
};
}
}
}
Expand Down Expand Up @@ -1718,11 +1732,13 @@ fun mark(value: any) {

header.set_is_marked(true);

debug(header);

let rep = (header.get_type_rep() as ptr[TypeRep]).at(0);
// FIXME: Why is this necessary?
if !types::is_pointer(rep) {
return;
}
// if !types::is_pointer(rep) {
// return;
// }
if rep.nilptr != nil {
debug(error_invalid_type);
debug(rep.nilptr);
Expand Down Expand Up @@ -1780,23 +1796,23 @@ fun test_types_is_i32() {
assert(types::is_i32(1));
assert(!types::is_i32(true));
assert(!types::is_i32("abc"));
assert(!types::is_i32(make[vec[i32]](1,2,3)));
assert(!types::is_i32(make[vec[i32]](1, 2, 3)));
}

@[test]
fun test_types_is_pointer() {
assert(!types::is_pointer(1));
assert(!types::is_pointer(true));
assert(types::is_pointer("abc"));
assert(types::is_pointer(make[vec[i32]](1,2,3)));
assert(types::is_pointer(make[vec[i32]](1, 2, 3)));
}

@[test]
fun test_types_is_bool() {
assert(!types::is_bool(1));
assert(types::is_bool(true));
assert(!types::is_bool("abc"));
assert(!types::is_bool(make[vec[i32]](1,2,3)));
assert(!types::is_bool(make[vec[i32]](1, 2, 3)));
}

@[test]
Expand Down Expand Up @@ -1845,7 +1861,12 @@ fun environs(): map[string, string] {
let mp = make[map[string, string]]();

for i in 0..count_i32 {
let p = i32_from_bytes(buf_index_ptrbyte.at(i * 4 + 0), buf_index_ptrbyte.at(i * 4 + 1), buf_index_ptrbyte.at(i * 4 + 2), buf_index_ptrbyte.at(i * 4 + 3)) as ptr[byte];
let p = i32_from_bytes(
buf_index_ptrbyte.at(i * 4 + 0),
buf_index_ptrbyte.at(i * 4 + 1),
buf_index_ptrbyte.at(i * 4 + 2),
buf_index_ptrbyte.at(i * 4 + 3),
) as ptr[byte];
let kv = read_until_null(p).split("=");
if kv.length == 1 {
mp.insert(kv.at(0), "");
Expand Down Expand Up @@ -1882,7 +1903,12 @@ fun args(): vec[string] {
let args = make[vec[string]]();

for i in 0..count_i32 {
let p = i32_from_bytes(buf_index_ptrbyte.at(i * 4 + 0), buf_index_ptrbyte.at(i * 4 + 1), buf_index_ptrbyte.at(i * 4 + 2), buf_index_ptrbyte.at(i * 4 + 3)) as ptr[byte];
let p = i32_from_bytes(
buf_index_ptrbyte.at(i * 4 + 0),
buf_index_ptrbyte.at(i * 4 + 1),
buf_index_ptrbyte.at(i * 4 + 2),
buf_index_ptrbyte.at(i * 4 + 3),
) as ptr[byte];
let arg = read_until_null(p);
args.push(arg);
}
Expand All @@ -1907,3 +1933,4 @@ fun assert(a: bool) {
panic("assert failed");
}
}

0 comments on commit 425fc97

Please sign in to comment.