statslog.h 2.8 KB

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