Skip to content

Commit b2ef08f

Browse files
fntlnzleodido
andcommitted
chore: clang format following the current style
Signed-off-by: Lorenzo Fontana <[email protected]> Co-authored-by: Leonardo Di Donato <[email protected]>
1 parent 5fdf658 commit b2ef08f

File tree

4 files changed

+131
-124
lines changed

4 files changed

+131
-124
lines changed

.clang-format

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
# This coding convention's solely goal is to approximately match the current code style.
2-
# It MUST not be intended in any other way until a real and definitive coding convention is put in.
31
---
4-
BreakBeforeBraces: GNU
5-
ColumnLimit: 0
6-
IndentWidth: 4
72
Language: Cpp
3+
BasedOnStyle: LLVM
4+
AccessModifierOffset: -8
5+
BreakBeforeBraces: Allman
6+
BreakConstructorInitializers: AfterColon
7+
ColumnLimit: 0
8+
ConstructorInitializerIndentWidth: 8
9+
ContinuationIndentWidth: 8
10+
DerivePointerAlignment: true
11+
IndentWidth: 8
12+
SortIncludes: false
13+
SpaceAfterTemplateKeyword: false
14+
SpaceBeforeCtorInitializerColon: false
815
SpaceBeforeParens: Never
9-
Standard: Auto
10-
UseTab: Always
16+
UseTab: Always

tests/engine/test_token_bucket.cpp

+41-41
Original file line numberDiff line numberDiff line change
@@ -23,61 +23,61 @@ using namespace Catch::literals;
2323

2424
TEST_CASE("token bucket default ctor", "[token_bucket]")
2525
{
26-
auto tb = new token_bucket();
27-
28-
REQUIRE(tb->get_tokens() == 1);
29-
30-
SECTION("initialising with specific time, rate 2 tokens/sec")
31-
{
32-
auto max = 2.0;
33-
uint64_t now = 1;
34-
tb->init(1.0, max, now);
35-
REQUIRE(tb->get_last_seen() == now);
36-
REQUIRE(tb->get_tokens() == max);
37-
}
26+
auto tb = new token_bucket();
27+
28+
REQUIRE(tb->get_tokens() == 1);
29+
30+
SECTION("initialising with specific time, rate 2 tokens/sec")
31+
{
32+
auto max = 2.0;
33+
uint64_t now = 1;
34+
tb->init(1.0, max, now);
35+
REQUIRE(tb->get_last_seen() == now);
36+
REQUIRE(tb->get_tokens() == max);
37+
}
3838
}
3939

4040
TEST_CASE("token bucket ctor with custom timer", "[token_bucket]")
4141
{
42-
auto t = []() -> uint64_t { return 22; };
43-
auto tb = new token_bucket(t);
42+
auto t = []() -> uint64_t { return 22; };
43+
auto tb = new token_bucket(t);
4444

45-
REQUIRE(tb->get_tokens() == 1);
46-
REQUIRE(tb->get_last_seen() == 22);
45+
REQUIRE(tb->get_tokens() == 1);
46+
REQUIRE(tb->get_last_seen() == 22);
4747
}
4848

