Skip to content

Commit

Permalink
Merge branch 'feature/average' into develop
Browse files Browse the repository at this point in the history
* feature/average:
  FPS retain one decimal
  Add average storage
  • Loading branch information
haowenwu committed Jun 9, 2021
2 parents dc9c4a9 + e09d724 commit dbfad32
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 130 deletions.
9 changes: 1 addition & 8 deletions MTHawkeye/Core/MTHawkeyeAppStat.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,7 @@ NS_ASSUME_NONNULL_BEGIN

@interface MTHawkeyeAppStat : NSObject

/// Used memory by app in byte. ( task_basic_info.resident_size )
@property (nonatomic, readonly, class) int64_t memoryAppUsed;

/* The real physical memory used by app.
- https://stackoverflow.com/questions/9660763/whats-the-right-statistic-for-ios-memory-footprint-live-bytes-real-memory-ot
- https://developer.apple.com/library/archive/technotes/tn2434/_index.html
*/
@property (nonatomic, readonly, class) int64_t memoryFootprint;
@property (nonatomic, readonly, class) int64_t memory;

@property (nonatomic, readonly, class) double cpuUsedByAllThreads;

Expand Down
8 changes: 8 additions & 0 deletions MTHawkeye/Core/MTHawkeyeAppStat.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@

@implementation MTHawkeyeAppStat

+ (int64_t)memory {
int64_t memory = self.memoryFootprint;
if (memory) {
return memory;
}
return self.memoryAppUsed;
}

