Chicagoland Sportbike Forums banner

Cpu usage info for java app Expert help needed

1K views 21 replies 5 participants last post by  Supergirl 
#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
 
See less See more
#2 ·
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....
 
#3 · (Edited)
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.
 
#4 ·
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:
 
#7 · (Edited)
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 :)
 
#8 ·
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:
 
#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
 
#10 ·
/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
 
#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!!
 
#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
 
#19 ·
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!
 
#22 ·
sure, i know about it, but i would have to install it on every single client i need to run the app at. so that wouldn't make the app as portable as if i was using csv or xml :D

but thanks anyway :)

i demonstrated that it worked, and a couple of us got an extension to work on gui. that's good news!!!
 
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top