0

I am trying to read some basic information about the device (in Solaris machine) like no. of CPUs, no. of HDDs, no. of Network adapters etc ...

From what I understand, we cannot get these information directly from Java. (Correct me if I am wrong).

So, the workaround is by calling JNI calls.

Now, how do I code this in C/C++ ? Is there any existing system call/ method in C to get these information ?

I am trying to avoid using system() method in C because I want to store the information as a string.

I am aware that there are shell commands like iostat, kstat, ifconfig etc ... But how do I simulate them in my C/C++ program ?

Thanks,

JohnH
  • 150
  • 1
  • 11
  • 2
    >> From what I understand, we cannot get these information directly from Java. You can always call system/shell programms and capture the output. That works from Java too. – Alexander Gessler Mar 09 '11 at 22:19
  • Agree with above. Unless you're just looking for an excuse to learn JNI, simply calling existing system commands and capturing the output is a whole lot easier. – Brian Roach Mar 09 '11 at 23:05
  • 1
    Some very basic information you can get from `System.getProperty()`, like `os.arch`, `os.name` - this may help you to choose the right shell programs to invoke (or the right files to read). – Paŭlo Ebermann Mar 10 '11 at 00:03
  • @Alexander: how do I capture system calls in Java ? – JohnH Mar 10 '11 at 04:58
  • @Brian: I have some experience with JNI. My problem is how to capture the system command's output in C/C++ ? using system() command prints to the stdout. – JohnH Mar 10 '11 at 05:00

3 Answers3

1

The /proc filesystem has lots of information for you to parse. iostat e.g. reads /proc/diskstats. There is much more there, and most of linuxs utilitiy programs just read the information from here.

You don't even need JNI to get all the info, at least, if you stay on linux or solaris.

Daniel
  • 27,718
  • 20
  • 89
  • 133
1

For a Java solution, an alternative to JNI would be to use Java's Runtime.exec() to execute system commands. Here's a simple example:

Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("/bin/cat /proc/cpuinfo");
process.waitFor();

InputStream in = process.getInputStream();
// read stdout contents from InputStream and parse accordingly

Note that this has disadvantages:

  • It is not platform-independent
  • Runtime.exec() has some gotchas it forks a child process to execute the command. In certain environments it will allocate as much memory to the forked process as the application invoking Runtime.exec() is using. According to this answer, it's because fork() duplicates the parent process when forking the child. This can be problematic for applications that use a lot of memory.
Community
  • 1
  • 1
Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
  • I think most kernels implement copy on write. They may even apply further optimizations in the `fork+exec` case. So it shouldn't be a memory problem. Apart from that, I consider this the most straightforward solution. – Alexander Gessler Mar 10 '11 at 12:59
  • This won't work as is on Solaris (I mean the "cat cpuinfo" thing). – jlliagre Mar 10 '11 at 14:35
  • @jlliagre - Yeah, my example was only indicative of how you would make the system call; I just threw `cat /proc/cpuinfo` in there as an example - it would obviously have to be changed depending on the environment and the system files/commands available. – Rob Hruska Mar 10 '11 at 16:51
0

To code this in JNI (bound to C)

  1. Write the Java class, with at least one method marked as "native".
  2. Use javac to compile the ".java" file to a ".class" file
  3. Use the javah tool to extract a C / C++ compatible header file from the compiled ".class" file.
  4. Write the C / C++ implementation for all the methods in the extracted header file, and compile them into a shared object library (.so for unix / linux, .dll for windows).
  5. Modify the original java source code to statically load the shared object library before any instances are created.

As far as how you can get the desired data, refer to any number of systems programming references for Windows or UNIX. If targeting the Linux platform, most of the tools are open source, so you can look at the source code to determine how the tools obtained their data (assuming that you can live with the license restrictions that such a task might place on your code).

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138