#include <sys/sendfile.h>
#include <time.h>
#include <zlib.h>
-
#include <stdint.h>
#include <strings.h>
#include <string.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/smack.h>
+#include <sys/types.h>
#include <unistd.h>
+
+#include <fstream>
+
#include "ttrace.h"
+
+using std::string;
+
#define TTRACE_TAG_NONE 9999
#define TAG_NONE_IDX 0
return writeStr(k_traceBufferSizePath, str);
}
-// 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.
-static bool setGlobalClockEnable(bool enable)
+// Set the clock to the best available option while tracing. Use 'boot' if it's
+// available; otherwise, use 'mono'. If neither are available use 'global'.
+// 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.
+static bool setBestClock(bool enable)
{
- return writeStr(k_traceClockPath, enable ? "global" : "local");
+ std::ifstream clockFile(k_traceClockPath, std::ios::in);
+ std::string clockStr((std::istreambuf_iterator<char>(clockFile)),
+ std::istreambuf_iterator<char>());
+ std::string newClock;
+
+ if (enable == false)
+ return writeStr(k_traceClockPath, "local");
+
+ if (clockStr.find("boot") != std::string::npos)
+ newClock = "boot";
+ else if (clockStr.find("mono") != std::string::npos)
+ newClock = "mono";
+ else
+ newClock = "global";
+
+ 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 writeStr(k_traceClockPath, newClock.c_str());
}
static bool setPrintTgidEnableIfPresent(bool enable)
ok &= setTraceOverwriteEnable(g_traceOverwrite);
if(!g_append_trace) {
ok &= setTraceBufferSizeKB(g_traceBufferSizeKB);
- ok &= setGlobalClockEnable(true);
+ ok &= setBestClock(true);
ok &= setKernelTraceFuncs(g_kernelTraceFuncs);
}
ok &= setPrintTgidEnableIfPresent(true);
// Set the options back to their defaults.
setTraceOverwriteEnable(true);
setTraceBufferSizeKB(1);
- setGlobalClockEnable(false);
+ setBestClock(false);
setPrintTgidEnableIfPresent(false);
setKernelTraceFuncs(nullptr);
}