public process_statistics FromByte(byte[] buf) { int offset = 0; Int16 bLen = BitConverter.ToInt16(buf, 0); //2 bytes offset += sizeof(System.Int16); if (bLen > 0) { this.sName = System.Text.Encoding.UTF8.GetString(buf, offset, bLen); } offset += bLen; this.dateTime = BitConverter.ToInt64(buf, offset); offset += sizeof(System.Int64); this.duration = BitConverter.ToUInt32(buf, offset); offset += sizeof(System.Int32); this.procUsage = new process_usage(ref buf, ref offset); //offset = offset; //has been changed by process_usage this.processID = BitConverter.ToUInt32(buf, offset); offset += sizeof(System.UInt32); //how many thtreads are in the byte stream Int16 iCnt = BitConverter.ToInt16(buf, offset); offset += sizeof(System.Int16); //start reading the threads List <threadStatistic> thList = new List <threadStatistic>(); for (int x = 0; x < iCnt; x++) { threadStatistic th = new threadStatistic(buf, ref offset); thList.Add(th); } this.ThreadStatList = thList; this.remoteIP = "0.0.0.0"; //unknown return(this); }
/// <summary> /// build thread and process list periodically and fire update event and enqueue results for the socket thread /// </summary> void usageThread() { try { int interval = 3000; uint start = Process.GetTickCount(); Dictionary<uint, thread> old_thread_List;// = Process.GetThreadList(); string exeFile = Process.exefile; //read all processes Dictionary<uint, process> ProcList = Process.getProcessNameList(); DateTime dtCurrent = DateTime.Now; //######### var declarations Dictionary<uint, thread> new_ThreadList; uint duration; long system_total; long user_total, kernel_total; //total process spend in user/kernel long thread_user, thread_kernel; //times the thread spend in user/kernel DWORD dwProc; float user_percent; float kernel_percent; ProcessStatistics.process_usage usage; ProcessStatistics.process_statistics stats = null; string sProcessName = ""; List<thread> processThreadList = new List<thread>(); //extended list List<threadStatistic> processThreadStatsList = new List<threadStatistic>(); //to store thread stats //threadtimes threadTimesTotal; while (!bStopMainThread) { eventEnableCapture.WaitOne(); old_thread_List = Process.GetThreadList(); //build a list of threads with user and kernel times System.Threading.Thread.Sleep(interval); //get a new thread list new_ThreadList = Process.GetThreadList(); //build another list of threads with user and kernel times, to compare duration = Process.GetTickCount() - start; ProcList = Process.getProcessNameList(); //update process list dtCurrent = DateTime.Now; system_total = 0; statisticsTimes.Clear(); //look thru all processes foreach (KeyValuePair<uint, process> p2 in ProcList) { //empty the process's thread list processThreadList=new List<thread>(); processThreadStatsList = new List<threadStatistic>(); user_total = 0; //hold sum of thread user times for a process kernel_total = 0; //hold sum of thread kernel times for a process sProcessName = p2.Value.sName; //SUM over all threads with that ProcID dwProc = p2.Value.dwProcID; foreach (KeyValuePair<uint, thread> kpNew in new_ThreadList) { thread_user = 0; thread_kernel = 0; //if the thread belongs to the process if (kpNew.Value.dwOwnerProcID == dwProc) { //is there an old thread entry we can use to calc? thread threadOld; if (old_thread_List.TryGetValue(kpNew.Value.dwThreadID, out threadOld)) { thread_user=Process.GetThreadTick(kpNew.Value.thread_times.user) - Process.GetThreadTick(old_thread_List[kpNew.Value.dwThreadID].thread_times.user); user_total += thread_user; thread_kernel =Process.GetThreadTick(kpNew.Value.thread_times.kernel) - Process.GetThreadTick(old_thread_List[kpNew.Value.dwThreadID].thread_times.kernel); kernel_total += thread_kernel; } //simple list thread threadsOfProcess = new thread(kpNew.Value.dwOwnerProcID, kpNew.Value.dwThreadID, kpNew.Value.thread_times); processThreadList.Add(threadsOfProcess); //extended list threadStatistic threadStats = new threadStatistic( kpNew.Value.dwOwnerProcID, kpNew.Value.dwThreadID, new threadtimes(thread_user, thread_kernel), duration, dtCurrent.Ticks); processThreadStatsList.Add(threadStats); }//if dwProcID matches } //end of sum for process user_percent = (float)user_total / (float)duration * 100f; kernel_percent = (float)kernel_total / (float)duration * 100f; system_total = user_total + kernel_total; // update the statistics with this process' info usage = new ProcessStatistics.process_usage(kernel_total, user_total); // update process statistics //stats = new ProcessStatistics.process_statistics(p2.Value.dwProcID, p2.Value.sName, usage, dtCurrent.Ticks, duration, processThreadList); stats = new ProcessStatistics.process_statistics(p2.Value.dwProcID, p2.Value.sName, usage, dtCurrent.Ticks, duration, processThreadStatsList); //add or update the proc stats if (exeFile != p2.Value.sName || bIncludeMySelf) { //if (sProcessName == "device.exe") // System.Diagnostics.Debug.WriteLine(stats.ToString()); statisticsTimes[p2.Value.sName] = stats; //lock (lockQueue) //{ //System.Diagnostics.Debug.WriteLine("Queue Adding " + stats.sName); //procStatsQueue.Enqueue(stats); procStatsQueueBytes.Enqueue(stats.ToByte()); //} } start = Process.GetTickCount(); }//foreach process onUpdateHandler(new ProcessStatsEventArgs(statisticsTimes, duration)); procStatsQueueBytes.Enqueue(ByteHelper.endOfTransferBytes); ((AutoResetEvent)eventEnableSend).Set(); //dumpStatistics(statisticsTimes); }//while true } catch (ThreadAbortException ex) { System.Diagnostics.Debug.WriteLine("ThreadAbortException: usageThread(): " + ex.Message); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Exception: usageThread(): " + ex.Message); } System.Diagnostics.Debug.WriteLine("Thread ENDED"); }
public process_statistics FromByte(byte[] buf) { int offset = 0; Int16 bLen = BitConverter.ToInt16(buf, 0); //2 bytes offset += sizeof(System.Int16); if (bLen > 0) this.sName = System.Text.Encoding.UTF8.GetString(buf, offset, bLen); offset += bLen; this.dateTime = BitConverter.ToInt64(buf, offset); offset += sizeof(System.Int64); this.duration = BitConverter.ToUInt32(buf, offset); offset += sizeof(System.Int32); this.procUsage = new process_usage(ref buf, ref offset); //offset = offset; //has been changed by process_usage this.processID = BitConverter.ToUInt32(buf, offset); offset += sizeof(System.UInt32); //how many thtreads are in the byte stream Int16 iCnt = BitConverter.ToInt16(buf, offset); offset += sizeof(System.Int16); //start reading the threads List<threadStatistic> thList = new List<threadStatistic>(); for (int x = 0; x < iCnt; x++) { threadStatistic th = new threadStatistic(buf, ref offset); thList.Add(th); } this.ThreadStatList = thList; this.remoteIP = "0.0.0.0"; //unknown return this; }