diff --git a/libpsi/include/psi/psi.h b/libpsi/include/psi/psi.h index 9162446..213c94e 100644 --- a/libpsi/include/psi/psi.h +++ b/libpsi/include/psi/psi.h @@ -22,9 +22,7 @@ __BEGIN_DECLS -#define PSI_PATH_MEMORY "/proc/pressure/memory" -#define PSI_PATH_IO "/proc/pressure/io" -#define PSI_PATH_CPU "/proc/pressure/cpu" +enum psi_resource { PSI_MEMORY, PSI_IO, PSI_CPU, PSI_RESOURCE_COUNT }; enum psi_stall_type { PSI_SOME, @@ -45,16 +43,22 @@ struct psi_data { struct psi_stats cpu_stats[PSI_TYPE_COUNT]; }; +static const char* psi_resource_file[PSI_RESOURCE_COUNT] = { + "/proc/pressure/memory", + "/proc/pressure/io", + "/proc/pressure/cpu", +}; + /* - * Initializes psi monitor. + * Initializes psi monitor for the given psi resource type. * stall_type, threshold_us and window_us are monitor parameters * When successful, the function returns file descriptor that can * be used with poll/epoll syscalls to wait for EPOLLPRI events. * When unsuccessful, the function returns -1 and errno is set * appropriately. */ -int init_psi_monitor(enum psi_stall_type stall_type, - int threshold_us, int window_us); +int init_psi_monitor(enum psi_stall_type stall_type, int threshold_us, int window_us, + enum psi_resource resource = PSI_MEMORY); /* * Registers psi monitor file descriptor fd on the epoll instance diff --git a/libpsi/psi.cpp b/libpsi/psi.cpp index 54f9971..557850a 100644 --- a/libpsi/psi.cpp +++ b/libpsi/psi.cpp @@ -33,13 +33,18 @@ static const char* stall_type_name[] = { "full", }; -int init_psi_monitor(enum psi_stall_type stall_type, - int threshold_us, int window_us) { +int init_psi_monitor(enum psi_stall_type stall_type, int threshold_us, int window_us, + enum psi_resource resource) { + if (resource < PSI_MEMORY || resource >= PSI_RESOURCE_COUNT) { + ALOGE("Invalid psi resource type: %d", resource); + errno = EINVAL; + return -1; + } int fd; int res; char buf[256]; - fd = TEMP_FAILURE_RETRY(open(PSI_PATH_MEMORY, O_WRONLY | O_CLOEXEC)); + fd = TEMP_FAILURE_RETRY(open(psi_resource_file[resource], O_WRONLY | O_CLOEXEC)); if (fd < 0) { ALOGE("No kernel psi monitor support (errno=%d)", errno); return -1; @@ -58,16 +63,16 @@ int init_psi_monitor(enum psi_stall_type stall_type, } if (res >= (ssize_t)sizeof(buf)) { - ALOGE("%s line overflow for psi stall type '%s'", - PSI_PATH_MEMORY, stall_type_name[stall_type]); + ALOGE("%s line overflow for psi stall type '%s'", psi_resource_file[resource], + stall_type_name[stall_type]); errno = EINVAL; goto err; } res = TEMP_FAILURE_RETRY(write(fd, buf, strlen(buf) + 1)); if (res < 0) { - ALOGE("%s write failed for psi stall type '%s'; errno=%d", - PSI_PATH_MEMORY, stall_type_name[stall_type], errno); + ALOGE("%s write failed for psi stall type '%s'; errno=%d", psi_resource_file[resource], + stall_type_name[stall_type], errno); goto err; } diff --git a/lmkd.cpp b/lmkd.cpp index 8b0055e..7926dab 100644 --- a/lmkd.cpp +++ b/lmkd.cpp @@ -2035,24 +2035,24 @@ static int psi_parse(struct reread_data *file_data, struct psi_stats stats[], bo static int psi_parse_mem(struct psi_data *psi_data) { static struct reread_data file_data = { - .filename = PSI_PATH_MEMORY, - .fd = -1, + .filename = psi_resource_file[PSI_MEMORY], + .fd = -1, }; return psi_parse(&file_data, psi_data->mem_stats, true); } static int psi_parse_io(struct psi_data *psi_data) { static struct reread_data file_data = { - .filename = PSI_PATH_IO, - .fd = -1, + .filename = psi_resource_file[PSI_IO], + .fd = -1, }; return psi_parse(&file_data, psi_data->io_stats, true); } static int psi_parse_cpu(struct psi_data *psi_data) { static struct reread_data file_data = { - .filename = PSI_PATH_CPU, - .fd = -1, + .filename = psi_resource_file[PSI_CPU], + .fd = -1, }; return psi_parse(&file_data, psi_data->cpu_stats, false); }