Skip to content

Commit 20815bd

Browse files
committed
Present max_cache_size in bytes
1 parent a37ac86 commit 20815bd

File tree

8 files changed

+16
-23
lines changed

8 files changed

+16
-23
lines changed

examples/example.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ main(int argc, char **argv)
505505
}
506506
if (has_arg(argc, argv, "cache-keep")) {
507507
sentry_options_set_cache_keep(options, true);
508-
sentry_options_set_cache_max_size(options, 1000 * 8);
508+
sentry_options_set_cache_max_size(options, 4 * 1024 * 1024);
509509
sentry_options_set_cache_max_age(options, 5 * 24 * 60 * 60);
510510
}
511511

include/sentry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,7 @@ SENTRY_API int sentry_options_get_symbolize_stacktraces(
13821382
SENTRY_API void sentry_options_set_cache_keep(
13831383
sentry_options_t *opts, int enabled);
13841384
/**
1385-
* Sets the maximum size (kb) for the cache folder.
1385+
* Sets the maximum size (in bytes) for the cache folder.
13861386
* On startup, we check new->old entries, and remove those that go over either
13871387
* boundary.
13881388
*/

src/backends/sentry_backend_crashpad.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,8 @@ crashpad_backend_prune_database(sentry_backend_t *backend)
749749
data->db->CleanDatabase(options->cache_max_age);
750750
crashpad::BinaryPruneCondition condition(
751751
crashpad::BinaryPruneCondition::OR,
752-
new crashpad::DatabaseSizePruneCondition(options->cache_max_size),
752+
new crashpad::DatabaseSizePruneCondition(
753+
options->cache_max_size / 1024),
753754
new crashpad::AgePruneCondition(
754755
options->cache_max_age / (24 * 60 * 60)));
755756
crashpad::PruneCrashReportDatabase(data->db, &condition);

src/sentry_database.c

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -323,17 +323,9 @@ sentry__process_old_runs(const sentry_options_t *options, uint64_t last_crash)
323323
typedef struct {
324324
sentry_path_t *path;
325325
time_t mtime;
326-
size_t size_in_kb;
326+
size_t size;
327327
} cache_entry_t;
328328

329-
static size_t
330-
get_file_size_in_kb(const sentry_path_t *path)
331-
{
332-
size_t bytes = sentry__path_get_size(path);
333-
// Round up to next KB boundary
334-
return (bytes + 1023) / 1024;
335-
}
336-
337329
/**
338330
* Comparison function to sort cache entries by mtime, newest first.
339331
*/
@@ -398,7 +390,7 @@ sentry__cleanup_cache(const sentry_options_t *options)
398390

399391
entries[entries_count].path = sentry__path_clone(entry);
400392
entries[entries_count].mtime = sentry__path_get_mtime(entry);
401-
entries[entries_count].size_in_kb = get_file_size_in_kb(entry);
393+
entries[entries_count].size = sentry__path_get_size(entry);
402394
entries_count++;
403395
}
404396
sentry__pathiter_free(iter);
@@ -414,7 +406,7 @@ sentry__cleanup_cache(const sentry_options_t *options)
414406

415407
// Prune entries: iterate newest-to-oldest, accumulating size
416408
// Remove if: too old OR accumulated size exceeds limit
417-
size_t accumulated_size_kb = 0;
409+
size_t accumulated_size = 0;
418410
for (size_t i = 0; i < entries_count; i++) {
419411
bool should_prune = false;
420412

@@ -424,9 +416,9 @@ sentry__cleanup_cache(const sentry_options_t *options)
424416
}
425417

426418
// Size-based pruning (accumulate size as we go, like crashpad)
427-
accumulated_size_kb += entries[i].size_in_kb;
419+
accumulated_size += entries[i].size;
428420
if (options->cache_max_size > 0
429-
&& accumulated_size_kb > (size_t)options->cache_max_size) {
421+
&& accumulated_size > options->cache_max_size) {
430422
should_prune = true;
431423
}
432424

src/sentry_options.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ sentry_options_new(void)
5555
opts->crashpad_limit_stack_capture_to_sp = false;
5656
opts->cache_keep = false;
5757
opts->cache_max_age = 2 * 24 * 60 * 60;
58-
opts->cache_max_size = 1024 * 8;
58+
opts->cache_max_size = 8 * 1024 * 1024;
5959
opts->symbolize_stacktraces =
6060
// AIX doesn't have reliable debug IDs for server-side symbolication,
6161
// and the diversity of Android makes it infeasible to have access to debug

src/sentry_options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct sentry_options_s {
4848
bool cache_keep;
4949

5050
uint64_t cache_max_age;
51-
size_t cache_max_size; // TODO in kb?
51+
size_t cache_max_size;
5252

5353
sentry_attachment_t *attachments;
5454
sentry_run_t *run;

tests/test_integration_cache.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,19 @@ def test_cache_max_size(cmake, backend):
7979
cache_files = list(cache_dir.glob("*.envelope"))
8080
for f in cache_files:
8181
with open(f, "r+b") as file:
82-
file.truncate(2048 * 1000)
82+
file.truncate(2 * 1024 * 1024)
8383

8484
run(
8585
tmp_path,
8686
"sentry_example",
8787
["log", "cache-keep", "no-setup"],
8888
)
8989

90-
# max 8mb
90+
# max 4mb
9191
assert cache_dir.exists()
9292
cache_files = list(cache_dir.glob("*.envelope"))
93-
assert len(cache_files) <= 4
94-
assert sum(f.stat().st_size for f in cache_files) <= 8 * 1000 * 1024
93+
assert len(cache_files) <= 2
94+
assert sum(f.stat().st_size for f in cache_files) <= 4 * 1024 * 1024
9595

9696

9797
@pytest.mark.parametrize(

tests/unit/test_cache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ SENTRY_TEST(cache_max_size)
8787
SENTRY_TEST_OPTIONS_NEW(options);
8888
sentry_options_set_dsn(options, "https://[email protected]/42");
8989
sentry_options_set_cache_keep(options, true);
90-
sentry_options_set_cache_max_size(options, 10); // 10 kb
90+
sentry_options_set_cache_max_size(options, 10 * 1024); // 10 kb
9191
sentry_init(options);
9292

9393
sentry_path_t *cache_path

0 commit comments

Comments
 (0)