• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cutils/fs.h>
28 #include <stdio.h>
29 #include "psi/psi.h"
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,enum psi_resource resource)36 int init_psi_monitor(enum psi_stall_type stall_type, int threshold_us, int window_us,
37                      enum psi_resource resource) {
38     if (resource < PSI_MEMORY || resource >= PSI_RESOURCE_COUNT) {
39         ALOGE("Invalid psi resource type: %d", resource);
40         errno = EINVAL;
41         return -1;
42     }
43     int fd;
44     int res;
45     char buf[256];
46 
47     fd = TEMP_FAILURE_RETRY(open(psi_resource_file[resource], O_WRONLY | O_CLOEXEC));
48     if (fd < 0) {
49         ALOGE("No kernel psi monitor support (errno=%d)", errno);
50         return -1;
51     }
52 
53     switch (stall_type) {
54     case (PSI_SOME):
55     case (PSI_FULL):
56         res = snprintf(buf, sizeof(buf), "%s %d %d",
57             stall_type_name[stall_type], threshold_us, window_us);
58         break;
59     default:
60         ALOGE("Invalid psi stall type: %d", stall_type);
61         errno = EINVAL;
62         goto err;
63     }
64 
65     if (res >= (ssize_t)sizeof(buf)) {
66         ALOGE("%s line overflow for psi stall type '%s'", psi_resource_file[resource],
67               stall_type_name[stall_type]);
68         errno = EINVAL;
69         goto err;
70     }
71 
72     res = TEMP_FAILURE_RETRY(write(fd, buf, strlen(buf) + 1));
73     if (res < 0) {
74         ALOGE("%s write failed for psi stall type '%s'; errno=%d", psi_resource_file[resource],
75               stall_type_name[stall_type], errno);
76         goto err;
77     }
78 
79     return fd;
80 
81 err:
82     close(fd);
83     return -1;
84 }
85 
register_psi_monitor(int epollfd,int fd,void * data)86 int register_psi_monitor(int epollfd, int fd, void* data) {
87     int res;
88     struct epoll_event epev;
89 
90     epev.events = EPOLLPRI;
91     epev.data.ptr = data;
92     res = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &epev);
93     if (res < 0) {
94         ALOGE("epoll_ctl for psi monitor failed; errno=%d", errno);
95     }
96     return res;
97 }
98 
unregister_psi_monitor(int epollfd,int fd)99 int unregister_psi_monitor(int epollfd, int fd) {
100     return epoll_ctl(epollfd, EPOLL_CTL_DEL, fd, NULL);
101 }
102 
destroy_psi_monitor(int fd)103 void destroy_psi_monitor(int fd) {
104     if (fd >= 0) {
105         close(fd);
106     }
107 }
108 
parse_psi_line(char * line,enum psi_stall_type stall_type,struct psi_stats stats[])109 int parse_psi_line(char *line, enum psi_stall_type stall_type, struct psi_stats stats[]) {
110     char type_name[5];
111     struct psi_stats *stat = &stats[stall_type];
112 
113     if (!line || sscanf(line, "%4s avg10=%f avg60=%f avg300=%f total=%lu",
114         type_name, &stat->avg10, &stat->avg60, &stat->avg300, &stat->total) != 5) {
115         return -1;
116     }
117     if (strcmp(type_name, stall_type_name[stall_type])) {
118         return -1;
119     }
120     return 0;
121 }
122