Skip to content

Commit 5a8baaa

Browse files
Allow usage of both timeout & loop together
1 parent 6e04336 commit 5a8baaa

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

src/arguments.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,7 @@ Error Arguments::parse(const char* args) {
242242
}
243243

244244
CASE("loop")
245-
_loop = true;
246-
if (value == NULL || (_timeout = parseTimeout(value)) == -1) {
245+
if (value == NULL || (_loop = parseTimeout(value)) == -1) {
247246
msg = "Invalid loop duration";
248247
}
249248

src/arguments.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ class Arguments {
176176
const char* _event;
177177
std::vector<const char*> _trace;
178178
int _timeout;
179+
int _loop;
179180
long _interval;
180181
long _alloc;
181182
long _nativemem;
@@ -195,7 +196,6 @@ class Arguments {
195196
std::vector<const char*> _include;
196197
std::vector<const char*> _exclude;
197198
unsigned char _mcache;
198-
bool _loop;
199199
bool _preloaded;
200200
bool _quiet;
201201
bool _threads;
@@ -237,6 +237,7 @@ class Arguments {
237237
_event(NULL),
238238
_trace(),
239239
_timeout(0),
240+
_loop(0),
240241
_interval(0),
241242
_alloc(-1),
242243
_nativemem(-1),
@@ -256,7 +257,6 @@ class Arguments {
256257
_include(),
257258
_exclude(),
258259
_mcache(0),
259-
_loop(false),
260260
_preloaded(false),
261261
_quiet(false),
262262
_threads(false),

src/profiler.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,8 +1267,11 @@ Error Profiler::start(Arguments& args, bool reset) {
12671267
_start_time = OS::micros();
12681268
_epoch++;
12691269

1270-
if (args._timeout != 0 || args._output == OUTPUT_JFR) {
1271-
_stop_time = addTimeout(_start_time, args._timeout);
1270+
if (args._timeout != 0 || args._loop != 0 || args._output == OUTPUT_JFR) {
1271+
_loop_time = addTimeout(_start_time, args._loop);
1272+
if (args._file_num == 0) {
1273+
_stop_time = addTimeout(_start_time, args._timeout);
1274+
}
12721275
startTimer();
12731276
}
12741277

@@ -1846,7 +1849,8 @@ void Profiler::stopTimer() {
18461849

18471850
void Profiler::timerLoop(void* timer_id) {
18481851
u64 current_micros = OS::micros();
1849-
u64 sleep_until = _jfr.active() ? current_micros + 1000000 : _stop_time;
1852+
u64 loop_limit = std::min(_stop_time, _loop_time);
1853+
u64 sleep_until = _jfr.active() ? current_micros + 1000000 : std::min(_stop_time, loop_limit);
18501854

18511855
while (true) {
18521856
{
@@ -1858,7 +1862,7 @@ void Profiler::timerLoop(void* timer_id) {
18581862
if (_timer_id != timer_id) return;
18591863
}
18601864

1861-
if ((current_micros = OS::micros()) >= _stop_time) {
1865+
if ((current_micros = OS::micros()) >= loop_limit) {
18621866
restart(_global_args);
18631867
return;
18641868
}
@@ -1998,8 +2002,9 @@ Error Profiler::run(Arguments& args) {
19982002

19992003
Error Profiler::restart(Arguments& args) {
20002004
MutexLocker ml(_state_lock);
2005+
bool restart = OS::micros() < _stop_time;
20012006

2002-
Error error = stop(args._loop);
2007+
Error error = stop(restart);
20032008
if (error) {
20042009
return error;
20052010
}
@@ -2015,7 +2020,7 @@ Error Profiler::restart(Arguments& args) {
20152020
}
20162021
}
20172022

2018-
if (args._loop) {
2023+
if (restart) {
20192024
args._fdtransfer = false; // keep the previous connection
20202025
args._file_num++;
20212026
return start(args, true);

src/profiler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class Profiler {
6868

6969
u64 _start_time;
7070
u64 _stop_time;
71+
u64 _loop_time;
7172
int _epoch;
7273
u32 _gc_id;
7374
WaitableMutex _timer_lock;

0 commit comments

Comments
 (0)