Compare commits

...
5 Commits
Author SHA1 Message Date
Youmu Liu 3951dd6686 l4 evict and swiss api improve 2026-10-20 18:47:31 +08:00
Youmu Liu e349bcc4dd predict method of l4 2026-10-19 18:38:43 +08:00
Youmu Liu d1efb15e3f L4 theoretical work 2026-10-16 18:19:34 +08:00
Youmu Liu 91cf6a43cd Add L4 predictor queue skeleton 2026-10-15 18:15:38 +08:00
Youmu Liu 9867e1f262 Improve Swiss rehash and NUMA topology handling 2026-10-15 18:15:16 +08:00
15 changed files with 586 additions and 181 deletions
+5
View File
@@ -0,0 +1,5 @@
root = true
[*.cs]
dotnet_diagnostic.IDE0032.severity = none
dotnet_diagnostic.IDE0055.severity = none
@@ -6,6 +6,7 @@
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Xml.Serialization;
namespace Nitload.Common.HighPerformance.Memory namespace Nitload.Common.HighPerformance.Memory
{ {
@@ -27,10 +28,38 @@ namespace Nitload.Common.HighPerformance.Memory
internal const uint MaxBufferSize = 0xFFFFFFFF - 64U; internal const uint MaxBufferSize = 0xFFFFFFFF - 64U;
[FieldOffset(16)] internal AllocationState State; [FieldOffset(31)] internal AllocationState State;
public static uint SizeOf { get; } = (uint)Unsafe.SizeOf<CacheLineMetadata>(); public static uint SizeOf { get; } = (uint)Unsafe.SizeOf<CacheLineMetadata>();
/// <summary>
/// only available in L4.Predict()
/// </summary>
public void CheckCount()
{
uint count = Volatile.Read(ref ConsumeCount);
if (ushort.MaxValue < count)
{
uint delta = unchecked(0u - count);
_ = Interlocked.Add(ref ConsumeCount, delta);
_ = Interlocked.Add(ref PredictCount, delta);
}
}
public void IncreaseConsumeCount()
{
unchecked {
_ = Interlocked.Increment(ref ConsumeCount);
}
}
public void IncreasePredictCount()
{
unchecked {
_ = Interlocked.Increment(ref PredictCount);
}
}
#region tlsf linked list #region tlsf linked list
[FieldOffset(0)] public short NextLevel; [FieldOffset(0)] public short NextLevel;
@@ -42,8 +71,15 @@ namespace Nitload.Common.HighPerformance.Memory
#region buffer #region buffer
[FieldOffset(8)] public uint PositionMask; [FieldOffset(8)] public nuint Position;
[FieldOffset(12)] public uint SizeMask; [FieldOffset(16)] public uint SizeMask;
#endregion
#region count
[FieldOffset(20)] public uint PredictCount;
[FieldOffset(24)] public uint ConsumeCount;
#endregion #endregion
} }
@@ -83,17 +83,20 @@ published buffer 地址以 `ulong` 保存:
## 6. 预测器注册、任务状态与在途缓冲 ## 6. 预测器注册、任务状态与在途缓冲
这三者职责不同,不应合并成一个队列: 预测器调度和预测任务执行是两条相关但不同的生命周期,不能把“预测器已预测完”当成“它产生的工作已完成”。
- **预测器管理数组**:`PredictorMonitor` 的固定槽位数组,保存注册顺序、稳定 ID 和生命周期状态。容量上限(曾讨论 256)是预测器数量,与缓存行计数无关。预测器按依赖顺序依次执行,一个 L4 同一时刻只有一个 predictor worker 处于 prefetch 阶段,因此不需要多 lane 或 thread-local lane 归属。 - **待调度预测器(多个)**:保存在 PredictorMonitor 的有序管理数组/队列中,等待轮到自己。固定槽位容量(曾讨论 256)表示预测器注册上限,与缓存行计数无关。
- **Swiss 预测任务状态**:保存/查找待映射的 `AddressSource` 条目;这是预测任务/映射状态,不是预测器注册表。 - **活动预测器(最多一个)**:当前 L4 正在执行其同步 `PredictMission`。预测器之间按依赖顺序切换,不需要多 lane 或 thread-local lane 归属。
- **在途预测器缓冲**:预测器已经产出预测,但对应实际工作尚未完成时,保留该预测器及其 mission/写回上下文。它与注册槽位不同:注册槽位描述“有哪些预测器”,在途缓冲描述“哪些预测工作还未完成”。具体容量、完成标记和回收时机尚待设计。 - **在途预测批次(可以多个)**:某预测器已经产出候选,但候选对应的实际任务尚未全部执行。此时要保留稳定的 predictor ID/代次、mission 或写回上下文;该记录属于任务 bag,不等同于预测器注册槽位。`PredictMission` 按预测器依赖顺序串行调用;前一批次的实际任务可以仍在途,是否与后续批次重叠由任务依赖决定。
- **已完成批次**:对应实际工作全部完成,且所需软件写回或显式丢弃已完成。此时释放在途记录及其引用资源,不再由 L4 保存该批次。预测器本身是否注销是另一项生命周期操作。
Swiss 负责预测任务/映射条目的查找状态;Monitor 负责预测器注册和调度;在途预测器缓冲负责尚未完成的预测工作及其归属。三者不可混为同一个队列或数组。
`PredictMission(in Span<AddressSource>)` 是同步接口,L4 可传入 stackalloc span。若返回后仍需跨线程或跨 NUMA 处理其内容,必须将内容复制到拥有明确生命周期的缓冲区;不得让 stack span 逃逸。由于预测器顺序有依赖,待处理预测必须按既定顺序交接。若采用异步 worker,队列应有界且不阻塞 Map 快路径。 `PredictMission(in Span<AddressSource>)` 是同步接口,L4 可传入 stackalloc span。若返回后仍需跨线程或跨 NUMA 处理其内容,必须将内容复制到拥有明确生命周期的缓冲区;不得让 stack span 逃逸。由于预测器顺序有依赖,待处理预测必须按既定顺序交接。若采用异步 worker,队列应有界且不阻塞 Map 快路径。
## 7. 驱逐与写回策略 ## 7. 驱逐与写回策略
L4 不得推断或解码预测器的 packed 格式。若丢弃一个尚未写回、且对应实际工作仍在途的预测条目会丢失必要状态,就不能静默驱逐它。按缓存行的保护/引用字段或 sidecar 正在讨论中,引用/owner metadata 布局尚未定稿。 L4 不得推断或解码预测器的 packed 格式。若丢弃一个尚未写回、且对应实际工作仍在途的预测条目会丢失必要状态,就不能静默驱逐它;必须由缓存行保护/引用状态阻止替换,或由 Monitor/预测器先完成写回、撤销或显式释放。相关状态可放在 KVP metadata 或 sidecar,布局尚未定稿。
owner ID 可用于路由显式写回;但若多个预测器能独立声明同一条目,单一 owner 字段不够。引用计数可保护共享条目,而写回路径仍需获得预测器 identity/source spec。 owner ID 可用于路由显式写回;但若多个预测器能独立声明同一条目,单一 owner 字段不够。引用计数可保护共享条目,而写回路径仍需获得预测器 identity/source spec。
@@ -18,9 +18,9 @@ namespace Nitload.Common.HighPerformance.Memory
{ {
private const int _low_predict = 512; private const int _low_predict = 512;
private readonly int _predict_slow_path_count;
private int _predict_count; private int _predict_count;
private readonly int _predict_slow_path_count;
private int _swiss_reader_counter; private int _swiss_reader_counter;
private int _swiss_swap_lock; private int _swiss_swap_lock;
private int _swiss_writer_lock; private int _swiss_writer_lock;
@@ -45,6 +45,7 @@ namespace Nitload.Common.HighPerformance.Memory
_addressMap.SwapBuffers(); _addressMap.SwapBuffers();
Volatile.Write(ref _predict_count, effectivePredictionCount); Volatile.Write(ref _predict_count, effectivePredictionCount);
_addressMap.TryRehash();
} }
if (_addressMap.TryGet(source, out handle)) { if (_addressMap.TryGet(source, out handle)) {
@@ -61,8 +62,9 @@ namespace Nitload.Common.HighPerformance.Memory
} }
} else if (predictCount < _low_predict + _predict_slow_path_count) } else if (predictCount < _low_predict + _predict_slow_path_count)
{ {
if (!TryGetAddress(source, out handle)) if (!TryGetAddress(source, out handle)) {
throw new InvalidOperationException($"Unexpected map request: [address source: 0x{source.Address:x16}; layout profile: {source.Description:x16}]."); throw new InvalidOperationException($"Unexpected map request: [address source: 0x{source.Address:x16}; layout profile: {source.Description:x16}].");
}
} else if (!_addressMap.TryGet(source, out handle)) { } else if (!_addressMap.TryGet(source, out handle)) {
throw new InvalidOperationException($"Unexpected map request: [address source: 0x{source.Address:x16}; layout profile: {source.Description:x16}]."); throw new InvalidOperationException($"Unexpected map request: [address source: 0x{source.Address:x16}; layout profile: {source.Description:x16}].");
} }
@@ -70,8 +72,8 @@ namespace Nitload.Common.HighPerformance.Memory
CacheLineMetadata* metadata = (CacheLineMetadata*)handle; CacheLineMetadata* metadata = (CacheLineMetadata*)handle;
// process metadata, unfinished // process metadata, unfinished
uint offset = metadata->PositionMask; ulong offset = metadata->Position;
nuint address = /*base ptr + */ offset; nuint address = /*base ptr + */ (nuint)offset;
_ = Interlocked.Decrement(ref _predict_count); _ = Interlocked.Decrement(ref _predict_count);
return new Handle(address, (nuint)handle); return new Handle(address, (nuint)handle);
@@ -16,7 +16,96 @@ namespace Nitload.Common.HighPerformance.Memory
public unsafe partial class L4 public unsafe partial class L4
{ {
internal partial int Predict() => 0; private int _active_line_count;
private int _last_evict_pos;
internal partial int Predict()
{
if (!TryGetCurrentPredictor(out IL4Predictor? p)) {
return 0;
}
int total_count = 0, count;
Span<L4.AddressSource> as_sp = stackalloc L4.AddressSource[4];
SwissEnumerator e = _addressMap.GetWriteBufferEnumerator();
Predict:
while ((count = p.PredictMission(in as_sp)) > 0)
{
for (int i = 0; i < count; i++)
{
if (_addressMap.TryGetWrite(as_sp[i], out ulong map_handle_adr))
{
L4.CacheLineMetadata* map_handle = (L4.CacheLineMetadata*)map_handle_adr;
map_handle->IncreasePredictCount();
map_handle->CheckCount();
} else
{
uint size = as_sp[i].Size;
int handle_idx = _bufferAllocator.Alloc(size);
L4.CacheLineMetadata* handle = _bufferAllocator.MetadataAt(handle_idx);
p.Prefetch(as_sp[i], handle->Position, handle->SizeMask);
if (!_addressMap.TryAddOrSet(as_sp[i], (ulong)handle)) {
throw new InvalidOperationException();
}
_active_line_count++;
if (_active_line_count >= MaxActiveCacheLineCount)
{
int retry = 0;
const int max_retry = 3;
for (; retry < max_retry && EvictOnce(ref e); retry++) {
e = _addressMap.GetWriteBufferEnumerator(_last_evict_pos);
}
if (retry >= max_retry) {
throw new OverflowException("Evict failed, maybe all cache lines are in use.");
}
_active_line_count--;
}
}
}
total_count += count;
if (total_count >= 2048)
{
_ = Interlocked.Add(ref _predict_count, total_count);
_last_evict_pos = e.CurrentOffset;
return total_count;
}
}
if (MoveNextPredictor())
{
_ = TryGetCurrentPredictor(out p);
goto Predict;
}
_ = Interlocked.Add(ref _predict_count, total_count);
_last_evict_pos = e.CurrentOffset;
return total_count;
}
private bool EvictOnce(
ref SwissEnumerator e
)
{
while (e.MoveNext())
{
(AddressSource source, ulong cache_head_ptr) = e.Current;
L4.CacheLineMetadata* handle = (L4.CacheLineMetadata*)cache_head_ptr;
if (Volatile.Read(ref handle->ConsumeCount) == Volatile.Read(ref handle->ConsumeCount))
{
// evict
_ = _addressMap.TryRemove(source, out _);
_bufferAllocator.Free(handle);
return true;
}
}
return false;
}
} }
} }
// Eviction follow-up:
// 1. Compare ConsumeCount with PredictCount; reset both on TLSF slot reuse and count new predictions.
// 2. Fix retry/result handling; decrement _active_line_count only after a successful eviction.
// 3. Correct TLSF reverse-slab pointer validation before freeing metadata pointers.
// 4. Defer TLSF Free until retired published buffers and outstanding handles can no longer reference the line.
// 5. Add Swiss TryRemoveAt(offset) to tombstone the enumerated slot without hashing and probing again.
@@ -1,4 +1,11 @@
using System; // -----------------------------------------------------------------------------
// file="L4.PredictorQueue"
// Project: StgSharp
// Copyright (c) Nitload.
// SPDX-License-Identifier: MIT
// -----------------------------------------------------------------------------
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@@ -6,7 +13,133 @@ using System.Threading.Tasks;
namespace Nitload.Common.HighPerformance.Memory namespace Nitload.Common.HighPerformance.Memory
{ {
public class L4 public partial class L4
{ {
private volatile int _consume;
private volatile int _enter;
private volatile int _predict;
private int _queue_sync;
internal void DropPredictor(
IL4Predictor predictor
)
{
if (predictor is null) {
return;
}
SpinWait sp = new();
while (Interlocked.CompareExchange(ref _queue_sync, 1, 0) != 0) {
sp.SpinOnce();
}
try
{
unchecked
{
for (int i = _consume; i < _predict; i++)
{
byte idx = (byte)i;
if (ReferenceEquals(predictor, _ring[idx]))
{
int end = _predict;
for (int move = i; move < end - 1; move++) {
_ring[(byte)move] = _ring[(byte)(move + 1)];
}
_ring[(byte)(end - 1)] = default!;
_predict = end - 1;
return;
}
}
}
}
finally
{
_ = Interlocked.Exchange(ref _queue_sync, 0);
}
}
internal bool MoveNextPredictor()
{
int cur = Interlocked.Increment(ref _predict);
return cur < _enter;
}
internal void RegistPredictor(
IL4Predictor predictor
)
{
if (predictor is null) {
return;
}
SpinWait sp = new();
wait_for_spare:
WaitForSpare();
while (Interlocked.CompareExchange(ref _queue_sync, 1, 0) != 0) {
sp.SpinOnce();
}
if (_enter - _consume >= PredictorRingCapacity)
{
_ = Interlocked.Exchange(ref _queue_sync, 0);
goto wait_for_spare;
}
try
{
unchecked
{
byte idx = (byte)_enter;
_enter++;
_ring[idx] = predictor;
}
}
finally
{
_ = Interlocked.Exchange(ref _queue_sync, 0);
}
}
internal bool TryGetCurrentPredictor(
out IL4Predictor predictor
)
{
int cur = _predict;
if (cur >= _enter)
{
predictor = default!;
return false;
}
unchecked
{
predictor = _ring[(byte)cur];
return true;
}
}
private void WaitForSpare()
{
int spin_count = 0;
while (_enter - _consume >= PredictorRingCapacity)
{
switch (spin_count)
{
case < 1024:
spin_count++;
break;
case < 65536:
spin_count++;
Thread.SpinWait(1);
break;
default:
Thread.Sleep(1);
break;
}
}
}
} }
} }
@@ -6,8 +6,10 @@
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
using System.Buffers; using System.Buffers;
using System.Numerics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86; using System.Runtime.Intrinsics.X86;
namespace Nitload.Common.HighPerformance.Memory namespace Nitload.Common.HighPerformance.Memory
@@ -37,8 +39,9 @@ namespace Nitload.Common.HighPerformance.Memory
public struct AddressSource : IEquatable<AddressSource> public struct AddressSource : IEquatable<AddressSource>
{ {
public uint Description;
public uint Size;
public ulong Address; public ulong Address;
public ulong Description;
public readonly bool Equals( public readonly bool Equals(
AddressSource other AddressSource other
@@ -93,11 +96,70 @@ namespace Nitload.Common.HighPerformance.Memory
} }
internal unsafe struct SwissEnumerator(
byte* buffer,
int begins_at = 0
)
{
private readonly SwissBucket* _buckets = (SwissBucket*)(buffer + IdealCount);
private readonly Vector128<byte>* _control_bytes = (Vector128<byte>*)buffer;
private bool _group_loaded = false;
private int _visited_group = 0;
private int _current_group = (begins_at / BucketCapacity) & (GroupCount - 1);
private int _current_mask = 0;
private int _current_offset = 0;
public (AddressSource key, ulong value) Current
{
get
{
SwissBucket* bucket = &_buckets[_current_group];
AddressSource key = bucket->Keys[_current_offset];
ulong value = bucket->Values[_current_offset];
return (key, value);
}
}
public readonly int CurrentOffset => (_current_group * BucketCapacity) + _current_offset;
public bool MoveNext()
{
while (_visited_group < GroupCount)
{
if (!_group_loaded)
{
if (Sse42.IsSupported)
{
_current_mask = SwissReaderX86.CompareOccupiedMask(*(_control_bytes + _current_group));
} else
{
throw new PlatformNotSupportedException();
}
_group_loaded = true;
}
if (_current_mask != 0)
{
_current_offset = BitOperations.TrailingZeroCount(_current_mask);
_current_mask &= _current_mask - 1;
return true;
}
_current_group = (_current_group + 1) & (GroupCount - 1);
_visited_group++;
_group_loaded = false;
}
return false;
}
}
internal unsafe struct SwissTable : IDisposable internal unsafe struct SwissTable : IDisposable
{ {
private byte* _buffer_write; private byte* _buffer_write;
private int _delete_count;
private ulong _published_buffer; private ulong _published_buffer;
private SwissTable( private SwissTable(
@@ -133,13 +195,13 @@ namespace Nitload.Common.HighPerformance.Memory
/// <summary> /// <summary>
/// Writes to the inactive buffer. The caller must serialize all writes. /// Writes to the inactive buffer. The caller must serialize all writes.
/// </summary> /// </summary>
public readonly bool TryAddOrSet( public bool TryAddOrSet(
AddressSource key, AddressSource key,
ulong value ulong value
) )
{ {
if (Sse42.IsSupported) { if (Sse42.IsSupported) {
return SwissWriterX86.TryAddOrSet(_buffer_write, key, value); return SwissWriterX86.TryAddOrSet(_buffer_write, key, value, ref _delete_count);
} }
throw new NotImplementedException("The Swiss backend is not implemented for this ISA."); throw new NotImplementedException("The Swiss backend is not implemented for this ISA.");
@@ -161,6 +223,56 @@ namespace Nitload.Common.HighPerformance.Memory
throw new NotImplementedException("The Swiss backend is not implemented for this ISA."); throw new NotImplementedException("The Swiss backend is not implemented for this ISA.");
} }
/// <summary>
/// Reads the inactive write buffer. The caller must serialize this query with all
/// writes.
/// </summary>
public bool TryGetWrite(
AddressSource key,
out ulong value
)
{
if (Sse42.IsSupported) {
return SwissReaderX86.TryGet(_buffer_write, key, out value);
}
throw new NotImplementedException("The Swiss backend is not implemented for this ISA.");
}
public void TryRehash()
{
if (_delete_count < 2048)
{
NativeMemory.Copy(_buffer_write, (byte*)_published_buffer, (nuint)TableBufferSize);
return;
}
try
{
SwissEnumerator e = new((byte*)_published_buffer);
InitializeBuffer(_buffer_write);
if (Sse42.IsSupported)
{
while (e.MoveNext())
{
(AddressSource key, ulong value) = e.Current;
if (!SwissWriterX86.TryAddOrSet(_buffer_write, key, value,
ref _delete_count)) {
throw new InvalidOperationException("The Swiss table could not fit all active entries during rehash.");
}
}
_delete_count = 0;
} else
{
throw new PlatformNotSupportedException();
}
}
catch (Exception)
{
NativeMemory.Copy(_buffer_write, (byte*)_published_buffer, (nuint)TableBufferSize);
throw;
}
}
/// <summary> /// <summary>
/// Removes from the inactive buffer. The caller must serialize all writes. /// Removes from the inactive buffer. The caller must serialize all writes.
/// </summary> /// </summary>
@@ -169,12 +281,15 @@ namespace Nitload.Common.HighPerformance.Memory
out ulong value out ulong value
) )
{ {
if (Sse42.IsSupported) { if (Sse42.IsSupported)
return SwissWriterX86.TryRemove(_buffer_write, key, out value); {
} return SwissWriterX86.TryRemove(_buffer_write, key, out value,
ref _delete_count);
} else
{
throw new NotImplementedException("The Swiss backend is not implemented for this ISA."); throw new NotImplementedException("The Swiss backend is not implemented for this ISA.");
} }
}
internal static SwissTable Create( internal static SwissTable Create(
byte* buffers byte* buffers
@@ -184,6 +299,14 @@ namespace Nitload.Common.HighPerformance.Memory
return new SwissTable(buffers); return new SwissTable(buffers);
} }
// Enumerates the write buffer; the caller must serialize writers and prevent swaps.
internal readonly SwissEnumerator GetWriteBufferEnumerator(
int begins_at = 0
)
{
return new SwissEnumerator(_buffer_write, begins_at);
}
private static void InitializeBuffer( private static void InitializeBuffer(
byte* buffer byte* buffer
) )
@@ -39,7 +39,8 @@ namespace Nitload.Common.HighPerformance.Memory
public static bool TryAddOrSet( public static bool TryAddOrSet(
byte* buffer, byte* buffer,
AddressSource key, AddressSource key,
ulong value ulong value,
ref int delete_count
) )
{ {
Vector128<byte>* control_bytes = (Vector128<byte>*)buffer; Vector128<byte>* control_bytes = (Vector128<byte>*)buffer;
@@ -101,6 +102,7 @@ namespace Nitload.Common.HighPerformance.Memory
bucket->Keys[first_tomb_idx] = key; bucket->Keys[first_tomb_idx] = key;
bucket->Values[first_tomb_idx] = value; bucket->Values[first_tomb_idx] = value;
((byte*)(control_bytes + first_tomb_group))[first_tomb_idx] = target_hash; ((byte*)(control_bytes + first_tomb_group))[first_tomb_idx] = target_hash;
delete_count--;
} }
return true; return true;
} }
@@ -113,6 +115,7 @@ namespace Nitload.Common.HighPerformance.Memory
bucket->Keys[first_tomb_idx] = key; bucket->Keys[first_tomb_idx] = key;
bucket->Values[first_tomb_idx] = value; bucket->Values[first_tomb_idx] = value;
((byte*)(control_bytes + first_tomb_group))[first_tomb_idx] = target_hash; ((byte*)(control_bytes + first_tomb_group))[first_tomb_idx] = target_hash;
delete_count--;
return true; return true;
} }
value = 0; value = 0;
@@ -122,7 +125,8 @@ namespace Nitload.Common.HighPerformance.Memory
public static bool TryRemove( public static bool TryRemove(
byte* buffer, byte* buffer,
AddressSource key, AddressSource key,
out ulong value out ulong value,
ref int delete_count
) )
{ {
Vector128<byte>* control_bytes = (Vector128<byte>*)buffer; Vector128<byte>* control_bytes = (Vector128<byte>*)buffer;
@@ -148,6 +152,7 @@ namespace Nitload.Common.HighPerformance.Memory
{ {
((byte*)(control_bytes + begin))[index] = (byte)SwissControl.Deleted; ((byte*)(control_bytes + begin))[index] = (byte)SwissControl.Deleted;
value = bucket->Values[index]; value = bucket->Values[index];
delete_count++;
return true; return true;
} }
mask &= ~(1 << index); mask &= ~(1 << index);
@@ -171,6 +176,20 @@ namespace Nitload.Common.HighPerformance.Memory
internal readonly unsafe struct SwissReaderX86 internal readonly unsafe struct SwissReaderX86
{ {
private static readonly Vector128<byte> _empty_mask = Vector128.Create((byte)SwissControl.Empty);
private static readonly Vector128<byte> _tomb_mask = Vector128.Create((byte)SwissControl.Deleted);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int CompareOccupiedMask(
Vector128<byte> control_bytes_v128
)
{
Vector128<byte> cmp_eql = Sse2.CompareEqual(control_bytes_v128, _empty_mask);
Vector128<byte> cmp_del = Sse2.CompareEqual(control_bytes_v128, _tomb_mask);
int specialMask = Sse2.MoveMask(Sse2.Or(cmp_eql, cmp_del));
return (ushort)~(ushort)specialMask;
}
public static bool TryGet( public static bool TryGet(
byte* buffer, byte* buffer,
AddressSource key, AddressSource key,
@@ -5,27 +5,29 @@
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
using Nitload.Common.HighPerformance.ProcessorAbstraction;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics; using System.Numerics;
using System.Runtime.InteropServices; using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
namespace Nitload.Common.HighPerformance.Memory namespace Nitload.Common.HighPerformance.Memory
{ {
public partial class L4 public partial class L4
{ {
/// <summary>
/// L4-specific TLSF allocator whose metadata nodes also serve as cache-line headers.
/// </summary>
/// <remarks>
/// Payload occupies the first arena region; a fixed metadata slab follows it and is
/// carved backward from its aligned high end. Handle 0 is the sentinel, and positive
/// handles are metadata slot indices. This layout is intentionally coupled to L4.
/// </remarks>
private unsafe struct TLSF private unsafe struct TLSF
{ {
public const int MaxCacheLineCount = 8192; public const int MaxCacheLineCount = MaxActiveCacheLineCount;
public static readonly nuint RequestedAllocation = public static readonly nuint RequestedAllocation =
((nuint)MaxCacheLineCount + 1u) * CacheLineMetadata.SizeOf; (((nuint)MaxCacheLineCount + 1u) * CacheLineMetadata.SizeOf) + CacheLineMetadata.SizeOf - 1u;
private readonly CacheLineMetadata* _spareMemory; private readonly CacheLineMetadata* _spareMemory;
private readonly int _align; private readonly int _align;
private readonly int _maxLevel; private readonly int _maxLevel;
@@ -52,12 +54,16 @@ namespace Nitload.Common.HighPerformance.Memory
// _bucket = new short[GetLevel(_maxAllocSize) + 2]; // _bucket = new short[GetLevel(_maxAllocSize) + 2];
_bucket[63] = (short)(GetLevel(_maxAllocSize) + 2); _bucket[63] = (short)(GetLevel(_maxAllocSize) + 2);
_spareMemory = (CacheLineMetadata*)(ulong)(_basePtr + _totalSize);
// Metadata occupies the reservation after payload: handle 0 is the sentinel, and handles 1..MaxCacheLineCount are carved backward from the aligned high end until the slab is full.
nuint metadataRegionEnd = (nuint)externalPointer + byteSize + RequestedAllocation;
nuint alignedMetadataEnd = metadataRegionEnd & ~((nuint)CacheLineMetadata.SizeOf - 1u);
_spareMemory = (CacheLineMetadata*)(alignedMetadataEnd - CacheLineMetadata.SizeOf);
_metadataEnd = 0; _metadataEnd = 0;
// init spare memory node // init spare memory node
_spareMemory->PositionMask = 0; _spareMemory->Position = (nuint)(ulong)_basePtr;
_spareMemory->SizeMask = _totalSize; _spareMemory->SizeMask = _totalSize;
_spareMemory->NextLevel = 0; _spareMemory->NextLevel = 0;
_spareMemory->NextNear = 0; _spareMemory->NextNear = 0;
@@ -66,7 +72,15 @@ namespace Nitload.Common.HighPerformance.Memory
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private int GetLevel( internal readonly CacheLineMetadata* MetadataAt(
int handle
)
{
return _spareMemory - handle;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private readonly int GetLevel(
uint size uint size
) )
{ {
@@ -93,11 +107,11 @@ namespace Nitload.Common.HighPerformance.Memory
// remove from position chain // remove from position chain
short prevNearIdx = node->PreviousNear; short prevNearIdx = node->PreviousNear;
short nextNearIdx = node->NextNear; short nextNearIdx = node->NextNear;
CacheLineMetadata* prevNear = &_spareMemory[prevNearIdx]; CacheLineMetadata* prevNear = MetadataAt(prevNearIdx);
prevNear->NextNear = nextNearIdx; prevNear->NextNear = nextNearIdx;
if (nextNearIdx != 0) if (nextNearIdx != 0)
{ {
CacheLineMetadata* nextNear = &_spareMemory[nextNearIdx]; CacheLineMetadata* nextNear = MetadataAt(nextNearIdx);
nextNear->PreviousNear = prevNearIdx; nextNear->PreviousNear = prevNearIdx;
} else } else
{ {
@@ -106,7 +120,7 @@ namespace Nitload.Common.HighPerformance.Memory
node->PreviousNear = 0; node->PreviousNear = 0;
node->NextNear = 0; node->NextNear = 0;
node->PositionMask = 0; node->Position = (nuint)(ulong)_basePtr;
node->SizeMask = 0; node->SizeMask = 0;
node->State = AllocationState.SpareOrCollect; node->State = AllocationState.SpareOrCollect;
node->PreviousLevel = 0; node->PreviousLevel = 0;
@@ -114,7 +128,7 @@ namespace Nitload.Common.HighPerformance.Memory
short nextIdx = _spareMemory->NextLevel; short nextIdx = _spareMemory->NextLevel;
if (nextIdx != 0) if (nextIdx != 0)
{ {
CacheLineMetadata* next = &_spareMemory[nextIdx]; CacheLineMetadata* next = MetadataAt(nextIdx);
next->PreviousLevel = nodeIdx; next->PreviousLevel = nodeIdx;
} }
node->NextLevel = nextIdx; node->NextLevel = nextIdx;
@@ -145,12 +159,12 @@ namespace Nitload.Common.HighPerformance.Memory
_bucket[currentLevel] = currentNextLevelIdx; _bucket[currentLevel] = currentNextLevelIdx;
} else if (currentPrevLevelIdx != 0) } else if (currentPrevLevelIdx != 0)
{ {
CacheLineMetadata* prevLevel = &_spareMemory[currentPrevLevelIdx]; CacheLineMetadata* prevLevel = MetadataAt(currentPrevLevelIdx);
prevLevel->NextLevel = currentNextLevelIdx; prevLevel->NextLevel = currentNextLevelIdx;
} }
if (currentNextLevelIdx != 0) if (currentNextLevelIdx != 0)
{ {
CacheLineMetadata* nextLevel = &_spareMemory[currentNextLevelIdx]; CacheLineMetadata* nextLevel = MetadataAt(currentNextLevelIdx);
nextLevel->PreviousLevel = currentPrevLevelIdx; nextLevel->PreviousLevel = currentPrevLevelIdx;
} }
node->PreviousLevel = 0; node->PreviousLevel = 0;
@@ -203,11 +217,11 @@ namespace Nitload.Common.HighPerformance.Memory
meta = null; meta = null;
return false; return false;
} }
meta = &_spareMemory[idx]; meta = MetadataAt(idx);
short nextSpareIdx = meta->NextLevel; short nextSpareIdx = meta->NextLevel;
if (nextSpareIdx != 0) if (nextSpareIdx != 0)
{ {
CacheLineMetadata* nextSpare = &_spareMemory[nextSpareIdx]; CacheLineMetadata* nextSpare = MetadataAt(nextSpareIdx);
nextSpare->PreviousLevel = 0; nextSpare->PreviousLevel = 0;
} }
_spareMemory->NextLevel = nextSpareIdx; _spareMemory->NextLevel = nextSpareIdx;
@@ -215,18 +229,19 @@ namespace Nitload.Common.HighPerformance.Memory
if (!TryGetNewMetadata(out meta)) { if (!TryGetNewMetadata(out meta)) {
return false; return false;
} }
idx = (short)(_spareMemory - meta);
spareSize -= size; spareSize -= size;
meta->SizeMask = size; meta->SizeMask = size;
meta->PositionMask = _spareMemory->PositionMask; meta->Position = _spareMemory->Position;
meta->State = AllocationState.Free; meta->State = AllocationState.Free;
meta->PreviousLevel = 0; meta->PreviousLevel = 0;
meta->NextLevel = 0; meta->NextLevel = 0;
_spareMemory->SizeMask = spareSize; _spareMemory->SizeMask = spareSize;
_spareMemory->PositionMask += size; _spareMemory->Position += size;
// insert the new chunk to position chain // insert the new chunk to position chain
short nearIdx = _spareMemory->PreviousNear; short nearIdx = _spareMemory->PreviousNear;
CacheLineMetadata* near = &_spareMemory[nearIdx]; CacheLineMetadata* near = MetadataAt(nearIdx);
near->NextNear = idx; near->NextNear = idx;
meta->PreviousNear = nearIdx; meta->PreviousNear = nearIdx;
_spareMemory->PreviousNear = idx; _spareMemory->PreviousNear = idx;
@@ -245,7 +260,7 @@ namespace Nitload.Common.HighPerformance.Memory
return false; return false;
} }
_metadataEnd++; _metadataEnd++;
meta = &_spareMemory[_metadataEnd]; meta = MetadataAt(_metadataEnd);
return true; return true;
} }
@@ -261,7 +276,7 @@ namespace Nitload.Common.HighPerformance.Memory
int idx = _bucket[level]; int idx = _bucket[level];
while (idx != 0) while (idx != 0)
{ {
CacheLineMetadata* candidate = &_spareMemory[idx]; CacheLineMetadata* candidate = MetadataAt(idx);
if (candidate->SizeMask >= minimumSize) if (candidate->SizeMask >= minimumSize)
{ {
RemoveFromBucket(level, candidate, idx); RemoveFromBucket(level, candidate, idx);
@@ -276,7 +291,7 @@ namespace Nitload.Common.HighPerformance.Memory
int idx = _bucket[i]; int idx = _bucket[i];
if (idx != 0) if (idx != 0)
{ {
CacheLineMetadata* candidate = &_spareMemory[idx]; CacheLineMetadata* candidate = MetadataAt(idx);
RemoveFromBucketHead(i, candidate, idx); RemoveFromBucketHead(i, candidate, idx);
entry = candidate; entry = candidate;
return true; return true;
@@ -313,7 +328,7 @@ namespace Nitload.Common.HighPerformance.Memory
TryGetNewMetadata(out CacheLineMetadata* newEntry)) TryGetNewMetadata(out CacheLineMetadata* newEntry))
{ {
short newEntryIdx = (short)(_spareMemory - newEntry); short newEntryIdx = (short)(_spareMemory - newEntry);
newEntry->PositionMask = entry->PositionMask + allocSize; newEntry->Position = entry->Position + allocSize;
newEntry->SizeMask = remain; newEntry->SizeMask = remain;
newEntry->State = AllocationState.Free; newEntry->State = AllocationState.Free;
int newLevel = GetLevel(remain); int newLevel = GetLevel(remain);
@@ -321,7 +336,7 @@ namespace Nitload.Common.HighPerformance.Memory
if (bucketHeadIdx != 0) if (bucketHeadIdx != 0)
{ {
// if there is already an entry in the bucket, link it to the new entry // if there is already an entry in the bucket, link it to the new entry
CacheLineMetadata* bucketHead = &_spareMemory[bucketHeadIdx]; CacheLineMetadata* bucketHead = MetadataAt(bucketHeadIdx);
bucketHead->PreviousLevel = newEntryIdx; bucketHead->PreviousLevel = newEntryIdx;
} }
newEntry->NextLevel = bucketHeadIdx; newEntry->NextLevel = bucketHeadIdx;
@@ -331,14 +346,14 @@ namespace Nitload.Common.HighPerformance.Memory
// push to position chain // push to position chain
short afterIdx = entry->NextNear; short afterIdx = entry->NextNear;
CacheLineMetadata* after = &_spareMemory[afterIdx]; CacheLineMetadata* after = MetadataAt(afterIdx);
newEntry->NextNear = afterIdx; newEntry->NextNear = afterIdx;
after->PreviousNear = newEntryIdx; after->PreviousNear = newEntryIdx;
entry->NextNear = newEntryIdx; entry->NextNear = newEntryIdx;
newEntry->PreviousNear = entryIdx; newEntry->PreviousNear = entryIdx;
} }
entry->State = AllocationState.Alloc; entry->State = AllocationState.Alloc;
if (entry->PositionMask - (ulong)_basePtr > _totalSize) { if (entry->Position - (ulong)_basePtr > _totalSize) {
throw new OverflowException("Allocated memory address exceeds the maximum block size."); throw new OverflowException("Allocated memory address exceeds the maximum block size.");
} }
return entryIdx; return entryIdx;
@@ -353,7 +368,7 @@ namespace Nitload.Common.HighPerformance.Memory
{ {
break; break;
} }
CacheLineMetadata* current = &_spareMemory[currentIdx]; CacheLineMetadata* current = MetadataAt(currentIdx);
if (current->State == AllocationState.Free) if (current->State == AllocationState.Free)
{ {
// scan next // scan next
@@ -361,7 +376,7 @@ namespace Nitload.Common.HighPerformance.Memory
int nextIdx = current->NextNear; int nextIdx = current->NextNear;
while (nextIdx != 0) while (nextIdx != 0)
{ {
CacheLineMetadata* next = &_spareMemory[nextIdx]; CacheLineMetadata* next = MetadataAt(nextIdx);
if (next->State != AllocationState.Free) if (next->State != AllocationState.Free)
{ {
break; break;
@@ -378,7 +393,7 @@ namespace Nitload.Common.HighPerformance.Memory
short bucketHeadIdx = _bucket[newLevel]; short bucketHeadIdx = _bucket[newLevel];
if (bucketHeadIdx != 0) if (bucketHeadIdx != 0)
{ {
CacheLineMetadata* bucketHead = &_spareMemory[bucketHeadIdx]; CacheLineMetadata* bucketHead = MetadataAt(bucketHeadIdx);
bucketHead->PreviousLevel = currentIdx; bucketHead->PreviousLevel = currentIdx;
} }
current->NextLevel = bucketHeadIdx; current->NextLevel = bucketHeadIdx;
@@ -391,11 +406,13 @@ namespace Nitload.Common.HighPerformance.Memory
} }
public void Free( public void Free(
int handle, CacheLineMetadata* head,
int collectScanWindow = 4 int collectScanWindow = 4
) )
{ {
CacheLineMetadata* head = &_spareMemory[handle]; if (head < _spareMemory || head >= _spareMemory + MaxCacheLineCount) {
throw new ArgumentOutOfRangeException(nameof(head), "Handle is out of range.");
}
short headIdx = (short)(_spareMemory - head); short headIdx = (short)(_spareMemory - head);
uint size = head->SizeMask; uint size = head->SizeMask;
for (int i = 0; i < collectScanWindow; i++) for (int i = 0; i < collectScanWindow; i++)
@@ -406,13 +423,13 @@ namespace Nitload.Common.HighPerformance.Memory
{ {
break; break;
} }
CacheLineMetadata* prev = &_spareMemory[forwardIdx]; CacheLineMetadata* prev = MetadataAt(forwardIdx);
if (prev->State != AllocationState.Free) if (prev->State != AllocationState.Free)
{ {
break; break;
} }
size += prev->SizeMask; size += prev->SizeMask;
head->PositionMask = prev->PositionMask; head->Position = prev->Position;
RecycleNode(prev); RecycleNode(prev);
} }
for (int i = 0; i < collectScanWindow; i++) for (int i = 0; i < collectScanWindow; i++)
@@ -423,7 +440,7 @@ namespace Nitload.Common.HighPerformance.Memory
{ {
break; break;
} }
CacheLineMetadata* prev = &_spareMemory[nextIdx]; CacheLineMetadata* prev = MetadataAt(nextIdx);
if (prev->State != AllocationState.Free) if (prev->State != AllocationState.Free)
{ {
break; break;
@@ -441,7 +458,7 @@ namespace Nitload.Common.HighPerformance.Memory
head->NextLevel = 0; head->NextLevel = 0;
if (bucketHeadIdx != 0) if (bucketHeadIdx != 0)
{ {
CacheLineMetadata* bucketHead = &_spareMemory[bucketHeadIdx]; CacheLineMetadata* bucketHead = MetadataAt(bucketHeadIdx);
bucketHead->PreviousLevel = headIdx; bucketHead->PreviousLevel = headIdx;
head->NextLevel = bucketHeadIdx; head->NextLevel = bucketHeadIdx;
} }
@@ -449,6 +466,27 @@ namespace Nitload.Common.HighPerformance.Memory
return; return;
} }
public void Free(
ulong handle,
int collectScanWindow = 4
)
{
if (handle < (ulong)_basePtr || handle >= (ulong)_basePtr + _totalSize) {
throw new ArgumentOutOfRangeException(nameof(handle), "Handle is out of range.");
}
CacheLineMetadata* head = (CacheLineMetadata*)handle;
Free(head, collectScanWindow);
}
public void Free(
int handle,
int collectScanWindow = 4
)
{
CacheLineMetadata* head = MetadataAt(handle);
Free(head, collectScanWindow);
}
public bool TryAlloc( public bool TryAlloc(
uint size, uint size,
out int h out int h
@@ -479,7 +517,7 @@ namespace Nitload.Common.HighPerformance.Memory
if (remain > (entrySize >> 7) && TryGetNewMetadata(out CacheLineMetadata* newEntry)) if (remain > (entrySize >> 7) && TryGetNewMetadata(out CacheLineMetadata* newEntry))
{ {
short newEntryIdx = (short)(_spareMemory - newEntry); short newEntryIdx = (short)(_spareMemory - newEntry);
newEntry->PositionMask = entry->PositionMask + allocSize; newEntry->Position = entry->Position + allocSize;
newEntry->SizeMask = remain; newEntry->SizeMask = remain;
newEntry->State = AllocationState.Free; newEntry->State = AllocationState.Free;
int newLevel = GetLevel(remain); int newLevel = GetLevel(remain);
@@ -487,7 +525,7 @@ namespace Nitload.Common.HighPerformance.Memory
if (bucketHeadIdx != 0) if (bucketHeadIdx != 0)
{ {
// if there is already an entry in the bucket, link it to the new entry // if there is already an entry in the bucket, link it to the new entry
CacheLineMetadata* bucketHead = &_spareMemory[bucketHeadIdx]; CacheLineMetadata* bucketHead = MetadataAt(bucketHeadIdx);
newEntry->NextLevel = bucketHeadIdx; newEntry->NextLevel = bucketHeadIdx;
bucketHead->PreviousLevel = newEntryIdx; bucketHead->PreviousLevel = newEntryIdx;
} else } else
@@ -499,14 +537,14 @@ namespace Nitload.Common.HighPerformance.Memory
// push to position chain // push to position chain
short afterIdx = entry->NextNear; short afterIdx = entry->NextNear;
CacheLineMetadata* after = &_spareMemory[afterIdx]; CacheLineMetadata* after = MetadataAt(afterIdx);
newEntry->NextNear = afterIdx; newEntry->NextNear = afterIdx;
after->PreviousNear = newEntryIdx; after->PreviousNear = newEntryIdx;
entry->NextNear = newEntryIdx; entry->NextNear = newEntryIdx;
newEntry->PreviousNear = entryIdx; newEntry->PreviousNear = entryIdx;
} }
entry->State = AllocationState.Alloc; entry->State = AllocationState.Alloc;
if (entry->PositionMask - (ulong)_basePtr > _totalSize) if (entry->Position - (ulong)_basePtr > _totalSize)
{ {
h = default; h = default;
throw new OverflowException("Allocated memory address exceeds the maximum block size."); throw new OverflowException("Allocated memory address exceeds the maximum block size.");
@@ -12,7 +12,12 @@ namespace Nitload.Common.HighPerformance.Memory
public sealed unsafe partial class L4 : IDisposable public sealed unsafe partial class L4 : IDisposable
{ {
private const int MaxActiveCacheLineCount = 8192;
private const nuint PageSize = 4096; private const nuint PageSize = 4096;
private const int PredictorRingCapacity = 256;
private readonly IL4Predictor[] _ring;
private bool _disposed; private bool _disposed;
private readonly int _bindedNuma; private readonly int _bindedNuma;
@@ -29,6 +34,7 @@ namespace Nitload.Common.HighPerformance.Memory
{ {
ArgumentOutOfRangeException.ThrowIfGreaterThan(bufferSize, CacheLineMetadata.MaxBufferSize); ArgumentOutOfRangeException.ThrowIfGreaterThan(bufferSize, CacheLineMetadata.MaxBufferSize);
ArgumentOutOfRangeException.ThrowIfGreaterThan(maxCacheLineSize, CacheLineMetadata.MaxBufferSize); ArgumentOutOfRangeException.ThrowIfGreaterThan(maxCacheLineSize, CacheLineMetadata.MaxBufferSize);
_ring = new IL4Predictor[PredictorRingCapacity];
_bindedNuma = bindedNuma; _bindedNuma = bindedNuma;
_predict_slow_path_count = GetPredictSlowPathCount(bindedNuma); _predict_slow_path_count = GetPredictSlowPathCount(bindedNuma);
nuint normalizedBufferSize = (bufferSize + (PageSize - 1)) & (~(PageSize - 1)); nuint normalizedBufferSize = (bufferSize + (PageSize - 1)) & (~(PageSize - 1));
@@ -73,6 +79,8 @@ namespace Nitload.Common.HighPerformance.Memory
_bufferAllocator.Free(handle); _bufferAllocator.Free(handle);
} }
internal partial int Predict();
internal bool TryAllocate( internal bool TryAllocate(
uint size, uint size,
out int handle out int handle
@@ -82,15 +90,16 @@ namespace Nitload.Common.HighPerformance.Memory
return _bufferAllocator.TryAlloc(size, out handle); return _bufferAllocator.TryAlloc(size, out handle);
} }
internal partial int Predict(); private static int GetPredictSlowPathCount(
int bindedNuma
private static int GetPredictSlowPathCount(int bindedNuma) )
{ {
foreach (NumaNode node in Numa.AllNumaNode) foreach (NumaNode node in Numa.AllNumaNode)
{ {
if (node.NumaNodeId == (uint)bindedNuma) if (node.NumaNodeId == (uint)bindedNuma) {
return checked(node.LogicalCoreCount * 2); return checked(node.LogicalCoreCount * 2);
} }
}
throw new ArgumentOutOfRangeException(nameof(bindedNuma), "No topology information exists for the bound NUMA node."); throw new ArgumentOutOfRangeException(nameof(bindedNuma), "No topology information exists for the bound NUMA node.");
} }
@@ -22,6 +22,12 @@ namespace Nitload.Common.HighPerformance.Memory
in Span<L4.AddressSource> RequiredBuffer in Span<L4.AddressSource> RequiredBuffer
); );
void Prefetch(
L4.AddressSource RequiredBuffer,
nuint allocBuffer,
nuint bufferSize
);
} }
public interface IL4Predictor<TMission> : IL4Predictor public interface IL4Predictor<TMission> : IL4Predictor
@@ -32,6 +38,4 @@ namespace Nitload.Common.HighPerformance.Memory
); );
} }
internal class PredictorMonitor { }
} }
+8 -31
View File
@@ -17,39 +17,16 @@ nfn_resolve_target_identifiers(
NFN_RESOLVED_ARCH_NAME NFN_RESOLVED_ARCH_NAME
NFN_RESOLVED_ARCH_DEFINE) NFN_RESOLVED_ARCH_DEFINE)
set(NFN_NATIVE_SOURCES # CONFIGURE_DEPENDS makes CMake reconfigure when files are added or removed,
${CMAKE_CURRENT_LIST_DIR}/nfn-src/dllmain.c # so native sources, headers, and templates need no per-file CMake edits.
${CMAKE_CURRENT_LIST_DIR}/nfn-src/load_glfw.c file(GLOB_RECURSE NFN_NATIVE_SOURCES CONFIGURE_DEPENDS
${CMAKE_CURRENT_LIST_DIR}/nfn-src/load_intrinsic.x86.c ${CMAKE_CURRENT_LIST_DIR}/nfn-src/*.c)
${CMAKE_CURRENT_LIST_DIR}/nfn-src/m.bufop.x86.c
${CMAKE_CURRENT_LIST_DIR}/nfn-src/m.solve.x86.c
${CMAKE_CURRENT_LIST_DIR}/nfn-src/m.transpose.x86.c
${CMAKE_CURRENT_LIST_DIR}/nfn-src/mk.fma.pack.c
${CMAKE_CURRENT_LIST_DIR}/nfn-src/mk.fma.x86.c
${CMAKE_CURRENT_LIST_DIR}/nfn-src/mk.p_scan.x86.c
${CMAKE_CURRENT_LIST_DIR}/nfn-src/s.factorial.x86.c
${CMAKE_CURRENT_LIST_DIR}/nfn-src/s.hash.x86.c
${CMAKE_CURRENT_LIST_DIR}/nfn-src/simdid_compare.x86.c
${CMAKE_CURRENT_LIST_DIR}/nfn-src/simdid.x86.c
${CMAKE_CURRENT_LIST_DIR}/nfn-src/sk.fma.x86.c
${CMAKE_CURRENT_LIST_DIR}/nfn-src/thread.linux.c
${CMAKE_CURRENT_LIST_DIR}/nfn-src/thread.windows.c
${CMAKE_CURRENT_LIST_DIR}/nfn-src/vector.c
${CMAKE_CURRENT_LIST_DIR}/nfn-src/vector_dot.c)
set(NFN_NATIVE_HEADERS file(GLOB_RECURSE NFN_NATIVE_HEADERS CONFIGURE_DEPENDS
${CMAKE_CURRENT_LIST_DIR}/nfn-head/glfw_function.h ${CMAKE_CURRENT_LIST_DIR}/nfn-head/*.h)
${CMAKE_CURRENT_LIST_DIR}/nfn-head/nfn_internal.h
${CMAKE_CURRENT_LIST_DIR}/nfn-head/nfn_intrinsic.context.base.h
${CMAKE_CURRENT_LIST_DIR}/nfn-head/nfn_intrinsic.context.x86.h
${CMAKE_CURRENT_LIST_DIR}/nfn-head/nfn_intrinsic.h
${CMAKE_CURRENT_LIST_DIR}/nfn-head/nfn_intrinsic.matkernel.h
${CMAKE_CURRENT_LIST_DIR}/nfn-head/nfn_intrinsic.std.h
${CMAKE_CURRENT_LIST_DIR}/nfn-head/nfn_target.h
${CMAKE_CURRENT_LIST_DIR}/nfn-head/NitloadFrameWork.h)
set(NFN_NATIVE_TEMPLATES file(GLOB_RECURSE NFN_NATIVE_TEMPLATES CONFIGURE_DEPENDS
${CMAKE_CURRENT_LIST_DIR}/nfn-template/nfn_template.fma.x86.h) ${CMAKE_CURRENT_LIST_DIR}/nfn-template/*.h)
# Nitload.Native now lives under <root>/src/Nitload.Native, so walk up two # Nitload.Native now lives under <root>/src/Nitload.Native, so walk up two
# levels (project dir -> src -> solution root) to reach the solution root. # levels (project dir -> src -> solution root) to reach the solution root.
@@ -27,9 +27,11 @@ NFN_API SIMDID NFN_DECL nfn_get_simd_level_global();
NFN_API SIMDID NFN_DECL nfn_get_simd_level_local(); NFN_API SIMDID NFN_DECL nfn_get_simd_level_local();
NFN_API SIMDID NFN_DECL nfn_get_unite_simd(SIMDID left, SIMDID right); NFN_API SIMDID NFN_DECL nfn_get_unite_simd(SIMDID left, SIMDID right);
NFN_API int NFN_DECL nfn_compare_simd(SIMDID left, SIMDID right); NFN_API int NFN_DECL nfn_compare_simd(SIMDID left, SIMDID right);
NFN_API void NFN_DECL nfn_set_thread_affinity(int core_id, int numa_id); NFN_API void NFN_DECL nfn_set_thread_affinity(int cpu_index, int processor_group_id);
NFN_API void *NFN_DECL nfn_numa_alloc(size_t size, int numa_id); NFN_API void *NFN_DECL nfn_numa_alloc(size_t size, int numa_id);
NFN_API void NFN_DECL nfn_numa_free(void *ptr, size_t size, int numa_id); NFN_API void NFN_DECL nfn_numa_free(void *ptr, size_t size, int numa_id);
NFN_API int NFN_DECL nfn_get_numa_each(int numa_id, uint64_t *internal, uint32_t *global); NFN_API int NFN_DECL nfn_get_numa_each(int numa_id, uint32_t *processor_group_ids,
uint32_t *socket_ids, uint32_t *cpu_indices,
uint32_t *numa_node_ids);
NFN_API int NFN_DECL nfn_get_numa_overall(int *out_numa_count, int *count_of_each_numa); NFN_API int NFN_DECL nfn_get_numa_overall(int *out_numa_count, int *count_of_each_numa);
#endif #endif
+10 -38
View File
@@ -26,18 +26,12 @@ typedef struct nfn_linux_max_context {
int max_value; int max_value;
} nfn_linux_max_context; } nfn_linux_max_context;
<<<<<<< HEAD:src/Nitload.Native/nfn-src/thread.linux.c
typedef struct nfn_linux_fill_cpu_context { typedef struct nfn_linux_fill_cpu_context {
uint64_t *internal;
uint32_t *global;
=======
typedef struct nif_linux_fill_cpu_context {
uint32_t *processor_group_ids; uint32_t *processor_group_ids;
uint32_t *socket_ids; uint32_t *socket_ids;
uint32_t *cpu_indices; uint32_t *cpu_indices;
uint32_t *numa_node_ids; uint32_t *numa_node_ids;
int numa_id; int numa_id;
>>>>>>> 3a2b1234782f893f5b71655289b372ffed8dfd93:src/Nitload.Native/nif-src/thread.linux.c
int index; int index;
} nfn_linux_fill_cpu_context; } nfn_linux_fill_cpu_context;
@@ -127,12 +121,7 @@ static int nfn_linux_track_max_id(int value, void *context)
return 1; return 1;
} }
<<<<<<< HEAD:src/Nitload.Native/nfn-src/thread.linux.c static int nfn_linux_read_socket_id(int cpu_id, uint32_t *socket_id)
static int nfn_linux_fill_cpu_id(int value, void *context)
{
nfn_linux_fill_cpu_context *fill_context = (nfn_linux_fill_cpu_context *)context;
=======
static int nif_linux_read_socket_id(int cpu_id, uint32_t *socket_id)
{ {
char path[160]; char path[160];
char buffer[64]; char buffer[64];
@@ -140,25 +129,24 @@ static int nif_linux_read_socket_id(int cpu_id, uint32_t *socket_id)
long parsed_value; long parsed_value;
snprintf(path, sizeof(path), snprintf(path, sizeof(path),
"/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu_id); "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu_id);
if (!nif_linux_read_first_line(path, buffer, sizeof(buffer))) if (!nfn_linux_read_first_line(path, buffer, sizeof(buffer)))
return 0; return 0;
errno = 0; errno = 0;
parsed_value = strtol(buffer, &end, 10); parsed_value = strtol(buffer, &end, 10);
if (end == buffer || errno == ERANGE || parsed_value < 0 || parsed_value > UINT32_MAX) if (end == buffer || errno == ERANGE || parsed_value < 0 || (uintmax_t)parsed_value > UINT32_MAX)
return 0; return 0;
*socket_id = (uint32_t)parsed_value; *socket_id = (uint32_t)parsed_value;
return 1; return 1;
} }
static int nif_linux_fill_cpu_id(int value, void *context) static int nfn_linux_fill_cpu_id(int value, void *context)
{ {
nif_linux_fill_cpu_context *fill_context = (nif_linux_fill_cpu_context *)context; nfn_linux_fill_cpu_context *fill_context = (nfn_linux_fill_cpu_context *)context;
uint32_t socket_id = 0; uint32_t socket_id = 0;
if (!nif_linux_read_socket_id(value, &socket_id)) if (!nfn_linux_read_socket_id(value, &socket_id))
return 0; return 0;
>>>>>>> 3a2b1234782f893f5b71655289b372ffed8dfd93:src/Nitload.Native/nif-src/thread.linux.c
fill_context->processor_group_ids[fill_context->index] = 0u; fill_context->processor_group_ids[fill_context->index] = 0u;
fill_context->socket_ids[fill_context->index] = socket_id; fill_context->socket_ids[fill_context->index] = socket_id;
@@ -249,15 +237,8 @@ static int nfn_linux_populate_writable_mapping(void *address, size_t size)
#endif #endif
} }
<<<<<<< HEAD:src/Nitload.Native/nfn-src/thread.linux.c
/* Pin the calling thread to a single logical processor.
* core_id >= 0 : bind to that logical processor (global index, across groups).
* core_id < 0 : clear affinity constraint within the current group. */
NFN_API void NFN_DECL nfn_set_thread_affinity(int core_id, int numa_id)
=======
/* Linux has no Windows-style processor groups; processor_group_id is ignored. */ /* Linux has no Windows-style processor groups; processor_group_id is ignored. */
NIF_API void NIF_DECL nif_set_thread_affinity(int cpu_index, int processor_group_id) NFN_API void NFN_DECL nfn_set_thread_affinity(int cpu_index, int processor_group_id)
>>>>>>> 3a2b1234782f893f5b71655289b372ffed8dfd93:src/Nitload.Native/nif-src/thread.linux.c
{ {
(void)processor_group_id; (void)processor_group_id;
@@ -311,15 +292,11 @@ NFN_API int NFN_DECL nfn_get_numa_overall(int *out_numa_count, int *count_of_eac
return 1; return 1;
} }
<<<<<<< HEAD:src/Nitload.Native/nfn-src/thread.linux.c NFN_API int NFN_DECL nfn_get_numa_each(int numa_id,
NFN_API int NFN_DECL nfn_get_numa_each(int numa_id, uint64_t *internal, uint32_t *global)
=======
NIF_API int NIF_DECL nif_get_numa_each(int numa_id,
uint32_t *processor_group_ids, uint32_t *processor_group_ids,
uint32_t *socket_ids, uint32_t *socket_ids,
uint32_t *cpu_indices, uint32_t *cpu_indices,
uint32_t *numa_node_ids) uint32_t *numa_node_ids)
>>>>>>> 3a2b1234782f893f5b71655289b372ffed8dfd93:src/Nitload.Native/nif-src/thread.linux.c
{ {
if (numa_id < 0 || processor_group_ids == NULL || socket_ids == NULL || if (numa_id < 0 || processor_group_ids == NULL || socket_ids == NULL ||
cpu_indices == NULL || numa_node_ids == NULL) cpu_indices == NULL || numa_node_ids == NULL)
@@ -331,11 +308,7 @@ NIF_API int NIF_DECL nif_get_numa_each(int numa_id,
if (!nfn_linux_read_first_line(path, buffer, sizeof(buffer))) if (!nfn_linux_read_first_line(path, buffer, sizeof(buffer)))
return 0; return 0;
<<<<<<< HEAD:src/Nitload.Native/nfn-src/thread.linux.c nfn_linux_fill_cpu_context fill_context = {
nfn_linux_fill_cpu_context fill_context = { internal, global, 0 };
return nfn_linux_visit_id_list(buffer, nfn_linux_fill_cpu_id, &fill_context);
=======
nif_linux_fill_cpu_context fill_context = {
processor_group_ids, processor_group_ids,
socket_ids, socket_ids,
cpu_indices, cpu_indices,
@@ -343,8 +316,7 @@ NIF_API int NIF_DECL nif_get_numa_each(int numa_id,
numa_id, numa_id,
0 0
}; };
return nif_linux_visit_id_list(buffer, nif_linux_fill_cpu_id, &fill_context); return nfn_linux_visit_id_list(buffer, nfn_linux_fill_cpu_id, &fill_context);
>>>>>>> 3a2b1234782f893f5b71655289b372ffed8dfd93:src/Nitload.Native/nif-src/thread.linux.c
} }
NFN_API void *NFN_DECL nfn_numa_alloc(size_t size, int numa_id) NFN_API void *NFN_DECL nfn_numa_alloc(size_t size, int numa_id)
+12 -19
View File
@@ -2,15 +2,12 @@
#include "NitloadFrameWork.h" #include "NitloadFrameWork.h"
#if NFN_IS_OS(NFN_OS_WINDOWS) #if NFN_IS_OS(NFN_OS_WINDOWS)
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
<<<<<<< HEAD:src/Nitload.Native/nfn-src/thread.windows.c static int nfn_windows_get_socket_map(uint32_t **out_socket_map, DWORD *out_group_count)
/* Pin the calling thread to a single logical processor.
* core_id >= 0 : bind to that logical processor (global index, across groups).
* core_id < 0 : clear affinity constraint within the current group. */
NFN_API void NFN_DECL nfn_set_thread_affinity(int core_id, int numa_id)
=======
static int nif_windows_get_socket_map(uint32_t **out_socket_map, DWORD *out_group_count)
>>>>>>> 3a2b1234782f893f5b71655289b372ffed8dfd93:src/Nitload.Native/nif-src/thread.windows.c
{ {
DWORD group_count = GetActiveProcessorGroupCount(); DWORD group_count = GetActiveProcessorGroupCount();
DWORD buffer_size = 0; DWORD buffer_size = 0;
@@ -71,7 +68,7 @@ static int nif_windows_get_socket_map(uint32_t **out_socket_map, DWORD *out_grou
return 1; return 1;
} }
static int nif_windows_get_processor_node(WORD group, BYTE cpu, USHORT *node_id) static int nfn_windows_get_processor_node(WORD group, BYTE cpu, USHORT *node_id)
{ {
PROCESSOR_NUMBER processor; PROCESSOR_NUMBER processor;
memset(&processor, 0, sizeof(processor)); memset(&processor, 0, sizeof(processor));
@@ -81,7 +78,7 @@ static int nif_windows_get_processor_node(WORD group, BYTE cpu, USHORT *node_id)
} }
/* Pin the calling thread using its processor group and group-local CPU index. */ /* Pin the calling thread using its processor group and group-local CPU index. */
NIF_API void NIF_DECL nif_set_thread_affinity(int cpu_index, int processor_group_id) NFN_API void NFN_DECL nfn_set_thread_affinity(int cpu_index, int processor_group_id)
{ {
if (cpu_index < 0 || processor_group_id < 0) { if (cpu_index < 0 || processor_group_id < 0) {
GROUP_AFFINITY current; GROUP_AFFINITY current;
@@ -98,7 +95,7 @@ NIF_API void NIF_DECL nif_set_thread_affinity(int cpu_index, int processor_group
if ((DWORD)processor_group_id >= GetActiveProcessorGroupCount() || cpu_index >= 64) if ((DWORD)processor_group_id >= GetActiveProcessorGroupCount() || cpu_index >= 64)
return; return;
USHORT node_id = 0; USHORT node_id = 0;
if (!nif_windows_get_processor_node((WORD)processor_group_id, (BYTE)cpu_index, &node_id)) if (!nfn_windows_get_processor_node((WORD)processor_group_id, (BYTE)cpu_index, &node_id))
return; return;
GROUP_AFFINITY affinity; GROUP_AFFINITY affinity;
@@ -124,7 +121,7 @@ NFN_API int NFN_DECL nfn_get_numa_overall(int *out_numa_count, int *count_of_eac
for (DWORD group = 0; group < group_count; group++) { for (DWORD group = 0; group < group_count; group++) {
for (DWORD cpu = 0; cpu < 64; cpu++) { for (DWORD cpu = 0; cpu < 64; cpu++) {
USHORT node_id = 0; USHORT node_id = 0;
if (nif_windows_get_processor_node((WORD)group, (BYTE)cpu, &node_id) && if (nfn_windows_get_processor_node((WORD)group, (BYTE)cpu, &node_id) &&
count_of_each_numa != NULL && node_id <= highest) count_of_each_numa != NULL && node_id <= highest)
count_of_each_numa[node_id]++; count_of_each_numa[node_id]++;
} }
@@ -132,15 +129,11 @@ NFN_API int NFN_DECL nfn_get_numa_overall(int *out_numa_count, int *count_of_eac
return 1; return 1;
} }
<<<<<<< HEAD:src/Nitload.Native/nfn-src/thread.windows.c NFN_API int NFN_DECL nfn_get_numa_each(int numa_id,
NFN_API int NFN_DECL nfn_get_numa_each(int numa_id, uint64_t *internal, uint32_t *global)
=======
NIF_API int NIF_DECL nif_get_numa_each(int numa_id,
uint32_t *processor_group_ids, uint32_t *processor_group_ids,
uint32_t *socket_ids, uint32_t *socket_ids,
uint32_t *cpu_indices, uint32_t *cpu_indices,
uint32_t *numa_node_ids) uint32_t *numa_node_ids)
>>>>>>> 3a2b1234782f893f5b71655289b372ffed8dfd93:src/Nitload.Native/nif-src/thread.windows.c
{ {
if (numa_id < 0 || processor_group_ids == NULL || socket_ids == NULL || if (numa_id < 0 || processor_group_ids == NULL || socket_ids == NULL ||
cpu_indices == NULL || numa_node_ids == NULL) cpu_indices == NULL || numa_node_ids == NULL)
@@ -148,14 +141,14 @@ NIF_API int NIF_DECL nif_get_numa_each(int numa_id,
uint32_t *socket_map = NULL; uint32_t *socket_map = NULL;
DWORD group_count = 0; DWORD group_count = 0;
if (!nif_windows_get_socket_map(&socket_map, &group_count)) if (!nfn_windows_get_socket_map(&socket_map, &group_count))
return 0; return 0;
int index = 0; int index = 0;
for (DWORD group = 0; group < group_count; group++) { for (DWORD group = 0; group < group_count; group++) {
for (DWORD cpu = 0; cpu < 64; cpu++) { for (DWORD cpu = 0; cpu < 64; cpu++) {
USHORT node_id = 0; USHORT node_id = 0;
if (!nif_windows_get_processor_node((WORD)group, (BYTE)cpu, &node_id) || if (!nfn_windows_get_processor_node((WORD)group, (BYTE)cpu, &node_id) ||
node_id != (USHORT)numa_id) node_id != (USHORT)numa_id)
continue; continue;