VirtualBox

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

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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