forked from kjpgit/techdemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
95 lines (79 loc) · 3.05 KB
/
Copy pathProgram.cs
File metadata and controls
95 lines (79 loc) · 3.05 KB
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
using System;
using System.Linq;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace dotnet_massive_async
{
class Program
{
static async Task Main(string[] args)
{
int numTasks = int.Parse(args[0]);
double sleepSeconds = double.Parse(args[1]);
//ThreadPool.SetMaxThreads(2, 2);
ThreadPool.GetMaxThreads(out int maxWorkerThreads, out int maxIOThreads);
ThreadPool.GetMinThreads(out int minWorkerThreads, out int minIOThreads);
Console.WriteLine($"MinThreads: {minWorkerThreads}, {minIOThreads}");
Console.WriteLine($"MaxThreads: {maxWorkerThreads}, {maxIOThreads}");
var scoreboard = new Scoreboard();
var taskList = new List<Task>();
for (var i = 0; i < numTasks; i++) {
var task = WorkTask(scoreboard, sleepSeconds);
// NB: Adding it to a list is unnecessary.
taskList.Add(task);
}
await scoreboard.PollScores();
}
static async Task WorkTask(Scoreboard scoreboard, double sleepSeconds)
{
while (true) {
await Task.Delay(TimeSpan.FromSeconds(sleepSeconds));
scoreboard.AddHit();
}
}
}
class Scoreboard
{
public Scoreboard() { }
// Record a hit for the current thread
// This shouldn't add a noticeable memory load,
// since there are so few physical threads.
public void AddHit() {
int threadId = Thread.CurrentThread.ManagedThreadId;
lock (this) {
long current = threadScore.GetValueOrDefault(threadId, 0);
threadScore[threadId] = current + 1;
}
}
// Run forever. Print stats every second.
public async Task PollScores() {
while (true) {
await Task.Delay(TimeSpan.FromSeconds(1.0));
lock (this) {
int numThreads = threadScore.Keys.Count;
long totalHits = threadScore.Values.Sum();
DateTime currentTime = DateTime.UtcNow;
TimeSpan elapsed = currentTime - lastDumpTime;
Console.WriteLine($"elapsed={elapsed} numThreads={numThreads}" +
$" totalHits={totalHits}");
PrintMemInfo();
threadScore.Clear();
lastDumpTime = currentTime;
}
}
}
private void PrintMemInfo() {
var lines = File.ReadAllText("/proc/self/status").Split('\n');
foreach (var line in lines) {
if (line.StartsWith("VmSize:") || line.StartsWith("VmRSS:")) {
Console.WriteLine(" - " + line);
}
}
}
DateTime lastDumpTime = DateTime.UtcNow;
// threadId -> operation count
Dictionary<int, long> threadScore = new Dictionary<int, long>();
}
}