forked from graalvm/mx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mx_benchmark.py
1172 lines (934 loc) · 41.4 KB
/
mx_benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# ----------------------------------------------------------------------------------------------------
#
# Copyright (c) 2016, 2016, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#
# ----------------------------------------------------------------------------------------------------
import json
import re
import socket
import time
import traceback
import uuid
from argparse import ArgumentParser
import os.path
import mx
_bm_suite_java_vms = {}
_bm_suites = {}
_benchmark_executor = None
class BenchmarkSuite(object):
"""
A harness for a benchmark suite.
A suite needs to be registered with mx_benchmarks.add_bm_suite.
"""
def name(self):
"""Returns the name of the suite.
:return: Name of the suite.
:rtype: str
"""
raise NotImplementedError()
def group(self):
"""The group that this benchmark suite belongs to, for example, `Graal`.
This is the name of the overall group of closely related projects.
:return: Name of the group.
:rtype: str
"""
raise NotImplementedError()
def subgroup(self):
"""The subgroup that this suite belongs to, e.g., `fastr` or `graal-compiler`.
This is the name of the subteam project within the group.
:return: Name of the subgroup.
:rtype: str
"""
raise NotImplementedError()
def benchmarkList(self, bmSuiteArgs):
"""Returns the list of the benchmarks provided by this suite.
:param list bmSuiteArgs: List of string arguments to the suite.
:return: List of benchmark string names.
:rtype: list
"""
# TODO: Remove old-style benchmarks after updating downstream suites.
return self.benchmarks()
def benchmarks(self):
# Deprecated, consider using `benchmarkList` instead!
raise NotImplementedError()
def validateEnvironment(self):
"""Validates the environment and raises exceptions if validation fails.
Can be overridden to check for existence of required environment variables
before the benchmark suite executed.
"""
pass
def vmArgs(self, bmSuiteArgs):
"""Extracts the VM flags from the list of arguments passed to the suite.
:param list bmSuiteArgs: List of string arguments to the suite.
:return: A list of string flags that are VM flags.
:rtype: list
"""
raise NotImplementedError()
def runArgs(self, bmSuiteArgs):
"""Extracts the run flags from the list of arguments passed to the suite.
:param list bmSuiteArgs: List of string arguments to the suite.
:return: A list of string flags that are arguments for the suite.
:rtype: list
"""
raise NotImplementedError()
def before(self, bmSuiteArgs):
"""Called exactly once before any benchmark invocations begin.
Useful for outputting information such as platform version, OS, etc.
Arguments: see `run`.
"""
pass
def after(self, bmSuiteArgs):
"""Called exactly once after all benchmark invocations are done.
Useful for cleaning up after the benchmarks.
Arguments: see `run`.
"""
pass
def run(self, benchmarks, bmSuiteArgs):
"""Runs the specified benchmarks with the given arguments.
More precisely, if `benchmarks` is a list, runs the list of the benchmarks from
the suite in one run (typically, one VM invocations). If `benchmarks` is None,
then it runs all the benchmarks from the suite.
.. note:: A benchmark suite may not support running multiple benchmarks,
or None, but it must at least run with a single benchmark in the
`benchmarks` list.
:param benchmarks: List of benchmark string names, or a None.
:type benchmarks: list or None
:param list bmSuiteArgs: List of string arguments to the suite.
:return:
List of measurement result dictionaries, each corresponding to a datapoint.
A measurement result is an object that can be converted into JSON and is
merged with the other dimensions of the data point.
A measurement result must contain a field `metric`, which has the following
values:
- `metric.name` -- name of the metric (e.g. `"time"`, `"memory"`, ...)
- `metric.value` -- value of the measurement (e.g. `1.54`)
- `metric.unit` -- a string that specified the unit of measurement
(e.g. `"ms"`)
- `metric.score-function` -- name of the scoring function for the result
(e.g. `id`, which just returns the measurement value)
- `metric.score-value` -- score of the value, evaluated by the specified
scoring function (e.g. `"1.54"`)
- `metric.type` -- a string denoting the type of the metric
(e.g. `"numeric"`)
- `metric.better` -- `"higher"` if higher is better, `"lower"` otherwise
- `metric.iteration` -- iteration number of the measurement (e.g. `0`)
:rtype: object
"""
raise NotImplementedError()
def add_bm_suite(suite, mxsuite=None):
if mxsuite is None:
mxsuite = mx.currently_loading_suite.get()
if suite.name() in _bm_suites:
raise RuntimeError("Benchmark suite '{0}' already exists.".format(suite.name()))
setattr(suite, ".mxsuite", mxsuite)
_bm_suites[suite.name()] = suite
class Rule(object):
# the maximum size of a string field
max_string_field_length = 255
@staticmethod
def crop_front(prefix=""):
"""Returns a function that truncates a string at the start."""
assert len(prefix) < Rule.max_string_field_length
def _crop(path):
if len(path) < Rule.max_string_field_length:
return str(path)
return str(prefix + path[-(Rule.max_string_field_length-len(prefix)):])
return _crop
@staticmethod
def crop_back(suffix=""):
"""Returns a function that truncates a string at the end."""
assert len(suffix) < Rule.max_string_field_length
def _crop(path):
if len(path) < Rule.max_string_field_length:
return str(path)
return str(path[:Rule.max_string_field_length-len(suffix)] + suffix)
return _crop
def parse(self, text):
"""Create a dictionary of variables for every measurment.
:param text: The standard output of the benchmark.
:type text: str
:return: Iterable of dictionaries with the matched variables.
:rtype: iterable
"""
raise NotImplementedError()
class BaseRule(Rule):
"""A rule parses a raw result and a prepares a structured measurement using a replacement
template.
A replacement template is a dictionary that describes how to create a measurement:
{
"benchmark": ("<benchmark>", str),
"metric.name": "time",
"metric.value": ("<value>", int),
"metric.unit": "ms",
"metric.score-function": "id",
"metric.type": "numeric",
"metric.better": "lower",
"metric.iteration": ("$iteration", id),
}
When `instantiate` is called, the tuples in the template shown above are
replaced with the corresponding named groups from the parsing pattern, and converted
to the specified type.
Tuples can contain one of the following special variables, prefixed with a `$` sign:
- `iteration` -- ordinal number of the match that produced the datapoint, among
all the matches for that parsing rule.
"""
def __init__(self, replacement):
self.replacement = replacement
def parseResults(self, text):
"""Parses the raw result of a benchmark and create a dictionary of variables
for every measurment.
:param text: The standard output of the benchmark.
:type text: str
:return: Iterable of dictionaries with the matched variables.
:rtype: iterable
"""
raise NotImplementedError()
def parse(self, text):
datapoints = []
capturepat = re.compile(r"<([a-zA-Z_][0-9a-zA-Z_]*)>")
varpat = re.compile(r"\$([a-zA-Z_][0-9a-zA-Z_]*)")
for iteration, m in enumerate(self.parseResults(text)):
datapoint = {}
for key, value in self.replacement.iteritems():
inst = value
if isinstance(inst, tuple):
v, vtype = inst
# Instantiate with named captured groups.
def var(name):
if name == "iteration":
return str(iteration)
else:
raise RuntimeError("Unknown var {0}".format(name))
v = varpat.sub(lambda vm: var(vm.group(1)), v)
v = capturepat.sub(lambda vm: m[vm.group(1)], v)
# Convert to a different type.
if vtype is str:
inst = str(v)
elif vtype is int:
inst = int(v)
elif vtype is float:
inst = float(v)
elif vtype is bool:
inst = bool(v)
elif hasattr(vtype, '__call__'):
inst = vtype(v)
else:
raise RuntimeError("Cannot handle type {0}".format(vtype))
if type(inst) not in [str, int, float, bool]:
raise RuntimeError("Object has unknown type: {0}".format(inst))
datapoint[key] = inst
datapoints.append(datapoint)
return datapoints
class StdOutRule(BaseRule):
"""Each rule contains a parsing pattern and a replacement template.
A parsing pattern is a regex that may contain any number of named groups,
as shown in the example:
r"===== DaCapo (?P<benchmark>[a-z]+) PASSED in (?P<value>[0-9]+) msec ====="
The above parsing regex captures the benchmark name into a variable `benchmark`
and the elapsed number of milliseconds into a variable called `value`.
"""
def __init__(self, pattern, replacement):
super(StdOutRule, self).__init__(replacement)
self.pattern = pattern
def parseResults(self, text):
return (m.groupdict() for m in re.finditer(self.pattern, text, re.MULTILINE))
class CSVBaseRule(BaseRule):
"""Parses a CSV file and creates a measurement result using the replacement."""
def __init__(self, colnames, replacement, filter_fn=None, **kwargs):
"""
:param colnames: list of column names of the CSV file. These names are used to
instantiate the replacement template.
:type colnames: list
:param filter_fn: function for filtering and transforming raw results
:type filter_fn: function
"""
super(CSVBaseRule, self).__init__(replacement)
self.colnames = colnames
self.kwargs = kwargs
self.filter_fn = filter_fn if filter_fn else self.filterResult
def filterResult(self, r):
"""Filters and transforms a raw result
:return: Dictionary of variables or None if the result should be omitted.
:rtype: dict or None
"""
return r
def getCSVFiles(self, text):
"""Get the CSV files which should be parsed.
:param text: The standard output of the benchmark.
:type text: str
:return: List of file names
:rtype: list
"""
raise NotImplementedError()
def parseResults(self, text):
import csv
l = []
files = self.getCSVFiles(text)
for filename in files:
with open(filename, 'rb') as csvfile:
csvReader = csv.DictReader(csvfile, fieldnames=self.colnames, **self.kwargs)
l = l + [r for r in (self.filter_fn(x) for x in csvReader) if r]
return l
class CSVFixedFileRule(CSVBaseRule):
"""CSV rule that parses a file with a predefined name."""
def __init__(self, filename, *args, **kwargs):
super(CSVFixedFileRule, self).__init__(*args, **kwargs)
self.filename = filename
def getCSVFiles(self, text):
return [self.filename]
class CSVStdOutFileRule(CSVBaseRule):
"""CSV rule that looks for CSV file names in the output of the benchmark."""
def __init__(self, pattern, match_name, *args, **kwargs):
super(CSVStdOutFileRule, self).__init__(*args, **kwargs)
self.pattern = pattern
self.match_name = match_name
def getCSVFiles(self, text):
return (m.groupdict()[self.match_name] for m in re.finditer(self.pattern, text, re.MULTILINE))
class JMHJsonRule(Rule):
"""Parses a JSON file produced by JMH and creates a measurement result."""
extra_jmh_keys = [
"mode",
"threads",
"forks",
"warmupIterations",
"warmupTime",
"warmupBatchSize",
"measurementIterations",
"measurementTime",
"measurementBatchSize",
]
def __init__(self, filename, suiteName):
self.filename = filename
self.suiteName = suiteName
def shortenPackageName(self, benchmark):
"""
Returns an abbreviated name for the benchmark.
Example: com.example.benchmark.Bench -> c.e.b.Bench
The full name is stored in the `extra.jmh.benchmark` property.
"""
s = benchmark.split(".")
# class and method
clazz = s[-2:]
package = [str(x[0]) for x in s[:-2]]
return ".".join(package + clazz)
def benchSuiteName(self):
return self.suiteName
def getExtraJmhKeys(self):
return JMHJsonRule.extra_jmh_keys
def parse(self, text):
r = []
with open(self.filename) as fp:
for result in json.load(fp):
benchmark = result["benchmark"]
mode = result["mode"]
pm = result["primaryMetric"]
unit = pm["scoreUnit"]
unit_parts = unit.split("/")
if mode == "thrpt":
# Throughput, ops/time
metricName = "throughput"
better = "higher"
if len(unit_parts) == 2:
metricUnit = "op/" + unit_parts[1]
else:
metricUnit = unit
elif mode in ["avgt", "sample", "ss"]:
# Average time, Sampling time, Single shot invocation time
metricName = "time"
better = "lower"
if len(unit_parts) == 2:
metricUnit = unit_parts[0]
else:
metricUnit = unit
else:
raise RuntimeError("Unknown benchmark mode {0}".format(mode))
d = {
"bench-suite" : self.benchSuiteName(),
"benchmark" : self.shortenPackageName(benchmark),
"metric.name": metricName,
"metric.unit": metricUnit,
"metric.score-function": "id",
"metric.better": better,
"metric.type": "numeric",
# full name
"extra.jmh.benchmark" : benchmark,
}
if "params" in result:
# add all parameter as a single string
d["extra.jmh.params"] = ", ".join(["=".join(kv) for kv in result["params"].iteritems()])
# and also the individual values
for k, v in result["params"].iteritems():
d["extra.jmh.param." + k] = str(v)
for k in self.getExtraJmhKeys():
if k in result:
d["extra.jmh." + k] = str(result[k])
for jmhFork, rawData in enumerate(pm["rawData"]):
for iteration, data in enumerate(rawData):
d2 = d.copy()
d2.update({
"metric.value": float(data),
"metric.iteration": int(iteration),
"extra.jmh.fork": str(jmhFork),
})
r.append(d2)
return r
class StdOutBenchmarkSuite(BenchmarkSuite):
"""Convenience suite for benchmarks that need to parse standard output.
The standard output benchmark proceeds in the following steps:
1. Run the benchmark.
2. Terminate if there was a non-zero exit code.
3. Terminate if one of the specified failure patterns was matched.
4. Terminate if none of the specified success patterns was matched.
5. Use the parse rules on the standard output to create data points.
"""
def run(self, benchmarks, bmSuiteArgs):
runretval = self.runAndReturnStdOut(benchmarks, bmSuiteArgs)
if len(runretval) == 3:
retcode, out, dims = runretval
return self.validateStdoutWithDimensions(
out, benchmarks, bmSuiteArgs, retcode=retcode, dims=dims)
else:
# TODO: Remove old-style validateStdOut after updating downstream suites.
retcode, out = runretval
return self.validateStdout(out, benchmarks, bmSuiteArgs, retcode=retcode)
# TODO: Remove once all the downstream suites become up-to-date.
def validateStdout(self, out, benchmarks, bmSuiteArgs, retcode=None):
return self.validateStdoutWithDimensions(
out, benchmarks, bmSuiteArgs, retcode=retcode, dims={})
def validateStdoutWithDimensions(
self, out, benchmarks, bmSuiteArgs, retcode=None, dims=None, *args, **kwargs):
"""Validate out against the parse rules and create data points.
The dimensions from the `dims` dict are added to each datapoint parsed from the
standard output.
Subclass may override to customize validation.
"""
if dims is None:
dims = {}
def compiled(pat):
if type(pat) is str:
return re.compile(pat)
return pat
flaky = False
for pat in self.flakySuccessPatterns():
if compiled(pat).search(out):
flaky = True
if not flaky:
if retcode:
if not self.validateReturnCode(retcode):
raise RuntimeError(
"Benchmark failed, exit code: {0}".format(retcode))
for pat in self.failurePatterns():
if compiled(pat).search(out):
raise RuntimeError("Benchmark failed, failure pattern found. Benchmark(s): {0}".format(benchmarks))
success = False
for pat in self.successPatterns():
if compiled(pat).search(out):
success = True
if not success:
raise RuntimeError("Benchmark failed, success pattern not found. Benchmark(s): {0}".format(benchmarks))
datapoints = []
for rule in self.rules(out, benchmarks, bmSuiteArgs):
parsedpoints = rule.parse(out)
for datapoint in parsedpoints:
datapoint.update(dims)
datapoints.extend(parsedpoints)
return datapoints
def validateReturnCode(self, retcode):
return retcode is 0
def flakySuccessPatterns(self):
"""List of regex pattern that can override matched failure and success patterns.
If any of the patterns in this list match, the output will not be checked for
failure or success patterns.
If none of the patterns in this list match, the output is checked normally.
This method should be overridden for suites that are known to be flaky.
"""
return []
def failurePatterns(self):
"""List of regex patterns which fail the benchmark when matched."""
return []
def successPatterns(self):
"""List of regex patterns which fail the benchmark if not matched."""
return []
def rules(self, output, benchmarks, bmSuiteArgs):
"""Returns a list of rules required to parse the standard output.
:param string output: Contents of the standard output.
:param list benchmarks: List of benchmarks that were run.
:param list bmSuiteArgs: Arguments to the benchmark suite (after first `--`).
:return: List of StdOutRule parse rules.
:rtype: list
"""
raise NotImplementedError()
def runAndReturnStdOut(self, benchmarks, bmSuiteArgs):
"""Runs the benchmarks and returns a string containing standard output.
See arguments `run`.
:return: The return code, and a standard output string
:rtype: tuple
"""
raise NotImplementedError()
class JavaBenchmarkSuite(StdOutBenchmarkSuite): #pylint: disable=R0922
"""Convenience suite used for benchmarks running on the JDK.
This suite relies on the `--jvm-config` flag to specify which JVM must be used to
run. The suite comes with methods `jvmConfig`, `vmArgs` and `runArgs` that know how
to extract the `--jvm-config` and the VM-and-run arguments to the benchmark suite
(see the `benchmark` method for more information).
"""
def createCommandLineArgs(self, benchmarks, bmSuiteArgs):
"""Creates a list of arguments for the JVM using the suite arguments.
:param list benchmarks: List of benchmarks from the suite to execute.
:param list bmSuiteArgs: Arguments passed to the suite.
:return: A list of command-line arguments.
:rtype: list
"""
raise NotImplementedError()
def workingDirectory(self, benchmarks, bmSuiteArgs):
"""Returns the desired working directory for running the benchmark.
By default it returns `None`, meaning that the working directory is not be
changed. It is meant to be overridden in subclasses when necessary.
"""
return None
def vmAndRunArgs(self, bmSuiteArgs):
return splitArgs(bmSuiteArgs, "--")
def splitJvmConfigArg(self, bmSuiteArgs):
parser = ArgumentParser()
parser.add_argument("--jvm", default=None,
help="JVM to run the benchmark with, for example 'server' or 'client'.")
parser.add_argument("--jvm-config", default=None,
help="JVM configuration for the selected JVM, for example 'graal-core'.")
args, remainder = parser.parse_known_args(self.vmAndRunArgs(bmSuiteArgs)[0])
return args.jvm, args.jvm_config, remainder
def jvm(self, bmSuiteArgs):
"""Returns the value of the `--jvm` argument or `None` if not present."""
return self.splitJvmConfigArg(bmSuiteArgs)[0]
def jvmConfig(self, bmSuiteArgs):
"""Returns the value of the `--jvm-config` argument or `None` if not present."""
return self.splitJvmConfigArg(bmSuiteArgs)[1]
def vmArgs(self, bmSuiteArgs):
"""Returns the VM arguments for the benchmark."""
return self.splitJvmConfigArg(bmSuiteArgs)[2]
def runArgs(self, bmSuiteArgs):
"""Returns the run arguments for the benchmark."""
return self.vmAndRunArgs(bmSuiteArgs)[1]
def getJavaVm(self, bmSuiteArgs):
jvm = self.jvm(bmSuiteArgs)
jvmConfig = self.jvmConfig(bmSuiteArgs)
if jvm is None:
if mx.get_opts().vm is not None:
mx.log("Defaulting --jvm to the deprecated --vm value. Please use --jvm.")
jvm = mx.get_opts().vm
else:
mx.log("Defaulting the JVM to 'server'.")
jvm = "server"
if jvmConfig is None:
mx.log("Defaulting --jvm-config to 'default'. Consider adding --jvm-config.")
jvmConfig = "default"
return get_java_vm(jvm, jvmConfig)
def before(self, bmSuiteArgs):
with mx.DisableJavaDebuggging():
self.getJavaVm(bmSuiteArgs).run(".", ["-version"])
def runAndReturnStdOut(self, benchmarks, bmSuiteArgs):
jvm = self.getJavaVm(bmSuiteArgs)
cwd = self.workingDirectory(benchmarks, bmSuiteArgs)
args = self.createCommandLineArgs(benchmarks, bmSuiteArgs)
if args is None:
return 0, "", {}
return jvm.run(cwd, args)
class JavaVm(object): #pylint: disable=R0922
"""Base class for objects that can run Java VMs."""
def name(self):
"""Returns the unique name of the Java VM (e.g. server, client, or jvmci)."""
raise NotImplementedError()
def config_name(self):
"""Returns the config name for a VM (e.g. graal-core or graal-enterprise)."""
raise NotImplementedError()
def run(self, cwd, args):
"""Runs the JVM with the specified args.
Returns an exit code, a capture of the standard output, and a dictionary of
extra dimensions to incorporate into the datapoints.
:param str cwd: Current working directory.
:param list args: List of command-line arguments for the VM.
:return: A tuple with an exit-code, stdout, and a dict with extra dimensions.
:rtype: tuple
"""
raise NotImplementedError()
class OutputCapturingJavaVm(JavaVm): #pylint: disable=R0921
"""A convenience class for running Java VMs."""
def post_process_command_line_args(self, suiteArgs):
"""Adapts command-line arguments to run the specific JVMCI VM."""
raise NotImplementedError()
def dimensions(self, cwd, args, code, out):
"""Returns a list of additional dimensions to put into every datapoint."""
raise NotImplementedError()
def run_java(self, args, out=None, err=None, cwd=None, nonZeroIsFatal=False):
"""Runs JVM with the specified arguments stdout and stderr, and working dir."""
raise NotImplementedError()
def run(self, cwd, args):
out = mx.TeeOutputCapture(mx.OutputCapture())
args = self.post_process_command_line_args(args)
mx.log("Running JVM with args: {0}".format(args))
code = self.run_java(args, out=out, err=out, cwd=cwd, nonZeroIsFatal=False)
out = out.underlying.data
dims = self.dimensions(cwd, args, code, out)
return code, out, dims
class DefaultJavaVm(OutputCapturingJavaVm):
def __init__(self, raw_name, raw_config_name):
self.raw_name = raw_name
self.raw_config_name = raw_config_name
def name(self):
return self.raw_name
def config_name(self):
return self.raw_config_name
def post_process_command_line_args(self, args):
return args
def dimensions(self, cwd, args, code, out):
return {
"host-vm": self.name(),
"host-vm-config": self.config_name(),
}
def run_java(self, args, out=None, err=None, cwd=None, nonZeroIsFatal=False):
mx.get_jdk().run_java(args, out=out, err=out, cwd=cwd, nonZeroIsFatal=False)
class DummyJavaVm(OutputCapturingJavaVm):
"""
Dummy VM to work around: "pylint #111138 disabling R0921 does'nt work"
https://www.logilab.org/ticket/111138
Note that the warning R0921 (abstract-class-little-used) has been removed
from pylint 1.4.3.
"""
pass
def add_java_vm(javavm):
key = (javavm.name(), javavm.config_name())
if key in _bm_suite_java_vms:
raise RuntimeError("Java VM and config '{0}' already exist.".format(key))
_bm_suite_java_vms[key] = javavm
def get_java_vm(vm_name, jvmconfig):
key = (vm_name, jvmconfig)
if not key in _bm_suite_java_vms:
raise RuntimeError("Java VM and config '{0}' do not exist.".format(key))
return _bm_suite_java_vms[key]
class TestBenchmarkSuite(JavaBenchmarkSuite):
"""Example suite used for testing and as a subclassing template.
"""
def name(self):
return "test"
def validateReturnCode(self, retcode):
return True
def createCommandLineArgs(self, benchmarks, bmSuiteArgs):
return bmSuiteArgs
def benchmarks(self):
return ["simple-bench", "complex-bench"]
def rules(self, out, benchmarks, bmSuiteArgs):
return [
StdOutRule(r"-d(?P<flag>[0-9]+)\s+use a (?P<bitnum>[0-9]+)-bit data model", {
"input": ("<flag>", int),
"metric.value": ("<bitnum>", int),
}),
]
class JMHBenchmarkSuiteBase(JavaBenchmarkSuite):
"""Base class for JMH based benchmark suites."""
jmh_result_file = "jmh_result.json"
def extraRunArgs(self):
return ["-rff", JMHBenchmarkSuiteBase.jmh_result_file, "-rf", "json"]
def extraVmArgs(self):
return []
def getJMHEntry(self):
raise NotImplementedError()
def createCommandLineArgs(self, benchmarks, bmSuiteArgs):
if benchmarks is None:
benchmarks = []
vmArgs = self.vmArgs(bmSuiteArgs) + self.extraVmArgs()
runArgs = self.extraRunArgs() + self.runArgs(bmSuiteArgs)
return vmArgs + self.getJMHEntry() + ['--jvmArgsPrepend', ' '.join(vmArgs)] + runArgs + benchmarks
def benchmarkList(self, bmSuiteArgs):
benchmarks = None
jvm = self.getJavaVm(bmSuiteArgs)
cwd = self.workingDirectory(benchmarks, bmSuiteArgs)
args = self.createCommandLineArgs(benchmarks, bmSuiteArgs)
_, out, _ = jvm.run(cwd, args + ["-l"])
benchs = out.splitlines()
assert benchs[0].startswith("Benchmarks:")
return benchs[1:]
def successPatterns(self):
return [
re.compile(
r"# Run complete.",
re.MULTILINE)
]
def benchSuiteName(self):
return self.name()
def failurePatterns(self):
return [re.compile(r"<failure>")]
def flakySuccessPatterns(self):
return []
def rules(self, out, benchmarks, bmSuiteArgs):
return [JMHJsonRule(JMHBenchmarkSuiteBase.jmh_result_file, self.benchSuiteName())]
class JMHRunnerBenchmarkSuite(JMHBenchmarkSuiteBase):
"""JMH benchmark suite that uses jmh-runner to execute projects with JMH benchmarks."""
def extraVmArgs(self):
# find all projects with a direct JMH dependency
jmhProjects = []
for p in mx.projects_opt_limit_to_suites():
if 'JMH' in [x.name for x in p.deps]:
jmhProjects.append(p)
cp = mx.classpath([p.name for p in jmhProjects], jdk=mx.get_jdk())
return ['-cp', cp]
def getJMHEntry(self):
return ["org.openjdk.jmh.Main"]
class JMHJarBenchmarkSuite(JMHBenchmarkSuiteBase):
"""
JMH benchmark suite that executes microbenchmarks in a JMH jar.
This suite relies on the `--jmh-jar` and `--jmh-name` to be set. The former
specifies the path to the JMH jar files. The later is the name suffix that is use
for the bench-suite property.
"""
def benchSuiteName(self):
return "jmh-" + self.jmhName()
def vmArgs(self, bmSuiteArgs):
vmArgs = super(JMHJarBenchmarkSuite, self).vmArgs(bmSuiteArgs)
parser = ArgumentParser(add_help=False)
parser.add_argument("--jmh-jar", default=None)
parser.add_argument("--jmh-name", default=None)
args, remaining = parser.parse_known_args(vmArgs)
self.jmh_jar = args.jmh_jar
self.jmh_name = args.jmh_name
return remaining
def getJMHEntry(self):
return ["-jar", self.jmhJAR()]
def jmhName(self):
if self.jmh_name is None:
mx.abort("Please use the --jmh-name benchmark suite argument to set the name of the JMH suite.")
return self.jmh_name
def jmhJAR(self):
if self.jmh_jar is None:
mx.abort("Please use the --jmh-jar benchmark suite argument to set the JMH jar file.")
jmh_jar = os.path.expanduser(self.jmh_jar)
if not os.path.exists(jmh_jar):
mx.abort("The --jmh-jar argument points to a non-existing file: " + jmh_jar)
return jmh_jar
class JMHRunnerMxBenchmarkSuite(JMHRunnerBenchmarkSuite):
def name(self):
return "jmh-mx"
def group(self):
return "Graal"
def subgroup(self):
return "mx"
class BenchmarkExecutor(object):
def uid(self):
return str(uuid.uuid1())
def group(self, suite):
return suite.group()
def buildFlags(self):
return ""
def machineArch(self):
return mx.get_arch()
def machineCpuCores(self):
return mx.cpu_count()
def machineHostname(self):
return socket.gethostname()
def machineName(self, mxBenchmarkArgs):
if mxBenchmarkArgs.machine_name:
return mxBenchmarkArgs.machine_name
return mx.get_env("MACHINE_NAME", default="")
def machineOs(self):
return mx.get_os()
def machineCpuClock(self):
return -1
def machineCpuFamily(self):
return "unknown"
def machineRam(self):
return -1
def branch(self):
mxsuite = mx.primary_suite()
name = mxsuite.vc and mxsuite.vc.active_branch(mxsuite.dir, abortOnError=False) or "<unknown>"
return name
def buildUrl(self):
return mx.get_env("BUILD_URL", default="")
def buildNumber(self):
build_num = mx.get_env("BUILD_NUMBER", default="")
if not build_num:
return -1
return int(build_num)
def checkEnvironmentVars(self):
pass
def dimensions(self, suite, mxBenchmarkArgs, bmSuiteArgs):
standard = {
"metric.uuid": self.uid(),
"group": self.group(suite),
"subgroup": suite.subgroup(),
"bench-suite": suite.name(),
"config.vm-flags": " ".join(suite.vmArgs(bmSuiteArgs)),
"config.run-flags": " ".join(suite.runArgs(bmSuiteArgs)),
"config.build-flags": self.buildFlags(),
"machine.name": self.machineName(mxBenchmarkArgs),
"machine.hostname": self.machineHostname(),
"machine.arch": self.machineArch(),
"machine.cpu-cores": self.machineCpuCores(),
"machine.cpu-clock": self.machineCpuClock(),
"machine.cpu-family": self.machineCpuFamily(),
"machine.ram": self.machineRam(),
"branch": self.branch(),
"build.url": self.buildUrl(),
"build.number": self.buildNumber(),
}