Skip to content

Commit

Permalink
Merge pull request #152 from WideSpectrumComputing/MemoryTelemetry
Browse files Browse the repository at this point in the history
Memory telemetry
  • Loading branch information
akornich authored Apr 19, 2022
2 parents d1250c8 + eee74ce commit 3a0acea
Show file tree
Hide file tree
Showing 27 changed files with 929 additions and 134 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ The change log has moved to this repo's [GitHub Releases Page](https://github.co

## Release Notes

### 2.2.0

- feat: resolve #148 - MemTel: Implement the memory usage Telemetry auto-collection based on the config options.
- feat: resolve #147 - MemTel: Implement necessary Telemetry auto-collection options config settings with the first available option being the memory usage collection.
- feat: resolve #146 - MemTel: Define custom data fields for a Manual Telemetry event to keep the collected data. Implement helpers to manage that data.
- test: resolve #148 - MemTel: Add unit tests.

### 2.1.0

- feat: resolve #141 - Apply developer options of the persisted payload when sending the payload
Expand Down
2 changes: 1 addition & 1 deletion RollbarAUL.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

Pod::Spec.new do |s|

s.version = "2.1.0"
s.version = "2.2.0"
s.name = "RollbarAUL"
s.summary = "Application or client side SDK for interacting with the Rollbar API Server."
s.description = <<-DESC
Expand Down
2 changes: 1 addition & 1 deletion RollbarCommon.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

Pod::Spec.new do |s|

s.version = "2.1.0"
s.version = "2.2.0"
s.name = "RollbarCommon"
s.summary = "Application or client side SDK for interacting with the Rollbar API Server."
s.description = <<-DESC
Expand Down
18 changes: 18 additions & 0 deletions RollbarCommon/Sources/RollbarCommon/DTOs/RollbarDTO+Protected.m
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,22 @@ - (void)setInteger:(NSInteger)data forKey:(NSString *)key {
[self setNumber:number forKey:key];
}

- (NSTimeInterval)safelyGetTimeIntervalByKey:(NSString *)key
withDefault:(NSTimeInterval)defaultValue {

NSNumber *value = [self safelyGetNumberByKey:key];
if (value) {
return value.doubleValue;
}
else {
return defaultValue;
}
}

- (void)setTimeInterval:(NSTimeInterval)data forKey:(NSString *)key {

NSNumber *number = @(data);
[self setNumber:number forKey:key];
}

@end
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 RollbarCommon/Sources/RollbarCommon/RollbarMemoryUtil.m
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
30 changes: 22 additions & 8 deletions RollbarCommon/Sources/RollbarCommon/include/RollbarDTO+Protected.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,33 @@ NS_ASSUME_NONNULL_BEGIN
#pragma mark - Convenience API

- (RollbarDTO *)safelyGetDataTransferObjectByKey:(NSString *)key;
- (void)setDataTransferObject:(RollbarDTO *)data forKey:(NSString *)key;
- (void)setDataTransferObject:(RollbarDTO *)data
forKey:(NSString *)key;

- (RollbarTriStateFlag)safelyGetTriStateFlagByKey:(NSString *)key;
- (void)setTriStateFlag:(RollbarTriStateFlag)data forKey:(NSString *)key;
- (void)setTriStateFlag:(RollbarTriStateFlag)data
forKey:(NSString *)key;

- (BOOL)safelyGetBoolByKey:(NSString *)key withDefault:(BOOL)defaultValue;
- (void)setBool:(BOOL)data forKey:(NSString *)key;
- (BOOL)safelyGetBoolByKey:(NSString *)key
withDefault:(BOOL)defaultValue;
- (void)setBool:(BOOL)data
forKey:(NSString *)key;

- (NSUInteger)safelyGetUIntegerByKey:(NSString *)key withDefault:(NSUInteger)defaultValue;
- (void)setUInteger:(NSUInteger)data forKey:(NSString *)key;
- (NSUInteger)safelyGetUIntegerByKey:(NSString *)key
withDefault:(NSUInteger)defaultValue;
- (void)setUInteger:(NSUInteger)data
forKey:(NSString *)key;

- (NSInteger)safelyGetIntegerByKey:(NSString *)key
withDefault:(NSInteger)defaultValue;
- (void)setInteger:(NSInteger)data
forKey:(NSString *)key;

- (NSTimeInterval)safelyGetTimeIntervalByKey:(NSString *)key
withDefault:(NSTimeInterval)defaultValue;
- (void)setTimeInterval:(NSTimeInterval)data
forKey:(NSString *)key;

- (NSInteger)safelyGetIntegerByKey:(NSString *)key withDefault:(NSInteger)defaultValue;
- (void)setInteger:(NSInteger)data forKey:(NSString *)key;

@end

Expand Down
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
Loading

0 comments on commit 3a0acea

Please sign in to comment.