Skip to content

Commit b095412

Browse files
committed
estat zil script always reports an average of 10 allocations
closes #58
1 parent f0bb551 commit b095412

File tree

1 file changed

+94
-25
lines changed

1 file changed

+94
-25
lines changed

bpf/standalone/zil.py

Lines changed: 94 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
u64 lwb_ts;
6868
u64 io_ts;
6969
int sync;
70-
int alloc_count;
7170
} zil_tid_info_t;
7271
7372
BPF_HASH(zil_info_map, u32, zil_tid_info_t);
@@ -83,7 +82,7 @@
8382
HIST_KEY(zil_hist_key_t, zil_key_t);
8483
8584
BPF_HASH(average_latency, zil_key_t, average_t);
86-
BPF_HASH(average_allocs, zil_key_t, average_t);
85+
BPF_HASH(call_counts, zil_key_t, u64);
8786
BPF_HASH(zil_latency, zil_hist_key_t, u64);
8887
8988
static inline bool equal_to_pool(char *str)
@@ -111,10 +110,21 @@
111110
avg->sum += delta;
112111
hist_key.slot = bpf_log2l(delta);
113112
zil_latency.increment(hist_key);
113+
call_counts.increment(key);
114114
115115
return 0;
116116
}
117117
118+
static int count_call(char *name) {
119+
zil_key_t key = {};
120+
__builtin_memcpy(&key.name, name, NAME_LENGTH);
121+
key.t = 1;
122+
key.cpuid = bpf_get_smp_processor_id();
123+
call_counts.increment(key);
124+
return 0;
125+
}
126+
127+
118128
int zfs_write_entry(struct pt_regs *ctx, struct znode *zn,
119129
void *uio, int ioflag)
120130
{
@@ -128,7 +138,6 @@
128138
if (!equal_to_pool(spa->spa_name))
129139
return 0;
130140
131-
info.alloc_count = 0;
132141
info.sync = ioflag & (O_SYNC | O_DSYNC) ||
133142
zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS;
134143
zil_info_map.update(&tid, &info);
@@ -154,13 +163,50 @@
154163
return 0;
155164
}
156165
166+
int zfs_fsync_entry(struct pt_regs *ctx, struct znode *zn)
167+
{
168+
u32 tid = bpf_get_current_pid_tgid();
169+
zil_tid_info_t info = {};
170+
171+
info.write_ts = bpf_ktime_get_ns();
172+
zfsvfs_t *zfsvfs = zn->z_inode.i_sb->s_fs_info;
173+
objset_t *z_os = zfsvfs->z_os;
174+
spa_t *spa = z_os->os_spa;
175+
if (!equal_to_pool(spa->spa_name))
176+
return 0;
177+
178+
zil_info_map.update(&tid, &info);
179+
return 0;
180+
}
181+
182+
int zfs_fsync_return(struct pt_regs *cts)
183+
{
184+
u32 tid = bpf_get_current_pid_tgid();
185+
zil_tid_info_t *info = zil_info_map.lookup(&tid);
186+
if (info == NULL) {
187+
return 0;
188+
}
189+
190+
// Record if called zil_commit
191+
if (info->commit_ts != 0) {
192+
latency_average_and_histogram("zfs_fysnc",
193+
(bpf_ktime_get_ns() - info->write_ts) / 1000);
194+
}
195+
196+
zil_info_map.delete(&tid);
197+
return 0;
198+
}
199+
200+
157201
158202
int zil_commit_entry(struct pt_regs *ctx)
159203
{
160204
u32 tid = bpf_get_current_pid_tgid();
161205
zil_tid_info_t *info = zil_info_map.lookup(&tid);
162-
if (info == NULL)
206+
if (info == NULL) {
163207
return 0;
208+
}
209+
164210
info->commit_ts = bpf_ktime_get_ns();
165211
return 0;
166212
}
@@ -176,26 +222,17 @@
176222
latency_average_and_histogram("zil_commit",
177223
(bpf_ktime_get_ns() - info->commit_ts) / 1000);
178224
179-
zil_key_t key = {};
180-
key.t = 1;
181-
key.cpuid = bpf_get_smp_processor_id();
182-
__builtin_memcpy(&key.name, "Allocations", NAME_LENGTH);
183-
average_t zero_avg = ZERO_AVERAGE;
184-
average_t *avg = average_allocs.lookup_or_init(&key, &zero_avg);
185-
avg->count++;
186-
avg->sum += 10; //info->alloc_count;
187225
return 0;
188226
}
189227
190228
// First major call in zil_commit_writer()
191229
int zil_get_commit_list_entry(struct pt_regs *ctx)
192230
{
193231
u32 tid = bpf_get_current_pid_tgid();
194-
u64 ts = bpf_ktime_get_ns();
195232
zil_tid_info_t *info = zil_info_map.lookup(&tid);
196233
if (info == NULL)
197234
return 0;
198-
info->lwb_ts = ts;
235+
info->lwb_ts = bpf_ktime_get_ns();
199236
return 0;
200237
}
201238
@@ -207,7 +244,9 @@
207244
if (info == NULL) {
208245
return 0;
209246
}
210-
247+
if (info->lwb_ts == 0) {
248+
return 0;
249+
}
211250
212251
return latency_average_and_histogram("allocation",
213252
(bpf_ktime_get_ns() - info->lwb_ts) / 1000);
@@ -243,10 +282,33 @@
243282
if (info == NULL) {
244283
return 0;
245284
}
246-
info->alloc_count++;
285+
count_call("block allocations");
286+
return 0;
287+
}
288+
289+
int zil_commit_waiter_skip_entry(struct pt_regs *cts)
290+
{
291+
u32 tid = bpf_get_current_pid_tgid();
292+
zil_tid_info_t *info = zil_info_map.lookup(&tid);
293+
if (info == NULL) {
294+
return 0;
295+
}
296+
297+
count_call("waiter skip");
247298
return 0;
248299
}
249300
301+
int zil_commit_writer_stall_entry(struct pt_regs *cts)
302+
{
303+
u32 tid = bpf_get_current_pid_tgid();
304+
zil_tid_info_t *info = zil_info_map.lookup(&tid);
305+
if (info == NULL) {
306+
return 0;
307+
}
308+
309+
count_call("writer stall");
310+
return 0;
311+
}
250312
"""
251313

252314
# load BPF program
@@ -261,6 +323,8 @@
261323

262324
b.attach_kprobe(event="zfs_write", fn_name="zfs_write_entry")
263325
b.attach_kretprobe(event="zfs_write", fn_name="zfs_write_return")
326+
b.attach_kprobe(event="zfs_fsync", fn_name="zfs_fsync_entry")
327+
b.attach_kretprobe(event="zfs_fsync", fn_name="zfs_fsync_return")
264328
b.attach_kprobe(event="zil_commit", fn_name="zil_commit_entry")
265329
b.attach_kretprobe(event="zil_commit", fn_name="zil_commit_return")
266330
b.attach_kretprobe(event="zil_process_commit_list",
@@ -271,19 +335,24 @@
271335
fn_name="zil_commit_waiter_entry")
272336
b.attach_kretprobe(event="zil_commit_waiter",
273337
fn_name="zil_commit_waiter_return")
274-
b.attach_kretprobe(event="zio_alloc_zil", fn_name="zio_alloc_zil_return")
338+
b.attach_kretprobe(event="zio_alloc_zil",
339+
fn_name="zio_alloc_zil_return")
340+
b.attach_kprobe(event="zil_commit_waiter_skip",
341+
fn_name="zil_commit_waiter_skip_entry")
342+
b.attach_kprobe(event="zil_commit_writer_stall",
343+
fn_name="zil_commit_writer_stall_entry")
275344

276345
latency_helper = BCCHelper(b, BCCHelper.ESTAT_PRINT_MODE)
277346
latency_helper.add_aggregation("average_latency",
278-
BCCHelper.AVERAGE_AGGREGATION, "avg")
347+
BCCHelper.AVERAGE_AGGREGATION, "avg latency")
279348
latency_helper.add_aggregation("zil_latency",
280349
BCCHelper.LOG_HISTOGRAM_AGGREGATION, "latency")
281350
latency_helper.add_key_type("name")
282351

283-
alloc_helper = BCCHelper(b, BCCHelper.ESTAT_PRINT_MODE)
284-
alloc_helper.add_aggregation("average_allocs",
285-
BCCHelper.AVERAGE_AGGREGATION, "avg")
286-
alloc_helper.add_key_type("name")
352+
call_count_helper = BCCHelper(b, BCCHelper.ESTAT_PRINT_MODE)
353+
call_count_helper.add_aggregation("call_counts",
354+
BCCHelper.COUNT_AGGREGATION, "count")
355+
call_count_helper.add_key_type("name")
287356

288357
if (not args.collection_sec):
289358
print(" Tracing enabled... Hit Ctrl-C to end.")
@@ -294,7 +363,7 @@
294363
try:
295364
print("%-16s\n" % strftime("%D - %H:%M:%S %Z"))
296365
latency_helper.printall()
297-
alloc_helper.printall()
366+
call_count_helper.printall()
298367
exit(0)
299368
except Exception as e:
300369
print(str(e))
@@ -307,12 +376,12 @@
307376
except KeyboardInterrupt:
308377
print("%-16s\n" % strftime("%D - %H:%M:%S %Z"))
309378
latency_helper.printall()
310-
alloc_helper.printall()
379+
call_count_helper.printall()
311380
break
312381
try:
313382
print("%-16s\n" % strftime("%D - %H:%M:%S %Z"))
314383
latency_helper.printall()
315-
alloc_helper.printall()
384+
call_count_helper.printall()
316385
except Exception as e:
317386
print(str(e))
318387
break

0 commit comments

Comments
 (0)