Skip to content

Commit

Permalink
8301749: Tracking malloc pooled memory size
Browse files Browse the repository at this point in the history
Reviewed-by: dholmes, stuefe
  • Loading branch information
jdksjolen committed Feb 20, 2023
1 parent 6ac5e05 commit b5a7426
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/hotspot/os/linux/mallocInfoDcmd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2023, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

#include "precompiled.hpp"
#include "mallocInfoDcmd.hpp"
#include "os_linux.hpp"
#include "utilities/globalDefinitions.hpp"
#include "utilities/ostream.hpp"

#include <malloc.h>

constexpr const char* malloc_info_unavailable = "Error: malloc_info(3) not available.";

void MallocInfoDcmd::execute(DCmdSource source, TRAPS) {
#ifdef __GLIBC__
char* buf;
size_t size;
ALLOW_C_FUNCTION(::open_memstream, FILE* stream = ::open_memstream(&buf, &size);)
if (stream == nullptr) {
_output->print_cr("Error: Could not call malloc_info(3)");
return;
}

int err = os::Linux::malloc_info(stream);
if (err == 0) {
ALLOW_C_FUNCTION(::fflush, fflush(stream);)
_output->print_raw(buf);
_output->cr();
} else if (err == -1) {
_output->print_cr("Error: %s", os::strerror(errno));
} else if (err == -2) {
_output->print_cr(malloc_info_unavailable);
} else {
ShouldNotReachHere();
}
ALLOW_C_FUNCTION(::fclose, ::fclose(stream);)
ALLOW_C_FUNCTION(::free, ::free(buf);)
#else
_output->print_cr(malloc_info_unavailable);
#endif // __GLIBC__
}
51 changes: 51 additions & 0 deletions src/hotspot/os/linux/mallocInfoDcmd.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2023, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/

#ifndef OS_LINUX_MALLOCINFODCMD_HPP
#define OS_LINUX_MALLOCINFODCMD_HPP

#include "services/diagnosticCommand.hpp"

class outputStream;

class MallocInfoDcmd : public DCmd {
public:
MallocInfoDcmd(outputStream* output, bool heap) : DCmd(output, heap) {}
static const char* name() {
return "System.native_heap_info";
}
static const char* description() {
return "Attempts to output information regarding native heap usage through malloc_info(3). If unsuccessful outputs \"Error: \" and a reason.";
}
static const char* impact() {
return "Low";
}
static const JavaPermission permission() {
JavaPermission p = { "java.lang.management.ManagementPermission", "monitor", nullptr };
return p;
}
void execute(DCmdSource source, TRAPS) override;
};

#endif // OS_LINUX_MALLOCINFODCMD_HPP
11 changes: 11 additions & 0 deletions src/hotspot/os/linux/os_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ struct new_mallinfo {
};
typedef struct new_mallinfo (*mallinfo2_func_t)(void);
static mallinfo2_func_t g_mallinfo2 = nullptr;

typedef int (*malloc_info_func_t)(int options, FILE *stream);
static malloc_info_func_t g_malloc_info = nullptr;
#endif // __GLIBC__

static int clock_tics_per_sec = 100;
Expand Down Expand Up @@ -4297,6 +4300,7 @@ void os::init(void) {
#ifdef __GLIBC__
g_mallinfo = CAST_TO_FN_PTR(mallinfo_func_t, dlsym(RTLD_DEFAULT, "mallinfo"));
g_mallinfo2 = CAST_TO_FN_PTR(mallinfo2_func_t, dlsym(RTLD_DEFAULT, "mallinfo2"));
g_malloc_info = CAST_TO_FN_PTR(malloc_info_func_t, dlsym(RTLD_DEFAULT, "malloc_info"));
#endif // __GLIBC__

os::Linux::CPUPerfTicks pticks;
Expand Down Expand Up @@ -5388,6 +5392,13 @@ void os::Linux::get_mallinfo(glibc_mallinfo* out, bool* might_have_wrapped) {
ShouldNotReachHere();
}
}

int os::Linux::malloc_info(FILE* stream) {
if (g_malloc_info == nullptr) {
return -2;
}
return g_malloc_info(0, stream);
}
#endif // __GLIBC__

bool os::trim_native_heap(os::size_change_t* rss_change) {
Expand Down
4 changes: 4 additions & 0 deletions src/hotspot/os/linux/os_linux.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@ class os::Linux {
size_t keepcost;
};
static void get_mallinfo(glibc_mallinfo* out, bool* might_have_wrapped);

// Calls out to GNU extension malloc_info if available
// otherwise does nothing and returns -2.
static int malloc_info(FILE* stream);
#endif // GLIBC
};

Expand Down
2 changes: 2 additions & 0 deletions src/hotspot/share/services/diagnosticCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#include "utilities/macros.hpp"
#ifdef LINUX
#include "trimCHeapDCmd.hpp"
#include "mallocInfoDcmd.hpp"
#endif

static void loadAgentModule(TRAPS) {
Expand Down Expand Up @@ -126,6 +127,7 @@ void DCmdRegistrant::register_dcmds(){
#ifdef LINUX
DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<PerfMapDCmd>(full_export, true, false));
DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<TrimCLibcHeapDCmd>(full_export, true, false));
DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<MallocInfoDcmd>(full_export, true, false));
#endif // LINUX
DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CodeHeapAnalyticsDCmd>(full_export, true, false));

Expand Down
9 changes: 9 additions & 0 deletions src/jdk.jcmd/share/man/jcmd.1
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,15 @@ Stops the remote management agent.
Impact: Low --- no impact
.RE
.TP
\f[V]System.native_heap_info\f[R] (Linux only)
Prints information about native heap usage through malloc_info(3).
.RS
.PP
Impact: Low
.PP
Permission: \f[V]java.lang.management.ManagementPermission(monitor)\f[R]
.RE
.TP
\f[V]System.trim_native_heap\f[R] (Linux only)
Attempts to free up memory by trimming the C-heap.
.RS
Expand Down
57 changes: 57 additions & 0 deletions test/hotspot/jtreg/serviceability/dcmd/vm/MallocInfoTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2023, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import jdk.test.lib.Platform;
import org.testng.annotations.Test;
import jdk.test.lib.dcmd.CommandExecutor;
import jdk.test.lib.dcmd.JMXExecutor;
import jdk.test.lib.process.OutputAnalyzer;

/*
* @test
* @summary Test of diagnostic command System.native_heap_info
* @library /test/lib
* @requires (os.family=="linux")
* @modules java.base/jdk.internal.misc
* java.compiler
* java.management
* jdk.internal.jvmstat/sun.jvmstat.monitor
* @run testng MallocInfoTest
*/
public class MallocInfoTest {
public void run(CommandExecutor executor) {
OutputAnalyzer output = executor.execute("System.native_heap_info");
if (!Platform.isMusl()) {
output.shouldNotContain("Error: ");
output.shouldContain("<malloc version=");
} else {
output.shouldContain("Error: malloc_info(3) not available.");
}
output.reportDiagnosticSummary();
}

@Test
public void jmx() {
run(new JMXExecutor());
}
}

1 comment on commit b5a7426

@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.