~xnox/androidos/platform-system-core

« back to all changes in this revision

Viewing changes to liblog/tests/liblog_benchmark.cpp

  • Committer: Gerrit Code Review
  • Author(s): Mark Salyzyn
  • Date: 2017-02-03 15:26:14 UTC
  • mfrom: (6634.1.3)
  • Revision ID: git-v1:98a6db5c9bb6497c5f9a71570a39659c366342b9
Merge changes I69e6489d,Ic17d52a7

* changes:
  liblog: add android_lookupEventTagNum
  liblog: add /dev/event-log-tags for Tag Map

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 */
16
16
 
17
17
#include <fcntl.h>
 
18
#include <inttypes.h>
 
19
#include <poll.h>
18
20
#include <sys/endian.h>
19
21
#include <sys/socket.h>
20
22
#include <sys/types.h>
22
24
 
23
25
#include <unordered_set>
24
26
 
 
27
#include <android-base/file.h>
25
28
#include <cutils/sockets.h>
26
29
#include <log/event_tag_map.h>
27
30
#include <private/android_logger.h>
695
698
 
696
699
// Keep maps around for multiple iterations
697
700
static std::unordered_set<uint32_t> set;
698
 
static const EventTagMap* map;
 
701
static EventTagMap* map;
699
702
 
700
703
static bool prechargeEventMap() {
701
704
    if (map) return true;
785
788
    StopBenchmarkTiming();
786
789
}
787
790
BENCHMARK(BM_lookupEventFormat);
 
791
 
 
792
/*
 
793
 *      Measure the time it takes for android_lookupEventTagNum plus above
 
794
 */
 
795
static void BM_lookupEventTagNum(int iters) {
 
796
 
 
797
    prechargeEventMap();
 
798
 
 
799
    std::unordered_set<uint32_t>::const_iterator it = set.begin();
 
800
 
 
801
    for (int i = 0; i < iters; ++i) {
 
802
        size_t len;
 
803
        const char* name = android_lookupEventTag_len(map, &len, (*it));
 
804
        std::string Name(name, len);
 
805
        const char* format = android_lookupEventFormat_len(map, &len, (*it));
 
806
        std::string Format(format, len);
 
807
        StartBenchmarkTiming();
 
808
        android_lookupEventTagNum(map, Name.c_str(), Format.c_str(),
 
809
                                  ANDROID_LOG_UNKNOWN);
 
810
        StopBenchmarkTiming();
 
811
        ++it;
 
812
        if (it == set.end()) it = set.begin();
 
813
    }
 
814
 
 
815
}
 
816
BENCHMARK(BM_lookupEventTagNum);
 
817
 
 
818
// Must be functionally identical to liblog internal __send_log_msg.
 
819
static void send_to_control(char *buf, size_t len)
 
820
{
 
821
    int sock = socket_local_client("logd",
 
822
                                   ANDROID_SOCKET_NAMESPACE_RESERVED,
 
823
                                   SOCK_STREAM);
 
824
    if (sock < 0) return;
 
825
    size_t writeLen = strlen(buf) + 1;
 
826
 
 
827
    ssize_t ret = TEMP_FAILURE_RETRY(write(sock, buf, writeLen));
 
828
    if (ret <= 0) {
 
829
        close(sock);
 
830
        return;
 
831
    }
 
832
    while ((ret = read(sock, buf, len)) > 0) {
 
833
        if (((size_t)ret == len) || (len < PAGE_SIZE)) {
 
834
            break;
 
835
        }
 
836
        len -= ret;
 
837
        buf += ret;
 
838
 
 
839
        struct pollfd p = {
 
840
            .fd = sock,
 
841
            .events = POLLIN,
 
842
            .revents = 0
 
843
        };
 
844
 
 
845
        ret = poll(&p, 1, 20);
 
846
        if ((ret <= 0) || !(p.revents & POLLIN)) {
 
847
            break;
 
848
        }
 
849
    }
 
850
    close(sock);
 
851
}
 
