This repository has been archived by the owner on Oct 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
103 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |