-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #152 from WideSpectrumComputing/MemoryTelemetry
Memory telemetry
- Loading branch information
Showing
27 changed files
with
929 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
RollbarCommon/Sources/RollbarCommon/RollbarMemoryStatsDescriptors.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#import "RollbarMemoryStatsDescriptors.h" | ||
|
||
#pragma mark - Rollbar memorty stats related descriptors | ||
|
||
static NSString *const Rollbar_memory_physical = @"physical"; | ||
static NSString *const Rollbar_memory_physical_totalMB = @"total_MB"; | ||
|
||
static NSString *const Rollbar_memory_vm = @"vm"; | ||
static NSString *const Rollbar_memory_vm_free = @"free_MB"; | ||
static NSString *const Rollbar_memory_vm_active = @"active_MB"; | ||
static NSString *const Rollbar_memory_vm_inactive = @"inactive_MB"; | ||
static NSString *const Rollbar_memory_vm_speculative = @"speculative_MB"; | ||
static NSString *const Rollbar_memory_vm_wired = @"wired_MB"; | ||
|
||
#pragma mark - RollbarMemoryStatsDescriptors class | ||
|
||
static RollbarMemoryStatsDescriptors *singleton = nil; | ||
|
||
@implementation RollbarMemoryStatsDescriptors | ||
{ | ||
@private | ||
NSArray<NSString *> *memoryTypeIndex; | ||
NSArray<NSString *> *physicalMemoryStatsIndex; | ||
NSArray<NSString *> *virtualMemoryStatsIndex; | ||
} | ||
|
||
#pragma mark - Sigleton pattern | ||
|
||
+ (nonnull instancetype)sharedInstance { | ||
|
||
//static RollbarMemoryStatsDescriptors *singleton = nil; | ||
static dispatch_once_t onceToken; | ||
|
||
dispatch_once(&onceToken, ^{ | ||
|
||
singleton = [[[self class] alloc] init]; | ||
singleton->memoryTypeIndex = @[ | ||
// match order of RollbarMemoryStatsType values: | ||
Rollbar_memory_physical, | ||
Rollbar_memory_vm, | ||
]; | ||
singleton->physicalMemoryStatsIndex = @[ | ||
// match order of RollbarPhysicalMemory values: | ||
Rollbar_memory_physical_totalMB, | ||
]; | ||
singleton->virtualMemoryStatsIndex = @[ | ||
// match order of RollbarVirtualMemory values: | ||
Rollbar_memory_vm_free, | ||
Rollbar_memory_vm_active, | ||
Rollbar_memory_vm_inactive, | ||
Rollbar_memory_vm_speculative, | ||
Rollbar_memory_vm_wired, | ||
]; | ||
}); | ||
|
||
return singleton; | ||
} | ||
|
||
+ (BOOL)sharedInstanceExists { | ||
|
||
return (nil != singleton); | ||
} | ||
|
||
#pragma mark - Instance methods | ||
|
||
- (nonnull NSString *)getMemoryStatsTypeDescriptor: (RollbarMemoryStatsType)memoryAttribute { | ||
|
||
return self->memoryTypeIndex[memoryAttribute]; | ||
} | ||
|
||
- (nonnull NSString *)getPhysicalMemoryDescriptor: (RollbarPhysicalMemory)memoryAttribute { | ||
|
||
return self->physicalMemoryStatsIndex[memoryAttribute]; | ||
} | ||
|
||
- (nonnull NSString *)getVirtualMemoryDescriptor: (RollbarVirtualMemory)memoryAttribute { | ||
|
||
return self->virtualMemoryStatsIndex[memoryAttribute]; | ||
} | ||
|
||
@end |
136 changes: 136 additions & 0 deletions
136
RollbarCommon/Sources/RollbarCommon/RollbarMemoryUtil.m
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
#import "RollbarMemoryUtil.h" | ||
#import "RollbarMemoryStatsDescriptors.h" | ||
#import <mach/mach.h> | ||
|
||
static NSByteCountFormatter *formatter = nil; | ||
static const NSInteger bytesInMB = 1024 * 1024; | ||
|
||
@implementation RollbarMemoryUtil | ||
|
||
+ (void)initialize { | ||
|
||
formatter = [[NSByteCountFormatter alloc] init]; | ||
formatter.countStyle = NSByteCountFormatterCountStyleMemory; | ||
formatter.includesActualByteCount = YES; | ||
formatter.adaptive = YES; | ||
formatter.includesUnit = YES; | ||
formatter.zeroPadsFractionDigits = YES; | ||
} | ||
|
||
#pragma mark - memory stats getters | ||
|
||
+ (nonnull NSDictionary<NSString *, NSObject *> *)getMemoryStats { | ||
|
||
NSObject *physicalMemoryStats = [RollbarMemoryUtil getPhysicalMemoryStats]; | ||
NSObject *virtualMemoryStats = [RollbarMemoryUtil getVirtualMemoryStats]; | ||
|
||
return @{ | ||
|
||
[[RollbarMemoryStatsDescriptors sharedInstance] getMemoryStatsTypeDescriptor:RollbarMemoryStatsType_Physical] : | ||
physicalMemoryStats ? physicalMemoryStats : [NSNull null], | ||
[[RollbarMemoryStatsDescriptors sharedInstance] getMemoryStatsTypeDescriptor:RollbarMemoryStatsType_VM] : | ||
virtualMemoryStats ? virtualMemoryStats : [NSNull null], | ||
}; | ||
} | ||
|
||
+ (nullable NSDictionary<NSString *, NSObject *> *)getPhysicalMemoryStats { | ||
|
||
return @{ | ||
[[RollbarMemoryStatsDescriptors sharedInstance] getPhysicalMemoryDescriptor:RollbarPhysicalMemory_TotalMB] : | ||
[NSNumber numberWithUnsignedInteger: [RollbarMemoryUtil getPhysicalMemoryInMBs]], | ||
}; | ||
} | ||
|
||
+ (nullable NSDictionary<NSString *, NSObject *> *)getVirtualMemoryStats { | ||
|
||
vm_statistics_data_t vmStats; | ||
mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT; | ||
kern_return_t kernReturn = host_statistics( | ||
mach_host_self(), | ||
HOST_VM_INFO, | ||
(host_info_t)&vmStats, &infoCount | ||
); | ||
if(kernReturn != KERN_SUCCESS) { | ||
|
||
return nil; | ||
} | ||
|
||
NSUInteger memoryMBs; | ||
|
||
memoryMBs = [RollbarMemoryUtil convertToMBs:(vm_page_size * vmStats.free_count)]; | ||
NSNumber *freeMemory = [NSNumber numberWithUnsignedInteger:[RollbarMemoryUtil convertToMBs:memoryMBs]]; | ||
|
||
memoryMBs = [RollbarMemoryUtil convertToMBs:(vm_page_size * vmStats.active_count)]; | ||
NSNumber *activeMemory = [NSNumber numberWithUnsignedInteger:[RollbarMemoryUtil convertToMBs:memoryMBs]]; | ||
|
||
memoryMBs = [RollbarMemoryUtil convertToMBs:(vm_page_size * vmStats.inactive_count)]; | ||
NSNumber *inactiveMemory = [NSNumber numberWithUnsignedInteger:[RollbarMemoryUtil convertToMBs:memoryMBs]]; | ||
|
||
memoryMBs = [RollbarMemoryUtil convertToMBs:(vm_page_size * vmStats.speculative_count)]; | ||
NSNumber *speculativeMemory = [NSNumber numberWithUnsignedInteger:[RollbarMemoryUtil convertToMBs:memoryMBs]]; | ||
|
||
memoryMBs = [RollbarMemoryUtil convertToMBs:(vm_page_size * vmStats.wire_count)]; | ||
NSNumber *wiredMemory = [NSNumber numberWithUnsignedInteger:[RollbarMemoryUtil convertToMBs:memoryMBs]]; | ||
|
||
return @{ | ||
|
||
[[RollbarMemoryStatsDescriptors sharedInstance] getVirtualMemoryDescriptor:RollbarVirtualMemory_FreeMB] : | ||
freeMemory ? freeMemory : [NSNull null], | ||
[[RollbarMemoryStatsDescriptors sharedInstance] getVirtualMemoryDescriptor:RollbarVirtualMemory_ActiveMB] : | ||
activeMemory ? activeMemory : [NSNull null], | ||
[[RollbarMemoryStatsDescriptors sharedInstance] getVirtualMemoryDescriptor:RollbarVirtualMemory_InactiveMB] : | ||
inactiveMemory ? inactiveMemory : [NSNull null], | ||
[[RollbarMemoryStatsDescriptors sharedInstance] getVirtualMemoryDescriptor:RollbarVirtualMemory_SpeculativeMB] : | ||
speculativeMemory ? speculativeMemory : [NSNull null], | ||
[[RollbarMemoryStatsDescriptors sharedInstance] getVirtualMemoryDescriptor:RollbarVirtualMemory_WiredMB] : | ||
wiredMemory ? wiredMemory : [NSNull null], | ||
}; | ||
} | ||
|
||
+ (NSUInteger)getPhysicalMemoryInBytes { | ||
|
||
static unsigned long long bytesTotal = 0; | ||
|
||
if (0 == bytesTotal) { | ||
|
||
bytesTotal = [[NSProcessInfo processInfo] physicalMemory]; | ||
} | ||
return (NSUInteger)bytesTotal; | ||
} | ||
|
||
#pragma mark - converters | ||
|
||
+ (NSUInteger)convertToMBs:(NSUInteger)bytesCount { | ||
|
||
NSUInteger result = (bytesCount / bytesInMB); | ||
result += (bytesCount % bytesInMB); | ||
return result; | ||
} | ||
|
||
+ (NSString *)convertToHumanFriendlyFormat:(NSUInteger)bytesCount { | ||
|
||
NSString *result = [formatter stringFromByteCount:bytesCount]; | ||
return result; | ||
} | ||
|
||
#pragma mark - convenience methods | ||
|
||
+ (NSUInteger)getPhysicalMemoryInMBs { | ||
|
||
static NSInteger result = 0; | ||
if (0 == result) { | ||
|
||
result = [RollbarMemoryUtil convertToMBs:[RollbarMemoryUtil getPhysicalMemoryInBytes]]; | ||
} | ||
return result; | ||
} | ||
|
||
#pragma mark - convenience string presenters | ||
|
||
+ (NSString *)getPhysicalMemorySizeWithUnits { | ||
|
||
NSString *result = [RollbarMemoryUtil convertToHumanFriendlyFormat:[RollbarMemoryUtil getPhysicalMemoryInBytes]]; | ||
return result; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
RollbarCommon/Sources/RollbarCommon/include/RollbarMemoryStatsDescriptors.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// RollbarMemoryStatsDescriptors.h | ||
// | ||
// | ||
// Created by Andrey Kornich on 2022-04-07. | ||
// | ||
|
||
@import Foundation; | ||
|
||
#pragma mark - RollbarMemoryStatsType enum | ||
|
||
typedef NS_ENUM(NSUInteger, RollbarMemoryStatsType) { | ||
RollbarMemoryStatsType_Physical = 0, | ||
RollbarMemoryStatsType_VM, | ||
}; | ||
|
||
#pragma mark - RollbarPhysicalMemory enum | ||
|
||
typedef NS_ENUM(NSUInteger, RollbarPhysicalMemory) { | ||
RollbarPhysicalMemory_TotalMB = 0, | ||
}; | ||
|
||
#pragma mark - RollbarVirtualMemory enum | ||
|
||
typedef NS_ENUM(NSUInteger, RollbarVirtualMemory) { | ||
RollbarVirtualMemory_FreeMB = 0, | ||
RollbarVirtualMemory_ActiveMB, | ||
RollbarVirtualMemory_InactiveMB, | ||
RollbarVirtualMemory_SpeculativeMB, | ||
RollbarVirtualMemory_WiredMB | ||
}; | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
#pragma mark - RollbarMemoryStatsDescriptors class | ||
|
||
@interface RollbarMemoryStatsDescriptors : NSObject | ||
{ | ||
} | ||
|
||
#pragma mark - Sigleton pattern | ||
|
||
+ (nonnull instancetype)sharedInstance; | ||
+ (BOOL)sharedInstanceExists; | ||
|
||
+ (instancetype)new NS_UNAVAILABLE; | ||
+ (instancetype)allocWithZone:(struct _NSZone *)zone NS_UNAVAILABLE; | ||
+ (instancetype)alloc NS_UNAVAILABLE; | ||
+ (id)copyWithZone:(struct _NSZone *)zone NS_UNAVAILABLE; | ||
+ (id)mutableCopyWithZone:(struct _NSZone *)zone NS_UNAVAILABLE; | ||
|
||
- (instancetype)init NS_UNAVAILABLE; | ||
- (void)dealloc NS_UNAVAILABLE; | ||
- (id)copy NS_UNAVAILABLE; | ||
- (id)mutableCopy NS_UNAVAILABLE; | ||
|
||
#pragma mark - Instance methods | ||
|
||
- (nonnull NSString *)getMemoryStatsTypeDescriptor: (RollbarMemoryStatsType)memoryAttribute; | ||
- (nonnull NSString *)getPhysicalMemoryDescriptor: (RollbarPhysicalMemory)memoryAttribute; | ||
- (nonnull NSString *)getVirtualMemoryDescriptor: (RollbarVirtualMemory)memoryAttribute; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
Oops, something went wrong.