aboutsummaryrefslogtreecommitdiff
path: root/spark-common/src/main/java/me/lucko/spark/common/CommandHandler.java
blob: 701dec1882a5176094e14289e48c89f51250a446 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * This file is part of spark.
 *
 *  Copyright (c) lucko (Luck) <luck@lucko.me>
 *  Copyright (c) contributors
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package me.lucko.spark.common;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;

import me.lucko.spark.common.http.Bytebin;
import me.lucko.spark.memory.HeapDump;
import me.lucko.spark.monitor.TickMonitor;
import me.lucko.spark.sampler.Sampler;
import me.lucko.spark.sampler.SamplerBuilder;
import me.lucko.spark.sampler.ThreadDumper;
import me.lucko.spark.sampler.ThreadGrouper;
import me.lucko.spark.sampler.TickCounter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Abstract command handling class used by all platforms.
 *
 * @param <T> the sender (e.g. CommandSender) type used by the platform
 */
public abstract class CommandHandler<T> {

    /** The URL of the viewer frontend */
    private static final String VIEWER_URL = "https://sparkprofiler.github.io/?";
    /** The prefix used in all messages */
    private static final String PREFIX = "&8[&fspark&8] &7";

    /** Guards {@link #activeSampler} */
    private final Object[] activeSamplerMutex = new Object[0];
    /** The WarmRoast instance currently running, if any */
    private Sampler activeSampler = null;
    /** The tick monitor instance currently running, if any */
    private ReportingTickMonitor activeTickMonitor = null;


    // abstract methods implemented by each platform

    protected abstract String getVersion();
    protected abstract String getLabel();
    protected abstract void sendMessage(T sender, String message);
    protected abstract void sendMessage(String message);
    protected abstract void sendLink(String url);
    protected abstract void runAsync(Runnable r);
    protected abstract ThreadDumper getDefaultThreadDumper();
    protected abstract TickCounter newTickCounter();

    private void sendPrefixedMessage(T sender, String message) {
        sendMessage(sender, PREFIX + message);
    }

    private void sendPrefixedMessage(String message) {
        sendMessage(PREFIX + message);
    }

    public void handleCommand(T sender, String[] args) {
        try {
            if (args.length == 0) {
                sendInfo(sender);
                return;
            }

            List<String> arguments = new ArrayList<>(Arrays.asList(args));
            switch (arguments.remove(0).toLowerCase()) {
                case "start":
                    handleStart(sender, arguments);
                    break;
                case "info":
                    handleInfo(sender);
                    break;
                case "cancel":
                    handleCancel(sender);
                    break;
                case "stop":
                case "upload":
                case "paste":
                    handleStop(sender);
                    break;
                case "monitoring":
                    handleMonitoring(sender, arguments);
                    break;
                case "heap":
                case "memory":
                    handleHeap(sender);
                    break;
                default:
                    sendInfo(sender);
                    break;
            }
        } catch (IllegalArgumentException e) {
            sendMessage(sender, "&c" + e.getMessage());
        }
    }

    private void sendInfo(T sender) {
        sendPrefixedMessage(sender, "&fspark profiler &7v" + getVersion());
        sendMessage(sender, "&b&l> &7/" + getLabel() + " start");
        sendMessage(sender, "       &8[&7--timeout&8 <timeout seconds>]");
        sendMessage(sender, "       &8[&7--thread&8 <thread name>]");
        sendMessage(sender, "       &8[&7--not-combined]");
        sendMessage(sender, "       &8[&7--interval&8 <interval millis>]");
        sendMessage(sender, "       &8[&7--only-ticks-over&8 <tick length millis>]");
        sendMessage(sender, "&b&l> &7/" + getLabel() + " info");
        sendMessage(sender, "&b&l> &7/" + getLabel() + " stop");
        sendMessage(sender, "&b&l> &7/" + getLabel() + " cancel");
        sendMessage(sender, "&b&l> &7/" + getLabel() + " monitoring");
        sendMessage(sender, "       &8[&7--threshold&8 <percentage increase>]");
    }

