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