After the App runs as a process, there will be multiple threads, and each thread has different CPU usage. The sum of CPU usage of each thread is the CPU usage of the current App. With this in mind, the idea of online MONITORING of CPU usage is clear.
On iOS, You can do this in /usr/include/mach/thread_info.h (macOS Catalina) As/Library/Developer/CommandLineTools/SDKs/MacOSX SDK/usr/include/Mach/thread_info h) see the structure of the basic information of the thread, Cpu_usage is the CPU usage. The complete code for the structure is as follows:
struct thread_basic_info { time_value_t user_time; // User running time time_value_t system_time; // System running time integer_t cpu_usage; // CPU usage policy_t policy; // Scheduling policy integer_t run_state; // Running state integer_t flags; // Various flags integer_t suspend_count; // Count the number of paused threads integer_t sleep_time; // Sleep time};Copy the code