|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | +package org.graalvm.visualvm.sampler; |
| 26 | + |
| 27 | +import java.util.HashSet; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Set; |
| 30 | +import org.graalvm.visualvm.application.Application; |
| 31 | +import org.graalvm.visualvm.application.ApplicationFinder; |
| 32 | +import org.netbeans.api.sendopts.CommandException; |
| 33 | +import org.netbeans.spi.sendopts.Env; |
| 34 | +import org.netbeans.spi.sendopts.Option; |
| 35 | +import org.netbeans.spi.sendopts.OptionProcessor; |
| 36 | +import org.openide.DialogDisplayer; |
| 37 | +import org.openide.NotifyDescriptor; |
| 38 | +import org.openide.util.NbBundle; |
| 39 | +import org.openide.util.lookup.ServiceProvider; |
| 40 | + |
| 41 | +/** |
| 42 | + * |
| 43 | + * @author Jiri Sedlacek |
| 44 | + */ |
| 45 | +@ServiceProvider(service=OptionProcessor.class) |
| 46 | +public final class SamplerArguments extends OptionProcessor { |
| 47 | + |
| 48 | + private static final String START_CPU_LONG_NAME = "start-cpu-sampler"; // NOI18N |
| 49 | + private static final Option START_CPU_ARGUMENT = Option.shortDescription(Option.requiredArgument(Option.NO_SHORT_NAME, START_CPU_LONG_NAME), "org.graalvm.visualvm.sampler", "Argument_StartCpu_ShortDescr"); // NOI18N |
| 50 | + private static final String START_MEMORY_LONG_NAME = "start-memory-sampler";// NOI18N |
| 51 | + private static final Option START_MEMORY_ARGUMENT = Option.shortDescription(Option.requiredArgument(Option.NO_SHORT_NAME, START_MEMORY_LONG_NAME), "org.graalvm.visualvm.sampler", "Argument_StartMemory_ShortDescr"); // NOI18N |
| 52 | + private static final String SNAPSHOT_LONG_NAME = "snapshot-sampler"; // NOI18N |
| 53 | + private static final Option SNAPSHOT_ARGUMENT = Option.shortDescription(Option.requiredArgument(Option.NO_SHORT_NAME, SNAPSHOT_LONG_NAME), "org.graalvm.visualvm.sampler", "Argument_Snapshot_ShortDescr"); // NOI18N |
| 54 | + private static final String STOP_LONG_NAME = "stop-sampler"; // NOI18N |
| 55 | + private static final Option STOP_ARGUMENT = Option.shortDescription(Option.requiredArgument(Option.NO_SHORT_NAME, STOP_LONG_NAME), "org.graalvm.visualvm.sampler", "Argument_Stop_ShortDescr"); // NOI18N |
| 56 | + |
| 57 | + static enum Request { NONE, CPU, MEMORY }; |
| 58 | + |
| 59 | + |
| 60 | + @Override |
| 61 | + protected Set<Option> getOptions() { |
| 62 | + Set<Option> options = new HashSet(); |
| 63 | + options.add(START_CPU_ARGUMENT); |
| 64 | + options.add(START_MEMORY_ARGUMENT); |
| 65 | + options.add(SNAPSHOT_ARGUMENT); |
| 66 | + options.add(STOP_ARGUMENT); |
| 67 | + return options; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + protected void process(Env env, Map<Option, String[]> maps) throws CommandException { |
| 72 | + String[] startCPU = maps.get(START_CPU_ARGUMENT); |
| 73 | + if (startCPU != null) { |
| 74 | + final String[] _startCPU = startCPU.length == 1 ? startCPU[0].split("@") : null; // NOI18N |
| 75 | + if (_startCPU != null && _startCPU.length == 2) startCPU[0] = _startCPU[0]; |
| 76 | + new Finder(startCPU, START_CPU_LONG_NAME) { |
| 77 | + @Override |
| 78 | + public void found(Application application) { |
| 79 | + String settings = _startCPU != null && _startCPU.length == 2 ? _startCPU[1] : null; |
| 80 | + SamplerSupport.getInstance().startCPU(application, settings); |
| 81 | + } |
| 82 | + }.find(); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + String[] startMemory = maps.get(START_MEMORY_ARGUMENT); |
| 87 | + if (startMemory != null) { |
| 88 | + final String[] _startMemory = startMemory.length == 1 ? startMemory[0].split("@") : null; // NOI18N |
| 89 | + if (_startMemory != null && _startMemory.length == 2) startMemory[0] = _startMemory[0]; |
| 90 | + new Finder(startMemory, START_MEMORY_LONG_NAME) { |
| 91 | + @Override |
| 92 | + public void found(Application application) { |
| 93 | + String settings = _startMemory != null && _startMemory.length == 2 ? _startMemory[1] : null; |
| 94 | + SamplerSupport.getInstance().startMemory(application, settings); |
| 95 | + } |
| 96 | + }.find(); |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + final String[] snapshot = maps.get(SNAPSHOT_ARGUMENT); |
| 101 | + final String[] stop = maps.get(STOP_ARGUMENT); |
| 102 | + if (snapshot != null) { |
| 103 | + new Finder(snapshot, SNAPSHOT_LONG_NAME) { |
| 104 | + @Override |
| 105 | + public void found(Application application) { |
| 106 | + SamplerSupport.getInstance().takeSnapshot(application, true); |
| 107 | + if (stop != null && stop.length == 1 && stop[0].equals(snapshot[0])) |
| 108 | + SamplerSupport.getInstance().stop(application); |
| 109 | + } |
| 110 | + }.find(); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + if (stop != null) { |
| 115 | + new Finder(stop, STOP_LONG_NAME) { |
| 116 | + @Override |
| 117 | + public void found(Application application) { |
| 118 | + SamplerSupport.getInstance().stop(application); |
| 119 | + } |
| 120 | + }.find(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + private static abstract class Finder extends ApplicationFinder { |
| 126 | + |
| 127 | + Finder(String[] pids, String longName) throws CommandException { |
| 128 | + super(resolvePid(pids, longName)); |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + public final void notFound(int pid, String id) { |
| 133 | + NotifyDescriptor desc = new NotifyDescriptor.Message(NbBundle.getMessage(SamplerArguments.class, "MSG_NO_APP_PID", new Object[] { Integer.toString(pid) }), NotifyDescriptor.WARNING_MESSAGE); |
| 134 | + DialogDisplayer.getDefault().notifyLater(desc); |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + private static int resolvePid(String[] pids, String longName) throws CommandException { |
| 139 | + if (pids.length == 1) { |
| 140 | + try { |
| 141 | + return Integer.valueOf(pids[0]); |
| 142 | + } catch (NumberFormatException e) { |
| 143 | + throw new CommandException(0, "Incorrect pid format for --" + longName + ": " + e.getMessage()); // NOI18N |
| 144 | + } |
| 145 | + } else { |
| 146 | + throw new CommandException(0, "--" + longName + " requires exactly one value"); // NOI18N |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments