Tim Murray | eecd7dd | 2018-11-09 09:10:12 -0800 | [diff] [blame] | 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 <string.h> |
| 21 | #include <sys/epoll.h> |
| 22 | |
| 23 | #include <log/log.h> |
| 24 | #include "psi/psi.h" |
| 25 | |
| 26 | #define PSI_MON_FILE_MEMORY "/proc/pressure/memory" |
| 27 | |
| 28 | static const char* stall_type_name[] = { |
| 29 | "some", |
| 30 | "full", |
| 31 | }; |
| 32 | |
| 33 | int init_psi_monitor(enum psi_stall_type stall_type, |
| 34 | int threshold_us, int window_us) { |
| 35 | int fd; |
| 36 | int res; |
| 37 | char buf[256]; |
| 38 | |
| 39 | fd = TEMP_FAILURE_RETRY(open(PSI_MON_FILE_MEMORY, O_WRONLY | O_CLOEXEC)); |
| 40 | if (fd < 0) { |
| 41 | ALOGE("No kernel psi monitor support (errno=%d)", errno); |
| 42 | return -1; |
| 43 | } |
| 44 | |
| 45 | switch (stall_type) { |
| 46 | case (PSI_SOME): |
| 47 | case (PSI_FULL): |
| 48 | res = snprintf(buf, sizeof(buf), "%s %d %d", |
| 49 | stall_type_name[stall_type], threshold_us, window_us); |
| 50 | break; |
| 51 | default: |
| 52 | ALOGE("Invalid psi stall type: %d", stall_type); |
| 53 | errno = EINVAL; |
| 54 | goto err; |
| 55 | } |
| 56 | |
| 57 | if (res >= (ssize_t)sizeof(buf)) { |
| 58 | ALOGE("%s line overflow for psi stall type '%s'", |
| 59 | PSI_MON_FILE_MEMORY, stall_type_name[stall_type]); |
| 60 | errno = EINVAL; |
| 61 | goto err; |
| 62 | } |
| 63 | |
| 64 | res = TEMP_FAILURE_RETRY(write(fd, buf, strlen(buf) + 1)); |
| 65 | if (res < 0) { |
| 66 | ALOGE("%s write failed for psi stall type '%s'; errno=%d", |
| 67 | PSI_MON_FILE_MEMORY, stall_type_name[stall_type], errno); |
| 68 | goto err; |
| 69 | } |
| 70 | |
| 71 | return fd; |
| 72 | |
| 73 | err: |
| 74 | close(fd); |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | int register_psi_monitor(int epollfd, int fd, void* data) { |
| 79 | int res; |
| 80 | struct epoll_event epev; |
| 81 | |
| 82 | epev.events = EPOLLPRI; |
| 83 | epev.data.ptr = data; |
| 84 | res = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &epev); |
| 85 | if (res < 0) { |
| 86 | ALOGE("epoll_ctl for psi monitor failed; errno=%d", errno); |
| 87 | } |
| 88 | return res; |
| 89 | } |
| 90 | |
| 91 | int unregister_psi_monitor(int epollfd, int fd) { |
| 92 | return epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL); |
| 93 | } |
| 94 | |
| 95 | void destroy_psi_monitor(int fd) { |
| 96 | if (fd >= 0) { |
| 97 | close(fd); |
| 98 | } |
| 99 | } |