bthread_setconcurrency_unittest.cpp 3.52 KB
Newer Older
gejun's avatar
gejun committed
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
// Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
// Author: Ge,Jun (gejun@baidu.com)
// Date: Sun Jul 13 15:04:18 CST 2014

#include <gtest/gtest.h>
#include "base/atomicops.h"
#include "base/time.h"
#include "base/macros.h"
#include "base/logging.h"
#include "base/thread_local.h"
#include <bthread/butex.h>
#include "base/logging.h"
#include "bthread/bthread.h"

namespace {
void* dummy(void*) {
    return NULL;
}

TEST(BthreadTest, setconcurrency) {
    ASSERT_EQ(8 + BTHREAD_EPOLL_THREAD_NUM, (size_t)bthread_getconcurrency());
    ASSERT_EQ(EINVAL, bthread_setconcurrency(BTHREAD_MIN_CONCURRENCY - 1));
    ASSERT_EQ(EINVAL, bthread_setconcurrency(0));
    ASSERT_EQ(EINVAL, bthread_setconcurrency(-1));
    ASSERT_EQ(EINVAL, bthread_setconcurrency(BTHREAD_MAX_CONCURRENCY + 1));
    ASSERT_EQ(0, bthread_setconcurrency(BTHREAD_MIN_CONCURRENCY));
    ASSERT_EQ(BTHREAD_MIN_CONCURRENCY, bthread_getconcurrency());
    ASSERT_EQ(0, bthread_setconcurrency(BTHREAD_MIN_CONCURRENCY + 1));
    ASSERT_EQ(BTHREAD_MIN_CONCURRENCY + 1, bthread_getconcurrency());
    ASSERT_EQ(0, bthread_setconcurrency(BTHREAD_MIN_CONCURRENCY));  // smaller value
    bthread_t th;
    ASSERT_EQ(0, bthread_start_urgent(&th, NULL, dummy, NULL));
    ASSERT_EQ(BTHREAD_MIN_CONCURRENCY + 1, bthread_getconcurrency());
    ASSERT_EQ(0, bthread_setconcurrency(BTHREAD_MIN_CONCURRENCY + 5));
    ASSERT_EQ(BTHREAD_MIN_CONCURRENCY + 5, bthread_getconcurrency());
    ASSERT_EQ(EPERM, bthread_setconcurrency(BTHREAD_MIN_CONCURRENCY + 1));
    ASSERT_EQ(BTHREAD_MIN_CONCURRENCY + 5, bthread_getconcurrency());
}

static base::atomic<int> *odd;
static base::atomic<int> *even;

gejun's avatar
gejun committed
43 44
static base::atomic<int> nbthreads(0);
static base::atomic<int> npthreads(0);
gejun's avatar
gejun committed
45 46 47 48
static BAIDU_THREAD_LOCAL bool counted = false;
static base::atomic<bool> stop (false);

static void *odd_thread(void *) {
gejun's avatar
gejun committed
49
    nbthreads.fetch_add(1);
gejun's avatar
gejun committed
50 51 52
    while (!stop) {
        if (!counted) {
            counted = true;
gejun's avatar
gejun committed
53
            npthreads.fetch_add(1);
gejun's avatar
gejun committed
54 55 56 57 58 59 60 61
        }
        bthread::butex_wake_all(even);
        bthread::butex_wait(odd, 0, NULL);
    }
    return NULL;
}

static void *even_thread(void *) {
gejun's avatar
gejun committed
62
    nbthreads.fetch_add(1);
gejun's avatar
gejun committed
63 64 65
    while (!stop) {
        if (!counted) {
            counted = true;
gejun's avatar
gejun committed
66
            npthreads.fetch_add(1);
gejun's avatar
gejun committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80
        }
        bthread::butex_wake_all(odd);
        bthread::butex_wait(even, 0, NULL);
    }
    return NULL;
}

TEST(BthreadTest, setconcurrency_with_running_bthread) {
    odd = bthread::butex_create_checked<base::atomic<int> >();
    even = bthread::butex_create_checked<base::atomic<int> >();
    ASSERT_TRUE(odd != NULL && even != NULL);
    *odd = 0;
    *even = 0;
    std::vector<bthread_t> tids;
gejun's avatar
gejun committed
81
    const int N = 500;
gejun's avatar
gejun committed
82 83 84 85 86 87 88 89 90 91 92
    for (int i = 0; i < N; ++i) {
        bthread_t tid;
        bthread_start_background(&tid, &BTHREAD_ATTR_SMALL, odd_thread, NULL);
        tids.push_back(tid);
        bthread_start_background(&tid, &BTHREAD_ATTR_SMALL, even_thread, NULL);
        tids.push_back(tid);
    }
    for (int i = 100; i <= N; ++i) {
        ASSERT_EQ(0, bthread_setconcurrency(i));
        ASSERT_EQ(i, bthread_getconcurrency());
    }
gejun's avatar
gejun committed
93
    usleep(1000 * N);
gejun's avatar
gejun committed
94 95 96 97 98 99 100 101 102
    *odd = 1;
    *even = 1;
    stop =  true;
    bthread::butex_wake_all(odd);
    bthread::butex_wake_all(even);
    for (size_t i = 0; i < tids.size(); ++i) {
        bthread_join(tids[i], NULL);
    }
    LOG(INFO) << "All bthreads has quit";
gejun's avatar
gejun committed
103 104 105 106
    ASSERT_EQ(2*N, nbthreads);
    // This is not necessarily true, not all workers need to run sth.
    //ASSERT_EQ(N, npthreads);
    LOG(INFO) << "Touched pthreads=" << npthreads;
gejun's avatar
gejun committed
107 108
}
} // namespace