Skip to content

Commit f79720e

Browse files
walaclenticularis39
authored andcommitted
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>
1 parent a29430c commit f79720e

2 files changed

Lines changed: 11 additions & 6 deletions

File tree

tools/tracing/rtla/src/trace.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ static void trace_event_save_hist(struct trace_instance *instance,
346346
mode_t mode = 0644;
347347
char path[MAX_PATH];
348348
char *hist;
349+
size_t hist_len;
349350

350351
if (!tevent)
351352
return;
@@ -376,9 +377,10 @@ static void trace_event_save_hist(struct trace_instance *instance,
376377
}
377378

378379
index = 0;
380+
hist_len = strlen(hist);
379381
do {
380-
index += write(out_fd, &hist[index], strlen(hist) - index);
381-
} while (index < strlen(hist));
382+
index += write(out_fd, &hist[index], hist_len - index);
383+
} while (index < hist_len);
382384

383385
free(hist);
384386
out_close:

tools/tracing/rtla/src/utils.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -819,23 +819,26 @@ static int open_cgroup_procs(const char *cgroup)
819819
char cgroup_procs[MAX_PATH];
820820
int retval;
821821
int cg_fd;
822+
size_t cg_path_len;
822823

823824
retval = find_mount("cgroup2", cgroup_path, sizeof(cgroup_path));
824825
if (!retval) {
825826
err_msg("Did not find cgroupv2 mount point\n");
826827
return -1;
827828
}
828829

830+
cg_path_len = strlen(cgroup_path);
831+
829832
if (!cgroup) {
830-
retval = get_self_cgroup(&cgroup_path[strlen(cgroup_path)],
831-
sizeof(cgroup_path) - strlen(cgroup_path));
833+
retval = get_self_cgroup(&cgroup_path[cg_path_len],
834+
sizeof(cgroup_path) - cg_path_len);
832835
if (!retval) {
833836
err_msg("Did not find self cgroup\n");
834837
return -1;
835838
}
836839
} else {
837-
snprintf(&cgroup_path[strlen(cgroup_path)],
838-
sizeof(cgroup_path) - strlen(cgroup_path), "%s/", cgroup);
840+
snprintf(&cgroup_path[cg_path_len],
841+
sizeof(cgroup_path) - cg_path_len, "%s/", cgroup);
839842
}
840843

841844
snprintf(cgroup_procs, MAX_PATH, "%s/cgroup.procs", cgroup_path);

0 commit comments

Comments
 (0)