Skip to content

Commit cd41c91

Browse files
Trace the census stack scan on macOS (diagnostic, not for main)
1 parent 2cbf03b commit cd41c91

3 files changed

Lines changed: 115 additions & 0 deletions

File tree

‎.github/workflows/diag.yml‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
name: diag
2+
# Throwaway: runs the census test with the stack scan traced, on macOS only
3+
on:
4+
push:
5+
branches: ['diag/**']
6+
jobs:
7+
diag:
8+
strategy:
9+
fail-fast: false
10+
matrix:
11+
os: [macos-latest, macos-15-intel]
12+
runs-on: ${{ matrix.os }}
13+
defaults:
14+
run:
15+
shell: bash
16+
steps:
17+
- uses: actions/checkout@v7
18+
- uses: ./.github/workflows/setup
19+
with:
20+
haxe: 4.3.7
21+
- name: build
22+
working-directory: test/diag
23+
run: haxe -m Main --cpp bin -D ${{ matrix.os == 'macos-latest' && 'HXCPP_ARM64' || 'HXCPP_M64' }} -D HXCPP_DEBUG_LINK
24+
- name: run
25+
working-directory: test/diag
26+
run: |
27+
HX_CENSUS_TRACE=1 bin/Main > out.txt 2> trace.txt || true
28+
cat out.txt
29+
line=$(grep -m1 'DIAG keep' trace.txt)
30+
echo "$line"
31+
keep=$(echo "$line" | awk '{print $3}')
32+
base=$(echo "$line" | awk '{print $5}')
33+
echo "lines mentioning keep $keep or its buffer $base:"
34+
grep -n -e "$keep" -e "$base" trace.txt | head -40
35+
echo "scans:"
36+
grep -n 'DIAG scan' trace.txt | head -20
37+
echo "total trace lines: $(wc -l < trace.txt)"
38+
echo "first 60 trace lines:"
39+
head -60 trace.txt

‎src/hx/gc/Immix.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5924,6 +5924,10 @@ void MarkConservative(int *inBottom, int *inTop,hx::MarkContext *__inCtx)
59245924
inTop--;
59255925
}
59265926

5927+
static bool censusTrace = getenv("HX_CENSUS_TRACE") != 0;
5928+
bool trace = censusTrace && sgCensus;
5929+
if (trace)
5930+
fprintf(stderr, "DIAG scan %p..%p (%d words)\n", (void *)inBottom, (void *)inTop, (int)(inTop-inBottom));
59275931
uintptr_t prev{};
59285932
uintptr_t lastPin{};
59295933
#ifdef HX_WATCH
@@ -5974,6 +5978,8 @@ void MarkConservative(int *inBottom, int *inTop,hx::MarkContext *__inCtx)
59745978
}
59755979
#endif
59765980

5981+
if (trace && mem!=memUnmanaged)
5982+
fprintf(stderr, "DIAG %p at %p mem %d\n", (void *)potentialObject, (void *)ptr, (int)mem);
59775983
if (mem!=memUnmanaged)
59785984
{
59795985
if (mem==memLarge)
@@ -6007,6 +6013,8 @@ void MarkConservative(int *inBottom, int *inTop,hx::MarkContext *__inCtx)
60076013
#endif
60086014

60096015

6016+
if (trace)
6017+
fprintf(stderr, "DIAG %p type %d\n", (void *)potentialObject, (int)t);
60106018
if ( t==allocObject )
60116019
{
60126020
#ifdef HX_WATCH

‎test/diag/Main.hx‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
class Small {
2+
public var a:Int;
3+
4+
public function new(a:Int)
5+
this.a = a;
6+
}
7+
8+
class Leaky {
9+
public var next:Leaky;
10+
11+
public function new(next:Leaky)
12+
this.next = next;
13+
}
14+
15+
class Flat {
16+
public var a:Int;
17+
18+
public function new(a:Int)
19+
this.a = a;
20+
}
21+
22+
class Main {
23+
static var held:Array<Flat>;
24+
25+
static function rowFor(census:Array<Dynamic>, name:String):Dynamic {
26+
for (row in census)
27+
if (row.name == name)
28+
return row;
29+
return null;
30+
}
31+
32+
// __hxcpp_gc_census counts the live objects of each class in a release build,
33+
// where Gc.trace needs a debug build.
34+
static function main() {
35+
var keep = [for (i in 0...20000) new Small(i)];
36+
var chain:Leaky = null;
37+
for (i in 0...300)
38+
chain = new Leaky(chain);
39+
for (i in 0...100000)
40+
new Small(i);
41+
42+
untyped __cpp__('fprintf(stderr, "DIAG keep %p base %p chain %p\\n", {0}.mPtr, {0}->GetBase(), {1}.mPtr)', keep, chain);
43+
for (round in 0...2) {
44+
var census:Array<Dynamic> = untyped __global__.__hxcpp_gc_census();
45+
var small:Dynamic = rowFor(census, "Small");
46+
var leaky:Dynamic = rowFor(census, "Leaky");
47+
if (small == null || leaky == null) {
48+
Sys.println("census " + round + " is missing a class: " + [for (row in census) row.name + " " + row.count].join(", "));
49+
continue;
50+
}
51+
var sorted = true;
52+
for (i in 1...census.length)
53+
if (census[i].bytes > census[i - 1].bytes)
54+
sorted = false;
55+
Sys.println("census " + round + ": Small " + small.count + ", Leaky " + leaky.count + ", bytes counted " + (small.bytes >= small.count * 8)
56+
+ ", most bytes first " + sorted);
57+
}
58+
59+
// An object with no pointer fields looks like raw memory on the stack, so
60+
// the last one a loop made, still in a stack slot, was marked as memory
61+
// there before the array reached it, and went uncounted
62+
held = [for (i in 0...1000) new Flat(i)];
63+
var census:Array<Dynamic> = untyped __global__.__hxcpp_gc_census();
64+
var flat:Dynamic = rowFor(census, "Flat");
65+
Sys.println("held " + (flat == null ? 0 : flat.count));
66+
Sys.println("kept " + keep.length + " " + held.length + " " + (chain.next != null));
67+
}
68+
}

0 commit comments

Comments
 (0)