Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 535ddbe2 authored by Jin Qian's avatar Jin Qian
Browse files

storaged: replace kmsg with LOG macros

Also convert android_log_event to C++ style calls.

Change-Id: I7d62c81789fe7925fe9cae3e2a798b6af8c8be9d
parent 5c8bdf42
Loading
Loading
Loading
Loading
+0 −18
Original line number Diff line number Diff line
@@ -17,13 +17,10 @@
#ifndef _STORAGED_H_
#define _STORAGED_H_

#define DEBUG

#include <queue>
#include <semaphore.h>
#include <stdint.h>
#include <string>
#include <syslog.h>
#include <unordered_map>
#include <vector>

@@ -39,21 +36,6 @@ friend class test_case_name##_##test_name##_Test
#define debuginfo(...)
#endif

#define KMSG_PRIORITY(PRI)                            \
    '<',                                              \
    '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) / 10, \
    '0' + LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(PRI)) % 10, \
    '>'

static char kmsg_error_prefix[] = { KMSG_PRIORITY(LOG_ERR),
    's', 't', 'o', 'r', 'a', 'g', 'e', 'd', ':', '\0' };

static char kmsg_info_prefix[] = { KMSG_PRIORITY(LOG_INFO),
    's', 't', 'o', 'r', 'a', 'g', 'e', 'd', ':', '\0' };

static char kmsg_warning_prefix[] = { KMSG_PRIORITY(LOG_WARNING),
    's', 't', 'o', 'r', 'a', 'g', 'e', 'd', ':', '\0' };

// number of attributes diskstats has
#define DISK_STATS_SIZE ( 11 )
// maximum size limit of a stats file
+5 −20
Original line number Diff line number Diff line
@@ -29,12 +29,12 @@
#include <vector>

#include <android-base/macros.h>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
#include <cutils/android_get_control_file.h>
#include <cutils/klog.h>
#include <cutils/sched_policy.h>
#include <private/android_filesystem_config.h>

@@ -90,15 +90,10 @@ static int drop_privs() {
}

