2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.context.metrics;
23 import java.io.BufferedReader;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.PrintWriter;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Map.Entry;
32 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
33 import org.onap.policy.apex.model.basicmodel.service.AbstractParameters;
34 import org.onap.policy.apex.model.basicmodel.service.ParameterService;
35 import org.slf4j.ext.XLogger;
36 import org.slf4j.ext.XLoggerFactory;
38 import com.google.gson.Gson;
41 * The Class ConcurrentContextMetricsJVMThread gets metrics for concurrent use of context.
43 * @author Liam Fallon (liam.fallon@ericsson.com)
45 public class ConcurrentContextMetricsJVMThread implements Runnable {
46 // Logger for this class
47 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextMetricsJVMThread.class);
49 private final String testType;
50 private final int jvm;
51 private final int threadCount;
52 private final int threadLoops;
53 private final int longArraySize;
54 private final int lockType;
56 private boolean readyToGo = false;
57 private boolean allFinished = false;
59 private PrintWriter processWriter;
64 * @param testType the test type
66 * @param threadCount the thread count
67 * @param threadLoops the thread loops
68 * @param longArraySize the long array size
69 * @param lockType the lock type
70 * @throws ApexException the apex exception
72 public ConcurrentContextMetricsJVMThread(final String testType, final int jvm, final int threadCount,
73 final int threadLoops, final int longArraySize, final int lockType) throws ApexException {
74 this.testType = testType;
76 this.threadCount = threadCount;
77 this.threadLoops = threadLoops;
78 this.longArraySize = longArraySize;
79 this.lockType = lockType;
85 * @see java.lang.Runnable#run()
89 final List<String> commandList = new ArrayList<>();
90 commandList.add(System.getProperty("java.home") + System.getProperty("file.separator") + "bin"
91 + System.getProperty("file.separator") + "java");
92 commandList.add("-cp");
93 commandList.add(System.getProperty("java.class.path"));
94 for (final Entry<Object, Object> property : System.getProperties().entrySet()) {
95 if (property.getKey().toString().startsWith("APEX")
96 || property.getKey().toString().equals("java.net.preferIPv4Stack")
97 || property.getKey().toString().equals("jgroups.bind_addr")) {
98 commandList.add("-D" + property.getKey().toString() + "=" + property.getValue().toString());
101 commandList.add("org.onap.policy.apex.plugins.context.metrics.ConcurrentContextMetricsJVM");
102 commandList.add(testType);
103 commandList.add(new Integer(jvm).toString());
104 commandList.add(new Integer(threadCount).toString());
105 commandList.add(new Integer(threadLoops).toString());
106 commandList.add(new Integer(longArraySize).toString());
107 commandList.add(new Integer(lockType).toString());
109 for (final Entry<Class<?>, AbstractParameters> parameterServiceEntry : ParameterService.getAll()) {
110 commandList.add(parameterServiceEntry.getKey().getCanonicalName());
111 commandList.add(new Gson().toJson(parameterServiceEntry.getValue()));
114 LOGGER.info("starting JVM " + jvm);
117 final ProcessBuilder processBuilder = new ProcessBuilder(commandList);
118 processBuilder.redirectErrorStream(true);
122 process = processBuilder.start();
124 final InputStream is = process.getInputStream();
125 processWriter = new PrintWriter(process.getOutputStream());
126 final InputStreamReader isr = new InputStreamReader(is);
127 final BufferedReader br = new BufferedReader(isr);
131 LOGGER.info("JVM Output for command " + commandList + "\n");
132 while ((line = br.readLine()) != null) {
135 if (line.trim().equals("ReadyToGo")) {
137 } else if (line.trim().equals("AllFinished")) {
142 // Wait to get exit value
144 final int exitValue = process.waitFor();
145 LOGGER.info("\n\nJVM " + jvm + " finished, exit value is " + exitValue);
146 } catch (final InterruptedException e) {
149 } catch (final IOException e1) {
150 e1.printStackTrace();
155 * Checks if is ready to go.
157 * @return true, if checks if is ready to go
159 public boolean isReadyToGo() {
164 * Checks if is all finished.
166 * @return true, if checks if is all finished
168 public boolean isAllFinished() {
175 public void offYouGo() {
176 processWriter.println("OffYouGo");
177 processWriter.flush();
183 public void finishItOut() {
184 processWriter.println("FinishItOut");
185 processWriter.flush();