-
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add memory usage to contexts (#2133)
- Loading branch information
Showing
4 changed files
with
192 additions
and
5 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
108 changes: 108 additions & 0 deletions
108
dart/lib/src/event_processor/enricher/io_platform_memory.dart
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,108 @@ | ||
import 'dart:io'; | ||
|
||
import '../../protocol.dart'; | ||
import '../../sentry_options.dart'; | ||
|
||
// Get total & free platform memory (in bytes) for linux and windows operating systems. | ||
// Source: https://github.com/onepub-dev/system_info/blob/8a9bf6b8eb7c86a09b3c3df4bf6d7fa5a6b50732/lib/src/platform/memory.dart | ||
class PlatformMemory { | ||
PlatformMemory(this.options); | ||
|
||
final SentryOptions options; | ||
|
||
int? getTotalPhysicalMemory() { | ||
if (options.platformChecker.platform.isLinux) { | ||
return _getLinuxMemInfoValue('MemTotal'); | ||
} else if (options.platformChecker.platform.isWindows) { | ||
return _getWindowsWmicValue('ComputerSystem', 'TotalPhysicalMemory'); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
int? getFreePhysicalMemory() { | ||
if (options.platformChecker.platform.isLinux) { | ||
return _getLinuxMemInfoValue('MemFree'); | ||
} else if (options.platformChecker.platform.isWindows) { | ||
return _getWindowsWmicValue('OS', 'FreePhysicalMemory'); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
int? _getWindowsWmicValue(String section, String key) { | ||
final os = _wmicGetValueAsMap(section, [key]); | ||
final totalPhysicalMemoryValue = os?[key]; | ||
if (totalPhysicalMemoryValue == null) { | ||
return null; | ||
} | ||
final size = int.tryParse(totalPhysicalMemoryValue); | ||
if (size == null) { | ||
return null; | ||
} | ||
return size; | ||
} | ||
|
||
int? _getLinuxMemInfoValue(String key) { | ||
final meminfoList = _exec('cat', ['/proc/meminfo']) | ||
?.trim() | ||
.replaceAll('\r\n', '\n') | ||
.split('\n') ?? | ||
[]; | ||
|
||
final meminfoMap = _listToMap(meminfoList, ':'); | ||
final memsizeResults = meminfoMap[key]?.split(' ') ?? []; | ||
|
||
if (memsizeResults.isEmpty) { | ||
return null; | ||
} | ||
final memsizeResult = memsizeResults.first; | ||
|
||
final memsize = int.tryParse(memsizeResult); | ||
if (memsize == null) { | ||
return null; | ||
} | ||
return memsize; | ||
} | ||
|
||
String? _exec(String executable, List<String> arguments, | ||
{bool runInShell = false}) { | ||
try { | ||
final result = | ||
Process.runSync(executable, arguments, runInShell: runInShell); | ||
if (result.exitCode == 0) { | ||
return result.stdout.toString(); | ||
} | ||
} catch (e) { | ||
options.logger(SentryLevel.warning, "Failed to run process: $e"); | ||
} | ||
return null; | ||
} | ||
|
||
Map<String, String>? _wmicGetValueAsMap(String section, List<String> fields) { | ||
final arguments = <String>[section]; | ||
arguments | ||
..add('get') | ||
..addAll(fields.join(', ').split(' ')) | ||
..add('/VALUE'); | ||
|
||
final list = | ||
_exec('wmic', arguments)?.trim().replaceAll('\r\n', '\n').split('\n') ?? | ||
[]; | ||
|
||
return _listToMap(list, '='); | ||
} | ||
|
||
Map<String, String> _listToMap(List<String> list, String separator) { | ||
final map = <String, String>{}; | ||
for (final string in list) { | ||
final index = string.indexOf(separator); | ||
if (index != -1) { | ||
final key = string.substring(0, index).trim(); | ||
final value = string.substring(index + 1).trim(); | ||
map[key] = value; | ||
} | ||
} | ||
return map; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
dart/test/event_processor/enricher/io_platform_memory_test.dart
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,60 @@ | ||
@TestOn('vm') | ||
library dart_test; | ||
|
||
import 'dart:io'; | ||
|
||
import 'package:sentry/sentry.dart'; | ||
import 'package:sentry/src/event_processor/enricher/io_platform_memory.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
late Fixture fixture; | ||
|
||
setUp(() { | ||
fixture = Fixture(); | ||
}); | ||
|
||
test('total physical memory', () { | ||
final sut = fixture.getSut(); | ||
final totalPhysicalMemory = sut.getTotalPhysicalMemory(); | ||
|
||
switch (Platform.operatingSystem) { | ||
case 'linux': | ||
expect(totalPhysicalMemory, isNotNull); | ||
expect(totalPhysicalMemory! > 0, true); | ||
break; | ||
case 'windows': | ||
expect(totalPhysicalMemory, isNotNull); | ||
expect(totalPhysicalMemory! > 0, true); | ||
break; | ||
default: | ||
expect(totalPhysicalMemory, isNull); | ||
} | ||
}); | ||
|
||
test('free physical memory', () { | ||
final sut = fixture.getSut(); | ||
final freePhysicalMemory = sut.getTotalPhysicalMemory(); | ||
|
||
switch (Platform.operatingSystem) { | ||
case 'linux': | ||
expect(freePhysicalMemory, isNotNull); | ||
expect(freePhysicalMemory! > 0, true); | ||
break; | ||
case 'windows': | ||
expect(freePhysicalMemory, isNotNull); | ||
expect(freePhysicalMemory! > 0, true); | ||
break; | ||
default: | ||
expect(freePhysicalMemory, isNull); | ||
} | ||
}); | ||
} | ||
|
||
class Fixture { | ||
var options = SentryOptions(); | ||
|
||
PlatformMemory getSut() { | ||
return PlatformMemory(options); | ||
} | ||
} |