On Thu, 24 Nov 2005, Qingqing Zhou wrote:
>
> I may need to write some separate tests to see if this is what we should
> pay for bus lock instruction.
>

Here I come up with a test program to see how spinlock costs:

$/pgsql/src/backend/storage/lmgr#./a.out
Spinlock pair(2648542) duration: 143.134 ms
$/pgsql/src/backend/storage/lmgr#./a.out
Spinlock pair(2648542) duration: 143.107 ms
$/pgsql/src/backend/storage/lmgr#./a.out
Spinlock pair(2648542) duration: 143.104 ms

So seems lock instruction really costs ...

Regards,
Qingqing

---

/*
 * spintest.c -
 *      Test spinlock acquire/release without concurrency.
 *
 *      To compile (the -pg is to match the gprof make I used):
 *              backend/storage/lmgr#gcc -O2 -pg -Wall -I ../../../include/ 
spintest.c
 */

#include "postgres.h"
#include "storage/lwlock.h"
#include "storage/spin.h"
#include <sys/time.h>

#define TIMES   2648542

int NumLocks = 0;

void
s_lock(volatile slock_t *lock, const char *file, int line)
{
        fprintf(stderr, "should never be here\n");
        abort();
}

int
main(void)
{
        int     i;
        slock_t lock = 0;
        struct timeval start_t, stop_t;
        long usecs;

        gettimeofday(&start_t, NULL);
        for (i = 0; i < TIMES; i ++)
        {
                SpinLockAcquire_NoHoldoff(&lock);

                /* pretend to do something */
                NumLocks ++;

                SpinLockRelease_NoHoldoff(&lock);
        }
        gettimeofday(&stop_t, NULL);


        if (stop_t.tv_usec < start_t.tv_usec)
        {
                stop_t.tv_sec--;
                stop_t.tv_usec += 1000000;
        }

        usecs = (long) (stop_t.tv_sec - start_t.tv_sec) * 1000000
                + (long) (stop_t.tv_usec - start_t.tv_usec);

        fprintf (stdout, "Spinlock pair(%u) duration: %ld.%03ld ms\n",
                TIMES,
                (long) ((stop_t.tv_sec - start_t.tv_sec) * 1000 +
                        (stop_t.tv_usec - start_t.tv_usec) / 1000),
                (long) (stop_t.tv_usec - start_t.tv_usec) % 1000);

        return 0;
}

---------------------------(end of broadcast)---------------------------
TIP 4: Have you searched our list archives?

               http://archives.postgresql.org

Reply via email to