提交 13134655 编写于 作者: cdy816's avatar cdy816

完成指定线程到指定CPU

上级 a88acdbd
......@@ -44,13 +44,13 @@ namespace Cdy.Tag
/// CPU 个数
/// </summary>
/// <returns></returns>
public static int CPUNumbers()
public static int GetProcessNumbers()
{
return Environment.ProcessorCount;
}
/// <summary>
///
/// 将当前线程绑定到指定CPU
/// </summary>
/// <param name="cpus"></param>
public static void AssignToCPU(params int[] cpus)
......@@ -69,6 +69,27 @@ namespace Cdy.Tag
}
}
/// <summary>
/// 获取当前线程所在CPU ID
/// </summary>
/// <returns></returns>
public static uint GetCurrentProcessorNumber()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return Win32Native.NtGetCurrentProcessorNumber();
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
}
else
{
//to do none;
}
return uint.MaxValue;
}
/// <summary>
/// Sets a processor affinity mask for the current thread.
/// </summary>
......
......@@ -2,7 +2,7 @@
"profiles": {
"DBInRun": {
"commandName": "Executable",
"executablePath": "C:\\Users\\Lenovo\\source\\repos\\mars\\Output\\DBInRun.exe"
"executablePath": "C:\\Users\\cdy81\\source\\repos\\mars\\Output\\DBInRun.exe"
}
}
}
\ No newline at end of file
//==============================================================
// Copyright (C) 2020 Chongdaoyang Inc. All rights reserved.
//
//==============================================================
// Create by 种道洋 at 2020/7/20 20:07:14 .
// Version 1.0
// CDYWORK
//==============================================================
using System;
using System.Collections.Generic;
using System.Text;
namespace Cdy.Tag
{
/// <summary>
///
/// </summary>
public class CPUAssignHelper
{
#region ... Variables ...
/// <summary>
///
/// </summary>
public static CPUAssignHelper Helper = new CPUAssignHelper();
#endregion ...Variables...
#region ... Events ...
#endregion ...Events...
#region ... Constructor...
#endregion ...Constructor...
#region ... Properties ...
/// <summary>
///
/// </summary>
public int[] CPUArray1 { get; set; }
/// <summary>
///
/// </summary>
public int[] CPUArray2 { get; set; }
#endregion ...Properties...
#region ... Methods ...
/// <summary>
///
/// </summary>
public void Init()
{
int count = ThreadHelper.GetProcessNumbers();
if(count==1)
{
CPUArray1 = new int[] { 0 };
CPUArray2 = new int[] { 0 };
}
else if(count==2)
{
CPUArray1 = new int[] { 0 };
CPUArray2 = new int[] { 1 };
}
else
{
List<int> ltmp = new List<int>();
for(int i=0;i<count/2;i++)
{
ltmp.Add(i);
}
CPUArray1 = ltmp.ToArray();
ltmp.Clear();
for(int i=count/2;i<count;i++)
{
ltmp.Add(i);
}
CPUArray2 = ltmp.ToArray();
}
}
#endregion ...Methods...
#region ... Interfaces ...
#endregion ...Interfaces...
}
}
......@@ -47,7 +47,6 @@ namespace Cdy.Tag
private long mTotalSize = 0;
public static List<int> UsedCPUs = new List<int>();
#endregion ...Variables...
......@@ -197,14 +196,14 @@ namespace Cdy.Tag
private void ThreadPro()
{
while(!mIsClosed)
ThreadHelper.AssignToCPU(CPUAssignHelper.Helper.CPUArray2);
while (!mIsClosed)
{
resetEvent.WaitOne();
resetEvent.Reset();
if (mIsClosed)
break;
ThreadHelper.AssignToCPU(UsedCPUs.ToArray());
//#if DEBUG
Stopwatch sw = new Stopwatch();
sw.Start();
......@@ -218,13 +217,9 @@ namespace Cdy.Tag
Thread.Sleep(500);
}
//foreach (var mm in mTargetMemorys)
//{
// mm.Value.Compress(sm);
//}
System.Threading.Tasks.Parallel.ForEach(mTargetMemorys, (mm) =>
{
ThreadHelper.AssignToCPU(CPUAssignHelper.Helper.CPUArray2);
mm.Value.Compress(sm);
});
......
......@@ -31,7 +31,7 @@ namespace Cdy.Tag
private IHisEngine mHisTagService;
private Dictionary<int, CompressUnitbase> mCompressCach = new Dictionary<int, CompressUnitbase>();
private Dictionary<int, long> dtmp = new Dictionary<int, long>();
private bool mIsDisposed=false;
//private bool mIsDisposed=false;
private bool mIsRunning=false;
......@@ -289,7 +289,7 @@ namespace Cdy.Tag
mHisTagService = null;
mTagAddress.Clear();
mTagAddress = null;
mIsDisposed = true;
//mIsDisposed = true;
base.Dispose();
}
......
......@@ -624,6 +624,7 @@ namespace Cdy.Tag
{
int number = MergeMemoryTime / CachMemoryTime;
int count = mStartMergeCount;
ThreadHelper.AssignToCPU(CPUAssignHelper.Helper.CPUArray1);
while (!mIsClosed)
{
......
......@@ -270,6 +270,7 @@ namespace Cdy.Tag
private void ThreadPro()
{
ThreadHelper.AssignToCPU(CPUAssignHelper.Helper.CPUArray2);
while (!mIsClosed)
{
resetEvent.WaitOne();
......
......@@ -52,11 +52,6 @@ namespace Cdy.Tag
private int mBusyCount = 0;
/// <summary>
///
/// </summary>
public static List<int> UsedCPUs = new List<int>();
#endregion ...Variables...
#region ... Events ...
......@@ -181,6 +176,9 @@ namespace Cdy.Tag
/// </summary>
private void ThreadProcess()
{
ThreadHelper.AssignToCPU(CPUAssignHelper.Helper.CPUArray1);
closedEvent.Reset();
var vkeys = mCount.Keys.ToArray();
while (!mIsClosed)
......@@ -188,6 +186,8 @@ namespace Cdy.Tag
resetEvent.WaitOne();
resetEvent.Reset();
if (mIsClosed) break;
// LoggerService.Service.Info("TimerMemoryCacheProcesser", ThreadHelper.GetCurrentProcessorNumber() + " CPU in running!");
try
{
mIsBusy = true;
......
......@@ -48,10 +48,6 @@ namespace Cdy.Tag
private Thread mRecordThread;
public static List<int> UsedCPUs = new List<int>();
#endregion ...Variables...
#region ... Events ...
......@@ -185,6 +181,8 @@ namespace Cdy.Tag
/// </summary>
private void ThreadProcess()
{
ThreadHelper.AssignToCPU(CPUAssignHelper.Helper.CPUArray1);
closedEvent.Reset();
while (!mIsClosed)
{
......
......@@ -158,6 +158,8 @@ namespace Cdy.Tag
}
InitPath();
CPUAssignHelper.Helper.Init();
if (CheckDatabaseExist(mDatabaseName))
{
LoadDatabase();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册