    private void handleStart(T sender, List<String> args) {
        SetMultimap<String, String> arguments = parseArguments(args);

        int timeoutSeconds = parseInt(arguments, "timeout", "d");
        if (timeoutSeconds != -1 && timeoutSeconds <= 10) {
            sendPrefixedMessage(sender, "&cThe specified timeout is not long enough for accurate results to be formed. Please choose a value greater than 10.");
            return;
        }

        if (timeoutSeconds != -1 && timeoutSeconds < 30) {
            sendPrefixedMessage(sender, "&7The accuracy of the output will significantly improve when sampling is able to run for longer periods. Consider setting a timeout value over 30 seconds.");
        }

        int intervalMillis = parseInt(arguments, "interval", "i");
        if (intervalMillis <= 0) {
            intervalMillis = 4;
        }

        Set<String> threads = Sets.union(arguments.get("thread"), arguments.get("t"));
        ThreadDumper threadDumper;
        if (threads.isEmpty()) {
            // use the server thread
            threadDumper = getDefaultThreadDumper();
        } else if (threads.contains("*")) {
            threadDumper = ThreadDumper.ALL;
        } else {
            threadDumper = new ThreadDumper.Specific(threads);
        }

        ThreadGrouper threadGrouper;
        if (arguments.containsKey("not-combined")) {
            threadGrouper = ThreadGrouper.BY_NAME;
        } else {
            threadGrouper = ThreadGrouper.BY_POOL;
        }

        int ticksOver = parseInt(arguments, "only-ticks-over", "o");
        TickCounter tickCounter = null;
        if (ticksOver != -1) {
            try {
                tickCounter = newTickCounter();
            } catch (UnsupportedOperationException e) {
                sendPrefixedMessage(sender, "&cTick counting is not supported!");
                return;
            }
        }

        Sampler sampler;
        synchronized (this.activeSamplerMutex) {
            if (this.activeSampler != null) {
                sendPrefixedMessage(sender, "&7An active sampler is already running.");
                return;
            }

            sendPrefixedMessage("&7Initializing a new profiler, please wait...");

            SamplerBuilder builder = new SamplerBuilder();
            builder.threadDumper(threadDumper);
            builder.threadGrouper(threadGrouper);
            if (timeoutSeconds != -1) {
                builder.completeAfter(timeoutSeconds, TimeUnit.SECONDS);
            }
            builder.samplingInterval(intervalMillis);
            if (ticksOver != -1) {
                builder.ticksOver(ticksOver, tickCounter);
            }
            sampler = this.activeSampler = builder.start();

            sendPrefixedMessage("&bProfiler now active!");
            if (timeoutSeconds == -1) {
                sendPrefixedMessage("&7Use '/" + getLabel() + " stop' to stop profiling and upload the results.");
            } else {
                sendPrefixedMessage("&7The results will be automatically returned after the profiler has been running for " + timeoutSeconds + " seconds.");
            }
        }

        CompletableFuture<Sampler> future = sampler.getFuture();

        // send message if profiling fails
        future.whenCompleteAsync((s, throwable) -> {
            if (throwable != null) {
                sendPrefixedMessage("&cSampling operation failed unexpectedly. Error: " + throwable.toString());
                throwable.printStackTrace();
            }
        });

        // set activeSampler to null when complete.
        future.whenCompleteAsync((s, throwable) -> {
            synchronized (this.activeSamplerMutex) {
                if (sampler == this.activeSampler) {
                    this.activeSampler = null;
                }
            }
        });

        // await the result
        if (timeoutSeconds != -1) {
            future.thenAcceptAsync(s -> {
                sendPrefixedMessage("&7The active sampling operation has completed! Uploading results...");
                handleUpload(s);
            });
        }
    }

    private void handleInfo(T sender) {
        synchronized (this.activeSamplerMutex) {
            if (this.activeSampler == null) {
                sendPrefixedMessage(sender, "&7There isn't an active sampling task running.");
            } else {
                long timeout = this.activeSampler.getEndTime();
                if (timeout == -1) {
                    sendPrefixedMessage(sender, "&7There is an active sampler currently running, with no defined timeout.");
                } else {
                    long timeoutDiff = (timeout - System.currentTimeMillis()) / 1000L;
                    sendPrefixedMessage(sender, "&7There is an active sampler currently running, due to timeout in " + timeoutDiff + " seconds.");
                }

                long runningTime = (System.currentTimeMillis() - this.activeSampler.getStartTime()) / 1000L;
                sendPrefixedMessage(sender, "&7It has been sampling for " + runningTime + " seconds so far.");
            }
        }
    }

    private void handleStop(T sender) {
        synchronized (this.activeSamplerMutex) {
            if (this.activeSampler == null) {
                sendPrefixedMessage(sender, "&7There isn't an active sampling task running.");
            } else {
                this.activeSampler.cancel();
                sendPrefixedMessage("&7The active sampling operation has been stopped! Uploading results...");
                handleUpload(this.activeSampler);
                this.activeSampler = null;
            }
        }
    }

