Este esPROBLEMA RESUELTO.
En el códigoproyecto de swift crear un nuevo fichero.m
que lo tenéis que enlazar en cuestiónel bridging-Header.h
y llamar desde el archivo swift load()
y todo funcionara.
CODIGO RESEUELTO
#import "ViewController<Foundation/Foundation.h"h>
#include <sys/sysctl.h>
#include <sys/types.h>
#include <mach/mach.h>
#include <mach/processor_info.h>
#include <mach/mach_host.h>
@interface ViewController ()
@end
@implementation ViewController
processor_info_array_t cpuInfo, prevCpuInfo;
mach_msg_type_number_t numCpuInfo, numPrevCpuInfo;
unsigned numCPUs;
NSTimer *updateTimer;
NSLock *CPUUsageLock;
-
void load(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
int mib[2U] = { CTL_HW, HW_NCPU };
size_t sizeOfNumCPUs = sizeof(numCPUs);
int status = sysctl(mib, 2U, &numCPUs, &sizeOfNumCPUs, NULL, 0U);
if(status)
numCPUs = 1;
CPUUsageLock = [[NSLock alloc] init];
[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(updateInfo:) userInfo:nil repeats:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that canCPUUsageLock be= recreated.
}
-[[NSLock (void)updateInfo:(NSTimeralloc] *)timerinit];
{
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];
}
NSLog(@"Core: %u Usage: %f",i,inUse / total);
}
[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;
} else {
NSLog(@"Error!");
}
}
@end