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