static void test1(void) { RTTestISub("Three threads"); /* * Create the threads and let them block on the event multi semaphore. */ RTSEMEVENTMULTI hSem; RTTESTI_CHECK_RC_RETV(RTSemEventMultiCreate(&hSem), VINF_SUCCESS); RTTHREAD hThread2; RTTESTI_CHECK_RC_RETV(RTThreadCreate(&hThread2, test1Thread2, &hSem, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "test2"), VINF_SUCCESS); RTThreadSleep(100); RTTHREAD hThread1; RTTESTI_CHECK_RC_RETV(RTThreadCreate(&hThread1, test1Thread1, &hSem, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "test1"), VINF_SUCCESS); /* Force first thread (which has a timeout of 1 second) to timeout in the * first wait, and the second wait will succeed. */ RTTESTI_CHECK_RC(RTThreadSleep(1500), VINF_SUCCESS); RTTESTI_CHECK_RC(RTSemEventMultiSignal(hSem), VINF_SUCCESS); RTTESTI_CHECK_RC(RTThreadWait(hThread1, 5000, NULL), VINF_SUCCESS); RTTESTI_CHECK_RC(RTThreadWait(hThread2, 5000, NULL), VINF_SUCCESS); RTTESTI_CHECK_RC(RTSemEventMultiDestroy(hSem), VINF_SUCCESS); }
static int tstPDMACTestFileThread(PVM pVM, PPDMTHREAD pThread) { PPDMACTESTFILE pTestFile = (PPDMACTESTFILE)pThread->pvUser; int iWriteChance = 100; /* Chance to get a write task in percent. */ uint32_t cTasksStarted = 0; int rc = VINF_SUCCESS; if (pThread->enmState == PDMTHREADSTATE_INITIALIZING) return VINF_SUCCESS; while (pTestFile->fRunning) { unsigned iTaskCurr = 0; /* Fill all tasks */ while ( (pTestFile->cTasksActiveCurr < pTestFile->cTasksActiveMax) && (iTaskCurr < pTestFile->cTasksActiveMax)) { PPDMACTESTFILETASK pTask = &pTestFile->paTasks[iTaskCurr]; if (!pTask->fActive) { /* Read or write task? */ bool fWrite = tstPDMACTestIsTrue(iWriteChance); ASMAtomicIncU32(&pTestFile->cTasksActiveCurr); if (fWrite) rc = tstPDMACStressTestFileWrite(pTestFile, pTask); else rc = tstPDMACStressTestFileRead(pTestFile, pTask); if (rc != VINF_AIO_TASK_PENDING) tstPDMACStressTestFileTaskCompleted(pVM, pTask, pTestFile, rc); cTasksStarted++; } iTaskCurr++; } /* * Recalc write chance. The bigger the file the lower the chance to have a write. * The minimum chance is 33 percent. */ iWriteChance = 100 - (int)(((float)100.0 / pTestFile->cbFileMax) * (float)pTestFile->cbFileCurr); iWriteChance = RT_MAX(33, iWriteChance); /* Wait a random amount of time. (1ms - 100ms) */ RTThreadSleep(RTRandU32Ex(1, 100)); } /* Wait for the rest to complete. */ while (pTestFile->cTasksActiveCurr) RTThreadSleep(250); RTPrintf("Thread exiting: processed %u tasks\n", cTasksStarted); return rc; }
bool RTCALL VBoxOglIs3DAccelerationSupported(void) { if (RTEnvExist("VBOX_CROGL_FORCE_SUPPORTED")) { LogRel(("VBOX_CROGL_FORCE_SUPPORTED is specified, skipping 3D test, and treating as supported\n")); return true; } static char pszVBoxPath[RTPATH_MAX]; const char *papszArgs[4] = { NULL, "-test", "3D", NULL}; int rc; RTPROCESS Process; RTPROCSTATUS ProcStatus; uint64_t StartTS; rc = RTPathExecDir(pszVBoxPath, RTPATH_MAX); AssertRCReturn(rc, false); #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2) rc = RTPathAppend(pszVBoxPath, RTPATH_MAX, "VBoxTestOGL.exe"); #else rc = RTPathAppend(pszVBoxPath, RTPATH_MAX, "VBoxTestOGL"); #endif papszArgs[0] = pszVBoxPath; /* argv[0] */ AssertRCReturn(rc, false); rc = RTProcCreate(pszVBoxPath, papszArgs, RTENV_DEFAULT, 0, &Process); if (RT_FAILURE(rc)) return false; StartTS = RTTimeMilliTS(); while (1) { rc = RTProcWait(Process, RTPROCWAIT_FLAGS_NOBLOCK, &ProcStatus); if (rc != VERR_PROCESS_RUNNING) break; #ifndef DEBUG_misha if (RTTimeMilliTS() - StartTS > 30*1000 /* 30 sec */) { RTProcTerminate(Process); RTThreadSleep(100); RTProcWait(Process, RTPROCWAIT_FLAGS_NOBLOCK, &ProcStatus); return false; } #endif RTThreadSleep(100); } if (RT_SUCCESS(rc)) { if ((ProcStatus.enmReason==RTPROCEXITREASON_NORMAL) && (ProcStatus.iStatus==0)) { return true; } } return false; }
static void test1(void) { RTTestSub(g_hTest, "Interrupt RTThreadSleep"); RTTHREAD hThread; RTTESTI_CHECK_RC_RETV(RTThreadCreate(&hThread, test1Thread, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "test1"), VINF_SUCCESS); RTThreadSleep(500); RTThreadSleep(1500); /* fudge */ RTTESTI_CHECK_RC(RTThreadPoke(hThread), VINF_SUCCESS); RTTESTI_CHECK_RC(RTThreadWait(hThread, RT_INDEFINITE_WAIT, NULL), VINF_SUCCESS); }
bool is3DAccelerationSupported() { static char pszVBoxPath[RTPATH_MAX]; const char *papszArgs[4] = { NULL, "-test", "3D", NULL}; int rc; RTPROCESS Process; RTPROCSTATUS ProcStatus; uint64_t StartTS; rc = RTPathExecDir(pszVBoxPath, RTPATH_MAX); AssertRCReturn(rc, false); #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2) rc = RTPathAppend(pszVBoxPath, RTPATH_MAX, "VBoxTestOGL.exe"); #else rc = RTPathAppend(pszVBoxPath, RTPATH_MAX, "VBoxTestOGL"); #endif papszArgs[0] = pszVBoxPath; /* argv[0] */ AssertRCReturn(rc, false); rc = RTProcCreate(pszVBoxPath, papszArgs, RTENV_DEFAULT, 0, &Process); if (RT_FAILURE(rc)) return false; StartTS = RTTimeMilliTS(); while (1) { rc = RTProcWait(Process, RTPROCWAIT_FLAGS_NOBLOCK, &ProcStatus); if (rc != VERR_PROCESS_RUNNING) break; if (RTTimeMilliTS() - StartTS > 30*1000 /* 30 sec */) { RTProcTerminate(Process); RTThreadSleep(100); RTProcWait(Process, RTPROCWAIT_FLAGS_NOBLOCK, &ProcStatus); return false; } RTThreadSleep(100); } if (RT_SUCCESS(rc)) { if ((ProcStatus.enmReason==RTPROCEXITREASON_NORMAL) && (ProcStatus.iStatus==0)) { return true; } } return false; }
static RTEXITCODE testSessionConnectionChild(int argc, char **argv, RTTEST hTest) { do { RTThreadSleep(2000); /* Fudge */ RTLOCALIPCSESSION clientSession; RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionConnect(&clientSession, "tstRTLocalIpcSessionConnection", 0 /* Flags */), VINF_SUCCESS); RTThreadSleep(5000); /* Fudge */ RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionClose(clientSession), VINF_SUCCESS); } while (0); return !RTTestErrorCount(hTest) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE; }
/** * @interface_method_impl{TXSTRANSPORT,pfnInit} */ static DECLCALLBACK(int) txsTcpInit(void) { int rc = RTCritSectInit(&g_TcpCritSect); if (RT_SUCCESS(rc) && g_enmTcpMode != TXSTCPMODE_CLIENT) { rc = RTTcpServerCreateEx(g_szTcpBindAddr[0] ? g_szTcpBindAddr : NULL, g_uTcpBindPort, &g_pTcpServer); if (RT_FAILURE(rc)) { if (rc == VERR_NET_DOWN) { RTMsgInfo("RTTcpServerCreateEx(%s, %u,) failed: %Rrc, retrying for 20 seconds...\n", g_szTcpBindAddr[0] ? g_szTcpBindAddr : NULL, g_uTcpBindPort, rc); uint64_t StartMs = RTTimeMilliTS(); do { RTThreadSleep(1000); rc = RTTcpServerCreateEx(g_szTcpBindAddr[0] ? g_szTcpBindAddr : NULL, g_uTcpBindPort, &g_pTcpServer); } while ( rc == VERR_NET_DOWN && RTTimeMilliTS() - StartMs < 20000); if (RT_SUCCESS(rc)) RTMsgInfo("RTTcpServerCreateEx succceeded.\n"); } if (RT_FAILURE(rc)) { g_pTcpServer = NULL; RTCritSectDelete(&g_TcpCritSect); RTMsgError("RTTcpServerCreateEx(%s, %u,) failed: %Rrc\n", g_szTcpBindAddr[0] ? g_szTcpBindAddr : NULL, g_uTcpBindPort, rc); } } } return rc; }
RTDECL(int) RTPipeClose(RTPIPE hPipe) { RTPIPEINTERNAL *pThis = hPipe; if (pThis == NIL_RTPIPE) return VINF_SUCCESS; AssertPtrReturn(pThis, VERR_INVALID_PARAMETER); AssertReturn(pThis->u32Magic == RTPIPE_MAGIC, VERR_INVALID_HANDLE); /* * Do the cleanup. */ AssertReturn(ASMAtomicCmpXchgU32(&pThis->u32Magic, ~RTPIPE_MAGIC, RTPIPE_MAGIC), VERR_INVALID_HANDLE); int fd = pThis->fd; pThis->fd = -1; close(fd); if (ASMAtomicReadU32(&pThis->u32State) & RTPIPE_POSIX_USERS_MASK) { AssertFailed(); RTThreadSleep(1); } RTMemFree(pThis); return VINF_SUCCESS; }
static DECLCALLBACK(int) testSessionWaitThread(RTTHREAD hSelf, void *pvUser) { PLOCALIPCTHREADCTX pCtx = (PLOCALIPCTHREADCTX)pvUser; AssertPtr(pCtx); int rc; for (;;) { RTTestPrintf(pCtx->hTest, RTTESTLVL_INFO, "testSessionWaitThread: Listening for incoming connections ...\n"); RTLOCALIPCSESSION ipcSession; rc = RTLocalIpcServerListen(pCtx->hServer, &ipcSession); if (RT_SUCCESS(rc)) { RTTestPrintf(pCtx->hTest, RTTESTLVL_INFO, "testSessionWaitThread: Got new client connection, waiting a bit ...\n"); RTThreadSleep(2000); rc = RTLocalIpcSessionClose(ipcSession); } else { RTTestPrintf(pCtx->hTest, RTTESTLVL_INFO, "testSessionWaitThread: Listening ended with rc=%Rrc\n", rc); break; } } RTTestPrintf(pCtx->hTest, RTTESTLVL_INFO, "testSessionWaitThread: Ended with rc=%Rrc\n", rc); return rc; }
static int tstTrafficThreadCommon(uintptr_t iThread, bool fNS) { for (uint32_t iLoop = 0; RTTimeMilliTS() - g_u64StartMilliTS < g_cSecs*1000; iLoop++) { /* fudge */ if ((iLoop % 223) == 223) RTThreadYield(); else if ((iLoop % 16127) == 16127) RTThreadSleep(1); if (fNS) { RTTEST_CHECK_RC(g_hTest,RTSemXRoadsNSEnter(g_hXRoads), VINF_SUCCESS); ASMAtomicIncU32(&g_cNSCrossings); RTTEST_CHECK_RC(g_hTest,RTSemXRoadsNSLeave(g_hXRoads), VINF_SUCCESS); } else { RTTEST_CHECK_RC(g_hTest,RTSemXRoadsEWEnter(g_hXRoads), VINF_SUCCESS); ASMAtomicIncU32(&g_cEWCrossings); RTTEST_CHECK_RC(g_hTest,RTSemXRoadsEWLeave(g_hXRoads), VINF_SUCCESS); } } return VINF_SUCCESS; }
RTDECL(int) RTTraceSetDefaultBuf(RTTRACEBUF hTraceBuf) { /* Retain the new buffer. */ if (hTraceBuf != NIL_RTTRACEBUF) { uint32_t cRefs = RTTraceBufRetain(hTraceBuf); if (cRefs >= _1M) return VERR_INVALID_HANDLE; } RTTRACEBUF hOldTraceBuf; #ifdef IN_RC hOldTraceBuf = (RTTRACEBUF)ASMAtomicXchgPtr((void **)&g_hDefaultTraceBuf, hTraceBuf); #else ASMAtomicXchgHandle(&g_hDefaultTraceBuf, hTraceBuf, &hOldTraceBuf); #endif if ( hOldTraceBuf != NIL_RTTRACEBUF && hOldTraceBuf != hTraceBuf) { /* Race prevention kludge. */ #ifndef IN_RC RTThreadSleep(33); #endif RTTraceBufRelease(hOldTraceBuf); } return VINF_SUCCESS; }
STDMETHODIMP UIFrameBufferQGL::RequestResize (ULONG aScreenId, ULONG aPixelFormat, BYTE *aVRAM, ULONG aBitsPerPixel, ULONG aBytesPerLine, ULONG aWidth, ULONG aHeight, BOOL *aFinished) { aWidth = VBOXQGL_PROF_WIDTH; aHeight = VBOXQGL_PROF_HEIGHT; VBoxFrameBuffer::RequestResize (aScreenId, aPixelFormat, aVRAM, aBitsPerPixel, aBytesPerLine, aWidth, aHeight, aFinished); // if(aVRAM) { for(;;) { ULONG aX = 0; ULONG aY = 0; ULONG aW = aWidth; ULONG aH = aHeight; NotifyUpdate (aX, aY, aW, aH); RTThreadSleep(40); } } return S_OK; }
/** * Time constrained test with and unlimited N threads. */ static void tst3(uint32_t cThreads, uint32_t cbObject, int iMethod, uint32_t cSecs) { RTTestISubF("Benchmark - %u threads, %u bytes, %u secs, %s", cThreads, cbObject, cSecs, iMethod == 0 ? "RTMemCache" : "RTMemAlloc"); /* * Create a cache with unlimited space, a start semaphore and line up * the threads. */ RTTESTI_CHECK_RC_RETV(RTMemCacheCreate(&g_hMemCache, cbObject, 0 /*cbAlignment*/, UINT32_MAX, NULL, NULL, NULL, 0 /*fFlags*/), VINF_SUCCESS); RTSEMEVENTMULTI hEvt; RTTESTI_CHECK_RC_OK_RETV(RTSemEventMultiCreate(&hEvt)); TST3THREAD aThreads[64]; RTTESTI_CHECK_RETV(cThreads < RT_ELEMENTS(aThreads)); ASMAtomicWriteBool(&g_fTst3Stop, false); for (uint32_t i = 0; i < cThreads; i++) { aThreads[i].hThread = NIL_RTTHREAD; aThreads[i].cIterations = 0; aThreads[i].fUseCache = iMethod == 0; aThreads[i].cbObject = cbObject; aThreads[i].hEvt = hEvt; RTTESTI_CHECK_RC_OK_RETV(RTThreadCreateF(&aThreads[i].hThread, tst3Thread, &aThreads[i], 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "tst3-%u", i)); } /* * Start the race. */ RTTimeNanoTS(); /* warmup */ uint64_t uStartTS = RTTimeNanoTS(); RTTESTI_CHECK_RC_OK_RETV(RTSemEventMultiSignal(hEvt)); RTThreadSleep(cSecs * 1000); ASMAtomicWriteBool(&g_fTst3Stop, true); for (uint32_t i = 0; i < cThreads; i++) RTTESTI_CHECK_RC_OK_RETV(RTThreadWait(aThreads[i].hThread, 60*1000, NULL)); uint64_t cElapsedNS = RTTimeNanoTS() - uStartTS; /* * Sum up the counts. */ uint64_t cIterations = 0; for (uint32_t i = 0; i < cThreads; i++) cIterations += aThreads[i].cIterations; RTTestIPrintf(RTTESTLVL_ALWAYS, "%'8u iterations per second, %'llu ns on avg\n", (unsigned)((long double)cIterations * 1000000000.0 / cElapsedNS), cElapsedNS / cIterations); /* clean up */ RTTESTI_CHECK_RC(RTMemCacheDestroy(g_hMemCache), VINF_SUCCESS); RTTESTI_CHECK_RC_OK(RTSemEventMultiDestroy(hEvt)); }
static void test1(void) { RTTestSub(g_hTest, "Interrupt RTThreadSleep"); RTTHREAD hThread[16]; RTMSINTERVAL msWait = 1000; for (unsigned i = 0; i < RT_ELEMENTS(hThread); i++) { RTTESTI_CHECK_RC_RETV(RTThreadCreate(&hThread[i], testThread, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "test"), VINF_SUCCESS); } RTThreadSleep(500); RTPrintf("Waiting for %dms ...\n", msWait); RTThreadSleep(msWait); for (unsigned i = 0; i < RT_ELEMENTS(hThread); i++) RTTESTI_CHECK_RC(RTThreadWait(hThread[i], RT_INDEFINITE_WAIT, NULL), VINF_SUCCESS); RTPrintf("sum kernel = %lldms, sum user = %lldms\n", g_kernel, g_user); }
/** * Receive thread loop. * * @returns 0 on success. * @param ThreadSelf Thread handle to this thread. * @param pvUser User argument. */ static DECLCALLBACK(int) drvCharReceiveLoop(RTTHREAD ThreadSelf, void *pvUser) { PDRVCHAR pThis = (PDRVCHAR)pvUser; char abBuffer[256]; char *pbRemaining = abBuffer; size_t cbRemaining = 0; int rc; while (!pThis->fShutdown) { if (!cbRemaining) { /* Get block of data from stream driver. */ if (pThis->pDrvStream) { pbRemaining = abBuffer; cbRemaining = sizeof(abBuffer); rc = pThis->pDrvStream->pfnRead(pThis->pDrvStream, abBuffer, &cbRemaining); if (RT_FAILURE(rc)) { LogFlow(("Read failed with %Rrc\n", rc)); break; } } else RTThreadSleep(100); } else { /* Send data to guest. */ size_t cbProcessed = cbRemaining; rc = pThis->pDrvCharPort->pfnNotifyRead(pThis->pDrvCharPort, pbRemaining, &cbProcessed); if (RT_SUCCESS(rc)) { Assert(cbProcessed); pbRemaining += cbProcessed; cbRemaining -= cbProcessed; STAM_COUNTER_ADD(&pThis->StatBytesRead, cbProcessed); } else if (rc == VERR_TIMEOUT) { /* Normal case, just means that the guest didn't accept a new * character before the timeout elapsed. Just retry. */ rc = VINF_SUCCESS; } else { LogFlow(("NotifyRead failed with %Rrc\n", rc)); break; } } } return VINF_SUCCESS; }
/** * Send thread. * This is constantly sending frames to the other interface. */ DECLCALLBACK(int) SendThread(RTTHREAD Thread, void *pvArg) { PMYARGS pArgs = (PMYARGS)pvArg; int rc; /* * Send g_cbTransfer of data. */ uint8_t abBuf[16384] = {0}; MYFRAMEHDR *pHdr = (MYFRAMEHDR *)&abBuf[0]; uint32_t iFrame = 0; uint32_t cbSent = 0; uint32_t cSend = 0; pHdr->SrcMac = pArgs->Mac; pHdr->DstMac = pArgs->Mac; pHdr->DstMac.au16[2] = (pArgs->Mac.au16[2] + 1) % 2; pArgs->u64Start = RTTimeNanoTS(); for (; cbSent < g_cbTransfer; iFrame++) { const unsigned cb = pArgs->cbFrame ? pArgs->cbFrame : iFrame % 1519 + sizeof(RTMAC) * 2 + sizeof(unsigned); pHdr->iFrame = iFrame; INTNETSG Sg; IntNetSgInitTemp(&Sg, abBuf, cb); RTTEST_CHECK_RC_OK(g_hTest, rc = intnetR0RingWriteFrame(&pArgs->pBuf->Send, &Sg, NULL)); if (RT_SUCCESS(rc)) RTTEST_CHECK_RC_OK(g_hTest, rc = IntNetR0IfSend(pArgs->hIf, g_pSession)); cbSent += cb; } /* * Termination frames. */ pHdr->iFrame = 0xffffdead; pHdr->auEos[0] = 0xffffdead; pHdr->auEos[1] = 0xffffdead; pHdr->auEos[2] = 0xffffdead; for (unsigned c = 0; c < 20; c++) { RTTEST_CHECK_RC_OK(g_hTest, rc = tstIntNetSendBuf(&pArgs->pBuf->Send, pArgs->hIf, g_pSession, abBuf, sizeof(RTMAC) * 2 + sizeof(unsigned) * 4)); RTThreadSleep(1); } RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "sender thread %.6Rhxs terminating.\n" "iFrame=%u cb=%'u\n", &pArgs->Mac, iFrame, cbSent); return 0; }
/** * The parent main routine. * @param argv0 The executable name (or whatever). */ static int mainParent(const char *argv0) { /* * Init. */ RTTEST hTest; int rc = RTTestInitAndCreate("tstSupSem-Zombie", &hTest); if (rc) return rc; RTTestBanner(hTest); /* * Spin of the child process which may or may not turn into a zombie */ for (uint32_t iPass = 0; iPass < 32; iPass++) { RTTestSubF(hTest, "Pass %u", iPass); RTPROCESS hProcess; const char *apszArgs[3] = { argv0, "--child", NULL }; RTTESTI_CHECK_RC_OK(rc = RTProcCreate(argv0, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, &hProcess)); if (RT_SUCCESS(rc)) { /* * Wait for 60 seconds then give up. */ RTPROCSTATUS Status; uint64_t StartTS = RTTimeMilliTS(); for (;;) { rc = RTProcWait(hProcess, RTPROCWAIT_FLAGS_NOBLOCK, &Status); if (RT_SUCCESS(rc)) break; uint64_t cElapsed = RTTimeMilliTS() - StartTS; if (cElapsed > 60*1000) break; RTThreadSleep(cElapsed < 60 ? 30 : cElapsed < 200 ? 10 : 100); } RTTESTI_CHECK_RC_OK(rc); if ( RT_SUCCESS(rc) && ( Status.enmReason != RTPROCEXITREASON_NORMAL || Status.iStatus != 0)) { RTTestIFailed("child %d (%#x) reason %d\n", Status.iStatus, Status.iStatus, Status.enmReason); rc = VERR_PERMISSION_DENIED; } } /* one zombie process is enough. */ if (RT_FAILURE(rc)) break; } return RTTestSummaryAndDestroy(hTest); }
int main(int argc, char **argv) { RTR3InitExe(argc, &argv, RTR3INIT_FLAGS_SUPLIB); if (argc <= 1) { RTPrintf("tstTime-3: usage: tstTime-3 <seconds> [seconds2 [..]]\n"); return 1; } RTPrintf("tstTime-3: Testing difference between RTTimeNanoTS() and OS time...\n"); for (int i = 1; i < argc; i++) { uint64_t cSeconds = 0; int rc = RTStrToUInt64Ex(argv[i], NULL, 0, &cSeconds); if (RT_FAILURE(rc)) { RTPrintf("tstTime-3: Invalid argument %d: %s\n", i, argv[i]); return 1; } RTPrintf("tstTime-3: %d - %RU64 seconds period...\n", i, cSeconds); RTTimeNanoTS(); OSNanoTS(); RTThreadSleep(1); uint64_t u64RTStartTS = RTTimeNanoTS(); uint64_t u64OSStartTS = OSNanoTS(); RTThreadSleep(cSeconds * 1000); uint64_t u64RTElapsedTS = RTTimeNanoTS(); uint64_t u64OSElapsedTS = OSNanoTS(); u64RTElapsedTS -= u64RTStartTS; u64OSElapsedTS -= u64OSStartTS; RTPrintf("tstTime-3: %d - RT: %16RU64 ns\n", i, u64RTElapsedTS); RTPrintf("tstTime-3: %d - OS: %16RU64 ns\n", i, u64OSElapsedTS); RTPrintf("tstTime-3: %d - diff: %16RI64 ns\n", i, u64RTElapsedTS - u64OSElapsedTS); } return 0; }
static DECLCALLBACK(int) testServerListenAndCancelThread(RTTHREAD hSelf, void *pvUser) { PRTLOCALIPCSERVER pServer = (PRTLOCALIPCSERVER)pvUser; AssertPtr(pServer); RTThreadSleep(5000); /* Wait a bit to simulate waiting in main thread. */ int rc = RTLocalIpcServerCancel(*pServer); AssertRC(rc); return 0; }
static void tstRTCreateProcEx2(const char *pszAsUser, const char *pszPassword) { RTTestISub("Standard Err"); RTPIPE hPipeR, hPipeW; RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS); const char * apszArgs[3] = { "non-existing-non-executable-file", "--testcase-child-2", NULL }; RTHANDLE Handle; Handle.enmType = RTHANDLETYPE_PIPE; Handle.u.hPipe = hPipeW; RTPROCESS hProc; RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL, NULL, &Handle, pszAsUser, pszPassword, &hProc), VINF_SUCCESS); RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS); char szOutput[_4K]; size_t offOutput = 0; for (;;) { size_t cbLeft = sizeof(szOutput) - 1 - offOutput; RTTESTI_CHECK(cbLeft > 0); if (cbLeft == 0) break; size_t cbRead; int rc = RTPipeReadBlocking(hPipeR, &szOutput[offOutput], cbLeft, &cbRead); if (RT_FAILURE(rc)) { RTTESTI_CHECK_RC(rc, VERR_BROKEN_PIPE); break; } offOutput += cbRead; } szOutput[offOutput] = '\0'; RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS); RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND }; RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS); RTThreadSleep(10); if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0) RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus); else if ( offOutput != sizeof("howdy") - 1 || strcmp(szOutput, "howdy")) RTTestIFailed("wrong output: \"%s\" (len=%u)", szOutput, offOutput); else RTTestIPassed(NULL); }
/** * Reads a key by name from the host SMC. * * @returns success indicator. * @param pszName The key name, must be exactly 4 chars long. * @param pbBuf The output buffer. * @param cbBuf The buffer size. Max 32 bytes. */ static bool devR0SmcQueryHostKey(const char *pszName, uint8_t *pbBuf, size_t cbBuf) { Assert(strlen(pszName) == 4); Assert(cbBuf <= 32); Assert(cbBuf > 0); /* * Issue the READ command. */ uint32_t cMsSleep = 1; for (;;) { ASMOutU32(SMC_PORT_CMD, SMC_CMD_GET_KEY_VALUE); RTThreadSleep(cMsSleep); uint8_t bCurState = ASMInU8(SMC_PORT_CMD); if ((bCurState & 0xf) == 0xc) break; cMsSleep <<= 1; if (cMsSleep > 64) { LogRel(("devR0Smc: %s: bCurState=%#x, wanted %#x.\n", "cmd", bCurState, 0xc)); return false; } } /* * Send it the key. */ for (unsigned off = 0; off < 4; off++) { ASMOutU8(SMC_PORT_DATA, pszName[off]); if (!devR0SmcWaitHostState(4, "key")) return false; } /* * The desired amount of output. */ ASMOutU8(SMC_PORT_DATA, (uint8_t)cbBuf); /* * Read the output. */ for (size_t off = 0; off < cbBuf; off++) { if (!devR0SmcWaitHostState(5, off ? "data" : "len")) return false; pbBuf[off] = ASMInU8(SMC_PORT_DATA); } LogRel(("SMC: pbBuf=%.*s\n", cbBuf, pbBuf)); return true; }
static int testSessionWait(RTTEST hTest, const char *pszExecPath) { RTTestSub(hTest, "testSessionWait"); RTLOCALIPCSERVER ipcServer; int rc = RTLocalIpcServerCreate(&ipcServer, "tstRTLocalIpcSessionWait", RTLOCALIPC_FLAGS_MULTI_SESSION); if (RT_SUCCESS(rc)) { LOCALIPCTHREADCTX threadCtx = { ipcServer, hTest }; /* Spawn a simple worker thread and let it listen for incoming connections. * In the meanwhile we try to cancel the server and see what happens. */ RTTHREAD hThread; rc = RTThreadCreate(&hThread, testSessionWaitThread, &threadCtx, 0 /* Stack */, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "tstIpc3"); if (RT_SUCCESS(rc)) { do { RTPROCESS hProc; const char *apszArgs[4] = { pszExecPath, "child", "tstRTLocalIpcSessionWaitFork", NULL }; RTTEST_CHECK_RC_BREAK(hTest, RTProcCreate(pszExecPath, apszArgs, RTENV_DEFAULT, 0 /* fFlags*/, &hProc), VINF_SUCCESS); RTThreadSleep(5000); /* Let the server run for some time ... */ RTTestPrintf(hTest, RTTESTLVL_INFO, "Cancelling server listening\n"); RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcServerCancel(ipcServer), VINF_SUCCESS); /* Wait for the server thread to terminate. */ int threadRc; RTTEST_CHECK_RC(hTest, RTThreadWait(hThread, 30 * 1000 /* 30s timeout */, &threadRc), VINF_SUCCESS); RTTEST_CHECK_RC_BREAK(hTest, threadRc, VERR_CANCELLED); RTTEST_CHECK_RC(hTest, RTLocalIpcServerDestroy(ipcServer), VINF_SUCCESS); RTTestPrintf(hTest, RTTESTLVL_INFO, "Server thread terminated successfully\n"); /* Check if the child ran successfully. */ RTPROCSTATUS stsChild; RTTEST_CHECK_RC_BREAK(hTest, RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &stsChild), VINF_SUCCESS); RTTestPrintf(hTest, RTTESTLVL_INFO, "Child terminated\n"); RTTEST_CHECK_BREAK(hTest, stsChild.enmReason == RTPROCEXITREASON_NORMAL); RTTEST_CHECK_BREAK(hTest, stsChild.iStatus == 0); } while (0); } else RTTestFailed(hTest, "Unable to create thread for cancelling server, rc=%Rrc\n", rc); } else RTTestFailed(hTest, "Unable to create IPC server, rc=%Rrc\n", rc); return VINF_SUCCESS; }
int testNetwork(pm::CollectorHAL *collector) { pm::CollectorHints hints; uint64_t hostRxStart, hostTxStart; uint64_t hostRxStop, hostTxStop, speed = 125000000; /* Assume 1Gbit/s */ RTPrintf("tstCollector: TESTING - Network load, sleeping for 5 sec...\n"); hostRxStart = hostTxStart = 0; int rc = collector->preCollect(hints, 0); if (RT_FAILURE(rc)) { RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc); return 1; } rc = collector->getRawHostNetworkLoad(NETIFNAME, &hostRxStart, &hostTxStart); if (RT_FAILURE(rc)) { RTPrintf("tstCollector: getRawHostNetworkLoad() -> %Rrc\n", rc); return 1; } RTThreadSleep(5000); // Sleep for five seconds rc = collector->preCollect(hints, 0); if (RT_FAILURE(rc)) { RTPrintf("tstCollector: preCollect() -> %Rrc\n", rc); return 1; } hostRxStop = hostRxStart; hostTxStop = hostTxStart; rc = collector->getRawHostNetworkLoad(NETIFNAME, &hostRxStop, &hostTxStop); if (RT_FAILURE(rc)) { RTPrintf("tstCollector: getRawHostNetworkLoad() -> %Rrc\n", rc); return 1; } RTPrintf("tstCollector: host network speed = %llu bytes/sec (%llu mbit/sec)\n", speed, speed/(1000000/8)); RTPrintf("tstCollector: host network rx = %llu bytes/sec (%llu mbit/sec, %u.%u %%)\n", (hostRxStop - hostRxStart)/5, (hostRxStop - hostRxStart)/(5000000/8), (hostRxStop - hostRxStart) * 100 / (speed * 5), (hostRxStop - hostRxStart) * 10000 / (speed * 5) % 100); RTPrintf("tstCollector: host network tx = %llu bytes/sec (%llu mbit/sec, %u.%u %%)\n\n", (hostTxStop - hostTxStart)/5, (hostTxStop - hostTxStart)/(5000000/8), (hostTxStop - hostTxStart) * 100 / (speed * 5), (hostTxStop - hostTxStart) * 10000 / (speed * 5) % 100); return 0; }
/** * Waits for the specified status on the host SMC. * * @returns success indicator. * @param bStatus The desired status. * @param pszWhat What we're currently doing. For the log. */ static bool devR0SmcWaitHostStatus(uint8_t bStatus, const char *pszWhat) { uint8_t bCurStatus; for (uint32_t cMsSleep = 1; cMsSleep <= 64; cMsSleep <<= 1) { RTThreadSleep(cMsSleep); bCurStatus = ASMInU8(APPLESMC_CMD_PORT); if ((bCurStatus & 0xf) == bStatus) return true; } LogRel(("devR0Smc: %s: bCurStatus=%#x, wanted %#x.\n", pszWhat, bCurStatus, bStatus)); return false; }
/** * Entry point. */ extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp) { bool fExit = false; RTEXITCODE rcExit = ParseOptions(argc, argv, &fExit); if (rcExit == RTEXITCODE_SUCCESS && !fExit) { rcExit = LoadModules(); if (rcExit == RTEXITCODE_SUCCESS) { for (;;) RTThreadSleep(RT_INDEFINITE_WAIT); } } return rcExit; }
static int testSessionDataChildWorker(RTTEST hTest) { size_t cbScratchBuf = _1K; /** @todo Make this random in future. */ uint8_t *pvScratchBuf = (uint8_t*)RTMemAlloc(cbScratchBuf); RTTEST_CHECK_RET(hTest, pvScratchBuf != NULL, RTEXITCODE_FAILURE); do { RTThreadSleep(2000); /* Fudge. */ RTLOCALIPCSESSION hSession; RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionConnect(&hSession, "tstRTLocalIpcSessionData", 0 /* Flags */), VINF_SUCCESS); /* Get number of rounds we want to read/write. */ uint32_t cRounds = 0; RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionWaitForData(hSession, RT_INDEFINITE_WAIT), VINF_SUCCESS); RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionRead(hSession, &cRounds, sizeof(cRounds), NULL /* Get exactly sizeof(cRounds) bytes */), VINF_SUCCESS); RTTEST_CHECK_BREAK(hTest, cRounds == 256); /** @todo Check for != 0 when using RTRand(). */ /* Receive all rounds. */ for (uint32_t i = 0; i < cRounds; i++) { RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionWaitForData(hSession, RT_INDEFINITE_WAIT), VINF_SUCCESS); char szMsg[32]; RTTEST_CHECK_BREAK(hTest, RTStrPrintf(szMsg, sizeof(szMsg), "YayIGotRound%RU32FromTheServer", i) > 0); RTTEST_CHECK_RC_BREAK(hTest, testSessionDataReadTestMsg(hTest, hSession, pvScratchBuf, cbScratchBuf, szMsg), VINF_SUCCESS); if (RTTestErrorCount(hTest)) break; } /* Send all rounds back to the server. */ for (uint32_t i = 0; i < cRounds; i++) { LOCALIPCTESTMSG msg; RTTEST_CHECK_BREAK(hTest, RTStrPrintf(msg.szOp, sizeof(msg.szOp), "YayIGotRound%RU32FromTheClient", i) > 0); RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionWrite(hSession, &msg, sizeof(msg)), VINF_SUCCESS); } RTTEST_CHECK_RC_BREAK(hTest, RTLocalIpcSessionClose(hSession), VINF_SUCCESS); } while (0); RTMemFree(pvScratchBuf); return !RTTestErrorCount(hTest) ? VINF_SUCCESS : VERR_GENERAL_FAILURE /* Doesn't matter */; }
static int VBoxServiceAutoMountUnmount(const char *pszMountPoint) { AssertPtrReturn(pszMountPoint, VERR_INVALID_PARAMETER); int rc = VINF_SUCCESS; uint8_t uTries = 0; int r; while (uTries++ < 3) { r = umount(pszMountPoint); if (r == 0) break; RTThreadSleep(5000); /* Wait a while ... */ } if (r == -1) rc = RTErrConvertFromErrno(errno); return rc; }
/** * Waits a little while for a debuggger to attach. * * @returns True is a debugger have attached. * @param pVM Pointer to the VM. * @param enmEvent Event. */ bool dbgfR3WaitForAttach(PVM pVM, DBGFEVENTTYPE enmEvent) { /* * First a message. */ #ifndef RT_OS_L4 # if !defined(DEBUG) || defined(DEBUG_sandervl) || defined(DEBUG_frank) || defined(IEM_VERIFICATION_MODE) int cWait = 10; # else int cWait = HWACCMIsEnabled(pVM) && ( enmEvent == DBGFEVENT_ASSERTION_HYPER || enmEvent == DBGFEVENT_FATAL_ERROR) && !RTEnvExist("VBOX_DBGF_WAIT_FOR_ATTACH") ? 10 : 150; # endif RTStrmPrintf(g_pStdErr, "DBGF: No debugger attached, waiting %d second%s for one to attach (event=%d)\n", cWait / 10, cWait != 10 ? "s" : "", enmEvent); RTStrmFlush(g_pStdErr); while (cWait > 0) { RTThreadSleep(100); if (pVM->dbgf.s.fAttached) { RTStrmPrintf(g_pStdErr, "Attached!\n"); RTStrmFlush(g_pStdErr); return true; } /* next */ if (!(cWait % 10)) { RTStrmPrintf(g_pStdErr, "%d.", cWait / 10); RTStrmFlush(g_pStdErr); } cWait--; } #endif RTStrmPrintf(g_pStdErr, "Stopping the VM!\n"); RTStrmFlush(g_pStdErr); return false; }
/** * I/O thread for pending TX. * * @returns VINF_SUCCESS (ignored). * @param pVM Pointer to the VM. * @param pThread The PDM thread data. */ static DECLCALLBACK(int) pdmR3NsTxThread(PVM pVM, PPDMTHREAD pThread) { PPDMNETSHAPER pShaper = (PPDMNETSHAPER)pThread->pvUser; LogFlow(("pdmR3NsTxThread: pShaper=%p\n", pShaper)); while (pThread->enmState == PDMTHREADSTATE_RUNNING) { RTThreadSleep(PDM_NETSHAPER_MAX_LATENCY); /* Go over all bandwidth groups/filters calling pfnXmitPending */ int rc = RTCritSectEnter(&pShaper->cs); AssertRC(rc); PPDMNSBWGROUP pBwGroup = pShaper->pBwGroupsHead; while (pBwGroup) { pdmNsBwGroupXmitPending(pBwGroup); pBwGroup = pBwGroup->pNext; } rc = RTCritSectLeave(&pShaper->cs); AssertRC(rc); } return VINF_SUCCESS; }
static DECLCALLBACK(int) Once2CB(void *pvUser) { if (ASMAtomicIncU32(&g_cOnce2CB) != 1) { RTPrintf("tstOnce: ERROR - Once2CB: g_cOnce2CB not zero!\n"); g_cErrors++; return VERR_WRONG_ORDER; } if (pvUser != (void *)42) { RTPrintf("tstOnce: ERROR - Once2CB: pvUser=%p!\n", pvUser); g_cErrors++; return VERR_INVALID_PARAMETER; } RTThreadSleep(2); Assert(!g_fOnce2Ready); ASMAtomicWriteBool(&g_fOnce2Ready, true); return VINF_SUCCESS; }