Skip to content
This repository has been archived by the owner on Oct 28, 2023. It is now read-only.

Commit

Permalink
add cpu cache counter #83
Browse files Browse the repository at this point in the history
  • Loading branch information
BohuTANG committed Sep 13, 2014
1 parent 6dc2c44 commit 3151dbc
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 9 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions include/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <sys/stat.h>
#include <pthread.h>
#include <unistd.h>
#include <sched.h>
#include "debug.h"
#include "xmalloc.h"
#include "status.h"
Expand Down
20 changes: 11 additions & 9 deletions tests/engine/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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


Expand Down
23 changes: 23 additions & 0 deletions tests/engine/counter-test.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*
*/

#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);
}
44 changes: 44 additions & 0 deletions util/counter.c
Original file line number Diff line number Diff line change
@@ -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;
}
23 changes: 23 additions & 0 deletions util/counter.h
Original file line number Diff line number Diff line change
@@ -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_ */

0 comments on commit 3151dbc

Please sign in to comment.