Как я могу масштабировать количество одновременных работников на основе доступного процессора (Windows)?C#

Место общения программистов C#
Ответить
Anonymous
 Как я могу масштабировать количество одновременных работников на основе доступного процессора (Windows)?

Сообщение Anonymous »

Моя текущая реализация пытается выяснить доступный процессор в Percents, а затем появляется новину, если в наличии не менее 5%. < /p>
Вот код: < /p>

Код: Выделить всё

public class CpuAvailabilityMonitor
{
[StructLayout(LayoutKind.Sequential)]
private struct FILETIME { public uint Low; public uint High; }

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool GetSystemTimes(out FILETIME idleTime, out FILETIME kernelTime, out FILETIME userTime);

private static ulong FileTimeToUInt64(FILETIME ft) => ((ulong)ft.High 
    private async Task MonitorAndSpawnWorkersAsync(CancellationToken cancellationToken)
{
for (int i = 0; i < m_initialWorkers; i++)
{
m_workers.Add(StartWorkerLoop(cancellationToken));
}

while (await m_workChannel.Reader.WaitToReadAsync(cancellationToken))
{
double idlePercent = await CpuAvailabilityMonitor
.GetAvailableCpuPercentAsync(m_spawnInterval)
.ConfigureAwait(false);

History.Add(((float)idlePercent, m_workers.Count));

if (idlePercent > 5.0 && m_workers.Count < m_maxWorkers)
{
double headroomCores = (idlePercent / 100.0 - 0.05) * Environment.ProcessorCount;
int toSpawn = Math.Min((int)Math.Ceiling(headroomCores), m_maxWorkers - m_workers.Count);

for (int i = 0; i < toSpawn; i++)
{
m_workers.Add(StartWorkerLoop(cancellationToken));
}
}
}
}
< /code>
However, the number I collect into the History array do not align with what I see in Task Manager when the code runs. E.g. in the last run I got these numbers:
-       processor.History   Count = 21  System.Collections.Generic.List
+       [0] (89.23077, 6)   (float, int)
+       [1] (79.3059158, 17)    (float, int)
+       [2] (96.29156, 26)  (float, int)
+       [3] (94.61539, 37)  (float, int)
+       [4] (95.1492538, 48)    (float, int)
+       [5] (96.20733, 50)  (float, int)
+       [6] (94.23313, 50)  (float, int)
+       [7] (95.25641, 50)  (float, int)
+       [8] (93.0817642, 50)    (float, int)
+       [9] (95.37869, 50)  (float, int)
+       [10]    (94.44444, 50)  (float, int)
+       [11]    (93.72647, 50)  (float, int)
+       [12]    (94.4029846, 50)    (float, int)
+       [13]    (93.06804, 50)  (float, int)
+       [14]    (92.43379, 50)  (float, int)
+       [15]    (91.44444, 50)  (float, int)
+       [16]    (90.97222, 50)  (float, int)
+       [17]    (93.11876, 50)  (float, int)
+       [18]    (94.56384, 50)  (float, int)
+       [19]    (93.30808, 50)  (float, int)
+       [20]    (95.31645, 50)  (float, int)
< /code>
This is obviously BS, because the CPU was not idle above 90%. The Task Manager shows quite a lot of CPU activity.
My workloads are mixed - CPU + IO. I know it is suboptimal, but I cannot help it.
The max workers is 50.  The initial count is set to Environment.ProcessorCount / 2

Что мне не хватает? Который представляет собой смешанный мешок с ИО и ЦП, который я не могу контролировать. И у меня есть сотни объектов, которые для создания, и они имеют разные размеры.>

Подробнее здесь: https://stackoverflow.com/questions/795 ... le-cpu-win
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C#»