-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
1323 lines (1166 loc) · 64.5 KB
/
Program.cs
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Sources;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Filters;
using NLog.Web;
using zero.cocoon;
using zero.cocoon.autopeer;
using zero.cocoon.events.services;
using zero.cocoon.identity;
using zero.core.misc;
using zero.core.network.ip;
using zero.core.patterns.misc;
using zero.core.patterns.queue;
using zero.core.patterns.semaphore.core;
using zero.core.runtime.scheduler;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
namespace zero.sync
{
class Program
{
private static ConcurrentBag<CcCollective> _nodes = new();
private static volatile bool _running;
private static volatile bool _verboseGossip;
private static volatile bool _startAccounting;
private static int _rampDelay = 300;
private static int _rampTarget = 40;
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder
.ConfigureKestrel(options =>
{
options.ListenAnyIP(27021,
listenOptions => { listenOptions.Protocols = HttpProtocols.Http2; });
})
.UseStartup<StartServices>()
.UseNLog()
.ConfigureLogging(builder =>
{
builder.AddFilter(level => level > LogLevel.Error);
builder.ClearProviders();
builder.SetMinimumLevel(LogLevel.Error);
});
});
/// <summary>
/// Cluster test mode
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Console.WriteLine($"zero ({Environment.OSVersion}: {Environment.MachineName} - dotnet v{Environment.Version}, CPUs = {Environment.ProcessorCount})");
//Task.Factory.StartNew(async () =>
//{
// //await SemTestAsync();
// await QueueTestAsync();
//}, CancellationToken.None, TaskCreationOptions.DenyChildAttach, IoZeroScheduler.ZeroDefault).Unwrap().GetAwaiter().GetResult();
//Tune dotnet for large tests
ThreadPool.GetMinThreads(out var wt, out var cp);
ThreadPool.SetMinThreads(wt * 3, cp * 2);
LogManager.LoadConfiguration("nlog.config");
var portOffset = 7051;
IHost host = null;
var grpc = Task.Factory.StartNew(() =>
{
host = CreateHostBuilder(args).Build();
host.Run();
}, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
var random = new Random((int)DateTime.Now.Ticks);
//Tangle("tcp://192.168.1.2:15600");
var total = 702;
var maxDrones = 3;
var maxAdjuncts = 16;
var boot = false;
var tasks = new ConcurrentBag<Task<CcCollective>>();
if (boot)
// ReSharper disable once HeuristicUnreachableCode
{
var t1 = CoCoonAsync(CcDesignation.Generate(), $"tcp://127.0.0.1:{1234}", $"udp://127.0.0.1:{1234}",
$"tcp://127.0.0.1:{1234}", $"udp://127.0.0.1:{1234}",
new string[]
{
$"udp://127.0.0.1:{1235}",
$"udp://127.0.0.1:{1236}",
$"udp://127.0.0.1:{1237}"
}.ToList(), boot);
var t2 = CoCoonAsync(CcDesignation.Generate(), $"tcp://127.0.0.1:{1235}",
$"udp://127.0.0.1:{1235}", $"tcp://127.0.0.1:{1235}",
$"udp://127.0.0.1:{1235}",
new[]
{
$"udp://127.0.0.1:{1234}",
$"udp://127.0.0.1:{1236}",
$"udp://127.0.0.1:{1237}"
}.ToList(), boot);
var t3 = CoCoonAsync(CcDesignation.Generate(), $"tcp://127.0.0.1:{1236}", $"udp://127.0.0.1:{1236}",
$"tcp://127.0.0.1:{1236}", $"udp://127.0.0.1:{1236}",
new[]
{
$"udp://127.0.0.1:{1235}",
$"udp://127.0.0.1:{1234}",
$"udp://127.0.0.1:{1237}"
}.ToList(), boot);
var t4 = CoCoonAsync(CcDesignation.Generate(), $"tcp://127.0.0.1:{1237}",
$"udp://127.0.0.1:{1237}", $"tcp://127.0.0.1:{1237}",
$"udp://127.0.0.1:{1237}",
new[]
{
$"udp://127.0.0.1:{1234}",
$"udp://127.0.0.1:{1235}",
$"udp://127.0.0.1:{1236}"
}.ToList(), boot);
tasks = new ConcurrentBag<Task<CcCollective>>
{
t1,t2,t3,t4
};
IoZeroScheduler.Zero.LoadAsyncCallback(() => {t1.Start();return default;});
IoZeroScheduler.Zero.LoadAsyncCallback(() => {t2.Start();return default;});
IoZeroScheduler.Zero.LoadAsyncCallback(() => {t3.Start();return default;});
IoZeroScheduler.Zero.LoadAsyncCallback(() => {t4.Start();return default;});
}
else
#pragma warning disable CS0162
{
var t1 = CoCoonAsync(CcDesignation.Generate(true), $"tcp://127.0.0.1:{1234}", $"udp://127.0.0.1:{1234}",
$"tcp://127.0.0.1:{1234}", $"udp://127.0.0.1:{1234}",
new string[]
{
//$"udp://127.0.0.1:{1235}",
//$"udp://127.0.0.1:{1236}",
//$"udp://127.0.0.1:{1237}"
}.ToList(), false);
var t2 = CoCoonAsync(CcDesignation.Generate(), $"tcp://127.0.0.1:{1235}",
$"udp://127.0.0.1:{1235}", $"tcp://127.0.0.1:{1235}",
$"udp://127.0.0.1:{1235}",
new string[]
{
$"udp://127.0.0.1:{1234}",
//$"udp://127.0.0.1:{1236}",
//$"udp://127.0.0.1:{1237}"
}.ToList(), false);
tasks = new ConcurrentBag<Task<CcCollective>>
{
t1,t2
};
#pragma warning disable VSTHRD110 // Observe result of async calls
Task.Factory.StartNew(() => t1.Start(), CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
Task.Factory.StartNew(() => t2.Start(), CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
#pragma warning restore VSTHRD110 // Observe result of async calls
}
#pragma warning restore CS0162
for (var i = 2; i < total; i++)
{
var range = 20;
//tasks.Add(CoCoonAsync(CcDesignation.Generate(), $"tcp://127.0.0.1:{1234 + portOffset + i}", $"udp://127.0.0.1:{1234 + portOffset + i}", $"tcp://127.0.0.1:{1334 + portOffset + i}", $"udp://127.0.0.1:{1234 + portOffset + i}", new[] { $"udp://127.0.0.1:{1234 + i % 4}", $"udp://127.0.0.1:{1234 + (i + 1) % 4}" }.ToList()));
var o1 = Random.Shared.Next(portOffset + Math.Max(0, i - range), portOffset + i);
var o2 = Random.Shared.Next(portOffset + Math.Max(0, i - range), portOffset + i);
var o3 = Random.Shared.Next(portOffset + Math.Max(0, i - range), portOffset + i);
var o4 = Random.Shared.Next(portOffset + Math.Max(0, i - range), portOffset + i);
//tasks.Add(CoCoonAsync(CcDesignation.Generate(), $"tcp://127.0.0.1:{1234 + portOffset + i}", $"udp://127.0.0.1:{1234 + portOffset + i}", $"tcp://127.0.0.1:{1334 + portOffset + i}", $"udp://127.0.0.1:{1234 + portOffset + i}", new[] { $"udp://127.0.0.1:{1234}", $"udp://127.0.0.1:{1235}", $"udp://127.0.0.1:{1234 + o1}", $"udp://127.0.0.1:{1234 + o2}", $"udp://127.0.0.1:{1234 + o3}" , $"udp://127.0.0.1:{1234 + o4}" }.ToList()));
tasks.Add(CoCoonAsync(CcDesignation.Generate(), $"tcp://127.0.0.1:{1234 + portOffset + i}", $"udp://127.0.0.1:{1234 + portOffset + i}", $"tcp://127.0.0.1:{1334 + portOffset + i}", $"udp://127.0.0.1:{1234 + portOffset + i}", new[] { $"udp://127.0.0.1:{1235}", $"udp://127.0.0.1:{1234 + o1}", $"udp://127.0.0.1:{1234 + o2}", $"udp://127.0.0.1:{1234 + o3}", $"udp://127.0.0.1:{1234 + o4}" }.ToList()));
if (tasks.Count % 10 == 0)
Console.WriteLine($"Spawned {tasks.Count}/{total}...");
}
_ = Task.Factory.StartNew(async () =>
{
Console.WriteLine($"Starting auto peering... {tasks.Count}");
var c = 1;
var rateLimit = 9000;
var injectionCount = 40;
var rampDelay = 250;
foreach (var task in tasks)
{
await Task.Factory.StartNew(async () =>
{
if (task.Status == TaskStatus.Created)
{
if (c == 1)
await Task.Delay(5000);
Console.WriteLine($"added {c++}/{tasks.Count}");
task.Start();
}
else
{
var queen = (CcCollective)task.AsyncState;
while (boot && !queen!.Online)
{
Console.WriteLine("Waiting for queen...");
await Task.Delay(1000);
}
Console.WriteLine($"Queen brought up {queen.Description}");
}
}, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default).Unwrap();
if (c % injectionCount == 0)
{
await Task.Delay(rateLimit += 10).ConfigureAwait(false);
Console.WriteLine($"Provisioned {c}/{total}...");
Console.WriteLine($"Provisioned {c}/{total}...");
Console.WriteLine($"Provisioned {c}/{total}...");
Console.WriteLine($"Provisioned {c}/{total}...");
Console.WriteLine($"Provisioned {c}/{total}...");
Console.WriteLine($"Provisioned {c}/{total}...");
if(injectionCount > 40)
injectionCount--;
}
await Task.Delay(rampDelay).ConfigureAwait(false);
if(rampDelay -1 > 0)
rampDelay -= 1;
}
}, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
_running = true;
var outBound = 0;
var inBound = 0;
var available = 0;
var logger = LogManager.GetCurrentClassLogger();
_ = Task.Factory.StartNew(() =>
{
var ooutBound = 0;
var oinBound = 0;
var oavailable = 0;
long uptime = 0;
long uptimeCount = 1;
long opeers = 0;
long peers = 0;
int minOut = int.MaxValue;
int minIn = int.MaxValue;
int minOutC = 0;
int minInC = 0;
int empty = 0;
long lastUpdate = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
long perfTime = Environment.TickCount, totalPerfTime = Environment.TickCount;
long opsCheckpoint = ThreadPool.CompletedWorkItemCount, totalOpsCheckpoint = ThreadPool.CompletedWorkItemCount;
while (_running)
{
try
{
ooutBound = 0;
oinBound = 0;
oavailable = 0;
opeers = 0;
empty = 0;
minOut = int.MaxValue;
minIn = int.MaxValue;
minOutC = 0;
minInC = 0;
uptime = 0;
uptimeCount = 1;
foreach (var ioCcNode in _nodes)
{
//if (ioCcNode.Zeroed())
//{
// Thread.Sleep(1000);
// continue;
//}
try
{
opeers += ioCcNode.Drones.Count;
var e = ioCcNode.EgressCount;
var i = ioCcNode.IngressCount;
minOut = Math.Min(minOut, e);
minIn = Math.Min(minIn, i);
if (ioCcNode.TotalConnections == 0)
empty++;
if (ioCcNode.EgressCount == 0)
minOutC++;
if (ioCcNode.IngressCount == 0)
minInC++;
if (ioCcNode.TotalConnections > ioCcNode.MaxDrones)
{
Console.WriteLine($"[{ioCcNode.Description}]");
foreach (var d in ioCcNode.Neighbors.Values)
{
var drone = (CcDrone) d;
Console.WriteLine($"t = {ioCcNode.TotalConnections} ({ioCcNode.IngressCount},{ioCcNode.EgressCount}), [{ioCcNode.Description}] -> {drone.Description} ][ {drone.Adjunct.MetaDesc}, uptime = {drone.UpTime.ElapsedMs():0.0}s");
}
}
ooutBound += e;
oinBound += i;
oavailable += ioCcNode.Hub.Neighbors.Values.Count(static n => ((CcAdjunct)n).IsProxy);
uptime += ioCcNode.Hub.Neighbors.Values.Select(static n =>
{
if (((CcAdjunct)n).IsDroneConnected && ((CcAdjunct)n).AttachTimestamp > 0)
return ((CcAdjunct)n).AttachTimestamp.ElapsedUtc();
return 0;
}).Sum();
uptimeCount += ioCcNode.TotalConnections;
}
catch
{
}
}
if (outBound != ooutBound || inBound != oinBound || available != oavailable || opeers != peers)
{
var prevPeers = peers;
outBound = ooutBound;
inBound = oinBound;
peers = opeers;
available = oavailable;
ThreadPool.GetAvailableThreads(out var wt, out var cpt);
ThreadPool.GetMaxThreads(out var maxwt, out var maxcpt);
ThreadPool.GetMinThreads(out var minwt, out var mincpt);
var localOps = (ThreadPool.CompletedWorkItemCount - opsCheckpoint);
var totalOps = (ThreadPool.CompletedWorkItemCount - totalOpsCheckpoint);
var fps = localOps / (double)perfTime.ElapsedMsToSec();
var tfps = totalOps / (double)totalPerfTime.ElapsedMsToSec();
opsCheckpoint = ThreadPool.CompletedWorkItemCount;
perfTime = Environment.TickCount;
try
{
Console.ForegroundColor = prevPeers <= peers ? ConsoleColor.Green : ConsoleColor.Red;
Console.WriteLine($"({outBound},{inBound}), ({minOut}/{minOutC},{minIn}/{minInC}::{empty}), {inBound + outBound}/{(double)_nodes.Count * maxDrones}, p = {peers}/{available}({_nodes.Count * maxAdjuncts}), {(inBound + outBound) / ((double)_nodes.Count * maxDrones * 2) * 100:0.00}%, t = {TimeSpan.FromSeconds(uptime / (double)uptimeCount).TotalHours:0.000}h, T = {TimeSpan.FromSeconds(uptime).TotalDays:0.00} days, ({minwt} < {wt} < {maxwt}), ({mincpt} < {cpt} < {maxcpt}), con = {CcAdjunct.ConnectionTime/(CcAdjunct.ConnectionCount + 1.0):0.0}ms, [iKo/s = {fps / 1000.0:0.000}, i = {localOps / 1000000.0:0.000} Mil], [tKo/s = {tfps / 1000.0:0.000}, t = {totalOps / 1000000.0:0.000} Mil]");
}
catch{}
finally
{
Console.ResetColor();
}
}
else if (DateTimeOffset.UtcNow.ToUnixTimeSeconds() - lastUpdate > 120)
{
ThreadPool.GetAvailableThreads(out var wt, out var cpt);
ThreadPool.GetMaxThreads(out var maxwt, out var maxcpt);
ThreadPool.GetMinThreads(out var minwt, out var mincpt);
var localOps = (ThreadPool.CompletedWorkItemCount - opsCheckpoint);
var totalOps = (ThreadPool.CompletedWorkItemCount - totalOpsCheckpoint);
var fps = localOps / (double)perfTime.ElapsedMsToSec();
var tfps = totalOps / (double)totalPerfTime.ElapsedMsToSec();
perfTime = Environment.TickCount;
opsCheckpoint = ThreadPool.CompletedWorkItemCount;
Console.ForegroundColor = ConsoleColor.Green;
try
{
Console.WriteLine($"({outBound},{inBound}), ({minOut}/{minOutC},{minIn}/{minInC}::{empty}), {inBound + outBound}/{(double)_nodes.Count * maxDrones}, p = {peers}/{available}({_nodes.Count * maxAdjuncts}), {(inBound + outBound) / (double)(_nodes.Count * maxDrones * 2) * 100:0.00}%, t = {TimeSpan.FromSeconds(uptime / (double)uptimeCount).TotalHours:0.000}h, T = {TimeSpan.FromSeconds(uptime).TotalDays:0.00} days, w = {-wt + maxwt}, ports = {-cpt + maxcpt}, connect time = {CcAdjunct.ConnectionTime / (CcAdjunct.ConnectionCount + 1.0):0.0}ms, [iKo/s = {fps / 1000.0:0.000}, i = {localOps / 1000000.0:0.000} Mil], [tKo/s = {tfps / 1000.0:0.000}, t = {totalOps / 1000000.0:0.000} Mil]");
}
catch
{
// ignored
}
finally
{
Console.ResetColor();
}
lastUpdate = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
}
if (!_startAccounting && (inBound + outBound) / (double)(_nodes.Count * maxDrones) > 0.955)
{
_startAccounting = true;
}
}
catch (Exception e)
{
Console.WriteLine($"Failed... {e.Message} {e.InnerException}");
}
Thread.Sleep(30000);
}
}, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);
long C = 0;
string line;
var ts = Environment.TickCount;
var tsOrig = Environment.TickCount;
var cOrig = ThreadPool.CompletedWorkItemCount;
var c = ThreadPool.CompletedWorkItemCount;
var LS = Environment.TickCount;
var LSOrig = Environment.TickCount;
var LOrig = IoZeroScheduler.Zero.CompletedWorkItemCount;
var LC = IoZeroScheduler.Zero.CompletedWorkItemCount;
var QS = Environment.TickCount;
var QSOrig = Environment.TickCount;
var QOrig = IoZeroScheduler.Zero.CompletedQItemCount;
var QC = IoZeroScheduler.Zero.CompletedQItemCount;
var AS = Environment.TickCount;
var AC = IoZeroScheduler.Zero.CompletedAsyncCount;
AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) =>
{
Console.WriteLine("###");
};
Console.CancelKeyPress += (sender, args) =>
{
args.Cancel = true;
ZeroAsync(total).AsTask().GetAwaiter();
};
while ((line = Console.ReadLine()) != null && !line.StartsWith("quit"))
{
try
{
if (line == "gc")
{
var pinned = GC.GetGCMemoryInfo(GCKind.Any).PinnedObjectsCount;
GC.Collect(GC.MaxGeneration);
Console.WriteLine($"Pinned was = {pinned}, now {GC.GetGCMemoryInfo(GCKind.Any).PinnedObjectsCount}");
}
if (line == "quit")
break;
if (line == "t")
{
Console.WriteLine($"load = {IoZeroScheduler.Zero.Load}({IoZeroScheduler.Zero.LoadFactor * 100:0.0}%), q time = {ThreadPool.PendingWorkItemCount / ((ThreadPool.CompletedWorkItemCount - c) / (double)ts.ElapsedMs()):0}ms, threads = {ThreadPool.ThreadCount}({IoZeroScheduler.Zero.ThreadCount}), p = {ThreadPool.PendingWorkItemCount}({IoZeroScheduler.Zero.WLength}), t = {ThreadPool.CompletedWorkItemCount}, {(ThreadPool.CompletedWorkItemCount - cOrig) / (double)tsOrig.ElapsedMsToSec():0.0} ops, c = {ThreadPool.CompletedWorkItemCount-c}, {(ThreadPool.CompletedWorkItemCount-c)/(double)ts.ElapsedMsToSec():0.0} tps");
ts = Environment.TickCount;
c = ThreadPool.CompletedWorkItemCount;
}
if (line == "L")
{
Console.WriteLine($"ZERO SCHEDULER: load = {IoZeroScheduler.Zero.Load}({IoZeroScheduler.Zero.LoadFactor * 100:0.0}%), q time = {IoZeroScheduler.Zero.WLength / ((IoZeroScheduler.Zero.CompletedWorkItemCount - LC) / (double)LS.ElapsedMs()):0}ms, workers = {IoZeroScheduler.Zero.ThreadCount}, p = {IoZeroScheduler.Zero.WLength}, C = {IoZeroScheduler.Zero.CompletedWorkItemCount - LOrig}, {(IoZeroScheduler.Zero.CompletedWorkItemCount - LOrig) / (double)LSOrig.ElapsedMsToSec():0.0} ops, c = {IoZeroScheduler.Zero.CompletedWorkItemCount - LC}, {(IoZeroScheduler.Zero.CompletedWorkItemCount - LC) / (double)LS.ElapsedMsToSec():0.0} tps");
Console.WriteLine($"[{IoZeroScheduler.Zero.Load}/{IoZeroScheduler.Zero.AsyncLoad}/{IoZeroScheduler.Zero.ForkLoad}/{IoZeroScheduler.Zero.Capacity}] load = {IoZeroScheduler.Zero.Load/ IoZeroScheduler.Zero.Capacity * 100:0.0}%, w time = {IoZeroScheduler.Zero.QTime:0.0}ms, aq time = {IoZeroScheduler.Zero.AQTime:0.0}ms, poll% = {((IoZeroScheduler.Zero.CompletedWorkItemCount - LOrig - LC) - (IoZeroScheduler.Zero.CompletedQItemCount - QC)) / (double)(IoZeroScheduler.Zero.CompletedWorkItemCount - LC) * 100.0:0.0}%, ({(IoZeroScheduler.Zero.CompletedWorkItemCount - LOrig - (IoZeroScheduler.Zero.CompletedQItemCount - QOrig)) / (double)(IoZeroScheduler.Zero.CompletedWorkItemCount - LOrig) * 100.0:0.00}%), p = {IoZeroScheduler.Zero.QLength}, F = {IoZeroScheduler.Zero.ForkLoad}, C = {IoZeroScheduler.Zero.CompletedQItemCount - QOrig}, {(IoZeroScheduler.Zero.CompletedWorkItemCount - QOrig) / (double)QSOrig.ElapsedMsToSec():0.0} ops, c = {IoZeroScheduler.Zero.CompletedQItemCount- QC}, {(IoZeroScheduler.Zero.CompletedQItemCount - QC) / (double)QS.ElapsedMsToSec():0.0} tps, {(IoZeroScheduler.Zero.CompletedAsyncCount - AC) / (double)AS.ElapsedMsToSec():0.0} async/s ({IoZeroScheduler.Zero.CompletedAsyncCount - AC})[{IoZeroScheduler.Zero.CompletedAsyncCount}]");
QS = Environment.TickCount;
AS = Environment.TickCount;
LS = Environment.TickCount;
QC = IoZeroScheduler.Zero.CompletedQItemCount;
LC = IoZeroScheduler.Zero.CompletedWorkItemCount;
AC = IoZeroScheduler.Zero.CompletedAsyncCount;
}
if (line.StartsWith("W"))
{
IoZeroScheduler.Dump();
}
if (line.StartsWith("logf"))
{
try
{
var line1 = line;
var res = line1.Split(' ')[1];
if (!string.IsNullOrEmpty(res))
{
LogManager.Configuration.LoggingRules.Last().Filters.Add(new WhenMethodFilter(logEvent =>
{
var res = logEvent.Message.Contains(line1.Split(' ')[1]);
return res
? FilterResult.Log
: FilterResult.Ignore;
}));
LogManager.Configuration = LogManager.Configuration;
}
}
catch { }
}
if (line.StartsWith("log "))
{
try
{
LogManager.Configuration.Variables["zeroLogLevel"] = $"{line.Split(' ')[1]}";
LogManager.ReconfigExistingLoggers();
}
catch { }
}
if (line.StartsWith("ramp "))
{
try
{
if (line.Contains("target"))
{
_rampTarget = int.Parse(line.Split(' ')[2]);
Console.WriteLine($"rampTarget = {_rampTarget}");
}
if (line.Contains("delay"))
{
_rampDelay = int.Parse(line.Split(' ')[2]);
Console.WriteLine($"rampDelay = {_rampDelay}");
}
}
catch { }
}
if (line.StartsWith("loadTest"))
{
var n = _nodes.Where(n => !n.ZeroDrone).ToArray();
IoZeroScheduler.Zero.LoadAsyncCallback(async () => await n[Random.Shared.Next(0, n.Length - 1)].BootAsync(0).FastPath());
IoZeroScheduler.Zero.LoadAsyncCallback(async () => await n[Random.Shared.Next(0, n.Length - 1)].BootAsync(0).FastPath());
IoZeroScheduler.Zero.LoadAsyncCallback(async () => await n[Random.Shared.Next(0, n.Length - 1)].BootAsync(0).FastPath());
IoZeroScheduler.Zero.LoadAsyncCallback(async () => await n[Random.Shared.Next(0, n.Length - 1)].BootAsync(0).FastPath());
Console.WriteLine("ZERO CORE best case horizontal scale cluster TCP/IP (DDoS) pressure test started... make sure your CPU has enough juice, this test will redline your kernel and hang your OS");
}
if (line.StartsWith("gr"))
{
foreach (var ccCollective in _nodes)
{
ccCollective.Drones.ForEach(d=>d.ToggleAccountingBit());
}
}
var gossipTasks = new List<Task>();
if (line.StartsWith("gossip"))
{
if (line.Contains("verbose"))
{
_verboseGossip = !_verboseGossip;
Console.WriteLine($"gossip verbose = {_verboseGossip}");
}
else if (line.Contains("restart"))
{
if (gossipTasks.Count > 0)
{
_running = false;
_startAccounting = false;
IoZeroScheduler.Zero.LoadAsyncCallback(async () =>
{
await Task.WhenAll(gossipTasks.ToArray());
await tasks.ToList().ForEachAsync<Task<CcCollective>, object>(static async (t,_) =>
{
#pragma warning disable VSTHRD003 // Avoid awaiting foreign Tasks
(await t).ClearDupBuf();
#pragma warning restore VSTHRD003 // Avoid awaiting foreign Tasks
}, null);
//tasks.ToList().ForEach(t => t.Result.ClearDupBuf());
gossipTasks.Clear();
await AutoPeeringEventService.QueuedEvents[0].ZeroManagedAsync<object>().FastPath();
});
}
long v = 1;
_rampDelay = 5000;
_rampTarget = 1000;
var start = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
var threads = 3;
_running = true;
for (int i = 0; i < threads; i++)
{
var i1 = i;
gossipTasks.Add(Task.Factory.StartNew(async () =>
{
while (!_startAccounting)
await Task.Delay(1000);
Console.WriteLine($"Starting accounting... {tasks.Count}");
await Task.Delay(random.Next(1000 / threads) * i1);
while (_running)
{
if (!_startAccounting)
{
await Task.Delay(1000);
continue;
}
foreach (var t in tasks)
{
if (!_startAccounting)
break;
var ts2 = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (!await ((CcCollective)t.AsyncState)!.BootAsync(Interlocked.Increment(ref v)).FastPath()
)
{
if (_verboseGossip)
Console.WriteLine($"* - {ts2.ElapsedMs()}ms");
continue;
}
if (_verboseGossip)
Console.WriteLine($". - {ts2.ElapsedMs()}ms");
//ramp
if (_rampDelay > _rampTarget)
{
Interlocked.Decrement(ref _rampDelay);
//Console.WriteLine($"ramp = {_rampDelay}");
}
await Task.Delay(_rampDelay).ConfigureAwait(false);
if (Interlocked.Increment(ref C) % 5000 == 0)
{
Console.WriteLine($"{C / ((DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - start) / 1000.0):0.0} g/ps");
Interlocked.Exchange(ref C, 0);
start = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
}
}
}
Console.WriteLine("Stopped gossip...");
}, CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default));
_startAccounting = true;
}
Console.WriteLine($"Stopped auto peering... {tasks.Count}");
}
else
{
_startAccounting = !_startAccounting;
Console.WriteLine($"gossip = {_startAccounting}");
}
}
if (line.StartsWith("dn "))
{
try
{
_nodes.FirstOrDefault(n => n.CcId.IdString() == line.Split(' ')[1]).PrintNeighborhood();
}
catch (Exception){}
}
if (line.StartsWith("ec"))
{
if (line.Contains("clear"))
{
foreach (var ccCollective in _nodes)
ccCollective.ClearEventCounter();
}
else
{
var nodes= _nodes.Where(n=>!n.ZeroDrone).ToList();
try
{
var min = nodes.Min(n => n.EventCount);
var max = nodes.Max(n => n.EventCount);
//var nmax = max;
var ave = nodes.Average(n => n.EventCount);
var err = nodes.Select(n => Math.Abs(n.EventCount - ave)).Average();
var r = 0;
var target = 0;
while (err > (target = 50 - r/100 * 40) && r++ < 100 && nodes.Count > 0)
{
nodes = nodes.Where(n => Math.Abs(n.EventCount - ave) < target).ToList();
ave = nodes.Average(n => n.EventCount);
err = nodes.Select(n => Math.Abs(n.EventCount - ave)).Average();
//nmax = nodes.Max(n => n.EventCount);
}
Console.WriteLine($"zero liveness at {nodes.Count()/((double)_nodes.Count - 4)*100:0.00}%, {nodes.Count()}/{_nodes.Count-4}, min/max = [{min},{max}], ave = {ave:0.0}, err = {err:0.0}, r = {r}");
}
catch (Exception) { }
}
}
if (line.StartsWith("stream"))
{
IoZeroScheduler.Zero.LoadAsyncCallback(async () => await AutoPeeringEventService.ToggleActiveAsync());
Console.WriteLine($"event stream = {(AutoPeeringEventService.Operational? "On":"Off")}");
}
if (line.StartsWith("zero"))
{
IoZeroScheduler.Zero.LoadAsyncCallback(async () =>
{
await ZeroAsync(total).FastPath();
if (_nodes != null)
{
Console.WriteLine($"z = {_nodes.Count(n => n.Zeroed())}/{total}");
_nodes.Clear();
_nodes = null;
tasks?.Clear();
tasks = null;
}
});
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
};
_running = false;
tasks?.Clear();
tasks = null;
_nodes?.Clear();
_nodes = null;
var s = host.StopAsync();
host = null;
LogManager.Shutdown();
Console.WriteLine("## - done");
GC.Collect(GC.MaxGeneration);
Console.ReadLine();
}
/// <summary>
/// Tests queue
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
private static async Task QueueTestAsync() //TODO make unit tests
{
await Task.Delay(1000);
var concurrencyLevel = Environment.ProcessorCount;
IoQueue<int> q = new("test", 16384, concurrencyLevel);
var head = await q.PushBackAsync(2).FastPath();
await q.PushBackAsync(1).FastPath();
await q.EnqueueAsync(3).FastPath();
await q.EnqueueAsync(4).FastPath();
var five = await q.PushBackAsync(5).FastPath();
await q.EnqueueAsync(6).FastPath();
await q.EnqueueAsync(7).FastPath();
await q.EnqueueAsync(8).FastPath();
var tail = await q.EnqueueAsync(9).FastPath();
Console.WriteLine("Init");
foreach (var ioZNode in q)
{
Console.Write(ioZNode.Value);
}
Console.WriteLine("\nDQ mid");
q.RemoveAsync(five).FastPath().GetAwaiter();
foreach (var ioZNode in q)
{
Console.Write(ioZNode.Value);
}
Console.WriteLine();
var c = q.Tail;
while (c != null)
{
Console.Write(c.Value);
c = c.Prev;
}
Console.WriteLine("\nDQ head");
q.RemoveAsync(q.Head).FastPath().GetAwaiter();
foreach (var ioZNode in q)
{
Console.Write(ioZNode.Value);
}
Console.WriteLine();
c = q.Tail;
while (c != null)
{
Console.Write(c.Value);
c = c.Prev;
}
Console.WriteLine("\nDQ tail");
q.RemoveAsync(q.Tail).FastPath().GetAwaiter();
foreach (var ioZNode in q)
{
Console.Write(ioZNode.Value);
}
Console.WriteLine();
c = q.Tail;
while (c != null)
{
Console.Write(c.Value);
c = c.Prev;
}
Console.WriteLine("\nDQ second last prime");
q.DequeueAsync().FastPath().GetAwaiter();
q.RemoveAsync(q.Head).FastPath().GetAwaiter();
q.RemoveAsync(q.Head).FastPath().GetAwaiter();
q.DequeueAsync().FastPath().GetAwaiter();
foreach (var ioZNode in q)
{
Console.Write(ioZNode.Value);
}
Console.WriteLine();
c = q.Tail;
while (c != null)
{
Console.Write(c.Value);
c = c.Prev;
}
Console.WriteLine("\nDQ second last");
if (true)
{
q.RemoveAsync(q.Head).FastPath().GetAwaiter();
q.RemoveAsync(q.Head).FastPath().GetAwaiter();
}
foreach (var ioZNode in q)
{
Console.Write(ioZNode.Value);
}
Console.WriteLine();
c = q.Head;
while (c != null)
{
Console.Write(c.Value);
c = c.Next;
}
var _concurrentTasks = new List<Task>();
var start = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
var rounds = concurrencyLevel;
var mult = 1000000;
//var rounds = 5;
//var mult = 1000;
for (var i = 0; i < rounds; i++)
{
Console.Write(".");
var i3 = i;
_concurrentTasks.Add(Task.Factory.StartNew(async () =>
{
for (int j = 0; j < mult; j++)
{
try
{
//await q.PushBackAsync(i3).FastPath();//Console.WriteLine("1");
await q.EnqueueAsync(i3 + 1).FastPath();//Console.WriteLine("2");
await q.EnqueueAsync(i3 + 1).FastPath();//Console.WriteLine("2");
await q.EnqueueAsync(i3 + 1).FastPath();//Console.WriteLine("2");
await q.EnqueueAsync(i3 + 1).FastPath();//Console.WriteLine("2");
await q.EnqueueAsync(i3 + 3).FastPath();//Console.WriteLine("4");
//await q.PushBackAsync(i3 + 2).FastPath();//Console.WriteLine("3");
//var n = await q.PushBackAsync(i3 + 4).FastPath();//Console.WriteLine("5");
//await q.RemoveAsync(n).FastPath();//Console.WriteLine("6");
await q.DequeueAsync().FastPath();//Console.WriteLine("7");
await q.DequeueAsync().FastPath();//Console.WriteLine("8");
await q.DequeueAsync().FastPath();//Console.WriteLine("9");
await q.DequeueAsync().FastPath();//Console.WriteLine("10");
await q.DequeueAsync().FastPath();//Console.WriteLine("10");
}
catch (Exception e)
{
Console.WriteLine($"Failed... {e.Message}");
break;
}
//if(j%2000 == 0)
// Console.Write($"({i3}-{q.Count})");
}
Console.Write($"({i3}-{q.Count})");
}, new CancellationToken(), TaskCreationOptions.DenyChildAttach, IoZeroScheduler.ZeroDefault).Unwrap());
}
await Task.WhenAll(_concurrentTasks);
Console.WriteLine($"count = {q.Count}, Head = {q?.Tail?.Value}, tail = {q?.Head?.Value}, time = {DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - start}ms, {rounds*mult*6/(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - start)} kOPS");
q.Reset();
foreach (var ioZNode in q)
{
Console.Write($"{ioZNode.Value},");
}
Console.ReadLine();
throw new Exception("done");
}
/// <summary>
/// Tests the semaphore
/// </summary>
private static async Task SemTestAsync() //TODO make unit tests
{
await Task.Delay(1000);
var asyncTasks = new CancellationTokenSource();
var capacity = 4;
//.NET RUNTIME REFERENCE MUTEX FOR TESTING
//var mutex = new IoZeroRefMut(asyncTasks.Token);
//var mutex = new IoZeroSemaphoreSlim(asyncTasks, "zero slim", maxBlockers: capacity, initialCount: 1, zeroAsyncMode: false, enableAutoScale: false, enableFairQ: false, enableDeadlockDetection: true);
IIoZeroSemaphoreBase<int> mutex = new IoZeroCore<int>("zero core", capacity, 1, false);
mutex = mutex.ZeroRef(ref mutex, o => Environment.TickCount);
var releaseCount = 2;
var waiters = 3;
var releasers = 4;
var disableRelease = false;
var targetSleep = (long)0;
var logSpam = 50000;//at least 1
var totalReleases = int.MaxValue;
var targetSleepMult = waiters > 1 ? 2 : 1;
var sw = new Stopwatch();
var sw2 = new Stopwatch();
var c = 0;
long semCount = 0;
long semPollCount = 0;
// IoFpsCounter wfps1 = new IoFpsCounter(1000, 10000);
// IoFpsCounter wfps2 = new IoFpsCounter(1000, 10000);
// IoFpsCounter ifps1 = new IoFpsCounter(1000, 10000);
// IoFpsCounter ifps2 = new IoFpsCounter(1000, 10000);
uint eq1 = 0;
uint eq2 = 0;
uint eq3 = 0;
var eq1_fps = 0.0;
var eq2_fps = 0.0;
var eq3_fps = 0.0;
var dq_fps = new double[10];
var r = new Random();
var dq = new long[10];
var ERR_T = 16 * 3;
//TaskCreationOptions options = TaskCreationOptions.LongRunning | TaskCreationOptions.PreferFairness | TaskCreationOptions.RunContinuationsAsynchronously;
//TaskCreationOptions options = TaskCreationOptions.None;
TaskCreationOptions options = TaskCreationOptions.DenyChildAttach;
var startTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
var mainSW = new Stopwatch();
const int qTimeE_ms = 16;
var t1 = Task.Factory.StartNew(async () =>
{
try
{
while (waiters>0)
{
mainSW.Restart();
var qt = await mutex.WaitAsync().FastPath();
//Debug.Assert(qt.ElapsedMs() < ERR_T);
if (qt.ElapsedMs() < targetSleep + ERR_T)
{
var tt = mainSW.ElapsedMilliseconds;
Interlocked.Increment(ref eq1);
Action a = (tt - targetSleep * targetSleepMult) switch
{
> qTimeE_ms<<1 => () => Console.ForegroundColor = ConsoleColor.DarkRed,
> qTimeE_ms => () => Console.ForegroundColor = ConsoleColor.DarkYellow,
> qTimeE_ms>>1 => () => Console.ForegroundColor = ConsoleColor.DarkGreen,
< -qTimeE_ms << 1 => () => Console.ForegroundColor = ConsoleColor.DarkRed,
< -qTimeE_ms => () => Console.ForegroundColor = ConsoleColor.DarkGreen,
< -qTimeE_ms>>2 => () => Console.ForegroundColor = ConsoleColor.DarkYellow,
< qTimeE_ms>>2 => () => Console.ForegroundColor = ConsoleColor.Green,
_ => () => Console.ForegroundColor = ConsoleColor.DarkGray,
};
a();
//var curTime = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond - 62135596800000;
var curTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
double delta = (curTime - startTime + 1) / 1000.0;
if (delta > 5)
{
eq1_fps = (eq1 / delta);
eq2_fps = (eq2 / delta);
eq3_fps = (eq3 / delta);
for (int i = 0; i < releasers; i++)
{