4949
TEST_CASE("token bucket with 2 tokens/sec rate, max 10 tokens", "[token_bucket]")
5050
{
51-
auto tb = new token_bucket();
52-
tb->init(2.0, 10, 1);
53-
54-
SECTION("claiming 5 tokens")
55-
{
56-
bool claimed = tb->claim(5, 1000000001);
57-
REQUIRE(tb->get_last_seen() == 1000000001);
58-
REQUIRE(tb->get_tokens() == 5.0_a);
59-
REQUIRE(claimed);
51+
auto tb = new token_bucket();
52+
tb->init(2.0, 10, 1);
6053

61-
SECTION("claiming all the 7 remaining tokens")
54+
SECTION("claiming 5 tokens")
6255
{
63-
bool claimed = tb->claim(7, 2000000001);
64-
REQUIRE(tb->get_last_seen() == 2000000001);
65-
REQUIRE(tb->get_tokens() == 0.0_a);
66-
REQUIRE(claimed);
67-
68-
SECTION("claiming 1 token more than the 2 available fails")
69-
{
70-
bool claimed = tb->claim(3, 3000000001);
71-
REQUIRE(tb->get_last_seen() == 3000000001);
72-
REQUIRE(tb->get_tokens() == 2.0_a);
73-
REQUIRE_FALSE(claimed);
74-
}
56+
bool claimed = tb->claim(5, 1000000001);
57+
REQUIRE(tb->get_last_seen() == 1000000001);
58+
REQUIRE(tb->get_tokens() == 5.0_a);
59+
REQUIRE(claimed);
60+
61+
SECTION("claiming all the 7 remaining tokens")
62+
{
63+
bool claimed = tb->claim(7, 2000000001);
64+
REQUIRE(tb->get_last_seen() == 2000000001);
65+
REQUIRE(tb->get_tokens() == 0.0_a);
66+
REQUIRE(claimed);
67+
68+
SECTION("claiming 1 token more than the 2 available fails")
69+
{
70+
bool claimed = tb->claim(3, 3000000001);
71+
REQUIRE(tb->get_last_seen() == 3000000001);
72+
REQUIRE(tb->get_tokens() == 2.0_a);
73+
REQUIRE_FALSE(claimed);
74+
}
75+
}
7576
}
76-
}
7777
}
7878

7979
TEST_CASE("token bucket default initialization", "[token_bucket]")
8080
{
81-
token_bucket tb;
82-
REQUIRE(tb.get_tokens() == 1);
81+
token_bucket tb;
82+
REQUIRE(tb.get_tokens() == 1);
8383
}

userspace/engine/token_bucket.cpp

+26-25
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ limitations under the License.
2424
#include "token_bucket.h"
2525
#include "utils.h"
2626

27-
token_bucket::token_bucket() : token_bucket(sinsp_utils::get_current_time_ns)
27+
token_bucket::token_bucket():
28+
token_bucket(sinsp_utils::get_current_time_ns)
2829
{
2930
}
3031

3132
token_bucket::token_bucket(std::function<uint64_t()> timer)
3233
{
33-
m_timer = timer;
34-
init(1, 1);
34+
m_timer = timer;
35+
init(1, 1);
3536
}
3637

3738
token_bucket::~token_bucket()
@@ -40,51 +41,51 @@ token_bucket::~token_bucket()
4041

4142
void token_bucket::init(double rate, double max_tokens, uint64_t now)
4243
{
43-
m_rate = rate;
44-
m_max_tokens = max_tokens;
45-
m_tokens = max_tokens;
46-
m_last_seen = now == 0 ? m_timer() : now;
44+
m_rate = rate;
45+
m_max_tokens = max_tokens;
46+
m_tokens = max_tokens;
47+
m_last_seen = now == 0 ? m_timer() : now;
4748
}
4849

4950
bool token_bucket::claim()
5051
{
51-
return claim(1, m_timer());
52+
return claim(1, m_timer());
5253
}
5354

5455
bool token_bucket::claim(double tokens, uint64_t now)
5556
{
56-
double tokens_gained = m_rate * ((now - m_last_seen) / (1000000000.0));
57-
m_last_seen = now;
57+
double tokens_gained = m_rate * ((now - m_last_seen) / (1000000000.0));
58+
m_last_seen = now;
5859

59-
m_tokens += tokens_gained;
60+
m_tokens += tokens_gained;
6061

61-
//
62-
// Cap at max_tokens
63-
//
64-
if(m_tokens > m_max_tokens)
62+
//
63+
// Cap at max_tokens
64+
//
65+
if(m_tokens > m_max_tokens)
6566
{
66-
m_tokens = m_max_tokens;
67+
m_tokens = m_max_tokens;
6768
}
6869

69-
//
70-
// If m_tokens is < tokens, can't claim.
71-
//
72-
if(m_tokens < tokens)
70+
//
71+
// If m_tokens is < tokens, can't claim.
72+
//
73+
if(m_tokens < tokens)
7374
{
74-
return false;
75+
return false;
7576
}
7677

77-
m_tokens -= tokens;
78+
m_tokens -= tokens;
7879

79-
return true;
80+
return true;
8081
}
8182

