blob: 0c230aeb908b2fb9a57590667db3a3ae7cd0f6de [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>
21#include <time.h>
22
23static int64_t getElapsedRealTimeNs() {
24 struct timespec t;
25 t.tv_sec = t.tv_nsec = 0;
26 clock_gettime(CLOCK_BOOTTIME, &t);
27 return (int64_t)t.tv_sec * 1000000000LL + t.tv_nsec;
28}
Rajeev Kumarf0216a82018-01-24 14:40:36 -080029
30/**
31 * Logs the change in LMKD state which is used as start/stop boundaries for logging
32 * LMK_KILL_OCCURRED event.
33 * Code: LMK_STATE_CHANGED = 54
34 */
35int
36stats_write_lmk_state_changed(android_log_context ctx, int32_t code, int32_t state) {
37 assert(ctx != NULL);
38 int ret = -EINVAL;
39 if (!ctx) {
40 return ret;
41 }
42
Yao Chen389aee12018-05-02 11:19:27 -070043 reset_log_context(ctx);
44
45 if ((ret = android_log_write_int64(ctx, getElapsedRealTimeNs())) < 0) {
46 return ret;
47 }
48
Rajeev Kumarf0216a82018-01-24 14:40:36 -080049 if ((ret = android_log_write_int32(ctx, code)) < 0) {
50 return ret;
51 }
52
53 if ((ret = android_log_write_int32(ctx, state)) < 0) {
54 return ret;
55 }
Yao Chen389aee12018-05-02 11:19:27 -070056
57 return write_to_logger(ctx, LOG_ID_STATS);
Rajeev Kumarf0216a82018-01-24 14:40:36 -080058}
59
60/**
61 * Logs the event when LMKD kills a process to reduce memory pressure.
62 * Code: LMK_KILL_OCCURRED = 51
63 */
64int
65stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid,
66 char const* process_name, int32_t oom_score, int64_t pgfault,
67 int64_t pgmajfault, int64_t rss_in_bytes, int64_t cache_in_bytes,
Suren Baghdasaryanec5e4c62019-03-04 11:07:39 -080068 int64_t swap_in_bytes, int64_t process_start_time_ns,
69 int32_t min_oom_score) {
Rajeev Kumarf0216a82018-01-24 14:40:36 -080070 assert(ctx != NULL);
71 int ret = -EINVAL;
72 if (!ctx) {
73 return ret;
74 }
Yao Chen389aee12018-05-02 11:19:27 -070075 reset_log_context(ctx);
76
77 if ((ret = android_log_write_int64(ctx, getElapsedRealTimeNs())) < 0) {
78 return ret;
79 }
Rajeev Kumarf0216a82018-01-24 14:40:36 -080080
81 if ((ret = android_log_write_int32(ctx, code)) < 0) {
82 return ret;
83 }
84
85 if ((ret = android_log_write_int32(ctx, uid)) < 0) {
86 return ret;
87 }
88
89 if ((ret = android_log_write_string8(ctx, (process_name == NULL) ? "" : process_name)) < 0) {
90 return ret;
91 }
92
93 if ((ret = android_log_write_int32(ctx, oom_score)) < 0) {
94 return ret;
95 }
96
97 if ((ret = android_log_write_int64(ctx, pgfault)) < 0) {
98 return ret;
99 }
100
101 if ((ret = android_log_write_int64(ctx, pgmajfault)) < 0) {
102 return ret;
103 }
104
105 if ((ret = android_log_write_int64(ctx, rss_in_bytes)) < 0) {
106 return ret;
107 }
108
109 if ((ret = android_log_write_int64(ctx, cache_in_bytes)) < 0) {
110 return ret;
111 }
112
113 if ((ret = android_log_write_int64(ctx, swap_in_bytes)) < 0) {
114 return ret;
115 }
116
Jim Blackler1417cdb2018-11-21 16:22:36 +0000117 if ((ret = android_log_write_int64(ctx, process_start_time_ns)) < 0) {
118 return ret;
119 }
120
Suren Baghdasaryanec5e4c62019-03-04 11:07:39 -0800121 if ((ret = android_log_write_int32(ctx, min_oom_score)) < 0) {
122 return ret;
123 }
124
Yao Chen389aee12018-05-02 11:19:27 -0700125 return write_to_logger(ctx, LOG_ID_STATS);
Rajeev Kumarf0216a82018-01-24 14:40:36 -0800126}