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

Commit 115d6cd1 authored by Carmen Jackson's avatar Carmen Jackson Committed by android-build-merger
Browse files

Merge "Set the atrace clock to the best available value: boot, mono, or global." into oc-dev

am: bfe6bf6f

Change-Id: I4b198e07cdf1742aff8a771721021ca6ba28f20a
parents f5c23748 bfe6bf6f
Loading
Loading
Loading
Loading
+21 −46
Original line number Original line Diff line number Diff line
@@ -30,6 +30,7 @@
#include <unistd.h>
#include <unistd.h>
#include <zlib.h>
#include <zlib.h>


#include <fstream>
#include <memory>
#include <memory>


#include <binder/IBinder.h>
#include <binder/IBinder.h>
@@ -434,56 +435,31 @@ static bool setTraceBufferSizeKB(int size)
    return writeStr(k_traceBufferSizePath, str);
    return writeStr(k_traceBufferSizePath, str);
}
}


// Read the trace_clock sysfs file and return true if it matches the requested
// Set the clock to the best available option while tracing. Use 'boot' if it's
// value.  The trace_clock file format is:
// available; otherwise, use 'mono'. If neither are available use 'global'.
// local [global] counter uptime perf
static bool isTraceClock(const char *mode)
{
    int fd = open((g_traceFolder + k_traceClockPath).c_str(), O_RDONLY);
    if (fd == -1) {
        fprintf(stderr, "error opening %s: %s (%d)\n", k_traceClockPath,
            strerror(errno), errno);
        return false;
    }

    char buf[4097];
    ssize_t n = read(fd, buf, 4096);
    close(fd);
    if (n == -1) {
        fprintf(stderr, "error reading %s: %s (%d)\n", k_traceClockPath,
            strerror(errno), errno);
        return false;
    }
    buf[n] = '\0';

    char *start = strchr(buf, '[');
    if (start == NULL) {
        return false;
    }
    start++;

    char *end = strchr(start, ']');
    if (end == NULL) {
        return false;
    }
    *end = '\0';

    return strcmp(mode, start) == 0;
}

// Enable or disable the kernel's use of the global clock.  Disabling the global
// clock will result in the kernel using a per-CPU local clock.
// Any write to the trace_clock sysfs file will reset the buffer, so only
// Any write to the trace_clock sysfs file will reset the buffer, so only
// update it if the requested value is not the current value.
// update it if the requested value is not the current value.
static bool setGlobalClockEnable(bool enable)
static bool setClock()
{
{
    const char *clock = enable ? "global" : "local";
    std::ifstream clockFile((g_traceFolder + k_traceClockPath).c_str(), O_RDONLY);
    std::string clockStr((std::istreambuf_iterator<char>(clockFile)),
        std::istreambuf_iterator<char>());

    std::string newClock;
    if (clockStr.find("boot") != std::string::npos) {
        newClock = "boot";
    } else if (clockStr.find("mono") != std::string::npos) {
        newClock = "mono";
    } else {
        newClock = "global";
    }


    if (isTraceClock(clock)) {
    size_t begin = clockStr.find("[") + 1;
    size_t end = clockStr.find("]");
    if (newClock.compare(0, std::string::npos, clockStr, begin, end-begin) == 0) {
        return true;
        return true;
    }
    }

    return writeStr(k_traceClockPath, newClock.c_str());
    return writeStr(k_traceClockPath, clock);
}
}


static bool setPrintTgidEnableIfPresent(bool enable)
static bool setPrintTgidEnableIfPresent(bool enable)
@@ -776,7 +752,7 @@ static bool setUpTrace()
    ok &= setCategoriesEnableFromFile(g_categoriesFile);
    ok &= setCategoriesEnableFromFile(g_categoriesFile);
    ok &= setTraceOverwriteEnable(g_traceOverwrite);
    ok &= setTraceOverwriteEnable(g_traceOverwrite);
    ok &= setTraceBufferSizeKB(g_traceBufferSizeKB);
    ok &= setTraceBufferSizeKB(g_traceBufferSizeKB);
    ok &= setGlobalClockEnable(true);
    ok &= setClock();
    ok &= setPrintTgidEnableIfPresent(true);
    ok &= setPrintTgidEnableIfPresent(true);
    ok &= setKernelTraceFuncs(g_kernelTraceFuncs);
    ok &= setKernelTraceFuncs(g_kernelTraceFuncs);


@@ -848,7 +824,6 @@ static void cleanUpTrace()
    // Set the options back to their defaults.
    // Set the options back to their defaults.
    setTraceOverwriteEnable(true);
    setTraceOverwriteEnable(true);
    setTraceBufferSizeKB(1);
    setTraceBufferSizeKB(1);
    setGlobalClockEnable(false);
    setPrintTgidEnableIfPresent(false);
    setPrintTgidEnableIfPresent(false);
    setKernelTraceFuncs(NULL);
    setKernelTraceFuncs(NULL);
}
}
+5 −0
Original line number Original line Diff line number Diff line
@@ -124,6 +124,11 @@ on post-fs
    write /sys/kernel/debug/tracing/tracing_on 0
    write /sys/kernel/debug/tracing/tracing_on 0
    write /sys/kernel/tracing/tracing_on 0
    write /sys/kernel/tracing/tracing_on 0


    # Set the trace clock to boot; falling back to mono or boot
    write /d/tracing/trace_clock global
    write /d/tracing/trace_clock mono
    write /d/tracing/trace_clock boot

# Allow only the shell group to read and truncate the kernel trace.
# Allow only the shell group to read and truncate the kernel trace.
    chown root shell /sys/kernel/debug/tracing/trace
    chown root shell /sys/kernel/debug/tracing/trace
    chown root shell /sys/kernel/tracing/trace
    chown root shell /sys/kernel/tracing/trace