Skip to content
  • Holger Hans Peter Freyther's avatar
    util: Avoid memory allocations for formatting paths · ab7d9b67
    Holger Hans Peter Freyther authored
    Avoid memory allocations to construct the path for files in the
    procfs. The procfs paths are way shorter than the PATH_MAX so we
    can use snprintf on a string located on the stack. This shows up
    as a win on x86 using the benchmark program below.
    
    $ make libsystemd-shared.la; gcc -O2 -Isrc/systemd/ -Isrc/ \
    	-o simple-perf-test simple-perf-test.c \
    	.libs/libsystemd-shared.a  -lrt
    
     #include "shared/util.h"
    void test_once(void) {
    	pid_t pid = getpid();
    	char *tmp = NULL;
    
    	get_process_comm(pid, &tmp);
    	free(tmp);
    	tmp = NULL;
    	get_process_cmdline(pid, 0, 1, &tmp);
    	free(tmp);
    	is_kernel_thread(pid);
    	tmp = NULL;
    	get_process_exe(pid, &tmp);
    	free(tmp);
    }
    
    int main(int argc, char **argv)
    {
    	int i;
    	for (i = 0; i < 50000; ++i)
    		test_once();
    }
    ab7d9b67