This repository has been archived by the owner on Apr 20, 2018. It is now read-only.
forked from microsoft/Tx
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTcpSyntheticCounters.cs
118 lines (98 loc) · 4.24 KB
/
TcpSyntheticCounters.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
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Linq.Charting;
using System.Net;
using System.Reactive;
using System.Reactive.Linq;
using System.Security.Principal;
using System.Windows.Forms;
using Tx.Windows;
using Tx.Windows.Microsoft_Windows_Kernel_Network;
namespace TcpSyntheticCounters
{
public partial class TcpSyntheticCounters : Form
{
const string SessionName = "tcp";
Guid ProviderId = new Guid("{7dd42a49-5329-4832-8dfd-43d979153a88}");
IDisposable _subscription;
Playback _playback;
Column _received;
Column _send;
Chart _chart;
Dictionary<string, FastLine> _trends = new Dictionary<string, FastLine>();
DateTime _start = DateTime.Now;
public TcpSyntheticCounters()
{
InitializeComponent();
ChartArea area = new ChartArea();
area.AxisX.Title = "Minutes since start";
area.AxisX.TitleFont = new System.Drawing.Font(area.AxisX.TitleFont, FontStyle.Bold);
area.AxisX.ArrowStyle = System.Windows.Forms.DataVisualization.Charting.AxisArrowStyle.Triangle;
area.AxisY.Title = "Bytes per second";
area.AxisY.TitleFont = new System.Drawing.Font(area.AxisY.TitleFont, FontStyle.Bold);
area.AxisY.ArrowStyle = System.Windows.Forms.DataVisualization.Charting.AxisArrowStyle.Triangle;
_received = new Column { Points = { }, LegendText = "Received" };
_send = new Column { Points = { }, LegendText = "Send" };
_chart = new Chart
{
ChartAreas = { area },
Dock = DockStyle.Fill,
};
this.Controls.Add(_chart);
}
private void TcpSyntheticCounters_Load(object sender, EventArgs e)
{
StartSession(SessionName, ProviderId);
_playback = new Playback();
_playback.AddRealTimeSession("tcp");
var received = _playback.GetObservable<KNetEvt_RecvIPV4>();
var x = from window in received.Window(TimeSpan.FromSeconds(1), _playback.Scheduler)
from stats in
( from packet in window
group packet by packet.daddr into g
from total in g.Sum(p=>p.size)
select new
{
address = new IPAddress(g.Key).ToString(),
received = total
})
.ToList()
select stats.OrderBy(s=>s.address);
_subscription = x.ObserveOn(_chart).Subscribe(v =>
{
_chart.BeginInit();
foreach (var point in v)
AddPoint(point.address, point.received);
_chart.Invalidate();
_chart.EndInit();
});
_playback.Start();
}
void AddPoint(string address, double received)
{
FastLine trend;
if (!_trends.TryGetValue(address, out trend))
{
trend = new FastLine{ LegendText=address };
_trends.Add(address, trend);
_chart.BaseSeries.Add(trend);
}
var x = DateTime.Now.Subtract(_start).TotalMinutes;
trend.Add(x, new FastLine.DataPoint(received));
}
static void StartSession(string sessionName, Guid providerId)
{
var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
throw new Exception("To use ETW real-time session you must be administrator");
Process logman = Process.Start("logman.exe", "stop " + sessionName + " -ets");
logman.WaitForExit();
logman = Process.Start("logman.exe", "create trace " + sessionName + " -rt -nb 2 2 -bs 1024 -p {" + providerId + "} 0xffffffffffffffff -ets");
logman.WaitForExit();
}
}
}