// Function of storaged's main thread
extern int fd_dmesg;
void* storaged_main(void* s) {
    storaged_t* storaged = (storaged_t*)s;

    if (fd_dmesg >= 0) {
        static const char start_message[] = {KMSG_PRIORITY(LOG_INFO),
            's', 't', 'o', 'r', 'a', 'g', 'e', 'd', ':', ' ', 'S', 't', 'a', 'r', 't', '\n'};
        write(fd_dmesg, start_message, sizeof(start_message));
    }
    LOG_TO(SYSTEM, INFO) << "storaged: Start";

    for (;;) {
        storaged->event();
@@ -121,7 +116,6 @@ static void help_message(void) {
#define DAY_TO_SEC ( 3600 * 24 )

int main(int argc, char** argv) {
    klog_set_level(KLOG_LEVEL);
    int flag_main_service = 0;
    int flag_dump_task = 0;
    int flag_config = 0;
@@ -221,11 +215,6 @@ int main(int argc, char** argv) {
    }

    if (flag_main_service) { // start main thread
        static const char dev_kmsg[] = "/dev/kmsg";
        fd_dmesg = android_get_control_file(dev_kmsg);
        if (fd_dmesg < 0)
            fd_dmesg = TEMP_FAILURE_RETRY(open(dev_kmsg, O_WRONLY));

        static const char mmc0_ext_csd[] = "/d/mmc0/mmc0:0001/ext_csd";
        fd_emmc = android_get_control_file(mmc0_ext_csd);
        if (fd_emmc < 0)
@@ -245,12 +234,9 @@ int main(int argc, char** argv) {

        // Start the main thread of storaged
        pthread_t storaged_main_thread;
        if (pthread_create(&storaged_main_thread, NULL, storaged_main, &storaged)) {
            if (fd_dmesg >= 0) {
                std::string error_message = android::base::StringPrintf(
                    "%s Failed to create main thread\n", kmsg_error_prefix);
                write(fd_dmesg, error_message.c_str(), error_message.length());
            }
        errno = pthread_create(&storaged_main_thread, NULL, storaged_main, &storaged);
        if (errno != 0) {
            PLOG_TO(SYSTEM, ERROR) << "Failed to create main thread";
            return -1;
        }

@@ -259,7 +245,6 @@ int main(int argc, char** argv) {
        IPCThreadState::self()->joinThreadPool();
        pthread_join(storaged_main_thread, NULL);

        close(fd_dmesg);
        close(fd_emmc);

        return 0;
+0 −1
Original line number Diff line number Diff line
service storaged /system/bin/storaged
    class main
    file /d/mmc0/mmc0:0001/ext_csd r
    file /dev/kmsg w
    group root readproc
+40 −112
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@
#include <time.h>
#include <unistd.h>

#include <iomanip>
#include <sstream>
#include <string>
#include <unordered_map>
@@ -35,8 +36,6 @@
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include <android-base/strings.h>
#include <cutils/klog.h>
#include <log/log.h>
#include <log/log_event_list.h>

#include <storaged.h>
@@ -47,8 +46,6 @@
#define MSEC_TO_USEC ( 1000 )
#define USEC_TO_NSEC ( 1000 )

int fd_dmesg = -1;

bool parse_disk_stats(const char* disk_stats_path, struct disk_stats* stats) {
    // Get time
    struct timespec ts;
@@ -56,22 +53,13 @@ bool parse_disk_stats(const char* disk_stats_path, struct disk_stats* stats) {
    // when system is running.
    int ret = clock_gettime(CLOCK_MONOTONIC, &ts);
    if (ret < 0) {
        if (fd_dmesg >= 0) {
            std::string error_message = android::base::StringPrintf(
                "%s clock_gettime() failed with errno %d\n",
                kmsg_error_prefix, ret);
            write(fd_dmesg, error_message.c_str(), error_message.length());
        }
        PLOG_TO(SYSTEM, ERROR) << "clock_gettime() failed";
        return false;
    }

    std::string buffer;
    if (!android::base::ReadFileToString(disk_stats_path, &buffer)) {
        if (fd_dmesg >= 0) {
            std::string error_message = android::base::StringPrintf(
                "%s %s: ReadFileToString failed.\n", kmsg_error_prefix, disk_stats_path);
            write(fd_dmesg, error_message.c_str(), error_message.length());
        }
        PLOG_TO(SYSTEM, ERROR) << disk_stats_path << ": ReadFileToString failed.";
        return false;
    }

@@ -149,13 +137,10 @@ struct disk_stats get_inc_disk_stats(struct disk_stats* prev, struct disk_stats*

// Add src to dst
void add_disk_stats(struct disk_stats* src, struct disk_stats* dst) {
    if (dst->end_time != 0 && dst->end_time != src->start_time && fd_dmesg >= 0) {
        std::string warning_message = android::base::StringPrintf(
            "%s Two dis-continuous periods of diskstats are added. "
            "dst end with %jd, src start with %jd\n",
            kmsg_warning_prefix, dst->end_time, src->start_time);

        write(fd_dmesg, warning_message.c_str(), warning_message.length());
    if (dst->end_time != 0 && dst->end_time != src->start_time) {
        LOG_TO(SYSTEM, WARNING) << "Two dis-continuous periods of diskstats"
            << " are added. dst end with " << dst->end_time
            << ", src start with " << src->start_time;
    }

    for (uint i = 0; i < DISK_STATS_SIZE; ++i) {
@@ -193,21 +178,13 @@ bool parse_emmc_ecsd(int ext_csd_fd, struct emmc_info* info) {
    CHECK(lseek(ext_csd_fd, 0, SEEK_SET) == 0);
    std::string buffer;
    if (!android::base::ReadFdToString(ext_csd_fd, &buffer)) {
        if (fd_dmesg >= 0) {
            std::string error_message = android::base::StringPrintf(
                "%s ReadFdToString failed.\n", kmsg_error_prefix);
            write(fd_dmesg, error_message.c_str(), error_message.length());
        }
        PLOG_TO(SYSTEM, ERROR) << "ReadFdToString failed.";
        return false;
    }

    if (buffer.length() < EXT_CSD_FILE_MIN_SIZE) {
        if (fd_dmesg >= 0) {
            std::string error_message = android::base::StringPrintf(
                "%s EMMC ext csd file has truncated content. File length: %d\n",
                kmsg_error_prefix, (int)buffer.length());
            write(fd_dmesg, error_message.c_str(), error_message.length());
        }
        LOG_TO(SYSTEM, ERROR) << "EMMC ext csd file has truncated content. "
            << "File length: " << buffer.length();
        return false;
    }

@@ -219,11 +196,7 @@ bool parse_emmc_ecsd(int ext_csd_fd, struct emmc_info* info) {
    ss << sub;
    ss >> std::hex >> ext_csd_rev;
    if (ext_csd_rev < 0) {
        if (fd_dmesg >= 0) {
            std::string error_message = android::base::StringPrintf(
                "%s Failure on parsing EXT_CSD_REV.\n", kmsg_error_prefix);
            write(fd_dmesg, error_message.c_str(), error_message.length());
        }
        LOG_TO(SYSTEM, ERROR) << "Failure on parsing EXT_CSD_REV.";
        return false;
    }
    ss.clear();
@@ -248,11 +221,7 @@ bool parse_emmc_ecsd(int ext_csd_fd, struct emmc_info* info) {
    ss << sub;
    ss >> std::hex >> info->eol;
    if (info->eol < 0) {
        if (fd_dmesg >= 0) {
            std::string error_message = android::base::StringPrintf(
                "%s Failure on parsing EXT_PRE_EOL_INFO.\n", kmsg_error_prefix);
            write(fd_dmesg, error_message.c_str(), error_message.length());
        }
        LOG_TO(SYSTEM, ERROR) << "Failure on parsing EXT_PRE_EOL_INFO.";
        return false;
    }
    ss.clear();
@@ -263,11 +232,7 @@ bool parse_emmc_ecsd(int ext_csd_fd, struct emmc_info* info) {
    ss << sub;
    ss >> std::hex >> info->lifetime_a;
    if (info->lifetime_a < 0) {
        if (fd_dmesg >= 0) {
            std::string error_message = android::base::StringPrintf(
                "%s Failure on parsing EXT_DEVICE_LIFE_TIME_EST_TYP_A.\n", kmsg_error_prefix);
            write(fd_dmesg, error_message.c_str(), error_message.length());
        }
        LOG_TO(SYSTEM, ERROR) << "Failure on parsing EXT_DEVICE_LIFE_TIME_EST_TYP_A.";
        return false;
    }
    ss.clear();
@@ -277,11 +242,7 @@ bool parse_emmc_ecsd(int ext_csd_fd, struct emmc_info* info) {
    ss << sub;
    ss >> std::hex >> info->lifetime_b;
    if (info->lifetime_b < 0) {
        if (fd_dmesg >= 0) {
            std::string error_message = android::base::StringPrintf(
                "%s Failure on parsing EXT_DEVICE_LIFE_TIME_EST_TYP_B.\n", kmsg_error_prefix);
            write(fd_dmesg, error_message.c_str(), error_message.length());
        }
        LOG_TO(SYSTEM, ERROR) << "Failure on parsing EXT_DEVICE_LIFE_TIME_EST_TYP_B.";
        return false;
    }
    ss.clear();
@@ -481,19 +442,14 @@ void log_kernel_disk_stats(struct disk_stats* stats, const char* type) {
    memset(&zero_cmp, 0, sizeof(zero_cmp));
    if (memcmp(&zero_cmp, stats, sizeof(struct disk_stats)) == 0) return;

    if (fd_dmesg >= 0) {
        std::string info_message = android::base::StringPrintf(
            "%s diskstats %s: %ju %ju %ju %ju %ju %ju %ju %ju %ju %ju %.1f %ju %ju\n",
             kmsg_info_prefix, type, stats->start_time, stats->end_time,
             stats->read_ios, stats->read_merges,
             stats->read_sectors, stats->read_ticks,
             stats->write_ios, stats->write_merges,
             stats->write_sectors, stats->write_ticks,
             stats->io_avg, stats->io_ticks,
             stats->io_in_queue);

        write(fd_dmesg, info_message.c_str(), info_message.length());
    }
    LOG_TO(SYSTEM, INFO) << "diskstats " << type << ": "
              << stats->start_time << " " << stats->end_time << " "
              << stats->read_ios << " " << stats->read_merges << " "
              << stats->read_sectors << " " << stats->read_ticks << " "
              << stats->write_ios << " " << stats->write_merges << " "
              << stats->write_sectors << " " << stats->write_ticks << " "
              << std::setprecision(1) << std::fixed << stats->io_avg << " "
              << stats->io_ticks << " " << stats->io_in_queue;
}

void log_kernel_disk_perf(struct disk_perf* perf, const char* type) {
@@ -503,16 +459,10 @@ void log_kernel_disk_perf(struct disk_perf* perf, const char* type) {
    memset(&zero_cmp, 0, sizeof(zero_cmp));
    if (memcmp(&zero_cmp, perf, sizeof(struct disk_perf)) == 0) return;

    if (fd_dmesg >= 0) {
        std::string info_message = android::base::StringPrintf(
            "%s perf(ios) %s rd:%luKB/s(%lu/s) wr:%luKB/s(%lu/s) q:%lu\n",
            kmsg_info_prefix, type,
            (unsigned long)perf->read_perf, (unsigned long)perf->read_ios,
            (unsigned long)perf->write_perf, (unsigned long)perf->write_ios,
            (unsigned long)perf->queue);

        write(fd_dmesg, info_message.c_str(), info_message.length());
    }
    LOG_TO(SYSTEM, INFO) << "perf(ios) " << type
              << " rd:" << perf->read_perf << "KB/s(" << perf->read_ios << "/s)"
              << " wr:" << perf->write_perf << "KB/s(" << perf->write_ios << "/s)"
              << " q:" << perf->queue;
}

void log_kernel_emmc_info(struct emmc_info* info) {
@@ -522,13 +472,9 @@ void log_kernel_emmc_info(struct emmc_info* info) {
    memset(&zero_cmp, 0, sizeof(zero_cmp));
    if (memcmp(&zero_cmp, info, sizeof(struct emmc_info)) == 0) return;

    if (fd_dmesg >= 0) {
        std::string info_message = android::base::StringPrintf(
            "%s MMC %s eol:%d, lifetime typA:%d, typB:%d\n",
            kmsg_info_prefix, info->mmc_ver, info->eol, info->lifetime_a, info->lifetime_b);

        write(fd_dmesg, info_message.c_str(), info_message.length());
    }
    LOG_TO(SYSTEM, INFO) << "MMC " << info->mmc_ver << " eol:" << info->eol << ", "
              << "lifetime typA:" << info->lifetime_a
              << ", typB:" << info->lifetime_b;
}

void log_event_disk_stats(struct disk_stats* stats, const char* type) {
@@ -539,26 +485,14 @@ void log_event_disk_stats(struct disk_stats* stats, const char* type) {
    // skip event logging diskstats when it is zero increment (all first 11 entries are zero)
    if (memcmp(&zero_cmp, stats, sizeof(uint64_t) * DISK_STATS_SIZE) == 0) return;

    // Construct eventlog list
    android_log_context ctx = create_android_logger(EVENTLOGTAG_DISKSTATS);

    android_log_write_string8(ctx, type);
    android_log_write_int64(ctx, stats->start_time);
    android_log_write_int64(ctx, stats->end_time);
    android_log_write_int64(ctx, stats->read_ios);
    android_log_write_int64(ctx, stats->read_merges);
    android_log_write_int64(ctx, stats->read_sectors);
    android_log_write_int64(ctx, stats->read_ticks);
    android_log_write_int64(ctx, stats->write_ios);
    android_log_write_int64(ctx, stats->write_merges);
    android_log_write_int64(ctx, stats->write_sectors);
    android_log_write_int64(ctx, stats->write_ticks);
    android_log_write_int64(ctx, (uint64_t)stats->io_avg);
    android_log_write_int64(ctx, stats->io_ticks);
    android_log_write_int64(ctx, stats->io_in_queue);

    android_log_write_list(ctx, LOG_ID_EVENTS);
    android_log_destroy(&ctx);
    android_log_event_list(EVENTLOGTAG_DISKSTATS)
        << type << stats->start_time << stats->end_time
        << stats->read_ios << stats->read_merges
        << stats->read_sectors << stats->read_ticks
        << stats->write_ios << stats->write_merges
        << stats->write_sectors << stats->write_ticks
        << (uint64_t)stats->io_avg << stats->io_ticks << stats->io_in_queue
        << LOG_ID_EVENTS;
}

void log_event_emmc_info(struct emmc_info* info) {
@@ -568,13 +502,7 @@ void log_event_emmc_info(struct emmc_info* info) {
    memset(&zero_cmp, 0, sizeof(zero_cmp));
    if (memcmp(&zero_cmp, info, sizeof(struct emmc_info)) == 0) return;

    android_log_context ctx = create_android_logger(EVENTLOGTAG_EMMCINFO);

    android_log_write_string8(ctx, info->mmc_ver);
    android_log_write_int32(ctx, info->eol);
    android_log_write_int32(ctx, info->lifetime_a);
    android_log_write_int32(ctx, info->lifetime_b);

    android_log_write_list(ctx, LOG_ID_EVENTS);
    android_log_destroy(&ctx);
    android_log_event_list(EVENTLOGTAG_EMMCINFO)
        << info->mmc_ver << info->eol << info->lifetime_a << info->lifetime_b
        << LOG_ID_EVENTS;
}