2015-11-24 21:03:53 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
import os, time
|
|
|
|
from collections import defaultdict
|
|
|
|
from ..database import get_db
|
|
|
|
from ..errors import UsageError
|
|
|
|
|
|
|
|
def abbrev(t):
|
|
|
|
if t is None:
|
|
|
|
return "-"
|
|
|
|
if t > 1.0:
|
|
|
|
return "%.3fs" % t
|
|
|
|
if t > 1e-3:
|
|
|
|
return "%.1fms" % (t*1e3)
|
|
|
|
return "%.1fus" % (t*1e6)
|
|
|
|
|
2015-12-04 04:15:29 +00:00
|
|
|
def abbreviate_space(s, SI=True):
|
|
|
|
if s is None:
|
|
|
|
return "-"
|
|
|
|
if SI:
|
|
|
|
U = 1000.0
|
|
|
|
isuffix = "B"
|
|
|
|
else:
|
|
|
|
U = 1024.0
|
|
|
|
isuffix = "iB"
|
|
|
|
def r(count, suffix):
|
|
|
|
return "%.2f %s%s" % (count, suffix, isuffix)
|
|
|
|
|
|
|
|
if s < 1024: # 1000-1023 get emitted as bytes, even in SI mode
|
|
|
|
return "%d B" % s
|
|
|
|
if s < U*U:
|
|
|
|
return r(s/U, "k")
|
|
|
|
if s < U*U*U:
|
|
|
|
return r(s/(U*U), "M")
|
|
|
|
if s < U*U*U*U:
|
|
|
|
return r(s/(U*U*U), "G")
|
|
|
|
if s < U*U*U*U*U:
|
|
|
|
return r(s/(U*U*U*U), "T")
|
|
|
|
if s < U*U*U*U*U*U:
|
|
|
|
return r(s/(U*U*U*U*U), "P")
|
|
|
|
return r(s/(U*U*U*U*U*U), "E")
|
|
|
|
|
2015-11-24 21:20:58 +00:00
|
|
|
def print_event(event):
|
2015-12-04 04:15:29 +00:00
|
|
|
event_type, started, result, total_bytes, waiting_time, total_time = event
|
2015-11-24 21:20:58 +00:00
|
|
|
followthrough = None
|
|
|
|
if waiting_time and total_time:
|
|
|
|
followthrough = total_time - waiting_time
|
2016-01-14 22:30:08 +00:00
|
|
|
print("%17s: total=%7s wait=%7s ft=%7s size=%s (%s)" %
|
2015-12-04 04:15:29 +00:00
|
|
|
("%s-%s" % (event_type, result),
|
2015-11-24 21:20:58 +00:00
|
|
|
abbrev(total_time),
|
|
|
|
abbrev(waiting_time),
|
|
|
|
abbrev(followthrough),
|
2015-12-04 04:15:29 +00:00
|
|
|
abbreviate_space(total_bytes),
|
|
|
|
time.ctime(started),
|
2015-11-24 21:20:58 +00:00
|
|
|
))
|
|
|
|
|
2015-11-24 21:03:53 +00:00
|
|
|
def show_usage(args):
|
|
|
|
if not os.path.exists("relay.sqlite"):
|
|
|
|
raise UsageError("cannot find relay.sqlite, please run from the server directory")
|
2015-11-24 21:20:58 +00:00
|
|
|
oldest = None
|
|
|
|
newest = None
|
2015-12-04 04:15:29 +00:00
|
|
|
rendezvous_counters = defaultdict(int)
|
|
|
|
transit_counters = defaultdict(int)
|
|
|
|
total_transit_bytes = 0
|
2015-11-24 21:03:53 +00:00
|
|
|
db = get_db("relay.sqlite")
|
2015-12-04 04:15:29 +00:00
|
|
|
c = db.execute("SELECT * FROM `usage`"
|
2015-12-04 03:43:20 +00:00
|
|
|
" ORDER BY `started` ASC LIMIT ?",
|
2015-12-04 04:15:29 +00:00
|
|
|
(args.n,))
|
2015-11-24 21:03:53 +00:00
|
|
|
for row in c.fetchall():
|
2015-12-04 04:15:29 +00:00
|
|
|
if row["type"] == u"rendezvous":
|
|
|
|
counters = rendezvous_counters
|
|
|
|
elif row["type"] == u"transit":
|
|
|
|
counters = transit_counters
|
|
|
|
total_transit_bytes += row["total_bytes"]
|
|
|
|
else:
|
|
|
|
continue
|
2015-11-24 21:03:53 +00:00
|
|
|
counters["total"] += 1
|
|
|
|
counters[row["result"]] += 1
|
2015-11-24 21:20:58 +00:00
|
|
|
if oldest is None or row["started"] < oldest:
|
|
|
|
oldest = row["started"]
|
|
|
|
if newest is None or row["started"] > newest:
|
|
|
|
newest = row["started"]
|
2015-12-04 04:15:29 +00:00
|
|
|
event = (row["type"], row["started"], row["result"],
|
|
|
|
row["total_bytes"], row["waiting_time"], row["total_time"])
|
2015-11-24 21:20:58 +00:00
|
|
|
print_event(event)
|
2015-12-04 04:15:29 +00:00
|
|
|
if rendezvous_counters["total"] or transit_counters["total"]:
|
|
|
|
print("---")
|
2015-11-24 21:20:58 +00:00
|
|
|
print("(most recent started %s ago)" % abbrev(time.time() - newest))
|
2015-12-04 04:15:29 +00:00
|
|
|
if rendezvous_counters["total"]:
|
|
|
|
print("rendezvous events:")
|
|
|
|
counters = rendezvous_counters
|
|
|
|
elapsed = time.time() - oldest
|
|
|
|
total = counters["total"]
|
|
|
|
print(" %d events in %s (%.2f per hour)" % (total, abbrev(elapsed),
|
|
|
|
(3600 * total / elapsed)))
|
|
|
|
print("", ", ".join(["%s=%d (%d%%)" %
|
|
|
|
(k, counters[k], (100.0 * counters[k] / total))
|
|
|
|
for k in sorted(counters)
|
|
|
|
if k != "total"]))
|
|
|
|
if transit_counters["total"]:
|
|
|
|
print("transit events:")
|
|
|
|
counters = transit_counters
|
2015-11-24 21:20:58 +00:00
|
|
|
elapsed = time.time() - oldest
|
2015-12-04 04:15:29 +00:00
|
|
|
total = counters["total"]
|
|
|
|
print(" %d events in %s (%.2f per hour)" % (total, abbrev(elapsed),
|
|
|
|
(3600 * total / elapsed)))
|
|
|
|
rate = total_transit_bytes / elapsed
|
|
|
|
print(" %s total bytes, %sps" % (abbreviate_space(total_transit_bytes),
|
|
|
|
abbreviate_space(rate)))
|
|
|
|
print("", ", ".join(["%s=%d (%d%%)" %
|
|
|
|
(k, counters[k], (100.0 * counters[k] / total))
|
|
|
|
for k in sorted(counters)
|
|
|
|
if k != "total"]))
|
2015-11-24 21:03:53 +00:00
|
|
|
return 0
|
2015-11-24 21:20:58 +00:00
|
|
|
|
|
|
|
def tail_usage(args):
|
|
|
|
if not os.path.exists("relay.sqlite"):
|
|
|
|
raise UsageError("cannot find relay.sqlite, please run from the server directory")
|
|
|
|
db = get_db("relay.sqlite")
|
|
|
|
# we don't seem to have unique row IDs, so this is an inaccurate and
|
|
|
|
# inefficient hack
|
|
|
|
seen = set()
|
2015-12-04 04:15:29 +00:00
|
|
|
try:
|
|
|
|
while True:
|
|
|
|
old = time.time() - 2*60*60
|
|
|
|
c = db.execute("SELECT * FROM `usage`"
|
|
|
|
" WHERE `started` > ?"
|
|
|
|
" ORDER BY `started` ASC", (old,))
|
|
|
|
for row in c.fetchall():
|
|
|
|
event = (row["type"], row["started"], row["result"],
|
|
|
|
row["total_bytes"], row["waiting_time"],
|
|
|
|
row["total_time"])
|
|
|
|
if event not in seen:
|
|
|
|
print_event(event)
|
|
|
|
seen.add(event)
|
|
|
|
time.sleep(2)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
return 0
|
2015-11-24 21:20:58 +00:00
|
|
|
return 0
|