    private void handleCancel(T sender) {
        synchronized (this.activeSamplerMutex) {
            if (this.activeSampler == null) {
                sendPrefixedMessage(sender, "&7There isn't an active sampling task running.");
            } else {
                this.activeSampler.cancel();
                this.activeSampler = null;
                sendPrefixedMessage("&bThe active sampling task has been cancelled.");
            }
        }
    }

    private void handleUpload(Sampler sampler) {
        runAsync(() -> {
            byte[] output = sampler.formCompressedDataPayload();
            try {
                String pasteId = Bytebin.postCompressedContent(output);
                sendPrefixedMessage("&bSampling results:");
                sendLink(VIEWER_URL + pasteId);
            } catch (IOException e) {
                sendPrefixedMessage("&cAn error occurred whilst uploading the results.");
                e.printStackTrace();
            }
        });
    }

    private void handleMonitoring(T sender, List<String> args) {
        SetMultimap<String, String> arguments = parseArguments(args);

        if (this.activeTickMonitor == null) {

            int threshold = parseInt(arguments, "threshold", "t");
            if (threshold == -1) {
                threshold = 100;
            }

            try {
                TickCounter tickCounter = newTickCounter();
                this.activeTickMonitor = new ReportingTickMonitor(tickCounter, threshold);
            } catch (UnsupportedOperationException e) {
                sendPrefixedMessage(sender, "&cNot supported!");
            }
        } else {
            this.activeTickMonitor.close();
            this.activeTickMonitor = null;
            sendPrefixedMessage("&7Tick monitor disabled.");
        }
    }

    private void handleHeap(T sender) {
        runAsync(() -> {
            sendPrefixedMessage("&7Creating a new heap dump, please wait...");

            HeapDump heapDump;
            try {
                heapDump = HeapDump.createNew();
            } catch (Exception e) {
                sendPrefixedMessage("&cAn error occurred whilst inspecting the heap.");
                e.printStackTrace();
                return;
            }

            byte[] output = heapDump.formCompressedDataPayload();
            try {
                String pasteId = Bytebin.postCompressedContent(output);
                sendPrefixedMessage("&bHeap dump output:");
                sendLink(VIEWER_URL + pasteId);
            } catch (IOException e) {
                sendPrefixedMessage("&cAn error occurred whilst uploading the data.");
                e.printStackTrace();
            }
        });
    }

    private class ReportingTickMonitor extends TickMonitor {
        ReportingTickMonitor(TickCounter tickCounter, int percentageChangeThreshold) {
            super(tickCounter, percentageChangeThreshold);
        }

        @Override
        protected void sendMessage(String message) {
            sendPrefixedMessage(message);
        }
    }

    private int parseInt(SetMultimap<String, String> arguments, String longArg, String shortArg) {
        Iterator<String> it = Sets.union(arguments.get(longArg), arguments.get(shortArg)).iterator();
        if (it.hasNext()) {
            try {
                return Math.abs(Integer.parseInt(it.next()));
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("Invalid input for '" + longArg + "' argument. Please specify a number!");
            }
        }
        return -1; // undefined
    }

    private static final Pattern FLAG_REGEX = Pattern.compile("--(.+)$|-([a-zA-z])$");

    private static SetMultimap<String, String> parseArguments(List<String> args) {
        SetMultimap<String, String> arguments = HashMultimap.create();

        String flag = null;
        List<String> value = null;

        for (int i = 0; i < args.size(); i++) {
            String arg = args.get(i);

            Matcher matcher = FLAG_REGEX.matcher(arg);
            boolean matches = matcher.matches();

            if (flag == null || matches) {
                if (!matches) {
                    throw new IllegalArgumentException("Expected flag at position " + i + " but got '" + arg + "' instead!");
                }

                String match = matcher.group(1);
                if (match == null) {
                    match = matcher.group(2);
                }

                // store existing value, if present
                if (flag != null) {
                    arguments.put(flag, String.join(" ", value));
                }

                flag = match.toLowerCase();
                value = new ArrayList<>();
            } else {
                // part of a value
                value.add(arg);
            }
        }

        // store remaining value, if present
        if (flag != null) {
            arguments.put(flag, String.join(" ", value));
        }

        return arguments;
    }
    
}