8 messages in net.java.dev.jna.usersRe: [jna-users] call to sysinfo() cau...
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:Re: [jna-users] call to sysinfo() causes crashActions...
From:Kevin Burton (bur@spinn3r.com)
Date:Apr 7, 2009 9:56:46 am
List:net.java.dev.jna.users

I just wrote an implementation of this recently... this works:

public class sysinfo {

private static SysinfoInterface delegate = (SysinfoInterface)Native.loadLibrary( "c", SysinfoInterface.class);

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

public static SysinfoStructure sysinfo() { SysinfoStructure result = new SysinfoStructure(); delegate.sysinfo( result ); return result; }

}

interface SysinfoInterface extends Library {

void sysinfo(sysinfo.SysinfoStructure sysinfo);

On Tue, Apr 7, 2009 at 8:38 AM, Joel Uckelman <ucke@nomic.net> wrote:

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?