Physical and logical CPU count

Hi,How can i get the physical and logical CPU count? please help. i could not found the solution for this problem in pure java. there is an API for the intel processors provided by intel, but its in C. How can i do the same from java?
[262 byte] By [arifa] at [2007-10-3 8:55:44]
# 1

> Hi,

>

> How can i get the physical and logical CPU count?

>

> please help. i could not found the solution for this

> problem in pure java.

> there is an API for the intel processors provided by

> intel, but its in C. How can i do the same from java?

You can't. What do you think would happen if you tried to use that API on another processor?

kajbja at 2007-7-15 4:05:54 > top of Java-index,Java Essentials,New To Java...
# 2

>What do you think would happen if you tried to use that API on another processor?

you are right, Intel API in C won't work on processors other than Intel's.

But if i could do it only for Intel's that's fine for me.

i like to keep my program in pure java but is there any way i can call C api from java and get the Physical CPU count from there?

i think only C can go to this level. as i remember, i had to parse the whole 'ipconfig' command's response to get the network information (i had 2 network cards on my system).

arifa at 2007-7-15 4:05:54 > top of Java-index,Java Essentials,New To Java...
# 3
Have a look at this tutorial: http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html
prometheuzza at 2007-7-15 4:05:54 > top of Java-index,Java Essentials,New To Java...
# 4

Ok, this solves the problem in a way ( i guess its the only way).

another question: is there any way to do #ifdefine and #endIf in java.

i mean the conditional compilation like in C/C++.

i am stuck to create four files for a small change in code. i don't want it to be visible to the user in any way so i can't read it from a file external to my jar file.

arifa at 2007-7-15 4:05:54 > top of Java-index,Java Essentials,New To Java...
# 5

You can do:private final boolean SOMEVALUE = true;

...

if (SOMEVALUE) {

// conditional code here

}

Because it's final, the compiler will remove the code entirely if SOMEVALUE is false.

Or, you can just use the C preprocessor if you want.

Or find another solution. Maybe just put this data in a jar. You do realize that users can always decompile your code, right?

You get a lot of information from java.net.NetworkInterface. Why did you need to parse ipconfig?

It sounds like you're doing a lot of things that Java was never meant to do.

Message was edited by:

paulcw

paulcwa at 2007-7-15 4:05:54 > top of Java-index,Java Essentials,New To Java...
# 6

> i am stuck to create four files for a small change in

> code. i don't want it to be visible to the user in

> any way so i can't read it from a file external to my

> jar file.

Are you talking about the JNI library (DLL) that you need to call?

One solution to this is to store the DLL as a resource in your jarfile. Extract the resource to a temporary file on disk (unfortunately, this step is needed), then use the System.loadLibrary() method (or whatever it's called) that takes a full pathname to the library.

Not sure why you want the #ifdef, is it to see if the user is running on a version that includes the CPU check? If that's the case, then it's a good use for reflection: try to load the class with Class.forName(); if that fails, you don't have your native classes available.

kdgregorya at 2007-7-15 4:05:54 > top of Java-index,Java Essentials,New To Java...
# 7

>Because it's final, the compiler will remove the code entirely if SOMEVALUE is false.

thanks, i got the idea. i can use the final boolean variable.

>You get a lot of information from java.net.NetworkInterface. Why did you need to parse ipconfig?

i wanted the mac address.

>It sounds like you're doing a lot of things that Java was never meant to do.

i'm new to java. and you guys are helping alot. Thanks.

arifa at 2007-7-15 4:05:54 > top of Java-index,Java Essentials,New To Java...
# 8
No.i needed the #ifdef statement to differentiate between the four versions of my program.in each case there is diffrent code and diffrent constants. its not related with the CPU cheeks or loading the JNI library.
arifa at 2007-7-15 4:05:54 > top of Java-index,Java Essentials,New To Java...
# 9
What are the four versions of your program? How do they differ?
paulcwa at 2007-7-15 4:05:54 > top of Java-index,Java Essentials,New To Java...
# 10

Basically its like access to some features based on license type.

I want a user with one license type to use some features and not others.

and there are four such possibilities.

e.g. think of it as a client application connecting to a remote server. based on a certain license the user can do remote calls, and stuff like that. and for another license type he can't.

arifa at 2007-7-15 4:05:54 > top of Java-index,Java Essentials,New To Java...
# 11

Personally, I'd try to do something like this not with conditional compilation, but with conditional deployment. Design your app so some classes can be missing, or be replaced with dummy classes. Then in the jars you send to the user, include the classes, or don't, depending on their license.

It seems to me that this would be easier to manage than code that changes per user. Also, I suspect, easier to test. And you don't need to worry about clues appearing in decompiled code.

paulcwa at 2007-7-15 4:05:54 > top of Java-index,Java Essentials,New To Java...
# 12
but you said that if i use final boolean than the compiler would remove the false code. And also using this approach i will have the one project.
arifa at 2007-7-15 4:05:54 > top of Java-index,Java Essentials,New To Java...
# 13

It will, but if you're distributing different versions to different people I think it would be easier to mix-and-match class files that have a single compiled version, than to manage different compiled versions. You could for example make an Ant script that compiles everything and then generates four different jar files, one for each type of customer. Also if you have single compiled versions of each class, then (for example) you can tell more easily from a stack trace what's going on -- there's only that one version of any class in the stack trace. If you had different versions of the compiled classes out there, you wouldn't know what version the user had.

It'll work with the final boolean, but I think it would be easier from a logistical perspective to just distribute different classes (with unique names, etc.).

I don't know what you mean about "the one project". Most uses of "project" in IDEs and the like (which I've seen anyway) allow for multiple classes, plus build scripts, etc.

