diff --git a/Makefile b/Makefile index 2b052bba..e7efd73c 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,7 @@ LIB_OBJS = \ ./util/file.o \ ./util/debug.o \ ./util/pma.o \ + ./util/counter.o \ ./txn/txnmgr.o \ ./txn/txn.o \ ./txn/rollback.o \ diff --git a/include/internal.h b/include/internal.h index 0f4f8fb2..4a7eb991 100644 --- a/include/internal.h +++ b/include/internal.h @@ -18,6 +18,7 @@ #include #include #include +#include #include "debug.h" #include "xmalloc.h" #include "status.h" diff --git a/tests/engine/Makefile b/tests/engine/Makefile index bd0ac462..812ff635 100644 --- a/tests/engine/Makefile +++ b/tests/engine/Makefile @@ -14,10 +14,10 @@ CFLAGS = $(INCLUDES) $(PLATFORM_SHARED_LDFLAGS) $(PLATFORM_SHARED_CFLAGS) $(OPT LIB_OBJS = \ $(top_builddir)/tree/compress/compress.o \ - $(top_builddir)/tree/compress/snappy.o \ + $(top_builddir)/tree/compress/snappy.o \ $(top_builddir)/tree/tree-func.o \ - $(top_builddir)/tree/flusher.o \ - $(top_builddir)/tree/msgpack.o \ + $(top_builddir)/tree/flusher.o \ + $(top_builddir)/tree/msgpack.o \ $(top_builddir)/tree/block.o \ $(top_builddir)/tree/layout.o \ $(top_builddir)/tree/hdr.o \ @@ -28,23 +28,25 @@ LIB_OBJS = \ $(top_builddir)/tree/lmb.o \ $(top_builddir)/tree/msg.o \ $(top_builddir)/tree/mb.o \ - $(top_builddir)/util/xmalloc.o \ - $(top_builddir)/util/mempool.o \ - $(top_builddir)/util/kibbutz.o \ + $(top_builddir)/util/comparator.o \ + $(top_builddir)/util/xmalloc.o \ + $(top_builddir)/util/mempool.o \ + $(top_builddir)/util/kibbutz.o \ $(top_builddir)/util/posix.o \ $(top_builddir)/util/crc32.o \ $(top_builddir)/util/file.o \ $(top_builddir)/util/debug.o \ $(top_builddir)/util/pma.o \ + $(top_builddir)/util/counter.o \ $(top_builddir)/txn/txnmgr.o \ $(top_builddir)/txn/txn.o \ - $(top_builddir)/txn/rollback.o \ + $(top_builddir)/txn/rollback.o \ $(top_builddir)/cache/cache.o \ $(top_builddir)/db/db.o -TEST_OBJS = \ - pma-test.o \ +TEST_OBJS = \ + counter-test.o \ main.o diff --git a/tests/engine/counter-test.c b/tests/engine/counter-test.c new file mode 100644 index 00000000..22a811d7 --- /dev/null +++ b/tests/engine/counter-test.c @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2012-2014 The nessDB Authors. All rights reserved. + * Code is licensed with GPL. See COPYING.GPL file. + * + * Author: BohuTANG + * + */ + +#include "counter.h" +#include "../ctest.h" + +CTEST(counter, inrc) +{ + struct counter *c = counter_new(128); + + counter_incr(c); + counter_incr(c); + + uint64_t all = counter_all(c); + ASSERT_EQUAL(2, all); + + counter_free(c); +} diff --git a/util/counter.c b/util/counter.c new file mode 100644 index 00000000..4d80c981 --- /dev/null +++ b/util/counter.c @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2012-2014 The nessDB Project Developers. All rights reserved. + * Code is licensed with GPL. See COPYING.GPL file. + * + */ + +#include "counter.h" + +struct counter *counter_new(int cpus) +{ + struct counter *c = xcalloc(1, sizeof(*c)); + + c->cpus = cpus; + c->per_cpu_counter = xcalloc(cpus, sizeof(uint64_t)); + + return c; +} + +void counter_free(struct counter *c) +{ + xfree(c->per_cpu_counter); + xfree(c); +} + + +void counter_incr(struct counter *c) +{ + int cpu = sched_getcpu(); + + nassert(cpu < c->cpus); + c->per_cpu_counter[cpu]++; +} + +uint64_t counter_all(struct counter *c) +{ + int i; + uint64_t all = 0UL; + + for ( i = 0; i < c->cpus; i++) { + all += c->per_cpu_counter[i]; + } + + return all; +} diff --git a/util/counter.h b/util/counter.h new file mode 100644 index 00000000..91c64147 --- /dev/null +++ b/util/counter.h @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2012-2014 The nessDB Project Developers. All rights reserved. + * Code is licensed with GPL. See COPYING.GPL file. + * + */ + +#ifndef nessDB_COUNTER_H_ +#define nessDB_COUNTER_H_ + +#include "internal.h" + +struct counter { + int cpus; + uint64_t *per_cpu_counter; +}; + +struct counter *counter_new(int cpus); +void counter_free(struct counter *); + +void counter_incr(struct counter *); +uint64_t counter_all(struct counter *); + +#endif /* nessDB_COUNTER_H_ */