0

I want to use the "CPU Sampler" in my Project for The CPU Measurement but i can't find it. I'm using XCode 4.3

keep on you
  • 310
  • 6
  • 21

4 Answers4

2

First import all the required header file in your interface file .

#include <sys/sysctl.h>
#include <sys/types.h>
#include <mach/mach.h>
#include <mach/processor_info.h>
#include <mach/mach_host.h>

processor_info_array_t cpuInfo, prevCpuInfo;
mach_msg_type_number_t numCpuInfo, numPrevCpuInfo;
unsigned numCPUs;
NSLock *CPUUsageLock;

and in your implementation file.

- (void)cpuSampler:(NSTimer *)timer
  {
    natural_t numCPUsU = 0U;
    kern_return_t err = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO,&numCPUsU, &cpuInfo, &numCpuInfo);
    if(err == KERN_SUCCESS) {
           [CPUUsageLock lock];

           for(unsigned i = 0U; i < numCPUs; ++i) {
               float inUse, total;
               if(prevCpuInfo) {
                      inUse = (
                     (cpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_USER]   - prevCpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_USER])
                     + (cpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_SYSTEM] - prevCpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_SYSTEM])
                     + (cpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_NICE]   - prevCpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_NICE])
                     );
                     total = inUse + (cpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_IDLE] - prevCpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_IDLE]);
               } else {
                        inUse = cpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_USER] + cpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_SYSTEM] + cpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_NICE];
                        total = inUse + cpuInfo[(CPU_STATE_MAX * i) + CPU_STATE_IDLE];
               }
               if (i == 0) {
                 NSString *string0 = [NSString stringWithFormat:@"Core: %u Usage: %f%",i,(inUse / total)*100];
               }
              if (i == 1) {
                 NSString *string1 = [NSString stringWithFormat:@"Core: %u Usage: %f%",i,(inUse / total)*100];
              }
          }
      [CPUUsageLock unlock];

    if(prevCpuInfo) {
        size_t prevCpuInfoSize = sizeof(integer_t) * numPrevCpuInfo;
        vm_deallocate(mach_task_self(), (vm_address_t)prevCpuInfo, prevCpuInfoSize);
    }

    prevCpuInfo = cpuInfo;
    numPrevCpuInfo = numCpuInfo;

    cpuInfo = NULL;
    numCpuInfo = 0U;
    }
}

NSlog the value for string0 and string1 to get to know CPU measurement and call this method through timer.

Praveen-K
  • 3,401
  • 1
  • 23
  • 31
1

You can use the Time Profiler instrument instead of the Sampler instrument. They record similar information.

If you need to use the Sampler instrument for iOS, choose the OpenGL ES Driver template. The OpenGL ES Driver template includes the Sampler instrument. After choosing the OpenGL ES Driver template, select the OpenGL ES Driver instrument from the instrument list and press the Delete (Backspace) key to remove the instrument from the document.

Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
1

The Sampler instrument is where it always has been in Instruments. To get to it, start Instruments and either use a blank profiling session or start from something like the OpenGL ES Driver template (as Mark suggests), which already has the Sampler instrument within it. To add the Sampler instrument to your template, click on the Library toolbar option and drag the Sampler into your template.

Personally, I prefer using Time Profiler, for reasons that I describe in this answer.

Community
  • 1
  • 1
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
  • the difference between Time Profiler and CPU Sampler is that the former is about the time it takes the functions to run, not the CPU usage it takes to run a function – keep on you Apr 01 '12 at 13:09
  • @ZiadTamim - Did you read the portion of the Instruments user guide I quoted in the above-linked answer? The difference between the two is more subtle than that. They both stop an application at regular intervals and sample the stack trace at that point. Both measure time spent on the CPU by something, which is a pretty good indication of CPU usage by that method or function. – Brad Larson Apr 01 '12 at 15:52
0

You have the run button in the top left which looks like a play button. If you click on that a drop down menu will appear, select profile. This will open up “Instruments” where you can select “Time Profiler”. If the app does not launch automatically in the simulator click Record.

I would recommend check the “Hide System Libraries” and “Show Obj-C only” checkboxes so you can see your code.

*Note if you double click on a function it will show you a percentage break down of how much time its spending on a bit of code, which is quite handy.

woot586
  • 3,906
  • 10
  • 32
  • 40
  • I didn't talk about “Time Profiler”, i was talking about the "CPU Sampler" where i can find it in xcode 4.3 – keep on you Mar 31 '12 at 14:06