internal static bool TryReadStatFile(int pid, out ParsedStat result, ReusableTextReader reusableReader) { bool b = TryParseStatFile(GetStatFilePathForProcess(pid), out result, reusableReader); Debug.Assert(!b || result.pid == pid, "Expected process ID from stat file to match supplied pid"); return(b); }
internal static bool TryReadStatFile(int pid, int tid, out ParsedStat result) { bool b = TryParseStatFile(GetStatFilePathForThread(pid, tid), out result); Debug.Assert(!b || result.pid == tid, "Expected thread ID from stat file to match supplied tid"); return(b); }
internal static bool TryReadStatFile(int pid, int tid, out ParsedStat result, ReusableTextReader reusableReader) { bool b = TryParseStatFile(GetStatFilePathForThread(pid, tid), out result, reusableReader); // // This assert currently fails in the Windows Subsystem For Linux. See https://github.com/Microsoft/BashOnWindows/issues/967. // //Debug.Assert(!b || result.pid == tid, "Expected thread ID from stat file to match supplied tid"); return(b); }
private static bool TryParseStatFile(string statFilePath, out ParsedStat result, ReusableTextReader reusableReader) { string statFileContents; try { using (var source = new FileStream(statFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1, useAsync: false)) { statFileContents = reusableReader.ReadAllText(source); } } catch (IOException) { // Between the time that we get an ID and the time that we try to read the associated stat // file(s), the process could be gone. result = default(ParsedStat); return false; } var parser = new StringParser(statFileContents, ' '); var results = default(ParsedStat); results.pid = parser.ParseNextInt32(); results.comm = parser.ParseRaw(delegate (string str, ref int startIndex, ref int endIndex) { if (str[startIndex] == '(') { int i; for (i = endIndex; i < str.Length && str[i - 1] != ')'; i++) ; if (str[i - 1] == ')') { endIndex = i; return str.Substring(startIndex + 1, i - startIndex - 2); } } throw new InvalidDataException(); }); results.state = parser.ParseNextChar(); parser.MoveNextOrFail(); // ppid parser.MoveNextOrFail(); // pgrp results.session = parser.ParseNextInt32(); parser.MoveNextOrFail(); // tty_nr parser.MoveNextOrFail(); // tpgid parser.MoveNextOrFail(); // flags parser.MoveNextOrFail(); // majflt parser.MoveNextOrFail(); // cmagflt parser.MoveNextOrFail(); // minflt parser.MoveNextOrFail(); // cminflt results.utime = parser.ParseNextUInt64(); results.stime = parser.ParseNextUInt64(); parser.MoveNextOrFail(); // cutime parser.MoveNextOrFail(); // cstime parser.MoveNextOrFail(); // priority results.nice = parser.ParseNextInt64(); parser.MoveNextOrFail(); // num_threads parser.MoveNextOrFail(); // itrealvalue results.starttime = parser.ParseNextUInt64(); results.vsize = parser.ParseNextUInt64(); results.rss = parser.ParseNextInt64(); results.rsslim = parser.ParseNextUInt64(); parser.MoveNextOrFail(); // startcode parser.MoveNextOrFail(); // endcode results.startstack = parser.ParseNextUInt64(); // The following lines are commented out as there's no need to parse through // the rest of the entry (we've gotten all of the data we need). Should any // of these fields be needed in the future, uncomment all of the lines up // through and including the one that's needed. For now, these are being left // commented to document what's available in the remainder of the entry. //parser.MoveNextOrFail(); // kstkesp //parser.MoveNextOrFail(); // kstkeip //parser.MoveNextOrFail(); // signal //parser.MoveNextOrFail(); // blocked //parser.MoveNextOrFail(); // sigignore //parser.MoveNextOrFail(); // sigcatch //parser.MoveNextOrFail(); // wchan //parser.MoveNextOrFail(); // nswap //parser.MoveNextOrFail(); // cnswap //parser.MoveNextOrFail(); // exit_signal //parser.MoveNextOrFail(); // processor //parser.MoveNextOrFail(); // rt_priority //parser.MoveNextOrFail(); // policy //parser.MoveNextOrFail(); // delayacct_blkio_ticks //parser.MoveNextOrFail(); // guest_time //parser.MoveNextOrFail(); // cguest_time result = results; return true; }
internal static bool TryReadStatFile(int pid, int tid, out ParsedStat result, ReusableTextReader reusableReader) { bool b = TryParseStatFile(GetStatFilePathForThread(pid, tid), out result, reusableReader); Debug.Assert(!b || result.pid == tid, "Expected thread ID from stat file to match supplied tid"); return b; }
internal static bool TryParseStatFile(string statFilePath, out ParsedStat result, ReusableTextReader reusableReader) { string statFileContents; try { using (var source = new FileStream(statFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1, useAsync: false)) { statFileContents = reusableReader.ReadAllText(source); } } catch (IOException) { // Between the time that we get an ID and the time that we try to read the associated stat // file(s), the process could be gone. result = default(ParsedStat); return(false); } var parser = new StringParser(statFileContents, ' '); var results = default(ParsedStat); results.pid = parser.ParseNextInt32(); results.comm = parser.MoveAndExtractNextInOuterParens(); results.state = parser.ParseNextChar(); results.ppid = parser.ParseNextInt32(); parser.MoveNextOrFail(); // pgrp results.session = parser.ParseNextInt32(); parser.MoveNextOrFail(); // tty_nr parser.MoveNextOrFail(); // tpgid parser.MoveNextOrFail(); // flags parser.MoveNextOrFail(); // majflt parser.MoveNextOrFail(); // cmagflt parser.MoveNextOrFail(); // minflt parser.MoveNextOrFail(); // cminflt results.utime = parser.ParseNextUInt64(); results.stime = parser.ParseNextUInt64(); parser.MoveNextOrFail(); // cutime parser.MoveNextOrFail(); // cstime parser.MoveNextOrFail(); // priority results.nice = parser.ParseNextInt64(); parser.MoveNextOrFail(); // num_threads parser.MoveNextOrFail(); // itrealvalue results.starttime = parser.ParseNextUInt64(); results.vsize = parser.ParseNextUInt64(); results.rss = parser.ParseNextInt64(); results.rsslim = parser.ParseNextUInt64(); // The following lines are commented out as there's no need to parse through // the rest of the entry (we've gotten all of the data we need). Should any // of these fields be needed in the future, uncomment all of the lines up // through and including the one that's needed. For now, these are being left // commented to document what's available in the remainder of the entry. //parser.MoveNextOrFail(); // startcode //parser.MoveNextOrFail(); // endcode //parser.MoveNextOrFail(); // startstack //parser.MoveNextOrFail(); // kstkesp //parser.MoveNextOrFail(); // kstkeip //parser.MoveNextOrFail(); // signal //parser.MoveNextOrFail(); // blocked //parser.MoveNextOrFail(); // sigignore //parser.MoveNextOrFail(); // sigcatch //parser.MoveNextOrFail(); // wchan //parser.MoveNextOrFail(); // nswap //parser.MoveNextOrFail(); // cnswap //parser.MoveNextOrFail(); // exit_signal //parser.MoveNextOrFail(); // processor //parser.MoveNextOrFail(); // rt_priority //parser.MoveNextOrFail(); // policy //parser.MoveNextOrFail(); // delayacct_blkio_ticks //parser.MoveNextOrFail(); // guest_time //parser.MoveNextOrFail(); // cguest_time result = results; return(true); }
internal static bool TryReadStatFile(int pid, int tid, out ParsedStat result, ReusableTextReader reusableReader) { bool b = TryParseStatFile(GetStatFilePathForThread(pid, tid), out result, reusableReader); // // This assert currently fails in the Windows Subsystem For Linux. See https://github.com/Microsoft/BashOnWindows/issues/967. // //Debug.Assert(!b || result.pid == tid, "Expected thread ID from stat file to match supplied tid"); return b; }
private static bool TryParseStatFile(string statFilePath, out ParsedStat result, ReusableTextReader reusableReader) { string statFileContents; try { using (var source = new FileStream(statFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1, useAsync: false)) { statFileContents = reusableReader.ReadAllText(source); } } catch (IOException) { // Between the time that we get an ID and the time that we try to read the associated stat // file(s), the process could be gone. result = default(ParsedStat); return false; } var parser = new StringParser(statFileContents, ' '); var results = default(ParsedStat); results.pid = parser.ParseNextInt32(); results.comm = parser.ParseRaw(delegate (string str, ref int startIndex, ref int endIndex) { if (str[startIndex] == '(') { int i; for (i = endIndex; i < str.Length && str[i - 1] != ')'; i++) ; if (str[i - 1] == ')') { endIndex = i; return str.Substring(startIndex + 1, i - startIndex - 2); } } throw new InvalidDataException(); }); results.state = parser.ParseNextChar(); parser.MoveNextOrFail(); // ppid parser.MoveNextOrFail(); // pgrp results.session = parser.ParseNextInt32(); parser.MoveNextOrFail(); // tty_nr parser.MoveNextOrFail(); // tpgid parser.MoveNextOrFail(); // flags parser.MoveNextOrFail(); // majflt parser.MoveNextOrFail(); // cmagflt parser.MoveNextOrFail(); // minflt parser.MoveNextOrFail(); // cminflt results.utime = parser.ParseNextUInt64(); results.stime = parser.ParseNextUInt64(); parser.MoveNextOrFail(); // cutime parser.MoveNextOrFail(); // cstime parser.MoveNextOrFail(); // priority results.nice = parser.ParseNextInt64(); parser.MoveNextOrFail(); // num_threads parser.MoveNextOrFail(); // itrealvalue results.starttime = parser.ParseNextUInt64(); results.vsize = parser.ParseNextUInt64(); results.rss = parser.ParseNextInt64(); results.rsslim = parser.ParseNextUInt64(); parser.MoveNextOrFail(); // startcode parser.MoveNextOrFail(); // endcode results.startstack = parser.ParseNextUInt64(); parser.MoveNextOrFail(); // kstkesp parser.MoveNextOrFail(); // kstkeip parser.MoveNextOrFail(); // signal parser.MoveNextOrFail(); // blocked parser.MoveNextOrFail(); // sigignore parser.MoveNextOrFail(); // sigcatch parser.MoveNextOrFail(); // wchan parser.MoveNextOrFail(); // nswap parser.MoveNextOrFail(); // cnswap parser.MoveNextOrFail(); // exit_signal parser.MoveNextOrFail(); // processor parser.MoveNextOrFail(); // rt_priority parser.MoveNextOrFail(); // policy parser.MoveNextOrFail(); // delayacct_blkio_ticks parser.MoveNextOrFail(); // guest_time parser.MoveNextOrFail(); // cguest_time result = results; return true; }
private static bool TryParseStatFile(string statFilePath, out ParsedStat result, ReusableTextReader reusableReader) { string statFileContents; try { using (var source = new FileStream(statFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 1, useAsync: false)) { statFileContents = reusableReader.ReadAllText(source); } } catch (IOException) { // Between the time that we get an ID and the time that we try to read the associated stat // file(s), the process could be gone. result = default(ParsedStat); return(false); } var parser = new StringParser(statFileContents, ' '); var results = default(ParsedStat); results.pid = parser.ParseNextInt32(); results.comm = parser.ParseRaw(delegate(string str, ref int startIndex, ref int endIndex) { if (str[startIndex] == '(') { int i; for (i = endIndex; i < str.Length && str[i - 1] != ')'; i++) { ; } if (str[i - 1] == ')') { endIndex = i; return(str.Substring(startIndex + 1, i - startIndex - 2)); } } throw new InvalidDataException(); }); results.state = parser.ParseNextChar(); parser.MoveNextOrFail(); // ppid parser.MoveNextOrFail(); // pgrp results.session = parser.ParseNextInt32(); parser.MoveNextOrFail(); // tty_nr parser.MoveNextOrFail(); // tpgid parser.MoveNextOrFail(); // flags parser.MoveNextOrFail(); // majflt parser.MoveNextOrFail(); // cmagflt parser.MoveNextOrFail(); // minflt parser.MoveNextOrFail(); // cminflt results.utime = parser.ParseNextUInt64(); results.stime = parser.ParseNextUInt64(); parser.MoveNextOrFail(); // cutime parser.MoveNextOrFail(); // cstime parser.MoveNextOrFail(); // priority results.nice = parser.ParseNextInt64(); parser.MoveNextOrFail(); // num_threads parser.MoveNextOrFail(); // itrealvalue results.starttime = parser.ParseNextUInt64(); results.vsize = parser.ParseNextUInt64(); results.rss = parser.ParseNextInt64(); results.rsslim = parser.ParseNextUInt64(); parser.MoveNextOrFail(); // startcode parser.MoveNextOrFail(); // endcode results.startstack = parser.ParseNextUInt64(); parser.MoveNextOrFail(); // kstkesp parser.MoveNextOrFail(); // kstkeip parser.MoveNextOrFail(); // signal parser.MoveNextOrFail(); // blocked parser.MoveNextOrFail(); // sigignore parser.MoveNextOrFail(); // sigcatch parser.MoveNextOrFail(); // wchan parser.MoveNextOrFail(); // nswap parser.MoveNextOrFail(); // cnswap parser.MoveNextOrFail(); // exit_signal parser.MoveNextOrFail(); // processor parser.MoveNextOrFail(); // rt_priority parser.MoveNextOrFail(); // policy parser.MoveNextOrFail(); // delayacct_blkio_ticks parser.MoveNextOrFail(); // guest_time parser.MoveNextOrFail(); // cguest_time result = results; return(true); }