48eacc52852e0ce797ac8c906732626d9333addb
1 package org
.lttng
.ust
.agent
.utils
;
3 import java
.io
.IOException
;
4 import java
.lang
.ProcessBuilder
.Redirect
;
5 import java
.nio
.file
.Files
;
6 import java
.nio
.file
.Path
;
7 import java
.util
.Arrays
;
9 import java
.util
.stream
.Collectors
;
11 public final class LttngSessionControl
{
13 private LttngSessionControl() {}
19 private final String flag
;
21 private Domain(String flag
) {
25 public String
flag() {
30 // ------------------------------------------------------------------------
31 // Public utility methods
32 // ------------------------------------------------------------------------
35 * Setup a LTTng session by enabling certain events (or none).
38 * The name of the session to create. May be null to use the
39 * default one from lttng-tools.
42 * @param enabledEvents
43 * The list of events to enable. May be null or empty, to not
45 * @return If the command executed successfully (return code = 0).
47 public static boolean setupSession(String sessionName
, Domain domain
, String
... enabledEvents
) {
48 String
[] createCommand
= (sessionName
== null ?
49 new String
[] { "lttng", "create" } :
50 new String
[] { "lttng", "create", sessionName
}
53 String eventsToEnable
= (enabledEvents
== null || enabledEvents
.length
== 0 ?
55 * We have to enable a channel for 'lttng start' to work.
56 * However, we cannot enable a channel directly, see
57 * https://bugs.lttng.org/issues/894 . Instead we will enable an
58 * event we know does not exist
61 Arrays
.stream(enabledEvents
).collect(Collectors
.joining(","))
64 return executeCommands(new String
[][] {
66 { "lttng", "enable-event", domain
.flag(), eventsToEnable
},
72 * Setup a LTTng session with all events enabled (lttng enable-event -a).
75 * The name of the session to create. May be null to use the
76 * default one from lttng-tools.
79 * @return If the command executed successfully (return code = 0).
81 public static boolean setupSessionAllEvents(String sessionName
, Domain domain
) {
82 String
[] createCommand
= (sessionName
== null ?
83 new String
[] { "lttng", "create" } :
84 new String
[] { "lttng", "create", sessionName
}
87 return executeCommands(new String
[][] {
89 { "lttng", "enable-event", domain
.flag(), "-a" },
95 * Stop the current tracing session
97 * @return If the command executed successfully (return code = 0).
99 public static boolean stopSession() {
100 return executeCommand(new String
[] { "lttng", "stop" });
103 public static List
<String
> viewSession() {
104 return getOutputFromCommand(new String
[] { "lttng", "view" });
108 * Destroy the current tracing session
110 * @return If the command executed successfully (return code = 0).
112 public static boolean destroySession() {
113 return executeCommand(new String
[] { "lttng", "destroy" });
116 // ------------------------------------------------------------------------
117 // Private helper methods
118 // ------------------------------------------------------------------------
120 private static boolean executeCommands(String
[][] commands
) {
121 for (String
[] command
: commands
) {
122 if (executeCommand(command
) == false) {
130 * Just to test the environment / stdout are working correctly
132 public static void main(String
[] args
) {
133 executeCommand(new String
[] {"ls", "-l"});
136 private static boolean executeCommand(String
[] command
) {
138 ProcessBuilder builder
= new ProcessBuilder(command
);
139 builder
.redirectErrorStream(true);
140 builder
.redirectOutput(Redirect
.INHERIT
);
142 Process p
= builder
.start();
143 int ret
= p
.waitFor();
146 } catch (IOException
| InterruptedException e
) {
151 private static List
<String
> getOutputFromCommand(String
[] command
) {
153 Path tempFile
= Files
.createTempFile("test-output", null);
155 ProcessBuilder builder
= new ProcessBuilder(command
);
156 builder
.redirectErrorStream(true);
157 builder
.redirectOutput(Redirect
.to(tempFile
.toFile()));
159 Process p
= builder
.start();
162 List
<String
> lines
= Files
.readAllLines(tempFile
);
163 Files
.delete(tempFile
);
165 /* Also print the output to the console */
166 lines
.stream().forEach(s
-> System
.out
.println(s
));
170 } catch (IOException
| InterruptedException e
) {
This page took 0.039057 seconds and 3 git commands to generate.