Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

CPU Time

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

How can I get the CPU time consumd by a specific process in java?

Thanks!
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i dont think you can do this in pure java. what OS are oyu on ? perhaps you can run some command trough Runtime.exec() or ProcessBuilder ?


pascal
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OS doesnot matter. I am working on Windows and Linux and I have to find a way to find processor time in both of them.
I think it's easier in C++.
Does anybody know how to integrate a c++ classe into a java program ?

Thank
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look into the management APIs used by JConsole in JDK5 and later. CPU time used might be there.
 
Marshal
Posts: 69806
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you want the CPU time or just the time taken to run a particular method?
Try the System.nanoTime() method at the beginning and end of a method.
Try the System.nanoTime() method at the beginning and beginning of a method.

CR
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anybody know how to integrate a c++ classe into a java program ?

Forgot to answer that one. Search for JNI - Java Native Interface. You can write C/C++ code with a specific format to make it callable from Java.
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any help with that ?
Where can I found something CLEARLY explained on how to use JNI ?

thank you!
 
aymane chetibi
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
Well I am starting to figure this problem out but I am getting some problems.
I created a java class: CPUJavaTest.java
that looks like this:
class CPUJavaTest{
public static native long cpuCount();
static {
System.loadLibrary("jnicputime");
}
public static void main(String[] args) {
long cputime;
int i,j;
cputime = cpuCount();// Use the function

for (i=0;i<300000000;i++) j = i*7+5%14;

cputime = cpuCount() - cputime;// and again

System.out.print("CPUTIME:\t");
}
}

Compiled it into CPUJavaTest.class

then created a header file using: javah -jni CPUJavaTest == > (CPUJavaTest.h)

UP TO HERE EVERYTHING WORKS FINE.

Then I fount that I have to write a C code that implements the function, I did the following:

#include <sys/times.h>
#include <unistd.h>
#include "CPUJavaTest.h"

#define CLK_TCK ((clock_t)(sysconf(_SC_CLK_TCK)))

JNIEXPORT jlong JNICALL Java_CPUtest_cpuCount (JNIEnv *env, jobject obj)
{
struct tms tmsbuff;
times(&tmsbuff);
return (jlong)tmsbuff.tms_utime/CLK_TCK;
}

Then compiling this code does not work.
I have to create a library from this code.
CAn you pleaaaaase help me with getting this code compiled. I compiled it on both Windows and Linux and it does not work.

It gives me some errors both on the C file and on the header file.

Thank you.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic