Пример #1
0
        internal void FetchProcessInfo(int id)
        {
            cmdInfo = new ProcessCommandInfo();

            try
            {
                if (id != -2147483648)
                {
                    var startInfo = new ProcessStartInfo("bash", string.Format("-c \"ps -p {0} -xo command && ps -p {0} -xco command && ps -p {0} -xco args && ps -p {0} -xo ppid && ps -p {0} -xo rss && ps -p {0} -xo minflt && ps -p {0} -xo majflt && ps -p {0} -xo sess && ps -p {0} -xo vsz && ps -p {0} -M | wc -l\"", id))
                    {
                        UseShellExecute = false, CreateNoWindow = false, RedirectStandardOutput = true, RedirectStandardError = true
                    };

                    string[] resultLines = new string[19];
                    int      i           = 0;
                    Process  p           = Process.Start(startInfo);
                    DataReceivedEventHandler receiveHandler = (object sender, DataReceivedEventArgs e) => {
                        if (i >= resultLines.Length)
                        {
                            return;
                        }
                        if (!string.IsNullOrEmpty(e.Data))
                        {
                            resultLines[i] = e.Data;
                        }
                        else
                        {
                            resultLines[i] = "";
                        }
                        i++;
                    };
                    DataReceivedEventHandler errorHandler = (object sender, DataReceivedEventArgs e) => {
                    };
                    p.ErrorDataReceived  += errorHandler;
                    p.OutputDataReceived += receiveHandler;
                    p.BeginOutputReadLine();
                    p.WaitForExit();
                    p.ErrorDataReceived  -= errorHandler;
                    p.OutputDataReceived -= receiveHandler;
                    cmdInfo.CommandLine   = resultLines[1];
                    string fileName = resultLines[3];
                    string args     = resultLines[5];
                    cmdInfo.ProcessName = fileName;
                    cmdInfo.FileName    = cmdInfo.CommandLine.Replace(args, fileName);
                    args = args.Replace(fileName, "");
                    if (args.StartsWith(" "))
                    {
                        args = args.Substring(1);
                    }
                    cmdInfo.Arguments = args;
                    int ppid;
                    if (int.TryParse(resultLines[7].Trim(), out ppid))
                    {
                        cmdInfo.ParentProcessId = ppid;
                    }
                    uint maxWSize;
                    if (uint.TryParse(resultLines[9].Trim(), out maxWSize))
                    {
                        cmdInfo.MaxWorkingSetSize = maxWSize;
                    }
                    uint minWSize;
                    if (uint.TryParse(resultLines[9].Trim(), out minWSize))
                    {
                        cmdInfo.MinWorkingSetSize = minWSize;
                    }
                    uint maxPf;
                    if (uint.TryParse(resultLines[11].Trim(), out maxPf))
                    {
                        cmdInfo.MajorPageFaults = maxPf;
                    }
                    uint minPf;
                    if (uint.TryParse(resultLines[13].Trim(), out minPf))
                    {
                        cmdInfo.MinorPageFaults = minPf;
                    }
                    uint sess;
                    if (uint.TryParse(resultLines[15].Trim(), out sess))
                    {
                        cmdInfo.SessionId = sess;
                    }
                    ulong vmem;
                    if (ulong.TryParse(resultLines[17].Trim(), out vmem))
                    {
                        cmdInfo.VirtualMemorySize = vmem;
                    }
                    uint thcount;
                    if (uint.TryParse(resultLines[18].Trim(), out thcount))
                    {
                        cmdInfo.ThreadsCount = thcount;
                    }
                }
            }
            catch (Win32Exception)
            {
            }
            catch (InvalidOperationException)
            {
            }
        }
Пример #2
0
		internal void FetchProcessInfo(int id)
		{
			cmdInfo = new ProcessCommandInfo();

			try
			{
				if (id != -2147483648)
				{
					var startInfo = new ProcessStartInfo("bash", string.Format ("-c \"ps -p {0} -xo command && ps -p {0} -xco command && ps -p {0} -xco args && ps -p {0} -xo ppid && ps -p {0} -xo rss && ps -p {0} -xo minflt && ps -p {0} -xo majflt && ps -p {0} -xo sess && ps -p {0} -xo vsz && ps -p {0} -M | wc -l\"", id)) { UseShellExecute = false, CreateNoWindow = false, RedirectStandardOutput = true, RedirectStandardError  = true };

					string[]  resultLines = new string[19];
					int i = 0;
					Process p = Process.Start (startInfo);
					DataReceivedEventHandler receiveHandler = (object sender, DataReceivedEventArgs e) => {
						if (i >= resultLines.Length) return;
						if (!string.IsNullOrEmpty (e.Data)) resultLines[i] = e.Data;
						else resultLines[i] = "";
						i++;
					};
					DataReceivedEventHandler errorHandler = (object sender, DataReceivedEventArgs e) => {
					
					};
					p.ErrorDataReceived += errorHandler;
					p.OutputDataReceived += receiveHandler;
					p.BeginOutputReadLine ();
					p.WaitForExit ();
					p.ErrorDataReceived -= errorHandler;
					p.OutputDataReceived -= receiveHandler;
					cmdInfo.CommandLine = resultLines[1];
					string fileName = resultLines[3];
					string args = resultLines[5];
					cmdInfo.ProcessName = fileName;
					cmdInfo.FileName = cmdInfo.CommandLine.Replace (args, fileName);
					args = args.Replace (fileName, "");
					if (args.StartsWith (" ")) args = args.Substring (1);
					cmdInfo.Arguments = args;
					int ppid;
					if (int.TryParse (resultLines[7].Trim (), out ppid))
					{
						cmdInfo.ParentProcessId = ppid;
					}
					uint maxWSize;
					if (uint.TryParse (resultLines[9].Trim (), out maxWSize))
					{
						cmdInfo.MaxWorkingSetSize = maxWSize;
					}
					uint minWSize;
					if (uint.TryParse (resultLines[9].Trim (), out minWSize))
					{
						cmdInfo.MinWorkingSetSize = minWSize;
					}
					uint maxPf;
					if (uint.TryParse (resultLines[11].Trim (), out maxPf))
					{
						cmdInfo.MajorPageFaults = maxPf;
					}
					uint minPf;
					if (uint.TryParse (resultLines[13].Trim (), out minPf))
					{
						cmdInfo.MinorPageFaults = minPf;
					}
					uint sess;
					if (uint.TryParse (resultLines[15].Trim (), out sess))
					{
						cmdInfo.SessionId = sess;
					}
					ulong vmem;
					if (ulong.TryParse (resultLines[17].Trim (), out vmem))
					{
						cmdInfo.VirtualMemorySize = vmem;
					}
					uint thcount;
					if (uint.TryParse (resultLines[18].Trim (), out thcount))
					{
						cmdInfo.ThreadsCount = thcount;
					}
				}
			}
			catch (Win32Exception)
			{

			}
			catch (InvalidOperationException)
			{

			}
		}