Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 2 Jun 2013 10:33:09 +0800 (CST)
From:      =?utf-8?B?6buE5paH6L6J?= <huanghwh@yahoo.com>
To:        "java@freebsd.org" <java@freebsd.org>
Subject:   System.currentTimeMillis() is totally wrong after call JNA mlockall
Message-ID:  <1370140389.34142.YahooMailNeo@web15306.mail.cnb.yahoo.com>

next in thread | raw e-mail | index | archive | help
Hi,=0AI recently found that Cassandra 1.2.4 could not shutdown when enable =
JNA,=0Aso I try to solve it, and found that under OpenJDK6/FreeBSD,=C2=A0=
=0ASystem.currentTimeMillis() is totally wrong after call JNA mlockall,=0At=
ime seams to not change anymore.=0A=0AThe sample code list here:=0A=0Aimpor=
t java.util.Date;=0A=0Aimport com.sun.jna.LastErrorException;=0Aimport com.=
sun.jna.Native;=0A=0Apublic class Test_mlockall2 {=0A=0A=C2=A0 static boole=
an run =3D true;=0A=C2=A0 private static final int MCL_CURRENT =3D 1;=0A=C2=
=A0 private static final int MCL_FUTURE =3D 2;=0A=0A=C2=A0 static { =C2=A0 =
=C2=A0=0A=C2=A0 =C2=A0 try=0A=C2=A0 =C2=A0 {=0A=C2=A0 =C2=A0 =C2=A0 Native.=
register("c");=0A=C2=A0 =C2=A0 }=0A=C2=A0 =C2=A0 catch (NoClassDefFoundErro=
r e)=0A=C2=A0 =C2=A0 {=0A=C2=A0 =C2=A0 =C2=A0 System.err.println("JNA not f=
ound. Native methods will be disabled.");=0A=C2=A0 =C2=A0 }=0A=C2=A0 =C2=A0=
 catch (UnsatisfiedLinkError e)=0A=C2=A0 =C2=A0 {=0A=C2=A0 =C2=A0 =C2=A0 Sy=
stem.err.println("JNA link failure, one or more native method will be unava=
ilable.");=0A=C2=A0 =C2=A0 =C2=A0 System.err.println("JNA link failure deta=
ils: " + e.getMessage());=0A=C2=A0 =C2=A0 }=0A=C2=A0 =C2=A0 catch (NoSuchMe=
thodError e)=0A=C2=A0 =C2=A0 {=0A=C2=A0 =C2=A0 =C2=A0 System.err.println("O=
bsolete version of JNA present; unable to register C library. Upgrade to JN=
A 3.2.7 or later");=0A=C2=A0 =C2=A0 }=0A=0A=C2=A0 }=0A=0A=C2=A0 private sta=
tic native int mlockall(int flags) throws LastErrorException;=0A=0A=C2=A0 p=
ublic static void tryMlockall()=0A=C2=A0 {=0A=C2=A0 =C2=A0 try=0A=C2=A0 =C2=
=A0 {=0A=C2=A0 =C2=A0 =C2=A0 mlockall(MCL_CURRENT);=0A=C2=A0 =C2=A0 =C2=A0 =
System.err.println("JNA mlockall successful");=0A=C2=A0 =C2=A0 }=0A=C2=A0 =
=C2=A0 catch (UnsatisfiedLinkError e)=0A=C2=A0 =C2=A0 {=0A=C2=A0 =C2=A0 =C2=
=A0 // this will have already been logged by CLibrary, no need to repeat it=
=0A=C2=A0 =C2=A0 }=0A=C2=A0 =C2=A0 catch (RuntimeException e)=0A=C2=A0 =C2=
=A0 {=0A=C2=A0 =C2=A0 =C2=A0 if (!(e instanceof LastErrorException))=0A=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 throw e;=0A=C2=A0 =C2=A0 =C2=A0 System.err.println=
("Unknown mlockall error " + e);=0A=0A=C2=A0 =C2=A0 }=0A=C2=A0 }=0A=0A=C2=
=A0 public static void main(String[] args) throws Exception {=0A=C2=A0 =C2=
=A0 tryMlockall();=0A=C2=A0 =C2=A0 while(run){=0A=C2=A0 =C2=A0 =C2=A0 long =
prev_time =3D System.currentTimeMillis();=0A=C2=A0 =C2=A0 =C2=A0 System.out=
.println("prev_time=3D"+new Date(prev_time));=0A=C2=A0 =C2=A0 =C2=A0 try {=
=0A=C2=A0 =C2=A0 =C2=A0 =C2=A0 Thread.sleep(1000);=0A=C2=A0 =C2=A0 =C2=A0 =
=C2=A0 System.out.println("Sleep 1 second.");=0A=C2=A0 =C2=A0 =C2=A0 }=0A=
=C2=A0 =C2=A0 =C2=A0 catch (InterruptedException e) {=0A=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 e.printStackTrace();=0A=C2=A0 =C2=A0 =C2=A0 }=0A=C2=A0 =C2=A0 =
=C2=A0 long now =3D System.currentTimeMillis();=0A=C2=A0 =C2=A0 =C2=A0 Syst=
em.out.println("now time"+new Date(now));=0A=C2=A0 =C2=A0 =C2=A0 System.out=
.printf("%d, %d, diff=3D%d\n", prev_time, now, now-prev_time);=0A=C2=A0 =C2=
=A0 =C2=A0 if(now-prev_time>=3D1000){=0A=C2=A0 =C2=A0 =C2=A0 =C2=A0 System.=
out.println("Hi.");=0A=C2=A0 =C2=A0 =C2=A0 }=0A=C2=A0 =C2=A0 }=0A=C2=A0 =C2=
=A0 System.err.println("thr1 quit!");=0A=C2=A0 }=0A}=0A=0A=0AThe sample pro=
gram output:=0A=0AJNA mlockall successful=0Aprev_time=3DSat Jun 01 21:12:22=
 CST 2013=0ASleep 1 second.=0Anow timeSat Jun 01 21:12:22 CST 2013=0A137009=
2342096, 1370092342347, diff=3D251=0Aprev_time=3DSat Jun 01 21:12:22 CST 20=
13=0ASleep 1 second.=0Anow timeSat Jun 01 21:12:22 CST 2013=0A1370092342382=
, 1370092342386, diff=3D4=0Aprev_time=3DSat Jun 01 21:12:22 CST 2013=0ASlee=
p 1 second.=0Anow timeSat Jun 01 21:12:22 CST 2013=0A1370092342390, 1370092=
342397, diff=3D7=0Aprev_time=3DSat Jun 01 21:12:22 CST 2013=0ASleep 1 secon=
d.=0Anow timeSat Jun 01 21:12:22 CST 2013=0A1370092342397, 1370092342401, d=
iff=3D4=0Aprev_time=3DSat Jun 01 21:12:22 CST 2013=0A=0A=0Ayou need user ro=
ot to run it or=C2=A0=0Asysctl security.bsd.unprivileged_mlock=3D1=0A=0AAny=
 ideas how to solve it?=0A=0ACheers,=0AHuang Wen Hui=0A



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1370140389.34142.YahooMailNeo>