Skip to content

Commit

Permalink
fprintf: Add alternative method for reading the data cacheline size
Browse files Browse the repository at this point in the history
Systems such as Alpine Linux don't have support for obtaining _SC_LEVEL1_DCACHE_LINESIZE
via sysconf(), provide altenative method reading it from sysfs.

Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
  • Loading branch information
acmel committed Aug 20, 2021
1 parent 71867af commit 9f9588d
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion dwarves_fprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@
#include <dwarf.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
#include <elfutils/version.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "config.h"
#include "dwarves.h"
Expand Down Expand Up @@ -1933,10 +1937,39 @@ void cus__print_error_msg(const char *progname, const struct cus *cus,
fprintf(stderr, "%s: %s\n", progname, strerror(-err));
}

#ifndef _SC_LEVEL1_DCACHE_LINESIZE
int filename__read_int(const char *filename, int *value)
{
char line[64];
int fd = open(filename, O_RDONLY), err = -1;

if (fd < 0)
return -1;

if (read(fd, line, sizeof(line)) > 0) {
*value = atoi(line);
err = 0;
}

close(fd);
return err;
}
#endif

static long cacheline__size(void)
{
#ifdef _SC_LEVEL1_DCACHE_LINESIZE
return sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
#else
int value;
return filename__read_int("/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size", &value) == 0 ? value : -1;
#endif
}

void dwarves__fprintf_init(uint16_t user_cacheline_size)
{
if (user_cacheline_size == 0) {
long sys_cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
long sys_cacheline_size = cacheline__size();

if (sys_cacheline_size > 0)
cacheline_size = sys_cacheline_size;
Expand Down

0 comments on commit 9f9588d

Please sign in to comment.