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 MEMCG_PROCESS_MEMORY_STAT_PATH "/dev/memcg/apps/uid_%u/pid_%d/memory.stat"
96 #define PROC_STAT_FILE_PATH "/proc/%d/stat"
97 #define PROC_STAT_BUFFER_SIZE 1024
98 #define BYTES_IN_KILOBYTE 1024
99
100 /**
101 * Produces packet with the change in LMKD state which is used as start/stop boundaries for logging
102 * LMK_KILL_OCCURRED event.
103 * Code: LMK_STATE_CHANGED = 54
104 */
105 size_t lmkd_pack_set_state_changed(LMKD_CTRL_PACKET packet,
106 enum lmk_state state);
107
108 /**
109 * Produces packet with the event when LMKD kills a process to reduce memory pressure.
110 * Code: LMK_KILL_OCCURRED = 51
111 */
112 size_t lmkd_pack_set_kill_occurred(LMK_KILL_OCCURRED_PACKET packet,
113 struct kill_stat *kill_st,
114 struct memory_stat *mem_st);
115
116 /**
117 * Reads memory stats used to log the statsd atom. Returns non-null ptr on success.
118 */
119 struct memory_stat *stats_read_memory_stat(bool per_app_memcg, int pid, uid_t uid,
120 int64_t rss_bytes, int64_t swap_bytes);
121
122 /**
123 * Registers a process taskname by pid, while it is still alive.
124 */
125 void stats_store_taskname(int pid, const char* taskname);
126
127 /**
128 * Unregister all process tasknames.
129 */
130 void stats_purge_tasknames();
131
132 /**
133 * Unregister a process taskname, e.g. after it has been killed.
134 */
135 void stats_remove_taskname(int pid);
136
137 const char* stats_get_task_name(int pid);
138
139 #else /* LMKD_LOG_STATS */
140
141 static inline size_t
lmkd_pack_set_state_changed(LMKD_CTRL_PACKET packet __unused,enum lmk_state state __unused)142 lmkd_pack_set_state_changed(LMKD_CTRL_PACKET packet __unused, enum lmk_state state __unused) {
143 return -EINVAL;
144 }
145
146 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)147 lmkd_pack_set_kill_occurred(LMK_KILL_OCCURRED_PACKET packet __unused,
148 struct kill_stat *kill_st __unused,
149 struct memory_stat *mem_st __unused) {
150 return -EINVAL;
151 }
152
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)153 static inline struct memory_stat *stats_read_memory_stat(bool per_app_memcg __unused,
154 int pid __unused, uid_t uid __unused,
155 int64_t rss_bytes __unused, int64_t swap_bytes __unused) {
156 return NULL;
157 }
158
stats_store_taskname(int pid __unused,const char * taskname __unused)159 static inline void stats_store_taskname(int pid __unused, const char* taskname __unused) {}
160
stats_purge_tasknames()161 static inline void stats_purge_tasknames() {}
162
stats_remove_taskname(int pid __unused)163 static inline void stats_remove_taskname(int pid __unused) {}
164
stats_get_task_name(int pid __unused)165 static inline const char* stats_get_task_name(int pid __unused) { return NULL; }
166
167 #endif /* LMKD_LOG_STATS */
168
169 __END_DECLS
170
171 #endif /* _STATSLOG_H_ */
172