8283
double token_bucket::get_tokens()
8384
{
84-
return m_tokens;
85+
return m_tokens;
8586
}
8687

8788
uint64_t token_bucket::get_last_seen()
8889
{
89-
return m_last_seen;
90+
return m_last_seen;
9091
}

userspace/engine/token_bucket.h

+51-51
Original file line numberDiff line numberDiff line change
@@ -26,55 +26,55 @@ limitations under the License.
2626
// for limited bursting in the form of "banked" tokens.
2727
class token_bucket
2828
{
29-
public:
30-
token_bucket();
31-
token_bucket(std::function<uint64_t()> timer);
32-
virtual ~token_bucket();
33-
34-
//
35-
// Initialize the token bucket and start accumulating tokens
36-
//
37-
void init(double rate, double max_tokens, uint64_t now = 0);
38-
39-
//
40-
// Try to claim tokens tokens from the token bucket, using a
41-
// timestamp of now. Returns true if the tokens could be
42-
// claimed. Also updates internal metrics.
43-
//
44-
bool claim(double tokens, uint64_t now);
45-
46-
// Simpler version of claim that claims a single token and
47-
// uses the current time for now
48-
bool claim();
49-
50-
// Return the current number of tokens available
51-
double get_tokens();
52-
53-
// Return the last time someone tried to claim a token.
54-
uint64_t get_last_seen();
55-
56-
private:
57-
std::function<uint64_t()> m_timer;
58-
59-
//
60-
// The number of tokens generated per second.
61-
//
62-
double m_rate;
63-
64-
//
65-
// The maximum number of tokens that can be banked for future
66-
// claim()s.
67-
//
68-
double m_max_tokens;
69-
70-
//
71-
// The current number of tokens
72-
//
73-
double m_tokens;
74-
75-
//
76-
// The last time claim() was called (or the object was created).
77-
// Nanoseconds since the epoch.
78-
//
79-
uint64_t m_last_seen;
29+
public:
30+
token_bucket();
31+
token_bucket(std::function<uint64_t()> timer);
32+
virtual ~token_bucket();
33+
34+
//
35+
// Initialize the token bucket and start accumulating tokens
36+
//
37+
void init(double rate, double max_tokens, uint64_t now = 0);
38+
39+
//
40+
// Try to claim tokens tokens from the token bucket, using a
41+
// timestamp of now. Returns true if the tokens could be
42+
// claimed. Also updates internal metrics.
43+
//
44+
bool claim(double tokens, uint64_t now);
45+
46+
// Simpler version of claim that claims a single token and
47+
// uses the current time for now
48+
bool claim();
49+
50+
// Return the current number of tokens available
51+
double get_tokens();
52+
53+
// Return the last time someone tried to claim a token.
54+
uint64_t get_last_seen();
55+
56+
private:
57+
std::function<uint64_t()> m_timer;
58+
59+
//
60+
// The number of tokens generated per second.
61+
//
62+
double m_rate;
63+
64+
//
65+
// The maximum number of tokens that can be banked for future
66+
// claim()s.
67+
//
68+
double m_max_tokens;
69+
70+
//
71+
// The current number of tokens
72+
//
73+
double m_tokens;
74+
75+
//
76+
// The last time claim() was called (or the object was created).
77+
// Nanoseconds since the epoch.
78+
//
79+
uint64_t m_last_seen;
8080
};

0 commit comments

Comments
 (0)