Skip to content

Commit ec57990

Browse files
committed
chore: auto obtain worker pid in gdb script
1 parent 387922e commit ec57990

File tree

5 files changed

+25
-18
lines changed

5 files changed

+25
-18
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ nix/nginx/logs/nginx.pid
1414
*.so
1515
nginx.pid
1616
tags
17+
net_worker.pid

docs/contributing.md

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,11 @@ select net.http_get('http://localhost:3000/projects');
5454

5555
### GDB
5656

57-
To debug the background worker, grab its PID from the logs:
57+
To debug the background worker, there's a script that wraps GDB. It automatically obtains the pid of the latest started worker:
5858

5959
```
6060
$ nix-shell
61-
$ net-with-pg-16 psql
62-
63-
2024-09-02 20:16:26.905 -05 [1145879] INFO: pg_net_worker started with a config of: pg_net.ttl=6 hours, pg_net.batch_size=200, pg_net.database_name=postgres
64-
```
65-
66-
And use it like:
67-
68-
```
69-
$ nix-shell
70-
$ sudo with-gdb -p 1145879
61+
$ sudo net-with-gdb
7162
```
7263

7364
## Load Testing

nix/gdbScript.nix

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
1-
{ gdb, writeText, writeShellScriptBin } :
1+
{ gdb, writeText, writeShellScriptBin, pidFileName } :
22

33
let
44
file = writeText "gdbconf" ''
55
# Do this so we can do `backtrace` once a segfault occurs. Otherwise once SIGSEGV is received the bgworker will quit and we can't backtrace.
66
handle SIGSEGV stop nopass
77
'';
88
script = ''
9-
${gdb}/bin/gdb -x ${file} "$@"
9+
set -euo pipefail
10+
11+
if [ ! -e ${pidFileName} ]
12+
then
13+
echo "The pg_net worker is not started. Exiting."
14+
exit 1
15+
fi
16+
pid=$(cat ${pidFileName})
17+
${gdb}/bin/gdb -x ${file} -p ''$pid "$@"
1018
'';
1119
in
12-
writeShellScriptBin "with-gdb" script
20+
writeShellScriptBin "net-with-gdb" script

nix/pgScript.nix

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{ postgresql, writeShellScriptBin } :
1+
{ postgresql, writeShellScriptBin, gnused, pidFileName } :
22

33
let
44
ver = builtins.head (builtins.splitVersion postgresql.version);
@@ -14,7 +14,7 @@ let
1414
export PGUSER=postgres
1515
export PGDATABASE=postgres
1616
17-
trap 'pg_ctl stop -m i && rm -rf "$tmpdir"' sigint sigterm exit
17+
trap 'pg_ctl stop -m i && rm -rf "$tmpdir" && rm ${pidFileName}' sigint sigterm exit
1818
1919
PGTZ=UTC initdb --no-locale --encoding=UTF8 --nosync -U "$PGUSER"
2020
@@ -26,6 +26,12 @@ let
2626
2727
psql -v ON_ERROR_STOP=1 -c "create extension pg_net" -d postgres
2828
29+
psql -t -c "\o ${pidFileName}" -c "select pid from pg_stat_activity where backend_type ilike '%pg_net%'"
30+
31+
${gnused}/bin/sed '/^''$/d;s/[[:blank:]]//g' -i ${pidFileName}
32+
33+
psql -f ${./bench.sql}
34+
2935
"$@"
3036
'';
3137
in

shell.nix

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ with import (builtins.fetchTarball {
66
mkShell {
77
buildInputs =
88
let
9+
pidFileName = "net_worker.pid";
910
supportedPgVersions = [
1011
postgresql_12
1112
postgresql_13
@@ -14,9 +15,9 @@ mkShell {
1415
postgresql_16
1516
];
1617
pgWithExt = { pg }: pg.withPackages (p: [ (callPackage ./nix/pg_net.nix { postgresql = pg;}) ]);
17-
extAll = map (x: callPackage ./nix/pgScript.nix { postgresql = pgWithExt { pg = x;}; }) supportedPgVersions;
18+
extAll = map (x: callPackage ./nix/pgScript.nix { postgresql = pgWithExt { pg = x;}; inherit pidFileName;}) supportedPgVersions;
19+
gdbScript = callPackage ./nix/gdbScript.nix {inherit pidFileName;};
1820
nginxCustom = callPackage ./nix/nginxCustom.nix {};
19-
gdbScript = callPackage ./nix/gdbScript.nix {};
2021
nixopsScripts = callPackage ./nix/nixopsScripts.nix {};
2122
pythonDeps = with python3Packages; [
2223
pytest

0 commit comments

Comments
 (0)