| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package sk.baka.webvm; |
| 20 | |
|
| 21 | |
import java.lang.management.ThreadInfo; |
| 22 | |
import java.util.ArrayList; |
| 23 | |
import java.util.List; |
| 24 | |
import java.util.Map; |
| 25 | |
import java.util.SortedMap; |
| 26 | |
import java.util.TreeMap; |
| 27 | |
import org.apache.wicket.behavior.SimpleAttributeModifier; |
| 28 | |
import org.apache.wicket.markup.html.basic.Label; |
| 29 | |
import org.apache.wicket.markup.html.list.ListItem; |
| 30 | |
import org.apache.wicket.markup.html.list.ListView; |
| 31 | |
import org.apache.wicket.model.IModel; |
| 32 | |
import org.apache.wicket.model.LoadableDetachableModel; |
| 33 | |
import sk.baka.webvm.analyzer.HistorySample; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | 0 | public final class Threads extends WebVMPage { |
| 40 | |
|
| 41 | |
private static final long serialVersionUID = 1L; |
| 42 | |
private static final int MAX_THREAD_NAME_LENGTH = 30; |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | 0 | public Threads() { |
| 48 | 0 | border.add(new ThreadListView("threads", new ThreadListModel())); |
| 49 | 0 | } |
| 50 | |
|
| 51 | |
private static SortedMap<Long, List<ThreadInfo>> analyzeThreads() { |
| 52 | 0 | final List<HistorySample> samples = WicketApplication.getHistory().getVmstatHistory(); |
| 53 | 0 | final SortedMap<Long, List<ThreadInfo>> history = new TreeMap<Long, List<ThreadInfo>>(); |
| 54 | |
|
| 55 | 0 | int i = 0; |
| 56 | 0 | for (final HistorySample sample : samples) { |
| 57 | 0 | for (final ThreadInfo info : sample.threads) { |
| 58 | 0 | if (info == null) { |
| 59 | 0 | continue; |
| 60 | |
} |
| 61 | 0 | List<ThreadInfo> list = history.get(info.getThreadId()); |
| 62 | 0 | if (list == null) { |
| 63 | 0 | list = new ArrayList<ThreadInfo>(samples.size()); |
| 64 | 0 | history.put(info.getThreadId(), list); |
| 65 | |
} |
| 66 | 0 | ensureSize(list, i); |
| 67 | 0 | list.add(info); |
| 68 | |
} |
| 69 | 0 | i++; |
| 70 | |
} |
| 71 | |
|
| 72 | 0 | for (final List<ThreadInfo> infos : history.values()) { |
| 73 | 0 | ensureSize(infos, i); |
| 74 | |
} |
| 75 | 0 | return history; |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
private static char getStateChar(final ThreadInfo info) { |
| 84 | 0 | if (info == null) { |
| 85 | 0 | return ' '; |
| 86 | |
} |
| 87 | 0 | switch (info.getThreadState()) { |
| 88 | |
case NEW: |
| 89 | 0 | return '.'; |
| 90 | |
case BLOCKED: |
| 91 | 0 | return 'x'; |
| 92 | |
case RUNNABLE: |
| 93 | 0 | return '|'; |
| 94 | |
case TERMINATED: |
| 95 | 0 | return '_'; |
| 96 | |
case TIMED_WAITING: |
| 97 | |
case WAITING: |
| 98 | 0 | return 'z'; |
| 99 | |
} |
| 100 | 0 | throw new AssertionError(); |
| 101 | |
} |
| 102 | |
|
| 103 | 0 | private static class ThreadListModel extends LoadableDetachableModel<List<List<ThreadInfo>>> { |
| 104 | |
|
| 105 | |
private static final long serialVersionUID = 1L; |
| 106 | |
private transient Map<Long, List<ThreadInfo>> map; |
| 107 | |
|
| 108 | |
@Override |
| 109 | |
protected List<List<ThreadInfo>> load() { |
| 110 | 0 | if (map == null) { |
| 111 | 0 | map = analyzeThreads(); |
| 112 | |
} |
| 113 | 0 | final List<List<ThreadInfo>> result = new ArrayList<List<ThreadInfo>>(map.values()); |
| 114 | 0 | return result; |
| 115 | |
} |
| 116 | |
} |
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
private static List<ThreadInfo> ensureSize(List<ThreadInfo> list, final int size) { |
| 125 | 0 | while (list.size() < size) { |
| 126 | 0 | list.add(null); |
| 127 | |
} |
| 128 | 0 | return list; |
| 129 | |
} |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
private static class ThreadListView extends ListView<List<ThreadInfo>> { |
| 135 | |
|
| 136 | |
private static final long serialVersionUID = 1L; |
| 137 | |
|
| 138 | |
public ThreadListView(String id, IModel<? extends List<? extends List<ThreadInfo>>> model) { |
| 139 | 0 | super(id, model); |
| 140 | 0 | } |
| 141 | |
|
| 142 | |
@Override |
| 143 | |
protected void populateItem(ListItem<List<ThreadInfo>> item) { |
| 144 | 0 | final List<ThreadInfo> infos = item.getModelObject(); |
| 145 | 0 | ThreadInfo ti = null; |
| 146 | 0 | for (final ThreadInfo info : infos) { |
| 147 | 0 | if (info != null) { |
| 148 | 0 | ti = info; |
| 149 | 0 | break; |
| 150 | |
} |
| 151 | |
} |
| 152 | 0 | final ThreadInfo last = infos.get(infos.size() - 1); |
| 153 | 0 | String name = ti.getThreadName(); |
| 154 | 0 | String title = name; |
| 155 | 0 | if (name.length() > MAX_THREAD_NAME_LENGTH) { |
| 156 | 0 | name = name.substring(0, MAX_THREAD_NAME_LENGTH) + "..."; |
| 157 | |
} |
| 158 | 0 | final Label l = new Label("threadName", name); |
| 159 | 0 | item.add(l); |
| 160 | 0 | l.add(new SimpleAttributeModifier("title", title)); |
| 161 | 0 | final String state = last == null ? "dead" : last.getThreadState().toString(); |
| 162 | 0 | item.add(new Label("threadState", state)); |
| 163 | 0 | final StringBuilder sb = new StringBuilder(); |
| 164 | 0 | for (final ThreadInfo info : infos) { |
| 165 | 0 | sb.append(getStateChar(info)); |
| 166 | |
} |
| 167 | 0 | item.add(new Label("threadHistory", sb.toString())); |
| 168 | 0 | } |
| 169 | |
} |
| 170 | |
} |
| 171 | |
|