Java 如何从/proc/swaps获取Linux中的交换大小?
我想开发JSF页面,它可以显示交换大小(如果操作系统是Linux)以及利用率。我发现这些信息可以从/proc/swaps中读取。但当我打开它时,我得到了这个Java 如何从/proc/swaps获取Linux中的交换大小?,java,linux,jsf,Java,Linux,Jsf,我想开发JSF页面,它可以显示交换大小(如果操作系统是Linux)以及利用率。我发现这些信息可以从/proc/swaps中读取。但当我打开它时,我得到了这个 Filename Type Size Used Priority /dev/sda2 partition 2047992 0 -1 我如何才能得到这些值-2047992和0?我知道如何读取文本文件的内容。例如: tr
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) {
}
另一个更高级的问题是,如果我有两个掉期怎么办
关于(特定于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 */
};
其中totalswap
和freeswap
可能就是您所追求的
我不知道如何在Java中进行本机平台调用,但这是自己解析/proc/swaps
文件的好选择
编辑:
我玩了一下,想出了这个:
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程序员混淆过:))系统调用(特定于Linux)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 */
};
其中totalswap
和freeswap
可能就是您所追求的
我不知道如何在Java中进行本机平台调用,但这是自己解析/proc/swaps
文件的好选择
编辑:
我玩了一下,想出了这个:
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程序员混淆过:)
)如果您决定解析文件,下面是一个实现
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();
如果您决定解析文件,下面是一个实现
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();
回答得很好!不幸的是,我是Java新手,不知道如何用Java编写代码。也许我可以使用JNI。还有其他有用的系统调用来监控Linux性能吗?例如,总网络会话数?据我在Github页面上看到的,JNA是针对Windows的?JNA运行在“大多数支持Java的平台”上。我上面发布的示例运行在我的Linux amd64机器上。回答得非常好!不幸的是,我是Java新手,不知道如何用Java编写代码。也许我可以使用JNI。还有其他有用的系统调用来监控Linux性能吗?例如,总网络会话数?据我在Github页面上看到的,JNA是针对Windows的?JNA运行在“大多数支持Java的平台”上。我上面发布的示例运行在我的Linux amd64机器上。使用JNI调用sysinfo可能是一个更健壮的解决方案。使用JNI调用sysinfo可能是一个更健壮的解决方案。