Compare commits

..
2 Commits
Author SHA1 Message Date
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
11 changed files with 288 additions and 123 deletions
+5
View File
@@ -0,0 +1,5 @@
root = true
[*.cs]
dotnet_diagnostic.IDE0032.severity = none
dotnet_diagnostic.IDE0055.severity = none
@@ -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}].");
} }
@@ -16,7 +16,10 @@ namespace Nitload.Common.HighPerformance.Memory
public unsafe partial class L4 public unsafe partial class L4
{ {
internal partial int Predict() => 0; internal partial int Predict()
{
throw new NotImplementedException();
}
} }
} }
@@ -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,97 @@ using System.Threading.Tasks;
namespace Nitload.Common.HighPerformance.Memory namespace Nitload.Common.HighPerformance.Memory
{ {
public class L4 public partial class L4
{ {
private readonly IL4Predictor[] _ring = new IL4Predictor[256];
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 void RegistPredictor(
IL4Predictor predictor
)
{
if (predictor is null) {
return;
}
SpinWait sp = new();
while (Interlocked.CompareExchange(ref _queue_sync, 1, 0) != 0) {
sp.SpinOnce();
}
WaitForSpare();
_consume++;
byte idx = (byte)_consume;
_ring[idx] = predictor;
}
private void WaitForSpare()
{
int spin_count = 0;
// wait until the queue has space
while (_enter - _consume >= 256)
{
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
@@ -93,11 +95,65 @@ namespace Nitload.Common.HighPerformance.Memory
} }
internal unsafe struct SwissEnumerator(
byte* buffer
)
{
private readonly SwissBucket* _buckets = (SwissBucket*)(buffer + IdealCount);
private readonly Vector128<byte>* _control_bytes = (Vector128<byte>*)buffer;
private int _current_group;
private int _current_mask;
private int _current_offset;
private bool _group_loaded;
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 bool MoveNext()
{
while (_current_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++;
_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 +189,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 +217,40 @@ 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.");
} }
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 +259,14 @@ 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
@@ -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,
+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;