unsigned long GetConfigedThreadCount() { if (0 == CPU_COUNT) { return GetCPUCount(); } else return CPU_COUNT; }
int main(){ pthread_t thread1,thread2; if(GetCPUCount()<2)return EXIT_FAILURE; pthread_create(&thread1, NULL, thread_func_1, NULL); pthread_create(&thread2, NULL, thread_func_2, NULL); pthread_join(thread1,NULL); pthread_join(thread2,NULL); return EXIT_SUCCESS; }
float UnixMetric::GetCPUUsageUnix(){ uint32 lastupdate = GetLastUpdate(); uint32 lastsecusage = GetLastSecUsage(); uint32 lastusecusage = GetLastUSecUsage(); float usage = 0.0f; uint32 secusage = 0; uint32 usecusage = 0; struct rusage susage; uint32 nocpus = GetCPUCount(); uint32 unixtime = time( NULL ); if(getrusage(RUSAGE_SELF, &susage) == 0){ secusage = susage.ru_utime.tv_sec + susage.ru_stime.tv_sec; usecusage = susage.ru_utime.tv_usec + susage.ru_stime.tv_usec; if(usecusage / 1000000 >= 1){ secusage = secusage + (usecusage / 1000000); usecusage = usecusage - ( usecusage / 1000000 ) * 1000000; } } uint32 difft = (unixtime - lastupdate); if(difft != 0){ // when we sample the cpu usage below 10 minutes we can be more precise, since the usecs will be representable even in 32bit values! if( difft <= 3600 ){ usage = ( static_cast<float>( (secusage * 1000000 + usecusage) - (lastsecusage * 1000000 + lastusecusage) ) / ( difft * 1000000 ) ); }else usage = ( static_cast<float>( secusage - lastsecusage) / difft); }else{ // this case means we are polling the cpu usage data in realtime, which shouldn't ever happen, except on startup // since we can't divide by 0 // We don't care about the cpu usage of the starting time anyway so we can just report 0% usage = 0.0f; } usage *= 100.0f; /* I will leave this here for now, so next time if I tinker I don't have to rewrite it heh printf("secusage: %lu\n", secusage); printf("lastsecusage: %lu\n", lastsecusage); printf("usecusage: %lu\n", usecusage); printf("lastusecusage: %lu\n", lastusecusage); printf("difft: %lu\n", difft); printf("usec difference: %lu\n", ( (secusage * 1000000 + usecusage) - (lastsecusage * 1000000 + lastusecusage) ) ); printf("usage%%: %lf\n", usage); */ SetLastUpdate( unixtime ); SetLastSecUsage( secusage ); SetLastUSecUsage( usecusage ); return usage / nocpus; }
bool wxThread::SetConcurrency(size_t level) { wxASSERT_MSG( IsMain(), _T("should only be called from the main thread") ); // ok only for the default one if ( level == 0 ) return 0; // how many CPUs have we got? if ( GetCPUCount() == 1 ) { // don't bother with all this complicated stuff - on a single // processor system it doesn't make much sense anyhow return level == 1; } return TRUE ; }
static int GetNumberofCPUs (void) { unsigned logical ; unsigned cores ; unsigned physical ; SYSTEM_INFO sysinfo ; static int result = -1; // we cache the result, since this function is called on each thread startup if (result != -1) return result; GetSystemInfo (&sysinfo) ; result = sysinfo.dwNumberOfProcessors; if (GetCPUCount(&logical, &cores, &physical)) result = cores; return result; }
bool ThreadPool::init() { #ifdef JS_THREADSAFE_ION // Compute desired number of workers based on env var or # of CPUs. size_t numWorkers = 0; char *pathreads = getenv("PATHREADS"); if (pathreads != NULL) { numWorkers = strtol(pathreads, NULL, 10); } else { numWorkers = GetCPUCount() - 1; } if (numWorkers == 0) return false; // Allocate workers array and then start the worker threads. // Ensure that the field numWorkers_ always tracks the number of // *successfully initialized* workers. for (size_t workerId = 0; workerId < numWorkers; workerId++) { ThreadPoolWorker *worker = runtime_->new_<ThreadPoolWorker>(workerId, this); if (!worker->init()) { delete worker; return false; } if (!workers_.append(worker)) { delete worker; return false; } if (!worker->start()) { return false; } } #endif return true; }
void CTradeGatewayGtjaDlg::start() { // 启动缓存线程,用于清除缓存数据 g_CacheData.Start(); //文件日志 gFileLogManager::instance().start(); gFileLogManager::instance().LoadFieldFilter(); gFileLogManager::instance().LoadFuncFilter(); // 分布式日志 if (gConfigManager::instance().m_nLogMqEnable) { if(!gLogConnectPool::instance().CreateConnectPool()) { AfxMessageBox("创建分布式日志连接池失败"); return; } } // 分布式日志 if (gConfigManager::instance().m_nLogMqEnable) { gDistributedLogManager::instance().start(); gDistributedLogManager::instance().LoadFieldFilter(); gDistributedLogManager::instance().LoadFuncFilter(); } // tcp 4字节 if (gConfigManager::instance().m_nTcpEnable) { pTradeServerTcpOld = new TradeServer(MSG_TYPE_TCP_OLD); iospool_tcp_old = new IOServicePool(GetCPUCount()); pTcpOldServer = new TcpServer(*iospool_tcp_old, gConfigManager::instance().m_nTcpPort, pTradeServerTcpOld->recv_queue(), MSG_TYPE_TCP_OLD); pTradeServerTcpOld->start(); pTcpOldServer->start(); GetDlgItem(IDC_TCP_STATUS)->SetWindowText("运行状态:运行"); } // ssl pb if (gConfigManager::instance().m_nSslEnable) { pTradeServerSslOld = new TradeServer(MSG_TYPE_SSL_PB); iospool_ssl_old = new IOServicePool(GetCPUCount()); pSslOldServer = new SSLServer(*iospool_ssl_old, gConfigManager::instance().m_nSslPort, pTradeServerSslOld->recv_queue(), MSG_TYPE_SSL_PB); pTradeServerSslOld->start(); pSslOldServer->start(); GetDlgItem(IDC_SSL_STATUS)->SetWindowText("运行状态:运行"); } // tcp new if (gConfigManager::instance().m_nTcpNewEnable) { pTradeServerTcpNew = new TradeServer(MSG_TYPE_TCP_NEW); iospool_tcp_new = new IOServicePool(GetCPUCount()); pTcpNewServer = new TcpServer(*iospool_tcp_new, gConfigManager::instance().m_nTcpNewPort, pTradeServerTcpNew->recv_queue(), MSG_TYPE_TCP_NEW); pTradeServerTcpNew->start(); pTcpNewServer->start(); GetDlgItem(IDC_NEWTCP_STATUS)->SetWindowText("运行状态:运行"); } // ssl new if (gConfigManager::instance().m_nSslNewEnable) { pTradeServerSslNew = new TradeServer(MSG_TYPE_SSL_NEW); iospool_ssl_new = new IOServicePool(GetCPUCount()); pSslNewServer = new SSLServer(*iospool_ssl_new, gConfigManager::instance().m_nSslNewPort, pTradeServerSslNew->recv_queue(), MSG_TYPE_SSL_NEW); pTradeServerSslNew->start(); pSslNewServer->start(); GetDlgItem(IDC_NEWSSL_STATUS)->SetWindowText("运行状态:运行"); } gFileLog::instance().Log("交易网关启动"); std::string caption = "中软万维证券网关版本:"; caption += VERSION; caption +=" 网络层版本:"; caption += MY_BOOST_VERSION; caption +=" OpenSSL版本:"; caption += SSL_VERSION; SetWindowText(caption.c_str()); updateStatusDataFinish = true; statusDataThread.reset(new boost::thread(boost::bind(&CTradeGatewayGtjaDlg::UpdateStatusData, this))); }