File: test_eh_thread.cpp

package info (click to toggle)
onetbb 2022.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,440 kB
  • sloc: cpp: 129,228; ansic: 9,745; python: 808; xml: 183; objc: 176; makefile: 66; sh: 66; awk: 41; javascript: 37
file content (127 lines) | stat: -rw-r--r-- 4,020 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
    Copyright (c) 2020-2025 Intel Corporation

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include "common/config.h"

#include "tbb/parallel_for.h"
#include "tbb/global_control.h"

#include "common/test.h"
#include "common/utils.h"
#include "common/utils_concurrency_limit.h"

#include <atomic>
#include <condition_variable>
#include <thread>
#include <vector>

//! \file test_eh_thread.cpp
//! \brief Test for [internal] functionality

// On Windows there is no real thread number limit beside available memory.
// Therefore, the test for thread limit is unreasonable.
// TODO: enable limitThreads with sanitizer under docker
#if TBB_USE_EXCEPTIONS && !_WIN32 && !__ANDROID__

#include <limits.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>

void limitThreads(size_t limit)
{
    rlimit rlim;

    int ret = getrlimit(RLIMIT_NPROC, &rlim);
    CHECK_MESSAGE(0 == ret, "getrlimit has returned an error");

    rlim.rlim_cur = (rlim.rlim_max == (rlim_t)RLIM_INFINITY) ? limit : utils::min((rlim_t)limit, rlim.rlim_max);

    ret = setrlimit(RLIMIT_NPROC, &rlim);
    CHECK_MESSAGE(0 == ret, "setrlimit has returned an error");
}

size_t getThreadLimit() {
    rlimit rlim;

    int ret = getrlimit(RLIMIT_NPROC, &rlim);
    CHECK_MESSAGE(0 == ret, "getrlimit has returned an error");
    return rlim.rlim_cur;
}

static void* thread_routine(void*)
{
    return nullptr;
}

class Thread {
    pthread_t mHandle{};
    bool mValid{};
public:
    Thread() {
        mValid = false;
        pthread_attr_t attr;
        // Limit the stack size not to consume all virtual memory on 32 bit platforms.
        std::size_t stacksize = utils::max(std::size_t(128*1024), std::size_t(PTHREAD_STACK_MIN));
        if (pthread_attr_init(&attr) == 0 && pthread_attr_setstacksize(&attr, stacksize) == 0) {
            mValid = pthread_create(&mHandle, &attr, thread_routine, /* arg = */ nullptr) == 0;
        }
    }
    bool isValid() const { return mValid; }
    void join() {
        pthread_join(mHandle, nullptr);
    }
};

//! Test for exception when too many threads
//! \brief \ref resource_usage
TEST_CASE("Too many threads") {
    if (utils::get_platform_max_threads() < 2) {
        // The test expects that the scheduler will try to create at least one thread.
        return;
    }

    // Some systems set really big limit (e.g. >45K) for the number of processes/threads
    limitThreads(1);
    if (getThreadLimit() == 1) {
        for (int attempt = 0; attempt < 5; ++attempt) {
            Thread thread;
            if (thread.isValid()) {
                WARN_MESSAGE(false, "We were able to create a thread. setrlimit seems having no effect");
                thread.join();
                return;
            }
        }
        bool g_exception_caught = false;
        try {
            // Initialize the library to create worker threads
            tbb::parallel_for(0, 2, [](int) {});
        } catch (const std::exception & e) {
            g_exception_caught = true;
            // Do not CHECK to avoid memory allocation (we can be out of memory)
            if (e.what()== nullptr) {
                FAIL("Exception does not have description");
            }
        }
        // Do not CHECK to avoid memory allocation (we can be out of memory)
        if (!g_exception_caught) {
            FAIL("No exception was thrown on library initialization");
        }
    } else {
        WARN_MESSAGE(false, "setrlimit seems having no effect");
    }
}
#endif