Skip to content

Instantly share code, notes, and snippets.

@zultron
Created July 3, 2019 18:20
Show Gist options
  • Save zultron/639c00091696f079ba34372cc5e8ed2e to your computer and use it in GitHub Desktop.
Save zultron/639c00091696f079ba34372cc5e8ed2e to your computer and use it in GitHub Desktop.
Disable CPU idle states using PM QoS framework
// Disable CPU idle states using PM QoS framework
//
// Directly related:
// https://github.com/torvalds/linux/blob/master/Documentation/power/pm_qos_interface.txt
// https://lwn.net/Articles/465195/
// https://access.redhat.com/articles/65410
//
// Network:
// https://elinux.org/images/f/f9/Elc2008_pm_qos_slides.pdf
//
// Per-CPU idle control:
// https://github.com/torvalds/linux/blob/master/Documentation/admin-guide/pm/cpuidle.rst
#include <stdio.h> // fprintf
#include <fcntl.h> // open()
#include <string.h> // strerror()
#include <errno.h> // errno
#include <stdlib.h> // exit()
#include <unistd.h> // write()
static int pm_qos_fd = -1;
static char * dev_path = "/dev/cpu_dma_latency";
void start_low_latency(void)
{
int32_t target = 0;
if (pm_qos_fd >= 0) {
fprintf(stderr, "Low latency already started; doing nothing\n");
return;
}
fprintf(stderr, "Starting low latency\n");
fprintf(stderr, " target size: %ld\n", sizeof(target));
pm_qos_fd = open(dev_path, O_RDWR);
fprintf(stderr, "Opened device %s on fd %d\n", dev_path, pm_qos_fd);
if (pm_qos_fd < 0) {
fprintf(stderr, "Failed to open PM QOS file: %s\n",
strerror(errno));
exit(errno);
}
write(pm_qos_fd, &target, sizeof(target));
fprintf(stderr, "Wrote to device %s\n", dev_path);
}
void sleep_forever(void)
{
fprintf(stderr, "Sleeping foverer\n");
while (1) {
sleep(100);
}
// Shouldn't get here
fprintf(stderr, "Done sleeping\n");
}
void stop_low_latency(void)
{
if (pm_qos_fd >= 0) {
fprintf(stderr, "Stopping disabled CPU idle states\n");
close(pm_qos_fd);
} else {
fprintf(stderr, "Disabled CPU idle states already stopped; doing nothing\n");
}
}
int main( int argc, const char* argv[] )
{
start_low_latency();
sleep_forever();
stop_low_latency(); // Never gets here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment