Skip to main content

My cvm can't create two cvm ,any body can tell why ?

11 replies [Last post]
jy870052
Offline
Joined: 2007-04-01
Points: 0

Hello :

when i use JNI_CreateJavaVM(&get_jvm,(void**)&get_env,&get_vm_args)

in main function which is a c progarm ,cvm can work !

but when i create a other cvm in thread ,cvm is crash !

the cvm dbug print

Assertion failed at line 187 in ../../src/linux/javavm/runtime/memory_md.c: memGrainSize == 0

i use gdb dbug :

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread -686134352 (LWP 26560)]
0x080880fe in CVMstackwalkInit (s=0x38, c=0xd71a5f0c) at ../../src/share/javavm/runtime/stackwalk.c:42
42 ../../src/share/javavm/runtime/stackwalk.c: can't find this file
in ../../src/share/javavm/runtime/stackwalk.c

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
xyzzy
Offline
Joined: 2006-08-30
Points: 0

Yanghui, the pthread_getattr_np crash has been fixed now in revision 5512. Thanks for reporting this.

Dean

jy870052
Offline
Joined: 2007-04-01
Points: 0

Hi Dean :
Thank you for answer my question !!

Thank you again !

he he
yanghui

xyzzy
Offline
Joined: 2006-08-30
Points: 0

> Hi Dean :
> Thank you for answer my question !!
> Thank you again !
>
> he he
>
> anghui

You're welcome. I hope this solved the problem you were having.

Dean

mlam
Offline
Joined: 2006-10-13
Points: 0

yanghui,

Why are you trying to create the VM in 2 threads? Perhaps what you really want is to get a JNI env for the other native thread. To do that, you need to use JNI's AttachCurrentThread(). Alternatively, you can create Java threads by instantiating instances of java.lang.Thread from a Java program that you run in the VM.

Regards,

-- Mark

jy870052
Offline
Joined: 2007-04-01
Points: 0

Mark and Chris :

i was compiled my cvm like this :

make CVM_MTASK=true CVM_SYMBOLS=true

my c program :

=========================================================
#include
#include
#include
#include
#include

#define TWO_VM

void* run_JavaVM(void *arg);
JNIEnv* get_JavaVM(JavaVM **get_jvm);

typedef struct ThreadArg{
JNIEnv *env;
char* pClassname;
};

int main()
{
pthread_t pid;
JavaVM *get_jvm = NULL;

JNIEnv* tmp_env = get_JavaVM(&get_jvm);

ThreadArg tmp;
ThreadArg *pThreadArg = &tmp;

pThreadArg->env = NULL;
pThreadArg->pClassname = NULL;

pThreadArg->pClassname = "A";
printf("get_jvm value is 0x%x \n",get_jvm);
int ret = pthread_create(&pid, NULL,run_JavaVM, (void*)get_jvm);

if(ret!=0)
{
printf ("Create pthread error!\n");

}

while(1)
{
printf("I'm in main function !\n ");
sleep(5);
}

}

void* run_JavaVM(void *transarg)
{

JNIEnv *pEnv;
//JavaVMInitArgs vm_args;
//jint res;
jclass cls;
jmethodID mid;
jstring jstr;
jobjectArray args;
void *pTmp;
//extern JavaVM *get_jvm;
//JavaVM *jvm = NULL;
printf(" now is in run_JavaVM !!\n");
printf(" now is in run_JavaVM !!\n");
printf(" now is in run_JavaVM !!\n");
printf(" now is in run_JavaVM !!\n");

JavaVM *jvm ;
printf("jvm value is 0x%x \n",jvm);
printf("transarg value is 0x%x \n",transarg);
jvm = (JavaVM *)transarg;

printf("jvm value is 0x%x \n",jvm);

printf(" now is in run_JavaVM !!\n");
sleep(1);

[b]/* cvm was crashed here */ [/b]
/***************************************************************************/
int rtn = (&jvm)->AttachCurrentThread((void **)&pEnv, 0);
/**************************************************************************/

if (JNI_OK == rtn)
{
printf("env is return now !\n");
//pEnv = (JNIEnv*)pTmp;
}
else
{
if (JNI_EVERSION == rtn)
printf("this version of vm is not support !\n");
}

cls = pEnv->FindClass("A");
if (cls == 0) {
printf("Can't find A class !! run_JavaVM \n");
goto destroy;
}

mid = pEnv->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
if (mid == 0) {
printf("Can't find TestThread.main !!\n");
goto destroy;
}
jstr = pEnv->NewStringUTF(" from C++!");
if (jstr == 0) {
printf("Out of memory !!\n");
goto destroy;
}
args = (jobjectArray)pEnv->NewObjectArray( 1, pEnv->FindClass("java/lang/String"), jstr);
if (args == 0) {
printf("Out of memory !!\n");
goto destroy;
}
pEnv->CallStaticVoidMethod( cls, mid, args);

while(1)
{
printf("now is in main\n");
sleep(2);
}

destroy:
if (pEnv->ExceptionOccurred()) {
pEnv->ExceptionDescribe();
}
jvm->DestroyJavaVM();

}