paulcwa at 2007-7-15 4:05:54 > top of Java-index,Java Essentials,New To Java...
# 14

>'ll work with the final boolean, but I think it would be easier from a logistical perspective to just distribute different classes (with unique names, etc.).

different classes with unique name is not good. it creates confusion for the end user. o let me explain again: i'm giving some API to the user, so unique names is not good.

Ant scripts idea seems better.

i'm using the "sun java studio enterprise 8.1" it uses ANT scripts.

now i'm looking for ANT scripts.

arifa at 2007-7-15 4:05:54 > top of Java-index,Java Essentials,New To Java...
# 15

> different classes with unique name is not good. it

> creates confusion for the end user. o let me explain

> again: i'm giving some API to the user, so unique

> names is not good.

This is where I'd use an interface to define the API, then provide different implementations of it: http://java.sun.com/docs/books/tutorial/java/concepts/interface.html

kdgregorya at 2007-7-21 12:37:57 > top of Java-index,Java Essentials,New To Java...
# 16

Absolutely.

There are many cases in the existing API where you, the user of the API, are using particular unnamed classes without knowing it, because you're dealing with the interface.

So for example, there might be a class, say, PlanetaryCalculatorFactory, and an abstract class or interface named PlanetaryCalculator. The user would do something like:

PlanetaryCalculator pc = PlanetaryCalculatorFactory.getCalculator("Jupiter");

pc.findOrbit(someProbe);

And it would turn out that someProbe is actually an instance of the class GasGiantCalculator. But this is never mentioned, because it doesn't have to be. And if for some reason the user only bought the license that gives them access to Mercury and Venus, then you might send them an UpgradeYourLicense object, which implements PlanetaryCalculator but returns 0 or null for everything.

OK maybe that's not the best example but hopefully you get the idea.

paulcwa at 2007-7-21 12:37:57 > top of Java-index,Java Essentials,New To Java...
# 17
i got your point. but what will be its impact on the performance of my API? The sole purpose of my application is performance.BTW I'm measuring code time on micro-second scale.
arifa at 2007-7-21 12:37:57 > top of Java-index,Java Essentials,New To Java...
# 18
interface; it means that i again have four classes each implementing that interface. please correct me if i'm wrong.
arifa at 2007-7-21 12:37:57 > top of Java-index,Java Essentials,New To Java...
# 19

My guess is that it wouldn't effect performance at all; it might improve performance because it would allow you to have different implementations tuned for whatever it is they're doing under whatever conditions it's operating under.

But I really don't know what you're implementing so I don't know. Profile profile profile.

paulcwa at 2007-7-21 12:37:57 > top of Java-index,Java Essentials,New To Java...
# 20

> i got your point. but what will be its impact on the

> performance of my API?

That's where profiling comes in. Implement, profile, and then figure out how to resolve any hotspots.

> The sole purpose of my application is performance.

Then why aren't you writing in assembly language?

And really, the sole purpose is to provide useful functionality to some end-user, right? Performance is a part of delivering that functionality, but it's hardly the sole purpose. If it were, you could just write a 3-line program that starts, does nothing, and exits ... can't beat that for performance.

kdgregorya at 2007-7-21 12:37:57 > top of Java-index,Java Essentials,New To Java...
# 21
> interface; it means that i again have four classes> each implementing that interface. please correct me> if i'm wrong.Yes. That's what object-oriented programming is all about.
kdgregorya at 2007-7-21 12:37:57 > top of Java-index,Java Essentials,New To Java...
# 22
I wanted to avoid the creation of four classes giving same functionality and differentiate only for license type.so that it does not affect the performance.
arifa at 2007-7-21 12:37:57 > top of Java-index,Java Essentials,New To Java...
# 23
Well, you could handle this at deploy time (when packaging stuff up in a jar, I mean) and not run time or at compile time. That probably would be pretty quick at runtime.
paulcwa at 2007-7-21 12:37:57 > top of Java-index,Java Essentials,New To Java...
# 24
finally after a much thought and help from you guys, its done. andastonishingly, i added only 5 lines of code.
arifa at 2007-7-21 12:37:57 > top of Java-index,Java Essentials,New To Java...