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