1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 EfficiOS Inc., Alexandre Montplaisir
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.module
;
12 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
13 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNullContents
;
14 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.nullToEmptyString
;
16 import java
.io
.BufferedReader
;
18 import java
.io
.IOException
;
19 import java
.io
.InputStreamReader
;
20 import java
.nio
.file
.Files
;
21 import java
.nio
.file
.Paths
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Arrays
;
24 import java
.util
.Collection
;
25 import java
.util
.Collections
;
26 import java
.util
.function
.Predicate
;
27 import java
.util
.List
;
29 import java
.util
.regex
.Pattern
;
30 import java
.util
.stream
.Collectors
;
31 import java
.util
.stream
.Stream
;
33 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
34 import org
.eclipse
.jdt
.annotation
.NonNull
;
35 import org
.eclipse
.jdt
.annotation
.Nullable
;
36 import org
.eclipse
.tracecompass
.internal
.analysis
.lami
.core
.Activator
;
37 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.LamiStrings
;
38 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.aspect
.LamiGenericAspect
;
39 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.aspect
.LamiIRQNameAspect
;
40 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.aspect
.LamiIRQNumberAspect
;
41 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.aspect
.LamiIRQTypeAspect
;
42 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.aspect
.LamiMixedAspect
;
43 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.aspect
.LamiProcessNameAspect
;
44 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.aspect
.LamiProcessPIDAspect
;
45 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.aspect
.LamiProcessTIDAspect
;
46 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.aspect
.LamiDurationAspect
;
47 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.aspect
.LamiEmptyAspect
;
48 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.aspect
.LamiTableEntryAspect
;
49 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.aspect
.LamiTimeRangeBeginAspect
;
50 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.aspect
.LamiTimeRangeDurationAspect
;
51 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.aspect
.LamiTimeRangeEndAspect
;
52 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.aspect
.LamiTimestampAspect
;
53 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.types
.LamiData
;
54 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.types
.LamiData
.DataType
;
55 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.core
.types
.LamiTimeRange
;
56 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.ondemand
.IOnDemandAnalysis
;
57 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.ondemand
.OnDemandAnalysisException
;
58 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
59 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
60 import org
.json
.JSONArray
;
61 import org
.json
.JSONException
;
62 import org
.json
.JSONObject
;
64 import com
.google
.common
.annotations
.VisibleForTesting
;
65 import com
.google
.common
.collect
.ImmutableList
;
66 import com
.google
.common
.collect
.ImmutableMap
;
67 import com
.google
.common
.collect
.ImmutableMultimap
;
68 import com
.google
.common
.collect
.Multimap
;
71 * Base class for analysis modules that call external scripts implementing the
74 * @author Alexandre Montplaisir
76 public class LamiAnalysis
implements IOnDemandAnalysis
{
78 /** Maximum major version of the LAMI protocol we support */
79 private static final int MAX_SUPPORTED_MAJOR_VERSION
= 1;
81 private static final String DOUBLE_QUOTES
= "\""; //$NON-NLS-1$
83 /* Flags passed to the analysis scripts */
84 private static final String METADATA_FLAG
= "--metadata"; //$NON-NLS-1$
85 private static final String PROGRESS_FLAG
= "--output-progress"; //$NON-NLS-1$
86 private static final String BEGIN_FLAG
= "--begin"; //$NON-NLS-1$
87 private static final String END_FLAG
= "--end"; //$NON-NLS-1$
89 /** Log message for commands being run */
90 private static final String RUNNING_MESSAGE
= "Running command:"; //$NON-NLS-1$
92 private final List
<String
> fScriptCommand
;
95 * The LAMI analysis is considered initialized after we have read the
96 * script's --metadata once. This will assign the fields below.
98 private boolean fInitialized
= false;
100 private boolean fIsAvailable
;
101 private final String fName
;
102 private final boolean fIsUserDefined
;
103 private final Predicate
<ITmfTrace
> fAppliesTo
;
105 /* Data defined by the analysis's metadata */
106 private @Nullable String fAnalysisTitle
;
107 private @Nullable Map
<String
, LamiTableClass
> fTableClasses
;
108 private boolean fUseProgressOutput
;
111 * Constructor. To be called by implementing classes.
114 * Name of this analysis
115 * @param isUserDefined
116 * {@code true} if this is a user-defined analysis
118 * Predicate to use to check whether or not this analysis applies
121 * Analysis arguments, including the executable name (first
124 public LamiAnalysis(String name
, boolean isUserDefined
, Predicate
<ITmfTrace
> appliesTo
,
126 fScriptCommand
= ImmutableList
.copyOf(args
);
128 fIsUserDefined
= isUserDefined
;
129 fAppliesTo
= appliesTo
;
133 * Map of pre-defined charts, for every table class names.
135 * If a table class is not in this map then it means that table has no
138 * @return The chart models, per table class names
140 protected Multimap
<String
, LamiChartModel
> getPredefinedCharts() {
141 return ImmutableMultimap
.of();
145 public final boolean appliesTo(ITmfTrace trace
) {
146 return fAppliesTo
.test(trace
);
150 public boolean canExecute(ITmfTrace trace
) {
156 * Perform initialization of the LAMI script. This means verifying that it
157 * is actually present on disk, and that it returns correct --metadata.
160 protected synchronized void initialize() {
165 /* Do the analysis's initialization */
167 /* Check if the script's expected executable is on the PATH */
168 String executable
= fScriptCommand
.get(0);
169 boolean exists
= Stream
.of(System
.getenv("PATH").split(checkNotNull(Pattern
.quote(File
.pathSeparator
)))) //$NON-NLS-1$
171 .anyMatch(path
-> Files
.exists(path
.resolve(executable
)));
173 /* Script is not found */
174 fIsAvailable
= false;
179 fIsAvailable
= checkMetadata();
184 * Verify that this script returns valid metadata.
186 * This will populate all remaining non-final fields of this class.
188 * @return If the metadata is valid or not
191 protected boolean checkMetadata() {
193 * The initialize() phase of the analysis will be used to check the
194 * script's metadata. Actual runs of the script will use the execute()
197 List
<String
> command
= ImmutableList
.<@NonNull String
> builder()
198 .addAll(fScriptCommand
).add(METADATA_FLAG
).build();
200 Activator
.instance().logInfo(RUNNING_MESSAGE
+ ' ' + command
.toString());
202 String output
= getOutputFromCommand(command
);
203 if (output
== null || output
.isEmpty()) {
209 * Metadata should look this this:
212 * "version": [1, 5, 2, "dev"],
213 * "title": "I/O latency statistics",
215 * "Julien Desfossez",
218 * "description": "Provides statistics about the latency involved in various I/O operations.",
219 * "url": "https://github.com/lttng/lttng-analyses",
227 * "syscall-latency": {
228 * "title": "System calls latency statistics",
229 * "column-descriptions": [
230 * {"title": "System call", "type": "syscall"},
231 * {"title": "Count", "type": "int", "unit": "operations"},
232 * {"title": "Minimum duration", "type": "duration"},
233 * {"title": "Average duration", "type": "duration"},
234 * {"title": "Maximum duration", "type": "duration"},
235 * {"title": "Standard deviation", "type": "duration"}
239 * "title": "Disk latency statistics",
240 * "column-descriptions": [
241 * {"title": "Disk name", "type": "disk"},
242 * {"title": "Count", "type": "int", "unit": "operations"},
243 * {"title": "Minimum duration", "type": "duration"},
244 * {"title": "Average duration", "type": "duration"},
245 * {"title": "Maximum duration", "type": "duration"},
246 * {"title": "Standard deviation", "type": "duration"}
255 JSONObject obj
= new JSONObject(output
);
256 fAnalysisTitle
= obj
.getString(LamiStrings
.TITLE
);
258 /* Very early scripts may not contain the "mi-version" */
259 JSONObject miVersion
= obj
.optJSONObject(LamiStrings
.MI_VERSION
);
260 if (miVersion
== null) {
261 /* Before version 0.1 */
262 fUseProgressOutput
= false;
264 int majorVersion
= miVersion
.getInt(LamiStrings
.MAJOR
);
265 if (majorVersion
<= MAX_SUPPORTED_MAJOR_VERSION
) {
266 fUseProgressOutput
= true;
268 /* Unknown version, we do not support it */
273 JSONObject tableClasses
= obj
.getJSONObject(LamiStrings
.TABLE_CLASSES
);
274 @NonNull String
[] tableClassNames
= checkNotNullContents(JSONObject
.getNames(tableClasses
));
276 ImmutableMap
.Builder
<String
, LamiTableClass
> tablesBuilder
= ImmutableMap
.builder();
277 for (String tableClassName
: tableClassNames
) {
278 JSONObject tableClass
= tableClasses
.getJSONObject(tableClassName
);
280 final String tableTitle
= checkNotNull(tableClass
.getString(LamiStrings
.TITLE
));
281 @NonNull JSONArray columnDescriptions
= checkNotNull(tableClass
.getJSONArray(LamiStrings
.COLUMN_DESCRIPTIONS
));
283 List
<LamiTableEntryAspect
> aspects
= getAspectsFromColumnDescriptions(columnDescriptions
);
284 Collection
<LamiChartModel
> chartModels
= getPredefinedCharts().get(tableClassName
);
286 tablesBuilder
.put(tableClassName
, new LamiTableClass(tableClassName
, tableTitle
, aspects
, chartModels
));
290 fTableClasses
= tablesBuilder
.build();
291 } catch (IllegalArgumentException e
) {
293 * This is thrown if there are duplicate keys in the map
296 throw new JSONException("Duplicate table class entry in " + fAnalysisTitle
); //$NON-NLS-1$
299 } catch (JSONException e
) {
300 /* Error in the parsing of the JSON, script is broken? */
301 Activator
.instance().logError(e
.getMessage());
307 private static List
<LamiTableEntryAspect
> getAspectsFromColumnDescriptions(JSONArray columnDescriptions
) throws JSONException
{
308 ImmutableList
.Builder
<LamiTableEntryAspect
> aspectsBuilder
= new ImmutableList
.Builder
<>();
309 for (int j
= 0; j
< columnDescriptions
.length(); j
++) {
310 JSONObject column
= columnDescriptions
.getJSONObject(j
);
311 DataType columnDataType
;
312 String columnClass
= column
.optString(LamiStrings
.CLASS
, null);
314 if (columnClass
== null) {
315 columnDataType
= DataType
.MIXED
;
317 columnDataType
= getDataTypeFromString(columnClass
);
320 String columnTitle
= column
.optString(LamiStrings
.TITLE
, null);
322 if (columnTitle
== null) {
323 columnTitle
= String
.format("%s #%d", columnDataType
.getTitle(), j
+ 1); //$NON-NLS-1$
326 final int colIndex
= j
;
327 switch (columnDataType
) {
330 * We will add 3 aspects, to represent the start, end and
331 * duration of this time range.
333 aspectsBuilder
.add(new LamiTimeRangeBeginAspect(columnTitle
, colIndex
));
334 aspectsBuilder
.add(new LamiTimeRangeEndAspect(columnTitle
, colIndex
));
335 aspectsBuilder
.add(new LamiTimeRangeDurationAspect(columnTitle
, colIndex
));
339 aspectsBuilder
.add(new LamiTimestampAspect(columnTitle
, colIndex
));
343 aspectsBuilder
.add(new LamiProcessNameAspect(columnTitle
, colIndex
));
344 aspectsBuilder
.add(new LamiProcessPIDAspect(columnTitle
, colIndex
));
345 aspectsBuilder
.add(new LamiProcessTIDAspect(columnTitle
, colIndex
));
349 aspectsBuilder
.add(new LamiIRQTypeAspect(columnTitle
, colIndex
));
350 aspectsBuilder
.add(new LamiIRQNameAspect(columnTitle
, colIndex
));
351 aspectsBuilder
.add(new LamiIRQNumberAspect(columnTitle
, colIndex
));
355 aspectsBuilder
.add(new LamiDurationAspect(columnTitle
, colIndex
));
359 aspectsBuilder
.add(new LamiMixedAspect(columnTitle
, colIndex
));
364 String units
= column
.optString(LamiStrings
.UNIT
, null);
367 units
= columnDataType
.getUnits();
370 /* We will add only one aspect representing the element */
371 LamiTableEntryAspect aspect
= new LamiGenericAspect(columnTitle
,
372 units
, colIndex
, columnDataType
.isContinuous(), false);
373 aspectsBuilder
.add(aspect
);
378 * SWT quirk : we need an empty column at the end or else the last data
379 * column will clamp to the right edge of the view if it is
382 aspectsBuilder
.add(LamiEmptyAspect
.INSTANCE
);
384 return aspectsBuilder
.build();
387 private static DataType
getDataTypeFromString(String value
) throws JSONException
{
389 return DataType
.fromString(value
);
390 } catch (IllegalArgumentException e
) {
391 throw new JSONException("Unrecognized data type: " + value
); //$NON-NLS-1$
396 * Get the title of this analysis, as read from the script's metadata.
398 * @return The analysis title. Should not be null after the initialization
399 * completed successfully.
401 public @Nullable String
getAnalysisTitle() {
402 return fAnalysisTitle
;
406 * Get the result table classes defined by this analysis, as read from the
409 * @return The analysis' result table classes. Should not be null after the
410 * execution completed successfully.
412 public @Nullable Map
<String
, LamiTableClass
> getTableClasses() {
413 return fTableClasses
;
417 * Print the full command that will be run when calling {@link #execute},
418 * with the exception of the 'extraParams' that will be passed to execute().
420 * This can be used to display the command in the UI before it is actually
424 * The trace on which to run the analysis
426 * The time range to specify. Null will not specify a time range,
427 * which means the whole trace will be taken.
428 * @return The command as a single, space-separated string
430 public String
getFullCommandAsString(ITmfTrace trace
, @Nullable TmfTimeRange range
) {
431 String tracePath
= checkNotNull(trace
.getPath());
433 ImmutableList
.Builder
<String
> builder
= getBaseCommand(range
);
435 * We can add double-quotes around the trace path, which could contain
436 * spaces, so that the resulting command can be easily copy-pasted into
439 builder
.add(DOUBLE_QUOTES
+ tracePath
+ DOUBLE_QUOTES
);
440 List
<String
> list
= builder
.build();
441 String ret
= list
.stream().collect(Collectors
.joining(" ")); //$NON-NLS-1$
442 return checkNotNull(ret
);
446 * Get the base part of the command that will be executed to run this
447 * analysis, supplying the given time range. Base part meaning:
450 * [script executable] [statically-defined parameters] [--begin/--end (if applicable)]
453 * Note that it does not include the path to the trace, that is to be added
457 * The time range that will be passed
458 * @return The elements of the command
460 private ImmutableList
.Builder
<String
> getBaseCommand(@Nullable TmfTimeRange range
) {
464 begin
= range
.getStartTime().getValue();
465 end
= range
.getEndTime().getValue();
468 ImmutableList
.Builder
<String
> builder
= ImmutableList
.builder();
469 builder
.addAll(fScriptCommand
);
471 if (fUseProgressOutput
) {
472 builder
.add(PROGRESS_FLAG
);
476 builder
.add(BEGIN_FLAG
).add(String
.valueOf(begin
));
477 builder
.add(END_FLAG
).add(String
.valueOf(end
));
483 * Call the currently defined LAMI script with the given arguments.
486 * The time range. Null for the whole trace.
488 * The progress monitor used to report progress
489 * @return The script's output, formatted into {@link LamiTableEntry}'s.
490 * @throws OnDemandAnalysisException
491 * If execution did not terminate normally
494 public List
<LamiResultTable
> execute(ITmfTrace trace
, @Nullable TmfTimeRange timeRange
,
495 String extraParams
, IProgressMonitor monitor
) throws OnDemandAnalysisException
{
496 /* Should have been called already, but in case it was not */
499 final @NonNull String tracePath
= checkNotNull(trace
.getPath());
500 final @NonNull String
[] splitParams
= extraParams
.trim().split(" "); //$NON-NLS-1$
502 ImmutableList
.Builder
<String
> builder
= getBaseCommand(timeRange
);
504 if (!extraParams
.trim().equals("")) { //$NON-NLS-1$
505 builder
.addAll(Arrays
.asList(splitParams
));
507 builder
.add(tracePath
);
508 List
<String
> command
= builder
.build();
510 Activator
.instance().logInfo(RUNNING_MESSAGE
+ ' ' + command
.toString());
511 String output
= getResultsFromCommand(command
, monitor
);
513 if (output
.isEmpty()) {
514 throw new OnDemandAnalysisException(Messages
.LamiAnalysis_NoResults
);
522 * "type": "time-range",
523 * "begin": 1444334398154194201,
524 * "end": 1444334425194487548
526 * "class": "syscall-latency",
529 * {"type": "syscall", "name": "open"},
531 * {"type": "duration", "value": 5562},
532 * {"type": "duration", "value": 13835},
533 * {"type": "duration", "value": 77683},
534 * {"type": "duration", "value": 15263}
537 * {"type": "syscall", "name": "read"},
539 * {"type": "duration", "value": 316},
540 * {"type": "duration", "value": 5774},
541 * {"type": "duration", "value": 62569},
542 * {"type": "duration", "value": 9277}
548 * "type": "time-range",
549 * "begin": 1444334425194487549,
550 * "end": 1444334425254887190
552 * "class": "syscall-latency",
555 * {"type": "syscall", "name": "open"},
557 * {"type": "duration", "value": 1578},
558 * {"type": "duration", "value": 16648},
559 * {"type": "duration", "value": 15444},
560 * {"type": "duration", "value": 68540}
563 * {"type": "syscall", "name": "read"},
565 * {"type": "duration", "value": 78},
566 * {"type": "duration", "value": 1948},
567 * {"type": "duration", "value": 11184},
568 * {"type": "duration", "value": 94670}
577 ImmutableList
.Builder
<LamiResultTable
> resultsBuilder
= new ImmutableList
.Builder
<>();
580 JSONObject obj
= new JSONObject(output
);
581 JSONArray results
= obj
.getJSONArray(LamiStrings
.RESULTS
);
583 if (results
.length() == 0) {
585 * No results were reported. This may be normal, but warn the
586 * user why a report won't be created.
588 throw new OnDemandAnalysisException(Messages
.LamiAnalysis_NoResults
);
591 for (int i
= 0; i
< results
.length(); i
++) {
592 JSONObject result
= results
.getJSONObject(i
);
594 /* Parse the time-range */
595 JSONObject trObject
= result
.getJSONObject(LamiStrings
.TIME_RANGE
);
596 long start
= trObject
.getLong(LamiStrings
.BEGIN
);
597 long end
= trObject
.getLong(LamiStrings
.END
);
598 LamiTimeRange tr
= new LamiTimeRange(start
, end
);
600 /* Parse the table's class */
601 LamiTableClass tableClass
;
602 JSONObject tableClassObject
= result
.optJSONObject(LamiStrings
.CLASS
);
603 if (tableClassObject
== null) {
605 * "class" is just a standard string, indicating we use a
606 * metadata-defined table class as-is
608 @NonNull String tableClassName
= checkNotNull(result
.getString(LamiStrings
.CLASS
));
609 tableClass
= getTableClassFromName(tableClassName
);
611 // FIXME Rest will become more generic eventually in the LAMI format.
612 } else if (tableClassObject
.has(LamiStrings
.INHERIT
)) {
614 * Dynamic title: We reuse an existing table class but
615 * override the title.
617 String baseTableName
= checkNotNull(tableClassObject
.getString(LamiStrings
.INHERIT
));
618 LamiTableClass baseTableClass
= getTableClassFromName(baseTableName
);
619 String newTitle
= checkNotNull(tableClassObject
.getString(LamiStrings
.TITLE
));
621 tableClass
= new LamiTableClass(baseTableClass
, newTitle
);
624 * Dynamic column descriptions: we implement a new table
627 String title
= checkNotNull(tableClassObject
.getString(LamiStrings
.TITLE
));
628 JSONArray columnDescriptions
= checkNotNull(tableClassObject
.getJSONArray(LamiStrings
.COLUMN_DESCRIPTIONS
));
629 List
<LamiTableEntryAspect
> aspects
= getAspectsFromColumnDescriptions(columnDescriptions
);
631 tableClass
= new LamiTableClass(nullToEmptyString(Messages
.LamiAnalysis_DefaultDynamicTableName
), title
, aspects
, Collections
.EMPTY_SET
);
634 /* Parse the "data", which is the array of rows */
635 JSONArray data
= result
.getJSONArray(LamiStrings
.DATA
);
636 ImmutableList
.Builder
<LamiTableEntry
> dataBuilder
= new ImmutableList
.Builder
<>();
638 for (int j
= 0; j
< data
.length(); j
++) {
639 /* A row is an array of cells */
640 JSONArray row
= data
.getJSONArray(j
);
641 ImmutableList
.Builder
<LamiData
> rowBuilder
= ImmutableList
.builder();
643 for (int k
= 0; k
< row
.length(); k
++) {
644 Object cellObject
= checkNotNull(row
.get(k
));
645 LamiData cellValue
= LamiData
.createFromObject(cellObject
);
646 rowBuilder
.add(cellValue
);
649 dataBuilder
.add(new LamiTableEntry(rowBuilder
.build()));
651 resultsBuilder
.add(new LamiResultTable(tr
, tableClass
, dataBuilder
.build()));
654 } catch (JSONException e
) {
655 throw new OnDemandAnalysisException(e
.getMessage());
658 return resultsBuilder
.build();
661 private LamiTableClass
getTableClassFromName(String tableClassName
) throws JSONException
{
662 Map
<String
, LamiTableClass
> map
= checkNotNull(fTableClasses
);
663 LamiTableClass tableClass
= map
.get(tableClassName
);
664 if (tableClass
== null) {
665 throw new JSONException("Table class " + tableClassName
+ //$NON-NLS-1$
666 " was not declared in the metadata"); //$NON-NLS-1$
672 * Get the output of an external command, used for getting the metadata.
673 * Cannot be cancelled, and will not report errors, simply returns null if
674 * the process ended abnormally.
677 * The parameters of the command, passed to
678 * {@link ProcessBuilder}
679 * @return The command output as a string
682 protected @Nullable String
getOutputFromCommand(List
<String
> command
) {
684 ProcessBuilder builder
= new ProcessBuilder(command
);
685 builder
.redirectErrorStream(true);
687 Process p
= builder
.start();
688 try (BufferedReader br
= new BufferedReader(new InputStreamReader(p
.getInputStream()));) {
689 int ret
= p
.waitFor();
690 String output
= br
.lines().collect(Collectors
.joining());
692 return (ret
== 0 ? output
: null);
694 } catch (IOException
| InterruptedException e
) {
700 * Get the results of invoking the specified command.
702 * The result should start with '{"results":...', as specified by the LAMI
703 * JSON protocol. The JSON itself may be split over multiple lines.
706 * The command to run (program and its arguments)
708 * The progress monitor
709 * @return The analysis results
710 * @throws OnDemandAnalysisException
711 * If the command ended abnormally, and normal results were not
715 protected String
getResultsFromCommand(List
<String
> command
, IProgressMonitor monitor
)
716 throws OnDemandAnalysisException
{
718 final int scale
= 1000;
719 double workedSoFar
= 0.0;
721 ProcessCanceller cancellerRunnable
= null;
722 Thread cancellerThread
= null;
725 monitor
.beginTask(Messages
.LamiAnalysis_MainTaskName
, scale
);
727 ProcessBuilder builder
= new ProcessBuilder(command
);
728 builder
.redirectErrorStream(false);
730 Process p
= checkNotNull(builder
.start());
732 cancellerRunnable
= new ProcessCanceller(p
, monitor
);
733 cancellerThread
= new Thread(cancellerRunnable
);
734 cancellerThread
.start();
736 List
<String
> results
= new ArrayList
<>();
738 try (BufferedReader in
= new BufferedReader(new InputStreamReader(p
.getInputStream()));) {
739 String line
= in
.readLine();
740 while (line
!= null && !line
.matches("\\s*\\{.*")) { //$NON-NLS-1$
742 * This is a line indicating progress, it has the form:
744 * 0.123 3000 of 5000 events processed
746 * The first part indicates the estimated fraction (out of
747 * 1.0) of work done. The second part is status text.
750 // Trim the line first to make sure the first character is
754 // Split at the first space
755 String
[] elems
= line
.split(" ", 2); //$NON-NLS-1$
757 if (elems
[0].matches("\\d.*")) { //$NON-NLS-1$
758 // It looks like we have a progress indication
760 // Try parsing the number
761 double cumulativeWork
= Double
.parseDouble(elems
[0]) * scale
;
762 double workedThisLoop
= cumulativeWork
- workedSoFar
;
764 // We're going backwards? Do not update the
766 if (workedThisLoop
> 0) {
767 monitor
.internalWorked(workedThisLoop
);
768 workedSoFar
= cumulativeWork
;
771 // There is a message: update the monitor's task name
772 if (elems
.length
>= 2) {
773 monitor
.setTaskName(elems
[1].trim());
775 } catch (NumberFormatException e
) {
776 // Continue reading progress lines anyway
780 line
= in
.readLine();
782 while (line
!= null) {
784 * We have seen the first line containing a '{', this is our
788 line
= in
.readLine();
791 int ret
= p
.waitFor();
793 if (monitor
.isCanceled()) {
794 /* We were interrupted by the canceller thread. */
795 throw new OnDemandAnalysisException(null);
800 * Something went wrong running the external script. We will
801 * gather the stderr and report it to the user.
803 BufferedReader br
= new BufferedReader(new InputStreamReader(p
.getErrorStream()));
804 String stdErrOutput
= br
.lines().collect(Collectors
.joining("\n")); //$NON-NLS-1$
805 throw new OnDemandAnalysisException(stdErrOutput
);
808 /* External script ended successfully, all is fine! */
809 String resultsStr
= results
.stream().collect(Collectors
.joining());
810 return checkNotNull(resultsStr
);
812 } catch (IOException
| InterruptedException e
) {
813 throw new OnDemandAnalysisException(Messages
.LamiAnalysis_ExecutionInterrupted
);
816 if (cancellerRunnable
!= null) {
817 cancellerRunnable
.setFinished();
819 if (cancellerThread
!= null) {
821 cancellerThread
.join();
822 } catch (InterruptedException e
) {
830 private static class ProcessCanceller
implements Runnable
{
832 private final Process fProcess
;
833 private final IProgressMonitor fMonitor
;
835 private boolean fIsFinished
= false;
837 public ProcessCanceller(Process process
, IProgressMonitor monitor
) {
842 public void setFinished() {
849 while (!fIsFinished
) {
851 if (fMonitor
.isCanceled()) {
856 } catch (InterruptedException e
) {
863 public @NonNull String
getName() {
868 public boolean isUserDefined() {
869 return fIsUserDefined
;