如何在/ proc / swaps中获取Linux中的交换大小?

时间:2012-03-23 20:07:50

标签: java linux jsf

我想开发JSF页面,如果操作系统是Linux并且使用了多少百分比,则可以显示交换大小。我发现这些信息可以从/ proc / swaps中读取。但是当我打开它时,我得到了这个

Filename                Type        Size    Used    Priority
/dev/sda2                               partition   2047992 0   -1

我如何才能获得价值 - 2047992和0?我知道如何阅读文本文件的内容。例如:

try {
    BufferedReader in = new BufferedReader(new FileReader("infilename"));
    String str;
    while ((str = in.readLine()) != null) {
        process(str);
    }
    in.close();
} catch (IOException e) {
}

另一个更高级的问题是,如果我有两个互换?

此致

2 个答案:

答案 0 :(得分:3)

(特定于Linux的)系统调用sysinfo(struct sysinfo* info)使用以下内容填充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 */
};

其中totalswapfreeswap可能就是你所追求的。

我不知道如何在Java中进行本机平台调用,但这是自己解析/proc/swaps文件的一个很好的替代方法。

编辑:

我和JNA玩了一下,想出了这个:

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

public class Test {


    public interface CStdLib extends Library {
        static 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;
            /* some padding? */
        }
        int sysinfo(SysInfo info);
    }

    public static void main(String[] args) {
        CStdLib c = (CStdLib)Native.loadLibrary("c", CStdLib.class);
        CStdLib.SysInfo s = new CStdLib.SysInfo();
        c.sysinfo(s);
        System.out.println("totalram: " + s.totalram);
    }

}

不幸的是,你会遇到签名long的值过大的问题,因此你可能会在Java中得到错误的值,我在尝试读取机器上的交换值时会看到这个值

希望这有帮助! (警告:我从未与Java程序员:)混淆)

答案 1 :(得分:3)

如果你决定解析文件,这里有一个实现......

Pattern pattern = Pattern.compile("([\\/A-Za-z0-9]+)[\\s]+([a-z]+)[\\s]+([0-9]+)[\\s]+([0-9]+)[\\s]+([\\-0-9]+).*");
BufferedReader reader = new BufferedReader(new FileReader("/proc/swaps"));
String s = reader.readLine();
while (s!= null){                
    Matcher matcher = pattern.matcher(s);
    if (matcher.matches()){
        System.out.println(s);
        System.out.println(matcher.group(3));
        System.out.println(matcher.group(4));
    }
    s = reader.readLine();
}            
reader.close();