bthread_futex_unittest.cpp 5.34 KB
Newer Older
gejun's avatar
gejun committed
1
// Copyright (c) 2014 baidu-rpc authors.
gejun's avatar
gejun committed
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
// Author: Ge,Jun (gejun@baidu.com)

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <gtest/gtest.h>
#include "base/time.h"
#include "base/macros.h"
#include "base/errno.h"
#include <syscall.h>
#include <limits.h>                            // INT_MAX
#include "base/atomicops.h"
#include "bthread/bthread.h"
#include <bthread/sys_futex.h>
#include <bthread/processor.h>

namespace {
volatile bool stop = false;

base::atomic<int> nthread(0);

void* read_thread(void* arg) {
    base::atomic<int>* m = (base::atomic<int>*)arg;
    int njob = 0;
    while (!stop) {
        int x;
        while (!stop && (x = *m) != 0) {
            if (x > 0) {
                while ((x = m->fetch_sub(1)) > 0) {
                    ++njob;
                    const long start = base::cpuwide_time_ns();
                    while (base::cpuwide_time_ns() < start + 10000) {
                    }
                    if (stop) {
                        return new int(njob);
                    }
                }
                m->fetch_add(1);
            } else {
                cpu_relax();
            }
        }

        ++nthread;
        bthread::futex_wait_private(m/*lock1*/, 0/*consumed_njob*/, NULL);
        --nthread;
    }
    return new int(njob);
}

TEST(FutexTest, rdlock_performance) {
    const size_t N = 100000;
    base::atomic<int> lock1(0);
    pthread_t rth[8];
    for (size_t i = 0; i < ARRAY_SIZE(rth); ++i) {
        ASSERT_EQ(0, pthread_create(&rth[i], NULL, read_thread, &lock1));
    }

    const size_t t1 = base::cpuwide_time_ns();
    for (size_t i = 0; i < N; ++i) {
        if (nthread) {
            lock1.fetch_add(1);
            bthread::futex_wake_private(&lock1, 1);
        } else {
            lock1.fetch_add(1);
            if (nthread) {
                bthread::futex_wake_private(&lock1, 1);
            }
        }
    }
    const size_t t2 = base::cpuwide_time_ns();

    bthread_usleep(3000000);
    stop = true;
    for (int i = 0; i < 10; ++i) {
        bthread::futex_wake_private(&lock1, INT_MAX);
        sched_yield();
    }

    int njob = 0;
    int* res;
    for (size_t i = 0; i < ARRAY_SIZE(rth); ++i) {
        pthread_join(rth[i], (void**)&res);
        njob += *res;
        delete res;
    }
    printf("wake %lu times, %ldns each, lock1=%d njob=%d\n",
           N, (t2-t1)/N, lock1.load(), njob);
    ASSERT_EQ(N, (size_t)(lock1.load() + njob));
}

TEST(FutexTest, futex_wake_before_wait) {
    int lock1 = 0;
    timespec timeout = { 1, 0 };
    ASSERT_EQ(0, bthread::futex_wake_private(&lock1, INT_MAX));
    ASSERT_EQ(-1, bthread::futex_wait_private(&lock1, 0, &timeout));
    ASSERT_EQ(ETIMEDOUT, errno);
}

void* dummy_waiter(void* lock) {
    bthread::futex_wait_private(lock, 0, NULL);
    return NULL;
}

TEST(FutexTest, futex_wake_many_waiters_perf) {
    
    int lock1 = 0;
    size_t N = 0;
    pthread_t th;
    for (; N < 1000 && !pthread_create(&th, NULL, dummy_waiter, &lock1); ++N) {}
    
    sleep(1);
    int nwakeup = 0;
    base::Timer tm;
    tm.start();
    for (size_t i = 0; i < N; ++i) {
        nwakeup += bthread::futex_wake_private(&lock1, 1);
    }
    tm.stop();
    printf("N=%lu, futex_wake a thread = %ldns\n", N, tm.n_elapsed() / N);
    ASSERT_EQ(N, (size_t)nwakeup);

    const size_t REP = 10000;
    nwakeup = 0;
    tm.start();
    for (size_t i = 0; i < REP; ++i) {
        nwakeup += bthread::futex_wake_private(&lock1, 1);
    }
    tm.stop();
    ASSERT_EQ(0, nwakeup);
    printf("futex_wake nop = %ldns\n", tm.n_elapsed() / REP);
}

base::atomic<int> nevent(0);

void* waker(void* lock) {
    bthread_usleep(10000);
    const size_t REP = 100000;
    int nwakeup = 0;
    base::Timer tm;
    tm.start();
    for (size_t i = 0; i < REP; ++i) {
        nwakeup += bthread::futex_wake_private(lock, 1);
    }
    tm.stop();
    EXPECT_EQ(0, nwakeup);
    printf("futex_wake nop = %ldns\n", tm.n_elapsed() / REP);
    return NULL;
} 

void* batch_waker(void* lock) {
    bthread_usleep(10000);
    const size_t REP = 100000;
    int nwakeup = 0;
    base::Timer tm;
    tm.start();
    for (size_t i = 0; i < REP; ++i) {
        if (nevent.fetch_add(1, base::memory_order_relaxed) == 0) {
            nwakeup += bthread::futex_wake_private(lock, 1);
            int expected = 1;
            while (1) {
                int last_expected = expected;
                if (nevent.compare_exchange_strong(expected, 0, base::memory_order_relaxed)) {
                    break;
                }
                nwakeup += bthread::futex_wake_private(lock, expected - last_expected);
            }
        }
    }
    tm.stop();
    EXPECT_EQ(0, nwakeup);
    printf("futex_wake nop = %ldns\n", tm.n_elapsed() / REP);
    return NULL;
} 

TEST(FutexTest, many_futex_wake_nop_perf) {
    pthread_t th[8];
    int lock1;
    std::cout << "[Direct wake]" << std::endl;
    for (size_t i = 0; i < ARRAY_SIZE(th); ++i) {
        ASSERT_EQ(0, pthread_create(&th[i], NULL, waker, &lock1));
    }
    for (size_t i = 0; i < ARRAY_SIZE(th); ++i) {
        ASSERT_EQ(0, pthread_join(th[i], NULL));
    }
    std::cout << "[Batch wake]" << std::endl;
    for (size_t i = 0; i < ARRAY_SIZE(th); ++i) {
        ASSERT_EQ(0, pthread_create(&th[i], NULL, batch_waker, &lock1));
    }
    for (size_t i = 0; i < ARRAY_SIZE(th); ++i) {
        ASSERT_EQ(0, pthread_join(th[i], NULL));
    }
}
} // namespace