void * thread_dlopen_func (void *arg) { double number = 0.2; double calculatedValue = 0; while (loop1) { void *handle = dlopen("my_dll.so", RTLD_LAZY); if (handle) { DLL_FUNC fptr = (DLL_FUNC)dlsym(handle, "my_dll_sin"); calculatedValue += (*fptr)(number); //sleep(1); dlclose(handle); } else { fprintf(stderr, "error opening my_dll.so, thread %d\n", GetTid()); exit(-1); } number += 0.01; } return 0; }
void replace_jemalloc_stats(jemalloc_stats_t* aStats) { AutoLock lock(sLock); sFuncs->jemalloc_stats(aStats); FdPrintf(sFd, "%zu %zu jemalloc_stats()\n", GetPid(), GetTid()); }
// Check if the O/S supports real-time scheduling. // static bool CheckSupported() { pid_t tid = GetTid(); if (tid == -1) { std::cout << "Platform doesn't support SYS_gettid.\n"; return false; } struct sched_param param; param.sched_priority = 2; if (sched_setscheduler(tid, SCHED_RR, ¶m) != 0) { std::cout << "Cannot set SCHED_RR priority level 2. Do one of the following to fix:\n"; std::cout << " 1) Run this test manually as root.\n"; std::cout << " 2) Edit the \"/etc/security/limits.conf\" file and raise the \"rtprio\"\n"; std::cout << " limit to at least 2.\n"; return false; } cpu_set_t cpus; CPU_ZERO(&cpus); CPU_SET(0, &cpus); if (sched_setaffinity(tid, sizeof(cpus), &cpus) != 0) { std::cout << "Cannot set CPU affinity to CPU 0.\n"; return false; } return true; }
void CCoFAHost::OnActivityNotification(const tstring description) { TRY_CATCH PTCHAR str = _tcsdup(description.c_str()); if (FALSE == PostThreadMessage(GetTid(),WM_USER, reinterpret_cast<WPARAM>(str), 0)) free(str); CATCH_LOG() }
void replace_free(void* aPtr) { AutoLock lock(sLock); if (aPtr) { FdPrintf(sFd, "%zu %zu free(%p)\n", GetPid(), GetTid(), aPtr); } sFuncs->free(aPtr); }
void* replace_valloc(size_t aSize) { AutoLock lock(sLock); void* ptr = sFuncs->valloc(aSize); if (ptr) { FdPrintf(sFd, "%zu %zu valloc(%zu)=%p\n", GetPid(), GetTid(), aSize, ptr); } return ptr; }
int main() { pthread_mutex_init(&Lock, 0); pthread_mutex_init(&ErrorLock, 0); pthread_cond_init(&WorkerInitialized, 0); // Make sure that the system supports real-time scheduling. The test // passes if it is not supported, which avoids failures during automated // testing. // if (!CheckSupported()) { std::cout << "Exiting with success, but could not perform test\n"; TellPinNotSupported(); return 0; } // Figure out the priority levels to use for "high" and "low". // PriorityLow = sched_get_priority_min(SCHED_RR); if (PriorityLow < 1) PriorityLow = 1; PriorityHigh = PriorityLow + 1; if (PriorityHigh > sched_get_priority_max(SCHED_RR)) { std::cerr << "Not enough priority levels" << std::endl; return 1; } // The main thread runs at high priority to ensure that it can wait for // the other threads to complete. // if (!SetPriority(GetTid(), PRIORITY_HIGH)) return 1; // Create the worker and scheduler threads. // pthread_t workerThread; if (pthread_create(&workerThread, 0, Worker, 0) != 0) { std::cerr << "Unable to create worker thread" << std::endl; return 1; } pthread_t schedulerThread; if (pthread_create(&schedulerThread, 0, Scheduler, 0) != 0) { std::cerr << "Unable to create scheduler thread" << std::endl; return 1; } pthread_join(workerThread, 0); pthread_join(schedulerThread, 0); return (IsError) ? 1 : 0; }
void* replace_memalign(size_t aAlignment, size_t aSize) { AutoLock lock(sLock); void* ptr = sFuncs->memalign(aAlignment, aSize); if (ptr) { FdPrintf(sFd, "%zu %zu memalign(%zu,%zu)=%p\n", GetPid(), GetTid(), aAlignment, aSize, ptr); } return ptr; }
void* replace_realloc(void* aPtr, size_t aSize) { AutoLock lock(sLock); void* new_ptr = sFuncs->realloc(aPtr, aSize); if (new_ptr || !aSize) { FdPrintf(sFd, "%zu %zu realloc(%p,%zu)=%p\n", GetPid(), GetTid(), aPtr, aSize, new_ptr); } return new_ptr; }
int replace_posix_memalign(void** aPtr, size_t aAlignment, size_t aSize) { AutoLock lock(sLock); int ret = sFuncs->posix_memalign(aPtr, aAlignment, aSize); if (ret == 0) { FdPrintf(sFd, "%zu %zu posix_memalign(%zu,%zu)=%p\n", GetPid(), GetTid(), aAlignment, aSize, *aPtr); } return ret; }
static void *Scheduler(void *) { if (!SetPriority(GetTid(), PRIORITY_HIGH) || !SetAffinity(GetTid())) IsError = true; // Wait for the worker to initialize itself. // pthread_mutex_lock(&Lock); while (!WorkerIsInitialized) pthread_cond_wait(&WorkerInitialized, &Lock); pthread_mutex_unlock(&Lock); // This loop tries to lower the priority of the worker while it holds // the PIN_LOCK. // volatile VOIDFUNPTR doGetLockWithPin = DoGetLockWithPin; for (unsigned long i = 0; i < NUM_SCHEDULES && !IsError; i++) { // Lower the priority, then try to acquire the PIN_LOCK. We want // to attempt to acuire the lock here while the worker has the // lock and is running at low priority. // if (!SetPriority(WorkerTid, PRIORITY_LOW)) IsError = true; doGetLockWithPin(); if ((i % (NUM_SCHEDULES / 10)) == 0) std::cout << "Iterations: " << std::dec << i << std::endl; // Raise the worker priority and yield the processor to it. Let the // worker start running again before the next attempt. // if (!SetPriority(WorkerTid, PRIORITY_HIGH)) IsError = true; sched_yield(); } Done = true; return 0; }
CCoFAHost::~CCoFAHost() { TRY_CATCH Log.Add(_MESSAGE_,_T("CCoFAHost::~CCoFAHost()")); PostThreadMessage(GetTid(), WM_QUIT, 0, 0); m_brokerEvents.Revoke(); // if( m_server.get() ) // m_server->Stop(); CATCH_LOG() }
int main() { PutString("\n-----------------------------------------\n"); PutString("Lancement du test testFin : \n"); PutString("Teste la terminaison des threads sans appel explicite a UserThreadExit.\n"); PutString("-----------------------------------------\n"); PutString("Main, Tid : \n"); PutInt(GetTid()); PutString("\n"); UserThreadCreate(f, 0); UserThreadCreate(g, 0); UserThreadCreate(fin, 0); return 0; }
// This is done under the Pin client lock so there is no race on the global variables. VOID onThreadAttach(VOID *sigset, VOID *v) { pid_t tid = GetTid(); // The master thread's tid is the same as the pid of the entire process. if (tid == masterPid) { // This is the master thread which should be first. fprintf(generated, "3\n"); } else { // This is the second thread which should be last, therefore close the file and exit. // If this happens before the master thread then there is a problem and the test should fail. // This failure will be discovered while comparing the two files. fprintf(generated, "4\n"); fclose(generated); PIN_ExitProcess(0); } }
static void *Worker(void *) { WorkerTid = GetTid(); if (!SetPriority(WorkerTid, PRIORITY_HIGH) || !SetAffinity(WorkerTid)) IsError = true; pthread_mutex_lock(&Lock); WorkerIsInitialized = true; pthread_cond_signal(&WorkerInitialized); pthread_mutex_unlock(&Lock); // Execute the work loop via a volatile pointer to prevent the compiler // from inlining. The Pin tool instruments DoWorkInstrumentedWithPin(), // so we don't want its body to be inlined. // volatile VOIDFUNPTR doWork = DoWorkInstrumentedWithPin; doWork(); return 0; }
void fin(void* arg) { PutString("fin, Tid : \n"); PutInt(GetTid()); PutString("\n"); }
void g(void* arg) { PutString("g, Tid : \n"); PutInt(GetTid()); PutString("\n"); }
void Print(const string& str) { GetLock(&printLock); fprintf(stderr, "APP: <%d> %s\n", GetTid(), str.c_str()); ReleaseLock(&printLock); }
void ErrorExit(Results res) { GetLock(&printLock); fprintf(stderr, "APP ERROR <%d>: %s\n", GetTid(), errorStrings[res].c_str()); ReleaseLock(&printLock); exit(res); }
void CLog::Log(const std::string &message, bool console) { time_t now; time(&now); struct tm *tm = localtime(&now); char header[64] = { 0 }; snprintf(header, 63, "%02d:%02d:%02d[%u.%u] ", tm->tm_hour, tm->tm_min, tm->tm_sec, GetPid(), GetTid()); std::cout << header << message << std::endl; if (console) std::cerr << header << message << std::endl; }
STDMETHODIMP CCoFAHost::HandleRequest(BSTR dstUserId,ULONG dstSvcId,BSTR srcUserId,ULONG srcSvcId,ULONG rId,ULONG rType,ULONG param,BSTR params) { TRY_CATCH_COM Log.Add(_MESSAGE_,_T("CCoFAHost::HandleRequest()")); USES_CONVERSION; switch(rType) { case BRT_PING: { Log.Add(_MESSAGE_,_T("CCoFAHost::HandleRequest(BRT_PING)")); if(!m_brokerEvents.m_dwCookie) throw MCException("_IBrokerClientEvents has not set. Call IBrokerClient::Init() method at first"); HRESULT result; CComPtr<_IBrokerClientEvents> brokerEvents; if(S_OK!=(result=m_brokerEvents.CopyTo(&brokerEvents))) throw CExceptionBase(__LINE__,_T(__FILE__),_T(__DATE__),tstring(_T("IBrokerClientEvents obtaining failed in Broker")),result); if(!brokerEvents.p) throw MCException("_IBrokerClientEvents has not marshaled"); brokerEvents->RequestSent(srcUserId,srcSvcId,srcUserId,dstSvcId,rId,rType|BRT_RESPONSE,param,params);//response on ping } break; case BRT_PING|BRT_RESPONSE: { Log.Add(_MESSAGE_,_T("CCoFAHost::HandleRequest(BRT_PING|BRT_RESPONSE)")); } break; case BRT_GET_SERVICE_INFO|BRT_RESPONSE: Log.Add(_MESSAGE_,_T("CCoFAHost::HandleRequest(BRT_GET_SERVICE_INFO|BRT_RESPONSE)")); if (NULL != m_server.get()) { tstring sid = _T("unknown"); tstring expertName = _T("unknown"); tstring customerName = _T("unknown"); USES_CONVERSION; std::vector<tstring> info = tokenize(_OLE2T(params), _T(";")); //params="[userId];;[UserName];;[remoteUserId];;[remoteUserName];;[sId]" if (info.size() != 5) Log.Add(_ERROR_,_T("Unknown BRT_GET_SERVICE_INFO response string format %s"),W2T(params)); else { sid = info[4]; expertName = Format(_T("%s[%s]"),info[3].c_str(),info[2].c_str()); customerName = Format(_T("%s[%s]"),info[1].c_str(),info[0].c_str()); } m_server->InitTransferLogging(sid, customerName, expertName); } break; case BRT_NWL_DISCONNECTED: case BRT_SERVICE_DESTROYED: Log.Add(_MESSAGE_,_T("CCoFAHost::HandleRequest(BRT_SERVICE_DESTROYED)")); TRY_CATCH HRESULT result; if(!m_brokerEvents.m_dwCookie) throw MCException("_IBrokerClientEvents has not set. Call IBrokerClient::Init() method at first"); CComPtr<_IBrokerClientEvents> brokerEvents; if(S_OK!=(result=m_brokerEvents.CopyTo(&brokerEvents))) throw CExceptionBase(__LINE__,_T(__FILE__),_T(__DATE__),tstring(_T("IBrokerClientEvents obtaining failed in Broker")),result); /// Sending request to indicate - service is off if(NULL != brokerEvents.p) brokerEvents->RequestSent(CComBSTR(BUSERIDPDV_LOCAL),BSVCIDPDV_JS,CComBSTR(BUSERIDPDV_AUTOSET),BSVCIDPDV_AUTOSET,0/*rid*/,BRT_SRV_STATE_CHANGED,ESM_SERVICE_STOPPED/*off*/,CComBSTR(_T("File manager"))); //TODO: treat name carefully CATCH_LOG() break; case BRT_STOP_SERVICE: Log.Add(_MESSAGE_,_T("CCoFAHost::HandleRequest(BRT_STOP_SERVICE|BRT_NWL_DISCONNECTED)")); { TRY_CATCH //m_server->Stop(); // m_server->Stop(); // m_stream->CancelReceiveOperation(); // m_stream.reset(); PostThreadMessage(GetTid(), WM_QUIT, 0, 0); Stop(false,3000); HRESULT result; if(!m_brokerEvents.m_dwCookie) throw MCException("_IBrokerClientEvents has not set. Call IBrokerClient::Init() method at first"); CComPtr<_IBrokerClientEvents> brokerEvents; if(S_OK!=(result=m_brokerEvents.CopyTo(&brokerEvents))) throw CExceptionBase(__LINE__,_T(__FILE__),_T(__DATE__),tstring(_T("IBrokerClientEvents obtaining failed in Broker")),result); /// Resending request to client side if(NULL != brokerEvents.p) brokerEvents->RequestSent(CComBSTR(BUSERIDPDV_AUTOSET),BSVCIDPDV_AUTOSET,CComBSTR(BUSERIDPDV_AUTOSET),BSVCIDPDV_AUTOSET,0/*rid*/,BRT_STOP_SERVICE,param,params); CATCH_LOG() } break; default: throw CExceptionBase(__LINE__,_T(__FILE__),_T(__DATE__),_T("Request type is unknown. HandleRequest(%s,0x%x,%s,0x%x,0x%x,0x%x,0x%x,%s)"),OLE2T(dstUserId),dstSvcId,OLE2T(srcUserId),srcSvcId,rId,rType,param,OLE2T(params)); } CATCH_LOG_COM }