statslog.h revision 61421baf96b45168ff7039d6c6b3e0649dcfb31d
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 <stdbool.h>
21#include <sys/cdefs.h>
22
23#include <cutils/properties.h>
24#include <log/log_event_list.h>
25
26__BEGIN_DECLS
27
28/*
29 * These are defined in
30 * http://cs/android/frameworks/base/cmds/statsd/src/atoms.proto
31 */
32#define LMK_KILL_OCCURRED 51
33#define LMK_STATE_CHANGED 54
34#define LMK_STATE_CHANGE_START 1
35#define LMK_STATE_CHANGE_STOP 2
36
37static inline void statslog_init() {
38    enable_stats_log = property_get_bool("ro.lmk.log_stats", false);
39
40    if (enable_stats_log) {
41        log_ctx = create_android_logger(kStatsEventTag);
42    }
43}
44
45static inline void statslog_destroy() {
46    if (log_ctx) {
47        android_log_destroy(&log_ctx);
48    }
49}
50
51struct memory_stat {
52    int64_t pgfault;
53    int64_t pgmajfault;
54    int64_t rss_in_bytes;
55    int64_t cache_in_bytes;
56    int64_t swap_in_bytes;
57};
58
59#define MEMCG_PROCESS_MEMORY_STAT_PATH "/dev/memcg/apps/uid_%u/pid_%u/memory.stat"
60
61/*
62 * The single event tag id for all stats logs.
63 * Keep this in sync with system/core/logcat/event.logtags
64 */
65const static int kStatsEventTag = 1937006964;
66
67/**
68 * Logs the change in LMKD state which is used as start/stop boundaries for logging
69 * LMK_KILL_OCCURRED event.
70 * Code: LMK_STATE_CHANGED = 54
71 */
72int
73stats_write_lmk_state_changed(android_log_context ctx, int32_t code, int32_t state);
74
75/**
76 * Logs the event when LMKD kills a process to reduce memory pressure.
77 * Code: LMK_KILL_OCCURRED = 51
78 */
79int
80stats_write_lmk_kill_occurred(android_log_context ctx, int32_t code, int32_t uid,
81                              char const* process_name, int32_t oom_score, int64_t pgfault,
82                              int64_t pgmajfault, int64_t rss_in_bytes, int64_t cache_in_bytes,
83                              int64_t swap_in_bytes);
84
85__END_DECLS
86
87#endif /* _STATSLOG_H_ */
88