Can someone make a .DLL for me that simply gets the rdtsc for the processor?

formulav8

Diamond Member
Sep 18, 2000
7,004
522
126
If you can make a simple dll with a single function to give me the cpu's time stamp in real-time, I would really appreciate it. :) I need the .dll to be compatible with .NET. Thanks for anyones help :) If you need somekind of compensation, I guess I could give you something. Just let me know if thats nessesary. Thanks for any help :)


Jason
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
This'll print an RDTSC timestamp but I'm too lazy to convert it to a DLL. ;) Perhaps someone else can go off this code.

It should work for dual core/dual CPUs just fine. Dual core is irrelevant to timestamp and it's impossible to multithread one x86 instruction. Both cores and CPUs have been alive for the same time.
 

formulav8

Diamond Member
Sep 18, 2000
7,004
522
126
Hi, thanks for the code but I do not even have C++ to try it myself. I have Visual C++ 5.0 somewhere but do not know where it is. I doubt I could compile it properly anyways. Thanks though :)



Jason
 

formulav8

Diamond Member
Sep 18, 2000
7,004
522
126
It seems to be working properly! Thanks alot for your help. If you pm me your paypal email, I will send you a couple bucks from my non-cc paypal account. Thanks :)



Jason
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Don't worry about it, it didn't take but a couple minutes. :) Just out of curiosity what would you be using this for? Measuring time it takes to complete an instruction?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
I've been working on this stupid thing for hours, just because I saw the problem and thought "that'd be an interesting thing to try, it's not something I normally do". It took about 5 minutes to get C++ code that would work, but I've spent FOREVER trying to get it to compile into some kind of DLL that will actually be useful in a .NET app.

I'm trying to do this with VS.NET and I have no fricking clue how to get a DLL that actually works. I've tried writing managed C++, which I could get to compile, but cpouldn't get my function to show up when imported into a C# project, even though it says the DLL is imported properly.

I've tried writing a generic DLL, but I have no idea how, and there's not even an option for "standard DLL" in the VS.NET project types....


<-- never written a windows DLL before.
 

formulav8

Diamond Member
Sep 18, 2000
7,004
522
126
Originally posted by: xtknight
Don't worry about it, it didn't take but a couple minutes. :) Just out of curiosity what would you be using this for? Measuring time it takes to complete an instruction?

Thanks :)

I mainly wanted it to calculate the cpu clockspeed and compare its benchmarking accuracy to the other timer functions. timeGetTime, QueryPerformanceCounter and such. The rdtsc of course should be the most accurate of any other timer. But I found out tonite thats its not always consistant. I read up on it and it seems that many variables could play a role in that. If the system/cpu is processing something when you call the time stamp, it seems to complete the other cycles before it carries out the rdtsc request, and thus returns the wrong value since it didn't return it instantly when the request was made. Especially if you do a back to back, or close to a back to back rdtsc request. There does seem to be some ways to fix that. Below is a link to a .pdf article that goes deep into the rdtsc feature, that you may find interesting. It seems, that by adding a line of code, it will help with consistancy.

Mentions adding this line:

cpuid ; force all previous instructions to complete

before requesting the time stamp that it will make requesting the time stamp counter more consistant.

If you would like to add some of the tweaks the document mentions, that would be great. I am of course happy with what you've done and am content with that. But if you did read the article and decide to tweak the dll alittle, I would be very appreciative. Completely up to you. :)

But anyways, I want to thank you again for taking the time to make the .dll for me. :)


PS: Here is the link to the .pdf document that goes deep into the rdtsc of the processor. Here is the document in a HTML format.


Jason
 

formulav8

Diamond Member
Sep 18, 2000
7,004
522
126
Originally posted by: notfred
I've been working on this stupid thing for hours, just because I saw the problem and thought "that'd be an interesting thing to try, it's not something I normally do". It took about 5 minutes to get C++ code that would work, but I've spent FOREVER trying to get it to compile into some kind of DLL that will actually be useful in a .NET app.

I'm trying to do this with VS.NET and I have no fricking clue how to get a DLL that actually works. I've tried writing managed C++, which I could get to compile, but cpouldn't get my function to show up when imported into a C# project, even though it says the DLL is imported properly.

I've tried writing a generic DLL, but I have no idea how, and there's not even an option for "standard DLL" in the VS.NET project types....


<-- never written a windows DLL before.


I am guessing you are trying to call the dll from Visual Basic? If so, Visual Basic will only reconnizes the: "__stdcall calling convention."