JNIEnv* get_JavaVM(JavaVM **get_jvm)
{
JNIEnv *get_env;
JavaVMInitArgs get_vm_args;
JavaVMOption options[4];
jint get_res;
jclass get_cls;
jmethodID get_mid;
jstring get_jstr;
jobjectArray get_args;

printf("create thread is success now !\n");

options[0].optionString = "-Xms500M";
options[1].optionString = "-Xmx16M";
options[2].optionString = "-Xss64K";
//options[3].optionString = "-Xoss50K";
options[3].optionString = "-Djava.class.path=./";

get_vm_args.version = JNI_VERSION_1_2;
get_vm_args.options = options;
get_vm_args.nOptions = 4;
get_vm_args.ignoreUnrecognized = JNI_FALSE;

/* Create the Java VM */
get_res = JNI_CreateJavaVM(get_jvm,(void**)&get_env,&get_vm_args);
if (get_res < 0) {
printf("Can't create Java VM !! get_ \n");
//return 1;
}
get_cls = get_env->FindClass("A");
if (get_cls == 0) {
printf("Can't find A class !! get_ \n");
//return 1;
}
get_mid = get_env->GetStaticMethodID(get_cls, "main", "([Ljava/lang/String;)V");
if (get_mid == 0) {
printf("Can't find TestThread.main !! get_ \n");
//return 1;
}
get_jstr = get_env->NewStringUTF(" from C++!");
if (get_jstr == 0) {
printf("Out of memory !! get_ \n");
//return 1;
}
get_args = (jobjectArray)get_env->NewObjectArray( 1, get_env->FindClass("java/lang/String"), get_jstr);
if (get_args == 0) {
printf("Out of memory !! get_ \n");
//return 1;
}
get_env->CallStaticVoidMethod( get_cls, get_mid, get_args);

printf(" get_jvm value is 0x%x \n",get_jvm);

[b] /* cvm is work fine here */[/b]
/*******************************************************************************/
int rtn = (*get_jvm)->AttachCurrentThread((void **)&get_env, 0);
/*******************************************************************************/

if (JNI_OK == rtn)
{
printf("env is return now !\n");
//pEnv = (JNIEnv*)pTmp;
}
else
{
if (JNI_EVERSION == rtn)
printf("this version of vm is not support !\n");
}

return get_env;

}

i used gdb dbug , backtrace displayed :

#0 0x004cf97b in pthread_getattr_np () from /lib/tls/libpthread.so.0
#1 0x08066505 in CVMthreadAttach (self=0x81f7e10, orphan=0) at ../../src/linux/javavm/runtime/threads_md.c:146
#2 0x0808efef in CVMattachExecEnv (ee=0x81f7d70, orphan=0) at ../../src/share/javavm/runtime/interpreter.c:480
#3 0x08055aaa in CVMjniAttachCurrentThread (vm=0x81e6054, penv=0xd71a6448, _args=0x0)
at ../../src/share/javavm/runtime/jni_impl.c:4498
#4 0x0804a6f2 in run_JavaVM (transarg=0x81e6054) at jni.h:1939
#5 0x004ce1d5 in start_thread () from /lib/tls/libpthread.so.0
#6 0x0035f2da in clone () from /lib/tls/libc.so.6

Regards

yanghui

Message was edited by: jy870052

cjplummer
Offline
Joined: 2006-10-16
Points: 0

Hello Yanghui,

How did you manage to get both of the calls to AttachCurrentThread to compile:

case #1:
JNIEnv* get_JavaVM(JavaVM **get_jvm)
int rtn = (*get_jvm)->AttachCurrentThread((void **)&get_env, 0);

case #2
JavaVM *jvm ;
int rtn = (&jvm)->AttachCurrentThread((void **)&pEnv, 0);

In case #1 you are using a JavaVM* (*get_jvm). In case #2 you are using a JavaVM** (&jvm). I don't see how both of these can work.

Chris

cjplummer
Offline
Joined: 2006-10-16
Points: 0

