A Discrete-Event Network Simulator
API
interface_statistics.py
Go to the documentation of this file.
1 from gi.repository import Gtk
2 
3 try:
4  from ns3.visualizer.base import InformationWindow
5 except ModuleNotFoundError:
6  from visualizer.base import InformationWindow
7 
8 NODE_STATISTICS_MEMORY = 10
9 
10 
11 
13  """
14  Collects interface statistics for all nodes.
15  """
16 
17 
21 
22 
24 
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_statisticsnode_statistics = {} # nodeid -> list(raw statistics)
43  self.visualizervisualizer = 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_statisticsnode_statistics[stats.nodeId]
56  except KeyError:
57  raw_stats_list = []
58  self.node_statisticsnode_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_statisticsnode_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.visualizervisualizer.sample_period * (NODE_STATISTICS_MEMORY - 1)
91  for iface, stats in enumerate(raw_stats_list[-1]):
92  outStat = self.NetDevStatsNetDevStats()
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 
108 
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.winwin = Gtk.Dialog(
162  parent=visualizer.window,
163  flags=Gtk.DialogFlags.DESTROY_WITH_PARENT,
164  buttons=("_Close", Gtk.ResponseType.CLOSE),
165  )
166  self.winwin.connect("response", self._response_cb_response_cb)
167  self.winwin.set_title("Statistics for node %i" % node_index)
168  self.visualizervisualizer = visualizer
169  self.statistics_collectorstatistics_collector = statistics_collector
170  self.node_indexnode_index = node_index
171  self.viz_nodeviz_node = visualizer.get_node(node_index)
172 
173  self.table_modeltable_model = Gtk.ListStore(*([str] * 13))
174 
175  treeview = Gtk.TreeView(self.table_modeltable_model)
176  treeview.show()
177  self.winwin.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.visualizervisualizer.add_information_window(self)
196  self.winwin.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.winwin.destroy()
207  self.visualizervisualizer.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_indexnode_index)
216  stats_list = self.statistics_collectorstatistics_collector.get_interface_statistics(self.node_indexnode_index)
217  self.table_modeltable_model.clear()
218  for iface, stats in enumerate(stats_list):
219  tree_iter = self.table_modeltable_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_modeltable_model.set(
225  tree_iter,
226  self.COLUMN_INTERFACE,
227  interface_name,
228  self.COLUMN_TX_PACKETS,
229  str(stats.txPackets),
230  self.COLUMN_TX_BYTES,
231  str(stats.txBytes),
232  self.COLUMN_TX_PACKET_RATE,
233  str(stats.txPacketRate),
234  self.COLUMN_TX_BIT_RATE,
235  str(stats.txBitRate),
236  self.COLUMN_RX_PACKETS,
237  str(stats.rxPackets),
238  self.COLUMN_RX_BYTES,
239  str(stats.rxBytes),
240  self.COLUMN_RX_PACKET_RATE,
241  str(stats.rxPacketRate),
242  self.COLUMN_RX_BIT_RATE,
243  str(stats.rxBitRate),
244  )
245 
246 
247 def 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 
258 def 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)
def _response_cb(self, win, response)
Response callback function.
def __init__(self, visualizer, node_index, statistics_collector)
Initializer.
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:28
def populate_node_menu(viz, node, menu, statistics_collector)