Home | History | Annotate | Download | only in lmkd
      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 
     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
     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
     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) {
     69     assert(ctx != NULL);
     70     int ret = -EINVAL;
     71     if (!ctx) {
     72         return ret;
     73     }
     74     reset_log_context(ctx);
     75 
     76     if ((ret = android_log_write_int64(ctx, getElapsedRealTimeNs())) < 0) {
     77         return ret;
     78     }
     79 
     80     if ((ret = android_log_write_int32(ctx, code)) < 0) {
     81         return ret;
     82     }
     83 
     84     if ((ret = android_log_write_int32(ctx, uid)) < 0) {
     85         return ret;
     86     }
     87 
     88     if ((ret = android_log_write_string8(ctx, (process_name == NULL) ? "" : process_name)) < 0) {
     89         return ret;
     90     }
     91 
     92     if ((ret = android_log_write_int32(ctx, oom_score)) < 0) {
     93         return ret;
     94     }
     95 
     96     if ((ret = android_log_write_int64(ctx, pgfault)) < 0) {
     97         return ret;
     98     }
     99 
    100     if ((ret = android_log_write_int64(ctx, pgmajfault)) < 0) {
    101         return ret;
    102     }
    103 
    104     if ((ret = android_log_write_int64(ctx, rss_in_bytes)) < 0) {
    105         return ret;
    106     }
    107 
    108     if ((ret = android_log_write_int64(ctx, cache_in_bytes)) < 0) {
    109         return ret;
    110     }
    111 
    112     if ((ret = android_log_write_int64(ctx, swap_in_bytes)) < 0) {
    113         return ret;
    114     }
    115 
    116     return write_to_logger(ctx, LOG_ID_STATS);
    117 }
    118