Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rtla: Simplify code by caching string lengths

Simplify trace_event_save_hist() and set_comm_cgroup() by computing
string lengths once and storing them in local variables, rather than
calling strlen() multiple times on the same unchanged strings. This
makes the code clearer by eliminating redundant function calls and
improving readability.

In trace_event_save_hist(), the write loop previously called strlen()
on the hist buffer twice per iteration for both the size calculation
and loop condition. Store the length in hist_len before entering the
loop. In set_comm_cgroup(), strlen() was called on cgroup_path up to
three times in succession. Store the result in cg_path_len to use in
both the offset calculation and size parameter for subsequent append
operations.

This simplification makes the code easier to read and maintain without
changing program behavior.

Signed-off-by: Wander Lairson Costa <wander@redhat.com>
Link: https://lore.kernel.org/r/20260309195040.1019085-7-wander@redhat.com
Signed-off-by: Tomas Glozar <tglozar@redhat.com>

authored by

Wander Lairson Costa and committed by
Tomas Glozar
f79720e2 a29430c2

+11 -6
+4 -2
tools/tracing/rtla/src/trace.c
··· 346 346 mode_t mode = 0644; 347 347 char path[MAX_PATH]; 348 348 char *hist; 349 + size_t hist_len; 349 350 350 351 if (!tevent) 351 352 return; ··· 377 376 } 378 377 379 378 index = 0; 379 + hist_len = strlen(hist); 380 380 do { 381 - index += write(out_fd, &hist[index], strlen(hist) - index); 382 - } while (index < strlen(hist)); 381 + index += write(out_fd, &hist[index], hist_len - index); 382 + } while (index < hist_len); 383 383 384 384 free(hist); 385 385 out_close:
+7 -4
tools/tracing/rtla/src/utils.c
··· 819 819 char cgroup_procs[MAX_PATH]; 820 820 int retval; 821 821 int cg_fd; 822 + size_t cg_path_len; 822 823 823 824 retval = find_mount("cgroup2", cgroup_path, sizeof(cgroup_path)); 824 825 if (!retval) { ··· 827 826 return -1; 828 827 } 829 828 829 + cg_path_len = strlen(cgroup_path); 830 + 830 831 if (!cgroup) { 831 - retval = get_self_cgroup(&cgroup_path[strlen(cgroup_path)], 832 - sizeof(cgroup_path) - strlen(cgroup_path)); 832 + retval = get_self_cgroup(&cgroup_path[cg_path_len], 833 + sizeof(cgroup_path) - cg_path_len); 833 834 if (!retval) { 834 835 err_msg("Did not find self cgroup\n"); 835 836 return -1; 836 837 } 837 838 } else { 838 - snprintf(&cgroup_path[strlen(cgroup_path)], 839 - sizeof(cgroup_path) - strlen(cgroup_path), "%s/", cgroup); 839 + snprintf(&cgroup_path[cg_path_len], 840 + sizeof(cgroup_path) - cg_path_len, "%s/", cgroup); 840 841 } 841 842 842 843 snprintf(cgroup_procs, MAX_PATH, "%s/cgroup.procs", cgroup_path);