20 #include <boost/filesystem.hpp>
21 #include <boost/algorithm/string.hpp>
22 #include <gutil/strings/substitute.h>
29 using boost::filesystem::exists;
30 using boost::algorithm::is_any_of;
31 using boost::algorithm::token_compress_on;
32 using boost::algorithm::split;
33 using namespace impala;
34 using namespace strings;
53 DCHECK(stats != NULL);
56 stringstream proc_path;
57 proc_path <<
"/proc/self/task/" << tid <<
"/stat";
58 if (!exists(proc_path.str()))
return Status(
"Thread path does not exist");
60 ifstream proc_file(proc_path.str().c_str());
61 if (!proc_file.is_open())
return Status(
"Could not open ifstream");
63 string buffer((std::istreambuf_iterator<char>(proc_file)),
64 std::istreambuf_iterator<char>());
66 vector<string> splits;
67 split(splits, buffer, is_any_of(
" "), token_compress_on);
71 int64_t tmp = StringParser::StringToInt<int64_t>(splits[
USER_TICKS].c_str(),
77 tmp = StringParser::StringToInt<int64_t>(splits[
KERNEL_TICKS].c_str(),
83 tmp = StringParser::StringToInt<int64_t>(splits[
IO_WAIT].c_str(),
84 splits[
IO_WAIT].size(), &parse_result);
94 FILE* fp = popen(cmd.c_str(),
"r");
96 *msg = Substitute(
"Failed to execute shell cmd: '$0', error was: $1", cmd,
103 size_t len = fread(buf, 1, 1024, fp);
105 output.assign(buf, len);
108 int status = pclose(fp);
109 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
114 *msg = Substitute(
"Shell cmd: '$0' exited with an error: '$1'. Output was: '$2'", cmd,
bool RunShellProcess(const std::string &cmd, std::string *msg)
static const int64_t IO_WAIT
static const int64_t USER_TICKS
static const int64_t TICKS_PER_SEC
static const int64_t KERNEL_TICKS
Container struct for statistics read from the /proc filesystem for a thread.
Status GetThreadStats(int64_t tid, ThreadStats *stats)
static const int64_t MAX_OFFSET