thread.php
6.69 KB
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
<?php
/**
* Summary of threading
*/
require_once "WEB-INF/php/inc.php";
$server_id = $_GET["server-id"];
if ($server_id) {
$mbean_server = new MBeanServer($server_id);
if (! $mbean_server) {
$title = "Resin: Thread for server $server_id";
display_header("thread.php", $title, $server);
echo "<h3 class='fail'>Can't contact $server_id</h3>";
return;
}
}
else
$mbean_server = new MBeanServer();
$resin = $mbean_server->lookup("resin:type=Resin");
$server = $mbean_server->lookup("resin:type=Server");
$jvm_thread = $mbean_server->lookup("java.lang:type=Threading");
$title = "Resin: Status";
if (! empty($server->Id))
$title = $title . " for server " . $server->Id;
?>
<?php display_header("thread.php", $title, $server); ?>
<h2>Server: <?= $server->Id ?></h2>
<?php
$thread_pool = $server->ThreadPool;
?>
<!--
"Restart" - "Exit this instance cleanly and allow the wrapper script to start a new JVM."
-->
<h2>Thread pool</h2>
<!--
<div class="description">
The ThreadPool manages all threads used by Resin.
</div>
-->
<table class="data">
<tr>
<th colspan='3'>Resin Threads</th>
<th colspan='2'>JVM Threads</th>
<th colspan='2'>Config</th>
</tr>
<tr>
<th title="The number of active threads. These threads are busy servicing requests or performing other tasks.">Active</th>
<th title="The number of idle threads. These threads are allocated but inactive, available for new requests or tasks.">Idle</th>
<th title="The current total number of threads managed by the pool.">Total</th>
<th title="The number of threads currently running in the JVM.">Total</th>
<th title="The maximum number of threads running in the JVM.">Peak</th>
<th title="The maximum number of threads that Resin can allocate.">thread-max</th>
<th title="The minimum number of threads Resin should have available for new requests or other tasks. This value causes a minimum number of idle threads, useful for situations where there is a sudden increase in the number of threads required.">thread-idle-min</th>
</tr>
<tr align='right'>
<td><?= $thread_pool->ThreadActiveCount ?></td>
<td><?= $thread_pool->ThreadIdleCount ?></td>
<td><?= $thread_pool->ThreadCount ?></td>
<td><?= $jvm_thread->ThreadCount ?></td>
<td><?= $jvm_thread->PeakThreadCount ?></td>
<td><?= $thread_pool->ThreadMax ?></td>
<td><?= $thread_pool->ThreadIdleMin ?></td>
</tr>
</table>
<?php
$threads = array();
$thread_ids = $jvm_thread->AllThreadIds;
foreach ($thread_ids as $id) {
$threads[] = $jvm_thread->getThreadInfo($id, 50);
}
$thread_group = partition_threads($threads);
$groups = array("active", "misc", "accept", "keepalive", "idle");
echo "<h2>Threads</h2>\n"
echo "<table class='threads'>\n";
foreach ($groups as $name) {
$threads = $thread_group[$name];
if (sizeof($threads) <= 0)
continue;
usort($threads, "thread_name_cmp");
echo "<tr class='head'><th colspan='5' align='left'>$name (" . sizeof($threads) . ")";
$show = "hide('s_$name');show('h_$name');";
foreach ($threads as $info) {
$show .= "show('t_{$info->threadId}');";
}
$hide = "show('s_$name');hide('h_$name');";
foreach ($threads as $info) {
$hide .= "hide('t_{$info->threadId}');";
}
echo " <a id='s_$name' href=\"javascript:$show\">show</a> ";
echo "<a id='h_$name' href=\"javascript:$hide\" style='display:none'>hide</a>";
echo "</th></tr>\n";
echo "<tr>";
echo "<td style='border-width:0'> </td>";
echo "<th>id</th>";
echo "<th>name</th>";
echo "<th>method</th>";
echo "<th>state</th>";
echo "</tr>\n";
foreach ($threads as $info) {
echo "<tr>";
$id = $info->threadId;
echo "<td style='border-width:0'> </td>";
echo "<td>" . $id . "</td>";
echo "<td>" . $info->threadName . "</td>";
if ($info->stackTrace[0]) {
echo "<td>" . $info->stackTrace[0]->className . "."
. $info->stackTrace[0]->methodName . "()</td>";
}
else
echo "<td></td>";
echo "<td>" . $info->threadState . "</td>";
echo "</tr>\n";
echo "<tr id='t_$id' style='display:none' class='stack_trace'>";
echo "<td style='border-width:0'></td>";
echo "<td colspan='4'>";
echo "<pre>\n";
foreach ($info->stackTrace as $elt) {
echo " at {$elt->className}.{$elt->methodName} ({$elt->fileName}:{$elt->lineNumber})\n";
}
echo "</pre>\n";
echo "</td>";
echo "</tr>\n";
}
}
echo "</table>\n";
/*
foreach ($thread_ids as $id) {
var_dump($jvm_thread->getThreadInfo($id));
}
*/
echo "</pre>";
/*
foreach ($slow_conn as $slow) {
echo "<h3>" . $slow->ObjectName . " " . ($slow->ActiveTime / 1000) . "</h3>";
$thread_id = $slow->ThreadId;
resin_var_dump($thread_id);
$info = $thread_mgr->getThreadInfo($thread_id, 16);
if ($info) {
$bean = Java("java.lang.management.ThreadInfo");
$info = $bean->from($info);
}
resin_var_dump($info->getStackTrace());
}
*/
function thread_name_cmp($thread_a, $thread_b)
{
if ($thread_a->threadName == $thread_b->threadName)
return 0;
else if ($thread_a->threadName < $thread_b->threadName)
return -1;
else
return 1;
}
function partition_threads($threads)
{
$partition = array();
foreach ($threads as $info) {
$stackTrace = $info->stackTrace;
if ($stackTrace[0]->className == "java.lang.Object"
&& $stackTrace[0]->methodName == "wait"
&& $stackTrace[1]->className == "com.caucho.util.ThreadPool\$Item"
&& $stackTrace[1]->methodName == "runTasks") {
$partition["idle"][] = $info;
}
else if (is_accept_thread($info)) {
$partition["accept"][] = $info;
}
else if (is_keepalive_thread($info)) {
$partition["keepalive"][] = $info;
}
else if (preg_match("/^(http|hmux)/", $info->threadName)) {
$partition["active"][] = $info;
}
else {
$partition["misc"][] = $info;
}
}
return $partition;
}
function is_accept_thread($info)
{
foreach ($info->stackTrace as $item) {
if ($item->className == "com.caucho.server.port.Port"
&& $item->methodName == "accept")
return true;
}
return false;
}
function is_keepalive_thread($info)
{
$stackTrace = $info->stackTrace;
for ($i = 0; $i < sizeof($stackTrace); $i++) {
$item = $stackTrace[$i];
if ($item->className == "com.caucho.server.port.TcpConnection"
&& $item->methodName == "run") {
$prev = $stackTrace[$i - 1];
if ($prev->className == "com.caucho.vfs.ReadStream"
&& $prev->methodName == "waitForRead")
return true;
else if ($prev->className == "com.caucho.server.port.TcpConnection"
&& $prev->methodName == "waitForKeepalive")
return true;
else
return false;
}
}
return false;
}
?>
<?php display_footer("status.php"); ?>