Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Report App Memory Usage #2027

Merged
merged 4 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- Report App Memory Usage (#2027)
- Include app permissions with event (#1984)

## 7.23.0
Expand Down
12 changes: 12 additions & 0 deletions Sources/Sentry/SentryClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,12 @@ - (void)storeFreeMemoryToDeviceContext:(SentryEvent *)event
block:^(NSMutableDictionary *device) {
device[SentryDeviceContextFreeMemoryKey] = @(self.crashWrapper.freeMemory);
}];

[self modifyContext:event
key:@"app"
block:^(NSMutableDictionary *app) {
app[SentryDeviceContextAppMemoryKey] = @(self.crashWrapper.appMemory);
}];
}

- (void)removeFreeMemoryFromDeviceContext:(SentryEvent *)event
Expand All @@ -711,6 +717,12 @@ - (void)removeFreeMemoryFromDeviceContext:(SentryEvent *)event
block:^(NSMutableDictionary *device) {
[device removeObjectForKey:SentryDeviceContextFreeMemoryKey];
}];

[self modifyContext:event
key:@"app"
block:^(NSMutableDictionary *app) {
[app removeObjectForKey:SentryDeviceContextAppMemoryKey];
}];
}

- (void)modifyContext:(SentryEvent *)event
Expand Down
13 changes: 13 additions & 0 deletions Sources/Sentry/SentryCrashWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#import <SentryCrashCachedData.h>
#import <SentryCrashDebug.h>
#import <SentryCrashMonitor_System.h>
#include <mach/mach.h>

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -75,6 +76,18 @@ - (uint64_t)freeMemory
return sentrycrashcm_system_freememory();
}

- (uint64_t)appMemory
{
task_vm_info_data_t info;
mach_msg_type_number_t size = TASK_VM_INFO_COUNT;
kern_return_t kerr = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&info, &size);
if (kerr == KERN_SUCCESS) {
return info.internal + info.compressed;
}

return 0;
}

@end

NS_ASSUME_NONNULL_END
1 change: 1 addition & 0 deletions Sources/Sentry/include/SentryCrashIntegration.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ NS_ASSUME_NONNULL_BEGIN
@class SentryScope, SentryCrashWrapper;

static NSString *const SentryDeviceContextFreeMemoryKey = @"free_memory";
static NSString *const SentryDeviceContextAppMemoryKey = @"app_memory";

@interface SentryCrashIntegration : NSObject <SentryIntegrationProtocol>

Expand Down
2 changes: 2 additions & 0 deletions Sources/Sentry/include/SentryCrashWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ SENTRY_NO_INIT

- (uint64_t)freeMemory;

- (uint64_t)appMemory;

@end

NS_ASSUME_NONNULL_END
15 changes: 15 additions & 0 deletions Tests/SentryTests/SentryClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ class SentryClientTest: XCTestCase {
assertLastSentEventWithAttachment { event in
XCTAssertEqual(oomEvent.eventId, event.eventId)
XCTAssertNil(event.context?["device"]?["free_memory"])
XCTAssertNil(event.context?["device"]?["app_memory"])
}
}

Expand Down Expand Up @@ -521,6 +522,20 @@ class SentryClientTest: XCTestCase {
}
}

func testCaptureCrash_AppMemory() {
let event = TestData.event
event.threads = nil
event.debugMeta = nil

fixture.crashWrapper.internalAppMemory = 123_456
fixture.getSut().captureCrash(event, with: fixture.scope)

assertLastSentEventWithAttachment { actual in
let eventAppMemory = actual.context?["app"]?["app_memory"] as? Int
XCTAssertEqual(eventAppMemory, 123_456)
}
}

func testCaptureCrash_Permissions() {
fixture.permissionsObserver.internalLocationPermissionStatus = SentryPermissionStatus.granted
fixture.permissionsObserver.internalPushPermissionStatus = SentryPermissionStatus.granted
Expand Down
2 changes: 2 additions & 0 deletions Tests/SentryTests/SentryCrash/TestSentryCrashWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ SENTRY_NO_INIT

@property (nonatomic, assign) uint64_t internalFreeMemory;

@property (nonatomic, assign) uint64_t internalAppMemory;

@end

NS_ASSUME_NONNULL_END
6 changes: 6 additions & 0 deletions Tests/SentryTests/SentryCrash/TestSentryCrashWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ + (instancetype)sharedInstance
instance.installAsyncHooksCalled = NO;
instance.closeCalled = NO;
instance.internalFreeMemory = 0;
instance.internalAppMemory = 0;
return instance;
}

Expand Down Expand Up @@ -63,4 +64,9 @@ - (uint64_t)freeMemory
return self.internalFreeMemory;
}

- (uint64_t)appMemory
{
return self.internalAppMemory;
}

@end