Mobile Development 6 min read

iOS CPU Power Monitoring: Understanding CPU Architecture and Implementing QiCPUMonitor

This article explains iOS CPU architecture, why arm64 dominates iPhone hardware, compares CPU and GPU, and provides a step‑by‑step guide with Objective‑C code for implementing the QiCPUMonitor tool that tracks high‑CPU‑usage threads and records their call stacks every three seconds.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
iOS CPU Power Monitoring: Understanding CPU Architecture and Implementing QiCPUMonitor

The author, inspired by a recent performance‑monitoring talk, outlines practical iOS performance‑monitoring techniques, starting with an overview of CPU fundamentals and the three main components—ALU, control unit, and registers.

It describes the four core functions of a CPU (instruction processing, operation execution, timing control, data handling) and notes that modern iPhones primarily use the low‑power ARM64 architecture, while Intel x86 is rare due to higher power consumption.

A brief comparison highlights that GPUs are dedicated image processors that render pixels, whereas CPUs handle general computation.

The article then introduces the QiCPUMonitor module for CPU‑power monitoring, explaining its workflow: obtain the current task, retrieve all live thread information, set a CPU‑usage threshold, and store the call‑stack of any thread exceeding that threshold.

It defines the struct thread_basic_info used to hold per‑thread metrics such as user_time , system_time , cpu_usage , policy , run_state , flags , suspend_count , and sleep_time :

struct thread_basic_info { time_value_t user_time; // user runtime time_value_t system_time; // system runtime integer_t cpu_usage; // CPU usage (max 1000) policy_t policy; // scheduling policy integer_t run_state; // running, stopped, etc. integer_t flags; // thread flags integer_t suspend_count;// suspend count integer_t sleep_time; // sleep time (seconds) };

Three key variables are declared: thread_act_array_t threads to hold the thread array, mach_msg_type_number_t threadCount = 0 for the number of threads, and task_t thisTask = mach_task_self() to reference the current task.

The code calls task_threads(thisTask, &threads, &threadCount) and checks the return value against KERN_SUCCESS . If successful, it iterates over each thread, retrieves thread_basic_info , skips idle threads, computes cpuUsage = threadBaseInfo->cpu_usage / 10 , and when cpuUsage exceeds CPUMONITORRATE , captures the stack with qiStackOfThread(threads[i]) , stores it in a QiCallStackModel , saves it to the database, and logs the information.

To minimize impact on the app, the monitoring runs on an NSTimer that fires every three seconds, invoking updateCPUInfo to repeat the check.

The article concludes with a reference to the full QiLagMonitor source code and a reminder to share and follow the author for more content.

performanceiOSObjective‑CCPU monitoringQiCPUMonitorthread info
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.