[or-cvs] r15445: Script takes p% to (p+5)% slices of guard nodes, and builds (torflow/branches/gsoc2008)

fallon at seul.org fallon at seul.org
Tue Jun 24 08:51:37 UTC 2008


Author: fallon
Date: 2008-06-24 04:51:37 -0400 (Tue, 24 Jun 2008)
New Revision: 15445

Added:
   torflow/branches/gsoc2008/guardslices.py
Log:
Script takes p% to (p+5)% slices of guard nodes, and builds circuits using those slices of guards. It outputs detailed circuit events, build times, and aggregate statistics.


Nonfunctional: File that outputs buildtimes contains nothing; details file and agg file should contain data
Bugs: One about a NoneType exception, that is difficult to reproduce



Added: torflow/branches/gsoc2008/guardslices.py
===================================================================
--- torflow/branches/gsoc2008/guardslices.py	                        (rev 0)
+++ torflow/branches/gsoc2008/guardslices.py	2008-06-24 08:51:37 UTC (rev 15445)
@@ -0,0 +1,157 @@
+#!/usr/bin/env python
+# uses metatroller to collect circuit build times for 5% slices of guard nodes
+# [OUTPUT] one directory, with three files: StatsHandler aggregate stats file, file with all circuit events (for detailed reference), file with just buildtimes
+
+import socket,sys,time,getopt,os
+from TorCtl.TorUtil import meta_port,meta_host,control_port,control_host
+from StatsSupport import StatsHandler
+from TorCtl import PathSupport, TorCtl
+__selmgr = PathSupport.SelectionManager(
+      pathlen=3,
+      order_exits=True,
+      percent_fast=80,
+      percent_skip=0,
+      min_bw=1024,
+      use_all_exits=True,
+      uniform=True,
+      use_exit=None,
+      use_guards=True,
+      restrict_guards=True)
+
+class Connection(PathSupport.Connection):
+  
+  """ thread quits when required number of circuits found, otherwise identical"""
+  def __init__(self,s):
+    PathSupport.Connection.__init__(self,s)
+  def _loop(self):
+    while 1:
+      try:
+        isEvent, reply = self._read_reply()
+      except:
+        self._err(sys.exc_info())
+        return
+
+      if isEvent:
+        if self._handler is not None:
+          self._eventQueue.put((time.time(), reply))
+      else:
+        cb = self._queue.get() # atomic..
+        cb(reply)
+
+      if self._handler is not None:
+        if self._handler.circ_failed + self._handler.circ_built >= self._handler.nstats:
+          print 'Finished gathering',self._handler.circ_failed + self._handler.circ_built,'circuits'
+          print self._handler.circ_failed,'failed',self._handler.circ_built,'built'
+          return 
+
+class StatsGatherer(StatsHandler):
+  def __init__(self,c, selmgr,basefile_name,nstats):
+    StatsHandler.__init__(self,c, selmgr)
+
+    self.detailfile = open(basefile_name + '.detail','w')
+    self.timesfile = open(basefile_name + '.buildtimes','w')
+    self.circ_built = 0
+    self.nstats = nstats
+
+
+  def circ_status_event(self, circ_event):
+    now = time.time()
+    now = '%3.10f' % now
+
+    if circ_event.circ_id in self.circuits.keys():
+      output = [circ_event.event_name, str(circ_event.circ_id),
+          circ_event.status]
+      if circ_event.path:
+        output.append(",".join(circ_event.path))
+      if circ_event.reason:
+        output.append("REASON=" + circ_event.reason)
+      if circ_event.remote_reason:
+        output.append("REMOTE_REASON=" + circ_event.remote_reason)
+     
+      output = [now]+ output
+      outstr = ' '.join(output) + '\n'
+      self.detailfile.write(outstr)
+      self.detailfile.flush()
+      if self.circuits[circ_event.circ_id].setup_duration:
+        self.buildtimesfile.write(str(self.circuits[circ_event.circ_id].setup_duration) + '\n')
+      print 'wrote',outstr
+
+      # check to see if done gathering data
+      if circ_event.status == 'BUILT': self.circ_built += 1
+
+    StatsHandler.circ_status_event(self,circ_event)
+
+def getdata(filename,ncircuits):
+  """ starts stat gathering thread """
+
+  s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
+  s.connect((control_host,control_port))
+  c = Connection(s)
+  c.authenticate()  # also launches thread...
+  h = StatsGatherer(c,__selmgr,filename,ncircuits)
+  c.set_event_handler(h)
+
+  c.set_events([TorCtl.EVENT_TYPE.STREAM,
+                TorCtl.EVENT_TYPE.BW,
+                TorCtl.EVENT_TYPE.NS,
+                TorCtl.EVENT_TYPE.CIRC,
+                TorCtl.EVENT_TYPE.STREAM_BW,
+                TorCtl.EVENT_TYPE.NEWDESC], True)
+  return c
+def setargs():
+  ncircuits = ""
+  dirname = ""
+  filename = ""
+  if len(sys.argv[1:]) < 4:
+    usage()
+    sys.exit(2)
+  try:
+    opts,args = getopt.getopt(sys.argv[1:],"p:n:d:")
+  except getopt.GetoptError,err:
+    print str(err)
+    usage()
+  for o,a in opts:
+    if o == '-n': 
+      if a.isdigit(): ncircuits = int(a)
+      else: usage()
+    elif o == '-d': dirname = a  #directory where output files go 
+    elif o == '-p':
+      if a.isdigit(): percentile = int(a)
+      else: usage()
+    else:
+      assert False, "Bad option"
+  return ncircuits,percentile,dirname
+
+def usage():
+    print 'usage: statscontroller.py -p <#percentile>  -n <# circuits> -d <output dir name>'
+    sys.exit(1)
+def main():
+  ncircuits,p,dirname = setargs()
+
+  print 'Making new directory:',dirname
+  if not os.path.isdir(dirname):
+    os.mkdir(dirname)
+  else:
+    print 'Directory',dirname,'exists, not making a new one.'
+
+  print 'Guard percentiles:',p,'to',p+5
+  print '#Circuits',ncircuits
+
+  basefile_name = dirname + '/' + str(p) + '-' + str(p+5) + '.' + str(ncircuits)
+  aggfile_name =  basefile_name + '.agg'
+
+  __selmgr.percent_fast = p+5
+  __selmgr.percent_skip = p
+  
+  c = getdata(basefile_name,ncircuits) 
+  for i in range(0,ncircuits):
+    circ = c.build_circuit(__selmgr.pathlen,__selmgr.path_selector)
+    c._handler.circuits[circ.circ_id] = circ
+  while True:
+    if c._handler.circ_built + c._handler.circ_failed >= ncircuits:
+      print 'Done gathering'
+      break
+  c._handler.write_stats(aggfile_name)
+
+if __name__ == '__main__':
+  main()


Property changes on: torflow/branches/gsoc2008/guardslices.py
___________________________________________________________________
Name: svn:executable
   + *



More information about the tor-commits mailing list