Improve read speed of a virtual filesystem

108 views Asked by At

I am using sysfs to capture CPU information. These sysfs files allow us the ability to get a lot of kernel subsystem information in user space through virtual files. My question boils down to reading a large number of sysfs files, where overhead can become quite large. Some sample code:

int cpu0_freq_open() {
  int fd = open("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq", O_RDONLY | O_NONBLOCK);
}

int cpu0_freq_read(int fd) {
  char buffer[64];
  if ( pread(fd, &buffer, sizeof(buffer), 0) == -1 ) {
    printf("Reading failed");
    return 1;
  }
  int freq = atoi(buffer);
  // function call to store the event value at this timestamp
}

int main() {
  int fd_cpu0 = cpu0_freq_open(); // we open for all cpu#s

  // do some action here
  // make a lot of cpu0_freq_read(fd_cpu0) calls for cpu0 and all other cpus

  cpu0_freq_close();
}

A single pread() call isn't too terrible, but if I make a calls to cpu0_freq_read, cpu1_freq_read, etc. then the overhead of all of these pread() calls add up quickly. I'm wondering if there is a faster way to retrieve the information from this virtual filesystem. The immediate alternative that comes to mind is memory mapping which won't work because we're dealing with a virtual filesystem. Are there ways to improve the read speed of these sysfs files? And, on a more broader scope, virtual filesystems in general?

0

There are 0 answers