852
 
 
853
static void BM_lookupEventTagNum_logd_new(int iters) {
 
854
    fprintf(stderr, "WARNING: "
 
855
            "This test can cause logd to grow in size and hit DOS limiter\n");
 
856
    // Make copies
 
857
    static const char empty_event_log_tags[] = "# content owned by logd\n";
 
858
    static const char dev_event_log_tags_path[] = "/dev/event-log-tags";
 
859
    std::string dev_event_log_tags;
 
860
    if (android::base::ReadFileToString(dev_event_log_tags_path,
 
861
                                        &dev_event_log_tags) &&
 
862
            (dev_event_log_tags.length() == 0)) {
 
863
        dev_event_log_tags = empty_event_log_tags;
 
864
    }
 
865
    static const char data_event_log_tags_path[] = "/data/misc/logd/event-log-tags";
 
866
    std::string data_event_log_tags;
 
867
    if (android::base::ReadFileToString(data_event_log_tags_path,
 
868
                                        &data_event_log_tags) &&
 
869
            (data_event_log_tags.length() == 0)) {
 
870
        data_event_log_tags = empty_event_log_tags;
 
871
    }
 
872
 
 
873
    for (int i = 0; i < iters; ++i) {
 
874
        char buffer[256];
 
875
        memset(buffer, 0, sizeof(buffer));
 
876
        log_time now(CLOCK_MONOTONIC);
 
877
        char name[64];
 
878
        snprintf(name, sizeof(name), "a%" PRIu64, now.nsec());
 
879
        snprintf(buffer, sizeof(buffer),
 
880
                 "getEventTag name=%s format=\"(new|1)\"", name);
 
881
        StartBenchmarkTiming();
 
882
        send_to_control(buffer, sizeof(buffer));
 
883
        StopBenchmarkTiming();
 
884
    }
 
885
 
 
886
    // Restore copies (logd still know about them, until crash or reboot)
 
887
    if (dev_event_log_tags.length() &&
 
888
            !android::base::WriteStringToFile(dev_event_log_tags,
 
889
                                              dev_event_log_tags_path)) {
 
890
        fprintf(stderr, "WARNING: "
 
891
                "failed to restore %s\n", dev_event_log_tags_path);
 
892
    }
 
893
    if (data_event_log_tags.length() &&
 
894
            !android::base::WriteStringToFile(data_event_log_tags,
 
895
                                              data_event_log_tags_path)) {
 
896
        fprintf(stderr, "WARNING: "
 
897
                "failed to restore %s\n", data_event_log_tags_path);
 
898
    }
 
899
    fprintf(stderr, "WARNING: "
 
900
            "Restarting logd to make it forget what we just did\n");
 
901
    system("stop logd ; start logd");
 
902
}
 
903
BENCHMARK(BM_lookupEventTagNum_logd_new);
 
904
 
 
905
static void BM_lookupEventTagNum_logd_existing(int iters) {
 
906
    prechargeEventMap();
 
907
 
 
908
    std::unordered_set<uint32_t>::const_iterator it = set.begin();
 
909
 
 
910
    for (int i = 0; i < iters; ++i) {
 
911
        size_t len;
 
912
        const char* name = android_lookupEventTag_len(map, &len, (*it));
 
913
        std::string Name(name, len);
 
914
        const char* format = android_lookupEventFormat_len(map, &len, (*it));
 
915
        std::string Format(format, len);
 
916
 
 
917
        char buffer[256];
 
918
        snprintf(buffer, sizeof(buffer),
 
919
                 "getEventTag name=%s format=\"%s\"",
 
920
                 Name.c_str(), Format.c_str());
 
921
 
 
922
        StartBenchmarkTiming();
 
923
        send_to_control(buffer, sizeof(buffer));
 
924
        StopBenchmarkTiming();
 
925
        ++it;
 
926
        if (it == set.end()) it = set.begin();
 
927
    }
 
928
}
 
929
BENCHMARK(BM_lookupEventTagNum_logd_existing);