A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
interface_statistics.py
Go to the documentation of this file.
1from gi.repository import Gtk
2
3try:
4 from nsnam.visualizer.base import InformationWindow
5except ModuleNotFoundError:
6 from visualizer.base import InformationWindow
7
8NODE_STATISTICS_MEMORY = 10
9
10
11
13 """
14 Collects interface statistics for all nodes.
15 """
16
20
21
23
24 __slots__ = ['rxPackets', 'rxBytes', 'txPackets', 'txBytes',
25 'rxPacketRate', 'rxBitRate', 'txPacketRate', 'txBitRate']
26
27 def __init__(self, visualizer):
28 """!
29 Collects interface statistics for all nodes.
30 @param self this object
31 @param visualizer visualizer object
32 """
33 self.node_statistics = {} # nodeid -> list(raw statistics)
34 self.visualizer = visualizer
35
37 """!
38 Simulation Periodic Update function.
39 @param self this object
40 @param viz visualizer object
41 @return none
42 """
43 nodes_statistics = viz.simulation.sim_helper.GetNodesStatistics()
44 for stats in nodes_statistics:
45 try:
46 raw_stats_list = self.node_statistics[stats.nodeId]
47 except KeyError:
48 raw_stats_list = []
49 self.node_statistics[stats.nodeId] = raw_stats_list
50 raw_stats_list.append(stats.statistics)
51 while len(raw_stats_list) > NODE_STATISTICS_MEMORY:
52 raw_stats_list.pop(0)
53
54 def get_interface_statistics(self, nodeId):
55 """!
56 Get interface statistics function.
57 @param self this object
58 @param nodeId node ID
59 @return the statistics
60 """
61 try:
62 raw_stats_list = self.node_statistics[nodeId]
63 except KeyError:
64 return []
65
66 if len(raw_stats_list) < NODE_STATISTICS_MEMORY:
67 return []
68 assert len(raw_stats_list) == NODE_STATISTICS_MEMORY
69 tx_packets1 = [] # transmitted packets, one value per interface
70 rx_packets1 = []
71 tx_bytes1 = []
72 rx_bytes1 = []
73 for iface, stats in enumerate(raw_stats_list[0]):
74 tx_packets1.append(stats.transmittedPackets)
75 tx_bytes1.append(stats.transmittedBytes)
76 rx_packets1.append(stats.receivedPackets)
77 rx_bytes1.append(stats.receivedBytes)
78
79 retval = []
80
81 k = self.visualizer.sample_period*(NODE_STATISTICS_MEMORY-1)
82 for iface, stats in enumerate(raw_stats_list[-1]):
83 outStat = self.NetDevStats()
84 outStat.txPackets = stats.transmittedPackets
85 outStat.txBytes = stats.transmittedBytes
86 outStat.rxPackets = stats.receivedPackets
87 outStat.rxBytes = stats.receivedBytes
88
89 outStat.txPacketRate = (stats.transmittedPackets - tx_packets1[iface])/k
90 outStat.rxPacketRate = (stats.receivedPackets - rx_packets1[iface])/k
91 outStat.txBitRate = (stats.transmittedBytes - tx_bytes1[iface])*8/k
92 outStat.rxBitRate = (stats.receivedBytes - rx_bytes1[iface])*8/k
93 retval.append(outStat)
94 return retval
95
96
97
99
111 (
112 COLUMN_INTERFACE,
113
114 COLUMN_TX_PACKETS,
115 COLUMN_TX_BYTES,
116 COLUMN_TX_PACKET_RATE,
117 COLUMN_TX_BIT_RATE,
118
119 COLUMN_RX_PACKETS,
120 COLUMN_RX_BYTES,
121 COLUMN_RX_PACKET_RATE,
122 COLUMN_RX_BIT_RATE,
123
124 ) = range(9)
125
126 def __init__(self, visualizer, node_index, statistics_collector):
127 """!
128 Initializer.
129 @param self this object
130 @param visualizer the visualizer object
131 @param node_index the node index
132 @param statistics_collector statistics collector class
133 """
134 InformationWindow.__init__(self)
135 self.win = Gtk.Dialog(parent=visualizer.window,
136 flags=Gtk.DialogFlags.DESTROY_WITH_PARENT,
137 buttons=("_Close", Gtk.ResponseType.CLOSE))
138 self.win.connect("response", self._response_cb)
139 self.win.set_title("Statistics for node %i" % node_index)
140 self.visualizer = visualizer
141 self.statistics_collector = statistics_collector
142 self.node_index = node_index
143 self.viz_node = visualizer.get_node(node_index)
144
145 self.table_model = Gtk.ListStore(*([str]*13))
146
147 treeview = Gtk.TreeView(self.table_model)
148 treeview.show()
149 self.win.vbox.add(treeview)
150
151 def add_column(descr, colid):
152 column = Gtk.TreeViewColumn(descr, Gtk.CellRendererText(), text=colid)
153 treeview.append_column(column)
154
155 add_column("Interface", self.COLUMN_INTERFACE)
156
157 add_column("Tx Packets", self.COLUMN_TX_PACKETS)
158 add_column("Tx Bytes", self.COLUMN_TX_BYTES)
159 add_column("Tx pkt/1s", self.COLUMN_TX_PACKET_RATE)
160 add_column("Tx bit/1s", self.COLUMN_TX_BIT_RATE)
161
162 add_column("Rx Packets", self.COLUMN_RX_PACKETS)
163 add_column("Rx Bytes", self.COLUMN_RX_BYTES)
164 add_column("Rx pkt/1s", self.COLUMN_RX_PACKET_RATE)
165 add_column("Rx bit/1s", self.COLUMN_RX_BIT_RATE)
166
167 self.visualizer.add_information_window(self)
168 self.win.show()
169
170 def _response_cb(self, win, response):
171 """!
172 Response callback function.
173 @param self this object
174 @param win the window
175 @param response the response
176 @return none
177 """
178 self.win.destroy()
179 self.visualizer.remove_information_window(self)
180
181 def update(self):
182 """!
183 Update function.
184 @param self this object
185 @return none
186 """
187 node = ns.NodeList.GetNode(self.node_index)
188 stats_list = self.statistics_collector.get_interface_statistics(self.node_index)
189 self.table_model.clear()
190 for iface, stats in enumerate(stats_list):
191 tree_iter = self.table_model.append()
192 netdevice = node.GetDevice(iface)
193 interface_name = ns.Names.FindName(netdevice)
194 if not interface_name:
195 interface_name = "(interface %i)" % iface
196 self.table_model.set(tree_iter,
197 self.COLUMN_INTERFACE, interface_name,
198
199 self.COLUMN_TX_PACKETS, str(stats.txPackets),
200 self.COLUMN_TX_BYTES, str(stats.txBytes),
201 self.COLUMN_TX_PACKET_RATE, str(stats.txPacketRate),
202 self.COLUMN_TX_BIT_RATE, str(stats.txBitRate),
203
204 self.COLUMN_RX_PACKETS, str(stats.rxPackets),
205 self.COLUMN_RX_BYTES, str(stats.rxBytes),
206 self.COLUMN_RX_PACKET_RATE, str(stats.rxPacketRate),
207 self.COLUMN_RX_BIT_RATE, str(stats.rxBitRate)
208 )
209
210
211def populate_node_menu(viz, node, menu, statistics_collector):
212
213 menu_item = Gtk.MenuItem("Show Interface Statistics")
214 menu_item.show()
215
216 def _show_it(dummy_menu_item):
217 ShowInterfaceStatistics(viz, node.node_index, statistics_collector)
218
219 menu_item.connect("activate", _show_it)
220 menu.add(menu_item)
221
222
223def register(viz):
224 statistics_collector = StatisticsCollector(viz)
225 viz.connect("populate-node-menu", populate_node_menu, statistics_collector)
226 viz.connect("simulation-periodic-update", statistics_collector.simulation_periodic_update)
def __init__(self, visualizer, node_index, statistics_collector)
Initializer.
def _response_cb(self, win, response)
Response callback function.
def get_interface_statistics(self, nodeId)
Get interface statistics function.
def __init__(self, visualizer)
Collects interface statistics for all nodes.
def simulation_periodic_update(self, viz)
Simulation Periodic Update function.
InformationWindow class.
Definition: base.py:25
def populate_node_menu(viz, node, menu, statistics_collector)