2015-09-22 13:59:19 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright 2015 the V8 project authors. All rights reserved.
|
|
|
|
# Use of this source code is governed by a BSD-style license that can be
|
|
|
|
# found in the LICENSE file.
|
|
|
|
|
|
|
|
"""This script is used to analyze GCTracer's NVP output."""
|
|
|
|
|
2015-09-23 13:52:35 +00:00
|
|
|
|
2019-02-19 08:28:26 +00:00
|
|
|
# for py2/py3 compatibility
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
|
2015-09-22 13:59:19 +00:00
|
|
|
from argparse import ArgumentParser
|
2015-09-23 13:52:35 +00:00
|
|
|
from copy import deepcopy
|
2015-09-22 13:59:19 +00:00
|
|
|
from gc_nvp_common import split_nvp
|
2019-02-19 08:28:26 +00:00
|
|
|
from math import ceil, log
|
2015-09-22 13:59:19 +00:00
|
|
|
from sys import stdin
|
|
|
|
|
2015-09-23 13:52:35 +00:00
|
|
|
|
2015-09-25 16:14:03 +00:00
|
|
|
class LinearBucket:
|
|
|
|
def __init__(self, granularity):
|
2015-09-22 13:59:19 +00:00
|
|
|
self.granularity = granularity
|
2015-09-25 16:14:03 +00:00
|
|
|
|
|
|
|
def value_to_bucket(self, value):
|
|
|
|
return int(value / self.granularity)
|
|
|
|
|
|
|
|
def bucket_to_range(self, bucket):
|
|
|
|
return (bucket * self.granularity, (bucket + 1) * self.granularity)
|
|
|
|
|
|
|
|
|
|
|
|
class Log2Bucket:
|
|
|
|
def __init__(self, start):
|
|
|
|
self.start = int(log(start, 2)) - 1
|
|
|
|
|
|
|
|
def value_to_bucket(self, value):
|
|
|
|
index = int(log(value, 2))
|
|
|
|
index -= self.start
|
|
|
|
if index < 0:
|
|
|
|
index = 0
|
|
|
|
return index
|
|
|
|
|
|
|
|
def bucket_to_range(self, bucket):
|
|
|
|
if bucket == 0:
|
|
|
|
return (0, 2 ** (self.start + 1))
|
|
|
|
bucket += self.start
|
|
|
|
return (2 ** bucket, 2 ** (bucket + 1))
|
|
|
|
|
|
|
|
|
|
|
|
class Histogram:
|
|
|
|
def __init__(self, bucket_trait, fill_empty):
|
2015-09-23 13:52:35 +00:00
|
|
|
self.histogram = {}
|
|
|
|
self.fill_empty = fill_empty
|
2015-09-25 16:14:03 +00:00
|
|
|
self.bucket_trait = bucket_trait
|
2015-09-23 13:52:35 +00:00
|
|
|
|
|
|
|
def add(self, key):
|
2015-09-25 16:14:03 +00:00
|
|
|
index = self.bucket_trait.value_to_bucket(key)
|
2015-09-23 13:52:35 +00:00
|
|
|
if index not in self.histogram:
|
|
|
|
self.histogram[index] = 0
|
|
|
|
self.histogram[index] += 1
|
2015-09-22 13:59:19 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
ret = []
|
2015-09-23 13:52:35 +00:00
|
|
|
keys = self.histogram.keys()
|
|
|
|
keys.sort()
|
2015-09-25 16:14:03 +00:00
|
|
|
last = keys[len(keys) - 1]
|
|
|
|
for i in range(0, last + 1):
|
|
|
|
(min_value, max_value) = self.bucket_trait.bucket_to_range(i)
|
|
|
|
if i == keys[0]:
|
|
|
|
keys.pop(0)
|
|
|
|
ret.append(" [{0},{1}[: {2}".format(
|
|
|
|
str(min_value), str(max_value), self.histogram[i]))
|
|
|
|
else:
|
|
|
|
if self.fill_empty:
|
2015-09-23 13:52:35 +00:00
|
|
|
ret.append(" [{0},{1}[: {2}".format(
|
2015-09-25 16:14:03 +00:00
|
|
|
str(min_value), str(max_value), 0))
|
2015-09-22 13:59:19 +00:00
|
|
|
return "\n".join(ret)
|
|
|
|
|
|
|
|
|
|
|
|
class Category:
|
2016-04-08 12:04:54 +00:00
|
|
|
def __init__(self, key, histogram, csv, percentiles):
|
2015-09-22 13:59:19 +00:00
|
|
|
self.key = key
|
|
|
|
self.values = []
|
|
|
|
self.histogram = histogram
|
2016-03-31 15:07:54 +00:00
|
|
|
self.csv = csv
|
2016-04-08 12:04:54 +00:00
|
|
|
self.percentiles = percentiles
|
2015-09-22 13:59:19 +00:00
|
|
|
|
|
|
|
def process_entry(self, entry):
|
|
|
|
if self.key in entry:
|
|
|
|
self.values.append(float(entry[self.key]))
|
2015-09-23 13:52:35 +00:00
|
|
|
if self.histogram:
|
|
|
|
self.histogram.add(float(entry[self.key]))
|
2015-09-22 13:59:19 +00:00
|
|
|
|
2015-10-15 11:37:30 +00:00
|
|
|
def min(self):
|
|
|
|
return min(self.values)
|
|
|
|
|
|
|
|
def max(self):
|
|
|
|
return max(self.values)
|
|
|
|
|
|
|
|
def avg(self):
|
2016-03-31 15:07:54 +00:00
|
|
|
if len(self.values) == 0:
|
|
|
|
return 0.0
|
2015-10-15 11:37:30 +00:00
|
|
|
return sum(self.values) / len(self.values)
|
|
|
|
|
2016-04-04 09:23:08 +00:00
|
|
|
def empty(self):
|
|
|
|
return len(self.values) == 0
|
|
|
|
|
2016-04-08 12:04:54 +00:00
|
|
|
def _compute_percentiles(self):
|
|
|
|
ret = []
|
|
|
|
if len(self.values) == 0:
|
|
|
|
return ret
|
|
|
|
sorted_values = sorted(self.values)
|
|
|
|
for percentile in self.percentiles:
|
|
|
|
index = int(ceil((len(self.values) - 1) * percentile / 100))
|
|
|
|
ret.append(" {0}%: {1}".format(percentile, sorted_values[index]))
|
|
|
|
return ret
|
|
|
|
|
2015-09-22 13:59:19 +00:00
|
|
|
def __str__(self):
|
2016-03-31 15:07:54 +00:00
|
|
|
if self.csv:
|
|
|
|
ret = [self.key]
|
|
|
|
ret.append(len(self.values))
|
|
|
|
ret.append(self.min())
|
|
|
|
ret.append(self.max())
|
|
|
|
ret.append(self.avg())
|
|
|
|
ret = [str(x) for x in ret]
|
|
|
|
return ",".join(ret)
|
|
|
|
else:
|
|
|
|
ret = [self.key]
|
|
|
|
ret.append(" len: {0}".format(len(self.values)))
|
|
|
|
if len(self.values) > 0:
|
|
|
|
ret.append(" min: {0}".format(self.min()))
|
|
|
|
ret.append(" max: {0}".format(self.max()))
|
|
|
|
ret.append(" avg: {0}".format(self.avg()))
|
|
|
|
if self.histogram:
|
|
|
|
ret.append(str(self.histogram))
|
2016-04-08 12:04:54 +00:00
|
|
|
if self.percentiles:
|
|
|
|
ret.append("\n".join(self._compute_percentiles()))
|
2016-03-31 15:07:54 +00:00
|
|
|
return "\n".join(ret)
|
2015-09-22 13:59:19 +00:00
|
|
|
|
2015-10-15 11:37:30 +00:00
|
|
|
def __repr__(self):
|
|
|
|
return "<Category: {0}>".format(self.key)
|
|
|
|
|
|
|
|
|
|
|
|
def make_key_func(cmp_metric):
|
|
|
|
def key_func(a):
|
|
|
|
return getattr(a, cmp_metric)()
|
|
|
|
return key_func
|
|
|
|
|
2015-09-22 13:59:19 +00:00
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = ArgumentParser(description="Process GCTracer's NVP output")
|
|
|
|
parser.add_argument('keys', metavar='KEY', type=str, nargs='+',
|
2015-09-25 16:14:03 +00:00
|
|
|
help='the keys of NVPs to process')
|
|
|
|
parser.add_argument('--histogram-type', metavar='<linear|log2>',
|
|
|
|
type=str, nargs='?', default="linear",
|
|
|
|
help='histogram type to use (default: linear)')
|
|
|
|
linear_group = parser.add_argument_group('linear histogram specific')
|
|
|
|
linear_group.add_argument('--linear-histogram-granularity',
|
|
|
|
metavar='GRANULARITY', type=int, nargs='?',
|
|
|
|
default=5,
|
|
|
|
help='histogram granularity (default: 5)')
|
|
|
|
log2_group = parser.add_argument_group('log2 histogram specific')
|
|
|
|
log2_group.add_argument('--log2-histogram-init-bucket', metavar='START',
|
|
|
|
type=int, nargs='?', default=64,
|
|
|
|
help='initial buck size (default: 64)')
|
|
|
|
parser.add_argument('--histogram-omit-empty-buckets',
|
|
|
|
dest='histogram_omit_empty',
|
|
|
|
action='store_true',
|
|
|
|
help='omit empty histogram buckets')
|
|
|
|
parser.add_argument('--no-histogram', dest='histogram',
|
|
|
|
action='store_false', help='do not print histogram')
|
2015-09-22 13:59:19 +00:00
|
|
|
parser.set_defaults(histogram=True)
|
2015-09-25 16:14:03 +00:00
|
|
|
parser.set_defaults(histogram_omit_empty=False)
|
2015-10-15 11:37:30 +00:00
|
|
|
parser.add_argument('--rank', metavar='<no|min|max|avg>',
|
|
|
|
type=str, nargs='?',
|
|
|
|
default="no",
|
|
|
|
help="rank keys by metric (default: no)")
|
2016-03-31 15:07:54 +00:00
|
|
|
parser.add_argument('--csv', dest='csv',
|
|
|
|
action='store_true', help='provide output as csv')
|
2016-04-08 12:04:54 +00:00
|
|
|
parser.add_argument('--percentiles', dest='percentiles',
|
|
|
|
type=str, default="",
|
|
|
|
help='comma separated list of percentiles')
|
2015-09-22 13:59:19 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2015-09-23 13:52:35 +00:00
|
|
|
histogram = None
|
|
|
|
if args.histogram:
|
2015-09-25 16:14:03 +00:00
|
|
|
bucket_trait = None
|
|
|
|
if args.histogram_type == "log2":
|
|
|
|
bucket_trait = Log2Bucket(args.log2_histogram_init_bucket)
|
|
|
|
else:
|
|
|
|
bucket_trait = LinearBucket(args.linear_histogram_granularity)
|
|
|
|
histogram = Histogram(bucket_trait, not args.histogram_omit_empty)
|
2015-09-23 13:52:35 +00:00
|
|
|
|
2016-04-08 12:04:54 +00:00
|
|
|
percentiles = []
|
|
|
|
for percentile in args.percentiles.split(','):
|
|
|
|
try:
|
|
|
|
percentiles.append(float(percentile))
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
categories = [ Category(key, deepcopy(histogram), args.csv, percentiles)
|
2015-09-22 13:59:19 +00:00
|
|
|
for key in args.keys ]
|
|
|
|
|
|
|
|
while True:
|
|
|
|
line = stdin.readline()
|
|
|
|
if not line:
|
|
|
|
break
|
|
|
|
obj = split_nvp(line)
|
|
|
|
for category in categories:
|
|
|
|
category.process_entry(obj)
|
|
|
|
|
2016-04-04 09:23:08 +00:00
|
|
|
# Filter out empty categories.
|
|
|
|
categories = [x for x in categories if not x.empty()]
|
|
|
|
|
2015-10-15 11:37:30 +00:00
|
|
|
if args.rank != "no":
|
|
|
|
categories = sorted(categories, key=make_key_func(args.rank), reverse=True)
|
|
|
|
|
2015-09-22 13:59:19 +00:00
|
|
|
for category in categories:
|
|
|
|
print(category)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|