Skip to content

Commit 6eab4d7

Browse files
Memory tracking for fastotf2converter
We use Chapel's built in `--memTrack` option to track Chapel memory usage. And we use the Linux kernel's `getrusage` call to get the memory usage from extern C libraries (otf2 and parquet). Passing `--memTrack` prints memory usage info from C and Chapel. [Reviewed by @brandon-neth]
2 parents 75dc89a + ff328d2 commit 6eab4d7

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

apps/FastOTF2Converter/src/FastOTF2Converter.chpl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,32 @@ module FastOTF2Converter {
66
use Strategy_LocBlock;
77
use Strategy_LocGroupBlock;
88
use Strategy_LocGroupDistBlock;
9+
use CTypes;
10+
use MemDiagnostics;
11+
private use MemTracking;
912
// Future strategies:
1013
// use Strategy_LocDynamic;
1114
// use Strategy_LocGroupDynamic;
1215
// use Strategy_LocGroupBlockDistDynamic;
1316
// use Strategy_LocGroupDistBalanced;
1417

18+
require "helpers/memtrack_helper.h";
19+
extern proc get_peak_rss_kib(): c_long;
20+
21+
param KiB_PER_GiB = 1024.0 * 1024.0;
22+
1523
proc main(args: [] string) throws {
1624
const conf = parseConverterArgs(args);
1725
validatePaths(conf);
1826

27+
// Capture baseline RSS per locale (before work)
28+
var baselineKiB: [0..#numLocales] int;
29+
if memTrack {
30+
coforall loc in Locales do on loc {
31+
baselineKiB[here.id] = get_peak_rss_kib(): int;
32+
}
33+
}
34+
1935
select conf.strategy {
2036
when "serial" do
2137
Strategy_Serial.run(conf);
@@ -39,5 +55,22 @@ module FastOTF2Converter {
3955
"locgroup_dynamic, locgroup_dist_block, ",
4056
"locgroup_blockdist_dynamic, locgroup_dist_balanced");
4157
}
58+
59+
// Report memory stats if --memTrack was passed
60+
if memTrack {
61+
writeln();
62+
writeln("=== Memory Report ===");
63+
coforall loc in Locales do on loc {
64+
var peakKiB = get_peak_rss_kib(): int;
65+
var deltaKiB = peakKiB - baselineKiB[here.id];
66+
var peakGiB = peakKiB: real / KiB_PER_GiB;
67+
var deltaGiB = deltaKiB: real / KiB_PER_GiB;
68+
writeln(" Locale ", here.id,
69+
": peak RSS=", peakGiB, " GiB",
70+
" delta RSS (OTF2+parquet memory usage)=", deltaGiB, " GiB");
71+
printMemAllocStats();
72+
}
73+
writeln("=== End Memory Report ===");
74+
}
4275
}
4376
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef MEMTRACK_HELPER_H
2+
#define MEMTRACK_HELPER_H
3+
4+
#include <sys/resource.h>
5+
6+
static inline long get_peak_rss_kib(void) {
7+
struct rusage usage;
8+
if (getrusage(RUSAGE_SELF, &usage) != 0) {
9+
return -1; // Error case
10+
}
11+
#ifdef __APPLE__
12+
return usage.ru_maxrss / 1024; // macOS: bytes -> KiB
13+
#else
14+
return usage.ru_maxrss; // Linux: already in KiB
15+
#endif
16+
}
17+
18+
#endif

0 commit comments

Comments
 (0)