Skip to content
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

Remove sigar from JvmMonitor #33

Merged
merged 2 commits into from
Aug 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/main/java/com/metamx/metrics/CpuAcctDeltaMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import com.metamx.emitter.service.ServiceMetricEvent;
import com.metamx.metrics.cgroups.CgroupDiscoverer;
import com.metamx.metrics.cgroups.CpuAcct;
import com.metamx.metrics.cgroups.JvmPidDiscoverer;
import com.metamx.metrics.cgroups.PidDiscoverer;
import com.metamx.metrics.cgroups.ProcCgroupDiscoverer;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
Expand All @@ -51,7 +49,7 @@ public CpuAcctDeltaMonitor(final Map<String, String[]> dimensions)

public CpuAcctDeltaMonitor(final Map<String, String[]> dimensions, final String feed)
{
this(feed, dimensions, new JvmPidDiscoverer(), new ProcCgroupDiscoverer());
this(feed, dimensions, JvmPidDiscoverer.instance(), new ProcCgroupDiscoverer());
}

public CpuAcctDeltaMonitor(
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/com/metamx/metrics/JvmMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@
public class JvmMonitor extends FeedDefiningMonitor
{
private final Map<String, String[]> dimensions;
private final long pid;

private final GcCounters gcCounters = new GcCounters();

public JvmMonitor()
{
this(ImmutableMap.<String, String[]>of());
this(ImmutableMap.of());
}

public JvmMonitor(Map<String, String[]> dimensions)
Expand All @@ -50,10 +51,16 @@ public JvmMonitor(Map<String, String[]> dimensions)
}

public JvmMonitor(Map<String, String[]> dimensions, String feed)
{
this(dimensions, feed, JvmPidDiscoverer.instance());
}

public JvmMonitor(Map<String, String[]> dimensions, String feed, PidDiscoverer pidDiscoverer)
{
super(feed);
Preconditions.checkNotNull(dimensions);
this.dimensions = ImmutableMap.copyOf(dimensions);
this.pid = Preconditions.checkNotNull(pidDiscoverer).getPid();
}

@Override
Expand Down Expand Up @@ -132,10 +139,9 @@ private class GcCounters

GcCounters()
{
long currentProcessId = SigarUtil.getCurrentProcessId();
// connect to itself
JStatData jStatData = JStatData.connect(currentProcessId);
Map<String, JStatData.Counter<?>> jStatCounters = jStatData.getAllCounters();
final JStatData jStatData = JStatData.connect(pid);
final Map<String, JStatData.Counter<?>> jStatCounters = jStatData.getAllCounters();

generations.add(new GcGeneration(jStatCounters, 0, "young"));
generations.add(new GcGeneration(jStatCounters, 1, "old"));
Expand All @@ -159,7 +165,8 @@ private class GcGeneration
private final GcGenerationCollector collector;
private final List<GcGenerationSpace> spaces = new ArrayList<>();

GcGeneration(Map<String, JStatData.Counter<?>> jStatCounters, long genIndex, String name){
GcGeneration(Map<String, JStatData.Counter<?>> jStatCounters, long genIndex, String name)
{
this.name = name.toLowerCase();

long spacesCount = ((JStatData.LongCounter) jStatCounters.get(
Expand Down Expand Up @@ -207,7 +214,8 @@ private class GcGenerationCollector
private long lastInvocations = 0;
private long lastCpuNanos = 0;

GcGenerationCollector(Map<String, JStatData.Counter<?>> jStatCounters, long genIndex){
GcGenerationCollector(Map<String, JStatData.Counter<?>> jStatCounters, long genIndex)
{
String collectorKeyPrefix = String.format("sun.gc.collector.%d", genIndex);

String nameKey = String.format("%s.name", collectorKeyPrefix);
Expand Down Expand Up @@ -270,7 +278,8 @@ private class GcGenerationSpace
private final LongCounter usedCounter;
private final LongCounter initCounter;

GcGenerationSpace(Map<String, JStatData.Counter<?>> jStatCounters, long genIndex, long spaceIndex){
GcGenerationSpace(Map<String, JStatData.Counter<?>> jStatCounters, long genIndex, long spaceIndex)
{
String spaceKeyPrefix = String.format("sun.gc.generation.%d.space.%d", genIndex, spaceIndex);

String nameKey = String.format("%s.name", spaceKeyPrefix);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,32 @@
* limitations under the License.
*/

package com.metamx.metrics.cgroups;
package com.metamx.metrics;

import com.metamx.common.RE;
import java.lang.management.ManagementFactory;
import java.util.regex.Pattern;

/**
* For systems that for whatever reason cannot use Sigar (through com.metamx.metrics.cgroups.SigarPidDiscoverer ),
* For systems that for whatever reason cannot use Sigar (through com.metamx.metrics.SigarPidDiscoverer ),
* this attempts to get the PID from the JVM "name".
*/
public class JvmPidDiscoverer implements PidDiscoverer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a static method to not always force to create an object JvmPidDiscoverer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant a static method that just returns Pid, but it works too

{
private static final JvmPidDiscoverer INSTANCE = new JvmPidDiscoverer();

public static JvmPidDiscoverer instance()
{
return INSTANCE;
}

/**
* use {JvmPidDiscoverer.instance()}
*/
private JvmPidDiscoverer()
{
}

/**
* Returns the PID as a best guess. This uses methods that are not guaranteed to actually be the PID.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.metamx.metrics.cgroups;
package com.metamx.metrics;

public interface PidDiscoverer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,25 @@
* limitations under the License.
*/

package com.metamx.metrics.cgroups;

import com.metamx.metrics.SigarUtil;
package com.metamx.metrics;

public class SigarPidDiscoverer implements PidDiscoverer
{
private static final SigarPidDiscoverer INSTANCE = new SigarPidDiscoverer();

public static SigarPidDiscoverer instance()
{
return INSTANCE;
}

/**
* use {SigarPidDiscoverer.instance()}
*/
private SigarPidDiscoverer()
{

}

@Override
public long getPid()
{
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/metamx/metrics/cgroups/CpuAcct.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.metamx.common.RE;
import com.metamx.common.logger.Logger;
import com.metamx.metrics.CgroupUtil;
import com.metamx.metrics.PidDiscoverer;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.metamx.metrics.cgroups;
package com.metamx.metrics;

import org.junit.Assert;
import org.junit.Test;
Expand All @@ -24,6 +24,6 @@ public class JvmPidDiscovererTest
@Test
public void getPid() throws Exception
{
Assert.assertNotNull(new JvmPidDiscoverer().getPid());
Assert.assertNotNull(JvmPidDiscoverer.instance().getPid());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.metamx.metrics.cgroups;
package com.metamx.metrics;

import org.junit.Test;

Expand All @@ -24,6 +24,6 @@ public class SigarPidDiscovererTest
public void simpleTest()
{
// Just make sure we don't crash
new SigarPidDiscoverer().getPid();
SigarPidDiscoverer.instance().getPid();
}
}