Skip to content

Commit

Permalink
8286441: Remove mode parameter from jdk.internal.perf.Perf.attach()
Browse files Browse the repository at this point in the history
Reviewed-by: redestad, alanb
  • Loading branch information
iklam committed May 11, 2022
1 parent 46a775a commit fcf49f4
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 202 deletions.
30 changes: 5 additions & 25 deletions src/hotspot/os/posix/perfMemory_posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1097,39 +1097,19 @@ static size_t sharedmem_filesize(int fd, TRAPS) {

// attach to a named shared memory region.
//
static void mmap_attach_shared(const char* user, int vmid, PerfMemory::PerfMemoryMode mode, char** addr, size_t* sizep, TRAPS) {
static void mmap_attach_shared(const char* user, int vmid, char** addr, size_t* sizep, TRAPS) {

char* mapAddress;
int result;
int fd;
size_t size = 0;
const char* luser = NULL;

int mmap_prot;
int file_flags;
int mmap_prot = PROT_READ;
int file_flags = O_RDONLY | O_NOFOLLOW;

ResourceMark rm;

// map the high level access mode to the appropriate permission
// constructs for the file and the shared memory mapping.
if (mode == PerfMemory::PERF_MODE_RO) {
mmap_prot = PROT_READ;
file_flags = O_RDONLY | O_NOFOLLOW;
}
else if (mode == PerfMemory::PERF_MODE_RW) {
#ifdef LATER
mmap_prot = PROT_READ | PROT_WRITE;
file_flags = O_RDWR | O_NOFOLLOW;
#else
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
"Unsupported access mode");
#endif
}
else {
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
"Illegal access mode");
}

// for linux, determine if vmid is for a containerized process
int nspid = LINUX_ONLY(os::Linux::get_namespace_pid(vmid)) NOT_LINUX(-1);

Expand Down Expand Up @@ -1288,15 +1268,15 @@ void PerfMemory::delete_memory_region() {
// the indicated process's PerfData memory region into this JVMs
// address space.
//
void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode, char** addrp, size_t* sizep, TRAPS) {
void PerfMemory::attach(const char* user, int vmid, char** addrp, size_t* sizep, TRAPS) {

if (vmid == 0 || vmid == os::current_process_id()) {
*addrp = start();
*sizep = capacity();
return;
}

mmap_attach_shared(user, vmid, mode, addrp, sizep, CHECK);
mmap_attach_shared(user, vmid, addrp, sizep, CHECK);
}

// detach from the PerfData memory region of another JVM
Expand Down
29 changes: 5 additions & 24 deletions src/hotspot/os/windows/perfMemory_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1573,36 +1573,17 @@ static size_t sharedmem_filesize(const char* filename, TRAPS) {
// this method opens a file mapping object and maps the object
// into the address space of the process
//
static void open_file_mapping(const char* user, int vmid,
PerfMemory::PerfMemoryMode mode,
char** addrp, size_t* sizep, TRAPS) {
static void open_file_mapping(const char* user, int vmid, char** addrp, size_t* sizep, TRAPS) {

ResourceMark rm;

void *mapAddress = 0;
size_t size = 0;
HANDLE fmh;
DWORD ofm_access;
DWORD mv_access;
DWORD ofm_access = FILE_MAP_READ;
DWORD mv_access = FILE_MAP_READ;
const char* luser = NULL;

if (mode == PerfMemory::PERF_MODE_RO) {
ofm_access = FILE_MAP_READ;
mv_access = FILE_MAP_READ;
}
else if (mode == PerfMemory::PERF_MODE_RW) {
#ifdef LATER
ofm_access = FILE_MAP_READ | FILE_MAP_WRITE;
mv_access = FILE_MAP_READ | FILE_MAP_WRITE;
#else
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
"Unsupported access mode");
#endif
}
else {
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
"Illegal access mode");
}

// if a user name wasn't specified, then find the user name for
// the owner of the target vm.
Expand Down Expand Up @@ -1796,7 +1777,7 @@ void PerfMemory::delete_memory_region() {
// the indicated process's PerfData memory region into this JVMs
// address space.
//
void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode,
void PerfMemory::attach(const char* user, int vmid,
char** addrp, size_t* sizep, TRAPS) {

if (vmid == 0 || vmid == os::current_process_id()) {
Expand All @@ -1805,7 +1786,7 @@ void PerfMemory::attach(const char* user, int vmid, PerfMemoryMode mode,
return;
}

open_file_mapping(user, vmid, mode, addrp, sizep, CHECK);
open_file_mapping(user, vmid, addrp, sizep, CHECK);
}

// detach from the PerfData memory region of another JVM
Expand Down
14 changes: 4 additions & 10 deletions src/hotspot/share/prims/perf.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2022, 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 Down Expand Up @@ -64,7 +64,7 @@ static char* jstr_to_utf(JNIEnv *env, jstring str, TRAPS) {
return utfstr;
}

PERF_ENTRY(jobject, Perf_Attach(JNIEnv *env, jobject unused, jstring user, int vmid, int mode))
PERF_ENTRY(jobject, Perf_Attach(JNIEnv *env, jobject unused, jstring user, int vmid))

PerfWrapper("Perf_Attach");

Expand All @@ -80,14 +80,8 @@ PERF_ENTRY(jobject, Perf_Attach(JNIEnv *env, jobject unused, jstring user, int v
user_utf = user == NULL ? NULL : jstr_to_utf(env, user, CHECK_NULL);
}

if (mode != PerfMemory::PERF_MODE_RO &&
mode != PerfMemory::PERF_MODE_RW) {
THROW_0(vmSymbols::java_lang_IllegalArgumentException());
}

// attach to the PerfData memory region for the specified VM
PerfMemory::attach(user_utf, vmid, (PerfMemory::PerfMemoryMode) mode,
&address, &capacity, CHECK_NULL);
PerfMemory::attach(user_utf, vmid, &address, &capacity, CHECK_NULL);

{
ThreadToNativeFromVM ttnfv(thread);
Expand Down Expand Up @@ -300,7 +294,7 @@ PERF_END

static JNINativeMethod perfmethods[] = {

{CC "attach", CC "(" JLS "II)" BB, FN_PTR(Perf_Attach)},
{CC "attach0", CC "(" JLS "I)" BB, FN_PTR(Perf_Attach)},
{CC "detach", CC "(" BB ")V", FN_PTR(Perf_Detach)},
{CC "createLong", CL_ARGS, FN_PTR(Perf_CreateLong)},
{CC "createByteArray", CBA_ARGS, FN_PTR(Perf_CreateByteArray)},
Expand Down
10 changes: 2 additions & 8 deletions src/hotspot/share/runtime/perfMemory.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2022, 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 Down Expand Up @@ -128,11 +128,6 @@ class PerfMemory : AllStatic {
static void delete_memory_region();

public:
enum PerfMemoryMode {
PERF_MODE_RO = 0,
PERF_MODE_RW = 1
};

static char* alloc(size_t size);
static char* start() { return _start; }
static char* end() { return _end; }
Expand All @@ -148,8 +143,7 @@ class PerfMemory : AllStatic {

// methods for attaching to and detaching from the PerfData
// memory segment of another JVM process on the same system.
static void attach(const char* user, int vmid, PerfMemoryMode mode,
char** addrp, size_t* size, TRAPS);
static void attach(const char* user, int vmid, char** addrp, size_t* size, TRAPS);
static void detach(char* addr, size_t bytes);

static void initialize();
Expand Down
62 changes: 13 additions & 49 deletions src/java.base/share/classes/jdk/internal/perf/Perf.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 2022, 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 Down Expand Up @@ -56,9 +56,6 @@ public final class Perf {

private static Perf instance;

private static final int PERF_MODE_RO = 0;
private static final int PERF_MODE_RW = 1;

private Perf() { } // prevent instantiation

/**
Expand Down Expand Up @@ -171,50 +168,28 @@ public static Perf getPerf()
* for the Java virtual machine running this method, then the returned
* <code>ByteBuffer</code> object will always be coherent and dynamically
* changing.
* <p>
* The attach mode specifies the access permissions requested for the
* instrumentation buffer of the target virtual machine. The permitted
* access permissions are:
* <ul>
* <li>"r" - Read only access. This Java virtual machine has only
* read access to the instrumentation buffer for the target Java
* virtual machine.
* <li>"rw" - Read/Write access. This Java virtual machine has read and
* write access to the instrumentation buffer for the target Java virtual
* machine. This mode is currently not supported and is reserved for
* future enhancements.
* </ul>
*
* @param lvmid an integer that uniquely identifies the
* target local Java virtual machine.
* @param mode a string indicating the attach mode.
* @return ByteBuffer a direct allocated byte buffer
* @throws IllegalArgumentException The lvmid or mode was invalid.
* @throws IllegalArgumentException The lvmid was invalid.
* @throws IOException An I/O error occurred while trying to acquire
* the instrumentation buffer.
* @throws OutOfMemoryError The instrumentation buffer could not be mapped
* into the virtual machine's address space.
* @see java.nio.ByteBuffer
*/
public ByteBuffer attach(int lvmid, String mode)
public ByteBuffer attach(int lvmid)
throws IllegalArgumentException, IOException
{
if (mode.compareTo("r") == 0) {
return attachImpl(null, lvmid, PERF_MODE_RO);
}
else if (mode.compareTo("rw") == 0) {
return attachImpl(null, lvmid, PERF_MODE_RW);
}
else {
throw new IllegalArgumentException("unknown mode");
}
return attachImpl(null, lvmid);
}

/**
* Attach to the instrumentation buffer for the specified Java virtual
* machine owned by the given user.
* <p>
* This method behaves just as the <code>attach(int lvmid, String mode)
* This method behaves just as the <code>attach(int lvmid)
* </code> method, except that it only searches for Java virtual machines
* owned by the specified user.
*
Expand All @@ -223,27 +198,18 @@ else if (mode.compareTo("rw") == 0) {
* virtual machine.
* @param lvmid an integer that uniquely identifies the
* target local Java virtual machine.
* @param mode a string indicating the attach mode.
* @return ByteBuffer a direct allocated byte buffer
* @throws IllegalArgumentException The lvmid or mode was invalid.
* @throws IllegalArgumentException The lvmid was invalid.
* @throws IOException An I/O error occurred while trying to acquire
* the instrumentation buffer.
* @throws OutOfMemoryError The instrumentation buffer could not be mapped
* into the virtual machine's address space.
* @see java.nio.ByteBuffer
*/
public ByteBuffer attach(String user, int lvmid, String mode)
public ByteBuffer attach(String user, int lvmid)
throws IllegalArgumentException, IOException
{
if (mode.compareTo("r") == 0) {
return attachImpl(user, lvmid, PERF_MODE_RO);
}
else if (mode.compareTo("rw") == 0) {
return attachImpl(user, lvmid, PERF_MODE_RW);
}
else {
throw new IllegalArgumentException("unknown mode");
}
return attachImpl(user, lvmid);
}

/**
Expand All @@ -259,18 +225,17 @@ else if (mode.compareTo("rw") == 0) {
* virtual machine.
* @param lvmid an integer that uniquely identifies the
* target local Java virtual machine.
* @param mode a string indicating the attach mode.
* @return ByteBuffer a direct allocated byte buffer
* @throws IllegalArgumentException The lvmid or mode was invalid.
* @throws IllegalArgumentException The lvmid was invalid.
* @throws IOException An I/O error occurred while trying to acquire
* the instrumentation buffer.
* @throws OutOfMemoryError The instrumentation buffer could not be mapped
* into the virtual machine's address space.
*/
private ByteBuffer attachImpl(String user, int lvmid, int mode)
private ByteBuffer attachImpl(String user, int lvmid)
throws IllegalArgumentException, IOException
{
final ByteBuffer b = attach(user, lvmid, mode);
final ByteBuffer b = attach0(user, lvmid);

if (lvmid == 0) {
// The native instrumentation buffer for this Java virtual
Expand Down Expand Up @@ -326,15 +291,14 @@ public void run() {
* virtual machine.
* @param lvmid an integer that uniquely identifies the
* target local Java virtual machine.
* @param mode a string indicating the attach mode.
* @return ByteBuffer a direct allocated byte buffer
* @throws IllegalArgumentException The lvmid or mode was invalid.
* @throws IllegalArgumentException The lvmid was invalid.
* @throws IOException An I/O error occurred while trying to acquire
* the instrumentation buffer.
* @throws OutOfMemoryError The instrumentation buffer could not be mapped
* into the virtual machine's address space.
*/
private native ByteBuffer attach(String user, int lvmid, int mode)
private native ByteBuffer attach0(String user, int lvmid)
throws IllegalArgumentException, IOException;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2022, 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 Down Expand Up @@ -258,7 +258,7 @@ private synchronized PerfInstrumentation getPerfInstrumentation() {
@SuppressWarnings("removal")
Perf perf = AccessController.doPrivileged(new Perf.GetPerfAction());
try {
ByteBuffer bb = perf.attach(0, "r");
ByteBuffer bb = perf.attach(0);
if (bb.capacity() == 0) {
noPerfData = true;
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2004, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2004, 2022, 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 Down Expand Up @@ -66,10 +66,7 @@
* (protocol) dependent. These components are ignored by the
* default local protocol, <em>local:</em>. For the default remote
* protocol, <em>rmi</em>, the Path component is interpreted as
* the name of the RMI remote object. The Query component may
* contain an access mode specifier <em>?mode=</em> specifying
* <em>"r"</em> or <em>"rw"</em> access (write access currently
* ignored). The Fragment part is ignored.
* the name of the RMI remote object. The Fragment part is ignored.
* </p></li>
* </ul>
* <p>
Expand Down Expand Up @@ -497,26 +494,6 @@ public String getFragment() {
return uri.getFragment();
}

/**
* Return the mode indicated in this HostIdentifier.
*
* @return String - the mode string. If no mode is specified, then "r"
* is returned. otherwise, the specified mode is returned.
*/
public String getMode() {
String query = getQuery();
if (query != null) {
String[] queryArgs = query.split("\\+");
for (String queryArg : queryArgs) {
if (queryArg.startsWith("mode=")) {
int index = queryArg.indexOf('=');
return queryArg.substring(index + 1);
}
}
}
return "r";
}

/**
* Return the URI associated with the HostIdentifier.
*
Expand Down
Loading

1 comment on commit fcf49f4

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