1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace csharpTests
{
class cpuoverload
{
public static UInt64 globI = 0;
public static UInt64 globI1 = 0;
public static UInt64 globI2 = 0;
public static UInt64 globI3 = 0;
public static UInt64 globI4 = 0;
public static UInt64 globI5 = 0;
public static UInt64 globI6 = 0;
public static Int64 shared = 0;
/// <summary>
/// le run 1 se limite a 25% du cpu sur un quad core.
/// </summary>
public static void run1()
{
bool stop = false;
while (!stop)
{
++globI;
}
}
public static void run1_shared()
{
bool stop = false;
while (!stop)
System.Threading.Interlocked.Add(ref shared, 1);
}
public static void run1(object state)
{
// Console.WriteLine("ici state = {0}", state);
bool stop = false;
if (state != null)
{
// string tmp = (string)Convert.ChangeType(state, typeof(string));
string tmp2 = state as string;
switch (tmp2)
{
case "1":
while (!stop)
++globI1;
break;
case "2":
while (!stop)
++globI2;
break;
case "3":
while (!stop)
++globI3;
break;
case "4":
while (!stop)
++globI4;
break;
case "5":
while (!stop)
++globI5;
break;
case "6":
while (!stop)
++globI6;
break;
}
}
else
{
while (!stop)
{
++globI;
}
}
}
/// <summary>
/// le run 2 monte a ~30% sur un quad core et use deux process: conhost
/// ainsi que csrss
/// </summary>
public static void run2()
{
bool stop = false;
while (!stop)
{
Console.WriteLine(++globI);
}
}
/// <summary>
/// idem que run1, limite a 25% sur quad core meme avec un thread prio au plus haut.
/// </summary>
public static void run3()
{
System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.Highest;
bool stop = false;
UInt64 i = 0;
while (!stop)
{
++i;
}
}
/// <summary>
/// MAX CPU ~99%, lance le run1 dans plusieurs thread
/// </summary>
public static void run4()
{
System.Threading.Thread a = new System.Threading.Thread(new System.Threading.ThreadStart(run1));
System.Threading.Thread b = new System.Threading.Thread(new System.Threading.ThreadStart(run1));
System.Threading.Thread c = new System.Threading.Thread(new System.Threading.ThreadStart(run1));
System.Threading.Thread d = new System.Threading.Thread(new System.Threading.ThreadStart(run1));
a.Start();
b.Start();
c.Start();
d.Start();
}
public static void run4(object state)
{
System.Threading.Thread[] pool = state as System.Threading.Thread[];
pool[0].Start("1");
pool[1].Start("2");
pool[2].Start("3");
pool[3].Start("4");
pool[4].Start("5");
pool[5].Start("6");
// run1("4");
}
public static void run5(object state)
{
System.Threading.Thread[] pool = state as System.Threading.Thread[];
pool[0].Start();
pool[1].Start();
//pool[2].Start();
//pool[3].Start();
//pool[4].Start();
//pool[5].Start();
run1_shared();
}
/// <summary>
/// lance chaque run pour 5s et affiche le nombre d'op;
/// </summary>
public static void run()
{
globI = 0;
Console.WriteLine("start run1 for 5s ...", globI);
System.Threading.Timer t = new System.Threading.Timer(run1, null, 0, System.Threading.Timeout.Infinite);
System.Threading.Thread.Sleep(5000);
t.Dispose();
Console.WriteLine("{0} nbop", globI);
globI1 = 0;
globI2 = 0;
globI3 = 0;
globI4 = 0;
System.Threading.Thread a = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(run1));
System.Threading.Thread b = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(run1));
System.Threading.Thread c = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(run1));
System.Threading.Thread d = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(run1));
System.Threading.Thread e = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(run1));
System.Threading.Thread f = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(run1));
Console.WriteLine("start run4 for 5s ...");
t = new System.Threading.Timer(run4, new System.Threading.Thread[] {a,b,c,d, e, f}, 0, System.Threading.Timeout.Infinite);
System.Threading.Thread.Sleep(5000);
t.Dispose();
a.Abort();
b.Abort();
c.Abort();
d.Abort();
e.Abort();
f.Abort();
Console.WriteLine("{0} nbop = {1} + {2} + {3} + {4} + {5} + {6}", globI1 + globI2 + globI3 + globI4 + globI5 + globI6, globI1, globI2, globI3, globI4, globI5, globI6);
//System.Environment.Exit(42);
shared = 0;
a = new System.Threading.Thread(new System.Threading.ThreadStart(run1_shared));
b = new System.Threading.Thread(new System.Threading.ThreadStart(run1_shared));
c = new System.Threading.Thread(new System.Threading.ThreadStart(run1_shared));
d = new System.Threading.Thread(new System.Threading.ThreadStart(run1_shared));
e = new System.Threading.Thread(new System.Threading.ThreadStart(run1_shared));
f = new System.Threading.Thread(new System.Threading.ThreadStart(run1_shared));
Console.WriteLine("start run5 for 5s ...");
t = new System.Threading.Timer(run5, new System.Threading.Thread[] { a, b, c, d, e, f }, 0, System.Threading.Timeout.Infinite);
System.Threading.Thread.Sleep(5000);
t.Dispose();
a.Abort();
b.Abort();
c.Abort();
d.Abort();
e.Abort();
f.Abort();
Console.WriteLine("{0} nbop", System.Threading.Interlocked.Read(ref shared));
}
}
} |
Partager