8 messages in net.java.dev.jna.users[jna-users] call to sysinfo() causes ...
FromSent OnAttachments
Joel UckelmanApr 7, 2009 8:37 am 
Timothy WallApr 7, 2009 8:47 am 
Joel UckelmanApr 7, 2009 9:05 am 
Kevin BurtonApr 7, 2009 9:56 am 
Timothy WallApr 7, 2009 9:58 am 
Joel UckelmanApr 7, 2009 12:19 pm 
Joel UckelmanApr 7, 2009 12:22 pm 
Kevin BurtonApr 7, 2009 1:33 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:[jna-users] call to sysinfo() causes crashActions...
From:Joel Uckelman (ucke@nomic.net)
Date:Apr 7, 2009 8:37:54 am
List:net.java.dev.jna.users

Hi,

I'm trying to call the sysinfo() function from the Linux C library:

int sysinfo(struct sysinfo *info);

struct sysinfo { long uptime; /* Seconds since boot */ unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ unsigned long totalram; /* Total usable main memory size */ unsigned long freeram; /* Available memory size */ unsigned long sharedram; /* Amount of shared memory */ unsigned long bufferram; /* Memory used by buffers */ unsigned long totalswap; /* Total swap space size */ unsigned long freeswap; /* swap space still available */ unsigned short procs; /* Number of current processes */ unsigned long totalhigh; /* Total high memory size */ unsigned long freehigh; /* Available high memory size */ unsigned int mem_unit; /* Memory unit size in bytes */ char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding for libc5 */ };

Here's my test case:

import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.NativeLong; import com.sun.jna.Structure;

public class Sysinfo {

public static interface Libc extends Library { public static final class sysinfo extends Structure { public NativeLong uptime; public NativeLong[] loads = new NativeLong[3]; public NativeLong totalram; public NativeLong freeram; public NativeLong sharedram; public NativeLong bufferram; public NativeLong totalswap; public NativeLong freeswap; public short procs; public NativeLong totalhigh; public NativeLong freehigh; public int mem_unit; public String _f; }

int sysinfo(sysinfo info);

String strerror(int errno);

public static final Libc INSTANCE = (Libc) Native.loadLibrary("c", Libc.class); }

public static void main(String[] args) throws Exception {

long ram_mbytes = -1L;

final Libc.sysinfo info = new Libc.sysinfo(); if (Libc.INSTANCE.sysinfo(info) == 0) { info.read(); ram_mbytes = (info.totalram.longValue() * info.mem_unit) >> 20; } else { final int errno = Native.getLastError(); System.err.println( "Error " + errno + ": " + Libc.INSTANCE.strerror(errno)); }

System.out.println(ram_mbytes + " MB"); } }

About a third of the time when I run this, the JVM crashes at the call to Libc.INSTANCE.sysinfo(). The other two-thirds of the time, I get the correct output. Does anyone see what I'm doing wrong here?