Chicagoland Sportbike Forums banner
1 - 20 of 22 Posts

·
Defender of the Innocent
Joined
·
2,918 Posts
Discussion Starter · #1 ·
Hi,

I spent the past 72 hours trying to figure out how to get cpuload (of ALL processes) to my java app, to further work with the numbers.

I need 3 pieces of information: OS, memory usage, and cpuload(1, 5, and 15 minutes) on win and linux. (not java VM usage)

Runtime and Properties classes give me the OS (so i can determine which command to use to gather other info)

i can exec systeminfo and get Total Physical Memory, Available Physical Memory, Virtual Memory Size, Virtual Memory: Available, Virtual Memory: In Use


apparently, there is no way to do this from java on windows (my app has to be portable to at least win and linux) and one has to use C libs and javah to create header files etc.
so, i found this awesome library, and after spending all day building it and getting it to run, it shows the cpu only of the current process.

can anyone help me either modify the existing library or do you have any idea how to get the info done. my homework goes wayyyyy beyond this, but I am stuck right here and see no way out.

thanks,

very frustrated SG
 

·
Irony helps us play!
Joined
·
29,500 Posts
can you exec vmstat on linux? :>

I know there exist utilities (and probably even the same command) under the cygwin package for windows (gives a unix-like API and commands to windows boxes through a very functional DLL system) but that does require it to be installed on the client machines unfortunately.. let me see if I can dig something up that's more native....
 

·
[Online]
Joined
·
3,695 Posts
Not a Java expert (.NET guy here, know just a little bit of Java to be dangerous) - but the obvious solution is to give up portability and either build two versions (Win/Linux) or dynamically load a library with the OS-specific implementation.

EDIT: I guess you have to use JNI to access OS specific implementation... But I can't be sure.

BTW, have you tried googling it? Java "java get cpu load" produces some results.
 

·
The Benchwarmer
Joined
·
14,574 Posts
Java MIGHT be a good example of what a programming language should be like. But Java applications are good examples of what applications SHOULDN'T be like. :laughing:
 

·
[Online]
Joined
·
3,695 Posts
Java MIGHT be a good example of what a programming language should be like. But Java applications are good examples of what applications SHOULDN'T be like. :laughing:
It all actually reminds me of what somebody said about Java once:

Saying that Java is nice because it works on all OS's is like saying that anal sex is nice because it works on all genders
 

·
The Benchwarmer
Joined
·
14,574 Posts
It all actually reminds me of what somebody said about Java once:
haha hell ya man, not a fan of c++ or java.

but put php and sql in front of me and i pop a chubby :laughing:

perl is nice too if i feel like programming something that looks like the code of the matrix.
 

·
Registered
Joined
·
701 Posts
Linux does not report such thing as CPU load. What you can find out though is % of CPU time spent in varuous states : user code,kernel code, waiting for IO , sleeping

Generally the "load" is defined as number of processes ready to run sampled over fixed time span . Linux uptime gives you that information. You can exec and parse output of that command. On Windows, you can peridocically query process tree for ready to run processes and build avarage yourself.

BTW: take advantage of "information hiding" technique not to get stuck on this particular topic of your homework. Build a stub and move on. You can alway "false" simulate the results when time comes :)
 

·
The Benchwarmer
Joined
·
14,574 Posts
Generally the "load" is defined as number of processes ready to run sampled over fixed time span . Linux uptime gives you that information. You can exec and parse output of that command. On Windows, you can peridocically query process tree for ready to run. Build avarage yourself.

BTW: take advantage of "information hiding" technique not to get stuck on this particular topic of your homework. Build a stub and move on. You can alway "false" simulate the results when time comes :)
+1

i would run the uptime command and parse the information to get the cpu load. just hard code the results :laughing:
 

·
Defender of the Innocent
Joined
·
2,918 Posts
Discussion Starter · #9 ·
wow answers!!!!! :)

shouldn't /proc/meminfo /proc/cpuinfo work on linux? don't have a second to try it, still stuck on windows.

obviously I will have different versions, BUT

i found a scaled down c++ lib for my "problem" but i'm too stoopid to compile it from the console and include all the java JNI include paths.

please help. i can deal with c++ OR with java, but not with both LOL and tomorrow comes c# part for it, don't ask.

so CURRENT problem (and this is modified from the previous c lib, i copied java inlcudes into c:\temp)

C:\MinGW\bin>cpp -D_JNI_IMPLEMENTATION_ -Wl,--kill-at -I C:/temp/include -I C:/T
emp/include/win32 -shared C:/temp/Resource_monitor.cpp C:/MinGW/lib/libpsapi.a
cpp: --kill-at: linker input file unused because linking not done

Please help me to fix this, so I can finally move on!!

Thanks to all for good ideas!

EDIT

when i run the app, i get the linking error

java.lang.UnsatisfiedLinkError: C:\WINDOWS\system32\pr_res.dll: %1 is not a valid Win32 application
 

·
The Benchwarmer
Joined
·
14,574 Posts
wow answers!!!!! :)

shouldn't /proc/meminfo /proc/cpuinfo work on linux? don't have a second to try it, still stuck on windows.

obviously I will have different versions, BUT

