blob: a09c7dd91f2281cc4cba3d3bff0892a30fecccc4 [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
Mark Salyzyn61421ba2018-02-01 09:54:32 -080017#ifndef _STATSLOG_H_
18#define _STATSLOG_H_
Rajeev Kumarf0216a82018-01-24 14:40:36 -080019
Rajeev Kumar1c669f72018-03-09 15:20:56 -080020#include <assert.h>
Suren Baghdasaryanb72c6652019-09-04 19:12:29 -070021#include <inttypes.h>
Yao Chen389aee12018-05-02 11:19:27 -070022#include <stats_event_list.h>
Mark Salyzyn61421ba2018-02-01 09:54:32 -080023#include <stdbool.h>
Rajeev Kumarf0216a82018-01-24 14:40:36 -080024#include <sys/cdefs.h>
Mark Salyzyn61421ba2018-02-01 09:54:32 -080025
26#include <cutils/properties.h>
Mark Salyzyn61421ba2018-02-01 09:54:32 -080027
Rajeev Kumarf0216a82018-01-24 14:40:36 -080028__BEGIN_DECLS
29
Suren Baghdasaryanb72c6652019-09-04 19:12:29 -070030struct memory_stat {
31 int64_t pgfault;
32 int64_t pgmajfault;
33 int64_t rss_in_bytes;
34 int64_t cache_in_bytes;
35 int64_t swap_in_bytes;
36 int64_t process_start_time_ns;
37};
38
39struct kernel_poll_info {
40 int poll_fd;
41 void (*handler)(int poll_fd);
42};
43
Rajeev Kumar70450032018-01-31 17:54:56 -080044/*
Mark Salyzyn61421ba2018-02-01 09:54:32 -080045 * These are defined in
46 * http://cs/android/frameworks/base/cmds/statsd/src/atoms.proto
47 */
48#define LMK_KILL_OCCURRED 51
49#define LMK_STATE_CHANGED 54
50#define LMK_STATE_CHANGE_START 1
51#define LMK_STATE_CHANGE_STOP 2
52
Suren Baghdasaryanb72c6652019-09-04 19:12:29 -070053#ifdef LMKD_LOG_STATS
54
Rajeev Kumar1c669f72018-03-09 15:20:56 -080055/*
56 * The single event tag id for all stats logs.
57 * Keep this in sync with system/core/logcat/event.logtags
58 */
59const static int kStatsEventTag = 1937006964;
Mark Salyzyn61421ba2018-02-01 09:54:32 -080060
Suren Baghdasaryanb72c6652019-09-04 19:12:29 -070061void statslog_init();
Rajeev Kumar1c669f72018-03-09 15:20:56 -080062
Suren Baghdasaryanb72c6652019-09-04 19:12:29 -070063void statslog_destroy();
Mark Salyzyn61421ba2018-02-01 09:54:32 -080064
65#define MEMCG_PROCESS_MEMORY_STAT_PATH "/dev/memcg/apps/uid_%u/pid_%u/memory.stat"
Rajeev Kumar4dbc24d2018-10-05 12:34:59 -070066#define PROC_STAT_FILE_PATH "/proc/%d/stat"
67#define PROC_STAT_BUFFER_SIZE 1024
68#define BYTES_IN_KILOBYTE 1024
Mark Salyzyn61421ba2018-02-01 09:54:32 -080069
Rajeev Kumarf0216a82018-01-24 14:40:36 -080070/**
71 * Logs the change in LMKD state which is used as start/stop boundaries for logging
72 * LMK_KILL_OCCURRED event.
73 * Code: LMK_STATE_CHANGED = 54
74 */
75int
Suren Baghdasaryanb72c6652019-09-04 19:12:29 -070076stats_write_lmk_state_changed(int32_t code, int32_t state);
Rajeev Kumarf0216a82018-01-24 14:40:36 -080077
78/**
79 * Logs the event when LMKD kills a process to reduce memory pressure.
80 * Code: LMK_KILL_OCCURRED = 51
81 */
82int
Suren Baghdasaryanb72c6652019-09-04 19:12:29 -070083stats_write_lmk_kill_occurred(int32_t code, int32_t uid,
84 char const* process_name, int32_t oom_score, int32_t min_oom_score,
85 int tasksize, struct memory_stat *mem_st);
Jim Blacklerd2da8142019-09-10 15:30:05 +010086
Suren Baghdasaryanb72c6652019-09-04 19:12:29 -070087struct memory_stat *stats_read_memory_stat(bool per_app_memcg, int pid, uid_t uid);
Mark Salyzyn61421ba2018-02-01 09:54:32 -080088
Jim Blacklerd2da8142019-09-10 15:30:05 +010089/**
90 * Registers a process taskname by pid, while it is still alive.
91 */
Suren Baghdasaryanb72c6652019-09-04 19:12:29 -070092void stats_store_taskname(int pid, const char* taskname, int poll_fd);
Jim Blacklerd2da8142019-09-10 15:30:05 +010093
94/**
95 * Unregister all process tasknames.
96 */
Suren Baghdasaryanb72c6652019-09-04 19:12:29 -070097void stats_purge_tasknames();
Jim Blacklerd2da8142019-09-10 15:30:05 +010098
99/**
100 * Unregister a process taskname, e.g. after it has been killed.
101 */
Suren Baghdasaryanb72c6652019-09-04 19:12:29 -0700102void stats_remove_taskname(int pid, int poll_fd);
103
104bool init_poll_kernel(struct kernel_poll_info *poll_info);
105
106#else /* LMKD_LOG_STATS */
107
108static inline void statslog_init() {}
109static inline void statslog_destroy() {}
110
111static inline int
112stats_write_lmk_state_changed(int32_t code __unused, int32_t state __unused) { return -EINVAL; }
113
114static inline int
115stats_write_lmk_kill_occurred(int32_t code __unused, int32_t uid __unused,
116 char const* process_name __unused, int32_t oom_score __unused,
117 int32_t min_oom_score __unused, int tasksize __unused,
118 struct memory_stat *mem_st __unused) { return -EINVAL; }
119
120static inline struct memory_stat *stats_read_memory_stat(bool per_app_memcg __unused,
121 int pid __unused, uid_t uid __unused) { return NULL; }
122
123static inline void stats_store_taskname(int pid __unused, const char* taskname __unused,
124 int poll_fd __unused) {}
125
126static inline void stats_purge_tasknames() {}
127
128static inline void stats_remove_taskname(int pid __unused, int poll_fd __unused) {}
129
130static inline bool init_poll_kernel(struct kernel_poll_info *poll_info __unused) { return false; }
131
132#endif /* LMKD_LOG_STATS */
Jim Blacklerd2da8142019-09-10 15:30:05 +0100133
Rajeev Kumarf0216a82018-01-24 14:40:36 -0800134__END_DECLS
Mark Salyzyn61421ba2018-02-01 09:54:32 -0800135
136#endif /* _STATSLOG_H_ */