RFR: JDK-8309462: vmTestbase/nsk/jvmti/RunAgentThread/agentthr001/TestDescription.java crashing due to empty while loop
Christoph Langer
clanger at openjdk.org
Tue Jun 6 21:54:00 UTC 2023
On Tue, 6 Jun 2023 20:43:36 GMT, Martin Doerr <mdoerr at openjdk.org> wrote:
>> The sys_thread_3() function contains an empty while loop, which by the standard can be optimized away. Please refer to discussion in https://github.com/llvm/llvm-project/issues/60622
>> The xlc17 compiler is doing so, and IBM claims that they are following the standard and will not fix this on compiler side.
>> So we have (at least) 3 ways to circumvent this behavior.
>>
>> 1. we can introduce the call of a nop library function, which will hinder the optimizer to throw away the loop (This is our proposed solution, but instead of a heavy looping thread, the result is a more or less idle thread):
>> `#include <unistd.h>`
>> `static void`
>> `sys_thread_3(jvmtiEnv* jvmti, JNIEnv* jni, void *p)`
>> `{`
>> ` while (1) {`
>> ` sleep(1);`
>> ` }`
>> `}`
>>
>> 2. We can make use of a volatile variable in the loop body which also hinders the optimizer to throw away the loop:
>> `static void`
>> `sys_thread_3(jvmtiEnv* jvmti, JNIEnv* jni, void *p)`
>> `{`
>> ` volatile int i = 1;`
>> ` while (i) {`
>> ` i += 2;`
>> ` }`
>> `}`
>>
>> 3. we can use the __attribute__ ((optnone)) modifier in the function declaration to suppress the optimization at all:
>> `static void`
>> `sys_thread_3(jvmtiEnv* jvmti, JNIEnv* jni, void *p) __attribute__ ((optnone))`
>> `{`
>> ` while (1) {`
>> ` }`
>> `}`
>>
>> To make the third approach platform independent, we can implement it in the following way:
>> In globalDefinitions.hpp
>> `#ifndef OPTNONE`
>> `#define OPTNONE`
>> `#endif`
>>
>> In globalDefinitions_xlc.hpp
>> `// optnone support`
>> `//`
>> `// To use if a function should not be optimized`
>> `// Usage:`
>> `// void* func(size_t size) OPTNONE {...}`
>> `#define OPTNONE __attribute__(( optnone))`
>>
>> With this we can change libagentthr001.cpp in a platform independent way to
>> `static void`
>> `sys_thread_3(jvmtiEnv* jvmti, JNIEnv* jni, void *p) OPTNONE`
>> `{`
>> ` while (1) {`
>> ` }`
>> `}`
>
> test/hotspot/jtreg/vmTestbase/nsk/jvmti/RunAgentThread/agentthr001/agentthr001.cpp line 26:
>
>> 24: #include <stdio.h>
>> 25: #include <string.h>
>> 26: #include <unistd.h>
>
> Breaks Windows build: agentthr001.cpp(26): fatal error C1083: Cannot open include file: 'unistd.h': No such file or directory
On Windows I think you need windows.h. I guess you can do it like here: https://github.com/openjdk/jdk/blob/4a75fd462c002a209201d8bfc8d6c9eb286a7444/src/java.base/share/native/libjli/wildcard.c#L99
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/14330#discussion_r1220410431
More information about the serviceability-dev
mailing list