apache-ignite
161 строка · 4.9 Кб
1# Licensed to the Apache Software Foundation (ASF) under one or more
2# contributor license agreements. See the NOTICE file distributed with
3# this work for additional information regarding copyright ownership.
4# The ASF licenses this file to You under the Apache License, Version 2.0
5# (the "License"); you may not use this file except in compliance with
6# the License. You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16"""
17This module contains JVM utilities.
18"""
19
20from ignitetest.services.utils.decorators import memoize21
22DEFAULT_HEAP = "768M"23
24JVM_PARAMS_GC_G1 = "-XX:+UseG1GC -XX:MaxGCPauseMillis=100 " \25"-XX:ConcGCThreads=$(((`nproc`/3)>1?(`nproc`/3):1)) " \26"-XX:ParallelGCThreads=$(((`nproc`*3/4)>1?(`nproc`*3/4):1)) "27
28JVM_PARAMS_GENERIC = "-server -XX:+DisableExplicitGC -XX:+AlwaysPreTouch " \29"-XX:+ParallelRefProcEnabled -XX:+DoEscapeAnalysis " \30"-XX:+OptimizeStringConcat -XX:+UseStringDeduplication"31
32
33def create_jvm_settings(heap_size=DEFAULT_HEAP, gc_settings=JVM_PARAMS_GC_G1, generic_params=JVM_PARAMS_GENERIC,34gc_dump_path=None, oom_path=None, vm_error_path=None):35"""36Provides settings string for JVM process.
37param opts: JVM options to merge. Adds new or rewrites default values. Can be list or string.
38"""
39gc_dump = ""40if gc_dump_path:41gc_dump = "-verbose:gc -Xloggc:" + gc_dump_path42
43out_of_mem_dump = ""44if oom_path:45out_of_mem_dump = "-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=" + oom_path46
47vm_error_dump = ""48if vm_error_path:49vm_error_dump = "-XX:ErrorFile=" + vm_error_path50
51as_string = f"-Xmx{heap_size} -Xms{heap_size} {gc_settings} {gc_dump} " \52f"{out_of_mem_dump} {vm_error_dump} {generic_params}".strip()53
54return as_string.split()55
56
57def merge_jvm_settings(src_settings, additionals):58"""59Merges two JVM settings.
60:param src_settings: base settings. Can be list or string.
61:param additionals: params to add to or overwrite in src_settings. Can be list or string.
62:return merged JVM settings. By default as string.
63"""
64mapped = _to_map(src_settings)65
66mapped.update(_to_map(additionals))67
68_remove_duplicates(mapped)69
70listed = []71for param, value in mapped.items():72if value:73listed.append(f"{param}={value}")74else:75listed.append(param)76
77return listed78
79
80def java_major_version(version):81"""82:param version: Full java version
83:return: Java major version
84"""
85if version:86version = version.split('.')87
88return int(version[1]) if version[0] == '1' else int(version[0])89
90return -191
92
93def java_version(node):94"""95:param node: Ducktape cluster node
96:return: java version
97"""
98cmd = r"java -version 2>&1 | awk -F[\"\-] '/version/ {print $2}'"99
100raw_version = list(node.account.ssh_capture(cmd, allow_fail=False))101
102return raw_version[0].strip() if raw_version else ''103
104
105def _to_map(params):106""""""107assert isinstance(params, (str, list)), "JVM params an be string or list only."108
109if isinstance(params, str):110params = params.split()111
112mapped = {}113
114for elem in params:115param_val = elem.split(sep="=", maxsplit=1)116mapped[param_val[0]] = param_val[1] if len(param_val) > 1 else None117
118return mapped119
120
121def _remove_duplicates(params: dict):122"""Removes specific duplicates"""123duplicates = {"-Xmx": False, "-Xms": False, "-Xss": False, "-Xmn": False}124
125for param_key in reversed(list(params.keys())):126for dup_key, _ in duplicates.items():127if param_key.startswith(dup_key):128if duplicates[dup_key]:129del params[param_key]130else:131duplicates[dup_key] = True132
133
134class JvmProcessMixin:135"""136Mixin to work with JVM processes
137"""
138
139@staticmethod140def pids(node, java_class):141"""142Return pids of jvm processes running this java class on service node.
143:param node: Service node.
144:param java_class: Java class name
145:return: List of service's pids.
146"""
147cmd = "ps -C java -wwo pid,args | grep '%s' | awk -F' ' '{print $1}'" % java_class148
149return [int(pid) for pid in node.account.ssh_capture(cmd, allow_fail=True)]150
151
152class JvmVersionMixin:153"""154Mixin to get java version on node.
155"""
156@memoize157def java_version(self):158"""159:return: Full java version of service.
160"""
161return java_version(self.nodes[0])162