blob: db7a76a22b6acbefce435e2360d0c0c2ebd5471a [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>
19#include <log/log_event_list.h>
20#include <log/log_id.h>
21
22/**
23 * Logs the change in LMKD state which is used as start/stop boundaries for logging
24 * LMK_KILL_OCCURRED event.
25 * Code: LMK_STATE_CHANGED = 54
26 */
27int
28stats_write_lmk_state_changed(android_log_context ctx, int32_t code, int32_t state) {
29 assert(ctx != NULL);
30 int ret = -EINVAL;
31 if (!ctx) {
32 return ret;
33 }
34
35 if ((ret = android_log_write_int32(ctx, code)) < 0) {
36 return ret;
37 }
38
39 if ((ret = android_log_write_int32(ctx, state)) < 0) {
40 return ret;
41 }
42 return ret;
43}
44
45/**
46 * Logs the event when LMKD kills a process to reduce memory pressure.
47 * Code: LMK_KILL_OCCURRED = 51
48 */
49int
50stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid,
51 char const* process_name, int32_t oom_score, int64_t pgfault,
52 int64_t pgmajfault, int64_t rss_in_bytes, int64_t cache_in_bytes,
53 int64_t swap_in_bytes) {
54 assert(ctx != NULL);
55 int ret = -EINVAL;
56 if (!ctx) {
57 return ret;
58 }
59
60 if ((ret = android_log_write_int32(ctx, code)) < 0) {
61 return ret;
62 }
63
64 if ((ret = android_log_write_int32(ctx, uid)) < 0) {
65 return ret;
66 }
67
68 if ((ret = android_log_write_string8(ctx, (process_name == NULL) ? "" : process_name)) < 0) {
69 return ret;
70 }
71
72 if ((ret = android_log_write_int32(ctx, oom_score)) < 0) {
73 return ret;
74 }
75
76 if ((ret = android_log_write_int64(ctx, pgfault)) < 0) {
77 return ret;
78 }
79
80 if ((ret = android_log_write_int64(ctx, pgmajfault)) < 0) {
81 return ret;
82 }
83
84 if ((ret = android_log_write_int64(ctx, rss_in_bytes)) < 0) {
85 return ret;
86 }
87
88 if ((ret = android_log_write_int64(ctx, cache_in_bytes)) < 0) {
89 return ret;
90 }
91
92 if ((ret = android_log_write_int64(ctx, swap_in_bytes)) < 0) {
93 return ret;
94 }
95
96 if ((ret = android_log_write_list(ctx, LOG_ID_STATS)) < 0) {
97 return ret;
98 }
99 return ret;
100}