Skip to content

Commit

Permalink
Add memoryPhysicalFootprint and memoryPhysicalFootprintString to …
Browse files Browse the repository at this point in the history
…`CSSystemInfoProvider`
  • Loading branch information
thecatalinstan committed Jun 18, 2021
1 parent 3a93c27 commit 2c6943f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ FOUNDATION_EXPORT NSString * const CSSystemInfoHelperIPAddressNone DEPRECATED_AT
/// @c -memoryUsage
@property (nonatomic, readonly, copy) NSString *memoryUsageString;

/// Gets the physical memory footprint size of the process, as reported by
/// @c task_info
// @see https://www.gnu.org/software/hurd/gnumach-doc/Task-Information.html
@property (nonatomic, readonly) vm_size_t memotyPhysicalFootprint;

/// A human-readable, formatted byte count string. from the value returned by
/// @c -memotyPhysicalFootprint
@property (nonatomic, readonly, copy) NSString *memotyPhysicalFootprintString;

/// @name UUID of the current device

/// Get the UUID of the current device
Expand Down
16 changes: 15 additions & 1 deletion CSSystemInfoHelper/Sources/CSSystemInfoHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ - (NSString *)systemVersionString {
- (vm_size_t)memoryUsage {
NSError *error;
vm_size_t memoryUsage = 0;
if (!([self.systemInfoProvider getResidentSize:&memoryUsage error:&error])) {
if (![self.systemInfoProvider getResidentSize:&memoryUsage error:&error]) {
NSLog(@"Error getting resident size: %@. %@.", error.localizedDescription, error.localizedFailureReason);
return 0;
}
Expand All @@ -118,6 +118,20 @@ - (NSString *)memoryUsageString {
return [self formatByteCount:self.memoryUsage];
}

- (vm_size_t)memotyPhysicalFootprint {
NSError *error;
vm_size_t memotyPhysicalFootprint = 0;
if (![self.systemInfoProvider getPhysFootprint:&memotyPhysicalFootprint error:&error]) {
NSLog(@"Error getting physical memory footprint: %@. %@.", error.localizedDescription, error.localizedFailureReason);
return 0;
}
return memotyPhysicalFootprint;
}

- (NSString *)memotyPhysicalFootprintString {
return [self formatByteCount:self.memotyPhysicalFootprint];
}

#if TARGET_OS_OSX
- (NSString *)platformUUID {
static NSString* platformUUID;
Expand Down
1 change: 1 addition & 0 deletions CSSystemInfoHelper/Sources/CSSystemInfoProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ NS_ASSUME_NONNULL_BEGIN
- (nullable NSArray<CSNetworkInterface *> *)queryNetworkInterfaces:(NSError *__autoreleasing *)error NS_WARN_UNUSED_RESULT;

- (BOOL)getResidentSize:(vm_size_t *)residentSize error:(NSError *__autoreleasing *)error NS_WARN_UNUSED_RESULT;
- (BOOL)getPhysFootprint:(vm_size_t *)physFootprint error:(NSError *__autoreleasing *)error NS_WARN_UNUSED_RESULT;

@end

Expand Down
18 changes: 18 additions & 0 deletions CSSystemInfoHelper/Sources/CSSystemInfoProvider.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,22 @@ - (BOOL)getResidentSize:(vm_size_t *)residentSize error:(NSError *__autoreleasin
return YES;
}

- (BOOL)getPhysFootprint:(vm_size_t *)physFootprint error:(NSError *__autoreleasing _Nullable *)error {
struct task_vm_info info = {0};

kern_return_t ret;
if (KERN_SUCCESS != (ret = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&info, TASK_VM_INFO_COUNT))) {
if (error) {
*error = CSMachError(ret, nil, nil);
}
return NO;
}

if (physFootprint) {
*physFootprint = info.phys_footprint;
}

return YES;
}

@end

0 comments on commit 2c6943f

Please sign in to comment.