Fix scheduler_stats may resize to negative value
scheduler_stats will be empty when thread had been exited.
Test: This issue occurred on MTBF-Monkey test, however,
the root cause can be verified on the source code.
Change-Id: Ie8e61683126432aa03edef9ebbbf4baaadbdeafd
diff --git a/runtime/thread.cc b/runtime/thread.cc
index 6c41ae4..8bec2d9 100644
--- a/runtime/thread.cc
+++ b/runtime/thread.cc
@@ -39,6 +39,7 @@
#include <sstream>
#include "android-base/stringprintf.h"
+#include "android-base/strings.h"
#include "arch/context-inl.h"
#include "arch/context.h"
@@ -1875,8 +1876,9 @@
// Grab the scheduler stats for this thread.
std::string scheduler_stats;
- if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", tid), &scheduler_stats)) {
- scheduler_stats.resize(scheduler_stats.size() - 1); // Lose the trailing '\n'.
+ if (ReadFileToString(StringPrintf("/proc/self/task/%d/schedstat", tid), &scheduler_stats)
+ && !scheduler_stats.empty()) {
+ scheduler_stats = android::base::Trim(scheduler_stats); // Lose the trailing '\n'.
} else {
scheduler_stats = "0 0 0";
}