Artifactspublic-v0

curl

master
ply4 git calls36 ms290 KiB3 cachedprotocol v2
392 lines11 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#include "urldata.h"
27#include "splay.h"
29static struct Curl_tree *splay(timediff_t key,
30 struct Curl_tree *root);
32#ifdef UNITTESTS
33uint32_t Curl_splayget(struct Curl_tree *node)
35 DEBUGASSERT(node);
36 return node->id;
39#endif
41void Curl_timeouts_init(struct Curl_timeouts *timeouts,
42 const struct curltime *ptime_base)
44 struct curltime time_base = ptime_base ? *ptime_base : curlx_now();
45 timeouts->tree = NULL;
46 timeouts->time_base_sec = time_base.tv_sec;
49bool Curl_timeouts_has(struct Curl_easy *data)
51 struct Curl_tree *node = data ? &data->state.timeouts.splaynode : NULL;
52 return node && node->registered;
55timediff_t Curl_timeouts_offset_us(struct Curl_timeouts *timeouts,
56 const struct curltime *pts)
58 struct curltime time_base;
59 time_base.tv_sec = timeouts->time_base_sec;
60 time_base.tv_usec = 0;
61 return curlx_ptimediff_us(pts, &time_base);
64int Curl_timeouts_next_ms(struct Curl_timeouts *timeouts,
65 const struct curltime *pnow,
66 timediff_t *pexpire_offset_us,
67 uint32_t *pmid)
69 if(timeouts->tree) { /* splay the lowest key to the root */
70 timeouts->tree = splay(TIMEDIFF_T_MIN, timeouts->tree);
71 }
73 if(timeouts->tree) {
74 timediff_t elapsed_us = Curl_timeouts_offset_us(timeouts, pnow);
75 timediff_t delta_us = timeouts->tree->key - elapsed_us;
76 if(pmid)
77 *pmid = timeouts->tree->id;
78 if(pexpire_offset_us)
79 *pexpire_offset_us = timeouts->tree->key;
80 if(delta_us > 0) { /* expires in the future */
81 timediff_t ms = curlx_us_to_ceil_ms(delta_us);
82 return (ms > INT_MAX) ? INT_MAX : (int)ms;
83 }
84 else /* has expired */
85 return 0;
86 }
87 if(pmid)
88 *pmid = UINT32_MAX;
89 if(pexpire_offset_us)
90 *pexpire_offset_us = 0;
91 return -1;
94/*
95 * Splay using the key i (which may or may not be in the tree).
96 * This rotates the tree, so:
97 * - root->smaller has all nodes smaller than `key`
98 * - root->larger has all nodes larger than `key`
99 * - root->key may equal `key` or not
100 * <https://en.wikipedia.org/wiki/Splay_tree>
101 */
102struct Curl_tree *splay(timediff_t key,
103 struct Curl_tree *root)
105 struct Curl_tree N, *l, *r, *y;
107 if(!root)
108 return NULL;
109 N.smaller = N.larger = NULL;
110 l = r = &N;
112 for(;;) {
113 if(key < root->key) {
114 /* key is somewhere in root->smaller branch */
115 if(!root->smaller) /* which is empty, done */
116 break;
117 if(key < root->smaller->key) {
118 /* key is somewhere in root->smaller->smaller, make a "Zig step" */
119 y = root->smaller;
120 root->smaller = y->larger;
121 y->larger = root;
122 root = y;
123 if(!root->smaller)
124 break;
125 }
126 /* Making root->smaller the new root, the old root is no longer
127 * referenced. Remember it in the N tree's `r`ight/larger side.
128 * Everything in old root is smaller than what the right side
129 * of N already has, so it gets added to r->smaller. */
130 r->smaller = root;
131 r = root;
132 root = root->smaller;
133 }
134 else if(key > root->key) {
135 /* key is somewhere in root->larger branch */
136 if(!root->larger) /* which is empty, done */
137 break;
138 if(key > root->larger->key) {
139 /* key is somewhere in root->larger->larger, make a "Zig step" */
140 y = root->larger;
141 root->larger = y->smaller;
142 y->smaller = root;
143 root = y;
144 if(!root->larger)
145 break;
146 }
147 /* Making root->larger the new root, the old root is no longer
148 * referenced. Remember it in the N tree's `l`eft/smaller side.
149 * Everything in old root is larger than what the left side
150 * of N already has, so it gets added to l->larger. */
151 l->larger = root;
152 l = root;
153 root = root->larger;
154 }
155 else /* exact match, root is key, done */
156 break;
157 }
159 /* Put it all together again.
160 * root->smaller has everything larger than current `l`.
161 * root->larger has everything smaller than current `r`. */
162 l->larger = root->smaller;
163 r->smaller = root->larger;
164 root->smaller = N.larger;
165 root->larger = N.smaller;
167 return root;
170/* Insert key i into the tree t. Return a pointer to the resulting tree or
171 * NULL if something went wrong.
172 *
173 * @unittest 1309
174 */
175UNITTEST struct Curl_tree *splayinsert(timediff_t key,
176 struct Curl_tree *root,
177 struct Curl_tree *node,
178 uint32_t id);
179UNITTEST struct Curl_tree *splayinsert(timediff_t key,
180 struct Curl_tree *root,
181 struct Curl_tree *node,
182 uint32_t id)
184 DEBUGASSERT(node);
186 node->key = key;
187 node->id = id;
188 node->same = NULL;
189 node->registered = TRUE;
190 if(root) {
191 root = splay(key, root);
192 DEBUGASSERT(root);
193 if(key == root->key) {
194 /* There already exists a node in the tree with the same key.
195 Append the new node to the `same` list. */
196 struct Curl_tree **panchor = &root->same;
197 while(*panchor)
198 panchor = &(*panchor)->same;
199 *panchor = node;
200 return root; /* the root node always stays the same */
201 }
202 }
204 /* node becomes the new root. Insert old root as sub-branch. */
205 if(!root) {
206 node->smaller = node->larger = NULL;
207 }
208 else if(key < root->key) {
209 node->smaller = root->smaller;
210 node->larger = root;
211 root->smaller = NULL;
212 }
213 else {
214 node->larger = root->larger;
215 node->smaller = root;
216 root->larger = NULL;
217 }
219 return node;
222void Curl_timeouts_add(struct Curl_timeouts *timeouts,
223 struct Curl_easy *data,
224 timediff_t offset_us)
226 struct Curl_tree *node = &data->state.timeouts.splaynode;
227 DEBUGASSERT(!node->registered);
228 timeouts->tree = splayinsert(offset_us, timeouts->tree,
229 node, data->mid);
232/* Finds and deletes the best-fit node from the tree. Return a pointer to the
233 resulting tree. best-fit means the smallest node if it is not larger than
234 the key
236 @unittest 1309
237*/
238UNITTEST struct Curl_tree *splaygetbest(timediff_t key,
239 struct Curl_tree *root,
240 struct Curl_tree **removed);
241UNITTEST struct Curl_tree *splaygetbest(timediff_t key,
242 struct Curl_tree *root,
243 struct Curl_tree **removed)
245 struct Curl_tree *x;
247 if(!root) {
248 *removed = NULL; /* none removed since there was no root */
249 return NULL;
250 }
252 /* find smallest */
253 root = splay(TIMEDIFF_T_MIN, root);
254 DEBUGASSERT(root);
255 if(key < root->key) {
256 /* even the smallest is too big */
257 *removed = NULL;
258 return root;
259 }
261 /* FIRST! Check if there is a list with identical keys */
262 if(root->same) {
263 x = root->same;
264 DEBUGASSERT(x->key == root->key);
265 /* 'x' becomes the new root node */
266 x->larger = root->larger;
267 x->smaller = root->smaller;
268 root->same = NULL;
269 root->registered = FALSE;
270 *removed = root;
271 return x; /* new root */
272 }
274 /* we splayed the tree to the smallest element, there is no smaller */
275 x = root->larger;
276 root->registered = FALSE;
277 *removed = root;
279 return x;
282bool Curl_timeouts_remove_expired(struct Curl_timeouts *timeouts,
283 const struct curltime *ts,
284 uint32_t *pmid)
286 if(timeouts->tree) {
287 struct Curl_tree *t = NULL;
288 timediff_t elapsed_us = Curl_timeouts_offset_us(timeouts, ts);
289 timeouts->tree = splaygetbest(elapsed_us, timeouts->tree, &t);
290 if(t) {
291 *pmid = t->id;
292 return TRUE;
293 }
294 }
295 *pmid = UINT32_MAX;
296 return FALSE;
299/* Deletes the node we point out from the tree if it is there. Stores a
300 * pointer to the new resulting tree in 'newroot'.
301 *
302 * Returns zero on success and non-zero on errors!
303 * When returning error, it does not touch the 'newroot' pointer.
304 *
305 * NOTE: when the last node of the tree is removed, there is no tree left so
306 * 'newroot' will be made to point to NULL.
307 *
308 * @unittest 1309
309 */
310UNITTEST int splayremove(struct Curl_tree *root,
311 struct Curl_tree *removenode,
312 struct Curl_tree **newroot);
313UNITTEST int splayremove(struct Curl_tree *root,
314 struct Curl_tree *removenode,
315 struct Curl_tree **newroot)
317 struct Curl_tree *x;
319 if(!root)
320 return 1;
322 DEBUGASSERT(removenode);
323 if(!removenode->registered)
324 return 2;
326 root = splay(removenode->key, root);
327 DEBUGASSERT(root);
329 /* First make sure that we got the same root key as the one we want
330 to remove, as otherwise we might be trying to remove a node that
331 is not actually in the tree. */
332 if(root->key != removenode->key) {
333 DEBUGASSERT(0);
334 return 2;
335 }
337 if(root != removenode) {
338 /* Should be in the root->same list then */
339 struct Curl_tree **panchor;
340 for(panchor = &root->same; *panchor; panchor = &(*panchor)->same) {
341 if(*panchor == removenode) {
342 *panchor = removenode->same;
343 removenode->same = NULL;
344 removenode->registered = FALSE;
345 *newroot = root;
346 return 0;
347 }
348 }
349 /* not found in same list, error */
350 DEBUGASSERT(0);
351 return 2;
352 }
353 /* removing the root node */
354 if(root->same) {
355 /* 'x' is the new root node, we make it use the root node's
356 smaller/larger links */
357 x = root->same;
358 x->larger = root->larger;
359 x->smaller = root->smaller;
360 root->same = NULL;
361 }
362 else {
363 /* Remove the root node */
364 if(!root->smaller)
365 x = root->larger;
366 else {
367 x = splay(removenode->key, root->smaller);
368 DEBUGASSERT(x);
369 x->larger = root->larger;
370 }
371 }
372 removenode->registered = FALSE;
373 *newroot = x; /* return new root */
374 return 0;
377bool Curl_timeouts_remove(struct Curl_timeouts *timeouts,
378 struct Curl_easy *data)
380 struct Curl_tree *node = &data->state.timeouts.splaynode;
381 if(node->registered) {
382 int rc = splayremove(timeouts->tree, node, &timeouts->tree);
383#ifdef DEBUGBUILD
384 if(rc)
385 curl_mfprintf(stderr, "Internal error removing splay node = %d\n", rc);
386#else
387 (void)rc;
388#endif
389 return TRUE;
390 }
391 return FALSE;
Diagnostics4 git calls · 36 ms
CallStatusSizeTime036 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=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 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 · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=29
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.4 KiB · hit29 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=3524
git.response.cache=HIT
git.response.duration_ms=29
git.fetch.wants=df3734d53c5b98fc26a94f3567154b5edf0e64a7
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=3524
git.pkt.payload.max=3503
git.sideband.band1.bytes=3502
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
a5e1800d0faf3014
Request
d7b01639…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format