-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exposed the AsyncProfiler stop method #121
Changes from 6 commits
9334c93
3a96305
a5feb74
6adb778
2d20500
fc97fb0
63450bd
9ecee93
67e1315
5ae13eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,9 +8,11 @@ | |||||
|
||||||
import java.lang.instrument.Instrumentation; | ||||||
import java.util.concurrent.atomic.AtomicBoolean; | ||||||
import java.util.concurrent.atomic.AtomicReference; | ||||||
|
||||||
public class PyroscopeAgent { | ||||||
private static final AtomicBoolean started = new AtomicBoolean(false); | ||||||
// this is used to store the options passed to the agent | ||||||
private static final AtomicReference<Options> startOptions = new AtomicReference<>(null); | ||||||
|
||||||
public static void premain(final String agentArgs, | ||||||
final Instrumentation inst) { | ||||||
|
@@ -35,22 +37,40 @@ public static void start(Config config) { | |||||
|
||||||
public static void start(Options options) { | ||||||
Logger logger = options.logger; | ||||||
|
||||||
if( !startOptions.compareAndSet(null, options)) { | ||||||
logger.log(Logger.Level.ERROR, "Failed to start profiling - already started"); | ||||||
return; | ||||||
} | ||||||
if (!options.config.agentEnabled) { | ||||||
logger.log(Logger.Level.INFO, "Pyroscope agent start disabled by configuration"); | ||||||
return; | ||||||
} | ||||||
logger.log(Logger.Level.DEBUG, "Config: %s", options.config); | ||||||
try { | ||||||
startOptions.compareAndSet(null, options); | ||||||
startOptions.get().scheduler.start(startOptions.get().profiler); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Simpler like that
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||
logger.log(Logger.Level.INFO, "Profiling started"); | ||||||
} catch (final Throwable e) { | ||||||
logger.log(Logger.Level.ERROR, "Error starting profiler %s", e); | ||||||
} | ||||||
} | ||||||
|
||||||
if (!started.compareAndSet(false, true)) { | ||||||
/** | ||||||
* stop is used to stop profiling | ||||||
simonswine marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
*/ | ||||||
public static void stop() { | ||||||
Logger logger = startOptions.get().logger; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will crash when startOptions is null There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a null check |
||||||
logger.log(Logger.Level.DEBUG, "Config: %s", startOptions.get().config); | ||||||
if( startOptions.get() == null) { | ||||||
logger.log(Logger.Level.ERROR, "Failed to start profiling - already started"); | ||||||
return; | ||||||
} | ||||||
logger.log(Logger.Level.DEBUG, "Config: %s", options.config); | ||||||
try { | ||||||
options.scheduler.start(options.profiler); | ||||||
logger.log(Logger.Level.INFO, "Profiling started"); | ||||||
startOptions.get().scheduler.stop(startOptions.get().profiler); | ||||||
startOptions.set(null); | ||||||
logger.log(Logger.Level.INFO, "Profiling stopped"); | ||||||
} catch (final Throwable e) { | ||||||
logger.log(Logger.Level.ERROR, "Error starting profiler %s", e); | ||||||
logger.log(Logger.Level.ERROR, "Error stopping profiler %s", e); | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unsure if we need the try catch block here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ya I removed the try/catch block. Might not even be needed in start |
||||||
} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,15 @@ public void start(Profiler profiler) { | |
|
||
} | ||
|
||
/** | ||
* Stops the profiling scheduler which stops the AsyncProfiler. | ||
* @param profiler | ||
*/ | ||
@Override | ||
public void stop(Profiler profiler) { | ||
profiler.stop(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this stop the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may not be stopping the this.job. Let me take a look at that. Also let me see if it can be executed concurrently with dumpProfile. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added the logic to stop There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure if the dumProfile and stop should be synchronized. @korniltsev any insights? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should. T1(dump) calls profiler.stop And now it profiles forever There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok let me make that change, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have made the change @korniltsev |
||
} | ||
|
||
private void stop() { | ||
if (job != null) { | ||
job.cancel(true); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
public class App { | ||
public static final int N_THREADS = 8; | ||
|
||
public static void main(String[] args) { | ||
public static void main(String[] args) throws InterruptedException { | ||
PyroscopeAgent.start( | ||
new PyroscopeAgent.Options.Builder( | ||
new Config.Builder() | ||
|
@@ -32,6 +32,11 @@ public static void main(String[] args) { | |
Pyroscope.setStaticLabels(mapOf("region", "us-east-1")); | ||
|
||
appLogic(); | ||
|
||
Thread.sleep(2 * 60 * 1000); | ||
|
||
// This is a naive implementation for the stop method | ||
PyroscopeAgent.stop(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To test this properly I suggest, you Start it, then in a separate thread: stop it after a 1 second and then start it again a second later. This will make sure it works as expected. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added the test. Please re-review. Let me know if I am making any blunders as well |
||
} | ||
|
||
private static void appLogic() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will fail, as it will always happen in line 40. You can safely remove it:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed it