Skip to content

Commit

Permalink
Allow dynamic update of agent parameters
Browse files Browse the repository at this point in the history
Made the agent profilers re-entrant allowing
to attach and update profiling parameters at runtime

This is useful when the VM is not started with the agent
but can be later attached and controlled, e.g:
- update profiling interval
- enabling/disabling profilers

For time being only profilers parameters can be updated.
Transformers, reporter not
  • Loading branch information
amuraru committed Apr 18, 2019
1 parent 5c2d558 commit 1bdcde7
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 205 deletions.
277 changes: 137 additions & 140 deletions src/main/java/com/uber/profiling/AgentImpl.java

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/main/java/com/uber/profiling/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.uber.profiling.util.DummyConfigProvider;
import com.uber.profiling.util.JsonUtils;
import com.uber.profiling.util.ReflectionUtils;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -59,6 +58,7 @@ public class Arguments {
private boolean noop = false;

private Constructor<Reporter> reporterConstructor;
private Reporter reporter;
private Constructor<ConfigProvider> configProviderConstructor;
private String configFile;

Expand Down Expand Up @@ -260,11 +260,14 @@ public Map<String, List<String>> getRawArgValues() {
}

public Reporter getReporter() {
if (reporter != null) {
return reporter;
}
if (reporterConstructor == null) {
return new ConsoleOutputReporter();
} else {
try {
Reporter reporter = reporterConstructor.newInstance();
reporter = reporterConstructor.newInstance();
reporter.updateArguments(getRawArgValues());
return reporter;
} catch (Throwable e) {
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/com/uber/profiling/Profiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,20 @@

package com.uber.profiling;

public interface Profiler {
long getIntervalMillis();
public abstract class Profiler {
private ProfilerRunner runner;

void setReporter(Reporter reporter);
public abstract long getIntervalMillis();
public abstract void setIntervalMillis(long millis);

void profile();
public abstract void setReporter(Reporter reporter);

public abstract void profile();

void setRunner(ProfilerRunner runner){
this.runner = runner;
}
ProfilerRunner getRunner(){
return this.runner;
}
}
38 changes: 0 additions & 38 deletions src/main/java/com/uber/profiling/ProfilerGroup.java

This file was deleted.

10 changes: 10 additions & 0 deletions src/main/java/com/uber/profiling/ProfilerRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.uber.profiling.util.AgentLogger;

import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.atomic.AtomicLong;

public class ProfilerRunner implements Runnable {
Expand All @@ -27,6 +28,7 @@ public class ProfilerRunner implements Runnable {

private final Profiler profiler;
private final AtomicLong errorCounter = new AtomicLong(0);
private ScheduledFuture<?> handler;

public ProfilerRunner(Profiler profiler) {
this.profiler = profiler;
Expand All @@ -45,4 +47,12 @@ public void run() {
}
}
}

public void setHandler(ScheduledFuture<?> handler) {
this.handler = handler;
}

public ScheduledFuture<?> getHandler() {
return handler;
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/uber/profiling/ShutdownHookRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public void run() {

for (Profiler profiler : profilers) {
try {
if (profiler.getIntervalMillis() <=0 ){
//one time profiler, skip
continue;
}
logShutdownMessage("Running periodic profiler (last run): " + profiler);
profiler.profile();
logShutdownMessage("Ran periodic profiler (last run): " + profiler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.uber.profiling.profilers;

import com.uber.profiling.Profiler;
import com.uber.profiling.Reporter;
import com.uber.profiling.util.AgentLogger;
import com.uber.profiling.util.ProcFileUtils;
Expand All @@ -36,7 +35,7 @@
import java.util.List;
import java.util.Map;

public class CpuAndMemoryProfiler extends ProcessInfoBase implements Profiler {
public class CpuAndMemoryProfiler extends ProcessInfoBase {
public final static String PROFILER_NAME = "CpuAndMemory";

private static final AgentLogger logger = AgentLogger.getLogger(CpuAndMemoryProfiler.class.getName());
Expand Down Expand Up @@ -70,6 +69,7 @@ public long getIntervalMillis() {
return intervalMillis;
}

@Override
public void setIntervalMillis(long intervalMillis) {
this.intervalMillis = intervalMillis;
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/uber/profiling/profilers/IOProfiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@

package com.uber.profiling.profilers;

import com.uber.profiling.Profiler;
import com.uber.profiling.Reporter;
import com.uber.profiling.util.ProcFileUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IOProfiler extends ProcessInfoBase implements Profiler {
public class IOProfiler extends ProcessInfoBase {
public final static String PROFILER_NAME = "IO";

private long intervalMillis = Constants.DEFAULT_METRIC_INTERVAL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.uber.profiling.util.ClassMethodArgumentMetricBuffer;

public class MethodArgumentCollector {
public static final String PROFILER_NAME = "MethodArgumentCollector";
private ClassMethodArgumentMetricBuffer buffer;

public MethodArgumentCollector(ClassMethodArgumentMetricBuffer buffer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.uber.profiling.profilers;

import com.uber.profiling.Profiler;
import com.uber.profiling.Reporter;
import com.uber.profiling.reporters.ConsoleOutputReporter;
import com.uber.profiling.util.ClassAndMethodMetricKey;
Expand All @@ -26,7 +25,7 @@
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

public class MethodArgumentProfiler extends ProcessInfoBase implements Profiler {
public class MethodArgumentProfiler extends ProcessInfoBase {
public static final String PROFILER_NAME = "MethodArgument";

private ClassMethodArgumentMetricBuffer buffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.uber.profiling.profilers;

import com.uber.profiling.Profiler;
import com.uber.profiling.Reporter;
import com.uber.profiling.reporters.ConsoleOutputReporter;
import com.uber.profiling.util.ClassAndMethodLongMetricBuffer;
Expand All @@ -26,7 +25,7 @@
import java.util.HashMap;
import java.util.Map;

public class MethodDurationProfiler extends ProcessInfoBase implements Profiler {
public class MethodDurationProfiler extends ProcessInfoBase {
public static final String PROFILER_NAME = "MethodDuration";

private ClassAndMethodLongMetricBuffer buffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@

package com.uber.profiling.profilers;

import com.uber.profiling.Profiler;
import com.uber.profiling.util.NetworkUtils;
import com.uber.profiling.util.ProcFileUtils;
import com.uber.profiling.util.ProcessUtils;
import com.uber.profiling.util.SparkUtils;

import java.util.UUID;

public class ProcessInfoBase {
public abstract class ProcessInfoBase extends Profiler {
private String tag = null;
private String cluster = null;
private String hostName = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.uber.profiling.profilers;

import com.uber.profiling.AgentImpl;
import com.uber.profiling.Profiler;
import com.uber.profiling.Reporter;
import com.uber.profiling.util.AgentLogger;
import com.uber.profiling.util.ProcFileUtils;
Expand All @@ -30,7 +29,7 @@
import java.util.List;
import java.util.Map;

public class ProcessInfoProfiler extends ProcessInfoBase implements Profiler {
public class ProcessInfoProfiler extends ProcessInfoBase {
public final static String PROFILER_NAME = "ProcessInfo";

private static final AgentLogger logger = AgentLogger.getLogger(ProcessInfoProfiler.class.getName());
Expand All @@ -50,7 +49,11 @@ public ProcessInfoProfiler(Reporter reporter) {

@Override
public long getIntervalMillis() {
return 0;
return -1;
}

@Override
public void setIntervalMillis(long millis) {
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
/**
* This class collects stacktraces by getting thread dump via JMX, and stores the stacktraces into the given buffer.
*/
public class StacktraceCollectorProfiler implements Profiler {
public class StacktraceCollectorProfiler extends Profiler {
public static final String PROFILER_NAME = "StacktraceCollector";
private long intervalMillis;
private StacktraceMetricBuffer buffer;
private String ignoreThreadNamePrefix = "";
Expand Down Expand Up @@ -63,6 +64,9 @@ public void setReporter(Reporter reporter) {

@Override
public void profile() {
if (getIntervalMillis() <=0){
return;
}
ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(false, false);
if (threadInfos == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.uber.profiling.profilers;

import com.uber.profiling.Profiler;
import com.uber.profiling.Reporter;
import com.uber.profiling.reporters.ConsoleOutputReporter;
import com.uber.profiling.util.ClassAndMethod;
Expand All @@ -32,7 +31,7 @@
/**
* This class reads the stacktraces from the given buffer and send out via given reporter.
*/
public class StacktraceReporterProfiler extends ProcessInfoBase implements Profiler {
public class StacktraceReporterProfiler extends ProcessInfoBase {
public static final String PROFILER_NAME = "Stacktrace";

private StacktraceMetricBuffer buffer;
Expand All @@ -41,6 +40,10 @@ public class StacktraceReporterProfiler extends ProcessInfoBase implements Profi

private long intervalMillis = Constants.DEFAULT_METRIC_INTERVAL;

public StacktraceMetricBuffer getBuffer() {
return buffer;
}

public StacktraceReporterProfiler(StacktraceMetricBuffer buffer, Reporter reporter) {
this.buffer = buffer;
this.reporter = reporter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

import com.uber.profiling.Reporter;
import com.uber.profiling.util.JsonUtils;

import java.util.List;
import java.util.Map;

public class ConsoleOutputReporter implements Reporter {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/uber/profiling/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
package com.uber.profiling.util;

import org.apache.commons.lang3.math.NumberUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down
4 changes: 4 additions & 0 deletions src/test/java/com/uber/profiling/ProfilerRunnableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public long getIntervalMillis() {
return 0;
}

@Override
public void setIntervalMillis(long millis) {
}

@Override
public void setReporter(Reporter reporter) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void close() {
}
});

Assert.assertEquals(0L, profiler.getIntervalMillis());
Assert.assertEquals(-1L, profiler.getIntervalMillis());

profiler.profile();
profiler.profile();
Expand Down

0 comments on commit 1bdcde7

Please sign in to comment.