-
Notifications
You must be signed in to change notification settings - Fork 133
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 #495 from DataDog/xgouchet/RUMM-1274/track_memory_…
…usage RUMM-1274 Implement Memory reader
- Loading branch information
Showing
4 changed files
with
122 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
import Foundation | ||
|
||
/// A class reading the Virtual Memory resident_size, that is the part of the virtual memory which is currently in RAM. | ||
internal class VitalMemoryReader: VitalReader { | ||
static let task_vm_info_count = MemoryLayout<task_vm_info>.size / MemoryLayout<natural_t>.size | ||
|
||
func readVitalData() -> Double? { | ||
var vmInfo = task_vm_info() | ||
var vmInfoSize = mach_msg_type_size_t(VitalMemoryReader.task_vm_info_count) | ||
|
||
let kern: kern_return_t = withUnsafeMutablePointer(to: &vmInfo) { | ||
$0.withMemoryRebound(to: integer_t.self, capacity: 1) { | ||
task_info( | ||
mach_task_self_, | ||
task_flavor_t(TASK_VM_INFO), | ||
$0, | ||
&vmInfoSize | ||
) | ||
} | ||
} | ||
|
||
if kern == KERN_SUCCESS { | ||
return Double(vmInfo.resident_size) | ||
} else { | ||
return nil | ||
} | ||
} | ||
} |
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,12 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
import Foundation | ||
|
||
/// A listener getting aggregated information from a VitalObserver | ||
internal protocol VitalReader { | ||
func readVitalData() -> Double? | ||
} |
64 changes: 64 additions & 0 deletions
64
Tests/DatadogTests/Datadog/RUM/RUMVitals/VitalMemoryReaderTest.swift
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,64 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
import XCTest | ||
@testable import Datadog | ||
|
||
internal class VitalMemoryReaderTest: XCTestCase { | ||
func testReadMemory() throws { | ||
let reader = VitalMemoryReader() | ||
|
||
let result = reader.readVitalData() | ||
|
||
XCTAssertNotNil(result) | ||
} | ||
|
||
func testWhenMemoryConsumptionGrows() { | ||
// Given | ||
let reader = VitalMemoryReader() | ||
let threshold = reader.readVitalData() | ||
let allocationSize = 128 * 1_024 | ||
|
||
// When | ||
let intPointer = UnsafeMutablePointer<Int>.allocate(capacity: allocationSize) | ||
for i in 0..<allocationSize { | ||
(intPointer + i).initialize(to: i) | ||
} | ||
let measure = reader.readVitalData() | ||
intPointer.deallocate() | ||
|
||
// Then | ||
// Test that at least half the allocated size is accounted for to mitigate flakiness in memory readings | ||
let expectedAllocatedSize = allocationSize * MemoryLayout<Int>.stride / 2 | ||
XCTAssertNotNil(threshold) | ||
XCTAssertNotNil(measure) | ||
let delta = measure! - threshold! | ||
XCTAssertGreaterThanOrEqual(delta, Double(expectedAllocatedSize)) | ||
} | ||
|
||
func testWhenMemoryConsumptionShrinks() { | ||
// Given | ||
let reader = VitalMemoryReader() | ||
let allocationSize = 128 * 1_024 | ||
let intPointer = UnsafeMutablePointer<Int>.allocate(capacity: allocationSize) | ||
for i in 0..<allocationSize { | ||
(intPointer + i).initialize(to: i) | ||
} | ||
let threshold = reader.readVitalData() | ||
|
||
// When | ||
intPointer.deallocate() | ||
let measure = reader.readVitalData() | ||
|
||
// Then | ||
// Test that at least half the allocated size is accounted for to mitigate flakiness in memory readings | ||
let expectedAllocatedSize = allocationSize * MemoryLayout<Int>.stride / 2 | ||
XCTAssertNotNil(threshold) | ||
XCTAssertNotNil(measure) | ||
let delta = threshold! - measure! | ||
XCTAssertGreaterThanOrEqual(delta, Double(expectedAllocatedSize)) | ||
} | ||
} |