tstRTMemCache.cpp revision 64babf0f32eaf36212d54af4a3ce5fe193b24825
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * IPRT Testcase - RTMemCache.
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * Copyright (C) 2010 Sun Microsystems, Inc.
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * available from http://www.virtualbox.org. This file is free software;
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * you can redistribute it and/or modify it under the terms of the GNU
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * General Public License (GPL) as published by the Free Software
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * The contents of this file may alternatively be used under the terms
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * of the Common Development and Distribution License Version 1.0
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * VirtualBox OSE distribution, in which case the provisions of the
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * CDDL are applicable instead of those of the GPL.
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * You may elect to license modified versions of this file under the
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * terms and conditions of either the GPL or the CDDL or both.
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * additional information or have any questions.
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync/*******************************************************************************
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync* Header Files *
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync*******************************************************************************/
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync/*******************************************************************************
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync* Structures and Typedefs *
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync*******************************************************************************/
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsynctypedef struct TST3THREAD
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync/*******************************************************************************
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync* Global Variables *
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync*******************************************************************************/
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync/** The test handle */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync/** Global mem cache handle for use in some of the testcases. */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync/** Stop indicator for tst3 threads. */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsyncstatic bool volatile g_fTst3Stop;
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * Basic API checks.
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * We'll return if any of these fails.
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsyncstatic void tst1(void)
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync /* Create one without constructor or destructor. */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RC_RETV(RTMemCacheCreate(&hMemCache, 256, cObjects, 32, NULL, NULL, NULL), VINF_SUCCESS);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync /* Allocate a bit and free it again. */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RC_RETV(RTMemCacheAllocEx(hMemCache, &pv), VINF_SUCCESS);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK((pv = RTMemCacheAlloc(hMemCache)) != NULL);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync /* Allocate everything and free it again, checking size constraints. */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync /* Allocate everything. */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RC(RTMemCacheAllocEx(hMemCache, &apv[i]), VINF_SUCCESS);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync /* Check that we've got it all. */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RC(rc = RTMemCacheAllocEx(hMemCache, &pv), VERR_MEM_CACHE_MAX_SIZE);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK((pv = RTMemCacheAlloc(hMemCache)) == NULL);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync /* Free all the allocations. */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK((pv = RTMemCacheAlloc(hMemCache)) != NULL);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync /* Destroy it. */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RC(RTMemCacheDestroy(hMemCache), VINF_SUCCESS);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RC(RTMemCacheDestroy(NIL_RTMEMCACHE), VINF_SUCCESS);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync/** Constructor for tst2. */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsyncstatic DECLCALLBACK(int) tst2Ctor(RTMEMCACHE hMemCache, void *pvObj, void *pvUser)
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK(ASMMemIsAll8(pvObj, 256, 0) == NULL);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync if (*(bool *)pvUser)
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync/** Destructor for tst2. Checks that it was constructed and used twice. */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsyncstatic DECLCALLBACK(void) tst2Dtor(RTMEMCACHE hMemCache, void *pvObj, void *pvUser)
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK(!strcmp((char *)pvObj, "ctor was called\nused\nused\n"));
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * Test constructor / destructor.
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsyncstatic void tst2(void)
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync /* Create one without constructor or destructor. */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync bool fFail = false;
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RC_RETV(RTMemCacheCreate(&g_hMemCache, 256, cObjects, 32, tst2Ctor, tst2Dtor, &fFail), VINF_SUCCESS);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync /* A failure run first. */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RC_RETV(RTMemCacheAllocEx(g_hMemCache, &pv), VERR_RESOURCE_BUSY);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync /* To two rounds where we allocate all the objects and free them again. */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RC_RETV(RTMemCacheAllocEx(g_hMemCache, &apv[i]), VINF_SUCCESS);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK(!strcmp((char *)apv[i], "ctor was called\n"));
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK(!strcmp((char *)apv[i], "ctor was called\nused\n"));
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RETV((pv = RTMemCacheAlloc(g_hMemCache)) == NULL);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync /* Cone, destroy the cache. */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RC(RTMemCacheDestroy(g_hMemCache), VINF_SUCCESS);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * Thread that allocates
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * @param hThreadSelf The thread.
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * @param pvArg Pointer to fUseCache.
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsyncstatic DECLCALLBACK(int) tst3Thread(RTTHREAD hThreadSelf, void *pvArg)
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync /* wait for the kick-off */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTEST_CHECK_RC_OK(g_hTest, RTSemEventMultiWait(pThread->hEvt, RT_INDEFINITE_WAIT));
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync /* allocate and free loop */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync /* report back the status */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * Time constrained test with and unlimited N threads.
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsyncstatic void tst3(uint32_t cThreads, uint32_t cbObject, bool fUseCache, uint32_t cSecs)
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTestISubF("Benchmark - %u threads, %s", cThreads, fUseCache ? "RTMemCache" : "RTMemAlloc");
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * Create a cache with unlimited space, a start semaphore and line up
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * the threads.
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RC_RETV(RTMemCacheCreate(&g_hMemCache, cbObject, 0 /*cbAlignment*/, UINT32_MAX, NULL, NULL, NULL), VINF_SUCCESS);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RC_OK_RETV(RTSemEventMultiCreate(&hEvt));
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RETV(cThreads < RT_ELEMENTS(aThreads));
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RC_OK_RETV(RTThreadCreateF(&aThreads[i].hThread, tst3Thread, &aThreads[i], 0,
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "tst3-%u", i));
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * Start the race.
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RC_OK_RETV(RTSemEventMultiSignal(hEvt));
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RC_OK_RETV(RTThreadWait(aThreads[i].hThread, 60*1000, NULL));
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync * Sum up the counts.
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTestIPrintf(RTTESTLVL_ALWAYS, "%'8u iterations per second\n",
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync (unsigned)((long double)cIterations * 1000000000.0 / cElapsedNS));
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync /* clean up */
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync RTTESTI_CHECK_RC(RTMemCacheDestroy(g_hMemCache), VINF_SUCCESS);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync int rc = RTTestInitAndCreate("tstRTMemCache", &hTest);
64babf0f32eaf36212d54af4a3ce5fe193b24825vboxsync /* threads, cbObj, fUseCache, cSecs */