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 <assert.h>
21 #include <inttypes.h>
22 #include <statslog_lmkd.h>
23 #include <stdbool.h>
24 #include <sys/cdefs.h>
25 #include <sys/types.h>
26
27 #include <cutils/properties.h>
28
29 __BEGIN_DECLS
30
31 struct memory_stat {
32 int64_t pgfault;
33 int64_t pgmajfault;
34 int64_t rss_in_bytes;
35 int64_t cache_in_bytes;
36 int64_t swap_in_bytes;
37 int64_t process_start_time_ns;
38 };
39
40 #ifdef LMKD_LOG_STATS
41
42 #define MEMCG_PROCESS_MEMORY_STAT_PATH "/dev/memcg/apps/uid_%u/pid_%d/memory.stat"
43 #define PROC_STAT_FILE_PATH "/proc/%d/stat"
44 #define PROC_STAT_BUFFER_SIZE 1024
45 #define BYTES_IN_KILOBYTE 1024
46
47 /**
48 * Logs the change in LMKD state which is used as start/stop boundaries for logging
49 * LMK_KILL_OCCURRED event.
50 * Code: LMK_STATE_CHANGED = 54
51 */
52 int
53 stats_write_lmk_state_changed(int32_t state);
54
55 /**
56 * Logs the event when LMKD kills a process to reduce memory pressure.
57 * Code: LMK_KILL_OCCURRED = 51
58 */
59 int
60 stats_write_lmk_kill_occurred(int32_t uid, char const* process_name,
61 int32_t oom_score, int32_t min_oom_score,
62 int tasksize, struct memory_stat *mem_st);
63
64 /**
65 * Logs the event when LMKD kills a process to reduce memory pressure.
66 * Code: LMK_KILL_OCCURRED = 51
67 */
68 int stats_write_lmk_kill_occurred_pid(int32_t uid, int pid, int32_t oom_score,
69 int32_t min_oom_score, int tasksize,
70 struct memory_stat* mem_st);
71
72 struct memory_stat *stats_read_memory_stat(bool per_app_memcg, int pid, uid_t uid);
73
74 /**
75 * Registers a process taskname by pid, while it is still alive.
76 */
77 void stats_store_taskname(int pid, const char* taskname);
78
79 /**
80 * Unregister all process tasknames.
81 */
82 void stats_purge_tasknames();
83
84 /**
85 * Unregister a process taskname, e.g. after it has been killed.
86 */
87 void stats_remove_taskname(int pid);
88
89 #else /* LMKD_LOG_STATS */
90
91 static inline int
stats_write_lmk_state_changed(int32_t state __unused)92 stats_write_lmk_state_changed(int32_t state __unused) { return -EINVAL; }
93
94 static inline int
stats_write_lmk_kill_occurred(int32_t uid __unused,char const * process_name __unused,int32_t oom_score __unused,int32_t min_oom_score __unused,int tasksize __unused,struct memory_stat * mem_st __unused)95 stats_write_lmk_kill_occurred(int32_t uid __unused,
96 char const* process_name __unused, int32_t oom_score __unused,
97 int32_t min_oom_score __unused, int tasksize __unused,
98 struct memory_stat *mem_st __unused) { return -EINVAL; }
99
stats_write_lmk_kill_occurred_pid(int32_t uid __unused,int pid __unused,int32_t oom_score __unused,int32_t min_oom_score __unused,int tasksize __unused,struct memory_stat * mem_st __unused)100 static inline int stats_write_lmk_kill_occurred_pid(int32_t uid __unused,
101 int pid __unused, int32_t oom_score __unused,
102 int32_t min_oom_score __unused,
103 int tasksize __unused,
104 struct memory_stat* mem_st __unused) {
105 return -EINVAL;
106 }
107
stats_read_memory_stat(bool per_app_memcg __unused,int pid __unused,uid_t uid __unused)108 static inline struct memory_stat *stats_read_memory_stat(bool per_app_memcg __unused,
109 int pid __unused, uid_t uid __unused) { return NULL; }
110
stats_store_taskname(int pid __unused,const char * taskname __unused)111 static inline void stats_store_taskname(int pid __unused, const char* taskname __unused) {}
112
stats_purge_tasknames()113 static inline void stats_purge_tasknames() {}
114
stats_remove_taskname(int pid __unused)115 static inline void stats_remove_taskname(int pid __unused) {}
116
117 #endif /* LMKD_LOG_STATS */
118
119 __END_DECLS
120
121 #endif /* _STATSLOG_H_ */
122