# Writing Code to Reveal the Performance Details of Mobile Processors

### Introduction

Calculations reveal the relationships between power, clock speed, and frequency

by Richard Winterton

James Joule developed the formula for power dissipation (P = I²R), which states that power is equal to electrical current squared times resistance. His laboratory work of long ago illuminates technologies that he could not have dreamed of, including a quite different, but related, type of power - the computing power of Intel's high performance laptops.

I am taking license by using the word "power" in these two different contexts, but the two are related. This paper and the related sample code explain how to generate very accurate frequency measurements of Intel® processors, as well as a nice little class method to measure time in microseconds.

### The Relationship between Power and Frequency

The frequency at which a processor runs is directly related to the amount of power it consumes.

Whereas the formula P=I²R calculates "real" power, we are more concerned here with a reactive component to power that is due to the inherent capacitance of the processor. Reactive power is directly related to frequency and can be expressed using the formula P=V²CF, which states that power is equal to the voltage squared times capacitance times frequency.

To see how this formula relates to concrete circumstances, consider the following case. I am working on a laptop based on a Mobile Intel® Pentium® 4 Processor - M and running Microsoft Windows XP*. The System Properties screen reports that my processor is running at 2.0 GHz when the laptop is plugged in, but it reports 1.2GHz when I am running on battery power and the processor is not actively being used.

Intel has gone to considerable effort and expense to build my processor to work at those different power levels when plugged in or unplugged. To understand why, look again at the formula P=V²CF. By lowering the frequency, the amount of power consumption is also decreased. There are many other tricks that Intel uses to decrease power consumption in their mobile processors, but this is a fairly obvious one.

### A Framework for Measuring

There is one particularly difficult complication to determining at what frequency a processor is actually running. Just as the Heisenberg uncertainty principle states more generally, the very act of measuring the frequency actually changes the frequency itself.

The explanation that follows and the sample code that accompanies this paper present a fairly accurate way to measure CPU frequency by using the RDTSC (time-stamp counter) and computing the number of clock ticks per microsecond. As a side note, this same method can be used to measure other functions with fairly high accuracy.

### QTime Class Description

The QTime Class is a C++ class that has three external methods:

```
void GetCPUTicsPerUSec(__int64 *pTickPerUSec);

__int64 GetTime();

BOOL IntelProcessor();
```

### GetTicksPerUSec Method

The GetTicksPerUSec method is the key to the QTimeClass. The first thing this method does is to read the time stamp counter and to set the reading process priority to realtime and the thread priority to time critical. The next stop in calculating the number of ticks per microsecond is to read the timestamp counter as fast as possible, making sure that you have a difference in the start and stop times:

```for (;;)

{

timeStop = timeGetTime();

if ((int)(timeStop - timeStart) > 0)

{

t1 = GetCPUTicks();

break;

}

}```

Next, use the system function call timeGetTime, which has a incremental value of 1 millisecond. Wait for 100 milliseconds before setting the thread priority back to normal:

```for (;;)

{

timeStop = timeGetTime();

if ((int)(timeStop - timeStart) > 100)

{

t2 = GetCPUTicks();

break;

}

}```

At that point, you can take the difference between the first reading and the second reading and divide it by the elapsed time calculated from the timeGetTime system call, after converting to units of microseconds:

```m_CPUTicksPerMicr
oSecond = (diff) / (elapsedTime * 1000);```

This technique yields a good estimate of the number of ticks per microsecond. You can save this value to calculate elapsed time in the GetTime method of this class.

### GetTime Method

The GetTime method reads the current value of the processor's time-stamp counter and divides the number of ticks read by the number of ticks per microsecond (calculated in the previously described method), to yield the number of microseconds. The next time you call this function, all you need to do is subtract the two return values to calculate the elapsed time between the two function calls in microseconds.

### Summary

This demonstrates how to calculate the processor frequency and time stamp differences. Developers can use these files to determine the actual clock speed at which an Intel processor is running.

This technique is increasingly valuable to independent software vendors as they customize their code for Intel mobile processors, including the recently-announced Intel® Centrino® mobile technology.

### About the Author

Richard Winterton graduated from Brigham Young University in 1986 with a BS degree in Electrical Engineering and a minor in mathematics. While at Lockheed Martin from 1986-1994, he designed two network ASIC chips and authored 2 network communication protocols that later become SAE and DOD network protocol standards. Richard was a member of a team that wrote an operating system for the U.S. Navy's A-12 Avenger, advanced attack aircraft, wrote the "BIOS" software for the main avionic computer for the F-16 Fighting Falcon and wrote part of the F-16 operating system. Richard attended the University of Texas at Arlington's graduate school, working on a master's degree in Electrical Engineering. In 1994 Richard went to work for Intel Corporation where his first assignment was a senior software engineer developing Intel's LANDesk Management products. Richard's current position at Intel is an application engineer responsible for software optimization, and enabling engineer, optimizing and enabling applications for Intel's latest and next generation products.