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 #ifndef __ANDROID_PSI_H__
18 #define __ANDROID_PSI_H__
19 
20 #include <sys/cdefs.h>
21 #include <sys/types.h>
22 
23 __BEGIN_DECLS
24 
25 #define PSI_PATH_MEMORY	"/proc/pressure/memory"
26 #define PSI_PATH_IO	"/proc/pressure/io"
27 #define PSI_PATH_CPU	"/proc/pressure/cpu"
28 
29 enum psi_stall_type {
30     PSI_SOME,
31     PSI_FULL,
32     PSI_TYPE_COUNT
33 };
34 
35 struct psi_stats {
36     float avg10;
37     float avg60;
38     float avg300;
39     unsigned long total;
40 };
41 
42 struct psi_data {
43     struct psi_stats mem_stats[PSI_TYPE_COUNT];
44     struct psi_stats io_stats[PSI_TYPE_COUNT];
45     struct psi_stats cpu_stats[PSI_TYPE_COUNT];
46 };
47 
48 /*
49  * Initializes psi monitor.
50  * stall_type, threshold_us and window_us are monitor parameters
51  * When successful, the function returns file descriptor that can
52  * be used with poll/epoll syscalls to wait for EPOLLPRI events.
53  * When unsuccessful, the function returns -1 and errno is set
54  * appropriately.
55  */
56 int init_psi_monitor(enum psi_stall_type stall_type,
57         int threshold_us, int window_us);
58 
59 /*
60  * Registers psi monitor file descriptor fd on the epoll instance
61  * referred to by the file descriptor epollfd.
62  * data parameter will be associated with event's epoll_data.ptr
63  * member.
64  */
65 int register_psi_monitor(int epollfd, int fd, void* data);
66 
67 /*
68  * Unregisters psi monitor file descriptor fd from the epoll instance
69  * referred to by the file descriptor epollfd.
70  */
71 int unregister_psi_monitor(int epollfd, int fd);
72 
73 /*
74  * Destroys psi monitor.
75  * fd is the file descriptor returned by psi monitor initialization
76  * routine.
77  * Note that if user process exits without calling this routine
78  * kernel will destroy the monitor as its lifetime is linked to
79  * the file descriptor.
80  */
81 void destroy_psi_monitor(int fd);
82 
83 /*
84  * Parse psi file line content. Expected file format is:
85  *    some avg10=0.00 avg60=0.00 avg300=0.00 total=0
86  *    full avg10=0.00 avg60=0.00 avg300=0.00 total=0
87  */
88 int parse_psi_line(char *line, enum psi_stall_type stall_type, struct psi_stats stats[]);
89 
90 __END_DECLS
91 
92 #endif  // __ANDROID_PSI_H__
93