Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
oceanbase
miniob
提交
e976988d
M
miniob
项目概览
oceanbase
/
miniob
大约 2 年 前同步成功
通知
76
Star
1521
Fork
537
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
分析
仓库
DevOps
项目成员
Pages
M
miniob
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Pages
分析
分析
仓库分析
DevOps
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
提交
提交
e976988d
编写于
2月 13, 2022
作者:
L
longdafeng
提交者:
Longda
2月 13, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Format all code by the OceanBase Clang-Format file.
上级
06fa8cf7
变更
173
展开全部
显示空白变更内容
内联
并排
Showing
173 changed file
with
4482 addition
and
3849 deletion
+4482
-3849
deps/common/conf/ini.h
deps/common/conf/ini.h
+12
-17
deps/common/defs.h
deps/common/defs.h
+14
-13
deps/common/io/io.cpp
deps/common/io/io.cpp
+37
-53
deps/common/io/io.h
deps/common/io/io.h
+6
-9
deps/common/io/roll_select_dir.cpp
deps/common/io/roll_select_dir.cpp
+5
-3
deps/common/io/roll_select_dir.h
deps/common/io/roll_select_dir.h
+9
-3
deps/common/io/select_dir.h
deps/common/io/select_dir.h
+5
-2
deps/common/lang/bitmap.cpp
deps/common/lang/bitmap.cpp
+18
-12
deps/common/lang/bitmap.h
deps/common/lang/bitmap.h
+7
-8
deps/common/lang/mutex.cpp
deps/common/lang/mutex.cpp
+64
-52
deps/common/lang/mutex.h
deps/common/lang/mutex.h
+120
-129
deps/common/lang/serializable.h
deps/common/lang/serializable.h
+2
-6
deps/common/lang/string.cpp
deps/common/lang/string.cpp
+31
-24
deps/common/lang/string.h
deps/common/lang/string.h
+19
-23
deps/common/log/log.cpp
deps/common/log/log.cpp
+54
-32
deps/common/log/log.h
deps/common/log/log.h
+94
-86
deps/common/math/md5.cpp
deps/common/math/md5.cpp
+124
-54
deps/common/math/md5.h
deps/common/math/md5.h
+2
-2
deps/common/math/random_generator.cpp
deps/common/math/random_generator.cpp
+9
-10
deps/common/math/random_generator.h
deps/common/math/random_generator.h
+9
-11
deps/common/math/regex.cpp
deps/common/math/regex.cpp
+3
-2
deps/common/math/regex.h
deps/common/math/regex.h
+1
-1
deps/common/metrics/console_reporter.cpp
deps/common/metrics/console_reporter.cpp
+5
-3
deps/common/metrics/console_reporter.h
deps/common/metrics/console_reporter.h
+3
-4
deps/common/metrics/histogram_snapshot.cpp
deps/common/metrics/histogram_snapshot.cpp
+65
-81
deps/common/metrics/histogram_snapshot.h
deps/common/metrics/histogram_snapshot.h
+2
-1
deps/common/metrics/log_reporter.cpp
deps/common/metrics/log_reporter.cpp
+7
-6
deps/common/metrics/log_reporter.h
deps/common/metrics/log_reporter.h
+3
-4
deps/common/metrics/metric.h
deps/common/metrics/metric.h
+6
-3
deps/common/metrics/metrics.cpp
deps/common/metrics/metrics.cpp
+55
-34
deps/common/metrics/metrics.h
deps/common/metrics/metrics.h
+11
-6
deps/common/metrics/metrics_registry.cpp
deps/common/metrics/metrics_registry.cpp
+16
-15
deps/common/metrics/metrics_registry.h
deps/common/metrics/metrics_registry.h
+6
-8
deps/common/metrics/reporter.h
deps/common/metrics/reporter.h
+2
-3
deps/common/metrics/reservoir.cpp
deps/common/metrics/reservoir.cpp
+4
-8
deps/common/metrics/reservoir.h
deps/common/metrics/reservoir.h
+1
-3
deps/common/metrics/sampler.cpp
deps/common/metrics/sampler.cpp
+15
-8
deps/common/metrics/sampler.h
deps/common/metrics/sampler.h
+2
-3
deps/common/metrics/snapshot.h
deps/common/metrics/snapshot.h
+25
-17
deps/common/metrics/timer_snapshot.cpp
deps/common/metrics/timer_snapshot.cpp
+15
-6
deps/common/metrics/timer_snapshot.h
deps/common/metrics/timer_snapshot.h
+3
-2
deps/common/metrics/uniform_reservoir.cpp
deps/common/metrics/uniform_reservoir.cpp
+19
-12
deps/common/metrics/uniform_reservoir.h
deps/common/metrics/uniform_reservoir.h
+4
-4
deps/common/mm/debug_new.cpp.skip
deps/common/mm/debug_new.cpp.skip
+42
-20
deps/common/mm/debug_new.h
deps/common/mm/debug_new.h
+8
-8
deps/common/mm/mem.cpp.skip
deps/common/mm/mem.cpp.skip
+59
-38
deps/common/mm/mem.h
deps/common/mm/mem.h
+7
-8
deps/common/os/os.cpp
deps/common/os/os.cpp
+6
-6
deps/common/os/os.h
deps/common/os/os.h
+1
-1
deps/common/os/path.h
deps/common/os/path.h
+3
-4
deps/common/os/pidfile.cpp
deps/common/os/pidfile.cpp
+11
-12
deps/common/os/pidfile.h
deps/common/os/pidfile.h
+3
-4
deps/common/os/process.cpp
deps/common/os/process.cpp
+19
-18
deps/common/os/process.h
deps/common/os/process.h
+2
-3
deps/common/os/process_param.cpp
deps/common/os/process_param.cpp
+5
-6
deps/common/os/process_param.h
deps/common/os/process_param.h
+69
-31
deps/common/os/signal.cpp
deps/common/os/signal.cpp
+15
-11
deps/common/os/signal.h
deps/common/os/signal.h
+1
-2
deps/common/seda/callback.cpp
deps/common/seda/callback.cpp
+15
-9
deps/common/seda/callback.h
deps/common/seda/callback.h
+21
-15
deps/common/seda/class_factory.h
deps/common/seda/class_factory.h
+19
-17
deps/common/seda/event_dispatcher.cpp
deps/common/seda/event_dispatcher.cpp
+14
-10
deps/common/seda/event_dispatcher.h
deps/common/seda/event_dispatcher.h
+16
-13
deps/common/seda/example_stage.cpp
deps/common/seda/example_stage.cpp
+16
-8
deps/common/seda/example_stage.h
deps/common/seda/example_stage.h
+2
-3
deps/common/seda/init.cpp
deps/common/seda/init.cpp
+9
-12
deps/common/seda/init.h
deps/common/seda/init.h
+2
-2
deps/common/seda/kill_thread.cpp
deps/common/seda/kill_thread.cpp
+7
-5
deps/common/seda/kill_thread.h
deps/common/seda/kill_thread.h
+12
-6
deps/common/seda/metrics_report_event.h
deps/common/seda/metrics_report_event.h
+2
-2
deps/common/seda/metrics_stage.cpp
deps/common/seda/metrics_stage.cpp
+20
-13
deps/common/seda/metrics_stage.h
deps/common/seda/metrics_stage.h
+4
-4
deps/common/seda/seda_config.cpp
deps/common/seda/seda_config.cpp
+69
-62
deps/common/seda/seda_config.h
deps/common/seda/seda_config.h
+12
-11
deps/common/seda/seda_defs.h
deps/common/seda/seda_defs.h
+2
-2
deps/common/seda/stage.cpp
deps/common/seda/stage.cpp
+19
-12
deps/common/seda/stage.h
deps/common/seda/stage.h
+51
-32
deps/common/seda/stage_event.cpp
deps/common/seda/stage_event.cpp
+33
-18
deps/common/seda/stage_event.h
deps/common/seda/stage_event.h
+27
-16
deps/common/seda/stage_factory.h
deps/common/seda/stage_factory.h
+2
-3
deps/common/seda/thread_pool.cpp
deps/common/seda/thread_pool.cpp
+44
-24
deps/common/seda/thread_pool.h
deps/common/seda/thread_pool.h
+15
-16
deps/common/seda/timer_stage.cpp
deps/common/seda/timer_stage.cpp
+127
-77
deps/common/seda/timer_stage.h
deps/common/seda/timer_stage.h
+24
-20
deps/common/time/datetime.cpp
deps/common/time/datetime.cpp
+70
-36
deps/common/time/datetime.h
deps/common/time/datetime.h
+124
-71
deps/common/time/timeout_info.cpp
deps/common/time/timeout_info.cpp
+11
-7
deps/common/time/timeout_info.h
deps/common/time/timeout_info.h
+9
-9
deps/common/version.h
deps/common/version.h
+4
-6
src/obclient/client.cpp
src/obclient/client.cpp
+26
-24
src/observer/event/execution_plan_event.cpp
src/observer/event/execution_plan_event.cpp
+4
-4
src/observer/event/execution_plan_event.h
src/observer/event/execution_plan_event.h
+8
-5
src/observer/event/session_event.cpp
src/observer/event/session_event.cpp
+28
-13
src/observer/event/session_event.h
src/observer/event/session_event.h
+1
-1
src/observer/event/sql_event.cpp
src/observer/event/sql_event.cpp
+4
-4
src/observer/event/sql_event.h
src/observer/event/sql_event.h
+7
-4
src/observer/event/storage_event.cpp
src/observer/event/storage_event.cpp
+4
-3
src/observer/event/storage_event.h
src/observer/event/storage_event.h
+4
-2
src/observer/handler/handler.h
src/observer/handler/handler.h
+52
-52
src/observer/ini_setting.h
src/observer/ini_setting.h
+1
-1
src/observer/init.cpp
src/observer/init.cpp
+34
-35
src/observer/init.h
src/observer/init.h
+1
-1
src/observer/main.cpp
src/observer/main.cpp
+35
-31
src/observer/net/connection_context.h
src/observer/net/connection_context.h
+1
-1
src/observer/net/server.cpp
src/observer/net/server.cpp
+42
-38
src/observer/net/server.h
src/observer/net/server.h
+1
-1
src/observer/net/server_param.h
src/observer/net/server_param.h
+1
-1
src/observer/rc.cpp
src/observer/rc.cpp
+5
-2
src/observer/rc.h
src/observer/rc.h
+36
-42
src/observer/session/session.cpp
src/observer/session/session.cpp
+16
-9
src/observer/session/session.h
src/observer/session/session.h
+7
-7
src/observer/session/session_stage.cpp
src/observer/session/session_stage.cpp
+24
-16
src/observer/session/session_stage.h
src/observer/session/session_stage.h
+2
-5
src/observer/sql/executor/execute_stage.cpp
src/observer/sql/executor/execute_stage.cpp
+64
-53
src/observer/sql/executor/execute_stage.h
src/observer/sql/executor/execute_stage.h
+3
-3
src/observer/sql/executor/execution_node.cpp
src/observer/sql/executor/execution_node.cpp
+13
-9
src/observer/sql/executor/execution_node.h
src/observer/sql/executor/execution_node.h
+7
-5
src/observer/sql/executor/tuple.cpp
src/observer/sql/executor/tuple.cpp
+70
-51
src/observer/sql/executor/tuple.h
src/observer/sql/executor/tuple.h
+41
-24
src/observer/sql/executor/value.h
src/observer/sql/executor/value.h
+28
-20
src/observer/sql/optimizer/optimize_stage.cpp
src/observer/sql/optimizer/optimize_stage.cpp
+16
-8
src/observer/sql/optimizer/optimize_stage.h
src/observer/sql/optimizer/optimize_stage.h
+2
-3
src/observer/sql/parser/lex.yy.c
src/observer/sql/parser/lex.yy.c
+1286
-1360
src/observer/sql/parser/lex.yy.h
src/observer/sql/parser/lex.yy.h
+99
-104
src/observer/sql/parser/parse.cpp
src/observer/sql/parser/parse.cpp
+108
-76
src/observer/sql/parser/parse.h
src/observer/sql/parser/parse.h
+2
-3
src/observer/sql/parser/parse_defs.h
src/observer/sql/parser/parse_defs.h
+7
-7
src/observer/sql/parser/parse_stage.cpp
src/observer/sql/parser/parse_stage.cpp
+19
-10
src/observer/sql/parser/parse_stage.h
src/observer/sql/parser/parse_stage.h
+3
-3
src/observer/sql/parser/resolve_stage.cpp
src/observer/sql/parser/resolve_stage.cpp
+16
-8
src/observer/sql/parser/resolve_stage.h
src/observer/sql/parser/resolve_stage.h
+2
-3
src/observer/sql/parser/yacc_sql.tab.h
src/observer/sql/parser/yacc_sql.tab.h
+64
-69
src/observer/sql/plan_cache/plan_cache_stage.cpp
src/observer/sql/plan_cache/plan_cache_stage.cpp
+16
-9
src/observer/sql/plan_cache/plan_cache_stage.h
src/observer/sql/plan_cache/plan_cache_stage.h
+2
-3
src/observer/sql/query_cache/query_cache_stage.cpp
src/observer/sql/query_cache/query_cache_stage.cpp
+16
-9
src/observer/sql/query_cache/query_cache_stage.h
src/observer/sql/query_cache/query_cache_stage.h
+2
-3
src/observer/storage/common/bplus_tree.cpp
src/observer/storage/common/bplus_tree.cpp
+7
-5
src/observer/storage/common/bplus_tree.h
src/observer/storage/common/bplus_tree.h
+4
-1
src/observer/storage/common/bplus_tree_index.cpp
src/observer/storage/common/bplus_tree_index.cpp
+1
-1
src/observer/storage/common/bplus_tree_index.h
src/observer/storage/common/bplus_tree_index.h
+4
-3
src/observer/storage/common/condition_filter.cpp
src/observer/storage/common/condition_filter.cpp
+1
-1
src/observer/storage/common/condition_filter.h
src/observer/storage/common/condition_filter.h
+23
-17
src/observer/storage/common/db.cpp
src/observer/storage/common/db.cpp
+1
-1
src/observer/storage/common/db.h
src/observer/storage/common/db.h
+6
-5
src/observer/storage/common/field_meta.cpp
src/observer/storage/common/field_meta.cpp
+1
-1
src/observer/storage/common/field_meta.h
src/observer/storage/common/field_meta.h
+1
-1
src/observer/storage/common/index.cpp
src/observer/storage/common/index.cpp
+3
-2
src/observer/storage/common/index.h
src/observer/storage/common/index.h
+5
-4
src/observer/storage/common/index_meta.cpp
src/observer/storage/common/index_meta.cpp
+1
-1
src/observer/storage/common/index_meta.h
src/observer/storage/common/index_meta.h
+1
-1
src/observer/storage/common/meta_util.cpp
src/observer/storage/common/meta_util.cpp
+1
-1
src/observer/storage/common/meta_util.h
src/observer/storage/common/meta_util.h
+1
-1
src/observer/storage/common/record_manager.cpp
src/observer/storage/common/record_manager.cpp
+1
-1
src/observer/storage/common/record_manager.h
src/observer/storage/common/record_manager.h
+1
-1
src/observer/storage/common/table.cpp
src/observer/storage/common/table.cpp
+1
-1
src/observer/storage/common/table.h
src/observer/storage/common/table.h
+19
-14
src/observer/storage/common/table_meta.cpp
src/observer/storage/common/table_meta.cpp
+1
-1
src/observer/storage/common/table_meta.h
src/observer/storage/common/table_meta.h
+1
-1
src/observer/storage/default/default_handler.cpp
src/observer/storage/default/default_handler.cpp
+50
-29
src/observer/storage/default/default_handler.h
src/observer/storage/default/default_handler.h
+14
-12
src/observer/storage/default/default_storage_stage.cpp
src/observer/storage/default/default_storage_stage.cpp
+1
-1
src/observer/storage/default/default_storage_stage.h
src/observer/storage/default/default_storage_stage.h
+4
-5
src/observer/storage/default/disk_buffer_pool.cpp
src/observer/storage/default/disk_buffer_pool.cpp
+1
-1
src/observer/storage/default/disk_buffer_pool.h
src/observer/storage/default/disk_buffer_pool.h
+2
-3
src/observer/storage/mem/mem_storage_stage.cpp
src/observer/storage/mem/mem_storage_stage.cpp
+16
-9
src/observer/storage/mem/mem_storage_stage.h
src/observer/storage/mem/mem_storage_stage.h
+3
-3
src/observer/storage/trx/trx.cpp
src/observer/storage/trx/trx.cpp
+68
-55
src/observer/storage/trx/trx.h
src/observer/storage/trx/trx.h
+20
-15
unitest/bitmap_test.cpp
unitest/bitmap_test.cpp
+4
-2
unitest/md5_test.cpp
unitest/md5_test.cpp
+8
-4
unitest/path_test.cpp
unitest/path_test.cpp
+4
-1
unitest/pidfile_test.cpp
unitest/pidfile_test.cpp
+2
-1
unitest/rc_test.cpp
unitest/rc_test.cpp
+3
-2
未找到文件。
deps/common/conf/ini.h
浏览文件 @
e976988d
...
...
@@ -31,7 +31,7 @@ namespace common {
// VARNAME=VALUE
class
Ini
{
public:
public:
/**
* To simplify the logic, no lock's when loading configuration
* So don't modify the data parallel
...
...
@@ -50,24 +50,22 @@ class Ini {
* get the map of the section
* if the section doesn't exist, return one empty section
*/
const
std
::
map
<
std
::
string
,
std
::
string
>
&
get
(
const
std
::
string
&
section
=
DEFAULT_SECTION
);
const
std
::
map
<
std
::
string
,
std
::
string
>
&
get
(
const
std
::
string
&
section
=
DEFAULT_SECTION
);
/**
* get the value of the key in the section,
* if the key-value doesn't exist,
* use the input default_value
*/
std
::
string
get
(
const
std
::
string
&
key
,
const
std
::
string
&
default_value
,
const
std
::
string
&
section
=
DEFAULT_SECTION
);
std
::
string
get
(
const
std
::
string
&
key
,
const
std
::
string
&
default_value
,
const
std
::
string
&
section
=
DEFAULT_SECTION
);
/**
* put the key-value pair to the section
* if the key-value already exist, just replace it
* if the section doesn't exist, it will create this section
*/
int
put
(
const
std
::
string
&
key
,
const
std
::
string
&
value
,
const
std
::
string
&
section
=
DEFAULT_SECTION
);
int
put
(
const
std
::
string
&
key
,
const
std
::
string
&
value
,
const
std
::
string
&
section
=
DEFAULT_SECTION
);
/**
* output all configuration to one string
...
...
@@ -92,7 +90,7 @@ class Ini {
static
const
char
CFG_SESSION_START_TAG
=
'['
;
static
const
char
CFG_SESSION_END_TAG
=
']'
;
protected:
protected:
/**
* insert one empty session to sections_
*/
...
...
@@ -102,21 +100,18 @@ class Ini {
* switch session according to the session_name
* if the section doesn't exist, it will create one
*/
std
::
map
<
std
::
string
,
std
::
string
>
*
switch_session
(
const
std
::
string
&
session_name
);
std
::
map
<
std
::
string
,
std
::
string
>
*
switch_session
(
const
std
::
string
&
session_name
);
/**
* insert one entry to session_map
* line's format is "key=value"
*
*/
int
insert_entry
(
std
::
map
<
std
::
string
,
std
::
string
>
*
session_map
,
const
std
::
string
&
line
);
int
insert_entry
(
std
::
map
<
std
::
string
,
std
::
string
>
*
session_map
,
const
std
::
string
&
line
);
typedef
std
::
map
<
std
::
string
,
std
::
map
<
std
::
string
,
std
::
string
>>
SessionsMap
;
typedef
std
::
map
<
std
::
string
,
std
::
map
<
std
::
string
,
std
::
string
>>
SessionsMap
;
private:
private:
static
const
std
::
map
<
std
::
string
,
std
::
string
>
empty_map_
;
std
::
set
<
std
::
string
>
file_names_
;
...
...
@@ -129,5 +124,5 @@ class Ini {
Ini
*&
get_properties
();
//********************************************************************
}
// namespace common
}
// namespace common
#endif //__COMMON_CONF_INI_H__
deps/common/defs.h
浏览文件 @
e976988d
...
...
@@ -36,7 +36,8 @@ namespace common {
#endif
inline
const
std
::
string
&
theSwVersion
()
{
inline
const
std
::
string
&
theSwVersion
()
{
static
const
std
::
string
swVersion
(
VERSION_STR
);
return
swVersion
;
...
...
deps/common/io/io.cpp
浏览文件 @
e976988d
...
...
@@ -27,13 +27,11 @@ See the Mulan PSL v2 for more details. */
namespace
common
{
int
readFromFile
(
const
std
::
string
&
fileName
,
char
*&
outputData
,
size_t
&
fileSize
)
{
int
readFromFile
(
const
std
::
string
&
fileName
,
char
*&
outputData
,
size_t
&
fileSize
)
{
FILE
*
file
=
fopen
(
fileName
.
c_str
(),
"rb"
);
if
(
file
==
NULL
)
{
std
::
cerr
<<
"Failed to open file "
<<
fileName
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
std
::
cerr
<<
"Failed to open file "
<<
fileName
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
return
-
1
;
}
...
...
@@ -50,8 +48,7 @@ int readFromFile(const std::string &fileName, char *&outputData,
memset
(
buffer
,
0
,
sizeof
(
buffer
));
oneRead
=
fread
(
buffer
,
1
,
sizeof
(
buffer
),
file
);
if
(
ferror
(
file
))
{
std
::
cerr
<<
"Failed to read data"
<<
fileName
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
std
::
cerr
<<
"Failed to read data"
<<
fileName
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
fclose
(
file
);
if
(
data
!=
NULL
)
{
lfree
(
data
);
...
...
@@ -62,8 +59,7 @@ int readFromFile(const std::string &fileName, char *&outputData,
data
=
(
char
*
)
lrealloc
(
data
,
readSize
+
oneRead
);
if
(
data
==
NULL
)
{
std
::
cerr
<<
"Failed to alloc memory for "
<<
fileName
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
std
::
cerr
<<
"Failed to alloc memory for "
<<
fileName
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
lfree
(
data
);
fclose
(
file
);
return
-
1
;
...
...
@@ -81,12 +77,11 @@ int readFromFile(const std::string &fileName, char *&outputData,
return
0
;
}
int
writeToFile
(
const
std
::
string
&
fileName
,
const
char
*
data
,
u32_t
dataSize
,
const
char
*
openMode
)
{
int
writeToFile
(
const
std
::
string
&
fileName
,
const
char
*
data
,
u32_t
dataSize
,
const
char
*
openMode
)
{
FILE
*
file
=
fopen
(
fileName
.
c_str
(),
openMode
);
if
(
file
==
NULL
)
{
std
::
cerr
<<
"Failed to open file "
<<
fileName
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
std
::
cerr
<<
"Failed to open file "
<<
fileName
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
return
-
1
;
}
...
...
@@ -95,8 +90,7 @@ int writeToFile(const std::string &fileName, const char *data, u32_t dataSize,
while
(
leftSize
>
0
)
{
int
writeCount
=
fwrite
(
buffer
,
1
,
leftSize
,
file
);
if
(
writeCount
<=
0
)
{
std
::
cerr
<<
"Failed to open file "
<<
fileName
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
std
::
cerr
<<
"Failed to open file "
<<
fileName
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
fclose
(
file
);
return
-
1
;
}
else
{
...
...
@@ -110,7 +104,8 @@ int writeToFile(const std::string &fileName, const char *data, u32_t dataSize,
return
0
;
}
int
getFileLines
(
const
std
::
string
&
fileName
,
u64_t
&
lineNum
)
{
int
getFileLines
(
const
std
::
string
&
fileName
,
u64_t
&
lineNum
)
{
lineNum
=
0
;
char
line
[
4
*
ONE_KILO
]
=
{
0
};
...
...
@@ -133,14 +128,13 @@ int getFileLines(const std::string &fileName, u64_t &lineNum) {
return
0
;
}
int
getFileNum
(
u64_t
&
fileNum
,
const
std
::
string
&
path
,
const
std
::
string
&
pattern
,
bool
recursive
)
{
int
getFileNum
(
u64_t
&
fileNum
,
const
std
::
string
&
path
,
const
std
::
string
&
pattern
,
bool
recursive
)
{
try
{
DIR
*
dirp
=
NULL
;
dirp
=
opendir
(
path
.
c_str
());
if
(
dirp
==
NULL
)
{
std
::
cerr
<<
"Failed to opendir "
<<
path
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
std
::
cerr
<<
"Failed to opendir "
<<
path
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
return
-
1
;
}
...
...
@@ -160,8 +154,7 @@ int getFileNum(u64_t &fileNum, const std::string &path,
fullPath
+=
entry
->
d_name
;
memset
(
&
fs
,
0
,
sizeof
(
fs
));
if
(
stat
(
fullPath
.
c_str
(),
&
fs
)
<
0
)
{
std
::
cout
<<
"Failed to stat "
<<
fullPath
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
std
::
cout
<<
"Failed to stat "
<<
fullPath
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
continue
;
}
...
...
@@ -181,8 +174,7 @@ int getFileNum(u64_t &fileNum, const std::string &path,
continue
;
}
if
(
pattern
.
empty
()
==
false
&&
regex_match
(
entry
->
d_name
,
pattern
.
c_str
()))
{
if
(
pattern
.
empty
()
==
false
&&
regex_match
(
entry
->
d_name
,
pattern
.
c_str
()))
{
// Don't match
continue
;
}
...
...
@@ -194,20 +186,18 @@ int getFileNum(u64_t &fileNum, const std::string &path,
return
0
;
}
catch
(...)
{
std
::
cerr
<<
"Failed to get file num "
<<
path
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
std
::
cerr
<<
"Failed to get file num "
<<
path
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
}
return
-
1
;
}
int
getFileList
(
std
::
vector
<
std
::
string
>
&
fileList
,
const
std
::
string
&
path
,
const
std
::
string
&
pattern
,
bool
recursive
)
{
int
getFileList
(
std
::
vector
<
std
::
string
>
&
fileList
,
const
std
::
string
&
path
,
const
std
::
string
&
pattern
,
bool
recursive
)
{
try
{
DIR
*
dirp
=
NULL
;
dirp
=
opendir
(
path
.
c_str
());
if
(
dirp
==
NULL
)
{
std
::
cerr
<<
"Failed to opendir "
<<
path
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
std
::
cerr
<<
"Failed to opendir "
<<
path
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
return
-
1
;
}
...
...
@@ -227,8 +217,7 @@ int getFileList(std::vector<std::string> &fileList, const std::string &path,
fullPath
+=
entry
->
d_name
;
memset
(
&
fs
,
0
,
sizeof
(
fs
));
if
(
stat
(
fullPath
.
c_str
(),
&
fs
)
<
0
)
{
std
::
cout
<<
"Failed to stat "
<<
fullPath
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
std
::
cout
<<
"Failed to stat "
<<
fullPath
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
continue
;
}
...
...
@@ -248,8 +237,7 @@ int getFileList(std::vector<std::string> &fileList, const std::string &path,
continue
;
}
if
(
pattern
.
empty
()
==
false
&&
regex_match
(
entry
->
d_name
,
pattern
.
c_str
()))
{
if
(
pattern
.
empty
()
==
false
&&
regex_match
(
entry
->
d_name
,
pattern
.
c_str
()))
{
// Don't match
continue
;
}
...
...
@@ -260,20 +248,18 @@ int getFileList(std::vector<std::string> &fileList, const std::string &path,
closedir
(
dirp
);
return
0
;
}
catch
(...)
{
std
::
cerr
<<
"Failed to get file list "
<<
path
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
std
::
cerr
<<
"Failed to get file list "
<<
path
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
}
return
-
1
;
}
int
getDirList
(
std
::
vector
<
std
::
string
>
&
dirList
,
const
std
::
string
&
path
,
const
std
::
string
&
pattern
)
{
int
getDirList
(
std
::
vector
<
std
::
string
>
&
dirList
,
const
std
::
string
&
path
,
const
std
::
string
&
pattern
)
{
try
{
DIR
*
dirp
=
NULL
;
dirp
=
opendir
(
path
.
c_str
());
if
(
dirp
==
NULL
)
{
std
::
cerr
<<
"Failed to opendir "
<<
path
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
std
::
cerr
<<
"Failed to opendir "
<<
path
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
return
-
1
;
}
...
...
@@ -293,8 +279,7 @@ int getDirList(std::vector<std::string> &dirList, const std::string &path,
fullPath
+=
entry
->
d_name
;
memset
(
&
fs
,
0
,
sizeof
(
fs
));
if
(
stat
(
fullPath
.
c_str
(),
&
fs
)
<
0
)
{
std
::
cout
<<
"Failed to stat "
<<
fullPath
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
std
::
cout
<<
"Failed to stat "
<<
fullPath
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
continue
;
}
...
...
@@ -302,8 +287,7 @@ int getDirList(std::vector<std::string> &dirList, const std::string &path,
continue
;
}
if
(
pattern
.
empty
()
==
false
&&
regex_match
(
entry
->
d_name
,
pattern
.
c_str
()))
{
if
(
pattern
.
empty
()
==
false
&&
regex_match
(
entry
->
d_name
,
pattern
.
c_str
()))
{
// Don't match
continue
;
}
...
...
@@ -314,13 +298,13 @@ int getDirList(std::vector<std::string> &dirList, const std::string &path,
closedir
(
dirp
);
return
0
;
}
catch
(...)
{
std
::
cerr
<<
"Failed to get file list "
<<
path
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
std
::
cerr
<<
"Failed to get file list "
<<
path
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
}
return
-
1
;
}
int
touch
(
const
std
::
string
&
path
)
{
int
touch
(
const
std
::
string
&
path
)
{
// CWE367: A check occurs on a file's attributes before
// the file is used in a privileged operation, but things
// may have changed
...
...
@@ -341,7 +325,8 @@ int touch(const std::string &path) {
return
0
;
}
int
getFileSize
(
const
char
*
filePath
,
u64_t
&
fileLen
)
{
int
getFileSize
(
const
char
*
filePath
,
u64_t
&
fileLen
)
{
if
(
filePath
==
NULL
||
*
filePath
==
'\0'
)
{
std
::
cerr
<<
"invalid filepath"
<<
std
::
endl
;
return
-
EINVAL
;
...
...
@@ -351,8 +336,7 @@ int getFileSize(const char *filePath, u64_t &fileLen) {
int
rc
=
stat
(
filePath
,
&
statBuf
);
if
(
rc
)
{
std
::
cerr
<<
"Failed to get stat of "
<<
filePath
<<
","
<<
errno
<<
":"
<<
strerror
(
errno
)
<<
std
::
endl
;
std
::
cerr
<<
"Failed to get stat of "
<<
filePath
<<
","
<<
errno
<<
":"
<<
strerror
(
errno
)
<<
std
::
endl
;
return
rc
;
}
...
...
@@ -365,4 +349,4 @@ int getFileSize(const char *filePath, u64_t &fileLen) {
return
0
;
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/io/io.h
浏览文件 @
e976988d
...
...
@@ -29,8 +29,7 @@ namespace common {
*/
int
readFromFile
(
const
std
::
string
&
fileName
,
char
*&
data
,
size_t
&
fileSize
);
int
writeToFile
(
const
std
::
string
&
fileName
,
const
char
*
data
,
u32_t
dataSize
,
const
char
*
openMode
);
int
writeToFile
(
const
std
::
string
&
fileName
,
const
char
*
data
,
u32_t
dataSize
,
const
char
*
openMode
);
/**
* return the line number which line.strip() isn't empty
...
...
@@ -47,12 +46,10 @@ int getFileLines(const std::string &fileName, u64_t &lineNum);
* @param[in] resursion if this has been set, it will search subdirs
* @return 0 if success, error code otherwise
*/
int
getFileList
(
std
::
vector
<
std
::
string
>
&
fileList
,
const
std
::
string
&
path
,
const
std
::
string
&
pattern
,
bool
recursive
);
int
getFileNum
(
u64_t
&
fileNum
,
const
std
::
string
&
path
,
const
std
::
string
&
pattern
,
bool
recursive
);
int
getDirList
(
std
::
vector
<
std
::
string
>
&
dirList
,
const
std
::
string
&
path
,
const
std
::
string
&
pattern
);
int
getFileList
(
std
::
vector
<
std
::
string
>
&
fileList
,
const
std
::
string
&
path
,
const
std
::
string
&
pattern
,
bool
recursive
);
int
getFileNum
(
u64_t
&
fileNum
,
const
std
::
string
&
path
,
const
std
::
string
&
pattern
,
bool
recursive
);
int
getDirList
(
std
::
vector
<
std
::
string
>
&
dirList
,
const
std
::
string
&
path
,
const
std
::
string
&
pattern
);
int
touch
(
const
std
::
string
&
fileName
);
...
...
@@ -61,5 +58,5 @@ int touch(const std::string &fileName);
*/
int
getFileSize
(
const
char
*
filePath
,
u64_t
&
fileLen
);
}
//
namespace common
}
//
namespace common
#endif
/* __COMMON_IO_IO_H__ */
deps/common/io/roll_select_dir.cpp
浏览文件 @
e976988d
...
...
@@ -17,7 +17,8 @@ See the Mulan PSL v2 for more details. */
#include "common/log/log.h"
namespace
common
{
void
RollSelectDir
::
setBaseDir
(
std
::
string
baseDir
)
{
void
RollSelectDir
::
setBaseDir
(
std
::
string
baseDir
)
{
mBaseDir
=
baseDir
;
std
::
vector
<
std
::
string
>
dirList
;
...
...
@@ -44,7 +45,8 @@ void RollSelectDir::setBaseDir(std::string baseDir) {
return
;
}
std
::
string
RollSelectDir
::
select
()
{
std
::
string
RollSelectDir
::
select
()
{
std
::
string
ret
;
MUTEX_LOCK
(
&
mMutex
);
...
...
@@ -55,4 +57,4 @@ std::string RollSelectDir::select() {
return
ret
;
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/io/roll_select_dir.h
浏览文件 @
e976988d
...
...
@@ -26,8 +26,14 @@ namespace common {
class
RollSelectDir
:
public
SelectDir
{
public:
RollSelectDir
()
{
MUTEX_INIT
(
&
mMutex
,
NULL
);
}
~
RollSelectDir
()
{
MUTEX_DESTROY
(
&
mMutex
);
}
RollSelectDir
()
{
MUTEX_INIT
(
&
mMutex
,
NULL
);
}
~
RollSelectDir
()
{
MUTEX_DESTROY
(
&
mMutex
);
}
public:
/**
...
...
@@ -43,5 +49,5 @@ public:
u32_t
mPos
;
};
}
//
namespace common
}
//
namespace common
#endif
/* __COMMON_IO_ROLL_SELECT_DIR__ */
deps/common/io/select_dir.h
浏览文件 @
e976988d
...
...
@@ -20,9 +20,12 @@ namespace common {
class
SelectDir
{
public:
virtual
std
::
string
select
()
{
return
std
::
string
(
""
);
};
virtual
std
::
string
select
()
{
return
std
::
string
(
""
);
};
virtual
void
setBaseDir
(
std
::
string
baseDir
){};
};
}
//
namespace common
}
//
namespace common
#endif
/* __COMMON_IO_SELECT_DIR_H__ */
deps/common/lang/bitmap.cpp
浏览文件 @
e976988d
...
...
@@ -14,10 +14,10 @@ See the Mulan PSL v2 for more details. */
#include "common/lang/bitmap.h"
namespace
common
{
namespace
common
{
int
find_first_zero
(
char
byte
,
int
start
)
{
int
find_first_zero
(
char
byte
,
int
start
)
{
for
(
int
i
=
start
;
i
<
8
;
i
++
)
{
if
((
byte
&
(
1
<<
i
))
==
0
)
{
return
i
;
...
...
@@ -26,7 +26,8 @@ int find_first_zero(char byte, int start) {
return
-
1
;
}
int
find_first_setted
(
char
byte
,
int
start
)
{
int
find_first_setted
(
char
byte
,
int
start
)
{
for
(
int
i
=
start
;
i
<
8
;
i
++
)
{
if
((
byte
&
(
1
<<
i
))
!=
0
)
{
return
i
;
...
...
@@ -35,25 +36,29 @@ int find_first_setted(char byte, int start) {
return
-
1
;
}
Bitmap
::
Bitmap
(
char
*
bitmap
,
int
size
)
:
bitmap_
(
bitmap
),
size_
(
size
)
{
}
Bitmap
::
Bitmap
(
char
*
bitmap
,
int
size
)
:
bitmap_
(
bitmap
),
size_
(
size
)
{
}
bool
Bitmap
::
get_bit
(
int
index
)
{
bool
Bitmap
::
get_bit
(
int
index
)
{
char
bits
=
bitmap_
[
index
/
8
];
return
(
bits
&
(
1
<<
(
index
%
8
)))
!=
0
;
}
void
Bitmap
::
set_bit
(
int
index
)
{
void
Bitmap
::
set_bit
(
int
index
)
{
char
&
bits
=
bitmap_
[
index
/
8
];
bits
|=
(
1
<<
(
index
%
8
));
}
void
Bitmap
::
clear_bit
(
int
index
)
{
void
Bitmap
::
clear_bit
(
int
index
)
{
char
&
bits
=
bitmap_
[
index
/
8
];
bits
&=
~
(
1
<<
(
index
%
8
));
}
int
Bitmap
::
next_unsetted_bit
(
int
start
)
{
int
Bitmap
::
next_unsetted_bit
(
int
start
)
{
int
ret
=
-
1
;
int
start_in_byte
=
start
%
8
;
for
(
int
iter
=
start
/
8
,
end
=
(
size_
%
8
==
0
?
size_
/
8
:
size_
/
8
+
1
);
iter
<=
end
;
iter
++
)
{
...
...
@@ -75,7 +80,8 @@ int Bitmap::next_unsetted_bit(int start) {
return
ret
;
}
int
Bitmap
::
next_setted_bit
(
int
start
)
{
int
Bitmap
::
next_setted_bit
(
int
start
)
{
int
ret
=
-
1
;
int
start_in_byte
=
start
%
8
;
for
(
int
iter
=
start
/
8
,
end
=
(
size_
%
8
==
0
?
size_
/
8
:
size_
/
8
+
1
);
iter
<=
end
;
iter
++
)
{
...
...
deps/common/lang/bitmap.h
浏览文件 @
e976988d
...
...
@@ -15,8 +15,7 @@ See the Mulan PSL v2 for more details. */
#ifndef __COMMON_LANG_BITMAP_H__
#define __COMMON_LANG_BITMAP_H__
namespace
common
{
namespace
common
{
class
Bitmap
{
public:
...
...
@@ -30,7 +29,7 @@ public:
int
next_setted_bit
(
int
start
);
private:
char
*
bitmap_
;
char
*
bitmap_
;
int
size_
;
};
...
...
deps/common/lang/mutex.cpp
浏览文件 @
e976988d
...
...
@@ -27,37 +27,48 @@ int LockTrace::mMaxBlockTids = 8;
#define CHECK_UNLOCK 0
void
LockTrace
::
foundDeadLock
(
LockID
&
current
,
LockTrace
::
LockID
&
other
,
pthread_mutex_t
*
otherWaitMutex
)
{
std
::
map
<
pthread_mutex_t
*
,
LockTrace
::
LockID
>::
iterator
itLocks
=
mLocks
.
find
(
otherWaitMutex
);
void
LockTrace
::
foundDeadLock
(
LockID
&
current
,
LockTrace
::
LockID
&
other
,
pthread_mutex_t
*
otherWaitMutex
)
{
std
::
map
<
pthread_mutex_t
*
,
LockTrace
::
LockID
>::
iterator
itLocks
=
mLocks
.
find
(
otherWaitMutex
);
if
(
itLocks
==
mLocks
.
end
())
{
LOG_ERROR
(
"Thread %ld own mutex %p and try to get mutex %s:%d, "
"other thread %ld own mutex %s:%d and try to get %p"
,
current
.
mThreadId
,
otherWaitMutex
,
current
.
mFile
.
c_str
(),
current
.
mLine
,
other
.
mThreadId
,
current
.
mFile
.
c_str
(),
current
.
mLine
,
otherWaitMutex
);
current
.
mThreadId
,
otherWaitMutex
,
current
.
mFile
.
c_str
(),
current
.
mLine
,
other
.
mThreadId
,
current
.
mFile
.
c_str
(),
current
.
mLine
,
otherWaitMutex
);
}
else
{
LockTrace
::
LockID
&
otherRecusive
=
itLocks
->
second
;
LOG_ERROR
(
"Thread %ld own mutex %p:%s:%d and try to get mutex %s:%d, "
"other thread %ld own mutex %s:%d and try to get %p:%s:%d"
,
current
.
mThreadId
,
otherWaitMutex
,
otherRecusive
.
mFile
.
c_str
(),
otherRecusive
.
mLine
,
current
.
mFile
.
c_str
(),
current
.
mLine
,
other
.
mThreadId
,
current
.
mFile
.
c_str
(),
current
.
mLine
,
otherWaitMutex
,
otherRecusive
.
mFile
.
c_str
(),
otherRecusive
.
mLine
);
current
.
mThreadId
,
otherWaitMutex
,
otherRecusive
.
mFile
.
c_str
(),
otherRecusive
.
mLine
,
current
.
mFile
.
c_str
(),
current
.
mLine
,
other
.
mThreadId
,
current
.
mFile
.
c_str
(),
current
.
mLine
,
otherWaitMutex
,
otherRecusive
.
mFile
.
c_str
(),
otherRecusive
.
mLine
);
}
}
bool
LockTrace
::
deadlockCheck
(
LockID
&
current
,
std
::
set
<
pthread_mutex_t
*>
&
ownMutexs
,
LockTrace
::
LockID
&
other
,
int
recusiveNum
)
{
bool
LockTrace
::
deadlockCheck
(
LockID
&
current
,
std
::
set
<
pthread_mutex_t
*>
&
ownMutexs
,
LockTrace
::
LockID
&
other
,
int
recusiveNum
)
{
if
(
recusiveNum
>=
mMaxBlockTids
)
{
return
false
;
}
std
::
map
<
long
long
,
pthread_mutex_t
*>::
iterator
otherIt
=
mWaitLocks
.
find
(
other
.
mThreadId
);
std
::
map
<
long
long
,
pthread_mutex_t
*>::
iterator
otherIt
=
mWaitLocks
.
find
(
other
.
mThreadId
);
if
(
otherIt
==
mWaitLocks
.
end
())
{
return
false
;
}
...
...
@@ -69,8 +80,7 @@ bool LockTrace::deadlockCheck(LockID ¤t,
return
true
;
}
std
::
map
<
pthread_mutex_t
*
,
LockTrace
::
LockID
>::
iterator
itLocks
=
mLocks
.
find
(
otherWaitMutex
);
std
::
map
<
pthread_mutex_t
*
,
LockTrace
::
LockID
>::
iterator
itLocks
=
mLocks
.
find
(
otherWaitMutex
);
if
(
itLocks
==
mLocks
.
end
())
{
return
false
;
}
...
...
@@ -79,12 +89,11 @@ bool LockTrace::deadlockCheck(LockID ¤t,
return
deadlockCheck
(
current
,
ownMutexs
,
otherRecusive
,
recusiveNum
+
1
);
}
bool
LockTrace
::
deadlockCheck
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
)
{
bool
LockTrace
::
deadlockCheck
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
)
{
mWaitLocks
[
threadId
]
=
mutex
;
std
::
map
<
pthread_mutex_t
*
,
LockTrace
::
LockID
>::
iterator
itLocks
=
mLocks
.
find
(
mutex
);
std
::
map
<
pthread_mutex_t
*
,
LockTrace
::
LockID
>::
iterator
itLocks
=
mLocks
.
find
(
mutex
);
if
(
itLocks
==
mLocks
.
end
())
{
return
false
;
}
...
...
@@ -101,8 +110,7 @@ bool LockTrace::deadlockCheck(pthread_mutex_t *mutex, const long long threadId,
}
}
std
::
map
<
long
long
,
std
::
set
<
pthread_mutex_t
*>>::
iterator
it
=
mOwnLocks
.
find
(
threadId
);
std
::
map
<
long
long
,
std
::
set
<
pthread_mutex_t
*>>::
iterator
it
=
mOwnLocks
.
find
(
threadId
);
if
(
it
==
mOwnLocks
.
end
())
{
return
false
;
}
...
...
@@ -115,8 +123,8 @@ bool LockTrace::deadlockCheck(pthread_mutex_t *mutex, const long long threadId,
return
deadlockCheck
(
current
,
ownMutexs
,
other
,
1
);
}
bool
LockTrace
::
checkLockTimes
(
pthread_mutex_t
*
mutex
,
const
char
*
file
,
const
int
line
)
{
bool
LockTrace
::
checkLockTimes
(
pthread_mutex_t
*
mutex
,
const
char
*
file
,
const
int
line
)
{
std
::
map
<
pthread_mutex_t
*
,
int
>::
iterator
it
=
mWaitTimes
.
find
(
mutex
);
if
(
it
==
mWaitTimes
.
end
())
{
mWaitTimes
.
insert
(
std
::
pair
<
pthread_mutex_t
*
,
int
>
(
mutex
,
1
));
...
...
@@ -132,8 +140,13 @@ bool LockTrace::checkLockTimes(pthread_mutex_t *mutex, const char *file,
LockTrace
::
LockID
&
lockId
=
mLocks
[
mutex
];
LOG_WARN
(
"mutex %p has been already lock %d times, this time %s:%d, first "
"time:%ld:%s:%d"
,
mutex
,
lockTimes
,
file
,
line
,
lockId
.
mThreadId
,
lockId
.
mFile
.
c_str
(),
lockId
.
mLine
);
mutex
,
lockTimes
,
file
,
line
,
lockId
.
mThreadId
,
lockId
.
mFile
.
c_str
(),
lockId
.
mLine
);
return
true
;
}
else
{
...
...
@@ -141,8 +154,8 @@ bool LockTrace::checkLockTimes(pthread_mutex_t *mutex, const char *file,
}
}
void
LockTrace
::
check
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
)
{
void
LockTrace
::
check
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
)
{
MUTEX_LOG
(
"Lock mutex %p, %s:%d"
,
mutex
,
file
,
line
);
pthread_rwlock_rdlock
(
&
mMapMutex
);
...
...
@@ -153,8 +166,8 @@ void LockTrace::check(pthread_mutex_t *mutex, const long long threadId,
pthread_rwlock_unlock
(
&
mMapMutex
);
}
void
LockTrace
::
insertLock
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
)
{
void
LockTrace
::
insertLock
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
)
{
LockID
lockID
(
threadId
,
file
,
line
);
mLocks
.
insert
(
std
::
pair
<
pthread_mutex_t
*
,
LockID
>
(
mutex
,
lockID
));
...
...
@@ -174,16 +187,16 @@ void LockTrace::insertLock(pthread_mutex_t *mutex, const long long threadId,
}
}
void
LockTrace
::
lock
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
)
{
void
LockTrace
::
lock
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
)
{
pthread_rwlock_wrlock
(
&
mMapMutex
);
insertLock
(
mutex
,
threadId
,
file
,
line
);
pthread_rwlock_unlock
(
&
mMapMutex
);
}
void
LockTrace
::
tryLock
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
)
{
void
LockTrace
::
tryLock
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
)
{
pthread_rwlock_wrlock
(
&
mMapMutex
);
if
(
mLocks
.
find
(
mutex
)
!=
mLocks
.
end
())
{
pthread_rwlock_unlock
(
&
mMapMutex
);
...
...
@@ -194,8 +207,8 @@ void LockTrace::tryLock(pthread_mutex_t *mutex, const long long threadId,
pthread_rwlock_unlock
(
&
mMapMutex
);
}
void
LockTrace
::
unlock
(
pthread_mutex_t
*
mutex
,
long
long
threadId
,
const
char
*
file
,
int
line
)
{
void
LockTrace
::
unlock
(
pthread_mutex_t
*
mutex
,
long
long
threadId
,
const
char
*
file
,
int
line
)
{
pthread_rwlock_wrlock
(
&
mMapMutex
);
mLocks
.
erase
(
mutex
);
...
...
@@ -206,13 +219,13 @@ void LockTrace::unlock(pthread_mutex_t *mutex, long long threadId,
pthread_rwlock_unlock
(
&
mMapMutex
);
}
void
LockTrace
::
toString
(
std
::
string
&
result
)
{
void
LockTrace
::
toString
(
std
::
string
&
result
)
{
const
int
TEMP_PAIR_LEN
=
24
;
// pthread_mutex_lock(&mMapMutex);
result
=
" mLocks:
\n
"
;
for
(
std
::
map
<
pthread_mutex_t
*
,
LockID
>::
iterator
it
=
mLocks
.
begin
();
it
!=
mLocks
.
end
();
it
++
)
{
for
(
std
::
map
<
pthread_mutex_t
*
,
LockID
>::
iterator
it
=
mLocks
.
begin
();
it
!=
mLocks
.
end
();
it
++
)
{
result
+=
it
->
second
.
toString
();
char
pointerBuf
[
TEMP_PAIR_LEN
]
=
{
0
};
...
...
@@ -222,22 +235,21 @@ void LockTrace::toString(std::string &result) {
}
result
+=
"mWaitTimes:
\n
"
;
for
(
std
::
map
<
pthread_mutex_t
*
,
int
>::
iterator
it
=
mWaitTimes
.
begin
();
it
!=
mWaitTimes
.
end
();
it
++
)
{
for
(
std
::
map
<
pthread_mutex_t
*
,
int
>::
iterator
it
=
mWaitTimes
.
begin
();
it
!=
mWaitTimes
.
end
();
it
++
)
{
char
pointerBuf
[
TEMP_PAIR_LEN
]
=
{
0
};
snprintf
(
pointerBuf
,
TEMP_PAIR_LEN
,
",mutex:%p, times:%d
\n
"
,
it
->
first
,
it
->
second
);
snprintf
(
pointerBuf
,
TEMP_PAIR_LEN
,
",mutex:%p, times:%d
\n
"
,
it
->
first
,
it
->
second
);
result
+=
pointerBuf
;
}
result
+=
"mWaitLocks:
\n
"
;
for
(
std
::
map
<
long
long
,
pthread_mutex_t
*>::
iterator
it
=
mWaitLocks
.
begin
();
it
!=
mWaitLocks
.
end
();
it
++
)
{
for
(
std
::
map
<
long
long
,
pthread_mutex_t
*>::
iterator
it
=
mWaitLocks
.
begin
();
it
!=
mWaitLocks
.
end
();
it
++
)
{
char
pointerBuf
[
TEMP_PAIR_LEN
]
=
{
0
};
snprintf
(
pointerBuf
,
TEMP_PAIR_LEN
,
snprintf
(
pointerBuf
,
TEMP_PAIR_LEN
,
"threadID: %llx"
", mutex:%p
\n
"
,
it
->
first
,
it
->
second
);
it
->
first
,
it
->
second
);
result
+=
pointerBuf
;
}
// pthread_mutex_unlock(&mMapMutex);
...
...
@@ -246,4 +258,4 @@ void LockTrace::toString(std::string &result) {
return
;
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/lang/mutex.h
浏览文件 @
e976988d
...
...
@@ -31,29 +31,26 @@ namespace common {
#define MUTEX_LOG LOG_DEBUG
class
LockTrace
{
public:
static
void
check
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
);
static
void
lock
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
);
static
void
tryLock
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
);
static
void
unlock
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
);
public:
static
void
check
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
);
static
void
lock
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
);
static
void
tryLock
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
);
static
void
unlock
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
);
static
void
toString
(
std
::
string
&
result
);
class
LockID
{
public:
LockID
(
const
long
long
threadId
,
const
char
*
file
,
const
int
line
)
:
mFile
(
file
),
mThreadId
(
threadId
),
mLine
(
line
)
{}
LockID
()
:
mFile
(),
mThreadId
(
0
),
mLine
(
0
)
{}
LockID
(
const
long
long
threadId
,
const
char
*
file
,
const
int
line
)
:
mFile
(
file
),
mThreadId
(
threadId
),
mLine
(
line
)
{}
LockID
()
:
mFile
(),
mThreadId
(
0
),
mLine
(
0
)
{}
std
::
string
toString
()
{
std
::
string
toString
()
{
std
::
ostringstream
oss
;
oss
<<
"threaId:"
<<
mThreadId
<<
",file name:"
<<
mFile
<<
",line:"
<<
mLine
;
oss
<<
"threaId:"
<<
mThreadId
<<
",file name:"
<<
mFile
<<
",line:"
<<
mLine
;
return
oss
.
str
();
}
...
...
@@ -64,28 +61,25 @@ class LockTrace {
int
mLine
;
};
static
void
foundDeadLock
(
LockID
&
current
,
LockID
&
other
,
pthread_mutex_t
*
otherWaitMutex
);
static
void
foundDeadLock
(
LockID
&
current
,
LockID
&
other
,
pthread_mutex_t
*
otherWaitMutex
);
static
bool
deadlockCheck
(
LockID
&
current
,
std
::
set
<
pthread_mutex_t
*>
&
ownMutexs
,
LockID
&
other
,
int
recusiveNum
);
static
bool
deadlockCheck
(
LockID
&
current
,
std
::
set
<
pthread_mutex_t
*>
&
ownMutexs
,
LockID
&
other
,
int
recusiveNum
);
static
bool
deadlockCheck
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
);
static
bool
deadlockCheck
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
);
static
bool
checkLockTimes
(
pthread_mutex_t
*
mutex
,
const
char
*
file
,
const
int
line
);
static
bool
checkLockTimes
(
pthread_mutex_t
*
mutex
,
const
char
*
file
,
const
int
line
);
static
void
insertLock
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
);
static
void
insertLock
(
pthread_mutex_t
*
mutex
,
const
long
long
threadId
,
const
char
*
file
,
const
int
line
);
static
void
setMaxBlockThreads
(
int
blockNum
)
{
mMaxBlockTids
=
blockNum
;
}
static
void
setMaxBlockThreads
(
int
blockNum
)
{
mMaxBlockTids
=
blockNum
;
}
public:
public:
static
std
::
set
<
pthread_mutex_t
*>
mEnableRecurisives
;
protected:
protected:
static
std
::
map
<
pthread_mutex_t
*
,
LockID
>
mLocks
;
static
std
::
map
<
pthread_mutex_t
*
,
int
>
mWaitTimes
;
static
std
::
map
<
long
long
,
pthread_mutex_t
*>
mWaitLocks
;
...
...
@@ -108,14 +102,12 @@ class LockTrace {
#define COND_INIT(cond, attr) pthread_cond_init(cond, attr)
#define COND_DESTROY(cond) pthread_cond_destroy(cond)
#define COND_WAIT(cond, mutex) pthread_cond_wait(cond, mutex)
#define COND_WAIT_TIMEOUT(cond, mutex, time, ret) \
ret = pthread_cond_timedwait(cond, mutex, time)
#define COND_WAIT_TIMEOUT(cond, mutex, time, ret) ret = pthread_cond_timedwait(cond, mutex, time)
#define COND_SIGNAL(cond) pthread_cond_signal(cond)
#define COND_BRAODCAST(cond) pthread_cond_broadcast(cond)
#else // DEBUG_LOCK
#define MUTEX_STATIC_INIT() \
PTHREAD_MUTEX_INITIALIZER; \
LOG_INFO("PTHREAD_MUTEX_INITIALIZER");
...
...
@@ -188,8 +180,7 @@ class LockTrace {
LockTrace::unlock(lock, gettid(), __FILE__, __LINE__); \
MUTEX_LOG("mutex:%p has been ulocked", lock); \
if (result) { \
LOG_ERROR("Failed to unlock %p, rc %d:%s", lock, errno, \
strerror(errno)); \
LOG_ERROR("Failed to unlock %p, rc %d:%s", lock, errno, strerror(errno)); \
} \
result; \
})
...
...
@@ -248,5 +239,5 @@ class LockTrace {
#endif // DEBUG_LOCK
}
//
namespace common
}
//
namespace common
#endif // __COMMON_LANG_MUTEX_H__
deps/common/lang/serializable.h
浏览文件 @
e976988d
...
...
@@ -21,11 +21,7 @@ namespace common {
/**
* Through this type to determine object type
*/
enum
{
MESSAGE_BASIC
=
100
,
MESSAGE_BASIC_REQUEST
=
1000
,
MESSAGE_BASIC_RESPONSE
=
-
1000
};
enum
{
MESSAGE_BASIC
=
100
,
MESSAGE_BASIC_REQUEST
=
1000
,
MESSAGE_BASIC_RESPONSE
=
-
1000
};
class
Deserializable
{
public:
...
...
@@ -68,5 +64,5 @@ public:
virtual
void
to_string
(
std
::
string
&
output
)
const
=
0
;
};
}
//
namespace common
}
//
namespace common
#endif
/* __COMMON_LANG_SERIALIZABLE_H__ */
deps/common/lang/string.cpp
浏览文件 @
e976988d
...
...
@@ -28,8 +28,9 @@ See the Mulan PSL v2 for more details. */
#include "common/log/log.h"
namespace
common
{
char
*
strip
(
char
*
str_
)
{
if
(
str_
==
NULL
||
*
str_
==
0
){
char
*
strip
(
char
*
str_
)
{
if
(
str_
==
NULL
||
*
str_
==
0
)
{
LOG_ERROR
(
"The augument is invalid!"
);
return
str_
;
}
...
...
@@ -45,7 +46,8 @@ char *strip(char *str_) {
return
head
;
}
void
strip
(
std
::
string
&
str
)
{
void
strip
(
std
::
string
&
str
)
{
size_t
head
=
0
;
while
(
isspace
(
str
[
head
]))
{
...
...
@@ -61,24 +63,27 @@ void strip(std::string &str) {
}
// Translation functions with templates are defined in the header file
std
::
string
size_to_pad_str
(
int
size
,
int
pad
)
{
std
::
string
size_to_pad_str
(
int
size
,
int
pad
)
{
std
::
ostringstream
ss
;
ss
<<
std
::
setw
(
pad
)
<<
std
::
setfill
(
'0'
)
<<
size
;
return
ss
.
str
();
}
std
::
string
&
str_to_upper
(
std
::
string
&
s
)
{
std
::
string
&
str_to_upper
(
std
::
string
&
s
)
{
std
::
transform
(
s
.
begin
(),
s
.
end
(),
s
.
begin
(),
(
int
(
*
)(
int
))
&
std
::
toupper
);
return
s
;
}
std
::
string
&
str_to_lower
(
std
::
string
&
s
)
{
std
::
string
&
str_to_lower
(
std
::
string
&
s
)
{
std
::
transform
(
s
.
begin
(),
s
.
end
(),
s
.
begin
(),
(
int
(
*
)(
int
))
&
std
::
tolower
);
return
s
;
}
void
split_string
(
const
std
::
string
&
str
,
std
::
string
delim
,
std
::
set
<
std
::
string
>
&
results
)
{
void
split_string
(
const
std
::
string
&
str
,
std
::
string
delim
,
std
::
set
<
std
::
string
>
&
results
)
{
int
cut_at
;
std
::
string
tmp_str
(
str
);
while
((
cut_at
=
tmp_str
.
find_first_of
(
delim
))
!=
(
signed
)
tmp_str
.
npos
)
{
...
...
@@ -93,8 +98,8 @@ void split_string(const std::string &str, std::string delim,
}
}
void
split_string
(
const
std
::
string
&
str
,
std
::
string
delim
,
std
::
vector
<
std
::
string
>
&
results
)
{
void
split_string
(
const
std
::
string
&
str
,
std
::
string
delim
,
std
::
vector
<
std
::
string
>
&
results
)
{
int
cut_at
;
std
::
string
tmp_str
(
str
);
while
((
cut_at
=
tmp_str
.
find_first_of
(
delim
))
!=
(
signed
)
tmp_str
.
npos
)
{
...
...
@@ -109,8 +114,8 @@ void split_string(const std::string &str, std::string delim,
}
}
void
split_string
(
char
*
str
,
char
dim
,
std
::
vector
<
char
*>
&
results
,
bool
keep_null
)
{
void
split_string
(
char
*
str
,
char
dim
,
std
::
vector
<
char
*>
&
results
,
bool
keep_null
)
{
char
*
p
=
str
;
char
*
l
=
p
;
while
(
*
p
)
{
...
...
@@ -127,11 +132,11 @@ void split_string(char *str, char dim, std::vector<char *> &results,
return
;
}
void
merge_string
(
std
::
string
&
str
,
std
::
string
delim
,
std
::
vector
<
std
::
string
>
&
source
,
size_t
result_len
)
{
void
merge_string
(
std
::
string
&
str
,
std
::
string
delim
,
std
::
vector
<
std
::
string
>
&
source
,
size_t
result_len
)
{
std
::
ostringstream
ss
;
if
(
source
.
empty
()
)
{
if
(
source
.
empty
())
{
str
=
ss
.
str
();
return
;
}
...
...
@@ -143,18 +148,17 @@ void merge_string(std::string &str, std::string delim,
for
(
unsigned
int
i
=
0
;
i
<
result_len
;
i
++
)
{
if
(
i
==
0
)
{
ss
<<
source
[
i
];
}
else
{
}
else
{
ss
<<
delim
<<
source
[
i
];
}
}
str
=
ss
.
str
();
return
;
return
;
}
void
replace
(
std
::
string
&
str
,
const
std
::
string
&
old
,
const
std
::
string
&
new_str
)
{
void
replace
(
std
::
string
&
str
,
const
std
::
string
&
old
,
const
std
::
string
&
new_str
)
{
if
(
old
.
compare
(
new_str
)
==
0
)
{
return
;
}
...
...
@@ -185,7 +189,8 @@ void replace(std::string &str, const std::string &old,
return
;
}
char
*
bin_to_hex
(
const
char
*
s
,
const
int
len
,
char
*
hex_buff
)
{
char
*
bin_to_hex
(
const
char
*
s
,
const
int
len
,
char
*
hex_buff
)
{
int
new_len
=
0
;
unsigned
char
*
end
=
(
unsigned
char
*
)
s
+
len
;
for
(
unsigned
char
*
p
=
(
unsigned
char
*
)
s
;
p
<
end
;
p
++
)
{
...
...
@@ -196,7 +201,8 @@ char *bin_to_hex(const char *s, const int len, char *hex_buff) {
return
hex_buff
;
}
char
*
hex_to_bin
(
const
char
*
s
,
char
*
bin_buff
,
int
*
dest_len
)
{
char
*
hex_to_bin
(
const
char
*
s
,
char
*
bin_buff
,
int
*
dest_len
)
{
char
buff
[
3
];
char
*
src
;
int
src_len
;
...
...
@@ -225,7 +231,8 @@ char *hex_to_bin(const char *s, char *bin_buff, int *dest_len) {
return
bin_buff
;
}
bool
is_blank
(
const
char
*
s
)
{
bool
is_blank
(
const
char
*
s
)
{
if
(
s
==
nullptr
)
{
return
true
;
}
...
...
@@ -238,4 +245,4 @@ bool is_blank(const char *s) {
return
true
;
}
}
//
namespace common
}
//
namespace common
deps/common/lang/string.h
浏览文件 @
e976988d
...
...
@@ -65,20 +65,15 @@ std::string &str_to_lower(std::string &s);
* @param[in] delims elimiter characters
* @param[in,out] results ector containing the split up string
*/
void
split_string
(
const
std
::
string
&
str
,
std
::
string
delim
,
std
::
set
<
std
::
string
>
&
results
);
void
split_string
(
const
std
::
string
&
str
,
std
::
string
delim
,
std
::
vector
<
std
::
string
>
&
results
);
void
split_string
(
char
*
str
,
char
dim
,
std
::
vector
<
char
*>
&
results
,
bool
keep_null
=
false
);
void
merge_string
(
std
::
string
&
str
,
std
::
string
delim
,
std
::
vector
<
std
::
string
>
&
result
,
size_t
result_len
=
0
);
void
split_string
(
const
std
::
string
&
str
,
std
::
string
delim
,
std
::
set
<
std
::
string
>
&
results
);
void
split_string
(
const
std
::
string
&
str
,
std
::
string
delim
,
std
::
vector
<
std
::
string
>
&
results
);
void
split_string
(
char
*
str
,
char
dim
,
std
::
vector
<
char
*>
&
results
,
bool
keep_null
=
false
);
void
merge_string
(
std
::
string
&
str
,
std
::
string
delim
,
std
::
vector
<
std
::
string
>
&
result
,
size_t
result_len
=
0
);
/**
* replace old with new in the string
*/
void
replace
(
std
::
string
&
str
,
const
std
::
string
&
old
,
const
std
::
string
&
new_str
);
void
replace
(
std
::
string
&
str
,
const
std
::
string
&
old
,
const
std
::
string
&
new_str
);
/**
* binary to hexadecimal
...
...
@@ -102,8 +97,7 @@ char *hex_to_bin(const char *s, char *bin_buff, int *dest_len);
* number, \c false otherwise
*/
template
<
class
T
>
bool
str_to_val
(
const
std
::
string
&
str
,
T
&
val
,
std
::
ios_base
&
(
*
radix
)(
std
::
ios_base
&
)
=
std
::
dec
);
bool
str_to_val
(
const
std
::
string
&
str
,
T
&
val
,
std
::
ios_base
&
(
*
radix
)(
std
::
ios_base
&
)
=
std
::
dec
);
/**
* Convert a numeric value into its string representation
...
...
@@ -116,17 +110,17 @@ bool str_to_val(const std::string &str, T &val,
* (hexidecimal).
*/
template
<
class
T
>
void
val_to_str
(
const
T
&
val
,
std
::
string
&
str
,
std
::
ios_base
&
(
*
radix
)(
std
::
ios_base
&
)
=
std
::
dec
);
void
val_to_str
(
const
T
&
val
,
std
::
string
&
str
,
std
::
ios_base
&
(
*
radix
)(
std
::
ios_base
&
)
=
std
::
dec
);
/**
* get type's name
*/
template
<
class
T
>
std
::
string
get_type_name
(
const
T
&
val
);
template
<
class
T
>
std
::
string
get_type_name
(
const
T
&
val
);
template
<
class
T
>
bool
str_to_val
(
const
std
::
string
&
str
,
T
&
val
,
std
::
ios_base
&
(
*
radix
)(
std
::
ios_base
&
)
/* = std::dec */
)
{
bool
str_to_val
(
const
std
::
string
&
str
,
T
&
val
,
std
::
ios_base
&
(
*
radix
)(
std
::
ios_base
&
)
/* = std::dec */
)
{
bool
success
=
true
;
std
::
istringstream
is
(
str
);
if
(
!
(
is
>>
radix
>>
val
))
{
...
...
@@ -137,14 +131,16 @@ bool str_to_val(const std::string &str, T &val,
}
template
<
class
T
>
void
val_to_str
(
const
T
&
val
,
std
::
string
&
str
,
std
::
ios_base
&
(
*
radix
)(
std
::
ios_base
&
)
/* = std::dec */
)
{
void
val_to_str
(
const
T
&
val
,
std
::
string
&
str
,
std
::
ios_base
&
(
*
radix
)(
std
::
ios_base
&
)
/* = std::dec */
)
{
std
::
stringstream
strm
;
strm
<<
radix
<<
val
;
str
=
strm
.
str
();
}
template
<
class
T
>
std
::
string
get_type_name
(
const
T
&
val
)
{
template
<
class
T
>
std
::
string
get_type_name
(
const
T
&
val
)
{
int
status
=
0
;
char
*
stmp
=
abi
::
__cxa_demangle
(
typeid
(
val
).
name
(),
0
,
0
,
&
status
);
if
(
!
stmp
)
...
...
@@ -158,5 +154,5 @@ template <class T> std::string get_type_name(const T &val) {
bool
is_blank
(
const
char
*
s
);
}
//
namespace common
}
//
namespace common
#endif // __COMMON_LANG_STRING_H__
deps/common/log/log.cpp
浏览文件 @
e976988d
...
...
@@ -23,9 +23,9 @@ namespace common {
Log
*
g_log
=
nullptr
;
Log
::
Log
(
const
std
::
string
&
log_file_name
,
const
LOG_LEVEL
log_level
,
const
LOG_LEVEL
console_level
)
:
log_name_
(
log_file_name
),
log_level_
(
log_level
),
console_level_
(
console_level
)
{
Log
::
Log
(
const
std
::
string
&
log_file_name
,
const
LOG_LEVEL
log_level
,
const
LOG_LEVEL
console_level
)
:
log_name_
(
log_file_name
),
log_level_
(
log_level
),
console_level_
(
console_level
)
{
prefix_map_
[
LOG_LEVEL_PANIC
]
=
"PANIC:"
;
prefix_map_
[
LOG_LEVEL_ERR
]
=
"ERROR:"
;
prefix_map_
[
LOG_LEVEL_WARN
]
=
"WARNNING:"
;
...
...
@@ -45,7 +45,8 @@ Log::Log(const std::string &log_file_name, const LOG_LEVEL log_level,
check_param_valid
();
}
Log
::~
Log
(
void
)
{
Log
::~
Log
(
void
)
{
pthread_mutex_lock
(
&
lock_
);
if
(
ofs_
.
is_open
())
{
ofs_
.
close
();
...
...
@@ -55,7 +56,8 @@ Log::~Log(void) {
pthread_mutex_destroy
(
&
lock_
);
}
void
Log
::
check_param_valid
()
{
void
Log
::
check_param_valid
()
{
assert
(
!
log_name_
.
empty
());
assert
(
LOG_LEVEL_PANIC
<=
log_level_
&&
log_level_
<
LOG_LEVEL_LAST
);
assert
(
LOG_LEVEL_PANIC
<=
console_level_
&&
console_level_
<
LOG_LEVEL_LAST
);
...
...
@@ -63,7 +65,8 @@ void Log::check_param_valid() {
return
;
}
bool
Log
::
check_output
(
const
LOG_LEVEL
level
,
const
char
*
module
)
{
bool
Log
::
check_output
(
const
LOG_LEVEL
level
,
const
char
*
module
)
{
if
(
LOG_LEVEL_LAST
>
level
&&
level
<=
console_level_
)
{
return
true
;
}
...
...
@@ -77,8 +80,8 @@ bool Log::check_output(const LOG_LEVEL level, const char *module) {
return
false
;
}
int
Log
::
output
(
const
LOG_LEVEL
level
,
const
char
*
module
,
const
char
*
prefix
,
const
char
*
f
,
...)
{
int
Log
::
output
(
const
LOG_LEVEL
level
,
const
char
*
module
,
const
char
*
prefix
,
const
char
*
f
,
...)
{
bool
locked
=
false
;
try
{
va_list
args
;
...
...
@@ -127,7 +130,8 @@ int Log::output(const LOG_LEVEL level, const char *module, const char *prefix,
return
LOG_STATUS_OK
;
}
int
Log
::
set_console_level
(
LOG_LEVEL
console_level
)
{
int
Log
::
set_console_level
(
LOG_LEVEL
console_level
)
{
if
(
LOG_LEVEL_PANIC
<=
console_level
&&
console_level
<
LOG_LEVEL_LAST
)
{
console_level_
=
console_level
;
return
LOG_STATUS_OK
;
...
...
@@ -136,9 +140,13 @@ int Log::set_console_level(LOG_LEVEL console_level) {
return
LOG_STATUS_ERR
;
}
LOG_LEVEL
Log
::
get_console_level
()
{
return
console_level_
;
}
LOG_LEVEL
Log
::
get_console_level
()
{
return
console_level_
;
}
int
Log
::
set_log_level
(
LOG_LEVEL
log_level
)
{
int
Log
::
set_log_level
(
LOG_LEVEL
log_level
)
{
if
(
LOG_LEVEL_PANIC
<=
log_level
&&
log_level
<
LOG_LEVEL_LAST
)
{
log_level_
=
log_level
;
return
LOG_STATUS_OK
;
...
...
@@ -147,9 +155,13 @@ int Log::set_log_level(LOG_LEVEL log_level) {
return
LOG_STATUS_ERR
;
}
LOG_LEVEL
Log
::
get_log_level
()
{
return
log_level_
;
}
LOG_LEVEL
Log
::
get_log_level
()
{
return
log_level_
;
}
const
char
*
Log
::
prefix_msg
(
LOG_LEVEL
level
)
{
const
char
*
Log
::
prefix_msg
(
LOG_LEVEL
level
)
{
if
(
LOG_LEVEL_PANIC
<=
level
&&
level
<
LOG_LEVEL_LAST
)
{
return
prefix_map_
[
level
].
c_str
();
}
...
...
@@ -157,22 +169,27 @@ const char *Log::prefix_msg(LOG_LEVEL level) {
return
empty_prefix
;
}
void
Log
::
set_default_module
(
const
std
::
string
&
modules
)
{
void
Log
::
set_default_module
(
const
std
::
string
&
modules
)
{
split_string
(
modules
,
","
,
default_set_
);
}
int
Log
::
set_rotate_type
(
LOG_ROTATE
rotate_type
)
{
int
Log
::
set_rotate_type
(
LOG_ROTATE
rotate_type
)
{
if
(
LOG_ROTATE_BYDAY
<=
rotate_type
&&
rotate_type
<
LOG_ROTATE_LAST
)
{
rotate_type_
=
rotate_type
;
}
return
LOG_STATUS_OK
;
}
LOG_ROTATE
Log
::
get_rotate_type
()
{
return
rotate_type_
;
}
LOG_ROTATE
Log
::
get_rotate_type
()
{
return
rotate_type_
;
}
int
Log
::
rotate_by_day
(
const
int
year
,
const
int
month
,
const
int
day
)
{
if
(
log_date_
.
year_
==
year
&&
log_date_
.
mon_
==
month
&&
log_date_
.
day_
==
day
)
{
int
Log
::
rotate_by_day
(
const
int
year
,
const
int
month
,
const
int
day
)
{
if
(
log_date_
.
year_
==
year
&&
log_date_
.
mon_
==
month
&&
log_date_
.
day_
==
day
)
{
// Don't need rotate
return
0
;
}
...
...
@@ -196,7 +213,8 @@ int Log::rotate_by_day(const int year, const int month, const int day) {
return
0
;
}
int
Log
::
rename_old_logs
()
{
int
Log
::
rename_old_logs
()
{
int
log_index
=
1
;
int
max_log_index
=
0
;
char
log_index_str
[
4
]
=
{
0
};
...
...
@@ -234,7 +252,8 @@ int Log::rename_old_logs() {
return
LOG_STATUS_OK
;
}
int
Log
::
rotate_by_size
()
{
int
Log
::
rotate_by_size
()
{
if
(
log_line_
<
0
)
{
// The first time open log file
ofs_
.
open
(
log_name_
.
c_str
(),
std
::
ios_base
::
out
|
std
::
ios_base
::
app
);
...
...
@@ -260,8 +279,7 @@ int Log::rotate_by_size() {
std
::
string
log_name_new
=
log_name_
+
"."
+
log_index_str
;
result
=
rename
(
log_name_
.
c_str
(),
log_name_new
.
c_str
());
if
(
result
)
{
std
::
cerr
<<
"Failed to rename "
<<
log_name_
<<
" to "
<<
log_name_new
<<
std
::
endl
;
std
::
cerr
<<
"Failed to rename "
<<
log_name_
<<
" to "
<<
log_name_new
<<
std
::
endl
;
}
ofs_
.
open
(
log_name_
.
c_str
(),
std
::
ios_base
::
out
|
std
::
ios_base
::
app
);
...
...
@@ -278,7 +296,8 @@ int Log::rotate_by_size() {
return
LOG_STATUS_OK
;
}
int
Log
::
rotate
(
const
int
year
,
const
int
month
,
const
int
day
)
{
int
Log
::
rotate
(
const
int
year
,
const
int
month
,
const
int
day
)
{
int
result
=
0
;
pthread_mutex_lock
(
&
lock_
);
if
(
rotate_type_
==
LOG_ROTATE_BYDAY
)
{
...
...
@@ -291,17 +310,19 @@ int Log::rotate(const int year, const int month, const int day) {
return
result
;
}
LoggerFactory
::
LoggerFactory
()
{
LoggerFactory
::
LoggerFactory
()
{
// Auto-generated constructor stub
}
LoggerFactory
::~
LoggerFactory
()
{
LoggerFactory
::~
LoggerFactory
()
{
// Auto-generated destructor stub
}
int
LoggerFactory
::
init
(
const
std
::
string
&
log_file
,
Log
**
logger
,
LOG_LEVEL
log_level
,
LOG_LEVEL
console_level
,
LOG_ROTATE
rotate_type
)
{
int
LoggerFactory
::
init
(
const
std
::
string
&
log_file
,
Log
**
logger
,
LOG_LEVEL
log_level
,
LOG_LEVEL
console_level
,
LOG_ROTATE
rotate_type
)
{
Log
*
log
=
new
(
std
::
nothrow
)
Log
(
log_file
,
log_level
,
console_level
);
if
(
log
==
nullptr
)
{
std
::
cout
<<
"Error: fail to construct a log object!"
<<
std
::
endl
;
...
...
@@ -314,8 +335,9 @@ int LoggerFactory::init(const std::string &log_file, Log **logger,
return
0
;
}
int
LoggerFactory
::
init_default
(
const
std
::
string
&
log_file
,
LOG_LEVEL
log_level
,
LOG_LEVEL
console_level
,
LOG_ROTATE
rotate_type
)
{
int
LoggerFactory
::
init_default
(
const
std
::
string
&
log_file
,
LOG_LEVEL
log_level
,
LOG_LEVEL
console_level
,
LOG_ROTATE
rotate_type
)
{
if
(
g_log
!=
nullptr
)
{
LOG_WARN
(
"Default logger has been initialized"
);
return
0
;
...
...
@@ -324,4 +346,4 @@ int LoggerFactory::init_default(const std::string &log_file, LOG_LEVEL log_level
return
init
(
log_file
,
&
g_log
,
log_level
,
console_level
,
rotate_type
);
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/log/log.h
浏览文件 @
e976988d
...
...
@@ -45,14 +45,10 @@ typedef enum {
LOG_LEVEL_LAST
}
LOG_LEVEL
;
typedef
enum
{
LOG_ROTATE_BYDAY
=
0
,
LOG_ROTATE_BYSIZE
,
LOG_ROTATE_LAST
}
LOG_ROTATE
;
typedef
enum
{
LOG_ROTATE_BYDAY
=
0
,
LOG_ROTATE_BYSIZE
,
LOG_ROTATE_LAST
}
LOG_ROTATE
;
class
Log
{
public:
public:
Log
(
const
std
::
string
&
log_name
,
const
LOG_LEVEL
log_level
=
LOG_LEVEL_INFO
,
const
LOG_LEVEL
console_level
=
LOG_LEVEL_WARN
);
~
Log
(
void
);
...
...
@@ -65,29 +61,28 @@ class Log {
* If the header information should be outputed
* please use LOG_PANIC, LOG_ERROR ...
*/
template
<
class
T
>
template
<
class
T
>
Log
&
operator
<<
(
T
message
);
template
<
class
T
>
template
<
class
T
>
int
panic
(
T
message
);
template
<
class
T
>
template
<
class
T
>
int
error
(
T
message
);
template
<
class
T
>
template
<
class
T
>
int
warnning
(
T
message
);
template
<
class
T
>
template
<
class
T
>
int
info
(
T
message
);
template
<
class
T
>
template
<
class
T
>
int
debug
(
T
message
);
template
<
class
T
>
template
<
class
T
>
int
trace
(
T
message
);
int
output
(
const
LOG_LEVEL
level
,
const
char
*
module
,
const
char
*
prefix
,
const
char
*
f
,
...);
int
output
(
const
LOG_LEVEL
level
,
const
char
*
module
,
const
char
*
prefix
,
const
char
*
f
,
...);
int
set_console_level
(
const
LOG_LEVEL
console_level
);
LOG_LEVEL
get_console_level
();
...
...
@@ -110,17 +105,17 @@ class Log {
int
rotate
(
const
int
year
=
0
,
const
int
month
=
0
,
const
int
day
=
0
);
private:
private:
void
check_param_valid
();
int
rotate_by_size
();
int
rename_old_logs
();
int
rotate_by_day
(
const
int
year
,
const
int
month
,
const
int
day
);
template
<
class
T
>
template
<
class
T
>
int
out
(
const
LOG_LEVEL
console_level
,
const
LOG_LEVEL
log_level
,
T
&
message
);
private:
private:
pthread_mutex_t
lock_
;
std
::
ofstream
ofs_
;
std
::
string
log_name_
;
...
...
@@ -145,28 +140,23 @@ class Log {
};
class
LoggerFactory
{
public:
public:
LoggerFactory
();
virtual
~
LoggerFactory
();
static
int
init
(
const
std
::
string
&
log_file
,
Log
**
logger
,
LOG_LEVEL
log_level
=
LOG_LEVEL_INFO
,
LOG_LEVEL
console_level
=
LOG_LEVEL_WARN
,
LOG_ROTATE
rotate_type
=
LOG_ROTATE_BYDAY
);
static
int
init
(
const
std
::
string
&
log_file
,
Log
**
logger
,
LOG_LEVEL
log_level
=
LOG_LEVEL_INFO
,
LOG_LEVEL
console_level
=
LOG_LEVEL_WARN
,
LOG_ROTATE
rotate_type
=
LOG_ROTATE_BYDAY
);
static
int
init_default
(
const
std
::
string
&
log_file
,
LOG_LEVEL
log_level
=
LOG_LEVEL_INFO
,
LOG_LEVEL
console_level
=
LOG_LEVEL_WARN
,
LOG_ROTATE
rotate_type
=
LOG_ROTATE_BYDAY
);
static
int
init_default
(
const
std
::
string
&
log_file
,
LOG_LEVEL
log_level
=
LOG_LEVEL_INFO
,
LOG_LEVEL
console_level
=
LOG_LEVEL_WARN
,
LOG_ROTATE
rotate_type
=
LOG_ROTATE_BYDAY
);
};
extern
Log
*
g_log
;
#ifndef __FILE_NAME__
#define __FILE_NAME__ (strrchr(__FILE__,
'/')?strrchr(__FILE__,'/')+1:
__FILE__)
#define __FILE_NAME__ (strrchr(__FILE__,
'/') ? strrchr(__FILE__, '/') + 1 :
__FILE__)
#endif
#define LOG_HEAD(prefix, level) \
if (common::g_log) { \
time_t now_time; \
...
...
@@ -174,14 +164,26 @@ extern Log *g_log;
struct tm *p = localtime(&now_time); \
char sz_head[64] = {0}; \
if (p) { \
sprintf(sz_head, "%d-%d-%d %d:%d:%u pid:%u tid:%llx ", p->tm_year + 1900, \
p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, \
(u32_t)getpid(), gettid()); \
sprintf(sz_head, \
"%d-%d-%d %d:%d:%u pid:%u tid:%llx ", \
p->tm_year + 1900, \
p->tm_mon + 1, \
p->tm_mday, \
p->tm_hour, \
p->tm_min, \
p->tm_sec, \
(u32_t)getpid(), \
gettid()); \
common::g_log->rotate(p->tm_year + 1900, p->tm_mon + 1, p->tm_mday); \
} \
snprintf(prefix, sizeof(prefix), "[%s %s %s %s %u]>>", sz_head, \
(common::g_log)->prefix_msg(level), __FILE_NAME__, \
__FUNCTION__, (u32_t)__LINE__); \
snprintf(prefix, \
sizeof(prefix), \
"[%s %s %s %s %u]>>", \
sz_head, \
(common::g_log)->prefix_msg(level), \
__FILE_NAME__, \
__FUNCTION__, \
(u32_t)__LINE__); \
}
#define LOG_OUTPUT(level, fmt, ...) \
...
...
@@ -194,8 +196,7 @@ extern Log *g_log;
} \
} while (0)
#define LOG_DEFAULT(fmt, ...) \
LOG_OUTPUT(common::g_log->get_log_level(), fmt, ##__VA_ARGS__)
#define LOG_DEFAULT(fmt, ...) LOG_OUTPUT(common::g_log->get_log_level(), fmt, ##__VA_ARGS__)
#define LOG_PANIC(fmt, ...) LOG_OUTPUT(common::LOG_LEVEL_PANIC, fmt, ##__VA_ARGS__)
#define LOG_ERROR(fmt, ...) LOG_OUTPUT(common::LOG_LEVEL_ERR, fmt, ##__VA_ARGS__)
#define LOG_WARN(fmt, ...) LOG_OUTPUT(common::LOG_LEVEL_WARN, fmt, ##__VA_ARGS__)
...
...
@@ -203,48 +204,56 @@ extern Log *g_log;
#define LOG_DEBUG(fmt, ...) LOG_OUTPUT(common::LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
#define LOG_TRACE(fmt, ...) LOG_OUTPUT(common::LOG_LEVEL_TRACE, fmt, ##__VA_ARGS__)
template
<
class
T
>
Log
&
Log
::
operator
<<
(
T
msg
)
{
template
<
class
T
>
Log
&
Log
::
operator
<<
(
T
msg
)
{
// at this time, the input level is the default log level
out
(
console_level_
,
log_level_
,
msg
);
return
*
this
;
}
template
<
class
T
>
int
Log
::
panic
(
T
message
)
{
template
<
class
T
>
int
Log
::
panic
(
T
message
)
{
return
out
(
LOG_LEVEL_PANIC
,
LOG_LEVEL_PANIC
,
message
);
}
template
<
class
T
>
int
Log
::
error
(
T
message
)
{
template
<
class
T
>
int
Log
::
error
(
T
message
)
{
return
out
(
LOG_LEVEL_ERR
,
LOG_LEVEL_ERR
,
message
);
}
template
<
class
T
>
int
Log
::
warnning
(
T
message
)
{
template
<
class
T
>
int
Log
::
warnning
(
T
message
)
{
return
out
(
LOG_LEVEL_WARN
,
LOG_LEVEL_WARN
,
message
);
}
template
<
class
T
>
int
Log
::
info
(
T
message
)
{
template
<
class
T
>
int
Log
::
info
(
T
message
)
{
return
out
(
LOG_LEVEL_INFO
,
LOG_LEVEL_INFO
,
message
);
}
template
<
class
T
>
int
Log
::
debug
(
T
message
)
{
template
<
class
T
>
int
Log
::
debug
(
T
message
)
{
return
out
(
LOG_LEVEL_DEBUG
,
LOG_LEVEL_DEBUG
,
message
);
}
template
<
class
T
>
int
Log
::
trace
(
T
message
)
{
template
<
class
T
>
int
Log
::
trace
(
T
message
)
{
return
out
(
LOG_LEVEL_TRACE
,
LOG_LEVEL_TRACE
,
message
);
}
template
<
class
T
>
int
Log
::
out
(
const
LOG_LEVEL
console_level
,
const
LOG_LEVEL
log_level
,
T
&
msg
)
{
template
<
class
T
>
int
Log
::
out
(
const
LOG_LEVEL
console_level
,
const
LOG_LEVEL
log_level
,
T
&
msg
)
{
bool
locked
=
false
;
if
(
console_level
<
LOG_LEVEL_PANIC
||
console_level
>
console_level_
||
log_level
<
LOG_LEVEL_PANIC
||
log_level
>
log_level_
)
{
if
(
console_level
<
LOG_LEVEL_PANIC
||
console_level
>
console_level_
||
log_level
<
LOG_LEVEL_PANIC
||
log_level
>
log_level_
)
{
return
LOG_STATUS_OK
;
}
try
{
...
...
@@ -288,9 +297,8 @@ int Log::out(const LOG_LEVEL console_level, const LOG_LEVEL log_level, T &msg) {
} while (0)
#endif // ASSERT
#define SYS_OUTPUT_FILE_POS \
", File:" << __FILE__ << ", line:" << __LINE__ << ",function:" << __FUNCTION__
#define SYS_OUTPUT_FILE_POS ", File:" << __FILE__ << ", line:" << __LINE__ << ",function:" << __FUNCTION__
#define SYS_OUTPUT_ERROR ",error:" << errno << ":" << strerror(errno)
}
//
namespace common
}
//
namespace common
#endif //__COMMON_LOG_LOG_H__
deps/common/math/md5.cpp
浏览文件 @
e976988d
...
...
@@ -41,10 +41,70 @@ static void Decode(UINT4 *, unsigned char *, unsigned int);
static
void
MD5_memcpy
(
POINTER
,
POINTER
,
unsigned
int
);
static
void
MD5_memset
(
POINTER
,
int
,
unsigned
int
);
static
unsigned
char
PADDING
[
64
]
=
{
0x80
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
};
static
unsigned
char
PADDING
[
64
]
=
{
0x80
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
};
/*
* F, G, H and I are basic MD5 functions.
...
...
@@ -91,7 +151,8 @@ static unsigned char PADDING[64] = {
/*
* MD5 initialization. Begins an MD5 operation, writing a new context.
*/
void
MD5Init
(
MD5_CTX
*
context
)
{
void
MD5Init
(
MD5_CTX
*
context
)
{
context
->
count
[
0
]
=
context
->
count
[
1
]
=
0
;
/*
* Load magic initialization constants.
...
...
@@ -106,16 +167,17 @@ void MD5Init(MD5_CTX *context) {
* MD5 block update operation. Continues an MD5 message-digest operation,
* processing another message block, and updating the context.
*/
void
MD5Update
(
MD5_CTX
*
context
,
unsigned
char
*
input
,
unsigned
int
inputLen
)
{
void
MD5Update
(
MD5_CTX
*
context
,
unsigned
char
*
input
,
unsigned
int
inputLen
)
{
unsigned
int
i
,
index
,
partLen
;
/* Compute number of bytes mod 64 */
index
=
(
unsigned
int
)
((
context
->
count
[
0
]
>>
3
)
&
0x3F
);
index
=
(
unsigned
int
)((
context
->
count
[
0
]
>>
3
)
&
0x3F
);
/* update number of bits */
if
((
context
->
count
[
0
]
+=
((
UINT4
)
inputLen
<<
3
))
<
((
UINT4
)
inputLen
<<
3
))
if
((
context
->
count
[
0
]
+=
((
UINT4
)
inputLen
<<
3
))
<
((
UINT4
)
inputLen
<<
3
))
context
->
count
[
1
]
++
;
context
->
count
[
1
]
+=
((
UINT4
)
inputLen
>>
29
);
context
->
count
[
1
]
+=
((
UINT4
)
inputLen
>>
29
);
partLen
=
64
-
index
;
...
...
@@ -123,7 +185,7 @@ void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputLen) {
* Transform as many times as possible.
*/
if
(
inputLen
>=
partLen
)
{
MD5_memcpy
((
POINTER
)
&
context
->
buffer
[
index
],
(
POINTER
)
input
,
partLen
);
MD5_memcpy
((
POINTER
)
&
context
->
buffer
[
index
],
(
POINTER
)
input
,
partLen
);
MD5Transform
(
context
->
state
,
context
->
buffer
);
for
(
i
=
partLen
;
i
+
63
<
inputLen
;
i
+=
64
)
...
...
@@ -134,15 +196,15 @@ void MD5Update(MD5_CTX *context, unsigned char *input, unsigned int inputLen) {
i
=
0
;
/* Buffer remaining input */
MD5_memcpy
((
POINTER
)
&
context
->
buffer
[
index
],
(
POINTER
)
&
input
[
i
],
inputLen
-
i
);
MD5_memcpy
((
POINTER
)
&
context
->
buffer
[
index
],
(
POINTER
)
&
input
[
i
],
inputLen
-
i
);
}
/*
* MD5 finalization. Ends an MD5 message-digest operation, writing the the
* message digest and zeroizing the context.
*/
void
MD5Final
(
unsigned
char
digest
[
16
],
MD5_CTX
*
context
)
{
void
MD5Final
(
unsigned
char
digest
[
16
],
MD5_CTX
*
context
)
{
unsigned
char
bits
[
8
];
unsigned
int
index
,
padLen
;
...
...
@@ -152,7 +214,7 @@ void MD5Final(unsigned char digest[16], MD5_CTX *context) {
/*
* Pad out to 56 mod 64.
*/
index
=
(
unsigned
int
)
((
context
->
count
[
0
]
>>
3
)
&
0x3f
);
index
=
(
unsigned
int
)((
context
->
count
[
0
]
>>
3
)
&
0x3f
);
padLen
=
(
index
<
56
)
?
(
56
-
index
)
:
(
120
-
index
);
MD5Update
(
context
,
PADDING
,
padLen
);
...
...
@@ -164,13 +226,14 @@ void MD5Final(unsigned char digest[16], MD5_CTX *context) {
/*
* Zeroize sensitive information.
*/
MD5_memset
((
POINTER
)
context
,
0
,
sizeof
(
*
context
));
MD5_memset
((
POINTER
)
context
,
0
,
sizeof
(
*
context
));
}
/*
* MD5 basic transformation. Transforms state based on block.
*/
static
void
MD5Transform
(
UINT4
state
[
4
],
unsigned
char
block
[
64
])
{
static
void
MD5Transform
(
UINT4
state
[
4
],
unsigned
char
block
[
64
])
{
UINT4
a
=
state
[
0
],
b
=
state
[
1
],
c
=
state
[
2
],
d
=
state
[
3
],
x
[
16
];
Decode
(
x
,
block
,
64
);
...
...
@@ -256,21 +319,22 @@ static void MD5Transform(UINT4 state[4], unsigned char block[64]) {
* Zeroize sensitive information.
*
*/
MD5_memset
((
POINTER
)
x
,
0
,
sizeof
(
x
));
MD5_memset
((
POINTER
)
x
,
0
,
sizeof
(
x
));
}
/*
* Encodes input (UINT4) into output (unsigned char). Assumes len is a
* multiple of 4.
*/
static
void
Encode
(
unsigned
char
*
output
,
UINT4
*
input
,
unsigned
int
len
)
{
static
void
Encode
(
unsigned
char
*
output
,
UINT4
*
input
,
unsigned
int
len
)
{
unsigned
int
i
,
j
;
for
(
i
=
0
,
j
=
0
;
j
<
len
;
i
++
,
j
+=
4
)
{
output
[
j
]
=
(
unsigned
char
)
(
input
[
i
]
&
0xff
);
output
[
j
+
1
]
=
(
unsigned
char
)
((
input
[
i
]
>>
8
)
&
0xff
);
output
[
j
+
2
]
=
(
unsigned
char
)
((
input
[
i
]
>>
16
)
&
0xff
);
output
[
j
+
3
]
=
(
unsigned
char
)
((
input
[
i
]
>>
24
)
&
0xff
);
output
[
j
]
=
(
unsigned
char
)(
input
[
i
]
&
0xff
);
output
[
j
+
1
]
=
(
unsigned
char
)((
input
[
i
]
>>
8
)
&
0xff
);
output
[
j
+
2
]
=
(
unsigned
char
)((
input
[
i
]
>>
16
)
&
0xff
);
output
[
j
+
3
]
=
(
unsigned
char
)((
input
[
i
]
>>
24
)
&
0xff
);
}
}
...
...
@@ -278,19 +342,21 @@ static void Encode(unsigned char *output, UINT4 *input, unsigned int len) {
* Decodes input (unsigned char) into output (UINT4). Assumes len is a
* multiple of 4.
*/
static
void
Decode
(
UINT4
*
output
,
unsigned
char
*
input
,
unsigned
int
len
)
{
static
void
Decode
(
UINT4
*
output
,
unsigned
char
*
input
,
unsigned
int
len
)
{
unsigned
int
i
,
j
;
for
(
i
=
0
,
j
=
0
;
j
<
len
;
i
++
,
j
+=
4
)
output
[
i
]
=
((
UINT4
)
input
[
j
])
|
(((
UINT4
)
input
[
j
+
1
])
<<
8
)
|
(((
UINT4
)
input
[
j
+
2
])
<<
16
)
|
(((
UINT4
)
input
[
j
+
3
])
<<
24
);
output
[
i
]
=
((
UINT4
)
input
[
j
])
|
(((
UINT4
)
input
[
j
+
1
])
<<
8
)
|
(((
UINT4
)
input
[
j
+
2
])
<<
16
)
|
(((
UINT4
)
input
[
j
+
3
])
<<
24
);
}
/*
* Note: Replace "for loop" with standard memcpy if possible.
*/
static
void
MD5_memcpy
(
POINTER
output
,
POINTER
input
,
unsigned
int
len
)
{
static
void
MD5_memcpy
(
POINTER
output
,
POINTER
input
,
unsigned
int
len
)
{
unsigned
int
i
;
for
(
i
=
0
;
i
<
len
;
i
++
)
...
...
@@ -300,36 +366,40 @@ static void MD5_memcpy(POINTER output, POINTER input, unsigned int len) {
/*
* Note: Replace "for loop" with standard memset if possible.
*/
static
void
MD5_memset
(
POINTER
output
,
int
value
,
unsigned
int
len
)
{
static
void
MD5_memset
(
POINTER
output
,
int
value
,
unsigned
int
len
)
{
unsigned
int
i
;
for
(
i
=
0
;
i
<
len
;
i
++
)
((
char
*
)
output
)[
i
]
=
(
char
)
value
;
((
char
*
)
output
)[
i
]
=
(
char
)
value
;
}
/*
* Digests a string
*/
int
MD5String
(
char
*
string
,
unsigned
char
digest
[
16
])
{
int
MD5String
(
char
*
string
,
unsigned
char
digest
[
16
])
{
MD5_CTX
context
;
unsigned
int
len
=
strlen
(
string
);
MD5Init
(
&
context
);
MD5Update
(
&
context
,
(
unsigned
char
*
)
string
,
len
);
MD5Update
(
&
context
,
(
unsigned
char
*
)
string
,
len
);
MD5Final
(
digest
,
&
context
);
return
0
;
}
int
MD5Buffer
(
char
*
buffer
,
unsigned
int
len
,
unsigned
char
digest
[
16
])
{
int
MD5Buffer
(
char
*
buffer
,
unsigned
int
len
,
unsigned
char
digest
[
16
])
{
MD5_CTX
context
;
MD5Init
(
&
context
);
MD5Update
(
&
context
,
(
unsigned
char
*
)
buffer
,
len
);
MD5Update
(
&
context
,
(
unsigned
char
*
)
buffer
,
len
);
MD5Final
(
digest
,
&
context
);
return
0
;
}
int
MD5File
(
char
*
filename
,
unsigned
char
digest
[
16
])
{
int
MD5File
(
char
*
filename
,
unsigned
char
digest
[
16
])
{
FILE
*
file
;
MD5_CTX
context
;
int
len
;
...
...
@@ -349,4 +419,4 @@ int MD5File(char *filename, unsigned char digest[16]) {
return
0
;
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/math/md5.h
浏览文件 @
e976988d
...
...
@@ -66,5 +66,5 @@ void MD5Final(unsigned char[16], MD5_CTX *);
}
#endif
}
//
namespace common
}
//
namespace common
#endif //__COMMON_MATH_MD5_H__
deps/common/math/random_generator.cpp
浏览文件 @
e976988d
...
...
@@ -12,27 +12,26 @@ See the Mulan PSL v2 for more details. */
// Created by Longda on 2021/4/20.
//
#include <chrono>
#include "common/math/random_generator.h"
namespace
common
{
RandomGenerator
::
RandomGenerator
()
:
randomData
(
std
::
chrono
::
system_clock
::
now
().
time_since_epoch
().
count
())
{}
RandomGenerator
::~
RandomGenerator
()
{}
RandomGenerator
::
RandomGenerator
()
:
randomData
(
std
::
chrono
::
system_clock
::
now
().
time_since_epoch
().
count
())
{}
unsigned
int
RandomGenerator
::
next
()
{
RandomGenerator
::~
RandomGenerator
()
{}
unsigned
int
RandomGenerator
::
next
()
{
return
randomData
();
}
unsigned
int
RandomGenerator
::
next
(
unsigned
int
range
)
{
unsigned
int
RandomGenerator
::
next
(
unsigned
int
range
)
{
if
(
range
>
0
)
{
return
next
()
%
range
;
}
else
{
...
...
@@ -40,4 +39,4 @@ unsigned int RandomGenerator::next(unsigned int range) {
}
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/math/random_generator.h
浏览文件 @
e976988d
...
...
@@ -16,13 +16,11 @@ See the Mulan PSL v2 for more details. */
#include <stdlib.h>
#include <random>
namespace
common
{
namespace
common
{
#define DEFAULT_RANDOM_BUFF_SIZE 512
class
RandomGenerator
{
class
RandomGenerator
{
public:
RandomGenerator
();
...
...
@@ -37,6 +35,6 @@ private:
std
::
mt19937
randomData
;
};
}
}
// namespace common
#endif
/* __COMMON_MATH_RANDOM_GENERATOR_H_ */
deps/common/math/regex.cpp
浏览文件 @
e976988d
...
...
@@ -19,7 +19,8 @@ See the Mulan PSL v2 for more details. */
#include "common/math/regex.h"
namespace
common
{
int
regex_match
(
const
char
*
str_
,
const
char
*
pat_
)
{
int
regex_match
(
const
char
*
str_
,
const
char
*
pat_
)
{
regex_t
reg
;
if
(
regcomp
(
&
reg
,
pat_
,
REG_EXTENDED
|
REG_NOSUB
))
return
-
1
;
...
...
@@ -29,4 +30,4 @@ int regex_match(const char *str_, const char *pat_) {
return
ret
;
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/math/regex.h
浏览文件 @
e976988d
...
...
@@ -18,5 +18,5 @@ namespace common {
int
regex_match
(
const
char
*
str_
,
const
char
*
pat_
);
}
//
namespace common
}
//
namespace common
#endif
/* __COMMON_MATH_REGEX_H__ */
deps/common/metrics/console_reporter.cpp
浏览文件 @
e976988d
...
...
@@ -21,13 +21,15 @@ See the Mulan PSL v2 for more details. */
namespace
common
{
ConsoleReporter
*
get_console_reporter
()
{
ConsoleReporter
*
get_console_reporter
()
{
static
ConsoleReporter
*
instance
=
new
ConsoleReporter
();
return
instance
;
}
void
ConsoleReporter
::
report
(
const
std
::
string
&
tag
,
Metric
*
metric
)
{
void
ConsoleReporter
::
report
(
const
std
::
string
&
tag
,
Metric
*
metric
)
{
Snapshot
*
snapshot
=
metric
->
get_snapshot
();
if
(
snapshot
!=
NULL
)
{
...
...
deps/common/metrics/console_reporter.h
浏览文件 @
e976988d
...
...
@@ -19,12 +19,11 @@ See the Mulan PSL v2 for more details. */
namespace
common
{
class
ConsoleReporter
:
public
Reporter
{
public:
void
report
(
const
std
::
string
&
tag
,
Metric
*
metric
);
};
ConsoleReporter
*
get_console_reporter
();
}
//
namespace common
ConsoleReporter
*
get_console_reporter
();
}
//
namespace common
#endif //__COMMON_METRICS_CONSOLE_REPORTER_H__
deps/common/metrics/histogram_snapshot.cpp
浏览文件 @
e976988d
...
...
@@ -24,24 +24,20 @@ See the Mulan PSL v2 for more details. */
namespace
common
{
HistogramSnapShot
::
HistogramSnapShot
()
{
}
{}
HistogramSnapShot
::
HistogramSnapShot
(
const
std
::
vector
<
double
>
&
collection
)
HistogramSnapShot
::
HistogramSnapShot
(
const
std
::
vector
<
double
>
&
collection
)
{
set_collection
(
collection
);
}
HistogramSnapShot
::~
HistogramSnapShot
()
{
}
{}
void
HistogramSnapShot
::
set_collection
(
const
std
::
vector
<
double
>
&
collection
)
void
HistogramSnapShot
::
set_collection
(
const
std
::
vector
<
double
>
&
collection
)
{
if
(
collection
.
empty
())
{
if
(
collection
.
empty
())
{
return
;
}
...
...
@@ -56,35 +52,30 @@ size_t HistogramSnapShot::size() const
double
HistogramSnapShot
::
get_value
(
double
quantile
)
{
if
(
quantile
>
1.0
f
)
{
if
(
quantile
>
1.0
f
)
{
quantile
=
1.0
f
;
}
if
(
quantile
<
0.0
f
)
{
if
(
quantile
<
0.0
f
)
{
quantile
=
0.0
f
;
}
if
(
data_
.
empty
())
{
if
(
data_
.
empty
())
{
return
0.0
f
;
}
double
pos
=
quantile
*
(
data_
.
size
()
+
1
);
if
(
pos
<
1
)
{
if
(
pos
<
1
)
{
return
data_
[
0
];
}
if
(
pos
>=
data_
.
size
())
{
if
(
pos
>=
data_
.
size
())
{
return
data_
[
data_
.
size
()
-
1
];
}
double
lower
=
data_
[(
int
)
pos
-
1
];
double
upper
=
data_
[(
int
)
pos
];
double
lower
=
data_
[(
int
)
pos
-
1
];
double
upper
=
data_
[(
int
)
pos
];
return
lower
+
(
pos
-
floor
(
pos
))
*
(
upper
-
lower
);
}
...
...
@@ -111,7 +102,6 @@ double HistogramSnapShot::get_95th()
double
HistogramSnapShot
::
get_99th
()
{
return
get_value
(
0.99
f
);
}
double
HistogramSnapShot
::
get_999th
()
{
...
...
@@ -120,8 +110,7 @@ double HistogramSnapShot::get_999th()
double
HistogramSnapShot
::
get_max
()
{
if
(
data_
.
empty
())
{
if
(
data_
.
empty
())
{
return
0.0
f
;
}
...
...
@@ -130,8 +119,7 @@ double HistogramSnapShot::get_max()
double
HistogramSnapShot
::
get_min
()
{
if
(
data_
.
empty
())
{
if
(
data_
.
empty
())
{
return
0.0
f
;
}
...
...
@@ -140,29 +128,25 @@ double HistogramSnapShot::get_min()
double
HistogramSnapShot
::
get_mean
()
{
if
(
data_
.
empty
())
{
if
(
data_
.
empty
())
{
return
0.0
f
;
}
return
std
::
accumulate
(
data_
.
begin
(),
data_
.
end
(),
(
double
)
0
)
*
1.0
f
/
data_
.
size
();
}
const
std
::
vector
<
double
>
&
HistogramSnapShot
::
get_values
()
const
std
::
vector
<
double
>
&
HistogramSnapShot
::
get_values
()
{
return
data_
;
}
std
::
string
HistogramSnapShot
::
to_string
()
{
std
::
string
HistogramSnapShot
::
to_string
()
{
std
::
stringstream
oss
;
oss
<<
"mean:"
<<
get_mean
()
<<
",min:"
<<
get_min
()
<<
",max:"
<<
get_max
()
<<
",median:"
<<
get_median
()
<<
", 75th:"
<<
get_75th
()
<<
",90th:"
<<
get_90th
()
<<
",99th:"
<<
get_99th
()
<<
",999th:"
<<
get_999th
();
oss
<<
"mean:"
<<
get_mean
()
<<
",min:"
<<
get_min
()
<<
",max:"
<<
get_max
()
<<
",median:"
<<
get_median
()
<<
", 75th:"
<<
get_75th
()
<<
",90th:"
<<
get_90th
()
<<
",99th:"
<<
get_99th
()
<<
",999th:"
<<
get_999th
();
return
oss
.
str
();
}
}
// namespace common
\ No newline at end of file
deps/common/metrics/histogram_snapshot.h
浏览文件 @
e976988d
...
...
@@ -65,6 +65,7 @@ public:
const
std
::
vector
<
double
>
&
get_values
();
std
::
string
to_string
();
protected:
std
::
vector
<
double
>
data_
;
};
...
...
deps/common/metrics/log_reporter.cpp
浏览文件 @
e976988d
...
...
@@ -19,23 +19,24 @@ See the Mulan PSL v2 for more details. */
#include "common/metrics/metric.h"
#include "common/log/log.h"
namespace
common
{
LogReporter
*
get_log_reporter
()
{
static
LogReporter
*
instance
=
new
LogReporter
();
LogReporter
*
get_log_reporter
()
{
static
LogReporter
*
instance
=
new
LogReporter
();
return
instance
;
}
void
LogReporter
::
report
(
const
std
::
string
&
tag
,
Metric
*
metric
)
{
void
LogReporter
::
report
(
const
std
::
string
&
tag
,
Metric
*
metric
)
{
Snapshot
*
snapshot
=
metric
->
get_snapshot
();
if
(
snapshot
!=
NULL
)
{
LOG_INFO
(
"%s:%s"
,
tag
.
c_str
(),
snapshot
->
to_string
().
c_str
());
}
else
{
}
else
{
LOG_WARN
(
"There is no snapshot of %s metrics."
,
tag
.
c_str
());
}
}
}
// namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/metrics/log_reporter.h
浏览文件 @
e976988d
...
...
@@ -19,12 +19,11 @@ See the Mulan PSL v2 for more details. */
namespace
common
{
class
LogReporter
:
public
Reporter
{
public:
void
report
(
const
std
::
string
&
tag
,
Metric
*
metric
);
};
LogReporter
*
get_log_reporter
();
}
//
namespace common
LogReporter
*
get_log_reporter
();
}
//
namespace common
#endif //__COMMON_METRICS_LOG_REPORTER_H__
deps/common/metrics/metric.h
浏览文件 @
e976988d
...
...
@@ -23,11 +23,14 @@ class Metric {
public:
virtual
void
snapshot
()
=
0
;
virtual
Snapshot
*
get_snapshot
()
{
return
snapshot_value_
;
}
virtual
Snapshot
*
get_snapshot
()
{
return
snapshot_value_
;
}
protected:
Snapshot
*
snapshot_value_
;
};
}
//
namespace common
}
//
namespace common
#endif //__COMMON_METRICS_METRIC_H__
deps/common/metrics/metrics.cpp
浏览文件 @
e976988d
...
...
@@ -16,7 +16,8 @@ See the Mulan PSL v2 for more details. */
#include "common/lang/mutex.h"
namespace
common
{
Meter
::
Meter
()
{
Meter
::
Meter
()
{
struct
timeval
start_time
;
gettimeofday
(
&
start_time
,
NULL
);
...
...
@@ -24,17 +25,26 @@ Meter::Meter() {
value_
.
store
(
0l
);
}
Meter
::~
Meter
()
{
Meter
::~
Meter
()
{
if
(
snapshot_value_
!=
NULL
)
{
delete
snapshot_value_
;
snapshot_value_
=
NULL
;
}
}
void
Meter
::
inc
(
long
increase
)
{
value_
.
fetch_add
(
increase
);
}
void
Meter
::
inc
()
{
inc
(
1l
);
}
void
Meter
::
inc
(
long
increase
)
{
value_
.
fetch_add
(
increase
);
}
void
Meter
::
inc
()
{
inc
(
1l
);
}
void
Meter
::
snapshot
()
{
void
Meter
::
snapshot
()
{
// lock here
struct
timeval
now
;
...
...
@@ -42,8 +52,7 @@ void Meter::snapshot() {
long
now_tick
=
now
.
tv_sec
*
1000000
+
now
.
tv_usec
;
double
temp_value
=
((
double
)
value_
.
exchange
(
0l
))
/
((
now_tick
-
snapshot_tick_
)
/
1000000
);
double
temp_value
=
((
double
)
value_
.
exchange
(
0l
))
/
((
now_tick
-
snapshot_tick_
)
/
1000000
);
snapshot_tick_
=
now_tick
;
if
(
snapshot_value_
==
NULL
)
{
...
...
@@ -52,21 +61,27 @@ void Meter::snapshot() {
((
SnapshotBasic
<
double
>
*
)
snapshot_value_
)
->
setValue
(
temp_value
);
}
SimpleTimer
::~
SimpleTimer
()
{
SimpleTimer
::~
SimpleTimer
()
{
if
(
snapshot_value_
!=
NULL
)
{
delete
snapshot_value_
;
snapshot_value_
=
NULL
;
}
}
void
SimpleTimer
::
inc
(
long
increase
)
{
void
SimpleTimer
::
inc
(
long
increase
)
{
value_
.
fetch_add
(
increase
);
times_
.
fetch_add
(
1
);
}
void
SimpleTimer
::
update
(
long
one
)
{
inc
(
one
);
}
void
SimpleTimer
::
update
(
long
one
)
{
inc
(
one
);
}
void
SimpleTimer
::
snapshot
()
{
void
SimpleTimer
::
snapshot
()
{
// lock here
struct
timeval
now
;
...
...
@@ -81,7 +96,7 @@ void SimpleTimer::snapshot() {
double
mean
=
0
;
if
(
times_snapshot
>
0
)
{
tps
=
((
double
)
times_snapshot
)
/
((
now_tick
-
snapshot_tick_
)
/
1000000
);
tps
=
((
double
)
times_snapshot
)
/
((
now_tick
-
snapshot_tick_
)
/
1000000
);
mean
=
((
double
)
value_snapshot
)
/
times_snapshot
;
}
...
...
@@ -93,21 +108,22 @@ void SimpleTimer::snapshot() {
((
SimplerTimerSnapshot
*
)
snapshot_value_
)
->
setValue
(
mean
,
tps
);
}
Histogram
::
Histogram
(
RandomGenerator
&
random
)
:
UniformReservoir
(
random
)
{}
Histogram
::
Histogram
(
RandomGenerator
&
random
,
size_t
size
)
:
UniformReservoir
(
random
,
size
)
{}
Histogram
::
Histogram
(
RandomGenerator
&
random
)
:
UniformReservoir
(
random
)
{}
Histogram
::~
Histogram
()
{
Histogram
::
Histogram
(
RandomGenerator
&
random
,
size_t
size
)
:
UniformReservoir
(
random
,
size
)
{}
}
Histogram
::~
Histogram
()
{}
void
Histogram
::
snapshot
()
{
void
Histogram
::
snapshot
()
{
UniformReservoir
::
snapshot
();
}
Timer
::
Timer
(
RandomGenerator
&
random
)
:
UniformReservoir
(
random
)
{
Timer
::
Timer
(
RandomGenerator
&
random
)
:
UniformReservoir
(
random
)
{
struct
timeval
start_time
;
gettimeofday
(
&
start_time
,
NULL
);
...
...
@@ -115,8 +131,8 @@ Timer::Timer(RandomGenerator &random)
value_
.
store
(
0l
);
}
Timer
::
Timer
(
RandomGenerator
&
random
,
size_t
size
)
:
UniformReservoir
(
random
,
size
)
{
Timer
::
Timer
(
RandomGenerator
&
random
,
size_t
size
)
:
UniformReservoir
(
random
,
size
)
{
struct
timeval
start_time
;
gettimeofday
(
&
start_time
,
NULL
);
...
...
@@ -124,19 +140,22 @@ Timer::Timer(RandomGenerator &random, size_t size)
value_
.
store
(
0l
);
}
Timer
::~
Timer
()
{
Timer
::~
Timer
()
{
if
(
snapshot_value_
==
NULL
)
{
delete
snapshot_value_
;
snapshot_value_
=
NULL
;
}
}
void
Timer
::
update
(
double
ms
)
{
void
Timer
::
update
(
double
ms
)
{
UniformReservoir
::
update
(
ms
);
value_
.
fetch_add
(
1l
);
}
void
Timer
::
snapshot
()
{
void
Timer
::
snapshot
()
{
if
(
snapshot_value_
==
NULL
)
{
snapshot_value_
=
new
TimerSnapshot
();
}
...
...
@@ -147,8 +166,7 @@ void Timer::snapshot() {
long
now_tick
=
now
.
tv_sec
*
1000000
+
now
.
tv_usec
;
double
tps
=
((
double
)
value_
.
exchange
(
0l
)
)
/
((
now_tick
-
snapshot_tick_
)
/
1000000
);
double
tps
=
((
double
)
value_
.
exchange
(
0l
))
/
((
now_tick
-
snapshot_tick_
)
/
1000000
);
snapshot_tick_
=
now_tick
;
MUTEX_LOCK
(
&
mutex
);
...
...
@@ -159,13 +177,14 @@ void Timer::snapshot() {
timer_snapshot
->
set_tps
(
tps
);
}
TimerStat
::
TimerStat
(
SimpleTimer
&
other_st
)
:
st_
(
other_st
),
start_tick_
(
0
),
end_tick_
(
0
)
{
TimerStat
::
TimerStat
(
SimpleTimer
&
other_st
)
:
st_
(
other_st
),
start_tick_
(
0
),
end_tick_
(
0
)
{
start
();
}
TimerStat
::~
TimerStat
()
{
TimerStat
::~
TimerStat
()
{
if
(
end_tick_
==
0
)
{
end
();
}
...
...
@@ -173,14 +192,16 @@ TimerStat::~TimerStat() {
st_
.
update
((
end_tick_
-
start_tick_
)
/
1000
);
}
void
TimerStat
::
start
()
{
void
TimerStat
::
start
()
{
struct
timeval
now
;
gettimeofday
(
&
now
,
NULL
);
start_tick_
=
now
.
tv_sec
*
1000000
+
now
.
tv_usec
;
}
void
TimerStat
::
end
()
{
void
TimerStat
::
end
()
{
struct
timeval
now
;
gettimeofday
(
&
now
,
NULL
);
...
...
deps/common/metrics/metrics.h
浏览文件 @
e976988d
...
...
@@ -27,11 +27,17 @@ namespace common {
class
Gauge
:
public
Metric
{
public:
// user implement snapshot function
void
set_snapshot
(
Snapshot
*
value
)
{
snapshot_value_
=
value
;
}
void
set_snapshot
(
Snapshot
*
value
)
{
snapshot_value_
=
value
;
}
};
class
Counter
:
public
Metric
{
void
set_snapshot
(
SnapshotBasic
<
long
>
*
value
)
{
snapshot_value_
=
value
;
}
void
set_snapshot
(
SnapshotBasic
<
long
>
*
value
)
{
snapshot_value_
=
value
;
}
};
class
Meter
:
public
Metric
{
...
...
@@ -76,7 +82,6 @@ public:
virtual
~
Histogram
();
void
snapshot
();
};
// timeunit is ms
...
...
deps/common/metrics/metrics_registry.cpp
浏览文件 @
e976988d
...
...
@@ -12,32 +12,33 @@ See the Mulan PSL v2 for more details. */
// Created by Longda on 2021/4/20.
//
#include "common/metrics/metrics_registry.h"
#include "common/log/log.h"
namespace
common
{
MetricsRegistry
&
get_metrics_registry
()
{
MetricsRegistry
&
get_metrics_registry
()
{
static
MetricsRegistry
instance
;
return
instance
;
}
void
MetricsRegistry
::
register_metric
(
const
std
::
string
&
tag
,
Metric
*
metric
)
{
std
::
map
<
std
::
string
,
Metric
*>::
iterator
it
=
metrics
.
find
(
tag
);
void
MetricsRegistry
::
register_metric
(
const
std
::
string
&
tag
,
Metric
*
metric
)
{
std
::
map
<
std
::
string
,
Metric
*>::
iterator
it
=
metrics
.
find
(
tag
);
if
(
it
!=
metrics
.
end
())
{
LOG_WARN
(
"%s has been registered!"
,
tag
.
c_str
());
return
;
}
//metrics[tag] = metric;
//
metrics[tag] = metric;
metrics
.
insert
(
std
::
pair
<
std
::
string
,
Metric
*>
(
tag
,
metric
));
LOG_INFO
(
"Successfully register metric :%s"
,
tag
.
c_str
());
}
void
MetricsRegistry
::
unregister
(
const
std
::
string
&
tag
)
{
void
MetricsRegistry
::
unregister
(
const
std
::
string
&
tag
)
{
unsigned
int
num
=
metrics
.
erase
(
tag
);
if
(
num
==
0
)
{
LOG_WARN
(
"There is no %s metric!"
,
tag
.
c_str
());
...
...
@@ -46,18 +47,18 @@ void MetricsRegistry::unregister(const std::string &tag) {
LOG_INFO
(
"Successfully remove metric of %s"
,
tag
.
c_str
());
}
void
MetricsRegistry
::
snapshot
()
{
std
::
map
<
std
::
string
,
Metric
*>::
iterator
it
=
metrics
.
begin
();
void
MetricsRegistry
::
snapshot
()
{
std
::
map
<
std
::
string
,
Metric
*>::
iterator
it
=
metrics
.
begin
();
for
(;
it
!=
metrics
.
end
();
it
++
)
{
it
->
second
->
snapshot
();
}
}
void
MetricsRegistry
::
report
()
{
for
(
std
::
list
<
Reporter
*>::
iterator
reporterIt
=
reporters
.
begin
();
reporterIt
!=
reporters
.
end
();
reporterIt
++
)
{
for
(
std
::
map
<
std
::
string
,
Metric
*>::
iterator
it
=
metrics
.
begin
();
it
!=
metrics
.
end
();
it
++
)
{
void
MetricsRegistry
::
report
()
{
for
(
std
::
list
<
Reporter
*>::
iterator
reporterIt
=
reporters
.
begin
();
reporterIt
!=
reporters
.
end
();
reporterIt
++
)
{
for
(
std
::
map
<
std
::
string
,
Metric
*>::
iterator
it
=
metrics
.
begin
();
it
!=
metrics
.
end
();
it
++
)
{
(
*
reporterIt
)
->
report
(
it
->
first
,
it
->
second
);
}
...
...
deps/common/metrics/metrics_registry.h
浏览文件 @
e976988d
...
...
@@ -26,7 +26,7 @@ namespace common {
class
MetricsRegistry
{
public:
MetricsRegistry
()
{};
MetricsRegistry
(){};
virtual
~
MetricsRegistry
(){};
void
register_metric
(
const
std
::
string
&
tag
,
Metric
*
metric
);
...
...
@@ -36,18 +36,16 @@ public:
void
report
();
void
add_reporter
(
Reporter
*
reporter
)
{
void
add_reporter
(
Reporter
*
reporter
)
{
reporters
.
push_back
(
reporter
);
}
protected:
std
::
map
<
std
::
string
,
Metric
*>
metrics
;
std
::
list
<
Reporter
*>
reporters
;
};
MetricsRegistry
&
get_metrics_registry
();
}
//
namespace common
MetricsRegistry
&
get_metrics_registry
();
}
//
namespace common
#endif //__COMMON_METRICS_METRICS_REGISTRY_H__
deps/common/metrics/reporter.h
浏览文件 @
e976988d
...
...
@@ -20,10 +20,9 @@ See the Mulan PSL v2 for more details. */
namespace
common
{
class
Reporter
{
public:
virtual
void
report
(
const
std
::
string
&
tag
,
Metric
*
metric
)
=
0
;
};
}
// namespace Reporter
}
// namespace common
#endif //__COMMON_METRICS_REPORTER_H__
deps/common/metrics/reservoir.cpp
浏览文件 @
e976988d
...
...
@@ -16,17 +16,13 @@ See the Mulan PSL v2 for more details. */
using
namespace
common
;
Reservoir
::
Reservoir
(
RandomGenerator
&
random
)
:
random
(
random
)
{
}
Reservoir
::
Reservoir
(
RandomGenerator
&
random
)
:
random
(
random
)
{}
Reservoir
::~
Reservoir
()
{
}
{}
size_t
Reservoir
::
next
(
size_t
range
)
{
return
random
.
next
(
range
);
}
deps/common/metrics/reservoir.h
浏览文件 @
e976988d
...
...
@@ -21,8 +21,6 @@ See the Mulan PSL v2 for more details. */
#include "common/metrics/metric.h"
#include "common/metrics/snapshot.h"
namespace
common
{
class
Reservoir
:
public
Metric
{
...
...
deps/common/metrics/sampler.cpp
浏览文件 @
e976988d
...
...
@@ -19,17 +19,21 @@ See the Mulan PSL v2 for more details. */
namespace
common
{
Sampler
*&
get_sampler
()
{
Sampler
*&
get_sampler
()
{
static
Sampler
*
g_sampler
=
new
Sampler
();
return
g_sampler
;
}
Sampler
::
Sampler
()
:
random_
()
{}
Sampler
::
Sampler
()
:
random_
()
{}
Sampler
::~
Sampler
()
{}
Sampler
::~
Sampler
()
{}
bool
Sampler
::
sampling
()
{
bool
Sampler
::
sampling
()
{
int
v
=
random_
.
next
(
RANGE_SIZE
);
if
(
v
<=
ratio_num_
)
{
return
true
;
...
...
@@ -38,9 +42,13 @@ bool Sampler::sampling() {
}
}
double
Sampler
::
get_ratio
()
{
return
ratio_
;
}
double
Sampler
::
get_ratio
()
{
return
ratio_
;
}
void
Sampler
::
set_ratio
(
double
ratio
)
{
void
Sampler
::
set_ratio
(
double
ratio
)
{
if
(
0
<=
ratio
&&
ratio
<=
1
)
{
this
->
ratio_
=
ratio
;
ratio_num_
=
ratio
*
RANGE_SIZE
;
...
...
@@ -49,5 +57,4 @@ void Sampler::set_ratio(double ratio) {
}
}
}
//namespace common
}
// namespace common
deps/common/metrics/sampler.h
浏览文件 @
e976988d
...
...
@@ -19,7 +19,6 @@ See the Mulan PSL v2 for more details. */
namespace
common
{
/**
* The most simple sample function
*/
...
...
@@ -40,5 +39,5 @@ private:
};
Sampler
*&
get_sampler
();
}
//
namespace common
}
//
namespace common
#endif //__COMMON_METRICS_SAMPLER_H__
deps/common/metrics/snapshot.h
浏览文件 @
e976988d
...
...
@@ -20,25 +20,30 @@ See the Mulan PSL v2 for more details. */
namespace
common
{
class
Snapshot
{
public:
virtual
~
Snapshot
()
{};
virtual
~
Snapshot
(){};
virtual
std
::
string
to_string
()
=
0
;
};
template
<
class
T
>
class
SnapshotBasic
:
public
Snapshot
{
public:
SnapshotBasic
()
:
value
(){
SnapshotBasic
()
:
value
(){
};
virtual
~
SnapshotBasic
()
{}
virtual
~
SnapshotBasic
()
{}
void
setValue
(
T
&
input
)
{
value
=
input
;
}
void
setValue
(
T
&
input
)
{
value
=
input
;
}
std
::
string
to_string
()
{
std
::
string
to_string
()
{
std
::
string
ret
;
val_to_str
(
value
,
ret
);
return
ret
;
...
...
@@ -48,28 +53,31 @@ private:
T
value
;
};
class
SimplerTimerSnapshot
:
public
Snapshot
{
class
SimplerTimerSnapshot
:
public
Snapshot
{
public:
SimplerTimerSnapshot
()
{
SimplerTimerSnapshot
()
{}
}
virtual
~
SimplerTimerSnapshot
()
{}
virtual
~
SimplerTimerSnapshot
()
{}
void
setValue
(
double
mean
,
double
tps
)
{
void
setValue
(
double
mean
,
double
tps
)
{
this
->
mean
=
mean
;
this
->
tps
=
tps
;
}
std
::
string
to_string
()
{
std
::
string
to_string
()
{
std
::
stringstream
oss
;
oss
<<
"mean:"
<<
mean
<<
",tps:"
<<
tps
;
oss
<<
"mean:"
<<
mean
<<
",tps:"
<<
tps
;
return
oss
.
str
();
}
private:
double
mean
=
1.0
;
double
tps
=
1.0
;
};
}
//
namespace common
}
//
namespace common
#endif //__COMMON_METRICS_SNAPSHOT_H__
deps/common/metrics/timer_snapshot.cpp
浏览文件 @
e976988d
...
...
@@ -17,15 +17,24 @@ See the Mulan PSL v2 for more details. */
namespace
common
{
TimerSnapshot
::
TimerSnapshot
()
{}
TimerSnapshot
::
TimerSnapshot
()
{}
TimerSnapshot
::~
TimerSnapshot
()
{}
TimerSnapshot
::~
TimerSnapshot
()
{}
double
TimerSnapshot
::
get_tps
()
{
return
tps
;
}
double
TimerSnapshot
::
get_tps
()
{
return
tps
;
}
void
TimerSnapshot
::
set_tps
(
double
tps
)
{
this
->
tps
=
tps
;
}
void
TimerSnapshot
::
set_tps
(
double
tps
)
{
this
->
tps
=
tps
;
}
std
::
string
TimerSnapshot
::
to_string
()
{
std
::
string
TimerSnapshot
::
to_string
()
{
std
::
stringstream
oss
;
oss
<<
HistogramSnapShot
::
to_string
()
<<
",tps:"
<<
tps
;
...
...
deps/common/metrics/timer_snapshot.h
浏览文件 @
e976988d
...
...
@@ -27,8 +27,9 @@ public:
void
set_tps
(
double
tps
);
std
::
string
to_string
();
protected:
double
tps
=
1.0
;
};
}
//
namespace common
}
//
namespace common
#endif //__COMMON_METRICS_TIMER_SNAPSHOT_H__
deps/common/metrics/uniform_reservoir.cpp
浏览文件 @
e976988d
...
...
@@ -23,8 +23,8 @@ namespace common {
#define DEFAULT_SIZE 1023
UniformReservoir
::
UniformReservoir
(
RandomGenerator
&
random
)
:
Reservoir
(
random
),
counter
(
0
)
{
UniformReservoir
::
UniformReservoir
(
RandomGenerator
&
random
)
:
Reservoir
(
random
),
counter
(
0
)
{
pthread_mutexattr_t
mutexatr
;
pthread_mutexattr_init
(
&
mutexatr
);
pthread_mutexattr_settype
(
&
mutexatr
,
PTHREAD_MUTEX_RECURSIVE
);
...
...
@@ -34,8 +34,8 @@ UniformReservoir::UniformReservoir(RandomGenerator &random)
init
(
DEFAULT_SIZE
);
}
UniformReservoir
::
UniformReservoir
(
RandomGenerator
&
random
,
size_t
size
)
:
Reservoir
(
random
),
counter
(
0
)
{
UniformReservoir
::
UniformReservoir
(
RandomGenerator
&
random
,
size_t
size
)
:
Reservoir
(
random
),
counter
(
0
)
{
pthread_mutexattr_t
mutexatr
;
pthread_mutexattr_init
(
&
mutexatr
);
...
...
@@ -45,35 +45,40 @@ UniformReservoir::UniformReservoir(RandomGenerator &random, size_t size)
init
(
size
);
}
UniformReservoir
::~
UniformReservoir
()
{
UniformReservoir
::~
UniformReservoir
()
{
if
(
snapshot_value_
==
NULL
)
{
delete
snapshot_value_
;
snapshot_value_
=
NULL
;
}
}
void
UniformReservoir
::
init
(
size_t
size
)
{
void
UniformReservoir
::
init
(
size_t
size
)
{
MUTEX_LOCK
(
&
mutex
);
counter
=
0
;
data
.
resize
(
size
);
MUTEX_UNLOCK
(
&
mutex
);
}
size_t
UniformReservoir
::
size
()
{
size_t
UniformReservoir
::
size
()
{
MUTEX_LOCK
(
&
mutex
);
size_t
size
=
(
counter
<
data
.
size
())
?
counter
:
data
.
size
();
MUTEX_UNLOCK
(
&
mutex
);
return
size
;
}
size_t
UniformReservoir
::
get_count
()
{
size_t
UniformReservoir
::
get_count
()
{
MUTEX_LOCK
(
&
mutex
);
size_t
ret
=
counter
;
MUTEX_UNLOCK
(
&
mutex
);
return
ret
;
}
void
UniformReservoir
::
update
(
double
value
)
{
void
UniformReservoir
::
update
(
double
value
)
{
MUTEX_LOCK
(
&
mutex
);
size_t
count
=
++
counter
;
...
...
@@ -87,7 +92,8 @@ void UniformReservoir::update(double value) {
MUTEX_UNLOCK
(
&
mutex
);
}
void
UniformReservoir
::
snapshot
()
{
void
UniformReservoir
::
snapshot
()
{
MUTEX_LOCK
(
&
mutex
);
std
::
vector
<
double
>
output
=
data
;
MUTEX_UNLOCK
(
&
mutex
);
...
...
@@ -98,7 +104,8 @@ void UniformReservoir::snapshot() {
((
HistogramSnapShot
*
)
snapshot_value_
)
->
set_collection
(
output
);
}
void
UniformReservoir
::
reset
()
{
void
UniformReservoir
::
reset
()
{
MUTEX_LOCK
(
&
mutex
);
counter
=
0
;
...
...
deps/common/metrics/uniform_reservoir.h
浏览文件 @
e976988d
deps/common/mm/debug_new.cpp.skip
浏览文件 @
e976988d
...
...
@@ -58,7 +58,8 @@ static new_ptr_list_t *new_ptr_list[DEBUG_NEW_HASHTABLESIZE];
bool new_verbose_flag = false;
bool new_autocheck_flag = true;
bool check_leaks() {
bool check_leaks()
{
bool fLeaked = false;
for (int i = 0; i < DEBUG_NEW_HASHTABLESIZE; ++i) {
new_ptr_list_t *ptr = new_ptr_list[i];
...
...
@@ -68,7 +69,9 @@ bool check_leaks() {
while (ptr) {
printf("Leaked object at %p (size %llu, %s:%d)\n",
(char *)ptr + sizeof(new_ptr_list_t),
(unsigned long long)ptr->size, ptr->file, ptr->line);
(unsigned long long)ptr->size,
ptr->file,
ptr->line);
ptr = ptr->next;
}
}
...
...
@@ -78,7 +81,8 @@ bool check_leaks() {
return false;
}
void *operator new(size_t size, const char *file, int line) {
void *operator new(size_t size, const char *file, int line)
{
size_t s = size + sizeof(new_ptr_list_t);
new_ptr_list_t *ptr = (new_ptr_list_t *)malloc(s);
if (ptr == NULL) {
...
...
@@ -102,23 +106,33 @@ void *operator new(size_t size, const char *file, int line) {
return pointer;
}
void *operator new[](size_t size, const char *file, int line) {
void *operator new[](size_t size, const char *file, int line)
{
return operator new(size, file, line);
}
void *operator new(size_t size) { return operator new(size, "<Unknown>", 0); }
void *operator new(size_t size)
{
return operator new(size, "<Unknown>", 0);
}
void *operator new[](size_t size) { return operator new(size); }
void *operator new[](size_t size)
{
return operator new(size);
}
void *operator new(size_t size, const std::nothrow_t &) throw() {
void *operator new(size_t size, const std::nothrow_t &) throw()
{
return operator new(size);
}
void *operator new[](size_t size, const std::nothrow_t &) throw() {
void *operator new[](size_t size, const std::nothrow_t &) throw()
{
return operator new[](size);
}
void operator delete(void *pointer) {
void operator delete(void *pointer)
{
if (pointer == NULL)
return;
size_t hash_index = DEBUG_NEW_HASH(pointer);
...
...
@@ -142,7 +156,10 @@ void operator delete(void *pointer) {
abort();
}
void operator delete[](void *pointer) { operator delete(pointer); }
void operator delete[](void *pointer)
{
operator delete(pointer);
}
// Some older compilers like Borland C++ Compiler 5.5.1 and Digital Mars
// Compiler 8.29 do not support placement delete operators.
...
...
@@ -151,22 +168,25 @@ void operator delete[](void *pointer) { operator delete(pointer); }
// is thrown in the initialization (constructor) of a dynamically
// created object.
#ifndef NO_PLACEMENT_DELETE
void operator delete(void *pointer, const char *file, int line) {
void operator delete(void *pointer, const char *file, int line)
{
if (new_verbose_flag)
printf("info: exception thrown on initializing object at %p (%s:%d)\n",
pointer, file, line);
printf("info: exception thrown on initializing object at %p (%s:%d)\n", pointer, file, line);
operator delete(pointer);
}
void operator delete[](void *pointer, const char *file, int line) {
void operator delete[](void *pointer, const char *file, int line)
{
operator delete(pointer, file, line);
}
void operator delete(void *pointer, const std::nothrow_t &) {
void operator delete(void *pointer, const std::nothrow_t &)
{
operator delete(pointer, "<Unknown>", 0);
}
void operator delete[](void *pointer, const std::nothrow_t &) {
void operator delete[](void *pointer, const std::nothrow_t &)
{
operator delete(pointer, std::nothrow);
}
#endif // NO_PLACEMENT_DELETE
...
...
@@ -174,8 +194,10 @@ void operator delete[](void *pointer, const std::nothrow_t &) {
// Proxy class to automatically call check_leaks if new_autocheck_flag is set
class new_check_t {
public:
new_check_t() {}
~new_check_t() {
new_check_t()
{}
~new_check_t()
{
if (new_autocheck_flag) {
// Check for leakage.
// If any leaks are found, set new_verbose_flag so that any
...
...
deps/common/mm/debug_new.h
浏览文件 @
e976988d
...
...
@@ -47,5 +47,5 @@ void operator delete[](void *); // MSVC 6 requires this declaration
extern
bool
new_verbose_flag
;
// default to false: no verbose information
extern
bool
new_autocheck_flag
;
// default to true: call check_leaks() on exit
}
//
namespace common
}
//
namespace common
#endif // __COMMON_MM_DEBUG_NEW_H__
deps/common/mm/mem.cpp.skip
浏览文件 @
e976988d
...
...
@@ -28,8 +28,8 @@ MemID *CLMemTrace::mMemIDs[MEM_HASHTABLE_SIZE] = {0};
bool CLMemTrace::mVerbose = false;
;
void *CLMemTrace::malloc(size_t size, const char *file, const int line,
bool retry) throw(std::bad_alloc)
{
void *CLMemTrace::malloc(size_t size, const char *file, const int line,
bool retry) throw(std::bad_alloc)
{
size_t allocSize = size + sizeof(MemID);
void *usedPointer = NULL;
...
...
@@ -81,8 +81,8 @@ void *CLMemTrace::malloc(size_t size, const char *file, const int line,
return NULL;
}
void *CLMemTrace::realloc(void *pointer, size_t size, const char *file,
const int line)
{
void *CLMemTrace::realloc(void *pointer, size_t size, const char *file,
const int line)
{
if (pointer == NULL && size == 0) {
return NULL;
} else if (pointer == NULL && size != 0) {
...
...
@@ -172,8 +172,7 @@ void *CLMemTrace::realloc(void *pointer, size_t size, const char *file,
/**
* Secondly, add the new one to table
*/
u64_t newHashIndex =
(u64_t)MEM_ID_HASH((char *)pNewMemID + sizeof(MemID));
u64_t newHashIndex = (u64_t)MEM_ID_HASH((char *)pNewMemID + sizeof(MemID));
pNewMemID->mNext = mMemIDs[newHashIndex];
mMemIDs[newHashIndex] = pNewMemID;
...
...
@@ -182,8 +181,7 @@ void *CLMemTrace::realloc(void *pointer, size_t size, const char *file,
* Third, do memory copy
* to simplify the old logic, copy memory here
*/
memcpy((char *)pNewMemID + sizeof(MemID),
(char *)pFreeMemID + sizeof(MemID), pFreeMemID->mSize);
memcpy((char *)pNewMemID + sizeof(MemID), (char *)pFreeMemID + sizeof(MemID), pFreeMemID->mSize);
break;
}
}
...
...
@@ -191,9 +189,7 @@ void *CLMemTrace::realloc(void *pointer, size_t size, const char *file,
MUTEX_UNLOCK(&mMutex);
if (foundOld == false) {
LOG_WARN(
"Something is wrong, the old pointer %p isn't found, so alloc new one",
pointer);
LOG_WARN("Something is wrong, the old pointer %p isn't found, so alloc new one", pointer);
try {
return malloc(size, file, line, false);
} catch (std::bad_alloc &e) {
...
...
@@ -203,8 +199,7 @@ void *CLMemTrace::realloc(void *pointer, size_t size, const char *file,
}
if (mVerbose) {
LOG_INFO("Delete %p, file:%s, line:%u, size:%llu", pointer, oldMemID.mFile,
oldMemID.mLine, oldMemID.mSize);
LOG_INFO("Delete %p, file:%s, line:%u, size:%llu", pointer, oldMemID.mFile, oldMemID.mLine, oldMemID.mSize);
}
if (pFreeMemID) {
...
...
@@ -215,8 +210,10 @@ void *CLMemTrace::realloc(void *pointer, size_t size, const char *file,
if (mVerbose) {
LOG_INFO("Alloc %p, file:%s, line:%u, size:%llu",
(char *)pNewMemID + sizeof(MemID), pNewMemID->mFile,
pNewMemID->mLine, pNewMemID->mSize);
(char *)pNewMemID + sizeof(MemID),
pNewMemID->mFile,
pNewMemID->mLine,
pNewMemID->mSize);
}
return pNewMemID;
}
...
...
@@ -224,7 +221,8 @@ void *CLMemTrace::realloc(void *pointer, size_t size, const char *file,
return NULL;
}
void CLMemTrace::free(void *pointer) {
void CLMemTrace::free(void *pointer)
{
if (pointer == NULL) {
LOG_WARN("Free one empty pointer");
return;
...
...
@@ -260,8 +258,7 @@ void CLMemTrace::free(void *pointer) {
if (pMemID) {
if (mVerbose) {
LOG_INFO("Delete %p, file:%s, line:%u, size:%llu", pointer, pMemID->mFile,
pMemID->mLine, pMemID->mSize);
LOG_INFO("Delete %p, file:%s, line:%u, size:%llu", pointer, pMemID->mFile, pMemID->mLine, pMemID->mSize);
}
::free(pMemID);
return;
...
...
@@ -273,7 +270,8 @@ void CLMemTrace::free(void *pointer) {
return;
}
std::new_handler CLMemTrace::getNewHandler() {
std::new_handler CLMemTrace::getNewHandler()
{
std::new_handler newHandler = NULL;
MUTEX_LOCK(&mMutex);
...
...
@@ -285,7 +283,8 @@ std::new_handler CLMemTrace::getNewHandler() {
return newHandler;
}
void CLMemTrace::output() {
void CLMemTrace::output()
{
for (int i = 0; i < MEM_HASHTABLE_SIZE; ++i) {
// Don't lock outside of the loop
// 1. avoid output too long to alloc/free memory
...
...
@@ -298,8 +297,8 @@ void CLMemTrace::output() {
}
while (ptr) {
// if LOG_INFO alloc memory, it will easy leading to dead lock
LOG_INFO(
"Exist %p, file:%s, line:%u, size:%llu",
(char *)ptr + sizeof(MemID), ptr->mFile, ptr->mLine, ptr->mSize);
LOG_INFO(
"Exist %p, file:%s, line:%u, size:%llu",
(char *)ptr + sizeof(MemID), ptr->mFile, ptr->mLine, ptr->mSize);
ptr = ptr->mNext;
}
...
...
@@ -307,23 +306,28 @@ void CLMemTrace::output() {
}
}
void *operator new(std::size_t size, const char *file, int line) {
void *operator new(std::size_t size, const char *file, int line)
{
return CLMemTrace::malloc(size, file, line, true);
}
void *operator new[](std::size_t size, const char *file, int line) {
void *operator new[](std::size_t size, const char *file, int line)
{
return operator new(size, file, line);
}
void *operator new(std::size_t size) throw(std::bad_alloc) {
void *operator new(std::size_t size) throw(std::bad_alloc)
{
return operator new(size, "<Unknown>", 0);
}
void *operator new[](std::size_t size) throw(std::bad_alloc) {
void *operator new[](std::size_t size) throw(std::bad_alloc)
{
return operator new(size);
}
void *operator new(std::size_t size, const std::nothrow_t &) throw() {
void *operator new(std::size_t size, const std::nothrow_t &) throw()
{
void *pointer = NULL;
try {
pointer = operator new(size);
...
...
@@ -334,7 +338,8 @@ void *operator new(std::size_t size, const std::nothrow_t &) throw() {
return pointer;
}
void *operator new[](std::size_t size, const std::nothrow_t &) throw() {
void *operator new[](std::size_t size, const std::nothrow_t &) throw()
{
void *pointer = NULL;
try {
pointer = operator[] new(size);
...
...
@@ -345,9 +350,15 @@ void *operator new[](std::size_t size, const std::nothrow_t &) throw() {
return pointer;
}
void operator delete(void *pointer) { CLMemTrace::free(pointer); }
void operator delete(void *pointer)
{
CLMemTrace::free(pointer);
}
void operator delete[](void *pointer) { operator delete(pointer); }
void operator delete[](void *pointer)
{
operator delete(pointer);
}
// Some older compilers like Borland C++ Compiler 5.5.1 and Digital Mars
// Compiler 8.29 do not support placement delete operators.
...
...
@@ -355,23 +366,28 @@ void operator delete[](void *pointer) { operator delete(pointer); }
// Also note that in that case memory leakage will occur if an exception
// is thrown in the initialization (constructor) of a dynamically
// created object.
void operator delete(void *pointer, const char *file, int line) {
void operator delete(void *pointer, const char *file, int line)
{
operator delete(pointer);
}
void operator delete[](void *pointer, const char *file, int line) {
void operator delete[](void *pointer, const char *file, int line)
{
operator delete(pointer, file, line);
}
void operator delete(void *pointer, const std::nothrow_t &) {
void operator delete(void *pointer, const std::nothrow_t &)
{
operator delete(pointer, "<Unknown>", 0);
}
void operator delete[](void *pointer, const std::nothrow_t &) {
void operator delete[](void *pointer, const std::nothrow_t &)
{
operator delete(pointer, std::nothrow);
}
void *Lcalloc(size_t nmemb, size_t size, const char *file, const int line) {
void *Lcalloc(size_t nmemb, size_t size, const char *file, const int line)
{
try {
void *point = CLMemTrace::malloc(size * nmemb, file, line, false);
if (point) {
...
...
@@ -384,7 +400,8 @@ void *Lcalloc(size_t nmemb, size_t size, const char *file, const int line) {
return pointer;
}
void *Lmalloc(size_t size, const char *file, const int line) {
void *Lmalloc(size_t size, const char *file, const int line)
{
try {
void *point = CLMemTrace::malloc(size, file, line, false);
} catch (std::bad_alloc &e) {
...
...
@@ -394,8 +411,12 @@ void *Lmalloc(size_t size, const char *file, const int line) {
return pointer;
}
void Lfree(void *ptr) { CLMemTrace::free(pointer); }
void *Lrealloc(void *ptr, size_t size, const char *file, const int line) {
void Lfree(void *ptr)
{
CLMemTrace::free(pointer);
}
void *Lrealloc(void *ptr, size_t size, const char *file, const int line)
{
// simplify the logic
return CLMemTrace::realloc(ptr, size, file, line);
}
...
...
deps/common/mm/mem.h
浏览文件 @
e976988d
...
...
@@ -31,8 +31,6 @@ namespace common {
#else
typedef
struct
MemID_t
{
public:
const
static
int
MEM_FILENAME_LEN
=
32
;
...
...
@@ -44,12 +42,10 @@ public:
class
CLMemTrace
{
public:
static
void
*
malloc
(
size_t
size
,
const
char
*
file
,
const
int
line
,
bool
retry
=
false
)
throw
(
std
::
bad_alloc
);
static
void
*
malloc
(
size_t
size
,
const
char
*
file
,
const
int
line
,
bool
retry
=
false
)
throw
(
std
::
bad_alloc
);
// just use for realloc, same functionality as realloc
static
void
*
realloc
(
void
*
ptr
,
size_t
size
,
const
char
*
file
,
const
int
line
);
static
void
*
realloc
(
void
*
ptr
,
size_t
size
,
const
char
*
file
,
const
int
line
);
static
void
free
(
void
*
ptr
);
...
...
@@ -58,7 +54,10 @@ public:
/**
* set whether show every details
*/
static
void
setVerbose
(
bool
verbose
)
{
mVerbose
=
verbose
;
}
static
void
setVerbose
(
bool
verbose
)
{
mVerbose
=
verbose
;
}
protected:
static
std
::
new_handler
getNewHandler
();
...
...
@@ -132,5 +131,5 @@ static void operator delete[](void *pointer);
#endif
/* MEM_DEBUG */
}
//
namespace common
}
//
namespace common
#endif
/* __COMMON_MM_MEM_H__ */
deps/common/os/os.cpp
浏览文件 @
e976988d
...
...
@@ -21,7 +21,8 @@ See the Mulan PSL v2 for more details. */
namespace
common
{
// Don't care windows
u32_t
getCpuNum
()
{
u32_t
getCpuNum
()
{
return
std
::
thread
::
hardware_concurrency
();
}
...
...
@@ -30,14 +31,13 @@ u32_t getCpuNum() {
void
print_stacktrace
()
{
int
size
=
MAX_STACK_SIZE
;
void
*
array
[
MAX_STACK_SIZE
];
void
*
array
[
MAX_STACK_SIZE
];
int
stack_num
=
backtrace
(
array
,
size
);
char
**
stacktrace
=
backtrace_symbols
(
array
,
stack_num
);
for
(
int
i
=
0
;
i
<
stack_num
;
++
i
)
{
char
**
stacktrace
=
backtrace_symbols
(
array
,
stack_num
);
for
(
int
i
=
0
;
i
<
stack_num
;
++
i
)
{
LOG_INFO
(
"%d ----- %s
\n
"
,
i
,
stacktrace
[
i
]);
}
free
(
stacktrace
);
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/os/os.h
浏览文件 @
e976988d
...
...
@@ -20,5 +20,5 @@ u32_t getCpuNum();
void
print_stacktrace
();
}
//
namespace common
}
//
namespace common
#endif
/* __COMMON_OS_OS_H__ */
deps/common/os/path.h
浏览文件 @
e976988d
...
...
@@ -18,7 +18,6 @@ See the Mulan PSL v2 for more details. */
#include <string>
namespace
common
{
/**
* get file name from full path
* example
...
...
@@ -68,5 +67,5 @@ bool check_directory(std::string &path);
*/
int
list_file
(
const
char
*
path
,
const
char
*
filter_pattern
,
std
::
vector
<
std
::
string
>
&
files
);
// io/io.h::getFileList
}
//
namespace common
}
//
namespace common
#endif //__COMMON_OS_PATH_H__
deps/common/os/pidfile.cpp
浏览文件 @
e976988d
...
...
@@ -26,25 +26,26 @@ See the Mulan PSL v2 for more details. */
#include "common/os/pidfile.h"
namespace
common
{
std
::
string
&
getPidPath
()
{
std
::
string
&
getPidPath
()
{
static
std
::
string
path
;
return
path
;
}
void
setPidPath
(
const
char
*
progName
)
{
void
setPidPath
(
const
char
*
progName
)
{
std
::
string
&
path
=
getPidPath
();
if
(
progName
!=
NULL
)
{
path
=
std
::
string
(
_PATH_TMP
)
+
progName
+
".pid"
;
}
else
{
}
else
{
path
=
""
;
}
}
int
writePidFile
(
const
char
*
progName
)
{
int
writePidFile
(
const
char
*
progName
)
{
assert
(
progName
);
std
::
ofstream
ostr
;
int
rv
=
1
;
...
...
@@ -58,15 +59,14 @@ int writePidFile(const char *progName) {
rv
=
0
;
}
else
{
rv
=
errno
;
std
::
cerr
<<
"error opening PID file "
<<
path
.
c_str
()
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
std
::
cerr
<<
"error opening PID file "
<<
path
.
c_str
()
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
}
return
rv
;
}
void
removePidFile
(
void
)
{
void
removePidFile
(
void
)
{
std
::
string
path
=
getPidPath
();
if
(
!
path
.
empty
())
{
unlink
(
path
.
c_str
());
...
...
@@ -75,5 +75,4 @@ void removePidFile(void) {
return
;
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/os/pidfile.h
浏览文件 @
e976988d
...
...
@@ -16,7 +16,6 @@ See the Mulan PSL v2 for more details. */
#define __COMMON_OS_PIDFILE_H__
namespace
common
{
//! Generates a PID file for the current component
/**
* Gets the process ID (PID) of the calling process and writes a file
...
...
@@ -35,7 +34,7 @@ int writePidFile(const char *progName);
*/
void
removePidFile
(
void
);
std
::
string
&
getPidPath
();
std
::
string
&
getPidPath
();
}
//
namespace common
}
//
namespace common
#endif // __COMMON_OS_PIDFILE_H__
deps/common/os/process.cpp
浏览文件 @
e976988d
...
...
@@ -35,7 +35,8 @@ namespace common {
#define RWRR (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
std
::
string
get_process_name
(
const
char
*
prog_name
)
{
std
::
string
get_process_name
(
const
char
*
prog_name
)
{
std
::
string
process_name
;
int
buf_len
=
strlen
(
prog_name
);
...
...
@@ -44,8 +45,7 @@ std::string get_process_name(const char *prog_name) {
char
*
buf
=
new
char
[
buf_len
+
1
];
if
(
buf
==
NULL
)
{
std
::
cerr
<<
"Failed to alloc memory for program name."
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
std
::
cerr
<<
"Failed to alloc memory for program name."
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
return
""
;
}
memset
(
buf
,
0
,
buf_len
+
1
);
...
...
@@ -59,7 +59,8 @@ std::string get_process_name(const char *prog_name) {
// Background the process by detaching it from the console and redirecting
// std in, out, and err to /dev/null
int
daemonize_service
(
bool
close_std_streams
)
{
int
daemonize_service
(
bool
close_std_streams
)
{
int
nochdir
=
1
;
int
noclose
=
close_std_streams
?
0
:
1
;
int
rc
=
daemon
(
nochdir
,
noclose
);
...
...
@@ -70,7 +71,8 @@ int daemonize_service(bool close_std_streams) {
return
rc
;
}
int
daemonize_service
(
const
char
*
std_out_file
,
const
char
*
std_err_file
)
{
int
daemonize_service
(
const
char
*
std_out_file
,
const
char
*
std_err_file
)
{
int
rc
=
daemonize_service
(
false
);
if
(
rc
!=
0
)
{
...
...
@@ -83,7 +85,8 @@ int daemonize_service(const char *std_out_file, const char *std_err_file) {
return
0
;
}
void
sys_log_redirect
(
const
char
*
std_out_file
,
const
char
*
std_err_file
)
{
void
sys_log_redirect
(
const
char
*
std_out_file
,
const
char
*
std_err_file
)
{
int
rc
=
0
;
// Redirect stdin to /dev/null
...
...
@@ -125,8 +128,7 @@ void sys_log_redirect(const char *std_out_file, const char *std_err_file) {
close
(
errfd
);
}
setvbuf
(
stderr
,
NULL
,
_IONBF
,
0
);
// Make sure stderr is not buffering
std
::
cerr
<<
"Process "
<<
getpid
()
<<
" built error output at "
<<
tv
.
tv_sec
<<
std
::
endl
;
std
::
cerr
<<
"Process "
<<
getpid
()
<<
" built error output at "
<<
tv
.
tv_sec
<<
std
::
endl
;
std
::
string
outFile
=
getAboslutPath
(
std_out_file
);
...
...
@@ -144,10 +146,9 @@ void sys_log_redirect(const char *std_out_file, const char *std_err_file) {
close
(
outfd
);
}
setvbuf
(
stdout
,
NULL
,
_IONBF
,
0
);
// Make sure stdout not buffering
std
::
cout
<<
"Process "
<<
getpid
()
<<
" built standard output at "
<<
tv
.
tv_sec
<<
std
::
endl
;
std
::
cout
<<
"Process "
<<
getpid
()
<<
" built standard output at "
<<
tv
.
tv_sec
<<
std
::
endl
;
return
;
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/os/process.h
浏览文件 @
e976988d
...
...
@@ -16,7 +16,6 @@ See the Mulan PSL v2 for more details. */
#define __COMMON_OS_PROCESS_H__
namespace
common
{
//! Get process Name
/**
* @param[in] prog_full_name process full name with full path
...
...
@@ -43,5 +42,5 @@ int daemonize_service(const char *std_out_file, const char *std_err_file);
void
sys_log_redirect
(
const
char
*
std_out_file
,
const
char
*
std_err_file
);
}
//
namespace common
}
//
namespace common
#endif //__COMMON_OS_PROCESS_H__
deps/common/os/process_param.cpp
浏览文件 @
e976988d
...
...
@@ -17,14 +17,15 @@ See the Mulan PSL v2 for more details. */
namespace
common
{
//! Global process config
ProcessParam
*&
the_process_param
()
ProcessParam
*&
the_process_param
()
{
static
ProcessParam
*
process_cfg
=
new
ProcessParam
();
static
ProcessParam
*
process_cfg
=
new
ProcessParam
();
return
process_cfg
;
}
void
ProcessParam
::
init_default
(
std
::
string
&
process_name
)
{
void
ProcessParam
::
init_default
(
std
::
string
&
process_name
)
{
assert
(
process_name
.
empty
()
==
false
);
this
->
process_name_
=
process_name
;
if
(
std_out_
.
empty
())
{
...
...
@@ -40,6 +41,4 @@ void ProcessParam::init_default(std::string &process_name) {
demon
=
false
;
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/os/process_param.h
浏览文件 @
e976988d
...
...
@@ -21,58 +21,96 @@ namespace common {
class
ProcessParam
{
public:
ProcessParam
()
{}
public:
ProcessParam
()
{}
virtual
~
ProcessParam
()
{}
virtual
~
ProcessParam
()
{}
void
init_default
(
std
::
string
&
process_name
);
const
std
::
string
&
get_std_out
()
const
{
return
std_out_
;
}
const
std
::
string
&
get_std_out
()
const
{
return
std_out_
;
}
void
set_std_out
(
const
std
::
string
&
std_out
)
{
ProcessParam
::
std_out_
=
std_out
;
}
void
set_std_out
(
const
std
::
string
&
std_out
)
{
ProcessParam
::
std_out_
=
std_out
;
}
const
std
::
string
&
get_std_err
()
const
{
return
std_err_
;
}
const
std
::
string
&
get_std_err
()
const
{
return
std_err_
;
}
void
set_std_err
(
const
std
::
string
&
std_err
)
{
ProcessParam
::
std_err_
=
std_err
;
}
void
set_std_err
(
const
std
::
string
&
std_err
)
{
ProcessParam
::
std_err_
=
std_err
;
}
const
std
::
string
&
get_conf
()
const
{
return
conf
;
}
const
std
::
string
&
get_conf
()
const
{
return
conf
;
}
void
set_conf
(
const
std
::
string
&
conf
)
{
ProcessParam
::
conf
=
conf
;
}
void
set_conf
(
const
std
::
string
&
conf
)
{
ProcessParam
::
conf
=
conf
;
}
const
std
::
string
&
get_process_name
()
const
{
return
process_name_
;
}
const
std
::
string
&
get_process_name
()
const
{
return
process_name_
;
}
void
set_process_name
(
const
std
::
string
&
processName
)
{
void
set_process_name
(
const
std
::
string
&
processName
)
{
ProcessParam
::
process_name_
=
processName
;
}
bool
is_demon
()
const
{
return
demon
;
}
bool
is_demon
()
const
{
return
demon
;
}
void
set_demon
(
bool
demon
)
{
ProcessParam
::
demon
=
demon
;
}
void
set_demon
(
bool
demon
)
{
ProcessParam
::
demon
=
demon
;
}
const
std
::
vector
<
std
::
string
>
&
get_args
()
const
{
return
args
;
}
const
std
::
vector
<
std
::
string
>
&
get_args
()
const
{
return
args
;
}
void
set_args
(
const
std
::
vector
<
std
::
string
>
&
args
)
{
void
set_args
(
const
std
::
vector
<
std
::
string
>
&
args
)
{
ProcessParam
::
args
=
args
;
}
void
set_server_port
(
int
port
)
{
void
set_server_port
(
int
port
)
{
server_port_
=
port
;
}
int
get_server_port
()
const
{
int
get_server_port
()
const
{
return
server_port_
;
}
void
set_unix_socket_path
(
const
char
*
unix_socket_path
)
{
void
set_unix_socket_path
(
const
char
*
unix_socket_path
)
{
unix_socket_path_
=
unix_socket_path
;
}
const
std
::
string
&
get_unix_socket_path
()
const
{
const
std
::
string
&
get_unix_socket_path
()
const
{
return
unix_socket_path_
;
}
private:
private:
std
::
string
std_out_
;
// The output file
std
::
string
std_err_
;
// The err output file
std
::
string
conf
;
// The configuration file
...
...
@@ -83,7 +121,7 @@ class ProcessParam {
std
::
string
unix_socket_path_
;
};
ProcessParam
*&
the_process_param
();
ProcessParam
*&
the_process_param
();
}
//
namespace common
}
//
namespace common
#endif //__COMMON_OS_PROCESS_PARAM_H__
deps/common/os/signal.cpp
浏览文件 @
e976988d
...
...
@@ -17,29 +17,31 @@ See the Mulan PSL v2 for more details. */
#include "pthread.h"
namespace
common
{
void
setSignalHandler
(
int
sig
,
sighandler_t
func
)
{
void
setSignalHandler
(
int
sig
,
sighandler_t
func
)
{
struct
sigaction
newsa
,
oldsa
;
sigemptyset
(
&
newsa
.
sa_mask
);
newsa
.
sa_flags
=
0
;
newsa
.
sa_handler
=
func
;
int
rc
=
sigaction
(
sig
,
&
newsa
,
&
oldsa
);
if
(
rc
)
{
std
::
cerr
<<
"Failed to set signal "
<<
sig
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
std
::
cerr
<<
"Failed to set signal "
<<
sig
<<
SYS_OUTPUT_FILE_POS
<<
SYS_OUTPUT_ERROR
<<
std
::
endl
;
}
}
/*
** Set Singal handling Fucntion
*/
void
setSignalHandler
(
sighandler_t
func
)
{
void
setSignalHandler
(
sighandler_t
func
)
{
setSignalHandler
(
SIGQUIT
,
func
);
setSignalHandler
(
SIGINT
,
func
);
setSignalHandler
(
SIGHUP
,
func
);
setSignalHandler
(
SIGTERM
,
func
);
}
void
blockDefaultSignals
(
sigset_t
*
signal_set
,
sigset_t
*
old_set
)
{
void
blockDefaultSignals
(
sigset_t
*
signal_set
,
sigset_t
*
old_set
)
{
sigemptyset
(
signal_set
);
#ifndef DEBUG
// SIGINT will effect our gdb debugging
...
...
@@ -50,7 +52,8 @@ void blockDefaultSignals(sigset_t *signal_set, sigset_t *old_set) {
pthread_sigmask
(
SIG_BLOCK
,
signal_set
,
old_set
);
}
void
unBlockDefaultSignals
(
sigset_t
*
signal_set
,
sigset_t
*
old_set
)
{
void
unBlockDefaultSignals
(
sigset_t
*
signal_set
,
sigset_t
*
old_set
)
{
sigemptyset
(
signal_set
);
#ifndef DEBUG
sigaddset
(
signal_set
,
SIGINT
);
...
...
@@ -60,7 +63,8 @@ void unBlockDefaultSignals(sigset_t *signal_set, sigset_t *old_set) {
pthread_sigmask
(
SIG_UNBLOCK
,
signal_set
,
old_set
);
}
void
*
waitForSignals
(
void
*
args
)
{
void
*
waitForSignals
(
void
*
args
)
{
LOG_INFO
(
"Start thread to wait signals."
);
sigset_t
*
signal_set
=
(
sigset_t
*
)
args
;
int
sig_number
=
-
1
;
...
...
@@ -77,7 +81,8 @@ void *waitForSignals(void *args) {
return
NULL
;
}
void
startWaitForSignals
(
sigset_t
*
signal_set
)
{
void
startWaitForSignals
(
sigset_t
*
signal_set
)
{
pthread_t
pThread
;
pthread_attr_t
pThreadAttrs
;
...
...
@@ -86,6 +91,5 @@ void startWaitForSignals(sigset_t *signal_set) {
pthread_attr_setdetachstate
(
&
pThreadAttrs
,
PTHREAD_CREATE_DETACHED
);
pthread_create
(
&
pThread
,
&
pThreadAttrs
,
waitForSignals
,
(
void
*
)
signal_set
);
}
}
// namespace common
\ No newline at end of file
deps/common/os/signal.h
浏览文件 @
e976988d
...
...
@@ -30,7 +30,6 @@ void blockDefaultSignals(sigset_t *signal_set, sigset_t *old_set);
*/
void
unBlockDefaultSignals
(
sigset_t
*
signal_set
,
sigset_t
*
old_set
);
void
*
waitForSignals
(
sigset_t
*
signal_set
);
void
startWaitForSignals
(
sigset_t
*
signal_set
);
...
...
@@ -42,5 +41,5 @@ typedef void (*sighandler_t)(int);
void
setSignalHandler
(
sighandler_t
func
);
void
setSignalHandler
(
int
sig
,
sighandler_t
func
);
}
//
namespace common
}
//
namespace common
#endif
/* __COMMON_OS_SIGNAL_H__ */
deps/common/seda/callback.cpp
浏览文件 @
e976988d
...
...
@@ -33,11 +33,12 @@ extern bool &get_event_history_flag();
// Constructor
CompletionCallback
::
CompletionCallback
(
Stage
*
trgt
,
CallbackContext
*
ctx
)
:
target_stage_
(
trgt
),
context_
(
ctx
),
next_cb_
(
NULL
),
ev_hist_flag_
(
get_event_history_flag
())
{}
:
target_stage_
(
trgt
),
context_
(
ctx
),
next_cb_
(
NULL
),
ev_hist_flag_
(
get_event_history_flag
())
{}
// Destructor
CompletionCallback
::~
CompletionCallback
()
{
CompletionCallback
::~
CompletionCallback
()
{
if
(
context_
)
{
delete
context_
;
}
...
...
@@ -47,14 +48,16 @@ CompletionCallback::~CompletionCallback() {
}
// Push onto a callback stack
void
CompletionCallback
::
push_callback
(
CompletionCallback
*
next
)
{
void
CompletionCallback
::
push_callback
(
CompletionCallback
*
next
)
{
ASSERT
((
!
next_cb_
),
"%s"
,
"cannot push a callback twice"
);
next_cb_
=
next
;
}
// Pop off of a callback stack
CompletionCallback
*
CompletionCallback
::
pop_callback
()
{
CompletionCallback
*
CompletionCallback
::
pop_callback
()
{
CompletionCallback
*
ret_val
=
next_cb_
;
next_cb_
=
NULL
;
...
...
@@ -62,7 +65,8 @@ CompletionCallback *CompletionCallback::pop_callback() {
}
// One event is complete
void
CompletionCallback
::
event_done
(
StageEvent
*
ev
)
{
void
CompletionCallback
::
event_done
(
StageEvent
*
ev
)
{
if
(
ev_hist_flag_
)
{
ev
->
save_stage
(
target_stage_
,
StageEvent
::
CALLBACK_EV
);
...
...
@@ -71,11 +75,13 @@ void CompletionCallback::event_done(StageEvent *ev) {
}
// Reschedule callback on target stage thread
void
CompletionCallback
::
event_reschedule
(
StageEvent
*
ev
)
{
void
CompletionCallback
::
event_reschedule
(
StageEvent
*
ev
)
{
target_stage_
->
add_event
(
ev
);
}
void
CompletionCallback
::
event_timeout
(
StageEvent
*
ev
)
{
void
CompletionCallback
::
event_timeout
(
StageEvent
*
ev
)
{
LOG_DEBUG
(
"to call event_timeout for stage %s"
,
target_stage_
->
get_name
());
if
(
ev_hist_flag_
)
{
ev
->
save_stage
(
target_stage_
,
StageEvent
::
TIMEOUT_EV
);
...
...
@@ -83,4 +89,4 @@ void CompletionCallback::event_timeout(StageEvent *ev) {
target_stage_
->
timeout_event
(
ev
,
context_
);
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/seda/callback.h
浏览文件 @
e976988d
...
...
@@ -59,7 +59,7 @@ class CompletionCallback {
// public interface operations
public:
public:
// Constructor
CompletionCallback
(
Stage
*
trgt
,
CallbackContext
*
ctx
=
NULL
);
...
...
@@ -84,7 +84,7 @@ class CompletionCallback {
// Complete this event if it has timed out
void
event_timeout
(
StageEvent
*
ev
);
protected:
protected:
// implementation state
Stage
*
target_stage_
;
// stage which is setting this callback
...
...
@@ -101,19 +101,25 @@ class CompletionCallback {
* callback context class from this base.
*/
class
CallbackContext
{
public:
virtual
~
CallbackContext
()
{}
public:
virtual
~
CallbackContext
()
{}
};
class
CallbackContextEvent
:
public
CallbackContext
{
public:
CallbackContextEvent
(
StageEvent
*
event
=
NULL
)
:
ev_
(
event
)
{}
~
CallbackContextEvent
()
{}
StageEvent
*
get_event
()
{
return
ev_
;
}
private:
public:
CallbackContextEvent
(
StageEvent
*
event
=
NULL
)
:
ev_
(
event
)
{}
~
CallbackContextEvent
()
{}
StageEvent
*
get_event
()
{
return
ev_
;
}
private:
StageEvent
*
ev_
;
};
}
//
namespace common
}
//
namespace common
#endif // __COMMON_SEDA_CALLBACK_H__
deps/common/seda/class_factory.h
浏览文件 @
e976988d
...
...
@@ -21,7 +21,6 @@ See the Mulan PSL v2 for more details. */
#include "common/log/log.h"
namespace
common
{
/**
* A class to construct arbitrary subclass instances
*
...
...
@@ -42,10 +41,10 @@ namespace common {
* with static linkage in a global initialization routine.
*/
template
<
class
T
>
template
<
class
T
>
class
ClassFactory
{
public:
public:
typedef
T
*
(
*
FactoryFunc
)(
const
std
::
string
&
);
/**
...
...
@@ -69,7 +68,7 @@ class ClassFactory {
*/
static
T
*
make_instance
(
const
std
::
string
&
tag
);
private:
private:
// Accessor function that gets the head of the factory list
static
ClassFactory
<
T
>
*&
fact_list_head
();
...
...
@@ -88,8 +87,9 @@ class ClassFactory {
* as static. C++ guarantees that the first time the function is
* invoked (from anywhere) the static local will be initialized.
*/
template
<
class
T
>
ClassFactory
<
T
>
*&
ClassFactory
<
T
>::
fact_list_head
()
{
template
<
class
T
>
ClassFactory
<
T
>
*&
ClassFactory
<
T
>::
fact_list_head
()
{
static
ClassFactory
<
T
>
*
fact_list
=
NULL
;
return
fact_list
;
}
...
...
@@ -99,16 +99,17 @@ ClassFactory<T> *&ClassFactory<T>::fact_list_head() {
* Implementation notes:
* constructor places current instance on the global factory list.
*/
template
<
class
T
>
ClassFactory
<
T
>::
ClassFactory
(
const
std
::
string
&
tag
,
FactoryFunc
func
)
:
identifier_
(
tag
),
fact_func_
(
func
)
{
template
<
class
T
>
ClassFactory
<
T
>::
ClassFactory
(
const
std
::
string
&
tag
,
FactoryFunc
func
)
:
identifier_
(
tag
),
fact_func_
(
func
)
{
next_
=
fact_list_head
();
fact_list_head
()
=
this
;
}
// Destructor
template
<
class
T
>
ClassFactory
<
T
>::~
ClassFactory
()
{}
template
<
class
T
>
ClassFactory
<
T
>::~
ClassFactory
()
{}
/**
* Construct an instance of a specified sub-class
...
...
@@ -116,8 +117,9 @@ ClassFactory<T>::~ClassFactory() {}
* scan global list to find matching tag and use the factory func to
* create an instance.
*/
template
<
class
T
>
T
*
ClassFactory
<
T
>::
make_instance
(
const
std
::
string
&
tag
)
{
template
<
class
T
>
T
*
ClassFactory
<
T
>::
make_instance
(
const
std
::
string
&
tag
)
{
T
*
instance
=
NULL
;
ClassFactory
<
T
>
*
current
=
fact_list_head
();
...
...
@@ -136,5 +138,5 @@ T *ClassFactory<T>::make_instance(const std::string &tag) {
return
instance
;
}
}
//
namespace common
}
//
namespace common
#endif // __COMMON_SEDA_CLASS_FACTORY_H__
deps/common/seda/event_dispatcher.cpp
浏览文件 @
e976988d
...
...
@@ -17,8 +17,8 @@ See the Mulan PSL v2 for more details. */
namespace
common
{
// Constructor
EventDispatcher
::
EventDispatcher
(
const
char
*
tag
)
:
Stage
(
tag
),
event_store_
(),
next_stage_
(
NULL
)
{
EventDispatcher
::
EventDispatcher
(
const
char
*
tag
)
:
Stage
(
tag
),
event_store_
(),
next_stage_
(
NULL
)
{
LOG_TRACE
(
"enter
\n
"
);
pthread_mutexattr_t
attr
;
...
...
@@ -31,7 +31,8 @@ EventDispatcher::EventDispatcher(const char *tag)
}
// Destructor
EventDispatcher
::~
EventDispatcher
()
{
EventDispatcher
::~
EventDispatcher
()
{
LOG_TRACE
(
"enter
\n
"
);
pthread_mutex_destroy
(
&
event_lock_
);
LOG_TRACE
(
"exit
\n
"
);
...
...
@@ -42,7 +43,8 @@ EventDispatcher::~EventDispatcher() {
* Check if the event can be dispatched. If not, hash it and store
* it. If so, send it on to the next stage.
*/
void
EventDispatcher
::
handle_event
(
StageEvent
*
event
)
{
void
EventDispatcher
::
handle_event
(
StageEvent
*
event
)
{
LOG_TRACE
(
"enter
\n
"
);
std
::
string
hash
;
...
...
@@ -67,7 +69,8 @@ void EventDispatcher::handle_event(StageEvent *event) {
}
// Initialize stage params and validate outputs
bool
EventDispatcher
::
initialize
()
{
bool
EventDispatcher
::
initialize
()
{
bool
ret_val
=
true
;
if
(
next_stage_list_
.
size
()
!=
1
)
{
...
...
@@ -82,15 +85,15 @@ bool EventDispatcher::initialize() {
* Cleanup stage after disconnection
* Call done() on any events left over in the event_store_.
*/
void
EventDispatcher
::
cleanup
()
{
void
EventDispatcher
::
cleanup
()
{
pthread_mutex_lock
(
&
event_lock_
);
// for each hash chain...
for
(
EventHash
::
iterator
i
=
event_store_
.
begin
();
i
!=
event_store_
.
end
();
i
++
)
{
// for each event on the chain
for
(
std
::
list
<
StoredEvent
>::
iterator
j
=
i
->
second
.
begin
();
j
!=
i
->
second
.
end
();
j
++
)
{
for
(
std
::
list
<
StoredEvent
>::
iterator
j
=
i
->
second
.
begin
();
j
!=
i
->
second
.
end
();
j
++
)
{
j
->
first
->
done
();
}
i
->
second
.
clear
();
...
...
@@ -101,7 +104,8 @@ void EventDispatcher::cleanup() {
}
// Wake up a stored event
bool
EventDispatcher
::
wakeup_event
(
std
::
string
hashkey
)
{
bool
EventDispatcher
::
wakeup_event
(
std
::
string
hashkey
)
{
bool
sent
=
false
;
EventHash
::
iterator
i
;
...
...
@@ -131,4 +135,4 @@ bool EventDispatcher::wakeup_event(std::string hashkey) {
return
sent
;
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/seda/event_dispatcher.h
浏览文件 @
e976988d
...
...
@@ -55,7 +55,7 @@ class EventDispatcher : public Stage {
// public interface operations
public:
public:
typedef
enum
{
SEND_EVENT
=
0
,
STORE_EVENT
,
FAIL_EVENT
}
status_t
;
/**
...
...
@@ -77,7 +77,7 @@ class EventDispatcher : public Stage {
// Note, EventDispatcher is an abstract class and needs no make_stage()
protected:
protected:
/**
* Constructor
* @param[in] tag The label that identifies this stage.
...
...
@@ -96,7 +96,10 @@ class EventDispatcher : public Stage {
bool
initialize
();
// set properties for this object
bool
set_properties
()
{
return
true
;
}
bool
set_properties
()
{
return
true
;
}
/**
* Cleanup stage after disconnection
...
...
@@ -119,8 +122,7 @@ class EventDispatcher : public Stage {
* FAIL_EVENT if failure, and event has been completed;
* ctx is NULL
*/
virtual
status_t
dispatch_event
(
StageEvent
*
ev
,
DispatchContext
*&
ctx
,
std
::
string
&
hash
)
=
0
;
virtual
status_t
dispatch_event
(
StageEvent
*
ev
,
DispatchContext
*&
ctx
,
std
::
string
&
hash
)
=
0
;
/**
* Wake up a stored event
...
...
@@ -140,7 +142,7 @@ class EventDispatcher : public Stage {
pthread_mutex_t
event_lock_
;
// protects access to event_store_
Stage
*
next_stage_
;
// target for dispatched events
protected:
protected:
};
/**
...
...
@@ -148,9 +150,10 @@ class EventDispatcher : public Stage {
* derive from this base class.
*/
class
DispatchContext
{
public:
virtual
~
DispatchContext
()
{}
public:
virtual
~
DispatchContext
()
{}
};
}
//
namespace common
}
//
namespace common
#endif // __COMMON_SEDA_EVENT_DISPATCHER_H__
deps/common/seda/example_stage.cpp
浏览文件 @
e976988d
...
...
@@ -26,13 +26,16 @@ See the Mulan PSL v2 for more details. */
using
namespace
common
;
// Constructor
ExampleStage
::
ExampleStage
(
const
char
*
tag
)
:
Stage
(
tag
)
{}
ExampleStage
::
ExampleStage
(
const
char
*
tag
)
:
Stage
(
tag
)
{}
// Destructor
ExampleStage
::~
ExampleStage
()
{}
ExampleStage
::~
ExampleStage
()
{}
// Parse properties, instantiate a stage object
Stage
*
ExampleStage
::
make_stage
(
const
std
::
string
&
tag
)
{
Stage
*
ExampleStage
::
make_stage
(
const
std
::
string
&
tag
)
{
ExampleStage
*
stage
=
new
ExampleStage
(
tag
.
c_str
());
if
(
stage
==
NULL
)
{
LOG_ERROR
(
"new ExampleStage failed"
);
...
...
@@ -43,7 +46,8 @@ Stage *ExampleStage::make_stage(const std::string &tag) {
}
// Set properties for this object set in stage specific properties
bool
ExampleStage
::
set_properties
()
{
bool
ExampleStage
::
set_properties
()
{
// std::string stageNameStr(stage_name_);
// std::map<std::string, std::string> section = g_properties()->get(
// stageNameStr);
...
...
@@ -56,7 +60,8 @@ bool ExampleStage::set_properties() {
}
// Initialize stage params and validate outputs
bool
ExampleStage
::
initialize
()
{
bool
ExampleStage
::
initialize
()
{
LOG_TRACE
(
"Enter"
);
// std::list<Stage*>::iterator stgp = next_stage_list_.begin();
...
...
@@ -68,20 +73,23 @@ bool ExampleStage::initialize() {
}
// Cleanup after disconnection
void
ExampleStage
::
cleanup
()
{
void
ExampleStage
::
cleanup
()
{
LOG_TRACE
(
"Enter"
);
LOG_TRACE
(
"Exit"
);
}
void
ExampleStage
::
handle_event
(
StageEvent
*
event
)
{
void
ExampleStage
::
handle_event
(
StageEvent
*
event
)
{
LOG_TRACE
(
"Enter
\n
"
);
LOG_TRACE
(
"Exit
\n
"
);
return
;
}
void
ExampleStage
::
callback_event
(
StageEvent
*
event
,
CallbackContext
*
context
)
{
void
ExampleStage
::
callback_event
(
StageEvent
*
event
,
CallbackContext
*
context
)
{
LOG_TRACE
(
"Enter
\n
"
);
LOG_TRACE
(
"Exit
\n
"
);
...
...
deps/common/seda/example_stage.h
浏览文件 @
e976988d
...
...
@@ -33,7 +33,6 @@ protected:
void
cleanup
();
void
handle_event
(
StageEvent
*
event
);
void
callback_event
(
StageEvent
*
event
,
CallbackContext
*
context
);
};
}
// namespace common
#endif //__COMMON_SEDA_EXAMPLE_STAGE_H__
deps/common/seda/init.cpp
浏览文件 @
e976988d
...
...
@@ -37,17 +37,15 @@ See the Mulan PSL v2 for more details. */
#include "common/seda/timer_stage.h"
namespace
common
{
int
init_seda
(
ProcessParam
*
process_cfg
)
{
int
init_seda
(
ProcessParam
*
process_cfg
)
{
// Initialize the static data structures of threadpool
Threadpool
::
create_pool_key
();
// initialize class factory instances here
static
StageFactory
kill_thread_factory
(
"KillThreads"
,
&
KillThreadStage
::
make_stage
);
static
StageFactory
kill_thread_factory
(
"KillThreads"
,
&
KillThreadStage
::
make_stage
);
static
StageFactory
timer_factory
(
"TimerStage"
,
&
TimerStage
::
make_stage
);
static
StageFactory
seda_stats_factory
(
"MetricsStage"
,
&
MetricsStage
::
make_stage
);
static
StageFactory
seda_stats_factory
(
"MetricsStage"
,
&
MetricsStage
::
make_stage
);
// try to parse the seda configuration files
SedaConfig
*
config
=
SedaConfig
::
get_instance
();
...
...
@@ -55,16 +53,14 @@ int init_seda(ProcessParam *process_cfg) {
config_stat
=
config
->
parse
();
if
(
config_stat
!=
SedaConfig
::
SUCCESS
)
{
LOG_ERROR
(
"Error: unable to parse file %s"
,
process_cfg
->
get_process_name
().
c_str
());
LOG_ERROR
(
"Error: unable to parse file %s"
,
process_cfg
->
get_process_name
().
c_str
());
return
errno
;
}
// Log a message to indicate that we are restarting, when looking
// at a log we can see if mmon is restarting us because we keep
// crashing.
LOG_INFO
(
"(Re)Starting State: Pid: %u Time: %s"
,
(
unsigned
int
)
getpid
(),
DateTime
::
now
().
to_string_local
().
c_str
());
LOG_INFO
(
"(Re)Starting State: Pid: %u Time: %s"
,
(
unsigned
int
)
getpid
(),
DateTime
::
now
().
to_string_local
().
c_str
());
LOG_INFO
(
"The process Name is %s"
,
process_cfg
->
get_process_name
().
c_str
());
// try to initialize the seda configuration
...
...
@@ -79,10 +75,11 @@ int init_seda(ProcessParam *process_cfg) {
return
0
;
}
void
cleanup_seda
()
{
void
cleanup_seda
()
{
SedaConfig
*
seda_config
=
SedaConfig
::
get_instance
();
delete
seda_config
;
SedaConfig
::
get_instance
()
=
NULL
;
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/seda/init.h
浏览文件 @
e976988d
...
...
@@ -36,5 +36,5 @@ int init_seda(ProcessParam *process_cfg);
void
cleanup_seda
();
}
//
namespace common
}
//
namespace common
#endif // __COMMON_SEDA_INIT_H__
deps/common/seda/kill_thread.cpp
浏览文件 @
e976988d
...
...
@@ -19,14 +19,14 @@ See the Mulan PSL v2 for more details. */
#include "common/seda/thread_pool.h"
namespace
common
{
/**
* Notify the pool and kill the thread
* @param[in] event Pointer to event that must be handled.
*
* @post Call never returns. Thread is killed. Pool is notified.
*/
void
KillThreadStage
::
handle_event
(
StageEvent
*
event
)
{
void
KillThreadStage
::
handle_event
(
StageEvent
*
event
)
{
get_pool
()
->
thread_kill
();
event
->
done
();
this
->
release_event
();
...
...
@@ -39,13 +39,15 @@ void KillThreadStage::handle_event(StageEvent *event) {
* @post initializing the class members
* @return the class object
*/
Stage
*
KillThreadStage
::
make_stage
(
const
std
::
string
&
tag
)
{
Stage
*
KillThreadStage
::
make_stage
(
const
std
::
string
&
tag
)
{
return
new
KillThreadStage
(
tag
.
c_str
());
}
bool
KillThreadStage
::
set_properties
()
{
bool
KillThreadStage
::
set_properties
()
{
// nothing to do
return
true
;
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/seda/kill_thread.h
浏览文件 @
e976988d
...
...
@@ -21,7 +21,6 @@ See the Mulan PSL v2 for more details. */
#include "common/seda/stage.h"
namespace
common
{
/**
* @file
* @author Longda
...
...
@@ -56,7 +55,8 @@ protected:
* @post event queue is empty
* @post stage is not connected
*/
KillThreadStage
(
const
char
*
tag
)
:
Stage
(
tag
)
{}
KillThreadStage
(
const
char
*
tag
)
:
Stage
(
tag
)
{}
/**
* Notify the pool and kill the thread
...
...
@@ -70,7 +70,10 @@ protected:
* Handle the callback
* Nothing special for callbacks in this stage.
*/
void
callback_event
(
StageEvent
*
event
,
CallbackContext
*
context
)
{
return
;
}
void
callback_event
(
StageEvent
*
event
,
CallbackContext
*
context
)
{
return
;
}
/**
* Initialize stage params
...
...
@@ -79,7 +82,10 @@ protected:
* @pre Stage not connected
* @return true
*/
bool
initialize
()
{
return
true
;
}
bool
initialize
()
{
return
true
;
}
/**
* set properties for this object
...
...
@@ -92,5 +98,5 @@ protected:
friend
class
Threadpool
;
};
}
//
namespace common
}
//
namespace common
#endif // __COMMON_SEDA_KILL_THREAD_H__
deps/common/seda/metrics_report_event.h
浏览文件 @
e976988d
...
...
@@ -28,5 +28,5 @@ public:
};
};
}
//
namespace common
}
//
namespace common
#endif //__COMMON_SEDA_METRICS_REPORT_EVENT_H__
deps/common/seda/metrics_stage.cpp
浏览文件 @
e976988d
...
...
@@ -29,20 +29,24 @@ See the Mulan PSL v2 for more details. */
using
namespace
common
;
MetricsRegistry
&
get_metric_registry
()
{
MetricsRegistry
&
get_metric_registry
()
{
static
MetricsRegistry
metrics_registry
;
return
metrics_registry
;
}
// Constructor
MetricsStage
::
MetricsStage
(
const
char
*
tag
)
:
Stage
(
tag
)
{}
MetricsStage
::
MetricsStage
(
const
char
*
tag
)
:
Stage
(
tag
)
{}
// Destructor
MetricsStage
::~
MetricsStage
()
{}
MetricsStage
::~
MetricsStage
()
{}
// Parse properties, instantiate a stage object
Stage
*
MetricsStage
::
make_stage
(
const
std
::
string
&
tag
)
{
Stage
*
MetricsStage
::
make_stage
(
const
std
::
string
&
tag
)
{
MetricsStage
*
stage
=
new
MetricsStage
(
tag
.
c_str
());
if
(
stage
==
NULL
)
{
LOG_ERROR
(
"new MetricsStage failed"
);
...
...
@@ -53,10 +57,10 @@ Stage *MetricsStage::make_stage(const std::string &tag) {
}
// Set properties for this object set in stage specific properties
bool
MetricsStage
::
set_properties
()
{
bool
MetricsStage
::
set_properties
()
{
std
::
string
stage_name_str
(
stage_name_
);
std
::
map
<
std
::
string
,
std
::
string
>
section
=
get_properties
()
->
get
(
stage_name_str
);
std
::
map
<
std
::
string
,
std
::
string
>
section
=
get_properties
()
->
get
(
stage_name_str
);
metric_report_interval_
=
DateTime
::
SECONDS_PER_MIN
;
...
...
@@ -70,7 +74,8 @@ bool MetricsStage::set_properties() {
}
// Initialize stage params and validate outputs
bool
MetricsStage
::
initialize
()
{
bool
MetricsStage
::
initialize
()
{
LOG_TRACE
(
"Enter"
);
std
::
list
<
Stage
*>::
iterator
stgp
=
next_stage_list_
.
begin
();
...
...
@@ -84,13 +89,15 @@ bool MetricsStage::initialize() {
}
// Cleanup after disconnection
void
MetricsStage
::
cleanup
()
{
void
MetricsStage
::
cleanup
()
{
LOG_TRACE
(
"Enter"
);
LOG_TRACE
(
"Exit"
);
}
void
MetricsStage
::
handle_event
(
StageEvent
*
event
)
{
void
MetricsStage
::
handle_event
(
StageEvent
*
event
)
{
LOG_TRACE
(
"Enter
\n
"
);
CompletionCallback
*
cb
=
new
CompletionCallback
(
this
,
NULL
);
...
...
@@ -102,8 +109,7 @@ void MetricsStage::handle_event(StageEvent *event) {
return
;
}
TimerRegisterEvent
*
tm_event
=
new
TimerRegisterEvent
(
event
,
metric_report_interval_
*
USEC_PER_SEC
);
TimerRegisterEvent
*
tm_event
=
new
TimerRegisterEvent
(
event
,
metric_report_interval_
*
USEC_PER_SEC
);
if
(
tm_event
==
NULL
)
{
LOG_ERROR
(
"Failed to new TimerRegisterEvent"
);
...
...
@@ -121,7 +127,8 @@ void MetricsStage::handle_event(StageEvent *event) {
return
;
}
void
MetricsStage
::
callback_event
(
StageEvent
*
event
,
CallbackContext
*
context
)
{
void
MetricsStage
::
callback_event
(
StageEvent
*
event
,
CallbackContext
*
context
)
{
LOG_TRACE
(
"Enter
\n
"
);
MetricsRegistry
&
metrics_registry
=
get_metrics_registry
();
...
...
deps/common/seda/metrics_stage.h
浏览文件 @
e976988d
...
...
@@ -37,7 +37,7 @@ protected:
protected:
private:
Stage
*
timer_stage_
=
nullptr
;
//report metrics every @metric_report_interval_ seconds
//
report metrics every @metric_report_interval_ seconds
int
metric_report_interval_
=
10
;
};
}
// namespace common
...
...
deps/common/seda/seda_config.cpp
浏览文件 @
e976988d
...
...
@@ -28,9 +28,8 @@ namespace common {
SedaConfig
*
SedaConfig
::
instance_
=
NULL
;
SedaConfig
*&
SedaConfig
::
get_instance
()
{
SedaConfig
*&
SedaConfig
::
get_instance
()
{
if
(
instance_
==
NULL
)
{
instance_
=
new
SedaConfig
();
ASSERT
((
instance_
!=
NULL
),
"failed to allocate SedaConfig"
);
...
...
@@ -39,12 +38,14 @@ SedaConfig *&SedaConfig::get_instance() {
}
// Constructor
SedaConfig
::
SedaConfig
()
:
cfg_file_
(),
cfg_str_
(),
thread_pools_
(),
stages_
()
{
SedaConfig
::
SedaConfig
()
:
cfg_file_
(),
cfg_str_
(),
thread_pools_
(),
stages_
()
{
return
;
}
// Destructor
SedaConfig
::~
SedaConfig
()
{
SedaConfig
::~
SedaConfig
()
{
ASSERT
(
instance_
,
"Instance should not be null"
);
// check to see if clean-up is necessary
if
((
!
thread_pools_
.
empty
())
||
(
!
stages_
.
empty
()))
{
...
...
@@ -55,7 +56,8 @@ SedaConfig::~SedaConfig() {
}
// Set the file holding the configuration
void
SedaConfig
::
set_cfg_filename
(
const
char
*
filename
)
{
void
SedaConfig
::
set_cfg_filename
(
const
char
*
filename
)
{
cfg_str_
.
clear
();
cfg_file_
.
clear
();
if
(
filename
!=
NULL
)
{
...
...
@@ -65,7 +67,8 @@ void SedaConfig::set_cfg_filename(const char *filename) {
}
// Set the string holding the configuration
void
SedaConfig
::
set_cfg_string
(
const
char
*
config_str
)
{
void
SedaConfig
::
set_cfg_string
(
const
char
*
config_str
)
{
cfg_str_
.
clear
();
cfg_file_
.
clear
();
if
(
config_str
!=
NULL
)
{
...
...
@@ -75,7 +78,8 @@ void SedaConfig::set_cfg_string(const char *config_str) {
}
// Parse config file or string
SedaConfig
::
status_t
SedaConfig
::
parse
()
{
SedaConfig
::
status_t
SedaConfig
::
parse
()
{
// first parse the config
try
{
// skip parse in this implementation
...
...
@@ -91,7 +95,8 @@ SedaConfig::status_t SedaConfig::parse() {
}
// instantiate the parsed SEDA configuration
SedaConfig
::
status_t
SedaConfig
::
instantiate_cfg
()
{
SedaConfig
::
status_t
SedaConfig
::
instantiate_cfg
()
{
status_t
stat
=
SUCCESS
;
// instantiate the configuration
...
...
@@ -101,7 +106,8 @@ SedaConfig::status_t SedaConfig::instantiate_cfg() {
}
// start the configuration - puts the stages_ into action
SedaConfig
::
status_t
SedaConfig
::
start
()
{
SedaConfig
::
status_t
SedaConfig
::
start
()
{
status_t
stat
=
SUCCESS
;
ASSERT
(
thread_pools_
.
size
(),
"Configuration not yet instantiated"
);
...
...
@@ -127,7 +133,8 @@ SedaConfig::status_t SedaConfig::start() {
}
// Initialize the thread_pools_ and stages_
SedaConfig
::
status_t
SedaConfig
::
init
()
{
SedaConfig
::
status_t
SedaConfig
::
init
()
{
status_t
stat
=
SUCCESS
;
// check the preconditions
...
...
@@ -150,7 +157,8 @@ SedaConfig::status_t SedaConfig::init() {
}
// Clean-up the threadpool and stages_
void
SedaConfig
::
cleanup
()
{
void
SedaConfig
::
cleanup
()
{
// first disconnect all stages_
if
(
stages_
.
empty
()
==
false
)
{
std
::
map
<
std
::
string
,
Stage
*>::
iterator
iter
=
stages_
.
begin
();
...
...
@@ -171,9 +179,9 @@ void SedaConfig::cleanup() {
clear_config
();
}
void
SedaConfig
::
init_event_history
()
{
std
::
map
<
std
::
string
,
std
::
string
>
base_section
=
get_properties
()
->
get
(
SEDA_BASE_NAME
);
void
SedaConfig
::
init_event_history
()
{
std
::
map
<
std
::
string
,
std
::
string
>
base_section
=
get_properties
()
->
get
(
SEDA_BASE_NAME
);
std
::
map
<
std
::
string
,
std
::
string
>::
iterator
it
;
std
::
string
key
;
...
...
@@ -198,16 +206,15 @@ void SedaConfig::init_event_history() {
}
get_max_event_hops
()
=
max_event_hops
;
LOG_INFO
(
"Successfully init_event_history, EventHistory:%d, MaxEventHops:%u"
,
(
int
)
ev_hist
,
max_event_hops
);
LOG_INFO
(
"Successfully init_event_history, EventHistory:%d, MaxEventHops:%u"
,
(
int
)
ev_hist
,
max_event_hops
);
return
;
}
SedaConfig
::
status_t
SedaConfig
::
init_thread_pool
()
{
SedaConfig
::
status_t
SedaConfig
::
init_thread_pool
()
{
try
{
std
::
map
<
std
::
string
,
std
::
string
>
base_section
=
get_properties
()
->
get
(
SEDA_BASE_NAME
);
std
::
map
<
std
::
string
,
std
::
string
>
base_section
=
get_properties
()
->
get
(
SEDA_BASE_NAME
);
std
::
map
<
std
::
string
,
std
::
string
>::
iterator
it
;
std
::
string
key
;
...
...
@@ -239,8 +246,7 @@ SedaConfig::status_t SedaConfig::init_thread_pool() {
int
thread_count
=
1
;
str_to_val
(
count_str
,
thread_count
);
if
(
thread_count
<
1
)
{
LOG_INFO
(
"Thread number of %s is %d, it is same as cpu's cores."
,
thread_name
.
c_str
(),
cpu_num
);
LOG_INFO
(
"Thread number of %s is %d, it is same as cpu's cores."
,
thread_name
.
c_str
(),
cpu_num
);
thread_count
=
cpu_num
;
}
const
int
max_thread_count
=
1000000
;
...
...
@@ -249,7 +255,7 @@ SedaConfig::status_t SedaConfig::init_thread_pool() {
return
INITFAIL
;
}
Threadpool
*
thread_pool
=
new
Threadpool
(
thread_count
,
thread_name
);
Threadpool
*
thread_pool
=
new
Threadpool
(
thread_count
,
thread_name
);
if
(
thread_pool
==
NULL
)
{
LOG_ERROR
(
"Failed to new %s threadpool
\n
"
,
thread_name
.
c_str
());
return
INITFAIL
;
...
...
@@ -258,8 +264,7 @@ SedaConfig::status_t SedaConfig::init_thread_pool() {
}
if
(
thread_pools_
.
find
(
DEFAULT_THREAD_POOL
)
==
thread_pools_
.
end
())
{
LOG_ERROR
(
"There is no default thread pool %s, please add it."
,
DEFAULT_THREAD_POOL
);
LOG_ERROR
(
"There is no default thread pool %s, please add it."
,
DEFAULT_THREAD_POOL
);
return
INITFAIL
;
}
...
...
@@ -279,39 +284,42 @@ SedaConfig::status_t SedaConfig::init_thread_pool() {
return
SUCCESS
;
}
std
::
string
SedaConfig
::
get_thread_pool
(
std
::
string
&
stage_name
)
{
std
::
string
SedaConfig
::
get_thread_pool
(
std
::
string
&
stage_name
)
{
std
::
string
ret
=
DEFAULT_THREAD_POOL
;
// Get thread pool
std
::
map
<
std
::
string
,
std
::
string
>
stage_section
=
get_properties
()
->
get
(
stage_name
);
std
::
map
<
std
::
string
,
std
::
string
>
stage_section
=
get_properties
()
->
get
(
stage_name
);
std
::
map
<
std
::
string
,
std
::
string
>::
iterator
itt
;
std
::
string
thread_pool_id
=
THREAD_POOL_ID
;
itt
=
stage_section
.
find
(
thread_pool_id
);
if
(
itt
==
stage_section
.
end
())
{
LOG_INFO
(
"Not set thread_pool_id for %s, use default threadpool %s"
,
stage_name
.
c_str
(),
DEFAULT_THREAD_POOL
);
LOG_INFO
(
"Not set thread_pool_id for %s, use default threadpool %s"
,
stage_name
.
c_str
(),
DEFAULT_THREAD_POOL
);
return
ret
;
}
std
::
string
thread_name
=
itt
->
second
;
if
(
thread_name
.
empty
())
{
LOG_ERROR
(
"Failed to set %s of the %s, use the default threadpool %s"
,
thread_pool_id
.
c_str
(),
stage_name
.
c_str
(),
DEFAULT_THREAD_POOL
);
thread_pool_id
.
c_str
(),
stage_name
.
c_str
(),
DEFAULT_THREAD_POOL
);
return
ret
;
}
if
(
thread_pools_
.
find
(
thread_name
)
==
thread_pools_
.
end
())
{
LOG_ERROR
(
"The stage %s's threadpool %s is invalid, use the default "
"threadpool %s"
,
stage_name
.
c_str
(),
thread_name
.
c_str
(),
DEFAULT_THREAD_POOL
);
stage_name
.
c_str
(),
thread_name
.
c_str
(),
DEFAULT_THREAD_POOL
);
}
return
ret
;
}
SedaConfig
::
status_t
SedaConfig
::
init_stages
()
{
SedaConfig
::
status_t
SedaConfig
::
init_stages
()
{
try
{
std
::
map
<
std
::
string
,
std
::
string
>
base_section
=
get_properties
()
->
get
(
SEDA_BASE_NAME
);
std
::
map
<
std
::
string
,
std
::
string
>
base_section
=
get_properties
()
->
get
(
SEDA_BASE_NAME
);
std
::
map
<
std
::
string
,
std
::
string
>::
iterator
it
;
std
::
string
key
;
...
...
@@ -328,8 +336,7 @@ SedaConfig::status_t SedaConfig::init_stages() {
split_tag
.
assign
(
1
,
Ini
::
CFG_DELIMIT_TAG
);
split_string
(
it
->
second
,
split_tag
,
stage_names_
);
for
(
std
::
vector
<
std
::
string
>::
iterator
it
=
stage_names_
.
begin
();
it
!=
stage_names_
.
end
();
it
++
)
{
for
(
std
::
vector
<
std
::
string
>::
iterator
it
=
stage_names_
.
begin
();
it
!=
stage_names_
.
end
();
it
++
)
{
std
::
string
stage_name
(
*
it
);
std
::
string
thread_name
=
get_thread_pool
(
stage_name
);
...
...
@@ -344,13 +351,11 @@ SedaConfig::status_t SedaConfig::init_stages() {
stages_
[
stage_name
]
=
stage
;
stage
->
set_pool
(
t
);
LOG_INFO
(
"Stage %s use threadpool %s."
,
stage_name
.
c_str
(),
thread_name
.
c_str
());
LOG_INFO
(
"Stage %s use threadpool %s."
,
stage_name
.
c_str
(),
thread_name
.
c_str
());
}
// end for stage
}
catch
(
std
::
exception
&
e
)
{
LOG_ERROR
(
"Failed to parse stages information, please check, err:%s"
,
e
.
what
());
LOG_ERROR
(
"Failed to parse stages information, please check, err:%s"
,
e
.
what
());
clear_config
();
return
INITFAIL
;
}
...
...
@@ -364,16 +369,15 @@ SedaConfig::status_t SedaConfig::init_stages() {
return
SUCCESS
;
}
SedaConfig
::
status_t
SedaConfig
::
gen_next_stages
()
{
SedaConfig
::
status_t
SedaConfig
::
gen_next_stages
()
{
try
{
for
(
std
::
vector
<
std
::
string
>::
iterator
it_name
=
stage_names_
.
begin
();
it_name
!=
stage_names_
.
end
();
it_name
++
)
{
for
(
std
::
vector
<
std
::
string
>::
iterator
it_name
=
stage_names_
.
begin
();
it_name
!=
stage_names_
.
end
();
it_name
++
)
{
std
::
string
stage_name
(
*
it_name
);
Stage
*
stage
=
stages_
[
stage_name
];
std
::
map
<
std
::
string
,
std
::
string
>
stage_section
=
get_properties
()
->
get
(
stage_name
);
std
::
map
<
std
::
string
,
std
::
string
>
stage_section
=
get_properties
()
->
get
(
stage_name
);
std
::
map
<
std
::
string
,
std
::
string
>::
iterator
it
;
std
::
string
next_stage_id
=
NEXT_STAGES
;
it
=
stage_section
.
find
(
next_stage_id
);
...
...
@@ -388,9 +392,9 @@ SedaConfig::status_t SedaConfig::gen_next_stages() {
split_tag
.
assign
(
1
,
Ini
::
CFG_DELIMIT_TAG
);
split_string
(
next_stage_names
,
split_tag
,
next_stage_name_list
);
for
(
std
::
vector
<
std
::
string
>::
iterator
next_it
=
next_stage_name_list
.
begin
();
next_it
!=
next_stage_name_list
.
end
();
next_it
++
)
{
for
(
std
::
vector
<
std
::
string
>::
iterator
next_it
=
next_stage_name_list
.
begin
();
next_it
!=
next_stage_name_list
.
end
();
next_it
++
)
{
std
::
string
&
next_stage_name
=
*
next_it
;
Stage
*
next_stage
=
stages_
[
next_stage_name
];
stage
->
push_stage
(
next_stage
);
...
...
@@ -406,7 +410,8 @@ SedaConfig::status_t SedaConfig::gen_next_stages() {
}
// instantiate the thread_pools_ and stages_
SedaConfig
::
status_t
SedaConfig
::
instantiate
()
{
SedaConfig
::
status_t
SedaConfig
::
instantiate
()
{
init_event_history
();
...
...
@@ -432,7 +437,8 @@ SedaConfig::status_t SedaConfig::instantiate() {
}
// delete all thread_pools_ and stages_
void
SedaConfig
::
clear_config
()
{
void
SedaConfig
::
clear_config
()
{
// delete stages_
std
::
map
<
std
::
string
,
Stage
*>::
iterator
s_iter
=
stages_
.
begin
();
std
::
map
<
std
::
string
,
Stage
*>::
iterator
s_end
=
stages_
.
end
();
...
...
@@ -441,8 +447,7 @@ void SedaConfig::clear_config() {
Stage
*
stg
=
s_iter
->
second
;
LOG_INFO
(
"Stage %s deleted."
,
stg
->
get_name
());
ASSERT
((
!
stg
->
is_connected
()),
"%s%s"
,
"Stage connected in clear_config "
,
stg
->
get_name
());
ASSERT
((
!
stg
->
is_connected
()),
"%s%s"
,
"Stage connected in clear_config "
,
stg
->
get_name
());
delete
stg
;
s_iter
->
second
=
NULL
;
}
...
...
@@ -466,23 +471,25 @@ void SedaConfig::clear_config() {
LOG_INFO
(
"Seda thread pools released"
);
}
void
SedaConfig
::
get_stage_names
(
std
::
vector
<
std
::
string
>
&
names
)
const
{
void
SedaConfig
::
get_stage_names
(
std
::
vector
<
std
::
string
>
&
names
)
const
{
names
=
stage_names_
;
}
void
SedaConfig
::
get_stage_queue_status
(
std
::
vector
<
int
>
&
stats
)
const
{
for
(
std
::
map
<
std
::
string
,
Stage
*>::
const_iterator
i
=
stages_
.
begin
();
i
!=
stages_
.
end
();
++
i
)
{
void
SedaConfig
::
get_stage_queue_status
(
std
::
vector
<
int
>
&
stats
)
const
{
for
(
std
::
map
<
std
::
string
,
Stage
*>::
const_iterator
i
=
stages_
.
begin
();
i
!=
stages_
.
end
();
++
i
)
{
Stage
*
stg
=
(
*
i
).
second
;
stats
.
push_back
(
stg
->
qlen
());
}
}
// Global seda config object
SedaConfig
*&
get_seda_config
()
{
SedaConfig
*&
get_seda_config
()
{
static
SedaConfig
*
seda_config
=
NULL
;
return
seda_config
;
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/seda/seda_config.h
浏览文件 @
e976988d
...
...
@@ -26,8 +26,7 @@ See the Mulan PSL v2 for more details. */
namespace
common
{
//keywords of sedaconfig
// keywords of sedaconfig
/**
* A class to configure seda stages
...
...
@@ -45,7 +44,7 @@ namespace common {
class
SedaConfig
{
public:
public:
typedef
enum
{
SUCCESS
=
0
,
INITFAIL
,
PARSEFAIL
}
status_t
;
static
SedaConfig
*&
get_instance
();
...
...
@@ -157,7 +156,7 @@ class SedaConfig {
std
::
map
<
std
::
string
,
Stage
*>::
iterator
begin
();
std
::
map
<
std
::
string
,
Stage
*>::
iterator
end
();
private:
private:
// Constructor
SedaConfig
();
...
...
@@ -203,18 +202,20 @@ class SedaConfig {
std
::
map
<
std
::
string
,
Threadpool
*>
thread_pools_
;
std
::
map
<
std
::
string
,
Stage
*>
stages_
;
std
::
vector
<
std
::
string
>
stage_names_
;
};
inline
std
::
map
<
std
::
string
,
Stage
*>::
iterator
SedaConfig
::
begin
()
{
inline
std
::
map
<
std
::
string
,
Stage
*>::
iterator
SedaConfig
::
begin
()
{
return
stages_
.
begin
();
}
inline
std
::
map
<
std
::
string
,
Stage
*>::
iterator
SedaConfig
::
end
()
{
inline
std
::
map
<
std
::
string
,
Stage
*>::
iterator
SedaConfig
::
end
()
{
return
stages_
.
end
();
}
inline
Stage
*
SedaConfig
::
get_stage
(
const
char
*
stagename
)
{
inline
Stage
*
SedaConfig
::
get_stage
(
const
char
*
stagename
)
{
if
(
stagename
)
{
std
::
string
sname
(
stagename
);
return
stages_
[
stagename
];
...
...
@@ -228,5 +229,5 @@ SedaConfig *&get_seda_config();
bool
&
get_event_history_flag
();
u32_t
&
get_max_event_hops
();
}
//
namespace common
}
//
namespace common
#endif //__COMMON_SEDA_SEDA_CONFIG_H__
deps/common/seda/seda_defs.h
浏览文件 @
e976988d
deps/common/seda/stage.cpp
浏览文件 @
e976988d
...
...
@@ -26,7 +26,6 @@ See the Mulan PSL v2 for more details. */
#include "common/seda/thread_pool.h"
namespace
common
{
/**
* Constructor
* @param[in] tag The label that identifies this stage.
...
...
@@ -35,8 +34,8 @@ namespace common {
* @post event queue is empty
* @post stage is not connected
*/
Stage
::
Stage
(
const
char
*
tag
)
:
next_stage_list_
(),
event_list_
(),
connected_
(
false
),
event_ref_
(
0
)
{
Stage
::
Stage
(
const
char
*
tag
)
:
next_stage_list_
(),
event_list_
(),
connected_
(
false
),
event_ref_
(
0
)
{
LOG_TRACE
(
"%s"
,
"enter"
);
assert
(
tag
!=
NULL
);
...
...
@@ -52,7 +51,8 @@ Stage::Stage(const char *tag)
* @pre stage is not connected
* @post pending events are deleted and stage is destroyed
*/
Stage
::~
Stage
()
{
Stage
::~
Stage
()
{
LOG_TRACE
(
"%s"
,
"enter"
);
assert
(
!
connected_
);
MUTEX_LOCK
(
&
list_mutex_
);
...
...
@@ -85,7 +85,8 @@ Stage::~Stage() {
* @post th_pool_ == pool
* @return true if the connection succeeded, else false
*/
bool
Stage
::
connect
()
{
bool
Stage
::
connect
()
{
LOG_TRACE
(
"%s%s"
,
"enter"
,
stage_name_
);
assert
(
!
connected_
);
assert
(
th_pool_
!=
NULL
);
...
...
@@ -126,7 +127,8 @@ bool Stage::connect() {
* @post th_pool_ NULL
* @post stage is not connected
*/
void
Stage
::
disconnect
()
{
void
Stage
::
disconnect
()
{
assert
(
connected_
==
true
);
LOG_TRACE
(
"%s%s"
,
"enter"
,
stage_name_
);
...
...
@@ -151,7 +153,8 @@ void Stage::disconnect() {
* @post event added to the end of event queue
* @post event must not be de-referenced by caller after return
*/
void
Stage
::
add_event
(
StageEvent
*
event
)
{
void
Stage
::
add_event
(
StageEvent
*
event
)
{
assert
(
event
!=
NULL
);
MUTEX_LOCK
(
&
list_mutex_
);
...
...
@@ -174,7 +177,8 @@ void Stage::add_event(StageEvent *event) {
* Query length of queue
* @return length of event queue.
*/
unsigned
long
Stage
::
qlen
()
const
{
unsigned
long
Stage
::
qlen
()
const
{
unsigned
long
res
;
MUTEX_LOCK
(
&
list_mutex_
);
...
...
@@ -187,7 +191,8 @@ unsigned long Stage::qlen() const {
* Query whether the queue is empty
* @return \c true if the queue is empty; \c false otherwise
*/
bool
Stage
::
qempty
()
const
{
bool
Stage
::
qempty
()
const
{
bool
empty
=
false
;
MUTEX_LOCK
(
&
list_mutex_
);
...
...
@@ -203,7 +208,8 @@ bool Stage::qempty() const {
* @return first event on queue.
* @post first event on queue is removed from queue.
*/
StageEvent
*
Stage
::
remove_event
()
{
StageEvent
*
Stage
::
remove_event
()
{
MUTEX_LOCK
(
&
list_mutex_
);
assert
(
!
event_list_
.
empty
());
...
...
@@ -220,7 +226,8 @@ StageEvent *Stage::remove_event() {
*
* @post event ref count on stage is decremented
*/
void
Stage
::
release_event
()
{
void
Stage
::
release_event
()
{
MUTEX_LOCK
(
&
list_mutex_
);
event_ref_
--
;
if
(
!
connected_
&&
event_ref_
==
0
)
{
...
...
@@ -229,4 +236,4 @@ void Stage::release_event() {
MUTEX_UNLOCK
(
&
list_mutex_
);
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/seda/stage.h
浏览文件 @
e976988d
...
...
@@ -89,7 +89,7 @@ class Stage {
// public interface operations
public:
public:
/**
* Destructor
* @pre stage is not connected
...
...
@@ -109,7 +109,10 @@ class Stage {
* Return the Threadpool object
* @return reference to the Threadpool for this Stage
*/
Threadpool
*
get_pool
()
{
return
th_pool_
;
}
Threadpool
*
get_pool
()
{
return
th_pool_
;
}
/**
* Push stage to the list of the next stages
...
...
@@ -195,7 +198,10 @@ class Stage {
* Query whether stage is connected
* @return true if stage is connected
*/
bool
is_connected
()
const
{
return
connected_
;
}
bool
is_connected
()
const
{
return
connected_
;
}
/**
* Perform Stage-specific processing for an event
...
...
@@ -223,12 +229,13 @@ class Stage {
* A stage only need to implement this interface if the down-stream
* stages support event timeout detection.
*/
virtual
void
timeout_event
(
StageEvent
*
event
,
CallbackContext
*
context
)
{
virtual
void
timeout_event
(
StageEvent
*
event
,
CallbackContext
*
context
)
{
LOG_INFO
(
"get a timed out evnet in %s timeout_event
\n
"
,
stage_name_
);
this
->
callback_event
(
event
,
context
);
}
protected:
protected:
/**
* Constructor
* @param[in] tag The label that identifies this stage.
...
...
@@ -264,7 +271,10 @@ class Stage {
* @pre Stage not connected
* @return TRUE if and only if outputs are valid and init succeeded.
*/
virtual
bool
initialize
()
{
return
true
;
}
virtual
bool
initialize
()
{
return
true
;
}
/**
* set properties for this object
...
...
@@ -272,7 +282,10 @@ class Stage {
* @post initializing the class members
* @return Stage instantiated object
*/
virtual
bool
set_properties
()
{
return
true
;
}
virtual
bool
set_properties
()
{
return
true
;
}
/**
* Prepare to disconnect the stage.
...
...
@@ -281,14 +294,20 @@ class Stage {
* from the pipeline. Most stages will not need to implement
* this function.
*/
virtual
void
disconnect_prepare
()
{
return
;
}
virtual
void
disconnect_prepare
()
{
return
;
}
/**
* Cleanup stage after disconnection
* After disconnection is completed, cleanup any resources held by the
* stage and prepare for destruction or re-initialization.
*/
virtual
void
cleanup
()
{
return
;
}
virtual
void
cleanup
()
{
return
;
}
// pipeline state
std
::
list
<
Stage
*>
next_stage_list_
;
// next stage(s) in the pipeline
...
...
@@ -298,33 +317,33 @@ class Stage {
friend
class
Threadpool
;
private:
private:
std
::
deque
<
StageEvent
*>
event_list_
;
// event queue
mutable
pthread_mutex_t
list_mutex_
;
// protects the event queue
pthread_cond_t
disconnect_cond_
;
// wait here for disconnect
bool
connected_
;
// is stage connected to pool?
unsigned
long
event_ref_
;
// # of outstanding events
Threadpool
*
th_pool_
=
nullptr
;
// Threadpool for this stage
};
inline
void
Stage
::
set_pool
(
Threadpool
*
th
)
{
ASSERT
((
th
!=
NULL
),
"threadpool not available for stage %s"
,
this
->
get_name
());
ASSERT
(
!
connected_
,
"attempt to set threadpool while connected: %s"
,
this
->
get_name
());
inline
void
Stage
::
set_pool
(
Threadpool
*
th
)
{
ASSERT
((
th
!=
NULL
),
"threadpool not available for stage %s"
,
this
->
get_name
());
ASSERT
(
!
connected_
,
"attempt to set threadpool while connected: %s"
,
this
->
get_name
());
th_pool_
=
th
;
}
inline
void
Stage
::
push_stage
(
Stage
*
st
)
{
ASSERT
((
st
!=
NULL
),
"next stage not available for stage %s"
,
this
->
get_name
());
ASSERT
(
!
connected_
,
"attempt to set push stage while connected: %s"
,
this
->
get_name
());
inline
void
Stage
::
push_stage
(
Stage
*
st
)
{
ASSERT
((
st
!=
NULL
),
"next stage not available for stage %s"
,
this
->
get_name
());
ASSERT
(
!
connected_
,
"attempt to set push stage while connected: %s"
,
this
->
get_name
());
next_stage_list_
.
push_back
(
st
);
}
inline
const
char
*
Stage
::
get_name
()
{
return
stage_name_
;
}
inline
const
char
*
Stage
::
get_name
()
{
return
stage_name_
;
}
}
//
namespace common
}
//
namespace common
#endif // __COMMON_SEDA_STAGE_H__
deps/common/seda/stage_event.cpp
浏览文件 @
e976988d
...
...
@@ -26,12 +26,12 @@ See the Mulan PSL v2 for more details. */
namespace
common
{
// Constructor
StageEvent
::
StageEvent
()
:
comp_cb_
(
NULL
),
ud_
(
NULL
),
cb_flag_
(
false
),
history_
(
NULL
),
stage_hops_
(
0
),
tm_info_
(
NULL
)
{}
StageEvent
::
StageEvent
()
:
comp_cb_
(
NULL
),
ud_
(
NULL
),
cb_flag_
(
false
),
history_
(
NULL
),
stage_hops_
(
0
),
tm_info_
(
NULL
)
{}
// Destructor
StageEvent
::~
StageEvent
()
{
StageEvent
::~
StageEvent
()
{
// clear all pending callbacks
while
(
comp_cb_
)
{
CompletionCallback
*
top
=
comp_cb_
;
...
...
@@ -52,7 +52,8 @@ StageEvent::~StageEvent() {
}
// Processing for this event is done; callbacks executed
void
StageEvent
::
done
()
{
void
StageEvent
::
done
()
{
CompletionCallback
*
top
;
if
(
comp_cb_
)
{
...
...
@@ -65,7 +66,8 @@ void StageEvent::done() {
}
// Processing for this event is done; callbacks executed immediately
void
StageEvent
::
done_immediate
()
{
void
StageEvent
::
done_immediate
()
{
CompletionCallback
*
top
;
if
(
comp_cb_
)
{
...
...
@@ -79,7 +81,8 @@ void StageEvent::done_immediate() {
}
}
void
StageEvent
::
done_timeout
()
{
void
StageEvent
::
done_timeout
()
{
CompletionCallback
*
top
;
if
(
comp_cb_
)
{
...
...
@@ -94,20 +97,26 @@ void StageEvent::done_timeout() {
}
// Push the completion callback onto the stack
void
StageEvent
::
push_callback
(
CompletionCallback
*
cb
)
{
void
StageEvent
::
push_callback
(
CompletionCallback
*
cb
)
{
cb
->
push_callback
(
comp_cb_
);
comp_cb_
=
cb
;
}
void
StageEvent
::
set_user_data
(
UserData
*
u
)
{
void
StageEvent
::
set_user_data
(
UserData
*
u
)
{
ud_
=
u
;
return
;
}
UserData
*
StageEvent
::
get_user_data
()
{
return
ud_
;
}
UserData
*
StageEvent
::
get_user_data
()
{
return
ud_
;
}
// Add stage to list of stages which have handled this event
void
StageEvent
::
save_stage
(
Stage
*
stg
,
HistType
type
)
{
void
StageEvent
::
save_stage
(
Stage
*
stg
,
HistType
type
)
{
if
(
!
history_
)
{
history_
=
new
std
::
list
<
HistEntry
>
;
}
...
...
@@ -118,16 +127,19 @@ void StageEvent::save_stage(Stage *stg, HistType type) {
}
}
void
StageEvent
::
set_timeout_info
(
time_t
deadline
)
{
void
StageEvent
::
set_timeout_info
(
time_t
deadline
)
{
TimeoutInfo
*
tmi
=
new
TimeoutInfo
(
deadline
);
set_timeout_info
(
tmi
);
}
void
StageEvent
::
set_timeout_info
(
const
StageEvent
&
ev
)
{
void
StageEvent
::
set_timeout_info
(
const
StageEvent
&
ev
)
{
set_timeout_info
(
ev
.
tm_info_
);
}
void
StageEvent
::
set_timeout_info
(
TimeoutInfo
*
tmi
)
{
void
StageEvent
::
set_timeout_info
(
TimeoutInfo
*
tmi
)
{
// release the previous timeout info
if
(
tm_info_
)
{
tm_info_
->
detach
();
...
...
@@ -139,7 +151,8 @@ void StageEvent::set_timeout_info(TimeoutInfo *tmi) {
}
}
bool
StageEvent
::
has_timed_out
()
{
bool
StageEvent
::
has_timed_out
()
{
if
(
!
tm_info_
)
{
return
false
;
}
...
...
@@ -148,15 +161,17 @@ bool StageEvent::has_timed_out() {
}
// Accessor function which wraps value for max hops an event is allowed
u32_t
&
get_max_event_hops
()
{
u32_t
&
get_max_event_hops
()
{
static
u32_t
max_event_hops
=
0
;
return
max_event_hops
;
}
// Accessor function which wraps value for event history flag
bool
&
get_event_history_flag
()
{
bool
&
get_event_history_flag
()
{
static
bool
event_history_flag
=
false
;
return
event_history_flag
;
}
}
//namespace common
\ No newline at end of file
}
// namespace common
\ No newline at end of file
deps/common/seda/stage_event.h
浏览文件 @
e976988d
...
...
@@ -58,7 +58,7 @@ class TimeoutInfo;
class
StageEvent
{
public:
public:
// Interface for collecting debugging information
typedef
enum
{
HANDLE_EV
=
0
,
CALLBACK_EV
,
TIMEOUT_EV
}
HistType
;
...
...
@@ -106,7 +106,10 @@ class StageEvent {
UserData
*
get_user_data
();
// True if event represents a callback
bool
is_callback
()
{
return
cb_flag_
;
}
bool
is_callback
()
{
return
cb_flag_
;
}
// Add stage to list of stages which have handled this event
void
save_stage
(
Stage
*
stg
,
HistType
type
);
...
...
@@ -123,12 +126,18 @@ class StageEvent {
// If the event has timed out (and should be dropped)
bool
has_timed_out
();
private:
private:
typedef
std
::
pair
<
Stage
*
,
HistType
>
HistEntry
;
// Interface to allow callbacks to be run on target stage's threads
void
mark_callback
()
{
cb_flag_
=
true
;
}
void
clear_callback
()
{
cb_flag_
=
false
;
}
void
mark_callback
()
{
cb_flag_
=
true
;
}
void
clear_callback
()
{
cb_flag_
=
false
;
}
// Set a timeout info into the event
void
set_timeout_info
(
TimeoutInfo
*
tmi
);
...
...
@@ -139,7 +148,6 @@ class StageEvent {
std
::
list
<
HistEntry
>
*
history_
;
// List of stages which have handled ev
u32_t
stage_hops_
;
// Number of stages which have handled ev
TimeoutInfo
*
tm_info_
;
// the timeout info for this event
};
/**
...
...
@@ -155,15 +163,18 @@ class StageEvent {
* originating stage can access the \c UserData member to recover its state.
*/
class
UserData
{
public:
public:
/**
* \brief A virtual destructor to enable the use of dynamic casts.
*/
virtual
~
UserData
()
{
return
;
}
virtual
~
UserData
()
{
return
;
}
};
bool
&
get_event_history_flag
();
u32_t
&
get_max_event_hops
();
}
//
namespace common
}
//
namespace common
#endif // __COMMON_SEDA_STAGE_EVENT_H__
deps/common/seda/stage_factory.h
浏览文件 @
e976988d
...
...
@@ -19,10 +19,9 @@ See the Mulan PSL v2 for more details. */
#include "common/seda/stage.h"
namespace
common
{
class
Stage
;
typedef
ClassFactory
<
Stage
>
StageFactory
;
}
//
namespace common
}
//
namespace common
#endif // __COMMON_SEDA_STAGE_FACTORY_H__
deps/common/seda/thread_pool.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
deps/common/seda/thread_pool.h
浏览文件 @
e976988d
...
...
@@ -42,7 +42,7 @@ class Stage;
*/
class
Threadpool
{
public:
public:
// Initialize the static data structures of ThreadPool
static
void
create_pool_key
();
...
...
@@ -109,8 +109,7 @@ class Threadpool {
// Get name of thread pool
const
std
::
string
&
get_name
();
protected:
protected:
/**
* Internal thread kill.
* Internal operation called only when a thread kill event is processed.
...
...
@@ -130,7 +129,7 @@ class Threadpool {
*/
unsigned
int
gen_kill_thread_events
(
unsigned
int
to_kill
);
private:
private:
/**
* Internal thread control function
* Function which contains the control loop for each service thread.
...
...
@@ -166,5 +165,5 @@ class Threadpool {
friend
class
KillThreadStage
;
};
}
//
namespace common
}
//
namespace common
#endif // __COMMON_SEDA_THREAD_POOL_H__
deps/common/seda/timer_stage.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
deps/common/seda/timer_stage.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
deps/common/time/datetime.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
deps/common/time/datetime.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
deps/common/time/timeout_info.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
deps/common/time/timeout_info.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
deps/common/version.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/obclient/client.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/event/execution_plan_event.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/event/execution_plan_event.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/event/session_event.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/event/session_event.h
浏览文件 @
e976988d
src/observer/event/sql_event.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/event/sql_event.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/event/storage_event.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/event/storage_event.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/handler/handler.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/ini_setting.h
浏览文件 @
e976988d
src/observer/init.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/init.h
浏览文件 @
e976988d
src/observer/main.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/net/connection_context.h
浏览文件 @
e976988d
src/observer/net/server.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/net/server.h
浏览文件 @
e976988d
src/observer/net/server_param.h
浏览文件 @
e976988d
src/observer/rc.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/rc.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/session/session.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/session/session.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/session/session_stage.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/session/session_stage.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/executor/execute_stage.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/executor/execute_stage.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/executor/execution_node.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/executor/execution_node.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/executor/tuple.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/executor/tuple.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/executor/value.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/optimizer/optimize_stage.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/optimizer/optimize_stage.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/parser/lex.yy.c
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/parser/lex.yy.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/parser/parse.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/parser/parse.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/parser/parse_defs.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/parser/parse_stage.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/parser/parse_stage.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/parser/resolve_stage.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/parser/resolve_stage.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/parser/yacc_sql.tab.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/plan_cache/plan_cache_stage.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/plan_cache/plan_cache_stage.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/query_cache/query_cache_stage.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/sql/query_cache/query_cache_stage.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/bplus_tree.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/bplus_tree.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/bplus_tree_index.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/bplus_tree_index.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/condition_filter.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/condition_filter.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/db.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/db.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/field_meta.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/field_meta.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/index.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/index.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/index_meta.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/index_meta.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/meta_util.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/meta_util.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/record_manager.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/record_manager.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/table.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/table.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/table_meta.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/common/table_meta.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/default/default_handler.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/default/default_handler.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/default/default_storage_stage.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/default/default_storage_stage.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/default/disk_buffer_pool.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/default/disk_buffer_pool.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/mem/mem_storage_stage.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/mem/mem_storage_stage.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/trx/trx.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
src/observer/storage/trx/trx.h
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
unitest/bitmap_test.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
unitest/md5_test.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
unitest/path_test.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
unitest/pidfile_test.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
unitest/rc_test.cpp
浏览文件 @
e976988d
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录