Skip to content

Commit a961ec4

Browse files
Isaac J. Manjarrespmladek
authored andcommitted
printk: Improve memory usage logging during boot
When the initial printk ring buffer size is updated, setup_log_buf() allocates a new ring buffer, as well as a set of meta-data structures for the new ring buffer. The function also emits the new size of the ring buffer, but not the size of the meta-data structures. This makes it difficult to assess how changing the log buffer size impacts memory usage during boot. For instance, increasing the ring buffer size from 512 KB to 1 MB through the command line yields an increase of 2304 KB in reserved memory at boot, while the only obvious change is the 512 KB difference in the ring buffer sizes: log_buf_len=512K: printk: log_buf_len: 524288 bytes Memory: ... (... 733252K reserved ...) log_buf_len=1M: printk: log_buf_len: 1048576 bytes Memory: ... (... 735556K reserved ...) This is because of how the size of the meta-data structures scale with the size of the ring buffer. Even when there aren't changes to the printk ring buffer size (i.e. the initial size == 1 << CONFIG_LOG_BUF_SHIFT), it is impossible to tell how much memory is consumed by the printk ring buffer during boot. Therefore, unconditionally log the sizes of the printk ring buffer and its meta-data structures, so that it's easier to understand how changing the log buffer size (either through the command line or by changing CONFIG_LOG_BUF_SHIFT) affects boot time memory usage. With the new logs, it is much easier to see exactly why the memory increased by 2304 KB: log_buf_len=512K: printk: log buffer data + meta data: 524288 + 1835008 = 2359296 bytes Memory: ... (... 733252K reserved ...) log_buf_len=1M: printk: log buffer data + meta data: 1048576 + 3670016 = 4718592 bytes Memory: ... (... 735556K reserved ...) Signed-off-by: Isaac J. Manjarres <isaacmanjarres@google.com> Reviewed-by: Petr Mladek <pmladek@suse.com> Tested-by: Petr Mladek <pmladek@suse.com> Link: https://lore.kernel.org/r/20240930184826.3595221-1-isaacmanjarres@google.com [pmladek@suse.com: Updated the examples in the commit message, simplified comment for default buffer.] Signed-off-by: Petr Mladek <pmladek@suse.com>
1 parent 17b6557 commit a961ec4

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

kernel/printk/printk.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,17 @@ static unsigned int __init add_to_rb(struct printk_ringbuffer *rb,
11561156

11571157
static char setup_text_buf[PRINTKRB_RECORD_MAX] __initdata;
11581158

1159+
static void print_log_buf_usage_stats(void)
1160+
{
1161+
unsigned int descs_count = log_buf_len >> PRB_AVGBITS;
1162+
size_t meta_data_size;
1163+
1164+
meta_data_size = descs_count * (sizeof(struct prb_desc) + sizeof(struct printk_info));
1165+
1166+
pr_info("log buffer data + meta data: %u + %zu = %zu bytes\n",
1167+
log_buf_len, meta_data_size, log_buf_len + meta_data_size);
1168+
}
1169+
11591170
void __init setup_log_buf(int early)
11601171
{
11611172
struct printk_info *new_infos;
@@ -1185,20 +1196,25 @@ void __init setup_log_buf(int early)
11851196
if (!early && !new_log_buf_len)
11861197
log_buf_add_cpu();
11871198

1188-
if (!new_log_buf_len)
1199+
if (!new_log_buf_len) {
1200+
/* Show the memory stats only once. */
1201+
if (!early)
1202+
goto out;
1203+
11891204
return;
1205+
}
11901206

11911207
new_descs_count = new_log_buf_len >> PRB_AVGBITS;
11921208
if (new_descs_count == 0) {
11931209
pr_err("new_log_buf_len: %lu too small\n", new_log_buf_len);
1194-
return;
1210+
goto out;
11951211
}
11961212

11971213
new_log_buf = memblock_alloc(new_log_buf_len, LOG_ALIGN);
11981214
if (unlikely(!new_log_buf)) {
11991215
pr_err("log_buf_len: %lu text bytes not available\n",
12001216
new_log_buf_len);
1201-
return;
1217+
goto out;
12021218
}
12031219

12041220
new_descs_size = new_descs_count * sizeof(struct prb_desc);
@@ -1261,7 +1277,7 @@ void __init setup_log_buf(int early)
12611277
prb_next_seq(&printk_rb_static) - seq);
12621278
}
12631279

1264-
pr_info("log_buf_len: %u bytes\n", log_buf_len);
1280+
print_log_buf_usage_stats();
12651281
pr_info("early log buf free: %u(%u%%)\n",
12661282
free, (free * 100) / __LOG_BUF_LEN);
12671283
return;
@@ -1270,6 +1286,8 @@ void __init setup_log_buf(int early)
12701286
memblock_free(new_descs, new_descs_size);
12711287
err_free_log_buf:
12721288
memblock_free(new_log_buf, new_log_buf_len);
1289+
out:
1290+
print_log_buf_usage_stats();
12731291
}
12741292

12751293
static bool __read_mostly ignore_loglevel;

0 commit comments

Comments
 (0)