blob: 4e8b328753389e7793619f4339baa6caf9c451dc [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
9
10class Counter:
11 def __init__(self):
12 self.count = 0
13 self.list = []
14 def update(self,name):
15 self.count = self.count + 1
Enrico Granata332b0b92012-03-09 19:04:53 +000016 # avoid getting the full dump of this ValueObject just to save its metrics
17 if isinstance(name,lldb.SBValue):
18 self.list.append(name.GetName())
19 else:
20 self.list.append(str(name))
Enrico Granataeb4a4792012-02-23 23:10:27 +000021 def __str__(self):
22 return str(self.count) + " times, for items [" + str(self.list) + "]"
23
24class MetricsPrinter_Verbose:
25 def __init__(self,metrics):
26 self.metrics = metrics
27 def __str__(self):
28 string = ""
29 for key,value in self.metrics.metrics.items():
30 string = string + "metric " + str(key) + ": " + str(value) + "\n"
31 return string
32
33class MetricsPrinter_Compact:
34 def __init__(self,metrics):
35 self.metrics = metrics
36 def __str__(self):
37 string = ""
38 for key,value in self.metrics.metrics.items():
39 string = string + "metric " + str(key) + " was hit " + str(value.count) + " times\n"
40 return string
41
42class Metrics:
43 def __init__(self):
44 self.metrics = {}
45
46 def add_metric(self,name):
47 self.metrics[name] = Counter()
48
49 def metric_hit(self,metric,trigger):
50 self.metrics[metric].update(trigger)
51
52 def __getitem__(self,key):
53 return self.metrics[key]
54
55 def __getattr__(self,name):
56 if name == 'compact':
57 return MetricsPrinter_Compact(self)
58 if name == 'verbose':
59 return MetricsPrinter_Verbose(self)
60 raise AttributeError("%r object has no attribute %r" %
61 (type(self).__name__, name))
62
63 def __str__(self):
64 return str(self.verbose)
65
66 def metric_success(self,metric):
67 total_count = 0
68 metric_count = self[metric].count
69 for key,value in self.metrics.items():
70 total_count = total_count + value.count
71 if total_count > 0:
72 return metric_count / float(total_count)
73 return 0