Skip to content

Commit 62dc7bd

Browse files
committed
Make --max-samples stop precisely at the limit
Previously, the sample limit was only checked at the start of each main loop iteration, allowing multiple samples to be processed before the check. Now the limit is checked inside the parse() loop before each message is processed, stopping immediately when the limit is reached. Changes: - proto.d: Add wantData() check at start of parse loop, return 0 when done - subproc.d: Add sampleLimit pointer and wantData() method - main.d: Set sample limit pointer on subprocesses No new global state - the limit is a local variable with all subprocesses pointing to it. Uses ulong.max to represent "no limit".
1 parent 257034f commit 62dc7bd

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

source/btdu/main.d

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,13 @@ Please report defects and enhancement requests to the GitHub issue tracker:
139139
if (maxTime)
140140
parsedMaxTime = parseDuration(maxTime);
141141

142-
ulong parsedMaxSamples;
142+
ulong parsedMaxSamples = ulong.max; // ulong.max means no limit
143143
if (maxSamples)
144144
parsedMaxSamples = maxSamples.to!ulong;
145145

146+
foreach (ref subproc; subprocesses)
147+
subproc.sampleLimit = &parsedMaxSamples;
148+
146149
@property real parsedMinResolution()
147150
{
148151
static Nullable!real value;
@@ -244,6 +247,7 @@ Please report defects and enhancement requests to the GitHub issue tracker:
244247
}
245248
// Only pause once
246249
maxSamples = maxTime = minResolution = null;
250+
parsedMaxSamples = ulong.max;
247251
}
248252
}
249253

source/btdu/proto.d

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,9 @@ size_t parse(H)(ref ubyte[] buf, ref H handler)
216216
{
217217
while (true)
218218
{
219+
if (!handler.wantData())
220+
return 0;
221+
219222
if (buf.length < Header.sizeof)
220223
return Header.sizeof - buf.length;
221224

source/btdu/subproc.d

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ struct Subprocess
5050
Pipe pipe;
5151
Socket socket;
5252
Pid pid;
53+
ulong* sampleLimit; /// Points to limit value; ulong.max means no limit
5354

5455
void start()
5556
{
@@ -81,6 +82,12 @@ struct Subprocess
8182
/// Section of buffer containing received and unparsed data
8283
private size_t bufStart, bufEnd;
8384

85+
/// Check if more data is wanted (called by proto.parse before each message)
86+
bool wantData()
87+
{
88+
return browserRoot.getSamples(SampleType.represented) < *sampleLimit;
89+
}
90+
8491
/// Called when select() identifies that the process wrote something.
8592
/// Reads one datum; returns `true` if there is more to read.
8693
bool handleInput()
@@ -92,6 +99,10 @@ struct Subprocess
9299
bufStart = bufEnd - data.length;
93100
if (bufStart == bufEnd)
94101
bufStart = bufEnd = 0;
102+
103+
if (bytesNeeded == 0)
104+
return false;
105+
95106
if (buf.length < bufEnd + bytesNeeded)
96107
{
97108
// Moving remaining data to the start of the buffer

0 commit comments

Comments
 (0)