VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTReqPool.cpp@ 42699

Last change on this file since 42699 was 39636, checked in by vboxsync, 13 years ago

reqpool: fixed RTREQPOOLSTAT_REQUESTS_PROCESSED.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1/* $Id: tstRTReqPool.cpp 39636 2011-12-16 00:19:35Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Request Thread Pool.
4 */
5
6/*
7 * Copyright (C) 2011 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#include <iprt/req.h>
32
33#include <iprt/err.h>
34#include <iprt/test.h>
35#include <iprt/thread.h>
36#include <iprt/time.h>
37
38
39
40/*******************************************************************************
41* Global Variables *
42*******************************************************************************/
43static RTTEST g_hTest = NIL_RTTEST;
44
45
46static DECLCALLBACK(int) NopCallback(void)
47{
48 return VINF_SUCCESS;
49}
50
51static void test1(void)
52{
53 RTTestISub("Basics");
54 RTREQPOOL hPool;
55 uint32_t cMaxThreads = 10;
56 RTTESTI_CHECK_RC_RETV(RTReqPoolCreate(cMaxThreads, RT_MS_1SEC, 6, 500, "test1", &hPool), VINF_SUCCESS);
57 RTTESTI_CHECK(RTReqPoolGetCfgVar(hPool, RTREQPOOLCFGVAR_THREAD_TYPE) == (uint64_t)RTTHREADTYPE_DEFAULT);
58 RTTESTI_CHECK(RTReqPoolGetCfgVar(hPool, RTREQPOOLCFGVAR_MAX_THREADS) == 10);
59 RTTESTI_CHECK(RTReqPoolGetCfgVar(hPool, RTREQPOOLCFGVAR_MIN_THREADS) > 1);
60 RTTESTI_CHECK(RTReqPoolGetCfgVar(hPool, RTREQPOOLCFGVAR_MS_MIN_IDLE) == RT_MS_1SEC);
61 RTTESTI_CHECK(RTReqPoolGetCfgVar(hPool, RTREQPOOLCFGVAR_MS_IDLE_SLEEP) == RT_MS_1SEC);
62 RTTESTI_CHECK(RTReqPoolGetCfgVar(hPool, RTREQPOOLCFGVAR_PUSH_BACK_THRESHOLD) == 6);
63 RTTESTI_CHECK(RTReqPoolGetCfgVar(hPool, RTREQPOOLCFGVAR_PUSH_BACK_MAX_MS) == 500);
64 RTTESTI_CHECK(RTReqPoolGetCfgVar(hPool, RTREQPOOLCFGVAR_PUSH_BACK_MIN_MS) < 500);
65 RTTESTI_CHECK(RTReqPoolGetCfgVar(hPool, RTREQPOOLCFGVAR_MAX_FREE_REQUESTS) >= 10);
66 RTTESTI_CHECK(RTReqPoolGetCfgVar(hPool, RTREQPOOLCFGVAR_MAX_FREE_REQUESTS) < 1024);
67
68 RTTESTI_CHECK(RTReqPoolGetStat(hPool, RTREQPOOLSTAT_REQUESTS_FREE) == 0);
69 RTTESTI_CHECK(RTReqPoolGetStat(hPool, RTREQPOOLSTAT_THREADS) == 0);
70 uint32_t const cMinThreads = RTReqPoolGetCfgVar(hPool, RTREQPOOLCFGVAR_MIN_THREADS);
71 uint32_t const cMaxFreeReqs = RTReqPoolGetCfgVar(hPool, RTREQPOOLCFGVAR_MAX_FREE_REQUESTS);
72
73 PRTREQ hReq;
74 RTTESTI_CHECK_RC_RETV(RTReqPoolAlloc(hPool, RTREQTYPE_INTERNAL, &hReq), VINF_SUCCESS);
75 RTTESTI_CHECK(RTReqRetain(hReq) == 2);
76 RTTESTI_CHECK(RTReqRelease(hReq) == 1);
77 RTTESTI_CHECK_RC(RTReqGetStatus(hReq), VERR_RT_REQUEST_STATUS_STILL_PENDING);
78 RTTESTI_CHECK(RTReqRelease(hReq) == 0);
79 RTTESTI_CHECK(RTReqPoolGetStat(hPool, RTREQPOOLSTAT_REQUESTS_FREE) == 1);
80
81 RTTESTI_CHECK(RTReqPoolGetStat(hPool, RTREQPOOLSTAT_REQUESTS_PROCESSED) == 0);
82 RTTESTI_CHECK_RC(RTReqPoolCallWait(hPool, (PFNRT)RTThreadSleep, 1, (RTMSINTERVAL)0), VINF_SUCCESS);
83 RTTESTI_CHECK_RC(RTReqPoolCallWait(hPool, (PFNRT)RTThreadSleep, 1, (RTMSINTERVAL)2), VINF_SUCCESS);
84 RTTESTI_CHECK_RC(RTReqPoolCallWait(hPool, (PFNRT)RTThreadSleep, 1, (RTMSINTERVAL)3), VINF_SUCCESS);
85 RTTESTI_CHECK(RTReqPoolGetStat(hPool, RTREQPOOLSTAT_REQUESTS_PROCESSED) > 1);
86 RTTESTI_CHECK(RTReqPoolGetStat(hPool, RTREQPOOLSTAT_THREADS) == 1);
87
88 /* Use no wait requests to maximize the number of worker threads. */
89 RTTestISub("No wait requests");
90 for (unsigned i = 0; i < 32; i++)
91 RTTESTI_CHECK_RC(RTReqPoolCallNoWait(hPool, (PFNRT)RTThreadSleep, 1, (RTMSINTERVAL)100), VINF_SUCCESS);
92 uint32_t cThreads = RTReqPoolGetStat(hPool, RTREQPOOLSTAT_THREADS);
93 RTTestIValue("thread-count-1", cThreads, RTTESTUNIT_OCCURRENCES);
94 RTTESTI_CHECK(cThreads >= cMinThreads);
95 RTTESTI_CHECK(cThreads <= cMaxThreads);
96
97 /* Check that idle thread shutdown kicks in. This means delaying a bit first. */
98 RTTestISub("Idle thread shutdown");
99 for (unsigned i = 0; i < 20; i++)
100 {
101 RTTESTI_CHECK_RC(RTReqPoolCallNoWait(hPool, (PFNRT)RTThreadSleep, 1, (RTMSINTERVAL)10), VINF_SUCCESS);
102 RTTESTI_CHECK_RC(RTReqPoolCallNoWait(hPool, (PFNRT)RTThreadSleep, 1, (RTMSINTERVAL)10), VINF_SUCCESS);
103 RTTESTI_CHECK_RC(RTReqPoolCallWait(hPool, (PFNRT)RTThreadSleep, 1, (RTMSINTERVAL)100), VINF_SUCCESS);
104 }
105 RTTESTI_CHECK(RTReqPoolGetStat(hPool, RTREQPOOLSTAT_REQUESTS_FREE) == cMaxFreeReqs || cMaxFreeReqs > 32);
106
107 /* Idle shutdown of worker threads should kick in now. */
108 uint32_t cThreads2 = RTReqPoolGetStat(hPool, RTREQPOOLSTAT_THREADS);
109 RTTestIValue("thread-count-2", cThreads2, RTTESTUNIT_OCCURRENCES);
110 RTTESTI_CHECK(cThreads2 >= cMinThreads);
111 RTTESTI_CHECK(cThreads2 < cThreads || cThreads2 <= cMinThreads);
112
113 RTTESTI_CHECK(RTReqPoolRelease(hPool) == 0);
114}
115
116
117static void test2(void)
118{
119 RTTestISub("Simple Benchmark");
120
121 RTREQPOOL hPool;
122 RTTESTI_CHECK_RC_RETV(RTReqPoolCreate(10, RT_MS_1SEC, 6, 500, "test1", &hPool), VINF_SUCCESS);
123 uint64_t NsTsStart = RTTimeNanoTS();
124 for (unsigned i = 0; i < 10000; i++)
125 RTTESTI_CHECK_RC_BREAK(RTReqPoolCallWait(hPool, (PFNRT)NopCallback, 0), VINF_SUCCESS);
126 uint64_t cNsElapsed = RTTimeNanoTS() - NsTsStart;
127 RTTestIValue("total time", cNsElapsed, RTTESTUNIT_NS);
128 RTTestIValue("per call", cNsElapsed / 10000, RTTESTUNIT_NS_PER_CALL);
129 RTTestIValue("total processing time", RTReqPoolGetStat(hPool, RTREQPOOLSTAT_NS_AVERAGE_REQ_PROCESSING), RTTESTUNIT_NS_PER_CALL);
130
131 RTTESTI_CHECK(RTReqPoolRelease(hPool) == 0);
132}
133
134
135int main()
136{
137 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTReqPool", &g_hTest);
138 if (rcExit != RTEXITCODE_SUCCESS)
139 return rcExit;
140 RTTestBanner(g_hTest);
141
142 test1();
143 if (RTTestIErrorCount() == 0)
144 {
145 test2();
146 }
147 return RTTestSummaryAndDestroy(g_hTest);
148}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette