コード例 #1
0
ファイル: DataStatistics.cs プロジェクト: hch-im/ptopw
 public static extern bool GetNetInfo(ref WNICInfo info);
コード例 #2
0
ファイル: DataStatistics.cs プロジェクト: hch-im/ptopw
        public void stat()
        {
            ManagementObject diskObj = RetrieveManagementObject("Win32_PerfFormattedData_PerfDisk_PhysicalDisk");
            //read system info
            sysInfo.percentDiskIdleTime = (UInt64)diskObj["PercentIdleTime"];
            sysInfo.percentDiskReadTime = (UInt64)diskObj["PercentDiskReadTime"];
            sysInfo.percentDiskWriteTime = (UInt64)diskObj["PercentDiskWriteTime"];
            //network
            sysInfo.nrecv = 0;
            sysInfo.nsend = 0;
            if (hasWNIC)
            {
                WNICInfo info = new WNICInfo();
                info.index = wnicIndex;
                bool res = GetNetInfo(ref info);
                if(res)
                {
                    sysInfo.nrecv = info.In - sysInfo.winfo.In;
                    sysInfo.nsend = info.Out - sysInfo.winfo.Out;
                    sysInfo.winfo = info;
                }
            }
            //memory
            ManagementObject memObj = RetrieveManagementObject("Win32_PerfFormattedData_PerfOS_Memory");
            ManagementObject cacheObj = RetrieveManagementObject("Win32_PerfFormattedData_PerfOS_Cache");

            sysInfo.memRead = (UInt32)memObj["PagesOutputPerSec"];//pages/sec
            sysInfo.memWrite = (UInt32)memObj["PagesInputPerSec"];
            sysInfo.memCopy = (UInt32)cacheObj["CopyReadsPerSec"];//pages/sec
            //processor
            ManagementObject cpuObj = RetrieveManagementObject("Win32_Processor");
            sysInfo.cpuFrequency = (UInt32)cpuObj["CurrentClockSpeed"];
            sysInfo.cpuTime = 0;
            sysInfo.voltage = (UInt16)cpuObj["CurrentVoltage"];
            //disk
            sysInfo.diskRead = 0;
            sysInfo.diskWrite = 0;

            //save
            tempData.beginRecord();
            tempData.addSystemInfo(sysInfo);//before add process info, important
            //read process info

                Process[] processes = Process.GetProcesses();
                HashSet<UInt32> processNetStatus = NetInfoWrapper.netActiveProcesses();
                sysInfo.NetActiveProcessNum = processNetStatus.Count;

                foreach (Process p in processes)
                {
                    int id = 0;
                    try
                    {
                        String name = p.ProcessName;
                        id = p.Id;

                        ProcessInfo pinfo = statProcessInfo(p);
                        if (pinfo != null)
                        {
                            if (processNetStatus.Contains((UInt32)p.Id))
                                pinfo.networkActive = true;
                            tempData.addProcessInfo(pinfo);

                            sysInfo.cpuTime += pinfo.cpuTime;
                            sysInfo.diskRead += pinfo.dread;
                            sysInfo.diskWrite += pinfo.dwrite;
                        }
                    }
                    catch (Exception ex)//process exited
                    {
                        if (counterTable.ContainsKey(id))
                        {
                            counterTable.Remove(id);
                        }
                        Console.WriteLine(ex.StackTrace);
                    }
                }

            //update time
            double time = new TimeSpan(DateTime.Now.Ticks).TotalMilliseconds;
            sysInfo.timeInterval = time - sysInfo.time;
            sysInfo.time = time;
            tempData.endRecord();

            //log if needed
            if (SaveResult == 1) LogResult();
        }