i found a scaled down c++ lib for my "problem" but i'm too stoopid to compile it from the console and include all the java JNI include paths.

please help. i can deal with c++ OR with java, but not with both LOL and tomorrow comes c# part for it, don't ask.

so CURRENT problem (and this is modified from the previous c lib, i copied java inlcudes into c:\temp)

C:\MinGW\bin>cpp -D_JNI_IMPLEMENTATION_ -Wl,--kill-at -I C:/temp/include -I C:/T
emp/include/win32 -shared C:/temp/Resource_monitor.cpp C:/MinGW/lib/libpsapi.a
cpp: --kill-at: linker input file unused because linking not done

Please help me to fix this, so I can finally move on!!

Thanks to all for good ideas!

EDIT

when i run the app, i get the linking error

java.lang.UnsatisfiedLinkError: C:\WINDOWS\system32\pr_res.dll: %1 is not a valid Win32 application
/proc/cpuinfo is just going to show you information on the processor like family, model, cache size, etc n shit it wont show you its load. so you cant really 'grep' that and expect to get the info you're looking for
 

·
Irony helps us play!
Joined
·
29,500 Posts
Linux does not report such thing as CPU load. What you can find out though is % of CPU time spent in varuous states : user code,kernel code, waiting for IO , sleeping

Generally the "load" is defined as number of processes ready to run sampled over fixed time span . Linux uptime gives you that information. You can exec and parse output of that command. On Windows, you can peridocically query process tree for ready to run processes and build avarage yourself.

BTW: take advantage of "information hiding" technique not to get stuck on this particular topic of your homework. Build a stub and move on. You can alway "false" simulate the results when time comes :)
Since he asked for 'cpu load' I thought vmstat could be read and then subtract the CPU idle column from 100 to give you the percentage of 'cpu usage'. Not really caring if it was spent in system land, user land or waiting on disk I/O :> I've always looked at the 'load' from uptime as being too subjective to really even care about because it's easy to have a system where the 'load' might be 15-20 and have it be much peppier than a system with a 'load' of 5 depending on exactly what applications are running. Reading the 'idle' time will give you a better machine feel of how much the cpu is actually working versus just giving you a process count. :2cents:

It really depends on what he's ultimately trying to find out to report on/display....
 

·
Registered
Joined
·
701 Posts
C:\MinGW\bin>cpp -D_JNI_IMPLEMENTATION_ -Wl,--kill-at -I C:/temp/include -I C:/T
emp/include/win32 -shared C:/temp/Resource_monitor.cpp C:/MinGW/lib/libpsapi.a
cpp: --kill-at: linker input file unused because linking not done
On linux that's C++ preprocessor , not linker , and not compiler.

The way to build shared lib is :

Compile source :
c++ -D_JNI_IMPLEMENTATION_ -I C:/temp/include -I C:/Temp/include/win32 -o Resource_monitor.o -c Resource_monitor.cpp

Link library libmylibname.so
c++ -shared -o ./libmylibname.so ./Resource_monitor.o [other .o or .a files]

However, if you use shared library or JNI you automatically make your application non portable.
 

·
Defender of the Innocent
Joined
·
2,918 Posts
Discussion Starter · #15 ·
Thank you Diesel boy!

now, but if I in my logic make use of the library only in case my OS is let's say "win", and don't make any use of it in other case, I should be fine, right?

this C++ lib is made for win, the other, first one I found was generic, but it gave me just the cpu usage of the current process.

if I can get my info by exec command on linux, I won't need the library.

Thanks for the syntax! Highly appreciate it!!
 

·
Registered
Joined
·
701 Posts
Thank you Diesel boy!
now, but if I in my logic make use of the library only in case my OS is let's say "win", and don't make any use of it in other case, I should be fine, right?
Yes , you should be fine. You just can't use the same library code for two different OS.
 

·
Defender of the Innocent
Joined
·
2,918 Posts
Discussion Starter · #17 ·
I never made it that far :(

This homework was bigger than the senior project in my undergrad.

it works for windows so far, but the server won't talk to the gui component and send him information collected from clients :(

I'll try to get that to work in the morning (gui) and hell, the homework is due 4:15pm :cry:

Thank you all for all the help! <3

SG
 

·
Defender of the Innocent
Joined
·
2,918 Posts
Discussion Starter · #19 ·
Heh, run a package like sysstat on the server and have it dump an XML or CSV file on a periodic basis, then have the java ap just grab the file :)

Hell, that works w/ any web browser too :lmao:
you know, that's a really good idea.

I thought of excel, bc that way i thought i could even pull the graph, but figured i won't have excel on linux.

xml or csv would have been way to go!

i still have 3 hours time :p

Thanks!
 

·
Irony helps us play!
Joined
·
29,500 Posts
you know, that's a really good idea.

I thought of excel, bc that way i thought i could even pull the graph, but figured i won't have excel on linux.

xml or csv would have been way to go!

i still have 3 hours time :p

Thanks!
Dude.. http://www.openoffice.org

Cross platform, open source, MS Office compatible and free.


...as for the sysstat idea, that will only dump the server's information, not the client's, just FYI....
 
1 - 20 of 22 Posts
Top