Here is a link to microsoft on how to make c++ .dll that can be called from Visual Basic.

Let me know how it goes, as I would be interested in testing out your results too :)


Jason





 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
It will now execute cpuid before rdtsc. Not sure if that will make any difference but you can give it a try: rdtsc v001

Edit: Also I added warmup() function, it just does cpuid/rdtsc 3 times (no .NET overhead): rdtsc v002

I believe CPUID flushes the CPU's registers (eax,ebx,etc) as a byproduct.

As for compiling a COM DLL you must have it export the function using a definition file and place a statement before function declaration.

Like this:
---------------------
__declspec(dllexport) unsigned __int64 funcName(void)
__declspec(dllexport) unsigned __int64 funcNumberTwo(void)
---------------------

Now suppose your library was named libName.dll.

Add a "libName.def" text file to the DLL project then inside of it put:

---------------------
LIBRARY libName
EXPORTS
funcName @1
funcNumberTwo @2
---------------------

Then you will be able to call the exported function from any program that can call COM DLLs.

Note: Don't forget that the Runtime.InteropServices.DllImport statement needs to be repeated twice in .NET now (once per function) for all the functions to work correctly! It's possible you can nest them under DllImport, but I'm not sure how.
 

formulav8

Diamond Member
Sep 18, 2000
7,004
522
126
I never expected you to do all of this. I am very grateful for the time you took to do this. Please pm me your paypal and I will be MORE than happy to send you something.

BTW, both of the newer .dll ARE more consistant. The 2nd is better, and the 3rd, is definitely the best of all 3x. Thanks alot for putting the time in to do this. :)



Jason
 

formulav8

Diamond Member
Sep 18, 2000
7,004
522
126
I tried it on my wifes lappy, which is single core and it is Almost Perfectly consistant. But it will not stay perfectly consistant with my dual core system.

Does anyone know how programs like CPUz calculates the clockspeed perfectly consistant for dual core comps? I would be interested in knowing. :)



Jason
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: formulav8
I tried it on my wifes lappy, which is single core and it is Almost Perfectly consistant. But it will not stay perfectly consistant with my dual core system.

Does anyone know how programs like CPUz calculates the clockspeed perfectly consistant for dual core comps? I would be interested in knowing. :)



Jason

They probably average a bunch of measurements taken over several seconds.
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
Originally posted by: formulav8
I never expected you to do all of this. I am very grateful for the time you took to do this. Please pm me your paypal and I will be MORE than happy to send you something.

BTW, both of the newer .dll ARE more consistant. The 2nd is better, and the 3rd, is definitely the best of all 3x. Thanks alot for putting the time in to do this. :)



Jason

I don't have Paypal but don't feel obligated. After all, I do busywork/homework every day and the school doesn't give me anything for it. I program a lot so this is very easy for me to do. I enjoy it and I'd be glad to make people freeware if it's easy for me and I have spare time (I do; I'm young). It only benefits people.

Anyhow I'm glad it's working for you. If you need anything else just let me know, or I'll aid you in setting up a compiler so you can make DLLs. Do check out the free VC++2005 Express or the free VC++2003 optimizing compiler command line kit from Microsoft. I personally just use VC6 for simple DLLs like this so I'm not 100% sure if/how you can do it with the newer versions.

Also note you can run assembly code under .NET (or anything that can call the Win32 API) with an API that's name is escaping me right now. It does however require you to convert it to byte-code first. I think it is CallWindowProc. I'm fairly certain you can call more than one instruction at once also to eliminate any applicable .NET overhead.

As for CPU-Z, yes, it probably does take an average of maybe five or so. If you are measuring the time it takes to do instructions, keep in mind that twice the amount of instructions have been done with two threads in the same amount of time. That of course is only applicable in multi-threaded situations (and not single-thread-on-dual-core-CPU situations).
 

formulav8

Diamond Member
Sep 18, 2000
7,004
522
126
Thanks alot for the info's notfred. I may try something like that.

The thing with me xtknight, is I do Not know assembly, well not enough to make functions and .dll's. I've seen alot of copy and paste code to get the cpu's features, sse, 3dnow and such. (Which is what I want to do next.) But I do not know how to put them in a function and compile into a .dll. It may be worth it to me since I am now wanting to get the supported flags of the processor, (SSE, SSE2, 3DNow, and ect..) to try and learn it in the future.

I remember seeing something that will add MIL or MSMIL or MISL or something like that to .net but I do not know that language either. It is supposed to be between assembly and c++ if I remember correctly. Intermediate language or something like that. I wonder how hard it would be to access the cpu registers and create functions and .dll's?? Hmm..

