Artifactspublic-v0

curl

master
ply4 git calls42 ms291 KiB2 cachedprotocol v2
475 lines15 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.
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.
18# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19# KIND, either express or implied.
21# SPDX-License-Identifier: curl
23###########################################################################
25package processhelp;
27use strict;
28use warnings;
30use Time::HiRes;
32use pathhelp qw(
33 os_is_win
34 );
36my $has_win32_process;
38BEGIN {
39 use base qw(Exporter);
41 our @EXPORT = qw(
42 pidfromfile
43 pidexists
44 pidwait
45 processexists
46 killpid
47 killsockfilters
48 killallsockfilters
49 set_advisor_read_lock
50 clear_advisor_read_lock
51 );
53 if(os_is_win() && $^O ne 'MSWin32') {
54 $has_win32_process = eval {
55 no warnings "all";
56 # https://metacpan.org/pod/Win32::Process
57 require Win32::Process;
58 # https://metacpan.org/pod/Win32::Process::List
59 require Win32::Process::List;
60 };
61 }
62 else {
63 $has_win32_process = 0;
64 }
67use serverhelp qw(
68 servername_id
69 mainsockf_pidfilename
70 datasockf_pidfilename
71 );
73use globalconfig qw(
74 $dev_null
75 );
77#######################################################################
78# pidfromfile returns the pid stored in the given pidfile. The value
79# of the returned pid is never a negative value. It is zero on any file
80# related error or if a pid can not be extracted from the given file.
82sub pidfromfile {
83 my $pidfile = $_[0];
84 my $timeout_sec = $_[1];
85 my $pid = 0;
86 my $waits = 0;
87 # wait at max 15 seconds for the file to exist and have valid content
88 while(!$pid && ($waits <= ($timeout_sec * 10))) {
89 if(-f $pidfile && -s $pidfile && open(my $pidfh, "<", $pidfile)) {
90 $pid = 0 + <$pidfh>;
91 close($pidfh);
92 $pid = 0 if($pid < 0);
93 }
94 Time::HiRes::sleep(0.1) unless $pid || !$timeout_sec;
95 ++$waits;
96 }
97 return $pid;
100#######################################################################
101# return Cygwin pid from virtual pid
103sub winpid_to_pid {
104 my $vpid = $_[0];
105 if(($^O eq 'cygwin' || $^O eq 'msys') && $vpid > 4194304) {
106 my $pid = Cygwin::winpid_to_pid($vpid - 4194304);
107 if($pid) {
108 return $pid;
109 }
110 else {
111 return $vpid
112 }
113 }
114 return $vpid;
117#######################################################################
118# pidexists checks if a process with a given pid exists and is alive.
119# This returns the positive pid if the process exists and is alive.
120# This returns the negative pid if the process exists differently.
121# This returns 0 if the process could not be found.
123sub pidexists {
124 my $pid = $_[0];
126 if($pid > 0) {
127 # verify if currently existing Windows process
128 $pid = winpid_to_pid($pid);
129 if($pid > 4194304 && os_is_win()) {
130 $pid -= 4194304;
131 if($^O ne 'MSWin32') {
132 if($has_win32_process) {
133 my %processes = Win32::Process::List->new()->GetProcesses();
134 if(exists $processes{$pid}) {
135 return -$pid;
136 }
137 }
138 else {
139 my $filter = "PID eq $pid";
140 # https://ss64.com/nt/tasklist.html
141 my $result = qx(tasklist -fi \"$filter\" 2>$dev_null);
142 if(index($result, $pid) != -1) {
143 return -$pid;
144 }
145 }
146 return 0;
147 }
148 }
150 # verify if currently existing and alive
151 if(kill(0, $pid)) {
152 return $pid;
153 }
154 }
156 return 0;
159#######################################################################
160# terminate or kill a process, pass "TERM" or "KILL" for $method.
162sub pid_term_or_kill {
163 my ($pid, $method) = @_;
165 if($pid > 0) {
166 # request the process to quit
167 $pid = winpid_to_pid($pid);
168 if($pid > 4194304 && os_is_win()) {
169 $pid -= 4194304;
170 if($^O ne 'MSWin32') {
171 if($has_win32_process) {
172 Win32::Process::KillProcess($pid, 0);
173 }
174 else {
175 my $pwsh;
176 # https://ss64.com/nt/tasklist.html
177 my $result = qx(tasklist -v -fo list -fi "PID eq $pid" 2>&1);
178 $result =~ s/\r//g;
179 $result =~ s/\n/ | /g;
180 print "Task info (via tasklist) for $pid before taskkill: '$result'\n";
182 $pwsh = "Get-CimInstance -ClassName Win32_Process -Filter 'ProcessId=$pid' | " .
183 "Select ProcessId, ParentProcessId, Name, CommandLine | " .
184 "Out-String -Width 2048";
185 $result = qx(powershell -Command "$pwsh");
186 $result =~ s/\r//g;
187 print "Task info (via Get-CimInstance) for $pid before taskkill:\n";
188 print "$result\n";
190 $pwsh = "Get-CimInstance -ClassName Win32_Process -Filter 'ParentProcessId=$pid' | " .
191 "Select ProcessId, ParentProcessId, Name, CommandLine | " .
192 "Out-String -Width 2048";
193 $result = qx(powershell -Command "$pwsh");
194 $result =~ s/\r//g;
195 print "Task child processes for $pid before taskkill:\n";
196 print "$result\n";
198 if(!$ENV{'CURL_TEST_NO_TASKKILL'}) {
199 # https://ss64.com/nt/taskkill.html
200 my $cmd;
201 if($ENV{'CURL_TEST_NO_TASKKILL_TREE'}) {
202 $cmd = "taskkill -f -pid $pid >$dev_null 2>&1";
203 }
204 else {
205 $cmd = "taskkill -f -t -pid $pid >$dev_null 2>&1";
206 }
207 print "Executing: '$cmd'\n";
208 system($cmd);
209 }
210 else {
211 print "taskkill disabled via CURL_TEST_NO_TASKKILL.\n";
212 }
213 }
214 return;
215 }
216 }
218 # signal the process to terminate
219 kill($method, $pid);
220 }
223#######################################################################
224# pidterm asks the process with a given pid to terminate gracefully.
226sub pidterm {
227 my $pid = $_[0];
228 pid_term_or_kill($pid, "TERM");
231#######################################################################
232# pidkill kills the process with a given pid mercilessly and forcefully.
234sub pidkill {
235 my $pid = $_[0];
236 pid_term_or_kill($pid, "KILL");
239#######################################################################
240# pidwait waits for the process with a given pid to be terminated.
242sub pidwait {
243 my $pid = $_[0];
244 my $flags = $_[1];
246 $pid = winpid_to_pid($pid);
247 # check if the process exists
248 if($pid > 4194304 && os_is_win()) {
249 if($flags == &WNOHANG) {
250 return pidexists($pid)?0:$pid;
251 }
252 my $start = time;
253 my $warn_at = 5;
254 while(pidexists($pid)) {
255 if(time - $start > $warn_at) {
256 print "pidwait: still waiting for PID ", $pid, "\n";
257 $warn_at += 5;
258 if($warn_at > 20) {
259 print "pidwait: giving up waiting for PID ", $pid, "\n";
260 last;
261 }
262 }
263 Time::HiRes::sleep(0.2);
264 }
265 return $pid;
266 }
268 # wait on the process to terminate
269 return waitpid($pid, $flags);
272#######################################################################
273# processexists checks if a process with the pid stored in the given
274# pidfile exists and is alive. This returns 0 on any file related
275# error or if a pid can not be extracted from the given file. When a
276# process with the same pid as the one extracted from the given file
277# is currently alive this returns that positive pid. Otherwise, when
278# the process is not alive, it returns the negative value of the pid.
280sub processexists {
281 use POSIX ":sys_wait_h";
282 my $pidfile = $_[0];
284 # fetch pid from pidfile
285 my $pid = pidfromfile($pidfile, 0);
287 if($pid > 0) {
288 # verify if currently alive
289 if(pidexists($pid)) {
290 return $pid;
291 }
292 else {
293 # get rid of the certainly invalid pidfile
294 unlink($pidfile) if($pid == pidfromfile($pidfile, 0));
295 # reap its dead children, if not done yet
296 pidwait($pid, &WNOHANG);
297 # negative return value means dead process
298 return -$pid;
299 }
300 }
301 return 0;
304#######################################################################
305# killpid attempts to gracefully stop processes in the given pid list
306# with a SIGTERM signal and SIGKILLs those which have not died on time.
308sub killpid {
309 my ($verbose, $pidlist) = @_;
310 use POSIX ":sys_wait_h";
311 my @requested;
312 my @signalled;
313 my @reapchild;
315 # The 'pidlist' argument is a string of whitespace separated pids.
316 return if(not defined($pidlist));
318 # Make 'requested' hold the non-duplicate pids from 'pidlist'.
319 @requested = split(' ', $pidlist);
320 return if(not @requested);
321 if(scalar(@requested) > 2) {
322 @requested = sort({$a <=> $b} @requested);
323 }
324 for(my $i = scalar(@requested) - 2; $i >= 0; $i--) {
325 if($requested[$i] == $requested[$i + 1]) {
326 splice @requested, $i + 1, 1;
327 }
328 }
330 # Send a SIGTERM to processes which are alive to gracefully stop them.
331 foreach my $tmp (@requested) {
332 chomp $tmp;
333 if($tmp =~ /^(\d+)$/) {
334 my $pid = $1;
335 if($pid > 0) {
336 if(pidexists($pid)) {
337 print("RUN: Process with pid $pid signalled to die\n")
338 if($verbose);
339 pidterm($pid);
340 push @signalled, $pid;
341 }
342 else {
343 print("RUN: Process with pid $pid already dead\n")
344 if($verbose);
345 # if possible reap its dead children
346 pidwait($pid, &WNOHANG);
347 push @reapchild, $pid;
348 }
349 }
350 }
351 }
353 # Allow all signalled processes five seconds to gracefully die.
354 if(@signalled) {
355 my $twentieths = 5 * 20;
356 while($twentieths--) {
357 for(my $i = scalar(@signalled) - 1; $i >= 0; $i--) {
358 my $pid = $signalled[$i];
359 if(!pidexists($pid)) {
360 print("RUN: Process with pid $pid gracefully died\n")
361 if($verbose);
362 splice @signalled, $i, 1;
363 # if possible reap its dead children
364 pidwait($pid, &WNOHANG);
365 push @reapchild, $pid;
366 }
367 }
368 last if(not scalar(@signalled));
369 # give any zombies of us a chance to move on to the afterlife
370 pidwait(0, &WNOHANG);
371 Time::HiRes::sleep(0.05);
372 }
373 }
375 # Mercilessly SIGKILL processes still alive.
376 if(@signalled) {
377 foreach my $pid (@signalled) {
378 if($pid > 0) {
379 print("RUN: Process with pid $pid forced to die with SIGKILL\n")
380 if($verbose);
381 pidkill($pid);
382 # if possible reap its dead children
383 pidwait($pid, &WNOHANG);
384 push @reapchild, $pid;
385 }
386 }
387 }
389 # Reap processes dead children for sure.
390 if(@reapchild) {
391 foreach my $pid (@reapchild) {
392 if($pid > 0) {
393 pidwait($pid, 0);
394 }
395 }
396 }
399#######################################################################
400# killsockfilters kills sockfilter processes for a given server.
402sub killsockfilters {
403 my ($piddir, $proto, $ipvnum, $idnum, $verbose, $which) = @_;
404 my $server;
405 my $pidfile;
406 my $pid;
408 return if($proto !~ /^(ftp|imap|pop3|smtp)$/);
410 die "unsupported sockfilter: $which"
411 if($which && ($which !~ /^(main|data)$/));
413 $server = servername_id($proto, $ipvnum, $idnum) if($verbose);
415 if(!$which || ($which eq 'main')) {
416 $pidfile = mainsockf_pidfilename($piddir, $proto, $ipvnum, $idnum);
417 $pid = processexists($pidfile);
418 if($pid > 0) {
419 printf("* kill pid for %s-%s => %d\n", $server,
420 ($proto eq 'ftp')?'ctrl':'filt', $pid) if($verbose);
421 pidkill($pid);
422 pidwait($pid, 0);
423 }
424 unlink($pidfile) if(-f $pidfile);
425 }
427 return if($proto ne 'ftp');
429 if(!$which || ($which eq 'data')) {
430 $pidfile = datasockf_pidfilename($piddir, $proto, $ipvnum, $idnum);
431 $pid = processexists($pidfile);
432 if($pid > 0) {
433 printf("* kill pid for %s-data => %d\n", $server,
434 $pid) if($verbose);
435 pidkill($pid);
436 pidwait($pid, 0);
437 }
438 unlink($pidfile) if(-f $pidfile);
439 }
442#######################################################################
443# killallsockfilters kills sockfilter processes for all servers.
445sub killallsockfilters {
446 my ($piddir, $verbose) = @_;
448 for my $proto (('ftp', 'imap', 'pop3', 'smtp')) {
449 for my $ipvnum (('4', '6')) {
450 for my $idnum (('1', '2')) {
451 killsockfilters($piddir, $proto, $ipvnum, $idnum, $verbose);
452 }
453 }
454 }
457sub set_advisor_read_lock {
458 my ($filename) = @_;
460 my $fileh;
461 if(open($fileh, ">", $filename) && close($fileh)) {
462 return;
463 }
464 printf "Error creating lock file $filename error: $!\n";
467sub clear_advisor_read_lock {
468 my ($filename) = @_;
470 if(-f $filename) {
471 unlink($filename);
472 }
4751;
Diagnostics4 git calls · 42 ms
CallStatusSizeTime042 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 · hit22 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=22
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
fetch2004.2 KiB · hit17 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=17
git.response.bytes=4265
git.response.cache=HIT
git.response.duration_ms=17
git.fetch.wants=fa3b28ee64184e770c8661ad7558035149f149c3
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=4265
git.pkt.payload.max=4244
git.sideband.band1.bytes=4243
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
a5e23123cb04a594
Request
a1492243…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format