Codebergdnkl

foot

master
ply5 git calls500 ms31 KiB3 cachedprotocol v2
496 lines12 KiBRaw
1#include "fdm.h"
3#include <stdlib.h>
4#include <stdbool.h>
5#include <inttypes.h>
6#include <unistd.h>
7#include <errno.h>
8#include <fcntl.h>
9#include <signal.h>
11#include <sys/epoll.h>
13#include <tllist.h>
15#define LOG_MODULE "fdm"
16#define LOG_ENABLE_DBG 0
17#include "log.h"
18#include "debug.h"
19#include "xmalloc.h"
21#if !defined(SIGABBREV_NP)
22#include <stdio.h>
24static const char *
25sigabbrev_np(int sig)
27 static char buf[16];
28 snprintf(buf, sizeof(buf), "<%d>", sig);
29 return buf;
31#endif
33struct fd_handler {
34 int fd;
35 int events;
36 fdm_fd_handler_t callback;
37 void *callback_data;
38 bool deleted;
39};
41struct sig_handler {
42 fdm_signal_handler_t callback;
43 void *callback_data;
44};
46struct hook {
47 fdm_hook_t callback;
48 void *callback_data;
49};
51typedef tll(struct hook) hooks_t;
53struct fdm {
54 int epoll_fd;
55 bool is_polling;
56 tll(struct fd_handler *) fds;
57 tll(struct fd_handler *) deferred_delete;
59 sigset_t sigmask;
60 struct sig_handler *signal_handlers;
62 hooks_t hooks_low;
63 hooks_t hooks_normal;
64 hooks_t hooks_high;
65};
67static volatile sig_atomic_t got_signal = false;
68static volatile sig_atomic_t *received_signals = NULL;
70struct fdm *
71fdm_init(void)
73 sigset_t sigmask;
74 if (sigprocmask(0, NULL, &sigmask) < 0) {
75 LOG_ERRNO("failed to get process signal mask");
76 return NULL;
77 }
79 int epoll_fd = epoll_create1(EPOLL_CLOEXEC);
80 if (epoll_fd == -1) {
81 LOG_ERRNO("failed to create epoll FD");
82 return NULL;
83 }
85 xassert(received_signals == NULL); /* Only one FDM instance supported */
86 received_signals = xcalloc(SIGRTMAX, sizeof(received_signals[0]));
87 got_signal = false;
89 struct fdm *fdm = malloc(sizeof(*fdm));
90 if (unlikely(fdm == NULL)) {
91 LOG_ERRNO("malloc() failed");
92 return NULL;
93 }
95 struct sig_handler *sig_handlers = calloc(SIGRTMAX, sizeof(sig_handlers[0]));
97 if (sig_handlers == NULL) {
98 LOG_ERRNO("failed to allocate signal handler array");
99 free(fdm);
100 return NULL;
101 }
103 *fdm = (struct fdm){
104 .epoll_fd = epoll_fd,
105 .is_polling = false,
106 .fds = tll_init(),
107 .deferred_delete = tll_init(),
108 .sigmask = sigmask,
109 .signal_handlers = sig_handlers,
110 .hooks_low = tll_init(),
111 .hooks_normal = tll_init(),
112 .hooks_high = tll_init(),
113 };
114 return fdm;
117void
118fdm_destroy(struct fdm *fdm)
120 if (fdm == NULL)
121 return;
123 if (tll_length(fdm->fds) > 0)
124 LOG_WARN("FD list not empty");
126 for (int i = 0; i < SIGRTMAX; i++) {
127 if (fdm->signal_handlers[i].callback != NULL)
128 LOG_WARN("handler for signal %d (SIG%s) not removed",
129 i, sigabbrev_np(i));
130 }
132 if (tll_length(fdm->hooks_low) > 0 ||
133 tll_length(fdm->hooks_normal) > 0 ||
134 tll_length(fdm->hooks_high) > 0)
135 {
136 LOG_WARN("hook list not empty");
137 }
139 xassert(tll_length(fdm->fds) == 0);
140 xassert(tll_length(fdm->deferred_delete) == 0);
141 xassert(tll_length(fdm->hooks_low) == 0);
142 xassert(tll_length(fdm->hooks_normal) == 0);
143 xassert(tll_length(fdm->hooks_high) == 0);
145 sigprocmask(SIG_SETMASK, &fdm->sigmask, NULL);
146 free(fdm->signal_handlers);
148 tll_free(fdm->fds);
149 tll_free(fdm->deferred_delete);
150 tll_free(fdm->hooks_low);
151 tll_free(fdm->hooks_normal);
152 tll_free(fdm->hooks_high);
153 close(fdm->epoll_fd);
154 free(fdm);
156 free((void *)received_signals);
157 received_signals = NULL;
160bool
161fdm_add(struct fdm *fdm, int fd, int events, fdm_fd_handler_t cb, void *data)
163#if defined(_DEBUG)
164 tll_foreach(fdm->fds, it) {
165 if (it->item->fd == fd) {
166 BUG("FD=%d already registered", fd);
167 }
168 }
169#endif
171 struct fd_handler *handler = malloc(sizeof(*handler));
172 if (unlikely(handler == NULL)) {
173 LOG_ERRNO("malloc() failed");
174 return false;
175 }
177 *handler = (struct fd_handler) {
178 .fd = fd,
179 .events = events,
180 .callback = cb,
181 .callback_data = data,
182 .deleted = false,
183 };
185 tll_push_back(fdm->fds, handler);
187 struct epoll_event ev = {
188 .events = events,
189 .data = {.ptr = handler},
190 };
192 if (epoll_ctl(fdm->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
193 LOG_ERRNO("failed to register FD=%d with epoll", fd);
194 free(handler);
195 tll_pop_back(fdm->fds);
196 return false;
197 }
199 return true;
202static bool
203fdm_del_internal(struct fdm *fdm, int fd, bool close_fd)
205 if (fd == -1)
206 return true;
208 tll_foreach(fdm->fds, it) {
209 if (it->item->fd != fd)
210 continue;
212 if (epoll_ctl(fdm->epoll_fd, EPOLL_CTL_DEL, fd, NULL) < 0)
213 LOG_ERRNO("failed to unregister FD=%d from epoll", fd);
215 if (close_fd)
216 close(it->item->fd);
218 it->item->deleted = true;
219 if (fdm->is_polling)
220 tll_push_back(fdm->deferred_delete, it->item);
221 else
222 free(it->item);
224 tll_remove(fdm->fds, it);
225 return true;
226 }
228 LOG_ERR("no such FD: %d", fd);
229 close(fd);
230 return false;
233bool
234fdm_del(struct fdm *fdm, int fd)
236 return fdm_del_internal(fdm, fd, true);
239bool
240fdm_del_no_close(struct fdm *fdm, int fd)
242 return fdm_del_internal(fdm, fd, false);
245static bool
246event_modify(struct fdm *fdm, struct fd_handler *fd, int new_events)
248 if (new_events == fd->events)
249 return true;
251 struct epoll_event ev = {
252 .events = new_events,
253 .data = {.ptr = fd},
254 };
256 if (epoll_ctl(fdm->epoll_fd, EPOLL_CTL_MOD, fd->fd, &ev) < 0) {
257 LOG_ERRNO("failed to modify FD=%d with epoll (events 0x%08x -> 0x%08x)",
258 fd->fd, fd->events, new_events);
259 return false;
260 }
262 fd->events = new_events;
263 return true;
266bool
267fdm_event_add(struct fdm *fdm, int fd, int events)
269 tll_foreach(fdm->fds, it) {
270 if (it->item->fd != fd)
271 continue;
273 return event_modify(fdm, it->item, it->item->events | events);
274 }
276 LOG_ERR("FD=%d not registered with the FDM", fd);
277 return false;
280bool
281fdm_event_del(struct fdm *fdm, int fd, int events)
283 tll_foreach(fdm->fds, it) {
284 if (it->item->fd != fd)
285 continue;
287 return event_modify(fdm, it->item, it->item->events & ~events);
288 }
290 LOG_ERR("FD=%d not registered with the FDM", fd);
291 return false;
294static hooks_t *
295hook_priority_to_list(struct fdm *fdm, enum fdm_hook_priority priority)
297 switch (priority) {
298 case FDM_HOOK_PRIORITY_LOW: return &fdm->hooks_low;
299 case FDM_HOOK_PRIORITY_NORMAL: return &fdm->hooks_normal;
300 case FDM_HOOK_PRIORITY_HIGH: return &fdm->hooks_high;
301 }
303 BUG("unhandled priority type");
304 return NULL;
307bool
308fdm_hook_add(struct fdm *fdm, fdm_hook_t hook, void *data,
309 enum fdm_hook_priority priority)
311 hooks_t *hooks = hook_priority_to_list(fdm, priority);
313#if defined(_DEBUG)
314 tll_foreach(*hooks, it) {
315 if (it->item.callback == hook) {
316 LOG_ERR("hook=0x%" PRIxPTR " already registered", (uintptr_t)hook);
317 return false;
318 }
319 }
320#endif
322 tll_push_back(*hooks, ((struct hook){hook, data}));
323 return true;
326bool
327fdm_hook_del(struct fdm *fdm, fdm_hook_t hook, enum fdm_hook_priority priority)
329 hooks_t *hooks = hook_priority_to_list(fdm, priority);
331 tll_foreach(*hooks, it) {
332 if (it->item.callback != hook)
333 continue;
335 tll_remove(*hooks, it);
336 return true;
337 }
339 LOG_WARN("hook=0x%" PRIxPTR " not registered", (uintptr_t)hook);
340 return false;
343static void
344signal_handler(int signo)
346 got_signal = true;
347 received_signals[signo] = true;
350bool
351fdm_signal_add(struct fdm *fdm, int signo, fdm_signal_handler_t handler, void *data)
353 if (fdm->signal_handlers[signo].callback != NULL) {
354 LOG_ERR("signal %d (SIG%s) already has a handler",
355 signo, sigabbrev_np(signo));
356 return false;
357 }
359 sigset_t mask, original;
360 sigemptyset(&mask);
361 sigaddset(&mask, signo);
363 if (sigprocmask(SIG_BLOCK, &mask, &original) < 0) {
364 LOG_ERRNO("failed to block signal %d (SIG%s)",
365 signo, sigabbrev_np(signo));
366 return false;
367 }
369 struct sigaction action = {.sa_handler = &signal_handler};
370 sigemptyset(&action.sa_mask);
371 if (sigaction(signo, &action, NULL) < 0) {
372 LOG_ERRNO("failed to set signal handler for signal %d (SIG%s)",
373 signo, sigabbrev_np(signo));
374 sigprocmask(SIG_SETMASK, &original, NULL);
375 return false;
376 }
378 received_signals[signo] = false;
379 fdm->signal_handlers[signo].callback = handler;
380 fdm->signal_handlers[signo].callback_data = data;
381 return true;
384bool
385fdm_signal_del(struct fdm *fdm, int signo)
387 if (fdm->signal_handlers[signo].callback == NULL)
388 return false;
390 struct sigaction action = {.sa_handler = SIG_DFL};
391 sigemptyset(&action.sa_mask);
392 if (sigaction(signo, &action, NULL) < 0) {
393 LOG_ERRNO("failed to restore signal handler for signal %d (SIG%s)",
394 signo, sigabbrev_np(signo));
395 return false;
396 }
398 received_signals[signo] = false;
399 fdm->signal_handlers[signo].callback = NULL;
400 fdm->signal_handlers[signo].callback_data = NULL;
402 sigset_t mask;
403 sigemptyset(&mask);
404 sigaddset(&mask, signo);
405 if (sigprocmask(SIG_UNBLOCK, &mask, NULL) < 0) {
406 LOG_ERRNO("failed to unblock signal %d (SIG%s)",
407 signo, sigabbrev_np(signo));
408 return false;
409 }
411 return true;
414bool
415fdm_poll(struct fdm *fdm)
417 xassert(!fdm->is_polling && "nested calls to fdm_poll() not allowed");
418 if (fdm->is_polling) {
419 LOG_ERR("nested calls to fdm_poll() not allowed");
420 return false;
421 }
423 tll_foreach(fdm->hooks_high, it) {
424 LOG_DBG(
425 "executing high priority hook 0x%" PRIxPTR" (fdm=%p, data=%p)",
426 (uintptr_t)it->item.callback, (void *)fdm,
427 (void *)it->item.callback_data);
428 it->item.callback(fdm, it->item.callback_data);
429 }
430 tll_foreach(fdm->hooks_normal, it) {
431 LOG_DBG(
432 "executing normal priority hook 0x%" PRIxPTR " (fdm=%p, data=%p)",
433 (uintptr_t)it->item.callback, (void *)fdm,
434 (void *)it->item.callback_data);
435 it->item.callback(fdm, it->item.callback_data);
436 }
437 tll_foreach(fdm->hooks_low, it) {
438 LOG_DBG(
439 "executing low priority hook 0x%" PRIxPTR " (fdm=%p, data=%p)",
440 (uintptr_t)it->item.callback, (void *)fdm,
441 (void *)it->item.callback_data);
442 it->item.callback(fdm, it->item.callback_data);
443 }
445 struct epoll_event events[tll_length(fdm->fds)];
447 int r = epoll_pwait(
448 fdm->epoll_fd, events, tll_length(fdm->fds), -1, &fdm->sigmask);
450 int errno_copy = errno;
452 if (unlikely(got_signal)) {
453 got_signal = false;
455 for (int i = 0; i < SIGRTMAX; i++) {
456 if (received_signals[i]) {
457 received_signals[i] = false;
458 struct sig_handler *handler = &fdm->signal_handlers[i];
460 xassert(handler->callback != NULL);
461 if (!handler->callback(fdm, i, handler->callback_data))
462 return false;
463 }
464 }
465 }
467 if (unlikely(r < 0)) {
468 if (errno_copy == EINTR)
469 return true;
471 LOG_ERRNO_P(errno_copy, "failed to epoll");
472 return false;
473 }
475 bool ret = true;
477 fdm->is_polling = true;
478 for (int i = 0; i < r; i++) {
479 struct fd_handler *fd = events[i].data.ptr;
480 if (fd->deleted)
481 continue;
483 if (!fd->callback(fdm, fd->fd, events[i].events, fd->callback_data)) {
484 ret = false;
485 break;
486 }
487 }
488 fdm->is_polling = false;
490 tll_foreach(fdm->deferred_delete, it) {
491 free(it->item);
492 tll_remove(fdm->deferred_delete, it);
493 }
495 return ret;
Diagnostics5 git calls · 500 ms
CallStatusSizeTime0500 ms
discover200178 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=178
git.server.agent=git/2.56.1-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=178
git.pkt.payload.max=35
ls-refs20010 KiB461 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=382
git.response.bytes=10322
git.response.cache=EXPIRED
git.response.duration_ms=461
git.protocol.version=2
git.pkt.frames=108
git.pkt.data=107
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=10322
git.pkt.payload.max=106
git.ls_refs.count=107
git.ls_refs.head=refs/heads/master
fetchcombine:blob:none+tree:2 · deepen 12008.6 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=8830
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=8830
git.pkt.payload.max=8730
git.sideband.band1.bytes=8730
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=14
git.pack.bytes=8730
git.fetch.wide.objects=14
git.fetch.wide.extra_bytes=9909
git.fetch.wide.blobs=0
fetchtree:0 · deepen 102009.3 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=9489
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=9489
git.pkt.payload.max=9389
git.sideband.band1.bytes=9389
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=10
git.pack.bytes=9389
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=de930681b6073ba5b599a0f956bfbd114762ad5a
git.pack.full=10
Open pack map
fetch2002.7 KiB · hit36 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=36
git.response.bytes=2780
git.response.cache=HIT
git.response.duration_ms=36
git.fetch.wants=4822cd97681e9089c737e860bc5b9e7fbfd17eb6
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=2780
git.pkt.payload.max=2753
git.sideband.band1.bytes=2753
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://codeberg.org/dnkl/foot
Ref
refs/heads/master
Commit
c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
cf-ray
a5e2215a5e32a87d
Request
a882f69f…
Protocol
v2 · sha1
Agent
git/2.56.1-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format