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; }
int PrintMemUsage(FILE *fp) { static int u[5]; GetMemUsage(u); fprintf(fp,"MEM: Size %d Peak: %d RSS: %d Data: %d Stk: %d\n", u[0],u[1],u[2],u[3],u[4]); fflush(fp); return(0); }
void WINAPI CpuLimitThread(LPVOID) { LARGE_INTEGER liOldIdleTime={0},liOldSystemTime={0}; SYSTEM_PERFORMANCE_INFORMATION spi; SYSTEM_TIME_INFORMATION sti; if ((!NtQuerySystemInformation(SystemPerformanceInformation,&spi,sizeof(spi),NULL)) && (!NtQuerySystemInformation(SystemTimeOfDayInformation,&sti,sizeof(sti),NULL))) { if (liOldIdleTime.QuadPart) { double dbIdleTime=Li2Double(spi.IdleTime)-Li2Double(liOldIdleTime), dbSystemTime=Li2Double(sti.nKeSystemTime)-Li2Double(liOldSystemTime); dbIdleTime=dbIdleTime/dbSystemTime; dbIdleTime=100.0-dbIdleTime*100.0*(double)dwProcessorsCount+0.5; dwIdleTime=(DWORD)dbIdleTime; if ((dwIdleTime >= MAX_CPU_TIME_LIMIT) && (GetUserIdleTime() < MAX_IDLE_TIME)) { if (bCpuSleep) { bCpuSleep=false; dwPeriodBegin=0; } else { DWORD dwPeriodEnd=GetTickCount(); if (dwPeriodEnd-dwPeriodBegin >= MAX_CPU_LOAD_DURATION) bCpuSleep=true; dwPeriodBegin=dwPeriodEnd; } } else { bCpuSleep=false; dwPeriodBegin=0; } } liOldIdleTime.QuadPart=spi.IdleTime.QuadPart; liOldSystemTime.QuadPart=sti.nKeSystemTime.QuadPart; } dwMemUsage=GetMemUsage(); Sleep(1000); return; }
NS_IMETHODIMP nsLoadCollector::OnStateChange(nsIWebProgress *webProgress, nsIRequest *request, PRUint32 flags, nsresult status) { NS_ASSERTION(flags & STATE_IS_DOCUMENT, "incorrect state change notification"); #ifdef PR_LOGGING if (MS_LOG_ENABLED()) { nsCString name; request->GetName(name); MS_LOG(("LoadCollector: progress = %p, request = %p [%s], flags = %x, status = %x", webProgress, request, name.get(), flags, status)); } #endif nsCOMPtr<nsIChannel> channel = do_QueryInterface(request); if (!channel) { // We don't care about non-channel requests return NS_OK; } nsresult rv; if (flags & STATE_START) { RequestEntry entry; NS_ASSERTION(!mRequestMap.Get(request, &entry), "duplicate STATE_START"); nsCOMPtr<nsIDocShell> docShell = do_QueryInterface(webProgress); nsCOMPtr<nsIDOMWindow> window = do_GetInterface(docShell); if (!window) { // We don't really care about windowless loads return NS_OK; } rv = nsMetricsUtils::NewPropertyBag(getter_AddRefs(entry.properties)); NS_ENSURE_SUCCESS(rv, rv); nsIWritablePropertyBag2 *props = entry.properties; rv = props->SetPropertyAsUint32(NS_LITERAL_STRING("window"), nsMetricsService::GetWindowID(window)); NS_ENSURE_SUCCESS(rv, rv); if (flags & STATE_RESTORING) { rv = props->SetPropertyAsBool(NS_LITERAL_STRING("bfCacheHit"), PR_TRUE); NS_ENSURE_SUCCESS(rv, rv); } nsString origin; PRUint32 loadType; docShell->GetLoadType(&loadType); switch (loadType) { case LOAD_NORMAL: case LOAD_NORMAL_REPLACE: case LOAD_BYPASS_HISTORY: origin = NS_LITERAL_STRING("typed"); break; case LOAD_NORMAL_EXTERNAL: origin = NS_LITERAL_STRING("external"); break; case LOAD_HISTORY: origin = NS_LITERAL_STRING("session-history"); break; case LOAD_RELOAD_NORMAL: case LOAD_RELOAD_BYPASS_CACHE: case LOAD_RELOAD_BYPASS_PROXY: case LOAD_RELOAD_BYPASS_PROXY_AND_CACHE: case LOAD_RELOAD_CHARSET_CHANGE: origin = NS_LITERAL_STRING("reload"); break; case LOAD_LINK: origin = NS_LITERAL_STRING("link"); break; case LOAD_REFRESH: origin = NS_LITERAL_STRING("refresh"); break; default: break; } if (!origin.IsEmpty()) { rv = props->SetPropertyAsAString(NS_LITERAL_STRING("origin"), origin); NS_ENSURE_SUCCESS(rv, rv); } entry.startTime = PR_Now(); NS_ENSURE_TRUE(mRequestMap.Put(request, entry), NS_ERROR_OUT_OF_MEMORY); } else if (flags & STATE_STOP) { RequestEntry entry; if (mRequestMap.Get(request, &entry)) { mRequestMap.Remove(request); // Log a <document action="load"> event nsIWritablePropertyBag2 *props = entry.properties; rv = props->SetPropertyAsACString(NS_LITERAL_STRING("action"), NS_LITERAL_CSTRING("load")); NS_ENSURE_SUCCESS(rv, rv); // Compute the load time now that we have the end time. PRInt64 loadTime = (PR_Now() - entry.startTime) / PR_USEC_PER_MSEC; rv = props->SetPropertyAsUint64(NS_LITERAL_STRING("loadtime"), loadTime); NS_ENSURE_SUCCESS(rv, rv); MemUsage mu; if (GetMemUsage(&mu)) { rv = props->SetPropertyAsUint64(NS_LITERAL_STRING("memtotal"), mu.total); NS_ENSURE_SUCCESS(rv, rv); rv = props->SetPropertyAsUint64(NS_LITERAL_STRING("memresident"), mu.resident); NS_ENSURE_SUCCESS(rv, rv); } // Look up the document id, or assign a new one nsCOMPtr<nsIDocShell> docShell = do_QueryInterface(webProgress); nsCOMPtr<nsIDOMWindow> window = do_GetInterface(docShell); if (!window) { MS_LOG(("Couldn't get window")); return NS_ERROR_UNEXPECTED; } nsCOMPtr<nsIDOMDocument> domDoc; window->GetDocument(getter_AddRefs(domDoc)); nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc); if (!doc) { MS_LOG(("Couldn't get document")); return NS_ERROR_UNEXPECTED; } nsCOMPtr<nsIDocShellTreeItem> item = do_QueryInterface(docShell); NS_ENSURE_STATE(item); PRBool subframe = nsMetricsUtils::IsSubframe(item); if (subframe) { rv = props->SetPropertyAsBool(NS_LITERAL_STRING("subframe"), PR_TRUE); NS_ENSURE_SUCCESS(rv, rv); } nsMetricsService *ms = nsMetricsService::get(); DocumentEntry docEntry; if (!mDocumentMap.Get(doc, &docEntry)) { docEntry.docID = mNextDocID++; docEntry.subframe = subframe; if (!ms->WindowMap().Get(window, &docEntry.windowID)) { MS_LOG(("Window not in the window map")); return NS_ERROR_UNEXPECTED; } NS_ENSURE_TRUE(mDocumentMap.Put(doc, docEntry), NS_ERROR_OUT_OF_MEMORY); } doc->AddObserver(this); // set up to log the document destroy rv = props->SetPropertyAsUint32(NS_LITERAL_STRING("docid"), docEntry.docID); NS_ENSURE_SUCCESS(rv, rv); // If this was a load of a chrome document, hash the URL of the document // so it can be identified. nsCOMPtr<nsIChannel> channel = do_QueryInterface(request); if (channel) { nsCOMPtr<nsIURI> uri; channel->GetURI(getter_AddRefs(uri)); if (uri) { PRBool isChrome = PR_FALSE; uri->SchemeIs("chrome", &isChrome); if (isChrome) { nsCString spec; uri->GetSpec(spec); nsCString hashedSpec; rv = ms->HashUTF8(spec, hashedSpec); NS_ENSURE_SUCCESS(rv, rv); rv = props->SetPropertyAsACString(NS_LITERAL_STRING("urlhash"), hashedSpec); NS_ENSURE_SUCCESS(rv, rv); } } } rv = ms->LogEvent(NS_LITERAL_STRING("document"), props); NS_ENSURE_SUCCESS(rv, rv); } else { NS_WARNING("STATE_STOP without STATE_START"); } } return NS_OK; }
Bool HawkProfiler::RunLoop() { UInt32 iCalcTime = HawkOSOperator::GetTickCount(); while(m_bRunning) { UInt32 iCurTime = HawkOSOperator::GetTickCount(); //定期计算CPU和内存使用情况 if (iCurTime - iCalcTime >= 1000) { GetCpuUsage(); GetMemUsage(); iCalcTime = iCurTime; } if (!m_sSocket.UpdateEvent(HEVENT_READ, DEFAULT_SLEEP)) continue; m_pRecvBuf->Clear(); Size_t iRecv = (Size_t)m_pRecvBuf->Capacity(); SocketAddr sAddr; if(!m_sSocket.ReceiveFrom(m_pRecvBuf->Begin(), iRecv, sAddr)) continue; m_pRecvBuf->Resize((UInt32)iRecv); Protocol* pProto = 0; try { pProto = P_ProtocolManager->Decode(*m_pRecvBuf); if (!pProto) continue; } catch (HawkException& rhsExcep) { HawkFmtPrint("Exception: %s", rhsExcep.GetMsg().c_str()); continue; } ProtoType eType = pProto->GetType(); if(eType == SysProtocol::SYS_PROF_REQ) { SysProtocol::Sys_ProfInfo sInfo; sInfo.m_iTimeStamp = HawkOSOperator::GetTickCount(); sInfo.m_iCpuCount = m_iCpuCount; sInfo.m_iTotalMem = m_iTotalMem; sInfo.m_iCpuUsage = m_iCpuUsage; sInfo.m_iMemUsage = m_iMemUsage; sInfo.m_iConnect = m_iConnect; sInfo.m_iRecvProto = m_iRecvProto; sInfo.m_iRecvSize = m_iRecvSize; sInfo.m_iSendProto = m_iSendProto; sInfo.m_iSendSize = m_iSendSize; m_pRecvBuf->Clear(); sInfo.Encode(*m_pRecvBuf); Size_t iSize = (Size_t)m_pRecvBuf->Size(); m_sSocket.SendTo(m_pRecvBuf->Begin(), iSize, sAddr); } P_ProtocolManager->ReleaseProto(pProto); } return true; }