blob: f3a6e55d86bdc0b132ffafe7fdfb1c0c54b9cc4b [file] [log] [blame]
Rajeev Kumarf0216a82018-01-24 14:40:36 -08001/*
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#include <assert.h>
18#include <errno.h>
Rajeev Kumarf0216a82018-01-24 14:40:36 -080019#include <log/log_id.h>
Yao Chen389aee12018-05-02 11:19:27 -070020#include <stats_event_list.h>
Jim Blacklerd2da8142019-09-10 15:30:05 +010021#include <stdlib.h>
22#include <string.h>
Yao Chen389aee12018-05-02 11:19:27 -070023#include <time.h>
24
Jim Blacklerd2da8142019-09-10 15:30:05 +010025#define LINE_MAX 128
26
27struct proc {
28 int pid;
29 char taskname[LINE_MAX];
30 struct proc* pidhash_next;
31};
32
33#define PIDHASH_SZ 1024
34static struct proc** pidhash = NULL;
35#define pid_hashfn(x) ((((x) >> 8) ^ (x)) & (PIDHASH_SZ - 1))
36
Yao Chen389aee12018-05-02 11:19:27 -070037static int64_t getElapsedRealTimeNs() {
38 struct timespec t;
39 t.tv_sec = t.tv_nsec = 0;
40 clock_gettime(CLOCK_BOOTTIME, &t);
41 return (int64_t)t.tv_sec * 1000000000LL + t.tv_nsec;
42}
Rajeev Kumarf0216a82018-01-24 14:40:36 -080043
44/**
45 * Logs the change in LMKD state which is used as start/stop boundaries for logging
46 * LMK_KILL_OCCURRED event.
47 * Code: LMK_STATE_CHANGED = 54
48 */
49int
50stats_write_lmk_state_changed(android_log_context ctx, int32_t code, int32_t state) {
51 assert(ctx != NULL);
52 int ret = -EINVAL;
53 if (!ctx) {
54 return ret;
55 }
56
Yao Chen389aee12018-05-02 11:19:27 -070057 reset_log_context(ctx);
58
59 if ((ret = android_log_write_int64(ctx, getElapsedRealTimeNs())) < 0) {
60 return ret;
61 }
62
Rajeev Kumarf0216a82018-01-24 14:40:36 -080063 if ((ret = android_log_write_int32(ctx, code)) < 0) {
64 return ret;
65 }
66
67 if ((ret = android_log_write_int32(ctx, state)) < 0) {
68 return ret;
69 }
Yao Chen389aee12018-05-02 11:19:27 -070070
71 return write_to_logger(ctx, LOG_ID_STATS);
Rajeev Kumarf0216a82018-01-24 14:40:36 -080072}
73
Jim Blacklerd2da8142019-09-10 15:30:05 +010074static struct proc* pid_lookup(int pid) {
75 struct proc* procp;
76
77 if (!pidhash) return NULL;
78
79 for (procp = pidhash[pid_hashfn(pid)]; procp && procp->pid != pid; procp = procp->pidhash_next)
80 ;
81
82 return procp;
83}
84
Rajeev Kumarf0216a82018-01-24 14:40:36 -080085/**
86 * Logs the event when LMKD kills a process to reduce memory pressure.
87 * Code: LMK_KILL_OCCURRED = 51
88 */
89int
90stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid,
91 char const* process_name, int32_t oom_score, int64_t pgfault,
92 int64_t pgmajfault, int64_t rss_in_bytes, int64_t cache_in_bytes,
Suren Baghdasaryanec5e4c62019-03-04 11:07:39 -080093 int64_t swap_in_bytes, int64_t process_start_time_ns,
94 int32_t min_oom_score) {
Rajeev Kumarf0216a82018-01-24 14:40:36 -080095 assert(ctx != NULL);
96 int ret = -EINVAL;
97 if (!ctx) {
98 return ret;
99 }
Yao Chen389aee12018-05-02 11:19:27 -0700100 reset_log_context(ctx);
101
102 if ((ret = android_log_write_int64(ctx, getElapsedRealTimeNs())) < 0) {
103 return ret;
104 }
Rajeev Kumarf0216a82018-01-24 14:40:36 -0800105
106 if ((ret = android_log_write_int32(ctx, code)) < 0) {
107 return ret;
108 }
109
110 if ((ret = android_log_write_int32(ctx, uid)) < 0) {
111 return ret;
112 }
113
114 if ((ret = android_log_write_string8(ctx, (process_name == NULL) ? "" : process_name)) < 0) {
115 return ret;
116 }
117
118 if ((ret = android_log_write_int32(ctx, oom_score)) < 0) {
119 return ret;
120 }
121
122 if ((ret = android_log_write_int64(ctx, pgfault)) < 0) {
123 return ret;
124 }
125
126 if ((ret = android_log_write_int64(ctx, pgmajfault)) < 0) {
127 return ret;
128 }
129
130 if ((ret = android_log_write_int64(ctx, rss_in_bytes)) < 0) {
131 return ret;
132 }
133
134 if ((ret = android_log_write_int64(ctx, cache_in_bytes)) < 0) {
135 return ret;
136 }
137
138 if ((ret = android_log_write_int64(ctx, swap_in_bytes)) < 0) {
139 return ret;
140 }
141
Jim Blackler1417cdb2018-11-21 16:22:36 +0000142 if ((ret = android_log_write_int64(ctx, process_start_time_ns)) < 0) {
143 return ret;
144 }
145
Suren Baghdasaryanec5e4c62019-03-04 11:07:39 -0800146 if ((ret = android_log_write_int32(ctx, min_oom_score)) < 0) {
147 return ret;
148 }
149
Yao Chen389aee12018-05-02 11:19:27 -0700150 return write_to_logger(ctx, LOG_ID_STATS);
Rajeev Kumarf0216a82018-01-24 14:40:36 -0800151}
Jim Blacklerd2da8142019-09-10 15:30:05 +0100152
153int stats_write_lmk_kill_occurred_pid(android_log_context ctx, int32_t code, int32_t uid, int pid,
154 int32_t oom_score, int64_t pgfault, int64_t pgmajfault,
155 int64_t rss_in_bytes, int64_t cache_in_bytes,
156 int64_t swap_in_bytes, int64_t process_start_time_ns,
157 int32_t min_oom_score) {
158 struct proc* proc = pid_lookup(pid);
159 if (!proc) return -EINVAL;
160
161 return stats_write_lmk_kill_occurred(ctx, code, uid, proc->taskname, oom_score, pgfault,
162 pgmajfault, rss_in_bytes, cache_in_bytes, swap_in_bytes,
163 process_start_time_ns, min_oom_score);
164}
165
166static void proc_insert(struct proc* procp) {
167 if (!pidhash)
168 pidhash = calloc(PIDHASH_SZ, sizeof(struct proc));
169 int hval = pid_hashfn(procp->pid);
170 procp->pidhash_next = pidhash[hval];
171 pidhash[hval] = procp;
172}
173
174void stats_remove_taskname(int pid) {
175 if (!pidhash) return;
176
177 int hval = pid_hashfn(pid);
178 struct proc* procp;
179 struct proc* prevp;
180
181 for (procp = pidhash[hval], prevp = NULL; procp && procp->pid != pid;
182 procp = procp->pidhash_next)
183 prevp = procp;
184
185 if (!procp)
186 return;
187
188 if (!prevp)
189 pidhash[hval] = procp->pidhash_next;
190 else
191 prevp->pidhash_next = procp->pidhash_next;
192
193 free(procp);
194}
195
196void stats_store_taskname(int pid, const char* taskname) {
197 struct proc* procp = pid_lookup(pid);
198 if (procp != NULL && strcmp(procp->taskname, taskname) == 0)
199 return;
200 procp = malloc(sizeof(struct proc));
201 stats_remove_taskname(pid);
202 procp->pid = pid;
203 strncpy(procp->taskname, taskname, LINE_MAX - 1);
204 procp->taskname[LINE_MAX - 1] = '\0';
205 proc_insert(procp);
206}
207
208void stats_purge_tasknames() {
209 if (!pidhash) return;
210 struct proc* procp;
211 struct proc* next;
212 int i;
213 for (i = 0; i < PIDHASH_SZ; i++) {
214 procp = pidhash[i];
215 while (procp) {
216 next = procp->pidhash_next;
217 free(procp);
218 procp = next;
219 }
220 }
221 memset(pidhash, 0, PIDHASH_SZ * sizeof(struct proc));
222}