markus / ConvertService / ServiceBase / ProecssAffinityTest / Class1.cs @ 3abe8d4e
이력 | 보기 | 이력해설 | 다운로드 (4.73 KB)
1 |
using System; |
---|---|
2 |
using System.Collections; |
3 |
using System.Collections.Generic; |
4 |
using System.Diagnostics; |
5 |
using System.Linq; |
6 |
using System.Text; |
7 |
using System.Threading.Tasks; |
8 |
|
9 |
namespace ProecssAffinityTest |
10 |
{ |
11 |
public class test |
12 |
{ |
13 |
static List<long> ProcessorAffinityList = new List<long>(); |
14 |
static List<item> processItems = new List<item>(); |
15 |
static string ProcessName = "ProcessAffinityTestProcess.exe"; |
16 |
[MTAThread] |
17 |
static void Main(string[] args) |
18 |
{ |
19 |
var processkill = Process.GetProcessesByName("ProcessAffinityTestProcess"); |
20 |
|
21 |
foreach (var item in processkill) |
22 |
{ |
23 |
item.Kill(); |
24 |
} |
25 |
|
26 |
Console.Write("input Process Count :"); |
27 |
var str = Console.ReadLine(); |
28 |
|
29 |
int count = 10; |
30 |
|
31 |
try |
32 |
{ |
33 |
int parseValue = 0; |
34 |
|
35 |
if(int.TryParse(str,out parseValue)) |
36 |
{ |
37 |
count = parseValue; |
38 |
} |
39 |
} |
40 |
catch (Exception) |
41 |
{ |
42 |
|
43 |
throw; |
44 |
} |
45 |
|
46 |
|
47 |
int processCount = Environment.ProcessorCount; |
48 |
|
49 |
if (count == 0) |
50 |
{ |
51 |
count = processCount; |
52 |
} |
53 |
|
54 |
Console.WriteLine($"Check Process Count :{processCount}"); |
55 |
Console.WriteLine("input any key"); |
56 |
Console.ReadKey(); |
57 |
|
58 |
long affinityMask = 0; |
59 |
|
60 |
for (int i = count; i > 0; i--) |
61 |
{ |
62 |
BitArray bitArray = new BitArray(processCount); |
63 |
|
64 |
if (processCount -1 < i) |
65 |
{ |
66 |
bitArray = new BitArray(processCount, true); |
67 |
} |
68 |
else |
69 |
{ |
70 |
bitArray.Set(i, true); |
71 |
} |
72 |
var bits = bitArray.Cast<bool>().Select(bit => bit ? 1 : 0); |
73 |
|
74 |
affinityMask = System.Convert.ToInt64(string.Join("", bits), 2); |
75 |
|
76 |
System.Diagnostics.Debug.WriteLine($"index{string.Format("0#", ProcessorAffinityList.Count())} - {string.Join("", bits)}"); |
77 |
|
78 |
ProcessorAffinityList.Add(affinityMask); |
79 |
Console.WriteLine($"{affinityMask}"); |
80 |
} |
81 |
|
82 |
Console.WriteLine("input any key"); |
83 |
Console.ReadKey(); |
84 |
|
85 |
while (true) |
86 |
{ |
87 |
var process = Process.GetProcessesByName("ProcessAffinityTestProcess"); |
88 |
|
89 |
if (process.Count() < count) |
90 |
{ |
91 |
Process testProcess = new Process(); |
92 |
long currentAffinity = 0; |
93 |
|
94 |
if (ProcessorAffinityList.Count() > 0) |
95 |
{ |
96 |
var processAffinity = ProcessorAffinityList.Except(process.Select(x => x.ProcessorAffinity.ToInt64())); |
97 |
|
98 |
if (processAffinity.Count() > 0) |
99 |
{ |
100 |
currentAffinity = processAffinity.First(); |
101 |
|
102 |
Console.WriteLine($"process currentAffinity set {currentAffinity}"); |
103 |
} |
104 |
else |
105 |
{ |
106 |
var items = processItems.Where(x => x.AffinityMask > 0).Select(x => x.AffinityMask).Distinct(); |
107 |
|
108 |
|
109 |
currentAffinity = items.Min(); |
110 |
|
111 |
Console.WriteLine($"process currentAffinity processAffinity.Count() > 0 : {currentAffinity}"); |
112 |
} |
113 |
|
114 |
processItems.Add(new item { AffinityMask = currentAffinity}); |
115 |
} |
116 |
|
117 |
ProcessStartInfo startInfo = new ProcessStartInfo |
118 |
{ |
119 |
UseShellExecute = false, |
120 |
FileName = ProcessName, |
121 |
WindowStyle = ProcessWindowStyle.Hidden, |
122 |
CreateNoWindow = true, |
123 |
ErrorDialog = false, |
124 |
RedirectStandardError = false, |
125 |
Arguments = $"{currentAffinity}" |
126 |
}; |
127 |
|
128 |
testProcess.StartInfo = startInfo; |
129 |
testProcess.EnableRaisingEvents = true; |
130 |
|
131 |
if (testProcess.Start()) |
132 |
{ |
133 |
testProcess.ProcessorAffinity = new IntPtr(currentAffinity); |
134 |
} |
135 |
} |
136 |
|
137 |
System.Threading.Thread.Sleep(100); |
138 |
} |
139 |
} |
140 |
private static long GetIntFromBitArray(BitArray bitArray) |
141 |
{ |
142 |
var array = new int[bitArray.Length]; |
143 |
bitArray.CopyTo(array, 0); |
144 |
return (uint)array[0] + ((long)(uint)array[1] << bitArray.Length); |
145 |
} |
146 |
} |
147 |
|
148 |
internal class item |
149 |
{ |
150 |
public long AffinityMask { get; set; } |
151 |
} |
152 |
} |