You can only have one created VM instance at a time with JNI_CreateJavaVM. The VM has global and static data that cannot be shared if you attempt to create multiple instance with JNI_CreateJavaVM. This is why you are hitting the assert. It sees that memGrainSize has already been initialized.

Chris

jy870052
Offline
Joined: 2007-04-01
Points: 0

thanks Chris !

i will use cvm in two different thread ,if cvm can only create one ,how can i solve this problem ?

i was tried to sent the pointer -- env to other thread , but it could't found the class

cjplummer
Offline
Joined: 2006-10-16
Points: 0

> thanks Chris !
>
> i will use cvm in two different thread ,if cvm can
> only create one ,how can i solve this problem ?
>
> i was tried to sent the pointer -- env to other
> thread , but it could't found the class

This won't work. You can only have one VM instance per process, not per thread. You may want to consider the MTASK support in MR1 or MR2. You can find MR1 help here:

https://phoneme.dev.java.net/content/phoneme_advanced_guide.html

Look for references in the build guide that talk about building with CVM_MTASK=true, and references in the runtime guide that tell you how to run cvmc. Chapter 12 in the porting guide also talks about mtask and ams support.

For MR2, things have changed around a lot and are not well documented, so for now you would be better off sticking with MR1.

Chris

jy870052
Offline
Joined: 2007-04-01
Points: 0

Hi :

when i set CVM_DEBUG = false

gdb print

Program received signal SIGSEGV, Segmentation fault.
0x004cf97b in pthread_getattr_np () from /lib/tls/libpthread.so.0
(gdb) bt
#0 0x004cf97b in pthread_getattr_np () from /lib/tls/libpthread.so.0
#1 0x08066475 in CVMthreadAttach (self=0x81e3fb8, orphan=0) at ../../src/linux/javavm/runtime/threads_md.c:146
#2 0x0808eb9f in CVMattachExecEnv (ee=0x81e3f18, orphan=0) at ../../src/share/javavm/runtime/interpreter.c:480
#3 0x0808bfab in CVMinitVMGlobalState (gs=0x81e3ba0, options=0xd71a6370) at ../../src/share/javavm/runtime/globals.c:845
#4 0x08054d8a in JNI_CreateJavaVM (p_jvm=0x0, p_env=0x0, args=0xd71a6430) at ../../src/share/javavm/runtime/jni_impl.c:4024
#5 0x0804a748 in run_JavaVM (transarg=0xfefff868) at main_thread.c:100
#6 0x004ce1d5 in start_thread () from /lib/tls/libpthread.so.0
#7 0x0035f2da in clone () from /lib/tls/libc.so.6

i was printed the attr which is in result = (*pthreadGetAttr)(tid, &attr);

the result is :

(1) only one cvm , it's work fine

(gdb) print attr
$3 = {__detachstate = 0, __schedpolicy = -685820984, __schedparam = {__sched_priority = 0}, __inheritsched = 0,
__scope = 0, __guardsize = 0, __stackaddr_set = 0, __stackaddr = 0x0, __stacksize = 0}
(gdb) c
Continuing.
[New Thread -685995088 (LWP 15992)]
[Switching to Thread -685995088 (LWP 15992)]

Breakpoint 1, CVMthreadAttach (self=0x81f41b8, orphan=136266168) at ../../src/linux/javavm/runtime/threads_md.c:146
146 result = (*pthreadGetAttr)(tid, &attr);
(gdb) print attr
$4 = {__detachstate = 0, __schedpolicy = 0, __schedparam = {__sched_priority = 0}, __inheritsched = 0, __scope = 0,
__guardsize = 0, __stackaddr_set = 0, __stackaddr = 0x0, __stacksize = 0}
(gdb) c
Continuing.
[New Thread -686064720 (LWP 15993)]
[Switching to Thread -686064720 (LWP 15993)]

Breakpoint 1, CVMthreadAttach (self=0x81fe8b8, orphan=136308920) at ../../src/linux/javavm/runtime/threads_md.c:146
146 result = (*pthreadGetAttr)(tid, &attr);
(gdb) print attr
$5 = {__detachstate = 0, __schedpolicy = 0, __schedparam = {__sched_priority = 0}, __inheritsched = 0, __scope = 0,
__guardsize = 0, __stackaddr_set = 0, __stackaddr = 0x0, __stacksize = 0}
(gdb) c
Continuing.

(2) when i run two cvm ,cvm was crashed

frist cvm : it's work fine

(gdb) print attr
$1 = {__detachstate = 0, __schedpolicy = -685820984, __schedparam = {__sched_priority = 0}, __inheritsched = 0,
__scope = 0, __guardsize = 0, __stackaddr_set = 0, __stackaddr = 0x0, __stacksize = 0}
(gdb) c
Continuing.
[New Thread -685995088 (LWP 16033)]
[Switching to Thread -685995088 (LWP 16033)]