+ (int64_t)memoryAppUsed {
struct task_basic_info info;
mach_msg_type_number_t size = (sizeof(task_basic_info_data_t) / sizeof(natural_t));
Expand Down
26 changes: 26 additions & 0 deletions MTHawkeye/Core/MTHawkeyeAverageStorage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// Copyright (c) 2008-present, Meitu, Inc.
// All rights reserved.
//
// This source code is licensed under the license found in the LICENSE file in
// the root directory of this source tree.
//
// Created on: 2020/11/13
// Created by: whw
//


#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface MTHawkeyeAverageStorage : NSObject

+ (void)recordMem:(CGFloat)memory;
+ (void)recordCPU:(double)cpu;
+ (void)recordFPS:(int)fps;
+ (void)recordglFPS:(int)fps;

@end

NS_ASSUME_NONNULL_END
97 changes: 97 additions & 0 deletions MTHawkeye/Core/MTHawkeyeAverageStorage.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//
// Copyright (c) 2008-present, Meitu, Inc.
// All rights reserved.
//
// This source code is licensed under the license found in the LICENSE file in
// the root directory of this source tree.
//
// Created on: 2020/11/13
// Created by: whw
//


#import "MTHawkeyeAverageStorage.h"
#import "MTHawkeyeUtility.h"
#import "MTHawkeyeLogMacros.h"
#import <sys/mman.h>

static const char *_fileChar = NULL;

static CGFloat _memTotal = 0.0;
static int _memCount = 0;
static double _cpuTotal = 0.0;
static int _cpuCount = 0;
static int _fpsTotal = 0;
static int _fpsCount = 0;
static int _glfpsTotal = 0;
static int _glfpsCount = 0;

@implementation MTHawkeyeAverageStorage

+ (void)setupIfNeeded {
if (_fileChar) {
return;
}
NSString *storagePath = [MTHawkeyeUtility currentStorePath];
NSString *path = [storagePath stringByAppendingPathComponent:@"average"];
if (![[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil]) {
MTHLogWarn(@"[Average] create directory %@ failed", path);
return;
}
const char *filePath = path.UTF8String;
if (!filePath) {
return;
}
int fd = open(filePath, O_CREAT | O_RDWR, 0666);
if (fd < 0) {
MTHLogWarn(@"[Average] failed to open %@", path);
return;
}
size_t sz = getpagesize();
char *ptr = (char *)mmap(NULL, sz, PROT_WRITE, MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) {
MTHLogWarn(@"[Average] failed to mmap %@, size: %@", path, @(sz));
return;
}
ftruncate(fd, sz);
_fileChar = ptr;
}

+ (void)record {
[self setupIfNeeded];
if (_fileChar) {
const char *record = [NSString stringWithFormat:@"\357\273\277CPU: %.1f%%\nMemory: %.1fM\nFPS: %.1f\nglFPS: %.1f\n",
_cpuTotal / MAX(_cpuCount, 1),
_memTotal / MAX(_memCount, 1),
(CGFloat)_fpsTotal / MAX(_fpsCount, 1),
(CGFloat)_glfpsTotal / MAX(_glfpsCount, 1)
].UTF8String;
memcpy((void *)_fileChar, (void *)record, strlen(record));
}
}

+ (void)recordMem:(CGFloat)memory {
_memCount++;
_memTotal += memory;
[self record];
}

+ (void)recordCPU:(double)cpu {
_cpuCount++;
_cpuTotal += cpu * 100.f;
[self record];
}

+ (void)recordFPS:(int)fps {
_fpsCount++;
_fpsTotal += fps;
[self record];
}

+ (void)recordglFPS:(int)fps {
_glfpsCount++;
_glfpsTotal += fps;
[self record];
}

@end
13 changes: 5 additions & 8 deletions MTHawkeye/Core/MTHawkeyeClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#import "MTHawkeyeLogMacros.h"
#import "MTHawkeyeStorage.h"
#import "MTHawkeyeUtility.h"

#import "MTHawkeyeAverageStorage.h"

@interface MTHawkeyeClient ()

Expand Down Expand Up @@ -203,18 +203,14 @@ - (void)doBuildInFlushStatusTasks {

// record memory usage
if ([MTHawkeyeUserDefaults shared].recordMemoryUsage) {
static CGFloat preResident = 0.f;
static CGFloat preMemFootprint = 0.f;
CGFloat resident = MTHawkeyeAppStat.memoryAppUsed / 1024.f / 1024.f;
CGFloat memFootprint = MTHawkeyeAppStat.memoryFootprint / 1024.f / 1024.f;
if (forceFlush || (fabs(resident - preResident) > DBL_EPSILON) || (fabs(memFootprint - preMemFootprint) > DBL_EPSILON)) {
preResident = resident;
CGFloat memFootprint = MTHawkeyeAppStat.memory / 1024.f / 1024.f;
[MTHawkeyeAverageStorage recordMem:memFootprint];
if (forceFlush || (fabs(memFootprint - preMemFootprint) > DBL_EPSILON)) {
preMemFootprint = memFootprint;

NSString *residentStr = [NSString stringWithFormat:@"%.2f", resident];
NSString *memFootprintStr = [NSString stringWithFormat:@"%.2f", memFootprint];

[[MTHawkeyeStorage shared] asyncStoreValue:residentStr withKey:time inCollection:@"mem"];
[[MTHawkeyeStorage shared] asyncStoreValue:memFootprintStr withKey:time inCollection:@"r-mem"];
}
}
Expand All @@ -223,6 +219,7 @@ - (void)doBuildInFlushStatusTasks {
if ([MTHawkeyeUserDefaults shared].recordCPUUsage) {
static double preCPUUsage = 0.f;
double cpuUsage = MTHawkeyeAppStat.cpuUsedByAllThreads;
[MTHawkeyeAverageStorage recordCPU:cpuUsage];
if (forceFlush || (fabs(cpuUsage - preCPUUsage) > DBL_EPSILON)) {
preCPUUsage = cpuUsage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ - (void)startANRTrace {
// enable anr trace buffer, needed for tracing hard stall(stall then killed by watchdog or user)
if (![MTHANRTracingBufferRunner isTracingBufferRunning]) {
NSString *storagePath = [MTHawkeyeUtility currentStorePath];
if (![[NSFileManager defaultManager] fileExistsAtPath:storagePath]) {
[MTHawkeyeStorage shared];
}
NSString *path = [storagePath stringByAppendingPathComponent:@"anr_tracing_buffer"];
[MTHANRTracingBufferRunner enableTracingBufferAtPath:path];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#import "MTHFPSTrace.h"
#import "MTHawkeyeClient.h"
#import "MTHawkeyeStorage.h"
#import "MTHawkeyeAverageStorage.h"
#import "MTHawkeyeUserDefaults.h"
#import "MTHawkeyeUtility.h"

Expand Down Expand Up @@ -91,6 +92,7 @@ - (void)receivedFlushStatusCommand {
{
static NSInteger preFPS = 0;
NSInteger curFPSValue = [MTHFPSTrace shared].fpsValue;
[MTHawkeyeAverageStorage recordFPS:(int)curFPSValue];
BOOL fpsChanged = (preFPS != curFPSValue);
if (fpsChanged || forceFlush) {
NSString *fpsValue = [NSString stringWithFormat:@"%@", @(curFPSValue)];
Expand All @@ -103,14 +105,14 @@ - (void)receivedFlushStatusCommand {
if ([MTHFPSTrace shared].gpuImageViewFPSEnable) {
static NSInteger preGPUImageFPS = 0;
NSInteger curGPUImageFPSValue = self.firstActiveRenderCounter.fpsValue;
BOOL gpuImageDisplaying = self.firstActiveRenderCounter.isActive;
[MTHawkeyeAverageStorage recordglFPS:(int)curGPUImageFPSValue];
BOOL gpuImageFPSChanged = (preGPUImageFPS != curGPUImageFPSValue);
if (gpuImageFPSChanged || forceFlush) {
// 平时记录时只记录了变化时的数据,这里补充记录退出前的最后一次数据
if (preGPUImageFPS > 0 && curGPUImageFPSValue == 0) {
[[MTHawkeyeStorage shared] asyncStoreValue:[NSString stringWithFormat:@"%@", @(preGPUImageFPS)] withKey:time inCollection:@"gl-fps"];
}

BOOL gpuImageDisplaying = self.firstActiveRenderCounter.isActive;
if (gpuImageDisplaying && curGPUImageFPSValue > 0.f) {
NSString *glfps = [NSString stringWithFormat:@"%@", @(curGPUImageFPSValue)];
[[MTHawkeyeStorage shared] asyncStoreValue:glfps withKey:time inCollection:@"gl-fps"];
Expand Down
4 changes: 1 addition & 3 deletions MTHawkeye/UISkeleton/MTHawkeyeBuildinFloatingWidget.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ - (BOOL)widgetHiddenDefault {
}

- (void)receivedFlushStatusCommand {
NSInteger fp = (NSInteger)round(MTHawkeyeAppStat.memoryFootprint / 1024 / 1024.f);
if (fp == 0)
fp = MTHawkeyeAppStat.memoryAppUsed / 1024.f / 1024.f;
NSInteger fp = (NSInteger)round(MTHawkeyeAppStat.memory / 1024 / 1024.f);
if (self.preMemFootPrint == fp)
return;

Expand Down
Loading

0 comments on commit dbfad32

Please sign in to comment.