64bc0cab6118236711a2f25e736d479ff33dcee5
[policy/apex-pdp.git] /
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.plugins.context.metrics;
22
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;
31
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;
37
38 import com.google.gson.Gson;
39
40 /**
41  * The Class ConcurrentContextMetricsJVMThread gets metrics for concurrent use of context.
42  *
43  * @author Liam Fallon (liam.fallon@ericsson.com)
44  */
45 public class ConcurrentContextMetricsJVMThread implements Runnable {
46     // Logger for this class
47     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ConcurrentContextMetricsJVMThread.class);
48
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;
55
56     private boolean readyToGo = false;
57     private boolean allFinished = false;
58
59     private PrintWriter processWriter;
60
61     /**
62      * The Constructor.
63      *
64      * @param testType the test type
65      * @param jvm the jvm
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
71      */
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;
75         this.jvm = jvm;
76         this.threadCount = threadCount;
77         this.threadLoops = threadLoops;
78         this.longArraySize = longArraySize;
79         this.lockType = lockType;
80     }
81
82     /*
83      * (non-Javadoc)
84      *
85      * @see java.lang.Runnable#run()
86      */
87     @Override
88     public void 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());
99             }
100         }
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());
108
109         for (final Entry<Class<?>, AbstractParameters> parameterServiceEntry : ParameterService.getAll()) {
110             commandList.add(parameterServiceEntry.getKey().getCanonicalName());
111             commandList.add(new Gson().toJson(parameterServiceEntry.getValue()));
112         }
113
114         LOGGER.info("starting JVM " + jvm);
115
116         // Run the JVM
117         final ProcessBuilder processBuilder = new ProcessBuilder(commandList);
118         processBuilder.redirectErrorStream(true);
119         Process process;
120
121         try {
122             process = processBuilder.start();
123
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);
128
129             String line;
130
131             LOGGER.info("JVM Output for command " + commandList + "\n");
132             while ((line = br.readLine()) != null) {
133                 LOGGER.info(line);
134
135                 if (line.trim().equals("ReadyToGo")) {
136                     readyToGo = true;
137                 } else if (line.trim().equals("AllFinished")) {
138                     allFinished = true;
139                 }
140             }
141
142             // Wait to get exit value
143             try {
144                 final int exitValue = process.waitFor();
145                 LOGGER.info("\n\nJVM " + jvm + " finished, exit value is " + exitValue);
146             } catch (final InterruptedException e) {
147                 e.printStackTrace();
148             }
149         } catch (final IOException e1) {
150             e1.printStackTrace();
151         }
152     }
153
154     /**
155      * Checks if is ready to go.
156      *
157      * @return true, if checks if is ready to go
158      */
159     public boolean isReadyToGo() {
160         return readyToGo;
161     }
162
163     /**
164      * Checks if is all finished.
165      *
166      * @return true, if checks if is all finished
167      */
168     public boolean isAllFinished() {
169         return allFinished;
170     }
171
172     /**
173      * Off you go.
174      */
175     public void offYouGo() {
176         processWriter.println("OffYouGo");
177         processWriter.flush();
178     }
179
180     /**
181      * Finish it out.
182      */
183     public void finishItOut() {
184         processWriter.println("FinishItOut");
185         processWriter.flush();
186     }
187 }