~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/pmu-events/metric_test.py

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 #!/usr/bin/env python3
  2 # SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
  3 import unittest
  4 from metric import Constant
  5 from metric import Event
  6 from metric import Expression
  7 from metric import ParsePerfJson
  8 from metric import RewriteMetricsInTermsOfOthers
  9 
 10 
 11 class TestMetricExpressions(unittest.TestCase):
 12 
 13   def test_Operators(self):
 14     a = Event('a')
 15     b = Event('b')
 16     self.assertEqual((a | b).ToPerfJson(), 'a | b')
 17     self.assertEqual((a ^ b).ToPerfJson(), 'a ^ b')
 18     self.assertEqual((a & b).ToPerfJson(), 'a & b')
 19     self.assertEqual((a < b).ToPerfJson(), 'a < b')
 20     self.assertEqual((a > b).ToPerfJson(), 'a > b')
 21     self.assertEqual((a + b).ToPerfJson(), 'a + b')
 22     self.assertEqual((a - b).ToPerfJson(), 'a - b')
 23     self.assertEqual((a * b).ToPerfJson(), 'a * b')
 24     self.assertEqual((a / b).ToPerfJson(), 'a / b')
 25     self.assertEqual((a % b).ToPerfJson(), 'a % b')
 26     one = Constant(1)
 27     self.assertEqual((a + one).ToPerfJson(), 'a + 1')
 28 
 29   def test_Brackets(self):
 30     a = Event('a')
 31     b = Event('b')
 32     c = Event('c')
 33     self.assertEqual((a * b + c).ToPerfJson(), 'a * b + c')
 34     self.assertEqual((a + b * c).ToPerfJson(), 'a + b * c')
 35     self.assertEqual(((a + a) + a).ToPerfJson(), 'a + a + a')
 36     self.assertEqual(((a + b) * c).ToPerfJson(), '(a + b) * c')
 37     self.assertEqual((a + (b * c)).ToPerfJson(), 'a + b * c')
 38     self.assertEqual(((a / b) * c).ToPerfJson(), 'a / b * c')
 39     self.assertEqual((a / (b * c)).ToPerfJson(), 'a / (b * c)')
 40 
 41   def test_ParsePerfJson(self):
 42     # Based on an example of a real metric.
 43     before = '(a + b + c + d) / (2 * e)'
 44     after = before
 45     self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
 46 
 47     # Parsing should handle events with '-' in their name. Note, in
 48     # the json file the '\' are doubled to '\\'.
 49     before = r'topdown\-fe\-bound / topdown\-slots - 1'
 50     after = before
 51     self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
 52 
 53     # Parsing should handle escaped modifiers. Note, in the json file
 54     # the '\' are doubled to '\\'.
 55     before = r'arb@event\=0x81\,umask\=0x1@ + arb@event\=0x84\,umask\=0x1@'
 56     after = before
 57     self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
 58 
 59     # Parsing should handle exponents in numbers.
 60     before = r'a + 1e12 + b'
 61     after = before
 62     self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
 63 
 64   def test_IfElseTests(self):
 65     # if-else needs rewriting to Select and back.
 66     before = r'Event1 if #smt_on else Event2'
 67     after = f'({before})'
 68     self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
 69 
 70     before = r'Event1 if 0 else Event2'
 71     after = f'({before})'
 72     self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
 73 
 74     before = r'Event1 if 1 else Event2'
 75     after = f'({before})'
 76     self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
 77 
 78     # Ensure the select is evaluate last.
 79     before = r'Event1 + 1 if Event2 < 2 else Event3 + 3'
 80     after = (r'Select(Event(r"Event1") + Constant(1), Event(r"Event2") < '
 81              r'Constant(2), Event(r"Event3") + Constant(3))')
 82     self.assertEqual(ParsePerfJson(before).ToPython(), after)
 83 
 84     before = r'Event1 > 1 if Event2 < 2 else Event3 > 3'
 85     after = (r'Select(Event(r"Event1") > Constant(1), Event(r"Event2") < '
 86              r'Constant(2), Event(r"Event3") > Constant(3))')
 87     self.assertEqual(ParsePerfJson(before).ToPython(), after)
 88 
 89     before = r'min(a + b if c > 1 else c + d, e + f)'
 90     after = r'min((a + b if c > 1 else c + d), e + f)'
 91     self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
 92 
 93     before = r'a if b else c if d else e'
 94     after = r'(a if b else (c if d else e))'
 95     self.assertEqual(ParsePerfJson(before).ToPerfJson(), after)
 96 
 97   def test_ToPython(self):
 98     # pylint: disable=eval-used
 99     # Based on an example of a real metric.
100     before = '(a + b + c + d) / (2 * e)'
101     py = ParsePerfJson(before).ToPython()
102     after = eval(py).ToPerfJson()
103     self.assertEqual(before, after)
104 
105   def test_Simplify(self):
106     before = '1 + 2 + 3'
107     after = '6'
108     self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
109 
110     before = 'a + 0'
111     after = 'a'
112     self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
113 
114     before = '0 + a'
115     after = 'a'
116     self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
117 
118     before = 'a | 0'
119     after = 'a'
120     self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
121 
122     before = '0 | a'
123     after = 'a'
124     self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
125 
126     before = 'a * 0'
127     after = '0'
128     self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
129 
130     before = '0 * a'
131     after = '0'
132     self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
133 
134     before = 'a * 1'
135     after = 'a'
136     self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
137 
138     before = '1 * a'
139     after = 'a'
140     self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
141 
142     before = 'a if 0 else b'
143     after = 'b'
144     self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
145 
146     before = 'a if 1 else b'
147     after = 'a'
148     self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
149 
150     before = 'a if b else a'
151     after = 'a'
152     self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
153 
154     # Pattern used to add a slots event to metrics that require it.
155     before = '0 * SLOTS'
156     after = '0 * SLOTS'
157     self.assertEqual(ParsePerfJson(before).Simplify().ToPerfJson(), after)
158 
159   def test_RewriteMetricsInTermsOfOthers(self):
160     Expression.__eq__ = lambda e1, e2: e1.Equals(e2)
161     before = [('cpu', 'm1', ParsePerfJson('a + b + c + d')),
162               ('cpu', 'm2', ParsePerfJson('a + b + c'))]
163     after = {('cpu', 'm1'): ParsePerfJson('m2 + d')}
164     self.assertEqual(RewriteMetricsInTermsOfOthers(before), after)
165     Expression.__eq__ = None
166 
167 if __name__ == '__main__':
168   unittest.main()

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php