Home | History | Annotate | Download | only in ANRdaemon
      1 /*
      2  * Copyright (c) 2015, The Android Open Source Project
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *   * Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  *   * Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer
     12  *     in the documentation and/or other materials provided with the
     13  *     distribution.
     14  *   * Neither the name of Google, Inc. nor the names of its contributors
     15  *     may be used to endorse or promote products derived from this
     16  *     software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     22  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     24  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     25  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     26  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     28  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <binder/IBinder.h>
     33 #include <binder/IServiceManager.h>
     34 #include <binder/Parcel.h>
     35 
     36 #include <ctime>
     37 #include <cutils/properties.h>
     38 #include <signal.h>
     39 #include <stdbool.h>
     40 #include <stdio.h>
     41 #include <stdlib.h>
     42 #include <string.h>
     43 
     44 #include <sys/resource.h>
     45 #include <sys/stat.h>
     46 #include <sys/time.h>
     47 #include <sys/types.h>
     48 #include <unistd.h>
     49 
     50 #include <utils/Log.h>
     51 #include <utils/String8.h>
     52 #include <utils/Trace.h>
     53 #include <zlib.h>
     54 
     55 using namespace android;
     56 
     57 #ifdef LOG_TAG
     58 #undef LOG_TAG
     59 #endif
     60 
     61 #define LOG_TAG "anrdaemon"
     62 
     63 static const int check_period = 1;              // in sec
     64 static const int tracing_check_period = 500000; // in micro sec
     65 static const int cpu_stat_entries = 7;          // number of cpu stat entries
     66 static const int min_buffer_size = 16;
     67 static const int max_buffer_size = 2048;
     68 static const char *min_buffer_size_str = "16";
     69 static const char *max_buffer_size_str = "2048";
     70 static const int time_buf_size = 20;
     71 static const int path_buf_size = 60;
     72 
     73 typedef struct cpu_stat {
     74     unsigned long utime, ntime, stime, itime;
     75     unsigned long iowtime, irqtime, sirqtime, steal;
     76     unsigned long total;
     77 } cpu_stat_t;
     78 
     79 /*
     80  * Logging on/off threshold.
     81  * Uint: 0.01%; default to 99.90% cpu.
     82  */
     83 static int idle_threshold = 10;
     84 
     85 static bool quit = false;
     86 static bool suspend= false;
     87 static bool err = false;
     88 static char err_msg[100];
     89 static bool tracing = false;
     90 
     91 static const char *buf_size_kb = "2048";
     92 static const char *apps = "";
     93 static uint64_t tag = 0;
     94 
     95 static cpu_stat_t new_cpu;
     96 static cpu_stat_t old_cpu;
     97 
     98 /* Log certain kernel activity when enabled */
     99 static bool log_sched = false;
    100 static bool log_stack = false;
    101 static bool log_irq   = false;
    102 static bool log_sync  = false;
    103 static bool log_workq = false;
    104 
    105 /* Paths for debugfs controls*/
    106 static const char* dfs_trace_output_path =
    107     "/d/tracing/trace";
    108 static const char* dfs_irq_path =
    109     "/d/tracing/events/irq/enable";
    110 static const char* dfs_sync_path =
    111     "/d/tracing/events/sync/enable";
    112 static const char* dfs_workq_path =
    113     "/d/tracing/events/workqueue/enable";
    114 static const char* dfs_stack_path =
    115     "/d/tracing/options/stacktrace";
    116 static const char* dfs_sched_switch_path =
    117     "/d/tracing/events/sched/sched_switch/enable";
    118 static const char* dfs_sched_wakeup_path =
    119     "/d/tracing/events/sched/sched_wakeup/enable";
    120 static const char* dfs_control_path =
    121     "/d/tracing/tracing_on";
    122 static const char* dfs_buffer_size_path =
    123     "/d/tracing/buffer_size_kb";
    124 static const char* dfs_tags_property = "debug.atrace.tags.enableflags";
    125 static const char* dfs_apps_property = "debug.atrace.app_cmdlines";
    126 
    127 /*
    128  * Read accumulated cpu data from /proc/stat
    129  */
    130 static void get_cpu_stat(cpu_stat_t *cpu) {
    131     FILE *fp = NULL;
    132     const char *params = "cpu  %lu %lu %lu %lu %lu %lu %lu %*d %*d %*d\n";
    133 
    134     if ((fp = fopen("/proc/stat", "r")) == NULL) {
    135         err = true;
    136         sprintf(err_msg, "can't read from /proc/stat with errno %d", errno);
    137     } else {
    138         if (fscanf(fp, params, &cpu->utime, &cpu->ntime,
    139                 &cpu->stime, &cpu->itime, &cpu->iowtime, &cpu->irqtime,
    140                 &cpu->sirqtime) != cpu_stat_entries) {
    141             /*
    142              * If failed in getting status, new_cpu won't be updated and
    143              * is_heavy_loaded() will return false.
    144              */
    145             ALOGE("Error in getting cpu status. Skipping this check.");
    146             return;
    147         }
    148 
    149         cpu->total = cpu->utime + cpu->ntime + cpu->stime + cpu->itime
    150             + cpu->iowtime + cpu->irqtime + cpu->sirqtime;
    151 
    152         fclose(fp);
    153     }
    154 }
    155 
    156 /*
    157  * Calculate cpu usage in the past interval.
    158  * If tracing is on, increase the idle threshold by 1.00% so that we do not
    159  * turn on and off tracing frequently whe the cpu load is right close to
    160  * threshold.
    161  */
    162 static bool is_heavy_load(void) {
    163     unsigned long diff_idle, diff_total;
    164     int threshold = idle_threshold + (tracing?100:0);
    165     get_cpu_stat(&new_cpu);
    166     diff_idle = new_cpu.itime - old_cpu.itime;
    167     diff_total = new_cpu.total - old_cpu.total;
    168     old_cpu = new_cpu;
    169     return (diff_idle * 10000 < diff_total * threshold);
    170 }
    171 
    172 /*
    173  * Force the userland processes to refresh their property for logging.
    174  */
    175 static void dfs_poke_binder(void) {
    176     sp<IServiceManager> sm = defaultServiceManager();
    177     Vector<String16> services = sm->listServices();
    178     for (size_t i = 0; i < services.size(); i++) {
    179         sp<IBinder> obj = sm->checkService(services[i]);
    180         if (obj != NULL) {
    181             Parcel data;
    182             obj->transact(IBinder::SYSPROPS_TRANSACTION, data, NULL, 0);
    183         }
    184     }
    185 }
    186 
    187 /*
    188  * Enable/disable a debugfs property by writing 0/1 to its path.
    189  */
    190 static int dfs_enable(bool enable, const char* path) {
    191     int fd = open(path, O_WRONLY);
    192     if (fd == -1) {
    193         err = true;
    194         sprintf(err_msg, "Can't open %s. Error: %d", path, errno);
    195         return -1;
    196     }
    197     const char* control = (enable?"1":"0");
    198     ssize_t len = strlen(control);
    199     int max_try = 10; // Fail if write was interrupted for 10 times
    200     while (write(fd, control, len) != len) {
    201         if (errno == EINTR && max_try-- > 0) {
    202             usleep(100);
    203             continue;
    204         }
    205 
    206         err = true;
    207         sprintf(err_msg, "Error %d in writing to %s.", errno, path);
    208     }
    209     close(fd);
    210     return (err?-1:0);
    211 }
    212 
    213 /*
    214  * Set the userland tracing properties.
    215  */
    216 static void dfs_set_property(uint64_t mtag, const char* mapp, bool enable) {
    217     char buf[64];
    218     snprintf(buf, 64, "%#" PRIx64, mtag);
    219     if (property_set(dfs_tags_property, buf) < 0) {
    220         err = true;
    221         sprintf(err_msg, "Failed to set debug tags system properties.");
    222     }
    223 
    224     if (strlen(mapp) > 0
    225             && property_set(dfs_apps_property, mapp) < 0) {
    226         err = true;
    227         sprintf(err_msg, "Failed to set debug applications.");
    228     }
    229 
    230     if (log_sched) {
    231         dfs_enable(enable, dfs_sched_switch_path);
    232         dfs_enable(enable, dfs_sched_wakeup_path);
    233     }
    234     if (log_stack) {
    235         dfs_enable(enable, dfs_stack_path);
    236     }
    237     if (log_irq) {
    238         dfs_enable(enable, dfs_irq_path);
    239     }
    240     if (log_sync) {
    241         dfs_enable(enable, dfs_sync_path);
    242     }
    243     if (log_workq) {
    244         dfs_enable(enable, dfs_workq_path);
    245     }
    246 }
    247 
    248 /*
    249  * Start logging when cpu usage is high. Meanwhile, moniter the cpu usage and
    250  * stop logging when it drops down.
    251  */
    252 static void start_tracing(void) {
    253     ALOGD("High cpu usage, start logging.");
    254 
    255     if (dfs_enable(true, dfs_control_path) != 0) {
    256         ALOGE("Failed to start tracing.");
    257         return;
    258     }
    259     tracing = true;
    260 
    261     /* Stop logging when cpu usage drops or the daemon is suspended.*/
    262     do {
    263         usleep(tracing_check_period);
    264     } while (!suspend && is_heavy_load());
    265 
    266     if (dfs_enable(false, dfs_control_path) != 0) {
    267         ALOGE("Failed to stop tracing.");
    268     }
    269 
    270     ALOGD("Usage back to low, stop logging.");
    271     tracing = false;
    272 }
    273 
    274 /*
    275  * Set the tracing log buffer size.
    276  * Note the actual buffer size will be buf_size_kb * number of cores.
    277  * E.g. for dory, the total buffer size is buf_size_kb * 4.
    278  */
    279 static int set_tracing_buffer_size(void) {
    280     int fd = open(dfs_buffer_size_path, O_WRONLY);
    281     if (fd == -1) {
    282         err = true;
    283         sprintf(err_msg, "Can't open atrace buffer size file under /d/tracing.");
    284         return -1;
    285     }
    286     ssize_t len = strlen(buf_size_kb);
    287     if (write(fd, buf_size_kb, len) != len) {
    288         err = true;
    289         sprintf(err_msg, "Error in writing to atrace buffer size file.");
    290     }
    291     close(fd);
    292     return (err?-1:0);
    293 
    294 }
    295 
    296 /*
    297  * Main loop to moniter the cpu usage and decided whether to start logging.
    298  */
    299 static void start(void) {
    300     if ((set_tracing_buffer_size()) != 0)
    301         return;
    302 
    303     dfs_set_property(tag, apps, true);
    304     dfs_poke_binder();
    305 
    306     get_cpu_stat(&old_cpu);
    307     sleep(check_period);
    308 
    309     while (!quit && !err) {
    310         if (!suspend && is_heavy_load()) {
    311             /*
    312              * Increase process priority to make sure we can stop logging when
    313              * necessary and do not overwrite the buffer
    314              */
    315             setpriority(PRIO_PROCESS, 0, -20);
    316             start_tracing();
    317             setpriority(PRIO_PROCESS, 0, 0);
    318         }
    319         sleep(check_period);
    320     }
    321     return;
    322 }
    323 
    324 /*
    325  * Dump the log in a compressed format for systrace to visualize.
    326  */
    327 static void dump_trace()
    328 {
    329     int remain_attempts = 5;
    330     suspend = true;
    331     while (tracing) {
    332         ALOGI("Waiting logging to stop.");
    333         usleep(tracing_check_period);
    334         remain_attempts--;
    335         if (remain_attempts == 0) {
    336             ALOGE("Can't stop logging after 5 attempts. Dump aborted.");
    337             return;
    338         }
    339     }
    340 
    341     /*
    342      * Create a dump file "dump_of_anrdaemon.<current_time>" under /data/misc/anrd
    343      */
    344     time_t now = time(0);
    345     struct tm  tstruct;
    346     char time_buf[time_buf_size];
    347     char path_buf[path_buf_size];
    348     const char* header = " done\nTRACE:\n";
    349     ssize_t header_len = strlen(header);
    350     tstruct = *localtime(&now);
    351     strftime(time_buf, time_buf_size, "%Y-%m-%d.%X", &tstruct);
    352     snprintf(path_buf, path_buf_size, "/data/misc/anrd/dump_of_anrdaemon.%s", time_buf);
    353     int output_fd = creat(path_buf, S_IRWXU);
    354     if (output_fd == -1) {
    355         ALOGE("Failed to create %s. Dump aborted.", path_buf);
    356         return;
    357     }
    358 
    359     if (write(output_fd, header, strlen(header)) != header_len) {
    360         ALOGE("Failed to write the header.");
    361         close(output_fd);
    362         return;
    363     }
    364 
    365     int trace_fd = open(dfs_trace_output_path, O_RDWR);
    366     if (trace_fd == -1) {
    367         ALOGE("Failed to open %s. Dump aborted.", dfs_trace_output_path);
    368         close(output_fd);
    369         return;
    370     }
    371 
    372     z_stream zs;
    373     uint8_t *in, *out;
    374     int result, flush;
    375 
    376     memset(&zs, 0, sizeof(zs));
    377     result = deflateInit(&zs, Z_DEFAULT_COMPRESSION);
    378     if (result != Z_OK) {
    379         ALOGE("error initializing zlib: %d\n", result);
    380         close(trace_fd);
    381         close(output_fd);
    382         return;
    383     }
    384 
    385     const size_t bufSize = 64*1024;
    386     in = (uint8_t*)malloc(bufSize);
    387     out = (uint8_t*)malloc(bufSize);
    388     flush = Z_NO_FLUSH;
    389 
    390     zs.next_out = out;
    391     zs.avail_out = bufSize;
    392 
    393     do {
    394         if (zs.avail_in == 0) {
    395             result = read(trace_fd, in, bufSize);
    396             if (result < 0) {
    397                 ALOGE("error reading trace: %s", strerror(errno));
    398                 result = Z_STREAM_END;
    399                 break;
    400             } else if (result == 0) {
    401                 flush = Z_FINISH;
    402             } else {
    403                 zs.next_in = in;
    404                 zs.avail_in = result;
    405             }
    406         }
    407 
    408         if (zs.avail_out == 0) {
    409             result = write(output_fd, out, bufSize);
    410             if ((size_t)result < bufSize) {
    411                 ALOGE("error writing deflated trace: %s", strerror(errno));
    412                 result = Z_STREAM_END;
    413                 zs.avail_out = bufSize;
    414                 break;
    415             }
    416             zs.next_out = out;
    417             zs.avail_out = bufSize;
    418         }
    419 
    420     } while ((result = deflate(&zs, flush)) == Z_OK);
    421 
    422     if (result != Z_STREAM_END) {
    423         ALOGE("error deflating trace: %s\n", zs.msg);
    424     }
    425 
    426     if (zs.avail_out < bufSize) {
    427         size_t bytes = bufSize - zs.avail_out;
    428         result = write(output_fd, out, bytes);
    429         if ((size_t)result < bytes) {
    430             ALOGE("error writing deflated trace: %s", strerror(errno));
    431         }
    432     }
    433 
    434     result = deflateEnd(&zs);
    435     if (result != Z_OK) {
    436         ALOGE("error cleaning up zlib: %d\n", result);
    437     }
    438 
    439     free(in);
    440     free(out);
    441 
    442     close(trace_fd);
    443     close(output_fd);
    444 
    445     suspend = false;
    446     ALOGI("Finished dump. Output file stored at: %s", path_buf);
    447 }
    448 
    449 static void handle_signal(int signo)
    450 {
    451     switch (signo) {
    452         case SIGQUIT:
    453             suspend = true;
    454             quit = true;
    455             break;
    456         case SIGSTOP:
    457             suspend = true;
    458             break;
    459         case SIGCONT:
    460             suspend = false;
    461             break;
    462         case SIGUSR1:
    463             dump_trace();
    464     }
    465 }
    466 
    467 /*
    468  * Set the signal handler:
    469  * SIGQUIT: Reset debugfs and tracing property and terminate the daemon.
    470  * SIGSTOP: Stop logging and suspend the daemon.
    471  * SIGCONT: Resume the daemon as normal.
    472  * SIGUSR1: Dump the logging to a compressed format for systrace to visualize.
    473  */
    474 static void register_sighandler(void)
    475 {
    476     struct sigaction sa;
    477     sigset_t block_mask;
    478 
    479     sigemptyset(&block_mask);
    480     sigaddset (&block_mask, SIGQUIT);
    481     sigaddset (&block_mask, SIGSTOP);
    482     sigaddset (&block_mask, SIGCONT);
    483     sigaddset (&block_mask, SIGUSR1);
    484 
    485     sa.sa_flags = 0;
    486     sa.sa_mask = block_mask;
    487     sa.sa_handler = handle_signal;
    488     sigaction(SIGQUIT, &sa, NULL);
    489     sigaction(SIGSTOP, &sa, NULL);
    490     sigaction(SIGCONT, &sa, NULL);
    491     sigaction(SIGUSR1, &sa, NULL);
    492 }
    493 
    494 static void show_help(void) {
    495 
    496     fprintf(stderr, "usage: ANRdaemon [options] [categoris...]\n");
    497     fprintf(stdout, "Options includes:\n"
    498                     "   -a appname  enable app-level tracing for a comma "
    499                        "separated list of cmdlines\n"
    500                     "   -t N        cpu threshold for logging to start "
    501                         "(uint = 0.01%%, min = 5000, max = 9999, default = 9990)\n"
    502                     "   -s N        use a trace buffer size of N KB "
    503                         "default to 2048KB\n"
    504                     "   -h          show helps\n");
    505     fprintf(stdout, "Categoris includes:\n"
    506                     "   am         - activity manager\n"
    507                     "   sm         - sync manager\n"
    508                     "   input      - input\n"
    509                     "   dalvik     - dalvik VM\n"
    510                     "   audio      - Audio\n"
    511                     "   gfx        - Graphics\n"
    512                     "   rs         - RenderScript\n"
    513                     "   hal        - Hardware Modules\n"
    514                     "   irq        - kernel irq events\n"
    515                     "   sched      - kernel scheduler activity\n"
    516                     "   stack      - kernel stack\n"
    517                     "   sync       - kernel sync activity\n"
    518                     "   workq      - kernel work queues\n");
    519     fprintf(stdout, "Control includes:\n"
    520                     "   SIGQUIT: terminate the process\n"
    521                     "   SIGSTOP: suspend all function of the daemon\n"
    522                     "   SIGCONT: resume the normal function\n"
    523                     "   SIGUSR1: dump the current logging in a compressed form\n");
    524     exit(0);
    525 }
    526 
    527 static int get_options(int argc, char *argv[]) {
    528     int opt = 0;
    529     int threshold;
    530     while ((opt = getopt(argc, argv, "a:s:t:h")) >= 0) {
    531         switch(opt) {
    532             case 'a':
    533                 apps = optarg;
    534                 break;
    535             case 's':
    536                 if (atoi(optarg) > max_buffer_size)
    537                     buf_size_kb = max_buffer_size_str;
    538                 else if (atoi(optarg) < min_buffer_size)
    539                     buf_size_kb = min_buffer_size_str;
    540                 else
    541                     buf_size_kb = optarg;
    542                 break;
    543             case 't':
    544                 threshold = atoi(optarg);
    545                 if (threshold > 9999 || threshold < 5000) {
    546                     fprintf(stderr, "logging threshold should be 5000-9999\n");
    547                     return 1;
    548                 }
    549                 idle_threshold = 10000 - threshold;
    550                 break;
    551             case 'h':
    552                 show_help();
    553                 break;
    554             default:
    555                 fprintf(stderr, "Error in getting options.\n"
    556                         "run \"%s -h\" for usage.\n", argv[0]);
    557                 return 1;
    558         }
    559     }
    560 
    561     for (int i = optind; i < argc; i++) {
    562         if (strcmp(argv[i], "am") == 0) {
    563             tag |= ATRACE_TAG_ACTIVITY_MANAGER;
    564         } else if (strcmp(argv[i], "input") == 0) {
    565             tag |= ATRACE_TAG_INPUT;
    566         } else if (strcmp(argv[i], "sm") == 0) {
    567             tag |= ATRACE_TAG_SYNC_MANAGER;
    568         } else if (strcmp(argv[i], "dalvik") == 0) {
    569             tag |= ATRACE_TAG_DALVIK;
    570         } else if (strcmp(argv[i], "gfx") == 0) {
    571             tag |= ATRACE_TAG_GRAPHICS;
    572         } else if (strcmp(argv[i], "audio") == 0) {
    573             tag |= ATRACE_TAG_AUDIO;
    574         } else if (strcmp(argv[i], "hal") == 0) {
    575             tag |= ATRACE_TAG_HAL;
    576         } else if (strcmp(argv[i], "rs") == 0) {
    577             tag |= ATRACE_TAG_RS;
    578         } else if (strcmp(argv[i], "sched") == 0) {
    579             log_sched = true;
    580         } else if (strcmp(argv[i], "stack") == 0) {
    581             log_stack = true;
    582         } else if (strcmp(argv[i], "workq") == 0) {
    583             log_workq = true;
    584         } else if (strcmp(argv[i], "irq") == 0) {
    585             log_irq = true;
    586         } else if (strcmp(argv[i], "sync") == 0) {
    587             log_sync = true;
    588         } else {
    589             fprintf(stderr, "invalid category: %s\n"
    590                     "run \"%s -h\" for usage.\n", argv[i], argv[0]);
    591             return 1;
    592         }
    593     }
    594 
    595     /* If nothing is enabled, don't run */
    596     if (!tag && !log_sched && !log_stack && !log_workq && !log_irq && !log_sync) {
    597         ALOGE("Specify at least one category to trace.");
    598         return 1;
    599     }
    600 
    601     return 0;
    602 }
    603 
    604 int main(int argc, char *argv[])
    605 {
    606     if(get_options(argc, argv) != 0)
    607         return 1;
    608 
    609     if (daemon(0, 0) != 0)
    610         return 1;
    611 
    612     register_sighandler();
    613 
    614     /* Clear any the trace log file by overwrite it with a new file */
    615     int fd = creat(dfs_trace_output_path, 0);
    616     if (fd == -1) {
    617         ALOGE("Faield to open and cleaup previous log");
    618         return 1;
    619     }
    620     close(fd);
    621 
    622     ALOGI("ANRdaemon starting");
    623     start();
    624 
    625     if (err)
    626         ALOGE("ANRdaemon stopped due to Error: %s", err_msg);
    627 
    628     ALOGI("ANRdaemon terminated.");
    629 
    630     return (err?1:0);
    631 }
    632