• 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 #include <assert.h>
18 #include <errno.h>
19 #include <log/log_id.h>
20 #include <stats_event_list.h>
21 #include <time.h>
22 
getElapsedRealTimeNs()23 static 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 }
29 
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  */
35 int
stats_write_lmk_state_changed(android_log_context ctx,int32_t code,int32_t state)36 stats_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 
43     reset_log_context(ctx);
44 
45     if ((ret = android_log_write_int64(ctx, getElapsedRealTimeNs())) < 0) {
46         return ret;
47     }
48 
49     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     }
56 
57     return write_to_logger(ctx, LOG_ID_STATS);
58 }
59 
60 /**
61  * Logs the event when LMKD kills a process to reduce memory pressure.
62  * Code: LMK_KILL_OCCURRED = 51
63  */
64 int
stats_write_lmk_kill_occurred(android_log_context ctx,int32_t code,int32_t uid,char const * process_name,int32_t oom_score,int64_t pgfault,int64_t pgmajfault,int64_t rss_in_bytes,int64_t cache_in_bytes,int64_t swap_in_bytes,int64_t process_start_time_ns,int32_t min_oom_score)65 stats_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,
68                               int64_t swap_in_bytes, int64_t process_start_time_ns,
69                               int32_t min_oom_score) {
70     assert(ctx != NULL);
71     int ret = -EINVAL;
72     if (!ctx) {
73         return ret;
74     }
75     reset_log_context(ctx);
76 
77     if ((ret = android_log_write_int64(ctx, getElapsedRealTimeNs())) < 0) {
78         return ret;
79     }
80 
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 
117     if ((ret = android_log_write_int64(ctx, process_start_time_ns)) < 0) {
118         return ret;
119     }
120 
121     if ((ret = android_log_write_int32(ctx, min_oom_score)) < 0) {
122         return ret;
123     }
124 
125     return write_to_logger(ctx, LOG_ID_STATS);
126 }
127