Turn LttngSession(Control) into a non-static class
[lttng-ust-java-tests.git] / src / org / lttng / ust / agent / utils / LttngSession.java
1 package org.lttng.ust.agent.utils;
2
3 import java.io.IOException;
4 import java.lang.ProcessBuilder.Redirect;
5 import java.nio.file.FileVisitResult;
6 import java.nio.file.Files;
7 import java.nio.file.Path;
8 import java.nio.file.Paths;
9 import java.nio.file.SimpleFileVisitor;
10 import java.nio.file.attribute.BasicFileAttributes;
11 import java.util.Arrays;
12 import java.util.List;
13 import java.util.UUID;
14 import java.util.stream.Collectors;
15
16 public class LttngSession implements AutoCloseable {
17
18 public enum Domain {
19 JUL("-j"),
20 LOG4J("-l");
21
22 private final String flag;
23
24 private Domain(String flag) {
25 this.flag = flag;
26 }
27
28 public String flag() {
29 return flag;
30 }
31 }
32
33 private final String sessionName;
34 private final Domain domain;
35
36 private volatile boolean channelCreated = false;
37
38 public LttngSession(String sessionName, Domain domain) {
39 if (sessionName != null) {
40 this.sessionName = sessionName;
41 } else {
42 this.sessionName = UUID.randomUUID().toString();
43 }
44 this.domain = domain;
45
46 /* Create the session in LTTng */
47 executeCommand(Arrays.asList("lttng", "create", this.sessionName));
48 }
49
50 @Override
51 public void close() {
52 /* Destroy the session */
53 executeCommand(Arrays.asList("lttng", "destroy", sessionName));
54 // FIXME also delete the trace we generated ?
55 }
56
57 // ------------------------------------------------------------------------
58 // Public methods
59 // ------------------------------------------------------------------------
60
61 /**
62 * Enable all events in the given session (enable-event -a)
63 *
64 * @return If the command executed successfully (return code = 0).
65 */
66 public boolean enableAllEvents() {
67 channelCreated = true;
68 return executeCommand(Arrays.asList(
69 "lttng", "enable-event", domain.flag(), "-a", "-s", sessionName));
70 }
71
72 /**
73 * Enable individual event(s).
74 *
75 * @param enabledEvents
76 * The list of events to enable. Should not be null or empty
77 * @return If the command executed successfully (return code = 0).
78 */
79 public boolean enableEvents(String... enabledEvents) {
80 if (enabledEvents == null || enabledEvents.length == 0) {
81 throw new IllegalArgumentException();
82 }
83 channelCreated = true;
84 return executeCommand(Arrays.asList(
85 "lttng", "enable-event", domain.flag(),
86 Arrays.stream(enabledEvents).collect(Collectors.joining(",")),
87 "-s", sessionName));
88 }
89
90 /**
91 * Send a disable-event command. Used to disable events that were previously
92 * enabled.
93 *
94 * @param disabledEvents
95 * The list of disabled events. Should not be null or empty
96 * @return If the command executed successfully (return code = 0).
97 */
98 public boolean disableEvents(String... disabledEvents) {
99 if (disabledEvents == null || disabledEvents.length == 0) {
100 throw new IllegalArgumentException();
101 }
102 return executeCommand(Arrays.asList(
103 "lttng", "disable-event", domain.flag(),
104 Arrays.stream(disabledEvents).collect(Collectors.joining(",")),
105 "-s", sessionName));
106 }
107
108 public boolean start() {
109 /*
110 * We have to enable a channel for 'lttng start' to work. However, we
111 * cannot enable a channel directly, see
112 * https://bugs.lttng.org/issues/894 . Instead we will enable an event
113 * we know does not exist
114 */
115 if (!channelCreated) {
116 enableEvents("non-event");
117 }
118 return executeCommand(Arrays.asList("lttng", "start", sessionName));
119 }
120
121 /**
122 * Stop the tracing session
123 *
124 * @return If the command executed successfully (return code = 0).
125 */
126 public boolean stop() {
127 return executeCommand(Arrays.asList("lttng", "stop", sessionName));
128 }
129
130 /**
131 * Issue a "lttng view" command on the session, and returns its output. This
132 * effectively returns the current content of the trace in text form.
133 *
134 * @return The output of Babeltrace on the session's current trace
135 */
136 public List<String> view() {
137 return TestUtils.getOutputFromCommand(Arrays.asList("lttng", "view", sessionName));
138 }
139
140 /**
141 * Outside of the scope of lttng-tools, but this utility method can be used
142 * to delete all traces currently under ~/lttng-traces/. This can be used by
143 * tests to cleanup a trace they have created.
144 *
145 * @return True if the command completes successfully, false if there was an
146 * error.
147 */
148 public static boolean deleteAllTracee() {
149 String tracesDir = new String(System.getProperty("user.home") + "/lttng-traces/");
150 return deleteDirectory(Paths.get(tracesDir));
151 }
152
153 // ------------------------------------------------------------------------
154 // Private helper methods
155 // ------------------------------------------------------------------------
156
157 private static boolean deleteDirectory(Path directory) {
158 try {
159 Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
160 @Override
161 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
162 Files.delete(file);
163 return FileVisitResult.CONTINUE;
164 }
165
166 @Override
167 public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
168 Files.delete(dir);
169 return FileVisitResult.CONTINUE;
170 }
171 });
172 } catch (IOException e) {
173 /* At least we tried... */
174 return false;
175 }
176 return true;
177 }
178
179 /**
180 * Just to test the environment / stdout are working correctly
181 */
182 public static void main(String[] args) {
183 List<String> command = Arrays.asList("ls", "-l");
184 executeCommand(command);
185 }
186
187 private static boolean executeCommand(List<String> command) {
188 try {
189 ProcessBuilder builder = new ProcessBuilder(command);
190 builder.redirectErrorStream(true);
191 builder.redirectOutput(Redirect.INHERIT);
192
193 Process p = builder.start();
194 int ret = p.waitFor();
195 return (ret == 0);
196
197 } catch (IOException | InterruptedException e) {
198 return false;
199 }
200 }
201 }
This page took 0.034937 seconds and 4 git commands to generate.