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