Breakpoint 1, CVMthreadAttach (self=0x81f41b8, orphan=136266168) at ../../src/linux/javavm/runtime/threads_md.c:146
146 result = (*pthreadGetAttr)(tid, &attr);
(gdb) print attr
$2 = {__detachstate = 0, __schedpolicy = 0, __schedparam = {__sched_priority = 0}, __inheritsched = 0, __scope = 0,
__guardsize = 0, __stackaddr_set = 0, __stackaddr = 0x0, __stacksize = 0}
(gdb) c
Continuing.
[New Thread -686064720 (LWP 16034)]
[Switching to Thread -686064720 (LWP 16034)]

Breakpoint 1, CVMthreadAttach (self=0x81fe8b8, orphan=136308920) at ../../src/linux/javavm/runtime/threads_md.c:146
146 result = (*pthreadGetAttr)(tid, &attr);
(gdb) print attr
$3 = {__detachstate = 0, __schedpolicy = 0, __schedparam = {__sched_priority = 0}, __inheritsched = 0, __scope = 0,
__guardsize = 0, __stackaddr_set = 0, __stackaddr = 0x0, __stacksize = 0}

create the second cvm ,crashed

(gdb) print attr
$4 = {__detachstate = 3, __schedpolicy = 136281680, __schedparam = {__sched_priority = -686136952},
__inheritsched = 134576917, __scope = 1, __guardsize = 24, __stackaddr_set = 0, __stackaddr = 0x0, __stacksize = 0}

Program received signal SIGSEGV, Segmentation fault.
0x004cf97b in pthread_getattr_np () from /lib/tls/libpthread.so.0
(gdb) bt
#0 0x004cf97b in pthread_getattr_np () from /lib/tls/libpthread.so.0
#1 0x08066475 in CVMthreadAttach (self=0x81e3fb8, orphan=0) at ../../src/linux/javavm/runtime/threads_md.c:146
#2 0x0808eb9f in CVMattachExecEnv (ee=0x81e3f18, orphan=0) at ../../src/share/javavm/runtime/interpreter.c:480
#3 0x0808bfab in CVMinitVMGlobalState (gs=0x81e3ba0, options=0xd71a6370) at ../../src/share/javavm/runtime/globals.c:845
#4 0x08054d8a in JNI_CreateJavaVM (p_jvm=0x0, p_env=0x0, args=0xd71a6430) at ../../src/share/javavm/runtime/jni_impl.c:4024
#5 0x0804a748 in run_JavaVM (transarg=0xfefff868) at main_thread.c:100
#6 0x004ce1d5 in start_thread () from /lib/tls/libpthread.so.0
#7 0x0035f2da in clone () from /lib/tls/libc.so.6

why ?????????????????????????????????

thanks

yanghui

jy870052
Offline
Joined: 2007-04-01
Points: 0

(gdb) bt
#0 0x080880fe in CVMstackwalkInit (s=0x38, c=0xd71a5f0c) at ../../src/share/javavm/runtime/stackwalk.c:42
#1 0x08087764 in CVMdumpFrames (s=0x38, verbose=0, includeData=0, frameLimit=0)
at ../../src/share/javavm/runtime/stacks.c:276
#2 0x08087897 in CVMdumpStack (s=0x38, verbose=0, includeData=0, frameLimit=0)
at ../../src/share/javavm/runtime/stacks.c:312
#3 0x080f36cf in CVMassertHook (filename=0x82f1df4 "../../src/linux/javavm/runtime/memory_md.c", lineno=187,
expr=0x82f1de0 "memGrainSize == 0") at ../../src/share/javavm/runtime/interpreter.c:3440
#4 0x0810fc98 in CVMmemInit () at ../../src/linux/javavm/runtime/memory_md.c:187
#5 0x080e3abb in CVMinitVMGlobalState (gs=0x833ab40, options=0xd71a6328) at ../../src/share/javavm/runtime/globals.c:725
#6 0x08064ae5 in JNI_CreateJavaVM (p_jvm=0xd71a6448, p_env=0xd71a644c, args=0xd71a6430)
at ../../src/share/javavm/runtime/jni_impl.c:4024
#7 0x0804aa3e in run_JavaVM (transarg=0xfefff8b8) at main_thread.c:98
#8 0x004ce1d5 in start_thread () from /lib/tls/libpthread.so.0
#9 0x0035f2da in clone () from /lib/tls/libc.so.6

yanghui