There has been a lot of the same question, but yet I cannot find great answers. I've looked at codes from this particular post which gives me usage per core, but it is inaccurate, always stuck at 14% or so even when Instrument tells me one core is under full load (primality test!).
I also read this question which I actually got the code from.
I have little knowledge of sysctl, which seems to be the key for these kind of things. Can someone explain to me how I could achieve that, code example will be even more appreciated. Thanks!
This is my current code, it was ran on a separate thread, which I just realized could be a problem, can anyone confirm? (I use NSThread and NSNotificationCenter to send usage information to main thread.)
- (void)updateCPUUsage
{
NSMutableArray* arrayOfCPUUsage = [NSMutableArray array];
while ( 1 )
{
processor_info_array_t _cpuInfo, _prevCPUInfo = nil;
mach_msg_type_number_t _numCPUInfo, _numPrevCPUInfo = 0;
unsigned _numCPUs;
NSLock *_cpuUsageLock;
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];
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) {
Float32 _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];
}
[arrayOfCPUUsage addObject:#(_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 = nil;
_numCPUInfo = 0U;
}
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:#"CPUUsageUpdate" object:[arrayOfCPUUsage copy]]];
[arrayOfCPUUsage removeAllObjects];
usleep(1000);
}
}
Related
I have a problem with my EA trades, now I will show you the code with a screenshot of a trade (it happens often)
I see that I was definitely wrong for how the entry below must read I put an image where it explains what I have in mind
void OnTimer()
{
//---
if (currBars != Bars){
calculate_price_levels();
//debug_levels();
check_nearest_levels();
if (is_session()){
if (Close[2] < Nearest_Up_Level && Close[1] >= Nearest_Up_Level && isAvailableOrder()){
//Print("BUY entry, Nearest_Up_Level:",Nearest_Up_Level, ",Close[2]:",Close[2], ",Close[1]:",Close[1]);
double lot = _get_lot_size (Stop_Loss);
OpenTrade(Symbol(), OP_BUY, lot);
}
if (Close[2] > Nearest_Down_Level && Close[1] <= Nearest_Down_Level && isAvailableOrder()){
//Print("SELL entry, Nearest_Down_Level:",Nearest_Down_Level, ",Close[2]:",Close[2], ",Close[1]:",Close[1]);
double lot = _get_lot_size (Stop_Loss);
OpenTrade(Symbol(), OP_SELL, lot);
}
}
currBars = Bars;
}
if (Trailing_Stop > 0) trail_sl();
// if (BE > 0) check_be();
int trades_number = _get_number_of_trades();
if (trades_number < Trades_Number_0) check_last_trade_result();
Trades_Number_0 = trades_number;
}
void check_nearest_levels(){
int size = ArraySize(Price_Levels);
for (int i = 0; i < size; i++){
if (Close[2] > Price_Levels[i] && Close[2] < Price_Levels[i + 1]){
Nearest_Down_Level = Price_Levels[i];
Nearest_Up_Level = Price_Levels[i + 1];
BUY_TP_Price = 0;
if (size > i + 2) BUY_TP_Price = Price_Levels[i + 2];
else BUY_TP_Price = Nearest_Up_Level + StepPips * Point * mp;
Next_BUY_TP_Price = 0;
if (size > i + 3) Next_BUY_TP_Price = Price_Levels[i + 3];
else Next_BUY_TP_Price = BUY_TP_Price + StepPips * Point * mp;
SELL_TP_Price = 0;
if (i >= 1) SELL_TP_Price = Price_Levels[i - 1];
else SELL_TP_Price = Nearest_Down_Level - StepPips * Point * mp;
Next_SELL_TP_Price = 0;
if (i >= 2) Next_SELL_TP_Price = Price_Levels[i - 2];
else Next_SELL_TP_Price = SELL_TP_Price - StepPips * Point * mp;
break;
}
}
}
Example of what I want to do
In the code bellow , Why we need slices ? and what does it for ?
//https://github.com/danginsburg/opengles-book-samples/blob/604a02cc84f9cc4369f7efe93d2a1d7f2cab2ba7/iPhone/Common/esUtil.h#L110
int esGenSphere(int numSlices, float radius, float **vertices,
float **texCoords, uint16_t **indices, int *numVertices_out) {
int numParallels = numSlices / 2;
int numVertices = (numParallels + 1) * (numSlices + 1);
int numIndices = numParallels * numSlices * 6;
float angleStep = (2.0f * ES_PI) / ((float) numSlices);
if (vertices != NULL) {
*vertices = malloc(sizeof(float) * 3 * numVertices);
}
if (texCoords != NULL) {
*texCoords = malloc(sizeof(float) * 2 * numVertices);
}
if (indices != NULL) {
*indices = malloc(sizeof(uint16_t) * numIndices);
}
for (int i = 0; i < numParallels + 1; i++) {
for (int j = 0; j < numSlices + 1; j++) {
int vertex = (i * (numSlices + 1) + j) * 3;
if (vertices) {
(*vertices)[vertex + 0] = radius * sinf(angleStep * (float)i) * sinf(angleStep * (float)j);
(*vertices)[vertex + 1] = radius * cosf(angleStep * (float)i);
(*vertices)[vertex + 2] = radius * sinf(angleStep * (float)i) * cosf(angleStep * (float)j);
}
if (texCoords) {
int texIndex = (i * (numSlices + 1) + j) * 2;
(*texCoords)[texIndex + 0] = (float)j / (float)numSlices;
(*texCoords)[texIndex + 1] = 1.0f - ((float)i / (float)numParallels);
}
}
}
// Generate the indices
if (indices != NULL) {
uint16_t *indexBuf = (*indices);
for (int i = 0; i < numParallels ; i++) {
for (int j = 0; j < numSlices; j++) {
*indexBuf++ = i * (numSlices + 1) + j;
*indexBuf++ = (i + 1) * (numSlices + 1) + j;
*indexBuf++ = (i + 1) * (numSlices + 1) + (j + 1);
*indexBuf++ = i * (numSlices + 1) + j;
*indexBuf++ = (i + 1) * (numSlices + 1) + (j + 1);
*indexBuf++ = i * (numSlices + 1) + (j + 1);
}
}
}
if (numVertices_out) {
*numVertices_out = numVertices;
}
return numIndices;
}
That code generates a sphere mesh that looks like this:
Source: https://commons.wikimedia.org/wiki/File:Sphere_wireframe_10deg_6r.svg CC BY 3.0
As you can see in the picture, there are horizontal parallel lines, and vertical lines which all meet at the poles. The horizontal lines are typically called parallels whereas the vertical ones are called meridians. The author of that code apparently didn't know this term, so they called it "slices" instead.
I've managed to decode and play H264 videos, however I'm having a difficult time with MPEG4 videos.
What CMVideoFormatDescription extensions does it need? I'm getting -8971 error (codecExtensionNotFoundErr) when trying to create a VTDecompressionSession.
This is how I create a VideoFormatDescription
OSStatus success = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
self.mediaCodec,
message.frameSize.width,
message.frameSize.height,
NULL,
&mediaDescriptor);
Instead of that NULL, I assume I need to specify a CFDictionaryRef, however I don't know what it should contain. Any idea?
After much pain and agony, I've finally managed to make it work.
I need to provide a CFDictionaryRef with at least a value for the kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms key. The value for this key also has to be a CFDictionaryRef. For H264 types this is created inside the CMVideoFormatDescriptionCreateFromH264ParameterSets and looks like this:
avcC = <014d401e ffe10016 674d401e 9a660a0f ff350101 01400000 fa000013 88010100 0468ee3c 80>
However for the MPEG4 type, you need to create this on your own. The end result should look like this:
esds = <00000000 038081e6 00000003 8081e611 00000000 00000000 058081e5 060102>
Now the way to create this is still fuzzy to me, however it somehow works. I was inspired by this link. This is the code:
- (CMFormatDescriptionRef)createFormatDescriptorFromMPEG4Message:(MessageContainer *)message {
CMVideoFormatDescriptionRef mediaDescriptor = NULL;
NSData *esdsData = [self newESDSFromData:message.frameData];
CFMutableDictionaryRef esdsDictionary = CFDictionaryCreateMutable(kCFAllocatorDefault, 1,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(esdsDictionary, CFSTR("esds"), (__bridge const void *)(esdsData));
NSDictionary *dictionary = #{(__bridge NSString *)kCMFormatDescriptionExtension_SampleDescriptionExtensionAtoms : (__bridge NSDictionary *)esdsDictionary};
OSStatus status = CMVideoFormatDescriptionCreate(kCFAllocatorDefault,
self.mediaCodec,
message.frameSize.width,
message.frameSize.height,
(__bridge CFDictionaryRef)dictionary,
&mediaDescriptor);
if (status) {
NSLog(#"CMVideoFormatDesciprionCreate failed with %zd", status);
}
return mediaDescriptor;
}
- (NSData *)newESDSFromData:(NSData *)data {
NSInteger dataLength = data.length;
int full_size = 3 + 5 + 13 + 5 + dataLength + 3;
// ES_DescrTag data + DecoderConfigDescrTag + data + DecSpecificInfoTag + size + SLConfigDescriptor
int config_size = 13 + 5 + dataLength;
int padding = 12;
int8_t *esdsInfo = calloc(full_size + padding, sizeof(int8_t));
//Version
esdsInfo[0] = 0;
//Flags
esdsInfo[1] = 0;
esdsInfo[2] = 0;
esdsInfo[3] = 0;
//ES_DescrTag
esdsInfo[4] |= 0x03;
[self addMPEG4DescriptionLength:full_size
toPointer:esdsInfo + 5];
//esid
esdsInfo[8] = 0;
esdsInfo[9] = 0;
//Stream priority
esdsInfo[10] = 0;
//DecoderConfigDescrTag
esdsInfo[11] = 0x03;
[self addMPEG4DescriptionLength:config_size
toPointer:esdsInfo + 12];
//Stream Type
esdsInfo[15] = 0x11;
//Buffer Size
esdsInfo[16] = 0;
esdsInfo[17] = 0;
//Max bitrate
esdsInfo[18] = 0;
esdsInfo[19] = 0;
esdsInfo[20] = 0;
//Avg bitrate
esdsInfo[21] = 0;
esdsInfo[22] = 0;
esdsInfo[23] = 0;
//< DecSpecificInfoTag
esdsInfo[24] |= 0x05;
[self addMPEG4DescriptionLength:dataLength
toPointer:esdsInfo + 25];
//SLConfigDescrTag
esdsInfo[28] = 0x06;
//Length
esdsInfo[29] = 0x01;
esdsInfo[30] = 0x02;
NSData *esdsData = [NSData dataWithBytes:esdsInfo length:31 * sizeof(int8_t)];
free(esdsInfo);
return esdsData;
}
- (void)addMPEG4DescriptionLength:(NSInteger)length
toPointer:(int8_t *)ptr {
for (int i = 3; i >= 0; i--) {
uint8_t b = (length >> (i * 7)) & 0x7F;
if (i != 0) {
b |= 0x80;
}
ptr[3 - i] = b;
}
}
The message container is a simple wrapper around the data received from the server:
#interface MessageContainer : NSObject
#property (nonatomic) CGSize frameSize;
#property (nonatomic) NSData *frameData;
#end
Where frameSize is the size of the frame (received separately from the server) and frameData is the data itself.
I am getting the Logic error when I analyzed my code. It is saying that "Argument in message expression is an uninitialized value"
Here is what I have
// allocate symbol
int baseMatrixSize = compact ? 11 + layers * 4 : 14 + layers * 4; // not including alignment lines
int alignmentMap[baseMatrixSize];
int matrixSize;
if (compact) {
// no alignment marks in compact mode, alignmentMap is a no-op
matrixSize = baseMatrixSize;
for (int i = 0; i < baseMatrixSize; i++) {
alignmentMap[i] = i;
}
} else {
matrixSize = baseMatrixSize + 1 + 2 * ((baseMatrixSize / 2 - 1) / 15);
int origCenter = baseMatrixSize / 2;
int center = matrixSize / 2;
for (int i = 0; i < origCenter; i++) {
int newOffset = i + i / 15;
alignmentMap[origCenter - i - 1] = center - newOffset - 1;
alignmentMap[origCenter + i] = center + newOffset + 1;
}
}
ZXBitMatrix *matrix = [[ZXBitMatrix alloc] initWithDimension:matrixSize];
// draw data bits
for (int i = 0, rowOffset = 0; i < layers; i++) {
int rowSize = compact ? (layers - i) * 4 + 9 : (layers - i) * 4 + 12;
for (int j = 0; j < rowSize; j++) {
int columnOffset = j * 2;
for (int k = 0; k < 2; k++) {
if ([messageBits get:rowOffset + columnOffset + k]) {
[matrix setX:alignmentMap[i * 2 + k] y:alignmentMap[i * 2 + j]];
}
if ([messageBits get:rowOffset + rowSize * 2 + columnOffset + k]) {
[matrix setX:alignmentMap[i * 2 + j] y:alignmentMap[baseMatrixSize - 1 - i * 2 - k]];
}
if ([messageBits get:rowOffset + rowSize * 4 + columnOffset + k]) {
[matrix setX:alignmentMap[baseMatrixSize - 1 - i * 2 - k] y:alignmentMap[baseMatrixSize - 1 - i * 2 - j]];
}
if ([messageBits get:rowOffset + rowSize * 6 + columnOffset + k]) {
[matrix setX:alignmentMap[baseMatrixSize - 1 - i * 2 - j] y:alignmentMap[i * 2 + k]];
}
}
}
rowOffset += rowSize * 8;
}
I tried by initializing the matrizSize with 0,but it still gives me the error .
Could you please tell me ,why I am getting this error?
The code below used to scale an image, but the performance is not good. I googled and found some advice to convert the codes to Neon ASM to get better performance.
inline void insetw32(char *pb_dst, char *pb_pFore, char *pb_pBack, char *pmix, int w)
{
register char bbm = (char)(pmix[0]<<24>>24);
for(int i = 0;i< w;i++)
{
*pb_dst++ = (((*pb_pFore++ - *pb_pBack++) * bbm)>>8) + *pb_pBack;
*pb_dst++ = (((*pb_pFore++ - *pb_pBack++) * bbm)>>8) + *pb_pBack;
*pb_dst++ = (((*pb_pFore++ - *pb_pBack++) * bbm)>>8) + *pb_pBack;
*pb_dst++ = (((*pb_pFore++ - *pb_pBack++) * bbm)>>8) + *pb_pBack;
}
}
I tried to translate the code with NEON, the performance is 2x faster than C language, but the same code I used MMX can get 4x faster on x86 platform, is there any other good way to improve that? Or is there any mistake in my NEON code?
uint8x8_t fac1 = vdup_n_u8(bbm);
int16x8_t fac = vmovl_u8(fac1);
w /= 2; // 8/ 4
while (w--)
{
int8x8_t temp8;
int16x8_t temp16;
uint8x8_t rgbaFore = vld1_u8(pb_pFore);
uint8x8_t rgbaBack = vld1_u8(pb_pBack);
uint8x8_t rgbaDst = vld1_u8(pb_dst);
int16x8_t fore = vmovl_u8(rgbaFore);
int16x8_t back = vmovl_u8(rgbaBack);
temp16 = vsubq_s16(fore, back);
temp16 = vmulq_s16(temp16, fac);
temp8 = vshrn_n_s16(temp16, 8);
temp8 = vadd_u8(temp8, rgbaBack);
vst1_u8(pb_dst, temp8);
pb_dst += 8;
pb_pFore += 8;
pb_pBack += 8;
}