Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 1 | // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 | // Copyright (c) 2007, Google Inc. |
| 3 | // All rights reserved. |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 4 | // |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 8 | // |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
Brian Silverman | 20350ac | 2021-11-17 18:19:55 -0800 | [diff] [blame] | 18 | // |
Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | // --- |
| 32 | // Author: Craig Silverstein |
| 33 | // |
| 34 | // A few routines that are useful for multiple tests in this directory. |
| 35 | |
| 36 | #include "config_for_unittests.h" |
| 37 | #include <stdlib.h> // for NULL, abort() |
| 38 | // On FreeBSD, if you #include <sys/resource.h>, you have to get stdint first. |
| 39 | #ifdef HAVE_STDINT_H |
| 40 | #include <stdint.h> |
| 41 | #endif |
| 42 | #ifdef HAVE_SYS_RESOURCE_H |
| 43 | #include <sys/resource.h> |
| 44 | #endif |
| 45 | #include "tests/testutil.h" |
| 46 | |
| 47 | |
| 48 | // When compiled 64-bit and run on systems with swap several unittests will end |
| 49 | // up trying to consume all of RAM+swap, and that can take quite some time. By |
| 50 | // limiting the address-space size we get sufficient coverage without blowing |
| 51 | // out job limits. |
| 52 | void SetTestResourceLimit() { |
| 53 | #ifdef HAVE_SYS_RESOURCE_H |
| 54 | // The actual resource we need to set varies depending on which flavour of |
| 55 | // unix. On Linux we need RLIMIT_AS because that covers the use of mmap. |
| 56 | // Otherwise hopefully RLIMIT_RSS is good enough. (Unfortunately 64-bit |
| 57 | // and 32-bit headers disagree on the type of these constants!) |
| 58 | #ifdef RLIMIT_AS |
| 59 | #define USE_RESOURCE RLIMIT_AS |
| 60 | #else |
| 61 | #define USE_RESOURCE RLIMIT_RSS |
| 62 | #endif |
| 63 | |
| 64 | // Restrict the test to 1GiB, which should fit comfortably well on both |
| 65 | // 32-bit and 64-bit hosts, and executes in ~1s. |
| 66 | const rlim_t kMaxMem = 1<<30; |
| 67 | |
| 68 | struct rlimit rlim; |
| 69 | if (getrlimit(USE_RESOURCE, &rlim) == 0) { |
| 70 | if (rlim.rlim_cur == RLIM_INFINITY || rlim.rlim_cur > kMaxMem) { |
| 71 | rlim.rlim_cur = kMaxMem; |
| 72 | setrlimit(USE_RESOURCE, &rlim); // ignore result |
| 73 | } |
| 74 | } |
| 75 | #endif /* HAVE_SYS_RESOURCE_H */ |
| 76 | } |
| 77 | |
| 78 | |
| 79 | struct FunctionAndId { |
| 80 | void (*ptr_to_function)(int); |
| 81 | int id; |
| 82 | }; |
| 83 | |
| 84 | #if defined(NO_THREADS) || !(defined(HAVE_PTHREAD) || defined(_WIN32)) |
| 85 | |
| 86 | extern "C" void RunThread(void (*fn)()) { |
| 87 | (*fn)(); |
| 88 | } |
| 89 | |
| 90 | extern "C" void RunManyThreads(void (*fn)(), int count) { |
| 91 | // I guess the best we can do is run fn sequentially, 'count' times |
| 92 | for (int i = 0; i < count; i++) |
| 93 | (*fn)(); |
| 94 | } |
| 95 | |
| 96 | extern "C" void RunManyThreadsWithId(void (*fn)(int), int count, int) { |
| 97 | for (int i = 0; i < count; i++) |
| 98 | (*fn)(i); // stacksize doesn't make sense in a non-threaded context |
| 99 | } |
| 100 | |
| 101 | #elif defined(_WIN32) |
| 102 | |
| 103 | #ifndef WIN32_LEAN_AND_MEAN |
| 104 | #define WIN32_LEAN_AND_MEAN /* We always want minimal includes */ |
| 105 | #endif |
| 106 | #include <windows.h> |
| 107 | |
| 108 | extern "C" { |
| 109 | // This helper function has the signature that pthread_create wants. |
| 110 | DWORD WINAPI RunFunctionInThread(LPVOID ptr_to_ptr_to_fn) { |
| 111 | (**static_cast<void (**)()>(ptr_to_ptr_to_fn))(); // runs fn |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | DWORD WINAPI RunFunctionInThreadWithId(LPVOID ptr_to_fnid) { |
| 116 | FunctionAndId* fn_and_id = static_cast<FunctionAndId*>(ptr_to_fnid); |
| 117 | (*fn_and_id->ptr_to_function)(fn_and_id->id); // runs fn |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | void RunManyThreads(void (*fn)(), int count) { |
| 122 | DWORD dummy; |
| 123 | HANDLE* hThread = new HANDLE[count]; |
| 124 | for (int i = 0; i < count; i++) { |
| 125 | hThread[i] = CreateThread(NULL, 0, RunFunctionInThread, &fn, 0, &dummy); |
| 126 | if (hThread[i] == NULL) ExitProcess(i); |
| 127 | } |
| 128 | WaitForMultipleObjects(count, hThread, TRUE, INFINITE); |
| 129 | for (int i = 0; i < count; i++) { |
| 130 | CloseHandle(hThread[i]); |
| 131 | } |
| 132 | delete[] hThread; |
| 133 | } |
| 134 | |
| 135 | void RunThread(void (*fn)()) { |
| 136 | RunManyThreads(fn, 1); |
| 137 | } |
| 138 | |
| 139 | void RunManyThreadsWithId(void (*fn)(int), int count, int stacksize) { |
| 140 | DWORD dummy; |
| 141 | HANDLE* hThread = new HANDLE[count]; |
| 142 | FunctionAndId* fn_and_ids = new FunctionAndId[count]; |
| 143 | for (int i = 0; i < count; i++) { |
| 144 | fn_and_ids[i].ptr_to_function = fn; |
| 145 | fn_and_ids[i].id = i; |
| 146 | hThread[i] = CreateThread(NULL, stacksize, RunFunctionInThreadWithId, |
| 147 | &fn_and_ids[i], 0, &dummy); |
| 148 | if (hThread[i] == NULL) ExitProcess(i); |
| 149 | } |
| 150 | WaitForMultipleObjects(count, hThread, TRUE, INFINITE); |
| 151 | for (int i = 0; i < count; i++) { |
| 152 | CloseHandle(hThread[i]); |
| 153 | } |
| 154 | delete[] fn_and_ids; |
| 155 | delete[] hThread; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | #else // not NO_THREADS, not !HAVE_PTHREAD, not _WIN32 |
| 160 | |
| 161 | #include <pthread.h> |
| 162 | |
| 163 | #define SAFE_PTHREAD(fncall) do { if ((fncall) != 0) abort(); } while (0) |
| 164 | |
| 165 | extern "C" { |
| 166 | // This helper function has the signature that pthread_create wants. |
| 167 | static void* RunFunctionInThread(void *ptr_to_ptr_to_fn) { |
| 168 | (**static_cast<void (**)()>(ptr_to_ptr_to_fn))(); // runs fn |
| 169 | return NULL; |
| 170 | } |
| 171 | |
| 172 | static void* RunFunctionInThreadWithId(void *ptr_to_fnid) { |
| 173 | FunctionAndId* fn_and_id = static_cast<FunctionAndId*>(ptr_to_fnid); |
| 174 | (*fn_and_id->ptr_to_function)(fn_and_id->id); // runs fn |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | // Run a function in a thread of its own and wait for it to finish. |
| 179 | // This is useful for tcmalloc testing, because each thread is |
| 180 | // handled separately in tcmalloc, so there's interesting stuff to |
| 181 | // test even if the threads are not running concurrently. |
| 182 | void RunThread(void (*fn)()) { |
| 183 | pthread_t thr; |
| 184 | // Even though fn is on the stack, it's safe to pass a pointer to it, |
| 185 | // because we pthread_join immediately (ie, before RunInThread exits). |
| 186 | SAFE_PTHREAD(pthread_create(&thr, NULL, RunFunctionInThread, &fn)); |
| 187 | SAFE_PTHREAD(pthread_join(thr, NULL)); |
| 188 | } |
| 189 | |
| 190 | void RunManyThreads(void (*fn)(), int count) { |
| 191 | pthread_t* thr = new pthread_t[count]; |
| 192 | for (int i = 0; i < count; i++) { |
| 193 | SAFE_PTHREAD(pthread_create(&thr[i], NULL, RunFunctionInThread, &fn)); |
| 194 | } |
| 195 | for (int i = 0; i < count; i++) { |
| 196 | SAFE_PTHREAD(pthread_join(thr[i], NULL)); |
| 197 | } |
| 198 | delete[] thr; |
| 199 | } |
| 200 | |
| 201 | void RunManyThreadsWithId(void (*fn)(int), int count, int stacksize) { |
| 202 | pthread_attr_t attr; |
| 203 | pthread_attr_init(&attr); |
| 204 | pthread_attr_setstacksize(&attr, stacksize); |
| 205 | |
| 206 | pthread_t* thr = new pthread_t[count]; |
| 207 | FunctionAndId* fn_and_ids = new FunctionAndId[count]; |
| 208 | for (int i = 0; i < count; i++) { |
| 209 | fn_and_ids[i].ptr_to_function = fn; |
| 210 | fn_and_ids[i].id = i; |
| 211 | SAFE_PTHREAD(pthread_create(&thr[i], &attr, |
| 212 | RunFunctionInThreadWithId, &fn_and_ids[i])); |
| 213 | } |
| 214 | for (int i = 0; i < count; i++) { |
| 215 | SAFE_PTHREAD(pthread_join(thr[i], NULL)); |
| 216 | } |
| 217 | delete[] fn_and_ids; |
| 218 | delete[] thr; |
| 219 | |
| 220 | pthread_attr_destroy(&attr); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | #endif |