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