1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 // this test verifies that the toku thread pool is resilient when hitting the nproc limit.
40 
41 #include <util/threadpool.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <assert.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <errno.h>
48 #include <sys/resource.h>
49 
50 int verbose = 0;
51 
usage(void)52 static int usage(void) {
53     fprintf(stderr, "[-q] [-v] [--verbose] (%d)\n", verbose);
54     return 1;
55 }
56 
f(void * arg)57 static void *f(void *arg) {
58     return arg;
59 }
60 
dotest(int the_limit)61 static int dotest(int the_limit) {
62     if (verbose)
63         fprintf(stderr, "%s:%u %d\n", __FILE__, __LINE__, the_limit);
64     int r;
65     struct toku_thread_pool *pool = nullptr;
66     r = toku_thread_pool_create(&pool, 10);
67     assert(r == 0 && pool != nullptr);
68 
69     struct rlimit current_nproc_limit;
70     r = getrlimit(RLIMIT_NPROC, &current_nproc_limit);
71     assert(r == 0);
72 
73     struct rlimit new_nproc_limit = current_nproc_limit;
74     new_nproc_limit.rlim_cur = the_limit;
75     r = setrlimit(RLIMIT_NPROC, &new_nproc_limit);
76     assert(r == 0);
77 
78     int want_n = 20;
79     int got_n = want_n;
80     r = toku_thread_pool_run(pool, 0, &got_n, f, nullptr);
81     if (r == 0)
82         assert(want_n == got_n);
83     else {
84         assert(r == EWOULDBLOCK);
85         assert(got_n <= want_n);
86     }
87 
88     r = setrlimit(RLIMIT_NPROC, &current_nproc_limit);
89     assert(r == 0);
90 
91     if (verbose)
92         toku_thread_pool_print(pool, stderr);
93     toku_thread_pool_destroy(&pool);
94     return got_n > 0;
95 }
96 
main(int argc,char * argv[])97 int main(int argc, char *argv[]) {
98     // parse args
99     for (int i = 1; i < argc; i++) {
100         char *arg = argv[i];
101         if (arg[0] != '-')
102             break;
103         if (strcmp(arg, "-v") == 0 || strcmp(arg, "--verbose") == 0) {
104             verbose = verbose+1;
105             continue;
106         }
107         if (strcmp(arg, "-q") == 0) {
108             verbose = verbose > 0 ? verbose-1 : 0;
109             continue;
110         }
111         return usage();
112     }
113     // set increasing nproc limits until the test succeeds in hitting the limit after > 0 threads are created
114     for (int i = 0; 1; i++) {
115         if (dotest(i))
116             break;
117     }
118     return 0;
119 }
120