Skip to content

Commit

Permalink
8261034: improve jcmd GC.class_histogram to support parallel
Browse files Browse the repository at this point in the history
Reviewed-by: cjplummer, sspitsyn
  • Loading branch information
Hamlin Li committed May 12, 2021
1 parent ed32e02 commit 3c47cab
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 49 deletions.
21 changes: 19 additions & 2 deletions src/hotspot/share/services/diagnosticCommand.cpp
Expand Up @@ -496,13 +496,30 @@ void HeapDumpDCmd::execute(DCmdSource source, TRAPS) {
ClassHistogramDCmd::ClassHistogramDCmd(outputStream* output, bool heap) :
DCmdWithParser(output, heap),
_all("-all", "Inspect all objects, including unreachable objects",
"BOOLEAN", false, "false") {
"BOOLEAN", false, "false"),
_parallel_thread_num("-parallel",
"Number of parallel threads to use for heap inspection. "
"0 (the default) means let the VM determine the number of threads to use. "
"1 means use one thread (disable parallelism). "
"For any other value the VM will try to use the specified number of "
"threads, but might use fewer.",
"INT", false, "0") {
_dcmdparser.add_dcmd_option(&_all);
_dcmdparser.add_dcmd_option(&_parallel_thread_num);
}

void ClassHistogramDCmd::execute(DCmdSource source, TRAPS) {
jlong num = _parallel_thread_num.value();
if (num < 0) {
output()->print_cr("Parallel thread number out of range (>=0): " JLONG_FORMAT, num);
return;
}
uint parallel_thread_num = num == 0
? MAX2<uint>(1, (uint)os::initial_active_processor_count() * 3 / 8)
: num;
VM_GC_HeapInspection heapop(output(),
!_all.value() /* request full gc if false */);
!_all.value(), /* request full gc if false */
parallel_thread_num);
VMThread::execute(&heapop);
}

Expand Down
1 change: 1 addition & 0 deletions src/hotspot/share/services/diagnosticCommand.hpp
Expand Up @@ -339,6 +339,7 @@ class HeapDumpDCmd : public DCmdWithParser {
class ClassHistogramDCmd : public DCmdWithParser {
protected:
DCmdArgument<bool> _all;
DCmdArgument<jlong> _parallel_thread_num;
public:
ClassHistogramDCmd(outputStream* output, bool heap);
static const char* name() {
Expand Down

This file was deleted.

42 changes: 36 additions & 6 deletions test/hotspot/jtreg/serviceability/dcmd/gc/ClassHistogramTest.java
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -21,6 +21,7 @@
* questions.
*/

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.regex.Pattern;
Expand All @@ -42,16 +43,19 @@
public class ClassHistogramTest {
public static class TestClass {}
public static TestClass[] instances = new TestClass[1024];
protected String classHistogramArgs = "";

static {
for (int i = 0; i < instances.length; ++i) {
instances[i] = new TestClass();
}
}

public void run(CommandExecutor executor) {
public void run(CommandExecutor executor, String classHistogramArgs, String expactedErrMsg) {
OutputAnalyzer output = executor.execute("GC.class_histogram " + classHistogramArgs);
if (!expactedErrMsg.isEmpty()) {
output.shouldMatch(expactedErrMsg);
return;
}

/*
* example output:
Expand Down Expand Up @@ -87,8 +91,34 @@ public void run(CommandExecutor executor) {
Pattern.quote(TestClass.class.getName()) + "\\s*$");
}

@Test
public void jmx() {
run(new JMXExecutor());
@DataProvider(name="ArgsProvider")
private Object[][] getArgs() {
String parallelErr = "Parallel thread number out of range";
return new Object[][] {
// valid args
{"", ""},
{"-parallel=0", ""},
{"-parallel=1", ""},
{"-parallel=2", ""},
{"-parallel="+Long.MAX_VALUE, ""},
{"-all=false -parallel=0", ""},
{"-all=false -parallel=1", ""},
{"-all=false -parallel=2", ""},
{"-all=true", ""},
{"-all=true -parallel=0", ""},
{"-all=true -parallel=1", ""},
{"-all=true -parallel=2", ""},
{"-parallel=2 -all=true", ""},
// invalid args
{"-parallel=-1", parallelErr},
{"-parallel="+Long.MIN_VALUE, parallelErr},
{"-all=false -parallel=-10", parallelErr},
{"-all=true -parallel=-100", parallelErr},
};
}

@Test(dataProvider="ArgsProvider")
public void jmx(String args, String expactedErrMsg) {
run(new JMXExecutor(), args, expactedErrMsg);
}
}

1 comment on commit 3c47cab

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.