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
|
set threadMonitorLib [apply {{} {
set cc [C]
$cc cflags -I. -I./vendor/tracy/public
$cc include "workqueue.h"
$cc include "common.h"
$cc code {
extern ThreadControlBlock threads[];
extern int _Atomic threadCount;
extern Db* db;
extern int unsafe_workQueueCopy(WorkQueueItem* into, int maxn, WorkQueue* q);
extern void traceItem(char* buf, size_t bufsz, WorkQueueItem item);
Jim_Obj* itemToStringObj(WorkQueueItem item) {
char buf[10000]; traceItem(buf, sizeof(buf), item);
return Jim_NewStringObj(interp, buf, -1);
}
}
$cc proc workerTids {} Jim_Obj* {
// Fallback on macOS, where we don't have /proc to query all
// threads of the Folk process.
Jim_Obj* ret = Jim_NewListObj(interp, NULL, 0);
for (int i = 0; i < threadCount; i++) {
if (threads[i].tid != 0) {
Jim_ListAppendElement(interp, ret,
Jim_NewIntObj(interp, threads[i].tid));
}
}
return ret;
}
$cc proc workerInfo {int threadIndex} Jim_Obj* {
if (threadIndex >= threadCount || threads[threadIndex].tid == 0) {
return Jim_NewEmptyStringObj(interp);
}
ThreadControlBlock *thread = &threads[threadIndex];
Jim_Obj* ret = Jim_NewDictObj(interp, NULL, 0);
WorkQueueItem item = thread->currentItem;
Jim_DictAddElement(interp, ret, Jim_NewStringObj(interp, "op", -1),
itemToStringObj(item));
WorkQueueItem items[100];
int nitems = unsafe_workQueueCopy(items, 100, thread->workQueue);
Jim_Obj* workQueueObj = Jim_NewListObj(interp, NULL, 0);
for (int i = 0; i < nitems; i++) {
Jim_ListAppendElement(interp, workQueueObj,
itemToStringObj(items[i]));
}
Jim_DictAddElement(interp, ret,
Jim_NewStringObj(interp, "workQueue", -1),
workQueueObj);
Jim_DictAddElement(interp, ret,
Jim_NewStringObj(interp, "isDeactivated", -1),
Jim_NewIntObj(interp, thread->isDeactivated));
Jim_DictAddElement(interp, ret,
Jim_NewStringObj(interp, "currentItemStartTimestamp", -1),
Jim_ObjPrintf("%" PRId64, thread->currentItemStartTimestamp));
int64_t now = timestamp_get(thread->clockid);
Jim_DictAddElement(interp, ret,
Jim_NewStringObj(interp, "elapsed", -1),
Jim_NewDoubleObj(interp, (double)(now - thread->currentItemStartTimestamp) / 1000.0));
Jim_DictAddElement(interp, ret,
Jim_NewStringObj(interp, "_allocs", -1),
Jim_NewDoubleObj(interp, thread->_allocs));
Jim_DictAddElement(interp, ret,
Jim_NewStringObj(interp, "_frees", -1),
Jim_NewDoubleObj(interp, thread->_frees));
return ret;
}
$cc proc workerInfoFromTid {int tid} Jim_Obj* {
for (int i = 0; i < threadCount; i++) {
if (threads[i].tid == tid) {
return workerInfo(i);
}
}
return Jim_NewEmptyStringObj(interp);
}
$cc code {
#include "vendor/c11-queues/mpmc_queue.h"
extern struct mpmc_queue globalWorkQueue;
extern _Atomic int globalWorkQueueSize;
}
$cc proc globalWorkQueueAvailable {} size_t {
return mpmc_queue_available(&globalWorkQueue);
}
# Unsafely peeks at the queue.
$cc proc globalWorkQueueItems {} Jim_Obj* {
Jim_Obj *ret = Jim_NewListObj(interp, NULL, 0);
size_t head = globalWorkQueue.head;
for (size_t i = head; i < globalWorkQueue.tail; i++) {
struct mpmc_queue_cell *cell = &globalWorkQueue.buffer[i & globalWorkQueue.buffer_mask];
WorkQueueItem *ptr = cell->data;
WorkQueueItem item = *ptr;
Jim_ListAppendElement(interp, ret, itemToStringObj(item));
}
return ret;
}
return [$cc compile]
}}]
Wish the web server handles route "/threads" with handler {
set pid [pid]
try {
set tids [glob -tails -directory /proc/$pid/task *]
set userStacks [dict create]
set euStackOutput [exec eu-stack --pid=[pid] --verbose]
set currentTid none
foreach line [split $euStackOutput "\n"] {
if {[regexp {TID ([0-9]+):} $line -> tid]} {
set currentTid $tid
} else {
dict set userStacks $currentTid \
"[dict getdef $userStacks $currentTid {}]\n$line"
}
}
} on error e {
set tids [$threadMonitorLib workerTids]
}
set totalAllocsMinusFrees 0.0
html [subst {
<html>
<head>
<link rel="stylesheet" href="/style.css">
<title>Threads</title>
</head>
<body>
<h2>Threads</h2>
<ol start="0">
[join [lmap tid $tids {
set taskdir /proc/$pid/task/$tid
try {
set statusFd [open $taskdir/status r]
set status [read $statusFd]; close $statusFd
} on error e {
set status "<status unknown>"
}
set workerInfo [$threadMonitorLib workerInfoFromTid $tid]
try {
set userStack [dict get $userStacks $tid]
} on error e {
set userStack "<not found>"
}
try {
set stackFd [open $taskdir/stack r]
set kernelStack [read $stackFd]; close $stackFd
} on error e {
set kernelStack "<not permitted>"
}
set allocs [dict getdef $workerInfo _allocs -1]
set frees [dict getdef $workerInfo _frees -1]
subst {<li style="[expr {$workerInfo eq "" ? "color: gray" : ""}]">
$taskdir: [regexp -inline {State:[^\n]*\n} $status]
[expr {$workerInfo(isDeactivated) ? "(deactivated)" : "(active)"}]<br>
[if {$workerInfo eq ""} {subst {
(Not a Folk worker thread)<br>
}} else {
set totalAllocsMinusFrees [expr {$totalAllocsMinusFrees + ($allocs - $frees)}]
set workQueue [dict get $workerInfo workQueue]
subst {
[format "allocs - frees: %.2f MB" [expr {($allocs - $frees) / 1000000.0}]]
([format "allocs: %.2f MB" [/ $allocs 1000000.0]];
[format "frees: %.2f MB" [/ $frees 1000000.0]])<br>
[htmlEscape [dict get $workerInfo op]] (elapsed: [dict get $workerInfo elapsed] us)<br>
<details>
<summary>Work queue ([llength $workQueue] items):</summary>
<pre>[htmlEscape [join $workQueue "\n"]]</pre>
</details>
}
}]
<details><summary>User stack:</summary><pre>[htmlEscape $userStack]</pre></details>
<details><summary>Kernel stack:</summary><pre>[htmlEscape $kernelStack]</pre></details>
</li>}
}] "\n"]
</ol>
<p>Total allocs - frees: [format "%.2f MB" [expr {$totalAllocsMinusFrees / 1000000.0}]]</p>
<h2>Global workqueue ([$threadMonitorLib globalWorkQueueAvailable])</h2>
<ol start="0">
[lmap item [$threadMonitorLib globalWorkQueueItems] {
subst { <li>$item</li> }
}]
</ol>
</body>
</html>
}]
}
|