Bool HawkProfiler::Start(const AString& sAddr) { if (!m_bRunning) { if (sAddr.size()) m_sAddr = sAddr; GetProcessorNumber(); GetTotalMem(); GetCpuUsage(); GetMemUsage(); if(!m_sSocket.Create(AF_INET,SOCK_DGRAM,IPPROTO_UDP)) { HawkPrint("Profiler Init Socket Error."); return false; } m_sSocket.SetNoDelay(true); m_sSocket.SetBlocking(false); if(!m_sSocket.Bind(SocketAddr(m_sAddr))) { HawkPrint("Profiler Bind Socket Error."); return false; } m_bRunning = true; m_pThread = new HawkThread(hawk_ProfilerRoutine); m_pThread->Start(this); return true; } return false; }
/** This function will be called from AP reset code if BSP uses WakeUpAP. @param ExchangeInfo Pointer to the MP exchange info buffer @param NumApsExecuting Number of curret executing AP **/ VOID EFIAPI ApCFunction ( IN MP_CPU_EXCHANGE_INFO *ExchangeInfo, IN UINTN NumApsExecuting ) { PEI_CPU_MP_DATA *PeiCpuMpData; UINTN ProcessorNumber; EFI_AP_PROCEDURE Procedure; UINTN BistData; PeiCpuMpData = ExchangeInfo->PeiCpuMpData; if (PeiCpuMpData->InitFlag) { // // This is first time AP wakeup, get BIST information from AP stack // BistData = *(UINTN *) (PeiCpuMpData->Buffer + NumApsExecuting * PeiCpuMpData->CpuApStackSize - sizeof (UINTN)); PeiCpuMpData->CpuData[NumApsExecuting].Health.Uint32 = (UINT32) BistData; PeiCpuMpData->CpuData[NumApsExecuting].ApicId = GetInitialApicId (); if (PeiCpuMpData->CpuData[NumApsExecuting].ApicId >= 0xFF) { // // Set x2APIC mode if there are any logical processor reporting // an APIC ID of 255 or greater. // AcquireSpinLock(&PeiCpuMpData->MpLock); PeiCpuMpData->X2ApicEnable = TRUE; ReleaseSpinLock(&PeiCpuMpData->MpLock); } // // Sync BSP's Mtrr table to all wakeup APs and load microcode on APs. // MtrrSetAllMtrrs (&PeiCpuMpData->MtrrTable); MicrocodeDetect (); } else { // // Execute AP function if AP is not disabled // GetProcessorNumber (PeiCpuMpData, &ProcessorNumber); if ((PeiCpuMpData->CpuData[ProcessorNumber].State != CpuStateDisabled) && (PeiCpuMpData->ApFunction != 0)) { PeiCpuMpData->CpuData[ProcessorNumber].State = CpuStateBusy; Procedure = (EFI_AP_PROCEDURE)(UINTN)PeiCpuMpData->ApFunction; Procedure ((VOID *)(UINTN)PeiCpuMpData->ApFunctionArgument); PeiCpuMpData->CpuData[ProcessorNumber].State = CpuStateIdle; } } // // AP finished executing C code // InterlockedIncrement ((UINT32 *)&PeiCpuMpData->FinishedCount); AsmCliHltLoop (); }
void ProfilerViewer_Imp::DrawProfiledInfoSection(int top) { DrawTextSprite( Vector2DF(COLUMN_HEADER_OFFSET, top), SECTION_HEADER_COLOR, ToAString("Profiled Information")); DrawTextSprite( Vector2DF(COLUMN1_OFFSET, top + ROW_HEIGHT), HEADER_COLOR, ToAString("ID")); DrawTextSprite( Vector2DF(COLUMN2_OFFSET, top + ROW_HEIGHT), HEADER_COLOR, ToAString("Time(ns)")); DrawTextSprite( Vector2DF(COLUMN3_OFFSET, top + ROW_HEIGHT), HEADER_COLOR, ToAString("Processor")); int bodyTop = top + ROW_HEIGHT * 2; int index = 0; for (auto& profile : m_profiler->GetProfiles()) { auto perf = profile->GetLastLog(); if (perf == nullptr) { continue; } int time = perf->GetEndTime() - perf->GetStartTime(); DrawTextSprite( Vector2DF(COLUMN1_OFFSET, bodyTop + index * ROW_HEIGHT), CONTENT_COLOR, ToAString(to_string(profile->GetID()).c_str())); DrawTextSprite( Vector2DF(COLUMN2_OFFSET, bodyTop + index * ROW_HEIGHT), CONTENT_COLOR, ToAString(to_string(time).c_str())); DrawTextSprite( Vector2DF(COLUMN3_OFFSET, bodyTop + index * ROW_HEIGHT), CONTENT_COLOR, ToAString(to_string(perf->GetProcessorNumber()).c_str())); ++index; } DrawSprite(RectF(0, top, m_windowSize.X, (index + 2)*ROW_HEIGHT + SECTION_SPAN), Color(0, 64, 64, 128), 0); }
/** This function will be called from AP reset code if BSP uses WakeUpAP. @param ExchangeInfo Pointer to the MP exchange info buffer @param NumApsExecuting Number of current executing AP **/ VOID EFIAPI ApCFunction ( IN MP_CPU_EXCHANGE_INFO *ExchangeInfo, IN UINTN NumApsExecuting ) { PEI_CPU_MP_DATA *PeiCpuMpData; UINTN ProcessorNumber; EFI_AP_PROCEDURE Procedure; UINTN BistData; volatile UINT32 *ApStartupSignalBuffer; PeiCpuMpData = ExchangeInfo->PeiCpuMpData; while (TRUE) { if (PeiCpuMpData->InitFlag) { ProcessorNumber = NumApsExecuting; // // Sync BSP's Control registers to APs // RestoreVolatileRegisters (&PeiCpuMpData->CpuData[0].VolatileRegisters, FALSE); // // This is first time AP wakeup, get BIST information from AP stack // BistData = *(UINTN *) (PeiCpuMpData->Buffer + ProcessorNumber * PeiCpuMpData->CpuApStackSize - sizeof (UINTN)); PeiCpuMpData->CpuData[ProcessorNumber].Health.Uint32 = (UINT32) BistData; PeiCpuMpData->CpuData[ProcessorNumber].ApicId = GetInitialApicId (); if (PeiCpuMpData->CpuData[ProcessorNumber].ApicId >= 0xFF) { // // Set x2APIC mode if there are any logical processor reporting // an APIC ID of 255 or greater. // AcquireSpinLock(&PeiCpuMpData->MpLock); PeiCpuMpData->X2ApicEnable = TRUE; ReleaseSpinLock(&PeiCpuMpData->MpLock); } // // Sync BSP's Mtrr table to all wakeup APs and load microcode on APs. // MtrrSetAllMtrrs (&PeiCpuMpData->MtrrTable); MicrocodeDetect (); PeiCpuMpData->CpuData[ProcessorNumber].State = CpuStateIdle; } else { // // Execute AP function if AP is not disabled // GetProcessorNumber (PeiCpuMpData, &ProcessorNumber); if (PeiCpuMpData->ApLoopMode == ApInHltLoop) { // // Restore AP's volatile registers saved // RestoreVolatileRegisters (&PeiCpuMpData->CpuData[ProcessorNumber].VolatileRegisters, TRUE); } if ((PeiCpuMpData->CpuData[ProcessorNumber].State != CpuStateDisabled) && (PeiCpuMpData->ApFunction != 0)) { PeiCpuMpData->CpuData[ProcessorNumber].State = CpuStateBusy; Procedure = (EFI_AP_PROCEDURE)(UINTN)PeiCpuMpData->ApFunction; // // Invoke AP function here // Procedure ((VOID *)(UINTN)PeiCpuMpData->ApFunctionArgument); // // Re-get the processor number due to BSP/AP maybe exchange in AP function // GetProcessorNumber (PeiCpuMpData, &ProcessorNumber); PeiCpuMpData->CpuData[ProcessorNumber].State = CpuStateIdle; } } // // AP finished executing C code // InterlockedIncrement ((UINT32 *)&PeiCpuMpData->FinishedCount); // // Place AP is specified loop mode // if (PeiCpuMpData->ApLoopMode == ApInHltLoop) { // // Save AP volatile registers // SaveVolatileRegisters (&PeiCpuMpData->CpuData[ProcessorNumber].VolatileRegisters); // // Place AP in Hlt-loop // while (TRUE) { DisableInterrupts (); CpuSleep (); CpuPause (); } } ApStartupSignalBuffer = PeiCpuMpData->CpuData[ProcessorNumber].StartupApSignal; // // Clear AP start-up signal // *ApStartupSignalBuffer = 0; while (TRUE) { DisableInterrupts (); if (PeiCpuMpData->ApLoopMode == ApInMwaitLoop) { // // Place AP in Mwait-loop // AsmMonitor ((UINTN)ApStartupSignalBuffer, 0, 0); if (*ApStartupSignalBuffer != WAKEUP_AP_SIGNAL) { // // If AP start-up signal is not set, place AP into // the maximum C-state // AsmMwait (PeiCpuMpData->ApTargetCState << 4, 0); } } else if (PeiCpuMpData->ApLoopMode == ApInRunLoop) { // // Place AP in Run-loop // CpuPause (); } else { ASSERT (FALSE); } // // If AP start-up signal is written, AP is waken up // otherwise place AP in loop again // if (*ApStartupSignalBuffer == WAKEUP_AP_SIGNAL) { break; } } } }
int ProcessWatch::GetProcessCpuUsage(int pid) { static int processor_count_ = -1; static __int64 last_time_ = 0; static __int64 last_system_time_ = 0; FILETIME now; FILETIME creation_time; FILETIME exit_time; FILETIME kernel_time; FILETIME user_time; __int64 system_time; __int64 time; // __int64 system_time_delta; // __int64 time_delta; double cpu = -1; if(processor_count_ == -1) { processor_count_ = GetProcessorNumber(); } GetSystemTimeAsFileTime(&now); HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION/*PROCESS_ALL_ACCESS*/, false, pid); if (!hProcess) { return -1; } if (!GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time)) { return -1; } system_time = (FileTimeToInt64(kernel_time) + FileTimeToInt64(user_time)) / processor_count_; time = FileTimeToInt64(now); last_system_time_ = system_time; last_time_ = time; CloseHandle( hProcess ); Sleep(30); hProcess = OpenProcess(PROCESS_QUERY_INFORMATION/*PROCESS_ALL_ACCESS*/, false, pid); if (!hProcess) { return -1; } if (!GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time)) { return -1; } GetSystemTimeAsFileTime(&now); system_time = (FileTimeToInt64(kernel_time) + FileTimeToInt64(user_time)) / processor_count_; time = FileTimeToInt64(now); CloseHandle( hProcess ); cpu = ((double)(system_time - last_system_time_) / (double)(time - last_time_)) * 100; return (int)cpu; }