Artifactspublic-v0

curl

master
ply4 git calls31 ms290 KiB2 cachedprotocol v2
433 lines12 KiBRaw
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "curl_setup.h"
26#ifdef USE_THREADS
28#include "llist.h"
29#include "curl_threads.h"
30#include "thrdpool.h"
31#include "thrdqueue.h"
32#include "curlx/timeval.h"
33#ifdef CURLVERBOSE
34#include "curl_trc.h"
35#include "urldata.h"
36#endif
39struct curl_thrdq {
40 const char *name;
41 curl_mutex_t lock;
42 curl_cond_t await;
43 struct Curl_llist sendq;
44 struct Curl_llist recvq;
45 struct curl_thrdpool *tpool;
46 Curl_thrdq_item_free_cb *fn_free;
47 Curl_thrdq_item_process_cb *fn_process;
48 Curl_thrdq_ev_cb *fn_event;
49 void *fn_user_data;
50 BIT(aborted);
51};
53struct thrdq_item {
54 struct Curl_llist_node node;
55 Curl_thrdq_item_free_cb *fn_free;
56 Curl_thrdq_item_process_cb *fn_process;
57 void *item;
58 struct curltime start;
59 timediff_t timeout_ms;
60 const char *description;
61};
63static struct thrdq_item *thrdq_item_create(struct curl_thrdq *tqueue,
64 void *item,
65 const char *description,
66 timediff_t timeout_ms)
68 struct thrdq_item *qitem;
70 qitem = curlx_calloc(1, sizeof(*qitem));
71 if(!qitem)
72 return NULL;
73 qitem->item = item;
74 qitem->description = description;
75 qitem->fn_free = tqueue->fn_free;
76 qitem->fn_process = tqueue->fn_process;
77 if(timeout_ms) {
78 qitem->start = curlx_now();
79 qitem->timeout_ms = timeout_ms;
80 }
81 return qitem;
84static void thrdq_item_destroy(struct thrdq_item *qitem)
86 if(qitem->item)
87 qitem->fn_free(qitem->item);
88 curlx_free(qitem);
91static void thrdq_item_list_dtor(void *user_data, void *elem)
93 (void)user_data;
94 thrdq_item_destroy(elem);
97static void *thrdq_tpool_take(void *user_data, const char **pdescription,
98 timediff_t *ptimeout_ms)
100 struct curl_thrdq *tqueue = user_data;
101 struct thrdq_item *qitem = NULL;
102 struct Curl_llist_node *e;
103 Curl_thrdq_ev_cb *fn_event = NULL;
104 void *fn_user_data = NULL;
106 Curl_mutex_acquire(&tqueue->lock);
107 *pdescription = NULL;
108 *ptimeout_ms = 0;
109 if(!tqueue->aborted) {
110 e = Curl_llist_head(&tqueue->sendq);
111 if(e) {
112 struct curltime now = curlx_now();
113 timediff_t timeout_ms;
114 while(e) {
115 qitem = Curl_node_take_elem(e);
116 timeout_ms = (!qitem->timeout_ms) ? 0 :
117 (qitem->timeout_ms - curlx_ptimediff_ms(&now, &qitem->start));
118 if(timeout_ms < 0) {
119 /* timed out while queued, place on receive queue */
120 Curl_llist_append(&tqueue->recvq, qitem, &qitem->node);
121 fn_event = tqueue->fn_event;
122 fn_user_data = tqueue->fn_user_data;
123 qitem = NULL;
124 e = Curl_llist_head(&tqueue->sendq);
125 continue;
126 }
127 else {
128 *pdescription = qitem->description;
129 *ptimeout_ms = timeout_ms;
130 break;
131 }
132 }
133 }
134 }
135 Curl_mutex_release(&tqueue->lock);
136 /* avoiding deadlocks */
137 if(fn_event)
138 fn_event(tqueue, CURL_THRDQ_EV_ITEM_DONE, fn_user_data);
139 return qitem;
142static void thrdq_tpool_return(void *item, void *user_data)
144 struct curl_thrdq *tqueue = user_data;
145 struct thrdq_item *qitem = item;
146 Curl_thrdq_ev_cb *fn_event = NULL;
147 void *fn_user_data = NULL;
149 if(!tqueue) {
150 thrdq_item_destroy(item);
151 return;
152 }
154 Curl_mutex_acquire(&tqueue->lock);
155 if(tqueue->aborted) {
156 thrdq_item_destroy(qitem);
157 }
158 else {
159 DEBUGASSERT(!Curl_node_llist(&qitem->node));
160 Curl_llist_append(&tqueue->recvq, qitem, &qitem->node);
161 fn_event = tqueue->fn_event;
162 fn_user_data = tqueue->fn_user_data;
163 }
164 Curl_mutex_release(&tqueue->lock);
165 /* avoiding deadlocks */
166 if(fn_event)
167 fn_event(tqueue, CURL_THRDQ_EV_ITEM_DONE, fn_user_data);
170static void thrdq_tpool_process(void *item)
172 struct thrdq_item *qitem = item;
173 qitem->fn_process(qitem->item);
176static void thrdq_unlink(struct curl_thrdq *tqueue, bool locked, bool join)
178 DEBUGASSERT(tqueue->aborted);
179 if(tqueue->tpool) {
180 if(locked)
181 Curl_mutex_release(&tqueue->lock);
182 Curl_thrdpool_destroy(tqueue->tpool, join);
183 tqueue->tpool = NULL;
184 if(locked)
185 Curl_mutex_acquire(&tqueue->lock);
186 }
188 Curl_llist_destroy(&tqueue->sendq, NULL);
189 Curl_llist_destroy(&tqueue->recvq, NULL);
190 Curl_cond_destroy(&tqueue->await);
191 if(locked)
192 Curl_mutex_release(&tqueue->lock);
193 Curl_mutex_destroy(&tqueue->lock);
194 curlx_free(tqueue);
197CURLcode Curl_thrdq_create(struct curl_thrdq **ptqueue,
198 const char *name,
199 uint32_t min_threads,
200 uint32_t max_threads,
201 uint32_t idle_time_ms,
202 Curl_thrdq_item_free_cb *fn_free,
203 Curl_thrdq_item_process_cb *fn_process,
204 Curl_thrdq_ev_cb *fn_event,
205 void *user_data)
207 struct curl_thrdq *tqueue;
208 CURLcode result = CURLE_OUT_OF_MEMORY;
209 DEBUGASSERT(name);
211 tqueue = curlx_calloc(1, sizeof(*tqueue));
212 if(!tqueue)
213 goto out;
215 Curl_mutex_init(&tqueue->lock);
216 Curl_cond_init(&tqueue->await);
217 Curl_llist_init(&tqueue->sendq, thrdq_item_list_dtor);
218 Curl_llist_init(&tqueue->recvq, thrdq_item_list_dtor);
219 tqueue->fn_free = fn_free;
220 tqueue->fn_process = fn_process;
221 tqueue->fn_event = fn_event;
222 tqueue->fn_user_data = user_data;
224 /* a const string that remains */
225 tqueue->name = name;
227 result = Curl_thrdpool_create(&tqueue->tpool, name,
228 min_threads, max_threads, idle_time_ms,
229 thrdq_tpool_take,
230 thrdq_tpool_process,
231 thrdq_tpool_return,
232 tqueue);
234out:
235 if(result && tqueue) {
236 tqueue->aborted = TRUE;
237 thrdq_unlink(tqueue, FALSE, TRUE);
238 tqueue = NULL;
239 }
240 *ptqueue = tqueue;
241 return result;
244void Curl_thrdq_destroy(struct curl_thrdq *tqueue, bool join)
246 Curl_mutex_acquire(&tqueue->lock);
247 DEBUGASSERT(!tqueue->aborted);
248 tqueue->aborted = TRUE;
249 thrdq_unlink(tqueue, TRUE, join);
252static uint32_t thrdq_get_signals(struct curl_thrdq *tqueue)
254 size_t qlen = Curl_llist_count(&tqueue->sendq);
255 return (qlen <= UINT32_MAX) ? (uint32_t)qlen : UINT32_MAX;
258CURLcode Curl_thrdq_send(struct curl_thrdq *tqueue, void *item,
259 const char *description, timediff_t timeout_ms)
261 struct thrdq_item *qitem;
262 CURLcode result = CURLE_OK;
263 uint32_t signals = 0;
265 Curl_mutex_acquire(&tqueue->lock);
266 if(tqueue->aborted) {
267 DEBUGASSERT(0);
268 result = CURLE_SEND_ERROR;
269 goto out;
270 }
271 if(timeout_ms < 0) {
272 result = CURLE_OPERATION_TIMEDOUT;
273 goto out;
274 }
276 qitem = thrdq_item_create(tqueue, item, description, timeout_ms);
277 if(!qitem) {
278 result = CURLE_OUT_OF_MEMORY;
279 goto out;
280 }
281 Curl_llist_append(&tqueue->sendq, qitem, &qitem->node);
282 signals = thrdq_get_signals(tqueue);
284out:
285 Curl_mutex_release(&tqueue->lock);
286 /* Signal thread pool unlocked to avoid deadlocks. Since we added
287 * item to the queue already, it might have been taken for processing
288 * already. Any error in signalling the pool cannot be reported to
289 * the caller since it needs to give up ownership of item. */
290 if(!result && signals)
291 (void)Curl_thrdpool_signal(tqueue->tpool, (uint32_t)signals);
292 return result;
295bool Curl_thrdq_check_started(struct curl_thrdq *tqueue)
297 size_t unprocessed;
299 Curl_mutex_acquire(&tqueue->lock);
300 unprocessed = tqueue->aborted ? 0 : Curl_llist_count(&tqueue->sendq);
301 Curl_mutex_release(&tqueue->lock);
302 return !unprocessed ||
303 !Curl_thrdpool_signal(tqueue->tpool, (uint32_t)unprocessed);
306CURLcode Curl_thrdq_recv(struct curl_thrdq *tqueue, void **pitem)
308 CURLcode result = CURLE_AGAIN;
309 struct Curl_llist_node *e;
310 uint32_t signals = 0;
312 *pitem = NULL;
313 Curl_mutex_acquire(&tqueue->lock);
314 if(tqueue->aborted) {
315 DEBUGASSERT(0);
316 result = CURLE_RECV_ERROR;
317 goto out;
318 }
320 e = Curl_llist_head(&tqueue->recvq);
321 if(e) {
322 struct thrdq_item *qitem = Curl_node_take_elem(e);
323 *pitem = qitem->item;
324 qitem->item = NULL;
325 thrdq_item_destroy(qitem);
326 result = CURLE_OK;
327 }
328 else
329 signals = thrdq_get_signals(tqueue);
331out:
332 Curl_mutex_release(&tqueue->lock);
333 /* Signal thread pool unlocked to avoid deadlocks. If items await
334 * processing while nothing was ready, make sure the pool has a
335 * thread to work on them. An earlier thread start may have failed,
336 * which `Curl_thrdq_send()` cannot report to its caller. Without
337 * this, such items would sit unprocessed until the next send. */
338 if(signals)
339 (void)Curl_thrdpool_signal(tqueue->tpool, (uint32_t)signals);
340 return result;
343static void thrdq_llist_clean_matches(struct Curl_llist *llist,
344 Curl_thrdq_item_match_cb *fn_match,
345 void *match_data)
347 struct Curl_llist_node *e, *n;
348 struct thrdq_item *qitem;
350 for(e = Curl_llist_head(llist); e; e = n) {
351 n = Curl_node_next(e);
352 qitem = Curl_node_elem(e);
353 if(fn_match(qitem->item, match_data))
354 Curl_node_remove(e);
355 }
358void Curl_thrdq_clear(struct curl_thrdq *tqueue,
359 Curl_thrdq_item_match_cb *fn_match,
360 void *match_data)
362 Curl_mutex_acquire(&tqueue->lock);
363 if(tqueue->aborted) {
364 DEBUGASSERT(0);
365 goto out;
366 }
367 thrdq_llist_clean_matches(&tqueue->sendq, fn_match, match_data);
368 thrdq_llist_clean_matches(&tqueue->recvq, fn_match, match_data);
369out:
370 Curl_mutex_release(&tqueue->lock);
373#ifdef UNITTESTS
374/* @unittest 3301 */
375UNITTEST CURLcode thrdq_await_done(struct curl_thrdq *tqueue,
376 uint32_t timeout_ms);
377UNITTEST CURLcode thrdq_await_done(struct curl_thrdq *tqueue,
378 uint32_t timeout_ms)
380 return Curl_thrdpool_await_idle(tqueue->tpool, timeout_ms);
382#endif
384CURLcode Curl_thrdq_set_props(struct curl_thrdq *tqueue,
385 uint32_t min_threads,
386 uint32_t max_threads,
387 uint32_t idle_time_ms)
389 CURLcode result;
390 uint32_t signals;
392 Curl_mutex_acquire(&tqueue->lock);
393 signals = thrdq_get_signals(tqueue);
394 Curl_mutex_release(&tqueue->lock);
396 result = Curl_thrdpool_set_props(tqueue->tpool, min_threads,
397 max_threads, idle_time_ms);
398 if(!result && signals)
399 result = Curl_thrdpool_signal(tqueue->tpool, (uint32_t)signals);
400 return result;
403#ifdef CURLVERBOSE
404void Curl_thrdq_trace(struct curl_thrdq *tqueue,
405 struct Curl_easy *data)
407 struct curl_trc_feat *feat = &Curl_trc_feat_threads;
408 if(Curl_trc_ft_is_verbose(data, feat)) {
409 struct Curl_llist_node *e;
410 struct thrdq_item *qitem;
412 Curl_thrdpool_trace(tqueue->tpool, data);
413 Curl_mutex_acquire(&tqueue->lock);
414 if(!Curl_llist_count(&tqueue->sendq) &&
415 !Curl_llist_count(&tqueue->recvq)) {
416 Curl_trc_feat_infof(data, feat, "[TQUEUE-%s] empty", tqueue->name);
417 }
418 for(e = Curl_llist_head(&tqueue->sendq); e; e = Curl_node_next(e)) {
419 qitem = Curl_node_elem(e);
420 Curl_trc_feat_infof(data, feat, "[TQUEUE-%s] in: %s",
421 tqueue->name, qitem->description);
422 }
423 for(e = Curl_llist_head(&tqueue->recvq); e; e = Curl_node_next(e)) {
424 qitem = Curl_node_elem(e);
425 Curl_trc_feat_infof(data, feat, "[TQUEUE-%s] out: %s",
426 tqueue->name, qitem->description);
427 }
428 Curl_mutex_release(&tqueue->lock);
429 }
431#endif
433#endif /* USE_THREADS */
Diagnostics4 git calls · 31 ms
CallStatusSizeTime031 ms
discover200152 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=152
git.server.agent=gitty/1.0
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch object-format
git.server.fetch=shallow filter sideband-all
git.protocol.version=2
git.pkt.frames=8
git.pkt.data=6
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=152
git.pkt.payload.max=34
ls-refs200149 B3 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=149
git.response.cache=UPDATING
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=149
git.pkt.payload.max=78
git.ls_refs.count=2
git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit4 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=293470
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=blob:none
git.fetch.filter.requested=tree:0
git.fetch.filter.fallback=blob:none
git.fetch.filter.learned=false
git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d
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=10
git.pkt.data=8
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=293470
git.pkt.payload.max=65516
git.sideband.band1.bytes=293354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=87
git.pack.bytes=293354
git.pack.ofs_delta=0
git.pack.ref_delta=1
git.pack.chain.max=1
git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0
git.pack.full=86
Open pack map
fetch2003.3 KiB · hit24 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=24
git.response.bytes=3405
git.response.cache=HIT
git.response.duration_ms=24
git.fetch.wants=b0094059052e3b6b665b33f01498f73acdd9b790
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=3
git.pkt.data=2
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=3405
git.pkt.payload.max=3384
git.sideband.band1.bytes=3383
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://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
Ref
refs/heads/master
Commit
31c911efe5652725e930f4d2e60c9060e5f6646d
cf-ray
a5e16be3dca04b2a
Request
571969bc…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format