-
Notifications
You must be signed in to change notification settings - Fork 3
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 #56 from jonnew/memory-usage-monitor
Add memory usage monitor support
- Loading branch information
Showing
4 changed files
with
127 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,57 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using Bonsai; | ||
|
||
namespace OpenEphys.Onix | ||
{ | ||
public class ConfigureMemoryMonitor : SingleDeviceFactory | ||
{ | ||
|
||
public ConfigureMemoryMonitor() | ||
: base(typeof(MemoryMonitor)) | ||
{ | ||
DeviceAddress = 10; | ||
} | ||
|
||
[Category(ConfigurationCategory)] | ||
[Description("Specifies whether the monitor device is enabled.")] | ||
public bool Enable { get; set; } = false; | ||
|
||
[Range(1, 1000)] | ||
[Category(ConfigurationCategory)] | ||
[Description("Frequency at which memory usage is recorded (Hz).")] | ||
public uint SampleFrequency { get; set; } = 10; | ||
|
||
public override IObservable<ContextTask> Process(IObservable<ContextTask> source) | ||
{ | ||
var deviceName = DeviceName; | ||
var deviceAddress = DeviceAddress; | ||
return source.ConfigureDevice(context => | ||
{ | ||
var device = context.GetDeviceContext(deviceAddress, MemoryMonitor.ID); | ||
device.WriteRegister(MemoryMonitor.ENABLE, 1); | ||
device.WriteRegister(MemoryMonitor.CLK_DIV, device.ReadRegister(MemoryMonitor.CLK_HZ) / SampleFrequency); | ||
|
||
return DeviceManager.RegisterDevice(deviceName, device, DeviceType); | ||
}); | ||
} | ||
} | ||
|
||
static class MemoryMonitor | ||
{ | ||
public const int ID = 28; | ||
|
||
public const uint ENABLE = 0; // Enable the monitor | ||
public const uint CLK_DIV = 1; // Sample clock divider ratio. Values less than CLK_HZ / 10e6 Hz will result in 1kHz. | ||
public const uint CLK_HZ = 2; // The frequency parameter, CLK_HZ, used in the calculation of CLK_DIV | ||
public const uint TOTAL_MEM = 3; // Total available memory in 32-bit words | ||
|
||
internal class NameConverter : DeviceNameConverter | ||
{ | ||
public NameConverter() | ||
: base(typeof(MemoryMonitor)) | ||
{ | ||
} | ||
} | ||
} | ||
} |
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,29 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Reactive.Linq; | ||
using Bonsai; | ||
|
||
namespace OpenEphys.Onix | ||
{ | ||
public class MemoryUsage : Source<MemoryUsageDataFrame> | ||
{ | ||
[TypeConverter(typeof(MemoryMonitor.NameConverter))] | ||
public string DeviceName { get; set; } | ||
|
||
public override IObservable<MemoryUsageDataFrame> Generate() | ||
{ | ||
return Observable.Using( | ||
() => DeviceManager.ReserveDevice(DeviceName), | ||
disposable => disposable.Subject.SelectMany(deviceInfo => | ||
{ | ||
var device = deviceInfo.GetDeviceContext(typeof(MemoryMonitor)); | ||
var totalMemory = device.ReadRegister(MemoryMonitor.TOTAL_MEM); | ||
|
||
return deviceInfo.Context.FrameReceived | ||
.Where(frame => frame.DeviceAddress == device.Address) | ||
.Select(frame => new MemoryUsageDataFrame(frame, totalMemory)); | ||
})); | ||
} | ||
} | ||
} |
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,37 @@ | ||
using System.Runtime.InteropServices; | ||
using OpenCV.Net; | ||
|
||
namespace OpenEphys.Onix | ||
{ | ||
public class MemoryUsageDataFrame | ||
{ | ||
public unsafe MemoryUsageDataFrame(oni.Frame frame, uint totalMemory) | ||
{ | ||
var payload = (MemoryUsagePayload*)frame.Data.ToPointer(); | ||
|
||
FrameClock = frame.Clock; | ||
DeviceAddress = frame.DeviceAddress; | ||
HubClock = payload->HubClock; | ||
PercentUsed = 100.0 * payload->Usage / totalMemory; | ||
BytesUsed = payload->Usage * 4; | ||
|
||
} | ||
|
||
public ulong FrameClock { get; private set; } | ||
|
||
public uint DeviceAddress { get; private set; } | ||
|
||
public ulong HubClock { get; } | ||
|
||
public double PercentUsed { get; } | ||
|
||
public uint BytesUsed { get; } | ||
} | ||
|
||
[StructLayout(LayoutKind.Sequential, Pack = 1)] | ||
struct MemoryUsagePayload | ||
{ | ||
public ulong HubClock; | ||
public uint Usage; | ||
} | ||
} |