一、在工作的代碼有一段while-True輪訓的邏輯,循環中主要的工作是阻塞的IO
代碼大概如下:
dispatch_async(dispatch_get_global_queue(0, 0), ^{
while (YES) {
NSLog(@"CPU %f", cpu_usage());
[NSThread sleepForTimeInterval:1];
}
});
正常的時候,因為io的阻塞關系,線程會在io的方法處等待返回,偶然發現特定情況下,阻塞io方法直接放回錯誤,這樣會不停的進行死循環,因此我想看看死循環會帶來什么問題
我寫了下面的代碼
//
// ViewController.m
// TestCPU
//
// Created by lilun on 2019/3/20.
// Copyright © 2019年 lilun. All rights reserved.
//
#import "ViewController.h"
#import <mach/mach.h>
#import <assert.h>
float cpu_usage()
{
kern_return_t kr;
task_info_data_t tinfo;
mach_msg_type_number_t task_info_count;
task_info_count = TASK_INFO_MAX;
kr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)tinfo, &task_info_count);
if (kr != KERN_SUCCESS) {
return -1;
}
task_basic_info_t basic_info;
thread_array_t thread_list;
mach_msg_type_number_t thread_count;
thread_info_data_t thinfo;
mach_msg_type_number_t thread_info_count;
thread_basic_info_t basic_info_th;
uint32_t stat_thread = 0; // Mach threads
basic_info = (task_basic_info_t)tinfo;
// get threads in the task
kr = task_threads(mach_task_self(), &thread_list, &thread_count);
if (kr != KERN_SUCCESS) {
return -1;
}
if (thread_count > 0)
stat_thread += thread_count;
long tot_sec = 0;
long tot_usec = 0;
float tot_cpu = 0;
int j;
for (j = 0; j < (int)thread_count; j++)
{
thread_info_count = THREAD_INFO_MAX;
kr = thread_info(thread_list[j], THREAD_BASIC_INFO,
(thread_info_t)thinfo, &thread_info_count);
if (kr != KERN_SUCCESS) {
return -1;
}
basic_info_th = (thread_basic_info_t)thinfo;
if (!(basic_info_th->flags & TH_FLAGS_IDLE)) {
tot_sec = tot_sec + basic_info_th->user_time.seconds + basic_info_th->system_time.seconds;
tot_usec = tot_usec + basic_info_th->user_time.microseconds + basic_info_th->system_time.microseconds;
tot_cpu = tot_cpu + basic_info_th->cpu_usage / (float)TH_USAGE_SCALE * 100.0;
}
} // for each thread
kr = vm_deallocate(mach_task_self(), (vm_offset_t)thread_list, thread_count * sizeof(thread_t));
assert(kr == KERN_SUCCESS);
return tot_cpu;
}
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.3
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self runOneThread:1];
// [self runOneThread:2];
// [self runOneThread:3];
// [self runOneThread:4];
});
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100)];
label.textColor = [UIColor redColor];
label.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:label];
label.center = self.view.center;
label.tag = 1234;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
while (YES) {
NSLog(@"CPU %f", cpu_usage());
dispatch_async(dispatch_get_main_queue(), ^{
UILabel *label = [self.view viewWithTag:1234];
label.text = [NSString stringWithFormat:@"CPU %f", cpu_usage()];
});
[NSThread sleepForTimeInterval:1];
}
});
}
- (void)runOneThread:(NSInteger)i
{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
while (YES) {
NSLog(@"run thread %ld", i);
}
});
}
@end
上面的代碼如果運行在模擬器的時候,大概4核的cpu(400%)會占據40%的CPU,還不是很多,貌似模擬器的CPU占用會限制。
此時電腦風扇會呼呼轉。
上面的代碼是真機調試的狀態下,會出現調試器卡死,Xcode無反應,必須拔掉USB線來中斷調試。
猜測原因是循環輸出的log占滿了USB通信的帶寬,導致調試信息無法傳遞到Xcode上。或者是調試線程因為工作線程導致無法工作正常
如果是非調試模式下面,大概會占用98%(以上的方法測試出來)的CPU,機器會發燙。
應該極力避免出現死循環的情況,即使是在子線程中出現的死循環。
總結:
死循環在子線程也是不能出現的,任何方法一定要限制好頻率。
NSlog會產生系統中斷,頻繁的系統中斷也會導致性能低下。
