Skip to content

Commit

Permalink
Add Table::stats() (#4405)
Browse files Browse the repository at this point in the history
* Add Table::stats()

* Optimize code
  • Loading branch information
matyhtf authored Sep 14, 2021
1 parent e6b0cb6 commit e648f7c
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 20 deletions.
2 changes: 2 additions & 0 deletions ext-src/php_swoole.cc
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ PHP_MINIT_FUNCTION(swoole) {
SW_REGISTER_LONG_CONSTANT("SWOOLE_ERROR_WEBSOCKET_BAD_OPCODE", SW_ERROR_WEBSOCKET_BAD_OPCODE);
SW_REGISTER_LONG_CONSTANT("SWOOLE_ERROR_WEBSOCKET_UNCONNECTED", SW_ERROR_WEBSOCKET_UNCONNECTED);
SW_REGISTER_LONG_CONSTANT("SWOOLE_ERROR_WEBSOCKET_HANDSHAKE_FAILED", SW_ERROR_WEBSOCKET_HANDSHAKE_FAILED);
SW_REGISTER_LONG_CONSTANT("SWOOLE_ERROR_WEBSOCKET_PACK_FAILED", SW_ERROR_WEBSOCKET_PACK_FAILED);
SW_REGISTER_LONG_CONSTANT("SWOOLE_ERROR_SERVER_MUST_CREATED_BEFORE_CLIENT", SW_ERROR_SERVER_MUST_CREATED_BEFORE_CLIENT);
SW_REGISTER_LONG_CONSTANT("SWOOLE_ERROR_SERVER_TOO_MANY_SOCKET", SW_ERROR_SERVER_TOO_MANY_SOCKET);
SW_REGISTER_LONG_CONSTANT("SWOOLE_ERROR_SERVER_WORKER_TERMINATED", SW_ERROR_SERVER_WORKER_TERMINATED);
Expand All @@ -645,6 +646,7 @@ PHP_MINIT_FUNCTION(swoole) {
SW_REGISTER_LONG_CONSTANT("SWOOLE_ERROR_SERVER_SEND_IN_MASTER", SW_ERROR_SERVER_SEND_IN_MASTER);
SW_REGISTER_LONG_CONSTANT("SWOOLE_ERROR_SERVER_INVALID_REQUEST", SW_ERROR_SERVER_INVALID_REQUEST);
SW_REGISTER_LONG_CONSTANT("SWOOLE_ERROR_SERVER_CONNECT_FAIL", SW_ERROR_SERVER_CONNECT_FAIL);
SW_REGISTER_LONG_CONSTANT("SWOOLE_ERROR_SERVER_INVALID_COMMAND", SW_ERROR_SERVER_INVALID_COMMAND);
SW_REGISTER_LONG_CONSTANT("SWOOLE_ERROR_SERVER_WORKER_EXIT_TIMEOUT", SW_ERROR_SERVER_WORKER_EXIT_TIMEOUT);
SW_REGISTER_LONG_CONSTANT("SWOOLE_ERROR_SERVER_WORKER_ABNORMAL_PIPE_DATA", SW_ERROR_SERVER_WORKER_ABNORMAL_PIPE_DATA);
SW_REGISTER_LONG_CONSTANT("SWOOLE_ERROR_SERVER_WORKER_UNPROCESSED_DATA", SW_ERROR_SERVER_WORKER_UNPROCESSED_DATA);
Expand Down
20 changes: 19 additions & 1 deletion ext-src/swoole_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ static PHP_METHOD(swoole_table, count);
static PHP_METHOD(swoole_table, destroy);
static PHP_METHOD(swoole_table, getSize);
static PHP_METHOD(swoole_table, getMemorySize);
static PHP_METHOD(swoole_table, stats);

static PHP_METHOD(swoole_table, rewind);
static PHP_METHOD(swoole_table, next);
Expand All @@ -206,8 +207,9 @@ static const zend_function_entry swoole_table_methods[] =
PHP_MALIAS(swoole_table, exist, exists, arginfo_swoole_table_exists, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, incr, arginfo_swoole_table_incr, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, decr, arginfo_swoole_table_decr, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, getSize, arginfo_swoole_table_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, getSize, arginfo_swoole_table_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, getMemorySize, arginfo_swoole_table_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, stats, arginfo_swoole_table_void, ZEND_ACC_PUBLIC)
// implement Iterator
PHP_ME(swoole_table, rewind, arginfo_swoole_table_void, ZEND_ACC_PUBLIC)
PHP_ME(swoole_table, valid, arginfo_swoole_table_void, ZEND_ACC_PUBLIC)
Expand Down Expand Up @@ -596,6 +598,22 @@ static PHP_METHOD(swoole_table, getSize) {
}
}

static PHP_METHOD(swoole_table, stats) {
Table *table = php_swoole_table_get_ptr(ZEND_THIS);
if (!table) {
RETURN_FALSE;
}
array_init(return_value);
add_assoc_long(return_value, "num", table->count());
add_assoc_long(return_value, "conflict_count", table->conflict_count);
add_assoc_long(return_value, "conflict_max_level", table->conflict_max_level);
add_assoc_long(return_value, "insert_count", table->insert_count);
add_assoc_long(return_value, "update_count", table->update_count);
add_assoc_long(return_value, "delete_count", table->delete_count);
add_assoc_long(return_value, "available_slice_num", table->get_available_slice_num());
add_assoc_long(return_value, "total_slice_num", table->get_total_slice_num());
}

static PHP_METHOD(swoole_table, rewind) {
Table *table = php_swoole_table_get_and_check_ptr2(ZEND_THIS);
table->rewind();
Expand Down
4 changes: 3 additions & 1 deletion include/swoole_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ class FixedPool : public MemoryPool {
void *alloc(uint32_t size);
void free(void *ptr);
void debug();

uint32_t get_number_of_spare_slice();
uint32_t get_number_of_total_slice();
uint32_t get_slice_size();
static size_t sizeof_struct_slice();
static size_t sizeof_struct_impl();
};
Expand Down
17 changes: 8 additions & 9 deletions include/swoole_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,19 @@ class Table {

void *memory;

#ifdef SW_TABLE_DEBUG
int conflict_count;
int insert_count;
int conflict_max_level;
#endif

public:
std::vector<TableColumn *> *column_list;

size_t conflict_count;
sw_atomic_long_t insert_count;
sw_atomic_long_t delete_count;
sw_atomic_long_t update_count;
uint32_t conflict_max_level;

static Table *make(uint32_t rows_size, float conflict_proportion);
size_t get_memory_size();
uint32_t get_available_slice_num();
uint32_t get_total_slice_num();
bool create();
bool add_column(const std::string &name, enum TableColumn::Type type, size_t size);
TableRow *set(const char *key, uint16_t keylen, TableRow **rowlock, int *out_flags);
Expand Down Expand Up @@ -289,9 +291,6 @@ class Table {
new_row->key_len = keylen;
new_row->active = 1;
sw_atomic_fetch_add(&(row_num), 1);
#ifdef SW_TABLE_DEBUG
insert_count++;
#endif
}
};
} // namespace swoole
4 changes: 4 additions & 0 deletions src/core/error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ const char *swoole_strerror(int code) {
return "Websocket unconnected";
case SW_ERROR_WEBSOCKET_HANDSHAKE_FAILED:
return "Websocket handshake failed";
case SW_ERROR_WEBSOCKET_PACK_FAILED:
return "Websocket pack failed";
case SW_ERROR_SERVER_MUST_CREATED_BEFORE_CLIENT:
return "Server must created before client";
case SW_ERROR_SERVER_TOO_MANY_SOCKET:
Expand All @@ -185,6 +187,8 @@ const char *swoole_strerror(int code) {
return "Server invalid request";
case SW_ERROR_SERVER_CONNECT_FAIL:
return "Server connect fail";
case SW_ERROR_SERVER_INVALID_COMMAND:
return "Server invalid command";
case SW_ERROR_SERVER_WORKER_EXIT_TIMEOUT:
return "Server worker exit timeout";
case SW_ERROR_SERVER_WORKER_ABNORMAL_PIPE_DATA:
Expand Down
12 changes: 12 additions & 0 deletions src/memory/fixed_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ void FixedPoolImpl::init() {
} while (1);
}

uint32_t FixedPool::get_number_of_spare_slice() {
return impl->slice_num - impl->slice_use;
}

uint32_t FixedPool::get_number_of_total_slice() {
return impl->slice_num;
}

uint32_t FixedPool::get_slice_size() {
return impl->slice_size;
}

void *FixedPool::alloc(uint32_t size) {
FixedPoolSlice *slice;

Expand Down
26 changes: 19 additions & 7 deletions src/memory/table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ size_t Table::get_memory_size() {
return _memory_size;
}

uint32_t Table::get_available_slice_num() {
lock();
uint32_t num = pool->get_number_of_spare_slice();
unlock();
return num;
}

uint32_t Table::get_total_slice_num() {
return pool->get_number_of_total_slice();
}

bool Table::create() {
if (created) {
return false;
Expand Down Expand Up @@ -297,9 +308,7 @@ TableRow *Table::set(const char *key, uint16_t keylen, TableRow **rowlock, int *
row->lock();
int _out_flags = 0;

#ifdef SW_TABLE_DEBUG
int _conflict_level = 0;
#endif
uint32_t _conflict_level = 1;

if (row->active) {
for (;;) {
Expand All @@ -308,12 +317,10 @@ TableRow *Table::set(const char *key, uint16_t keylen, TableRow **rowlock, int *
} else if (row->next == nullptr) {
lock();
TableRow *new_row = (TableRow *) pool->alloc(0);
#ifdef SW_TABLE_DEBUG
conflict_count++;
if (_conflict_level > conflict_max_level) {
conflict_max_level = _conflict_level;
}
#endif
unlock();
if (!new_row) {
return nullptr;
Expand All @@ -326,9 +333,7 @@ TableRow *Table::set(const char *key, uint16_t keylen, TableRow **rowlock, int *
} else {
row = row->next;
_out_flags |= SW_TABLE_FLAG_CONFLICT;
#ifdef SW_TABLE_DEBUG
_conflict_level++;
#endif
}
}
} else {
Expand All @@ -340,6 +345,12 @@ TableRow *Table::set(const char *key, uint16_t keylen, TableRow **rowlock, int *
*out_flags = _out_flags;
}

if (_out_flags & SW_TABLE_FLAG_NEW_ROW) {
sw_atomic_fetch_add(&(insert_count), 1);
} else {
sw_atomic_fetch_add(&(update_count), 1);
}

return row;
}

Expand Down Expand Up @@ -398,6 +409,7 @@ bool Table::del(const char *key, uint16_t keylen) {
}

_delete_element:
sw_atomic_fetch_add(&(delete_count), 1);
sw_atomic_fetch_sub(&(row_num), 1);
row->unlock();

Expand Down
4 changes: 2 additions & 2 deletions tests/swoole_table/random_bytes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ $map = [];
while($n--) {
$key = "key-".rand(1000000, 9999999);
$value = RandStr::getBytes(rand(100*1024, 250*1024));
$data[$key] = $value;
$table->set($key, ['string' => $value]);
$map[$key] = $value;
$table->set($key, ['string' => $value]);
}

foreach($map as $k => $v) {
Expand Down
61 changes: 61 additions & 0 deletions tests/swoole_table/stats.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
--TEST--
swoole_table: stats
--SKIPIF--
<?php require __DIR__ . '/../include/skipif.inc'; ?>
--FILE--
<?php
require __DIR__ . '/../include/bootstrap.php';

use Swoole\Table;

define('N', IS_IN_TRAVIS ? 10000 : 100000);

$table = new Table(N);
$table->column('string', Table::TYPE_STRING, 256);
$table->create();

$map = [];
$keys = [];

$n = N;
while ($n--) {
$key = base64_decode(RandStr::getBytes(rand(10, 30)));
$value = RandStr::getBytes(rand(100, 250));
if ($table->set($key, ['string' => $value])) {
$map[$key] = $value;
$keys[] = $key;
}
}

$stats1 = $table->stats();

Assert::eq(count($keys), N);
Assert::eq(count(array_unique($keys)), $stats1['insert_count']);

phpt_var_dump("insert\n".str_repeat('-', 64), $stats1);

define('UPDATE_N', rand(100, 1000));

$_n = UPDATE_N;
while ($_n--) {
$key = array_rand($map);
$value = RandStr::getBytes(rand(100, 250));
Assert::true($table->set($key, ['string' => $value]));
$map[$key] = $value;
}

$stats2 = $table->stats();
Assert::eq($stats1['update_count'] + UPDATE_N, $stats2['update_count']);
phpt_var_dump("update\n" . str_repeat('-', 64), $stats2);

foreach($map as $k => $v) {
Assert::same($table->get($k)['string'], $v);
$table->del($k);
}

$stats3 = $table->stats();
Assert::eq($stats3['num'], 0);
Assert::eq($stats3['available_slice_num'], $stats3['total_slice_num']);
phpt_var_dump("delete\n" . str_repeat('-', 64), $stats3);
?>
--EXPECT--
8 changes: 8 additions & 0 deletions tools/build-library.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,15 @@
'alias_ns.php',
];

$ignore_files = ['vendor_init.php',];

$diff_files = array_diff(swoole_library_files(), $files);
foreach ($diff_files as $k => $f) {
if (in_array($f, $ignore_files)) {
unset($diff_files[$k]);
}
}

if (!empty($diff_files)) {
swoole_error('Some files are not loaded: ', ...$diff_files);
}
Expand Down

0 comments on commit e648f7c

Please sign in to comment.