I do have Visual C++ 5.0 but do not know 'enough' to do even what you made for me. I do know some of the basic, since I have to do alot of C++ to Visual Basic code converting. Especially if I want to use a Windows Api. Microsoft almost always has them listed as C++ codes. So, I end up having to convert them to work with VB.

But thanks again xtknight for all of your help. Maybe if your ever bored, and if your up to it, you can make a .dll to get the basic cpu feature flags. (SSE, and such) :)


Jason
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
To be honest I don't know much assembly either. But just putting "__asm rdtsc" in a function that returns a UInt64 value gives the RDTSC timestamp right to you (via eax register).

You can probably get SSE, etc. flags from WMI (Windows Management Instrumentation) but I'm not sure. To me, MSIL looks a lot more complicated than just raw assembly for doing menial tasks, and it still runs under .NET. You can always use put unmanaged C in a C# app.

You are trying to make a benchmarking program? Because of speed maybe you should write it as a C console program and use a .NET program as a front-end to that console program. Or, you could do DLL calls like you're doing now too. It's actually fairly easy. The Project Wizard does the grunt work for you for the most part in terms of DLLs. All you have to do is add that .def file, then add a function to call cpuid or whatever you want (in __asm {} clause) and you're basically all set. If you're a hardcore VB programmer, learning how to interface with C is great knowledge to have. If you'd like I can send you the project file I have for this DLL.

Basically: When in Visual C, choose DLL for new project.
It doesn't matter what you do in the wizard, just make sure you get a C file you can type in once you're done with the wizard.

Then, add a .def file to the project like I explain above. In the C file implement the function(s) you defined in the .def file (rdtsc() or whatever). Sample:

Project name: myfirstdll

[Add to project as a blank text file]: myfirstdll.def

; contents start here, not including this line
LIBRARY myfirstdll
EXPORTS
myfirstfunction @1
mysecondfunction @2

; contents end here

(Just make sure that each function has a unique number after its @ symbol. At least that's how it works in my experience.)

Then VC will look for a .def file with the same name as the project name and it'll find that and use it as a definitions file to export those functions. Exported functions are just ones that are accessible externally via a call from another program (like a .NET one). .NET imports the DLL's exported functions. There is one function in the DLL that is not exported just as an example.

[Open up the main .c/.cpp file that VC Wizard created for you]

Just ignore every other function in there for now (DllMain, etc). Don't worry about them.
But at the end of the C++ file (not stdafx.cpp, it's going to be 'myfirstdll.cpp'), add these implementations (see attached code) for your functions you defined in the .def:

And it's as simple as that. Just compile the DLL (debug,release doesn't matter for now). Then use .NET to import it (code for DllImport is above). It's the same unsigned __int64 as my rdtsc so that should be easy. See how that works for you (DllImport myfirstfunction and DllImport mysecondfunction).
 

formulav8

Diamond Member
Sep 18, 2000
7,004
522
126
Thanks alot knight for the great info. I got some of what you mentioned, and some more of it will come when I get experience.

Would this process work for Managed C++ 2003? I have that installed already and maybe it would be capable of doing more than C++ 5.0?? Or do you not mess with Managed C++?

I'm trying to find my Visual C++ 5.0 but do not know where my wife put the box at yet. I will give it a try as soon as I can find it and get it installed. Thanks :)


PS: Do you have that rdtsc .dll project you made to get the time stamp? Maybe I can use that dll and project as a starting point? OR will it not compile with 5.0? Would it compile with Managed C++ 2003?


Jason

 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
While I haven't played with managed C++ I've heard of it. It's C++ that uses the .NET framework but I'm not sure if you can create a regular DLL in it.

Here's the VC6 project workspace for rdtsc v002: http://xtknight.atothosting.com/misc/rdtscv002_vc6proj.zip

I'm not aware of an actual GUI named Managed C++ 2003 though. Isn't it just plain old Visual Studio 2003? If so you should be able to open the file and have it convert it to VS2003 format. In order to support managed C++ it has to support regular C++ too so technically this should work. With VS2005 I can open the rdtsc v002 VC6 project with no issues (I chose 'Yes to All' on the conversion dialog). It compiles right away. Try opening the .dsw first and if that doesn't work then try opening the .dsp.

Good luck...
 

formulav8

Diamond Member
Sep 18, 2000
7,004
522
126
Thanks knight.

I'm going to give it a try later on. The C++ I have is a part of VS too. 2003 Version.



Jason