Skip to content

Commit

Permalink
Merge pull request #395 from bozhodimitrov/main
Browse files Browse the repository at this point in the history
Examples for episode 566 & 567
  • Loading branch information
asottile authored Dec 1, 2024
2 parents 9eee860 + 7642168 commit 3a39c42
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
29 changes: 29 additions & 0 deletions sample_code/ep566/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# [ONE TERABYTE of RAM saved with a single line of code (advanced)](https://youtu.be/Hgw_RlCaIds)

Today I show off a small change I made at work with huge impact and explain how it works!

## Setup commands

```bash
git clone [email protected]:python/cpython
# git clone https://github.com/python/cpython

cd cpython
```

## Interactive examples

### Python

```python
import gc
gc.freeze()
```

### Bash

```bash
git grep 'typedef .* PyObject;'
git grep 'struct _object'
nano Include/object.h
```
62 changes: 62 additions & 0 deletions sample_code/ep567/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# [using memray to debug (and fix) a memory leak in krb5! (advanced)](https://youtu.be/bw5AHdZA7e4)

Today I show how I utilized memray to both find, debug, and ultimately fix a memory leak in krb5 -- sadly this was not the leak I was looking for though!

## Setup commands

```bash
git clone [email protected]:getsentry/sentry
# git clone https://github.com/getsentry/sentry
cd sentry
```

## Interactive examples

### Bash

Session 1:

```bash
python -m sentry run worker --queues counters-0 --concurrency 1 --max-tasks-per-child 1000000000
timeout --signal=SIGINT 10s memray run --quiet -m sentry run worker --queues counters-0 --concurrency 1 --max-tasks-per-child 1000000000

memray flamegraph --help
memray flamegraph --leaks <memray_bin_filename>
open <memray_flamegraph_html_filename>

PYTHONMALLOC=malloc timeout --signal=SIGINT 10s memray run --follow-fork --quiet -m sentry run worker --queues counters-0 --concurrency 1 --max-tasks-per-child 1000000000
memray flamegraph --leaks <memray_bin_filename>
open <memray_flamegraph_html_filename>

PYTHONMALLOC=malloc timeout --signal=SIGINT 20s memray run --follow-fork --quiet -m sentry run worker --queues counters-0 --concurrency 1 --max-tasks-per-child 1000000000
memray flamegraph --leaks <memray_bin_filename>
open <memray_flamegraph_html_filename>

PYTHONMALLOC=malloc memray run -m t
memray flamegraph --leaks <memray_bin_filename>
open <memray_flamegraph_html_filename>

PYTHONMALLOC=malloc memray run --native -m t
memray flamegraph <memray_bin_filename>
xdg-open <memray_flamegraph_html_filename>

gcc $(PKG_CONFIG_PATH=<broken_version> pkg-config kbr5-gssapi --cflags --libs) t.c
./a.out
leaks --atExit -- ./a.out

gcc $(PKG_CONFIG_PATH=<fixed_version> pkg-config kbr5-gssapi --cflags --libs) t.c
./a.out
leaks --atExit -- ./a.out
```

Session 2:

```bash
sentry exec t.py
```

Session 3:

```bash
htop --filter ' worker '
```
18 changes: 18 additions & 0 deletions sample_code/ep567/rev01/t.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <gssapi.h>

int main(void) {
for (int i = 0; i < 10; i += 1) {
gss_cred_id_t cred = 0;
OM_uint32 minor = 0;
OM_uint32 ret = gss_acquire_cred(
&minor, GSS_C_NO_NAME, 0, GSS_C_NO_OID_SET,
GSS_C_INITIATE, &cred, NULL, NULL
);
if (ret == GSS_S_COMPLETE) {
printf("no error\n");
gss_release_cred(&minor, &cred)
} else {
printf("got error: %d\n", ret)
}
}
}
21 changes: 21 additions & 0 deletions sample_code/ep567/rev01/t.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from sentry.buffer import backend
from sentry.models.group import Group
from sentry.models.organization import Organization
from sentry.models.project import Project
from sentry.models.release import Release
from sentry.models.releases.release_project import ReleaseProject
from sentry.models.team import Team


group = Group.objects.create(project=Project(id=1))
org = Organization.objects.create(slug="test-org")
team = Team.objects.create(organization=org, slug="test-team")
project = Project.objects.create(organization=org, slug="test-project")
project.add_team(team)
release = Release.objects.create(organization=org, version="abcdefg")
release_project = ReleaseProject.objects.create(project, release=release)

while True:
columns = {"times_seen": 1}
filters = {"id": group.id, "project_id": 1}
backend.incr(Group, columns, filters)
9 changes: 9 additions & 0 deletions sample_code/ep567/rev01/t2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import psycopg2


def main():
for _ in range(10000):
psycopg2.connect("user=postgres host=127.0.0.1 dbname=sentry").close()


main()

0 comments on commit 3a39c42

Please sign in to comment.