Skip to content

Commit

Permalink
add "--ttl <time>" option to support to automatically shutdown profil…
Browse files Browse the repository at this point in the history
…er when it's in loop (continuous profiling) model.
  • Loading branch information
zhangyi committed May 29, 2023
1 parent 32601bc commit 6da8211
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PROFILER_VERSION=2.9
PROFILER_VERSION=2.9.1

PACKAGE_NAME=async-profiler-$(PROFILER_VERSION)-$(OS_TAG)-$(ARCH_TAG)
PACKAGE_DIR=/tmp/$(PACKAGE_NAME)
Expand Down
5 changes: 5 additions & 0 deletions profiler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ usage() {
echo " --reverse generate stack-reversed FlameGraph / Call tree"
echo ""
echo " --loop time run profiler in a loop"
echo " --ttl time automatically shutdown profiler at <time>(absolute or relative) when it's in loop (continuous profiling) model"
echo " --alloc bytes allocation profiling interval in bytes"
echo " --live build allocation profile from live objects only"
echo " --lock duration lock profiling threshold in nanoseconds"
Expand Down Expand Up @@ -237,6 +238,10 @@ while [ $# -gt 0 ]; do
PARAMS="$PARAMS,${1#--}=$2"
shift
;;
--ttl)
PARAMS="$PARAMS,${1#--}=$2"
shift
;;
--all-user)
PARAMS="$PARAMS,alluser"
;;
Expand Down
6 changes: 6 additions & 0 deletions src/arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ static const Multiplier UNIVERSAL[] = {{'n', 1}, {'u', 1000}, {'m', 1000000}, {'
// chunktime=N - duration of JFR chunk in seconds (default: 1 hour)
// timeout=TIME - automatically stop profiler at TIME (absolute or relative)
// loop=TIME - run profiler in a loop (continuous profiling)
// ttl=TIME - automatically shutdown profiler at TIME(absolute or relative) when it's in loop (continuous profiling) model
// interval=N - sampling interval in ns (default: 10'000'000, i.e. 10 ms)
// jstackdepth=N - maximum Java stack depth (default: 2048)
// safemode=BITS - disable stack recovery techniques (default: 0, i.e. everything enabled)
Expand Down Expand Up @@ -228,6 +229,11 @@ Error Arguments::parse(const char* args) {
msg = "Invalid loop duration";
}

CASE("ttl")
if (value == NULL || (_ttl = parseTimeout(value)) == -1 || !_persistent) {
msg = "Invalid ttl duration";
}

CASE("alloc")
_alloc = value == NULL ? 0 : parseUnits(value, BYTES);

Expand Down
2 changes: 2 additions & 0 deletions src/arguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class Arguments {
int _exclude;
unsigned char _mcache;
bool _loop;
int _ttl;
bool _threads;
bool _sched;
bool _live;
Expand Down Expand Up @@ -199,6 +200,7 @@ class Arguments {
_exclude(0),
_mcache(0),
_loop(false),
_ttl(0),
_threads(false),
_sched(false),
_live(false),
Expand Down
13 changes: 13 additions & 0 deletions src/profiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,13 @@ Error Profiler::start(Arguments& args, bool reset) {

if (args._timeout != 0 || args._output == OUTPUT_JFR) {
_stop_time = addTimeout(_start_time, args._timeout);
if (_in_first_loop) {
if (args._ttl == 0) {
_hung_time = 0;
} else {
_hung_time = addTimeout(_start_time, args._ttl);
}
}
startTimer();
}

Expand Down Expand Up @@ -1086,6 +1093,7 @@ Error Profiler::stop() {
return Error("Profiler is not active");
}

_in_first_loop = false;
uninstallTraps();

if (_event_mask & EM_LOCK) lock_tracer.stop();
Expand Down Expand Up @@ -1500,6 +1508,7 @@ Error Profiler::runInternal(Arguments& args, std::ostream& out) {
switch (args._action) {
case ACTION_START:
case ACTION_RESUME: {
_in_first_loop = true;
Error error = start(args, args._action == ACTION_START);
if (error) {
return error;
Expand Down Expand Up @@ -1619,6 +1628,10 @@ Error Profiler::restart(Arguments& args) {
}

if (args._loop) {
if (_hung_time > 0 && OS::micros() >= (u64)_hung_time * 1000000ULL) {
return Error::OK;
}
_in_first_loop = false;
args._file_num++;
return start(args, true);
}
Expand Down
3 changes: 3 additions & 0 deletions src/profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class Profiler {

time_t _start_time;
time_t _stop_time;
bool _in_first_loop;
time_t _hung_time;
int _epoch;
WaitableMutex _timer_lock;
void* _timer_id;
Expand Down Expand Up @@ -165,6 +167,7 @@ class Profiler {
_call_trace_storage(),
_jfr(),
_start_time(0),
_hung_time(0),
_epoch(0),
_timer_id(NULL),
_max_stack_depth(0),
Expand Down

0 comments on commit 6da8211

Please sign in to comment.