blob: 6a73a7344b95c102ab36808ae36c36c69bf53bd1 [file] [log] [blame]
Enrico Granata3f1052b2012-03-13 21:52:00 +00001"""
2Objective-C runtime wrapper for use by LLDB Python formatters
3
4part of The LLVM Compiler Infrastructure
5This file is distributed under the University of Illinois Open Source
6License. See LICENSE.TXT for details.
7"""
Enrico Granataeb4a4792012-02-23 23:10:27 +00008import lldb
Kate Stoneb9c1b512016-09-06 20:57:50 +00009import time
10import datetime
Enrico Granata263ebe52012-05-18 19:55:37 +000011import inspect
12
Kate Stoneb9c1b512016-09-06 20:57:50 +000013
Enrico Granata263ebe52012-05-18 19:55:37 +000014class TimeMetrics:
Kate Stoneb9c1b512016-09-06 20:57:50 +000015
16 @staticmethod
17 def generate(label=None):
18 return TimeMetrics(label)
19
20 def __init__(self, lbl=None):
21 self.label = "" if lbl is None else lbl
22 pass
23
24 def __enter__(self):
25 caller = inspect.stack()[1]
26 self.function = str(caller)
27 self.enter_time = time.clock()
28
29 def __exit__(self, a, b, c):
30 self.exit_time = time.clock()
31 print("It took " + str(self.exit_time - self.enter_time) +
32 " time units to run through " + self.function + self.label)
33 return False
34
Enrico Granataeb4a4792012-02-23 23:10:27 +000035
36class Counter:
Kate Stoneb9c1b512016-09-06 20:57:50 +000037
38 def __init__(self):
39 self.count = 0
40 self.list = []
41
42 def update(self, name):
43 self.count = self.count + 1
44 # avoid getting the full dump of this ValueObject just to save its
45 # metrics
46 if isinstance(name, lldb.SBValue):
47 self.list.append(name.GetName())
48 else:
49 self.list.append(str(name))
50
51 def __str__(self):
52 return str(self.count) + " times, for items [" + str(self.list) + "]"
53
Enrico Granataeb4a4792012-02-23 23:10:27 +000054
55class MetricsPrinter_Verbose:
Kate Stoneb9c1b512016-09-06 20:57:50 +000056
57 def __init__(self, metrics):
58 self.metrics = metrics
59
60 def __str__(self):
61 string = ""
62 for key, value in self.metrics.metrics.items():
63 string = string + "metric " + str(key) + ": " + str(value) + "\n"
64 return string
65
Enrico Granataeb4a4792012-02-23 23:10:27 +000066
67class MetricsPrinter_Compact:
Kate Stoneb9c1b512016-09-06 20:57:50 +000068
69 def __init__(self, metrics):
70 self.metrics = metrics
71
72 def __str__(self):
73 string = ""
74 for key, value in self.metrics.metrics.items():
75 string = string + "metric " + \
76 str(key) + " was hit " + str(value.count) + " times\n"
77 return string
78
Enrico Granataeb4a4792012-02-23 23:10:27 +000079
80class Metrics:
Enrico Granataeb4a4792012-02-23 23:10:27 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 def __init__(self):
83 self.metrics = {}
Enrico Granataeb4a4792012-02-23 23:10:27 +000084
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 def add_metric(self, name):
86 self.metrics[name] = Counter()
Enrico Granataeb4a4792012-02-23 23:10:27 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 def metric_hit(self, metric, trigger):
89 self.metrics[metric].update(trigger)
Enrico Granataeb4a4792012-02-23 23:10:27 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 def __getitem__(self, key):
92 return self.metrics[key]
Enrico Granataeb4a4792012-02-23 23:10:27 +000093
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 def __getattr__(self, name):
95 if name == 'compact':
96 return MetricsPrinter_Compact(self)
97 if name == 'verbose':
98 return MetricsPrinter_Verbose(self)
99 raise AttributeError("%r object has no attribute %r" %
100 (type(self).__name__, name))
Enrico Granataeb4a4792012-02-23 23:10:27 +0000101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 def __str__(self):
103 return str(self.verbose)
104
105 def metric_success(self, metric):
106 total_count = 0
107 metric_count = self[metric].count
108 for key, value in self.metrics.items():
109 total_count = total_count + value.count
110 if total_count > 0:
111 return metric_count / float(total_count)
112 return 0