1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "libpsi"
18 
19 #include <errno.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/epoll.h>
24 #include <unistd.h>
25 
26 #include <log/log.h>
27 #include "psi/psi.h"
28 
29 #define PSI_MON_FILE_MEMORY "/proc/pressure/memory"
30 
31 static const char* stall_type_name[] = {
32         "some",
33         "full",
34 };
35 
init_psi_monitor(enum psi_stall_type stall_type,int threshold_us,int window_us)36 int init_psi_monitor(enum psi_stall_type stall_type,
37              int threshold_us, int window_us) {
38     int fd;
39     int res;
40     char buf[256];
41 
42     fd = TEMP_FAILURE_RETRY(open(PSI_MON_FILE_MEMORY, O_WRONLY | O_CLOEXEC));
43     if (fd < 0) {
44         ALOGE("No kernel psi monitor support (errno=%d)", errno);
45         return -1;
46     }
47 
48     switch (stall_type) {
49     case (PSI_SOME):
50     case (PSI_FULL):
51         res = snprintf(buf, sizeof(buf), "%s %d %d",
52             stall_type_name[stall_type], threshold_us, window_us);
53         break;
54     default:
55         ALOGE("Invalid psi stall type: %d", stall_type);
56         errno = EINVAL;
57         goto err;
58     }
59 
60     if (res >= (ssize_t)sizeof(buf)) {
61         ALOGE("%s line overflow for psi stall type '%s'",
62             PSI_MON_FILE_MEMORY, stall_type_name[stall_type]);
63         errno = EINVAL;
64         goto err;
65     }
66 
67     res = TEMP_FAILURE_RETRY(write(fd, buf, strlen(buf) + 1));
68     if (res < 0) {
69         ALOGE("%s write failed for psi stall type '%s'; errno=%d",
70             PSI_MON_FILE_MEMORY, stall_type_name[stall_type], errno);
71         goto err;
72     }
73 
74     return fd;
75 
76 err:
77     close(fd);
78     return -1;
79 }
80 
register_psi_monitor(int epollfd,int fd,void * data)81 int register_psi_monitor(int epollfd, int fd, void* data) {
82     int res;
83     struct epoll_event epev;
84 
85     epev.events = EPOLLPRI;
86     epev.data.ptr = data;
87     res = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &epev);
88     if (res < 0) {
89         ALOGE("epoll_ctl for psi monitor failed; errno=%d", errno);
90     }
91     return res;
92 }
93 
unregister_psi_monitor(int epollfd,int fd)94 int unregister_psi_monitor(int epollfd, int fd) {
95     return epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL);
96 }
97 
destroy_psi_monitor(int fd)98 void destroy_psi_monitor(int fd) {
99     if (fd >= 0) {
100         close(fd);
101     }
102 }
103