Hi,

The attached patch defines PG_HAVE_8BYTE_SINGLE_COPY_ATOMICITY on
64-bit LoongArch, by adding src/include/port/atomics/arch-loongarch.h
and including it from atomics.h.  

On LoongArch64 a naturally aligned 8-byte load or store is single-copy
atomic, which is the same property the existing x86-64, aarch64 and
powerpc implementations rely on.  GCC and LLVM already lower aligned
8-byte atomic loads/stores to a single ld.d/st.d on this target.
The macro only enables the plain load/store path for
pg_atomic_read_u64()/pg_atomic_write_u64(); ordering and RMW
semantics are unchanged.

The effect is most visible in BufferGetLSNAtomic().  When hint bits
cannot be set yet (data checksums or wal_log_hints enabled, and the
commit record is still in WAL), every tuple visibility check calls
BufferGetLSNAtomic().  Without the patch that takes the buffer header
spinlock; with the patch it is a single 8-byte load.

I measured this on a Loongson 3C6000 , comparing the same tree
with the macro disabled and enabled.  The setup is:

    initdb -k -D /tmp/pgdata
    pg_ctl -D /tmp/pgdata -l /tmp/pg.log -o \
      "-c synchronous_commit=off -c wal_writer_delay=10s" start

    psql -q -d postgres -c \
      "CREATE TABLE hint_t (id int PRIMARY KEY, v int NOT NULL DEFAULT 0)
         WITH (fillfactor = 50)"

    # Reload once per second.  TRUNCATE and INSERT must be separate
    # transactions: TRUNCATE flushes WAL, and if both statements are in
    # one transaction the INSERT commit is flushed too, so hint bits can
    # be set and the path is not exercised.
    while true; do
      psql -q -d postgres -c 'TRUNCATE hint_t' \
           -c 'INSERT INTO hint_t
                 SELECT g, 0 FROM generate_series(1, 1000) g'
      sleep 1
    done &

    echo 'SELECT count(*) FROM hint_t;' > /tmp/scan.sql
    pgbench -d postgres -n -M prepared -c 24 -j 12 -T 20 -f /tmp/scan.sql
    kill %1

With 24 clients and a 20-second run:

    unpatched:  ~21,000 tps
    patched:   ~143,000 tps

perf on the server shows LockBufHdr at 67.7% of samples without the
patch and below the noise floor with it, while both builds call
BufferGetLSNAtomic() exactly 1000 times per transaction.

Regards,
Changqi Hu


Attachment: 0001-add-64-bit-atomic-access-on-loongarch64.patch
Description: Binary data

Reply via email to