Skip to content

Commit

Permalink
Added dummy dcmd for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
schmelter-sap committed Aug 15, 2023
1 parent b11c789 commit 14ad402
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 10 deletions.
63 changes: 54 additions & 9 deletions src/hotspot/os/posix/malloctrace/mallocTrace2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class MallocHooksSafeAllocator {

MallocHooksSafeAllocator::MallocHooksSafeAllocator(size_t allocation_size, real_funcs_t* funcs) :
_funcs(funcs),
_allocation_size(align_up(allocation_size, 16)),
_allocation_size(align_up(allocation_size, 8)), // We need no stricter alignment
_entries_per_chunk(16384),
_chunks(NULL),
_nr_of_chunks(0),
Expand All @@ -138,12 +138,11 @@ MallocHooksSafeAllocator::~MallocHooksSafeAllocator() {
}

void* MallocHooksSafeAllocator::allocate() {
void** as_array = (void**) _free_list[0];
if (_free_list != NULL) {
void* result = _free_list;
_free_list = (void**) ((void**) result)[0];

if (as_array != NULL) {
_free_list = (void**) as_array[0];

return (void*) as_array;
return result;
}

// We need a new chunk.
Expand Down Expand Up @@ -171,9 +170,11 @@ void* MallocHooksSafeAllocator::allocate() {
}

void MallocHooksSafeAllocator::free(void* ptr) {
void** as_array = (void**) ptr;
as_array[0] = (void*) _free_list;
_free_list = as_array;
if (ptr != NULL) {
void** as_array = (void**) ptr;
as_array[0] = (void*) _free_list;
_free_list = as_array;
}
}


Expand Down Expand Up @@ -338,5 +339,49 @@ void MallocStatistic::print(outputStream* st) {
MallocStatisticImpl::print(st);
}


//
//
//
//
// Class MallocStatisticDCmd
//
//
//
//
MallocStatisticDCmd::MallocStatisticDCmd(outputStream* output, bool heap) :
DCmdWithParser(output, heap),
_option("option", "dummy", "STRING", true),
_suboption("suboption", "see option", "STRING", false) {
_dcmdparser.add_dcmd_argument(&_option);
_dcmdparser.add_dcmd_argument(&_suboption);
}

void MallocStatisticDCmd::execute(DCmdSource source, TRAPS) {
// For now just do some tests.
real_funcs_t* funcs = setup_hooks(NULL, _output);

#define MAX_ALLOCS (1024 * 1024)

static void* results[MAX_ALLOCS];

for (int r = 0; r < 10000; ++r) {

for (int i = 0; i < MAX_ALLOCS; ++i) {
results[i] = NULL;
}

MallocHooksSafeAllocator alloc(96, funcs);
for (int i = 0; i < MAX_ALLOCS; ++i) {
results[i] = alloc.allocate();
alloc.free(results[(317 * (int64_t) i) & (MAX_ALLOCS - 1)]);
}
}

MallocStatistic::enable(_output);
MallocStatistic::disable(_output);
_output->print_raw_cr("Test succeeded.");
}

}

35 changes: 35 additions & 0 deletions src/hotspot/os/posix/malloctrace/mallocTrace2.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef OS_POSIX_MALLOCTRACE_MALLOCTRACE_HPP
#define OS_POSIX_MALLOCTRACE_MALLOCTRACE_HPP

#include "services/diagnosticCommand.hpp"
#include "utilities/globalDefinitions.hpp"

class outputStream;
Expand Down Expand Up @@ -30,6 +31,40 @@ class MallocStatistic : public AllStatic {
static void print(outputStream* st);
};


class MallocStatisticDCmd : public DCmdWithParser {
private:

DCmdArgument<char*> _option;
DCmdArgument<char*> _suboption;

public:
static int num_arguments() {
return 2;
}

MallocStatisticDCmd(outputStream* output, bool heap);

static const char* name() {
return "System.mallocstatistic";
}

static const char* description() {
return "Trace malloc call sites";
}

static const char* impact() {
return "Low";
}

static const JavaPermission permission() {
JavaPermission p = { "java.lang.management.ManagementPermission", "control", NULL };
return p;
}

virtual void execute(DCmdSource source, TRAPS);
};

}

#endif
Expand Down
4 changes: 4 additions & 0 deletions src/hotspot/share/services/diagnosticCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@
#include "mallocInfoDcmd.hpp"
// SapMachine 2021-09-01: malloc-trace
#include "malloctrace/mallocTraceDCmd.hpp"
// SapMachine 2023-08-15: malloc trace2
#include "malloctrace/mallocTrace2.hpp"
#endif

// SapMachine 2019-02-20 : vitals
Expand Down Expand Up @@ -139,6 +141,8 @@ void DCmd::register_dcmds(){
DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<MallocInfoDcmd>(full_export, true, false));
// SapMachine 2021-09-01: malloc-trace
DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<sap::MallocTraceDCmd>(full_export, true, false));
// SapMachine 2023-08-15: malloc trace2
DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<sap::MallocStatisticDCmd>(full_export, true, false));
#endif // LINUX
DCmdFactory::register_DCmdFactory(new DCmdFactoryImpl<CodeHeapAnalyticsDCmd>(full_export, true, false));

Expand Down
2 changes: 1 addition & 1 deletion src/java.base/unix/native/libmallochooks/mallochooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// The level of testing (0: none, 1: just debug output, 2: additional normal allocations
// during startup, 3: additional aligned allocations during startup, 4: use fallbacks
// whenever possible
#define TEST_LEVEL 1
#define TEST_LEVEL 0


#if defined(__APPLE__)
Expand Down

0 comments on commit 14ad402

Please sign in to comment.