master
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 "curlx/timeval.h"
31#include "curlx/strparse.h"
32#include "thrdpool.h"
33#ifdef CURLVERBOSE
34#include "curl_trc.h"
35#include "urldata.h"
36#endif
39struct thrdslot {
40 struct Curl_llist_node node;
41 struct curl_thrdpool *tpool;
42 curl_thread_t thread;
43 curl_cond_t await;
44 struct curltime starttime;
45 const char *work_description;
46 timediff_t work_timeout_ms;
47 uint32_t id;
48 BIT(running);
49 BIT(idle);
50};
52struct curl_thrdpool {
53 const char *name;
54 uint64_t refcount;
55 curl_mutex_t lock;
56 curl_cond_t await;
57 struct Curl_llist slots;
58 struct Curl_llist zombies;
59 Curl_thrdpool_take_item_cb *fn_take;
60 Curl_thrdpool_process_item_cb *fn_process;
61 Curl_thrdpool_return_item_cb *fn_return;
62 void *fn_user_data;
63 CURLcode fatal_err;
64 uint32_t min_threads;
65 uint32_t max_threads;
66 uint32_t idle_time_ms;
67 uint32_t next_id;
68#ifdef DEBUGBUILD
69 int dbg_fail_starts; /* fail this many thread starts */
70#endif
71 BIT(aborted);
72 BIT(detached);
73};
75static void thrdpool_join_zombies(struct curl_thrdpool *tpool);
76static bool thrdpool_unlink(struct curl_thrdpool *tpool, bool locked);
78static void thrdslot_destroy(struct thrdslot *tslot)
79{
80 DEBUGASSERT(tslot->thread == curl_thread_t_null);
81 DEBUGASSERT(!tslot->running);
82 Curl_cond_destroy(&tslot->await);
83 curlx_free(tslot);
84}
86static void thrdslot_done(struct thrdslot *tslot)
87{
88 struct curl_thrdpool *tpool = tslot->tpool;
90 DEBUGASSERT(Curl_node_llist(&tslot->node) == &tpool->slots);
91 Curl_node_remove(&tslot->node);
92 tslot->running = FALSE;
93 Curl_llist_append(&tpool->zombies, tslot, &tslot->node);
94 Curl_cond_signal(&tpool->await);
95}
97static CURL_THREAD_RETURN_T CURL_STDCALL thrdslot_run(void *arg)
98{
99 struct thrdslot *tslot = arg;
100 struct curl_thrdpool *tpool = tslot->tpool;
101 void *item;
103 Curl_mutex_acquire(&tpool->lock);
104 DEBUGASSERT(Curl_node_llist(&tslot->node) == &tpool->slots);
105 for(;;) {
106 while(!tpool->aborted) {
107 tslot->work_description = NULL;
108 tslot->work_timeout_ms = 0;
109 item = tpool->fn_take(tpool->fn_user_data, &tslot->work_description,
110 &tslot->work_timeout_ms);
111 if(!item)
112 break;
113 tslot->starttime = curlx_now();
114 tslot->idle = FALSE;
115 Curl_mutex_release(&tpool->lock);
117 tpool->fn_process(item);
119 Curl_mutex_acquire(&tpool->lock);
120 tslot->work_description = NULL;
121 tpool->fn_return(item, tpool->aborted ? NULL : tpool->fn_user_data);
122 }
124 if(tpool->aborted ||
125 (Curl_llist_count(&tpool->slots) > tpool->max_threads))
126 goto out;
128 tslot->idle = TRUE;
129 tslot->starttime = curlx_now();
130 thrdpool_join_zombies(tpool);
131 Curl_cond_signal(&tpool->await);
132 /* Only wait with idle timeout when we are above the minimum
133 * number of threads. Otherwise short idle timeouts will keep
134 * on activating threads that have no means to shut down. */
135 if((tpool->idle_time_ms > 0) &&
136 (Curl_llist_count(&tpool->slots) > tpool->min_threads)) {
137 CURLcode result = Curl_cond_timedwait(&tslot->await, &tpool->lock,
138 tpool->idle_time_ms);
139 if((result == CURLE_OPERATION_TIMEDOUT) &&
140 (Curl_llist_count(&tpool->slots) > tpool->min_threads)) {
141 goto out;
142 }
143 }
144 else {
145 Curl_cond_wait(&tslot->await, &tpool->lock);
146 }
147 }
149out:
150 thrdslot_done(tslot);
151 if(!thrdpool_unlink(tslot->tpool, TRUE)) {
152 /* tpool not destroyed */
153 Curl_mutex_release(&tpool->lock);
154 }
155 return 0;
156}
158static CURLcode thrdslot_start(struct curl_thrdpool *tpool)
159{
160 struct thrdslot *tslot;
161 CURLcode result = CURLE_OUT_OF_MEMORY;
163 tslot = curlx_calloc(1, sizeof(*tslot));
164 if(!tslot)
165 goto out;
166 tslot->id = tpool->next_id++;
167 tslot->tpool = tpool;
168 tslot->thread = curl_thread_t_null;
169 Curl_cond_init(&tslot->await);
171 tpool->refcount++;
172 tslot->running = TRUE;
173#ifdef DEBUGBUILD
174 if(tpool->dbg_fail_starts > 0) {
175 --tpool->dbg_fail_starts;
176 tslot->thread = curl_thread_t_null;
177 }
178 else
179#endif
180 tslot->thread = Curl_thread_create(thrdslot_run, tslot);
181 if(tslot->thread == curl_thread_t_null) { /* never started */
182 tslot->running = FALSE;
183 thrdpool_unlink(tpool, TRUE);
184 result = CURLE_FAILED_INIT;
185 goto out;
186 }
188 Curl_llist_append(&tpool->slots, tslot, &tslot->node);
189 tslot = NULL;
190 result = CURLE_OK;
192out:
193 if(tslot)
194 thrdslot_destroy(tslot);
195 return result;
196}
198static void thrdpool_wake_all(struct curl_thrdpool *tpool)
199{
200 struct Curl_llist_node *e;
201 for(e = Curl_llist_head(&tpool->slots); e; e = Curl_node_next(e)) {
202 struct thrdslot *tslot = Curl_node_elem(e);
203 Curl_cond_signal(&tslot->await);
204 }
205}
207static void thrdpool_join_zombies(struct curl_thrdpool *tpool)
208{
209 struct Curl_llist_node *e;
211 for(e = Curl_llist_head(&tpool->zombies); e;
212 e = Curl_llist_head(&tpool->zombies)) {
213 struct thrdslot *tslot = Curl_node_elem(e);
215 Curl_node_remove(&tslot->node);
216 if(tslot->thread != curl_thread_t_null) {
217 Curl_mutex_release(&tpool->lock);
218 Curl_thread_join(&tslot->thread);
219 Curl_mutex_acquire(&tpool->lock);
220 tslot->thread = curl_thread_t_null;
221 }
222 thrdslot_destroy(tslot);
223 }
224}
226static bool thrdpool_unlink(struct curl_thrdpool *tpool, bool locked)
227{
228 DEBUGASSERT(tpool->refcount);
229 if(tpool->refcount)
230 tpool->refcount--;
231 if(tpool->refcount)
232 return FALSE;
234 /* no more references, free */
235 DEBUGASSERT(tpool->aborted);
236 thrdpool_join_zombies(tpool);
237 if(locked)
238 Curl_mutex_release(&tpool->lock);
239 Curl_cond_destroy(&tpool->await);
240 Curl_mutex_destroy(&tpool->lock);
241 curlx_free(tpool);
242 return TRUE;
243}
245static CURLcode thrdpool_signal(struct curl_thrdpool *tpool,
246 uint32_t nthreads)
247{
248 struct Curl_llist_node *e, *n;
249 CURLcode result = CURLE_OK;
251 DEBUGASSERT(!tpool->aborted);
252 thrdpool_join_zombies(tpool);
254 for(e = Curl_llist_head(&tpool->slots); e && nthreads; e = n) {
255 struct thrdslot *tslot = Curl_node_elem(e);
256 n = Curl_node_next(e);
257 if(tslot->idle) {
258 Curl_cond_signal(&tslot->await);
259 --nthreads;
260 }
261 else if(!tslot->starttime.tv_sec && !tslot->starttime.tv_usec) {
262 /* starting thread, queries for work soon. */
263 --nthreads;
264 }
265 }
267 while(nthreads && !result &&
268 Curl_llist_count(&tpool->slots) < tpool->max_threads) {
269 result = thrdslot_start(tpool);
270 if(result)
271 break;
272 --nthreads;
273 }
275 return result;
276}
278CURLcode Curl_thrdpool_set_props(struct curl_thrdpool *tpool,
279 uint32_t min_threads,
280 uint32_t max_threads,
281 uint32_t idle_time_ms)
282{
283 CURLcode result = CURLE_OK;
284 size_t running;
286 if(!max_threads || (min_threads > max_threads))
287 return CURLE_BAD_FUNCTION_ARGUMENT;
289 Curl_mutex_acquire(&tpool->lock);
290 tpool->min_threads = min_threads;
291 tpool->max_threads = max_threads;
292 tpool->idle_time_ms = idle_time_ms;
293 running = Curl_llist_count(&tpool->slots);
294 if(tpool->min_threads > running) {
295 result = thrdpool_signal(tpool, tpool->min_threads - (uint32_t)running);
296 }
297 Curl_mutex_release(&tpool->lock);
299 return result;
300}
302CURLcode Curl_thrdpool_create(struct curl_thrdpool **ptpool,
303 const char *name,
304 uint32_t min_threads,
305 uint32_t max_threads,
306 uint32_t idle_time_ms,
307 Curl_thrdpool_take_item_cb *fn_take,
308 Curl_thrdpool_process_item_cb *fn_process,
309 Curl_thrdpool_return_item_cb *fn_return,
310 void *user_data)
311{
312 struct curl_thrdpool *tpool;
313 CURLcode result = CURLE_OUT_OF_MEMORY;
314 DEBUGASSERT(name);
316 tpool = curlx_calloc(1, sizeof(*tpool));
317 if(!tpool)
318 goto out;
319 tpool->refcount = 1;
321 Curl_mutex_init(&tpool->lock);
322 Curl_cond_init(&tpool->await);
323 Curl_llist_init(&tpool->slots, NULL);
324 Curl_llist_init(&tpool->zombies, NULL);
325 tpool->fn_take = fn_take;
326 tpool->fn_process = fn_process;
327 tpool->fn_return = fn_return;
328 tpool->fn_user_data = user_data;
330 /* a const string that remains */
331 tpool->name = name;
333#ifdef DEBUGBUILD
334 {
335 const char *p = getenv("CURL_DBG_THRDPOOL_FAIL_STARTS");
336 if(p) {
337 curl_off_t l;
338 if(!curlx_str_number(&p, &l, INT_MAX))
339 tpool->dbg_fail_starts = (int)l;
340 }
341 }
342#endif
344 result = Curl_thrdpool_set_props(tpool, min_threads, max_threads,
345 idle_time_ms);
347out:
348 if(result && tpool) {
349 tpool->aborted = TRUE;
350 thrdpool_unlink(tpool, FALSE);
351 tpool = NULL;
352 }
353 *ptpool = tpool;
354 return result;
355}
357void Curl_thrdpool_destroy(struct curl_thrdpool *tpool, bool join)
358{
359 Curl_mutex_acquire(&tpool->lock);
361 tpool->aborted = TRUE;
363 while(join && Curl_llist_count(&tpool->slots)) {
364 thrdpool_wake_all(tpool);
365 Curl_cond_wait(&tpool->await, &tpool->lock);
366 }
368 thrdpool_join_zombies(tpool);
370 /* detach all still running threads */
371 if(Curl_llist_count(&tpool->slots)) {
372 struct Curl_llist_node *e;
373 for(e = Curl_llist_head(&tpool->slots); e; e = Curl_node_next(e)) {
374 struct thrdslot *tslot = Curl_node_elem(e);
375 if(tslot->thread != curl_thread_t_null)
376 Curl_thread_destroy(&tslot->thread);
377 }
378 tpool->detached = TRUE;
379 }
381 if(!thrdpool_unlink(tpool, TRUE)) {
382 /* tpool not destroyed */
383 Curl_mutex_release(&tpool->lock);
384 }
385}
387CURLcode Curl_thrdpool_signal(struct curl_thrdpool *tpool, uint32_t nthreads)
388{
389 CURLcode result;
391 Curl_mutex_acquire(&tpool->lock);
392 result = thrdpool_signal(tpool, nthreads);
393 Curl_mutex_release(&tpool->lock);
394 return result;
395}
397static bool thrdpool_all_idle(struct curl_thrdpool *tpool)
398{
399 struct Curl_llist_node *e;
400 for(e = Curl_llist_head(&tpool->slots); e; e = Curl_node_next(e)) {
401 struct thrdslot *tslot = Curl_node_elem(e);
402 if(!tslot->idle)
403 return FALSE;
404 }
405 return TRUE;
406}
408CURLcode Curl_thrdpool_await_idle(struct curl_thrdpool *tpool,
409 uint32_t timeout_ms)
410{
411 CURLcode result = CURLE_OK;
412 struct curltime end = { 0 };
414 Curl_mutex_acquire(&tpool->lock);
415 DEBUGASSERT(!tpool->aborted);
416 if(tpool->aborted) {
417 result = CURLE_FAILED_INIT;
418 goto out;
419 }
421 while(!thrdpool_all_idle(tpool)) {
422 if(timeout_ms) {
423 timediff_t remain_ms;
424 CURLcode r;
426 if(!end.tv_sec && !end.tv_usec) {
427 end = curlx_now();
428 end.tv_sec += (time_t)(timeout_ms / 1000);
429 end.tv_usec += (int)(timeout_ms % 1000) * 1000;
430 if(end.tv_usec >= 1000000) {
431 end.tv_sec++;
432 end.tv_usec -= 1000000;
433 }
434 }
435 remain_ms = curlx_timediff_ms(curlx_now(), end);
436 if(remain_ms <= 0)
437 r = CURLE_OPERATION_TIMEDOUT;
438 else
439 r = Curl_cond_timedwait(&tpool->await, &tpool->lock,
440 (uint32_t)remain_ms);
441 if(r == CURLE_OPERATION_TIMEDOUT) {
442 result = r;
443 break;
444 }
445 }
446 else {
447 Curl_cond_wait(&tpool->await, &tpool->lock);
448 }
449 }
451out:
452 thrdpool_join_zombies(tpool);
453 Curl_mutex_release(&tpool->lock);
454 return result;
455}
457#ifdef CURLVERBOSE
458void Curl_thrdpool_trace(struct curl_thrdpool *tpool,
459 struct Curl_easy *data)
460{
461 struct curl_trc_feat *feat = &Curl_trc_feat_threads;
462 if(Curl_trc_ft_is_verbose(data, feat)) {
463 struct Curl_llist_node *e;
464 struct curltime now = curlx_now();
466 Curl_mutex_acquire(&tpool->lock);
467 if(!Curl_llist_count(&tpool->slots)) {
468 Curl_trc_feat_infof(data, feat, "[TPOOL-%s] no threads running",
469 tpool->name);
470 }
471 for(e = Curl_llist_head(&tpool->slots); e; e = Curl_node_next(e)) {
472 struct thrdslot *tslot = Curl_node_elem(e);
473 timediff_t elapsed_ms = curlx_ptimediff_ms(&now, &tslot->starttime);
474 if(!tslot->running) {
475 Curl_trc_feat_infof(data, feat, "[TPOOL-%s] [%u]: not running",
476 tpool->name, tslot->id);
477 }
478 else if(!tslot->starttime.tv_sec && !tslot->starttime.tv_usec) {
479 Curl_trc_feat_infof(data, feat, "[TPOOL-%s] [%u]: starting...",
480 tpool->name, tslot->id);
481 }
482 else if(tslot->idle) {
483 Curl_trc_feat_infof(data, feat, "[TPOOL-%s] [%u]: idle for %"
484 FMT_TIMEDIFF_T "ms",
485 tpool->name, tslot->id, elapsed_ms);
486 }
487 else {
488 timediff_t remain_ms = tslot->work_timeout_ms ?
489 (tslot->work_timeout_ms - elapsed_ms) : 0;
490 Curl_trc_feat_infof(data, feat, "[TPOOL-%s] [%u]: busy %"
491 FMT_TIMEDIFF_T "ms, timeout in %" FMT_TIMEDIFF_T
492 "ms: %s",
493 tpool->name, tslot->id, elapsed_ms, remain_ms,
494 tslot->work_description);
495 }
496 }
497 Curl_mutex_release(&tpool->lock);
498 }
499}
500#endif
502#endif /* USE_THREADS */
Diagnostics4 git calls · 30 ms
CallStatusSizeTime030 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 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 B · hit3 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=HIT 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 · hit3 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=293470 git.response.cache=HIT git.response.duration_ms=3 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=86Open pack map
fetch2003.8 KiB · hit24 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3919 git.response.cache=HIT git.response.duration_ms=24 git.fetch.wants=900fd1cfc09d3eff12e7d3ffda0eda7b75c664c1 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=3919 git.pkt.payload.max=3898 git.sideband.band1.bytes=3897 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
- a5e9e380deccf3c9
- Request
- 83e36919…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format