• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2018 Google, Inc
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 _STATSLOG_H_
18 #define _STATSLOG_H_
19 
20 #include <lmkd.h>
21 
22 #include <assert.h>
23 #include <inttypes.h>
24 #include <stdbool.h>
25 #include <sys/cdefs.h>
26 #include <sys/types.h>
27 
28 #include <cutils/properties.h>
29 
30 __BEGIN_DECLS
31 
32 #define MAX_TASKNAME_LEN 128
33 
34 /*
35  * Max LMKD reply packet length in bytes
36  * Notes about size calculation:
37  * 4 bytes for packet type
38  * 88 bytes for the LmkKillOccurred fields: memory_stat + kill_stat
39  * 2 bytes for process name string size
40  * MAX_TASKNAME_LEN bytes for the process name string
41  *
42  * Must be in sync with LmkdConnection.java
43  */
44 #define LMKD_REPLY_MAX_SIZE 222
45 
46 /* LMK_MEMORY_STATS packet payload */
47 struct memory_stat {
48     int64_t pgfault;
49     int64_t pgmajfault;
50     int64_t rss_in_bytes;
51     int64_t cache_in_bytes;
52     int64_t swap_in_bytes;
53     int64_t process_start_time_ns;
54 };
55 
56 // If you update this, also update the corresponding stats enum mapping and LmkdStatsReporter.java
57 enum kill_reasons {
58     NONE = -1, /* To denote no kill condition */
59     PRESSURE_AFTER_KILL = 0,
60     NOT_RESPONDING,
61     LOW_SWAP_AND_THRASHING,
62     LOW_MEM_AND_SWAP,
63     LOW_MEM_AND_THRASHING,
64     DIRECT_RECL_AND_THRASHING,
65     LOW_MEM_AND_SWAP_UTIL,
66     LOW_FILECACHE_AFTER_THRASHING,
67     KILL_REASON_COUNT
68 };
69 
70 /* LMK_KILL_STAT packet payload */
71 struct kill_stat {
72     int32_t uid;
73     const char *taskname;
74     enum kill_reasons kill_reason;
75     int32_t oom_score;
76     int32_t min_oom_score;
77     int64_t free_mem_kb;
78     int64_t free_swap_kb;
79     int32_t thrashing;
80     int32_t max_thrashing;
81 };
82 
83 /* LMKD reply packet to hold data for the LmkKillOccurred statsd atom */
84 typedef char LMK_KILL_OCCURRED_PACKET[LMKD_REPLY_MAX_SIZE];
85 
86 // If you update this, also update the corresponding stats enum mapping.
87 enum lmk_state {
88     STATE_UNKNOWN = 0,
89     STATE_START,
90     STATE_STOP,
91 };
92 
93 #ifdef LMKD_LOG_STATS
94 
95 #define PROC_STAT_FILE_PATH "/proc/%d/stat"
96 #define PROC_STAT_BUFFER_SIZE 1024
97 #define BYTES_IN_KILOBYTE 1024
98 
99 /**
100  * Produces packet with the change in LMKD state which is used as start/stop boundaries for logging
101  * LMK_KILL_OCCURRED event.
102  * Code: LMK_STATE_CHANGED = 54
103  */
104 size_t lmkd_pack_set_state_changed(LMKD_CTRL_PACKET packet,
105                                    enum lmk_state state);
106 
107 /**
108  * Produces packet with the event when LMKD kills a process to reduce memory pressure.
109  * Code: LMK_KILL_OCCURRED = 51
110  */
111 size_t lmkd_pack_set_kill_occurred(LMK_KILL_OCCURRED_PACKET packet,
112                                    struct kill_stat *kill_st,
113                                    struct memory_stat *mem_st);
114 
115 /**
116  * Reads memory stats used to log the statsd atom. Returns non-null ptr on success.
117  */
118 struct memory_stat *stats_read_memory_stat(bool per_app_memcg, int pid, uid_t uid,
119                                            int64_t rss_bytes, int64_t swap_bytes);
120 
121 /**
122  * Registers a process taskname by pid, while it is still alive.
123  */
124 void stats_store_taskname(int pid, const char* taskname);
125 
126 /**
127  * Unregister all process tasknames.
128  */
129 void stats_purge_tasknames();
130 
131 /**
132  * Unregister a process taskname, e.g. after it has been killed.
133  */
134 void stats_remove_taskname(int pid);
135 
136 const char* stats_get_task_name(int pid);
137 
138 #else /* LMKD_LOG_STATS */
139 
140 static inline size_t
lmkd_pack_set_state_changed(LMKD_CTRL_PACKET packet __unused,enum lmk_state state __unused)141 lmkd_pack_set_state_changed(LMKD_CTRL_PACKET packet __unused, enum lmk_state state __unused) {
142     return -EINVAL;
143 }
144 
145 static inline size_t
lmkd_pack_set_kill_occurred(LMK_KILL_OCCURRED_PACKET packet __unused,struct kill_stat * kill_st __unused,struct memory_stat * mem_st __unused)146 lmkd_pack_set_kill_occurred(LMK_KILL_OCCURRED_PACKET packet __unused,
147                             struct kill_stat *kill_st __unused,
148                             struct memory_stat *mem_st __unused) {
149     return -EINVAL;
150 }
151 
stats_read_memory_stat(bool per_app_memcg __unused,int pid __unused,uid_t uid __unused,int64_t rss_bytes __unused,int64_t swap_bytes __unused)152 static inline struct memory_stat *stats_read_memory_stat(bool per_app_memcg __unused,
153                                     int pid __unused, uid_t uid __unused,
154                                     int64_t rss_bytes __unused, int64_t swap_bytes __unused) {
155     return NULL;
156 }
157 
stats_store_taskname(int pid __unused,const char * taskname __unused)158 static inline void stats_store_taskname(int pid __unused, const char* taskname __unused) {}
159 
stats_purge_tasknames()160 static inline void stats_purge_tasknames() {}
161 
stats_remove_taskname(int pid __unused)162 static inline void stats_remove_taskname(int pid __unused) {}
163 
stats_get_task_name(int pid __unused)164 static inline const char* stats_get_task_name(int pid __unused) { return NULL; }
165 
166 #endif /* LMKD_LOG_STATS */
167 
168 __END_DECLS
169 
170 #endif /* _STATSLOG_H_ */
171