1.添加对Ctrl+C信号捕获并释放资源

2.修改valgrind提示错误
This commit is contained in:
fengweihao
2018-07-12 10:56:13 +08:00
parent 84624425a9
commit 7fa5f58be6
8 changed files with 102 additions and 47 deletions

View File

@@ -706,12 +706,14 @@ rd_encode_sendbuf(struct request_t *request, redisAsyncContext *c, char *sendbuf
int xret = -1;
uint64_t startTime = 0, endTime = 0;
libevent_thread *thread = threads + request->thread_id;
libevent_thread *thread = threads + request->thread_id;
struct config_bucket_t *rte = cert_default_config();
char cert[SG_DATA_SIZE] = {0}, pubkey[SG_DATA_SIZE] = {0};
startTime = rt_time_ms();
startTime = rt_time_ns();
x509_online_append(request->host, thread->key, thread->root, cert, pubkey);
if (cert[0] == '\0' && pubkey[0] == '\0'){
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Failed to issue certificate");
@@ -719,9 +721,10 @@ rd_encode_sendbuf(struct request_t *request, redisAsyncContext *c, char *sendbuf
goto finish;
}
endTime = rt_time_ms();
endTime = rt_time_ns();
thread->diffTime += (endTime - startTime);
//printf("%lu - %lu = %lu(%lu)\n", startTime, endTime, endTime - startTime, thread->diffTime);
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "%lu - %lu = %lu\n", startTime, endTime, endTime - startTime);
fs_internal_operate(thread->column_ids, thread->field_ids, SGstats.line_ids[2], SGstats.line_ids[3], thread->diffTime);
snprintf(sendbuf, SG_DATA_SIZE * 2, "%s%s", pubkey, cert);
@@ -765,9 +768,11 @@ void rd_get_callback(redisAsyncContext *c, void *r, void *privdata)
int xret = -1;
char sendbuf[SG_DATA_SIZE * 2] = {0};
redisReply *reply = (redisReply*)r;
redisReply *reply = (redisReply*)r;
struct request_t *request = (struct request_t *)privdata;
struct request_t *request = (struct request_t *)privdata;
struct evhttp_request *evh_req = request->evh_req;
switch(reply->type){
case REDIS_REPLY_STRING:
@@ -787,7 +792,7 @@ void rd_get_callback(redisAsyncContext *c, void *r, void *privdata)
if (xret < 0)
goto finish;
evhttp_socket_send(request->evh_req, sendbuf);
evhttp_socket_send(evh_req, sendbuf);
finish:
return;
}
@@ -909,11 +914,11 @@ finish:
void
pthread_work_proc(struct evhttp_request *evh_req, void *arg)
{
int xret = -1;
const char *cmdtype;
struct request_t *request = NULL;
struct evhttp_uri *decoded = NULL;
libevent_thread *thread_info = (libevent_thread *)arg;
int xret = -1;
const char *cmdtype = NULL;
struct request_t *request = NULL;
struct evhttp_uri *decoded = NULL;
libevent_thread *t = (libevent_thread *)arg;
/* we want to know if this connection closes on us */
evhttp_connection_set_closecb(evhttp_request_get_connection(evh_req), evhttp_socket_close_cb, NULL);
@@ -928,34 +933,39 @@ pthread_work_proc(struct evhttp_request *evh_req, void *arg)
request = (struct request_t *) kmalloc (sizeof(struct request_t), MPF_CLR, -1);
if (request != NULL){
request->thread_id = thread_info->id;
request->evh_req = evh_req;
request->thread_id = t->id;
request->evh_req = evh_req;
}
switch (evhttp_request_get_command(evh_req)) {
case EVHTTP_REQ_GET: cmdtype = "GET"; break;
default: cmdtype = "unknown"; break;
case EVHTTP_REQ_GET: cmdtype = "GET"; break;
default: cmdtype = "unknown"; break;
}
fs_internal_operate(thread_info->column_ids, -1, SGstats.line_ids[0], -1, 0);
fs_internal_operate(t->column_ids, -1, SGstats.line_ids[0], -1, 0);
rt_decode_uri(uri, request->host, &request->flag, &request->valid);
mesa_runtime_log(RLOG_LV_INFO, MODULE_NAME, "[Thread %d]Received a %s request for %s, host:%s, flag:%d, valid:%d\nHeaders:",
request->thread_id, cmdtype, uri, request->host,
request->flag, request->valid);
request->thread_id, cmdtype, uri, request->host,
request->flag, request->valid);
if (request->host[0] != '\0' && request->evh_req != NULL){
#ifdef RD_MUTEX_LOCK
rd_mutex_lock(request->host, 30, &request->mtx, thread_info->sync);
char key[DATALEN] = {0};
snprintf(key, DATALEN, "%s_%s",request->host, "key");
rd_mutex_lock(key, 30, &request->mtx, t->sync);
#endif
xret = redisAsyncCommand(thread_info->cl_ctx, rd_get_callback, request, "GET %s", request->host);
xret = redisAsyncCommand(t->cl_ctx, rd_get_callback, request, "GET %s", request->host);
if (xret < 0)
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Failed to get information from redis server");
}
else {
kfree(request);
evhttp_uri_free(decoded);
goto error;
}
evhttp_uri_free(decoded);
goto finish;
@@ -990,20 +1000,21 @@ finish:
static void *pthread_worker_libevent(void *arg)
{
int xret = -1;
struct evhttp *http = NULL;
struct event_base *base = NULL;
int xret = -1;
struct evhttp *http = NULL;
struct event_base *base = NULL;
struct evhttp_bound_socket *bound = NULL;
libevent_thread *thread_info = (libevent_thread *)arg;
struct config_bucket_t *rte = cert_default_config();
struct config_bucket_t *rte = cert_default_config();
base = event_base_new();
if (! base) {
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Can't allocate event base\n");
goto finish;
}
http = evhttp_new(base);
if (!http) {
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "couldn't create evhttp. Exiting.\n");
@@ -1021,8 +1032,8 @@ static void *pthread_worker_libevent(void *arg)
if (bound != NULL) {
mesa_runtime_log(RLOG_LV_INFO, MODULE_NAME, "Bound(%p) to port %d - Awaiting connections ... ", bound, rte->e_port);
}
event_base_dispatch(base);
event_base_dispatch(base);
error:
event_base_free(base);
finish:
@@ -1093,18 +1104,18 @@ libevent_socket_init()
struct sockaddr_in sin;
evutil_socket_t accept_fd = -1;
int xret = -1;
unsigned int tid = 0;
unsigned int tid = 0;
libevent_thread *thread = NULL;
struct config_bucket_t *rte = cert_default_config();
unsigned int thread_nu = rte->thread_nu;
unsigned int thread_nu = rte->thread_nu;
/* Create a new evhttp object to handle requests. */
memset(&sin, 0, sizeof(struct sockaddr_in));
sin.sin_family = AF_INET;
sin.sin_port = htons(rte->e_port);
accept_fd = evhttp_listen_socket_byuser((struct sockaddr*)&sin, sizeof(struct sockaddr_in),
LEV_OPT_REUSEABLE_PORT|LEV_OPT_CLOSE_ON_FREE, -1);
sin.sin_port = htons(rte->e_port);
accept_fd = evhttp_listen_socket_byuser((struct sockaddr*)&sin, sizeof(struct sockaddr_in),
LEV_OPT_REUSEABLE_PORT|LEV_OPT_CLOSE_ON_FREE, -1);
if (accept_fd < 0) {
mesa_runtime_log(RLOG_LV_FATAL, MODULE_NAME, "Could not create a listen!\n");
goto finish;
@@ -1122,11 +1133,11 @@ libevent_socket_init()
/* Create threads after we've done all the libevent setup. */
for (tid = 0; tid < thread_nu; tid++) {
thread = threads + tid;
thread = threads + tid;
thread->id = tid;
thread->id = tid;
thread->accept_fd = accept_fd;
thread->routine = pthread_worker_libevent;
thread->routine = pthread_worker_libevent;
fs_screen_preview(thread);
@@ -1165,8 +1176,30 @@ rt_get_pname_by_pid(pid_t pid, char *task_name)
}
}
static int
fs_screen_init()
void sigproc(int __attribute__((__unused__))sig)
{
unsigned int tid = 0;
libevent_thread *thread = NULL;
struct config_bucket_t *rte = cert_default_config();
rd_lock_fini();
for (tid = 0; tid < rte->thread_nu; tid++) {
thread = threads + tid;
X509_free(thread->root);
EVP_PKEY_free(thread->key);
if (thread->cl_ctx)
redisAsyncDisconnect(thread->cl_ctx);
redisFree(thread->sync);
}
kfree(threads);
exit(1);
}
static int cert_screen_init()
{
int value=0;
char stat_path[63] = {0};
@@ -1200,6 +1233,8 @@ fs_screen_init()
snprintf(buff,sizeof(buff),"%s", "take-time");
SGstats.line_ids[3] = FS_internal_register(SGstats.handle, FS_STYLE_COLUMN, FS_CALC_CURRENT, buff);
value=SGstats.line_ids[3];
FS_internal_set_para(SGstats.handle, ID_INVISBLE, &value, sizeof(value));
snprintf(buff,sizeof(buff),"average-time");
FS_internal_register_ratio(SGstats.handle, SGstats.line_ids[3],
SGstats.line_ids[2], 1,
@@ -1214,7 +1249,7 @@ int cert_session_init()
{
int xret = 0;
fs_screen_init();
cert_screen_init();
libevent_socket_init();

View File

@@ -39,7 +39,9 @@ typedef struct {
} libevent_thread;
extern int cert_session_init();
int cert_session_init();
void sigproc(int __attribute__((__unused__))sig);
#endif

View File

@@ -8,6 +8,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "rt_string.h"
#include "rt_common.h"
@@ -103,6 +104,7 @@ int main(int argc, char **argv)
if (MODE_TYPE(0x20)){
daemonize();
}
signal(SIGINT, sigproc);
cert_preview();
cert_session_init();

View File

@@ -61,13 +61,13 @@ get_unique_lockid()
}
static int
rd_lock_instance(redisContext *c, const char *resource,
rd_lock_instance(redisContext *c, const char *key,
const char *val, const int ttl)
{
int xret = 0;
redisReply *reply;
reply = (redisReply *)redisCommand(c, "set %s %s px %d nx", resource, val, ttl);
reply = (redisReply *)redisCommand(c, "set %s %s px %d nx", key, val, ttl);
if (NULL == reply)
goto finish;
@@ -129,15 +129,14 @@ int rd_mutex_unlock(struct rd_lock_scb *mtx, struct redisContext *c)
freeReplyObject(reply);
}
if (reply->integer){
sdsfree(mtx->m_resource);
sdsfree(mtx->m_val);
}
sdsfree(mtx->m_resource);
sdsfree(mtx->m_val);
return 0;
}
/* redis lock*/
int rd_mutex_lock(const char *resource, const int ttl,
int rd_mutex_lock(const char *key, const int ttl,
struct rd_lock_scb *mtx, struct redisContext *c)
{
struct rd_RedLock *redlock = mutx_redlock();
@@ -149,7 +148,7 @@ int rd_mutex_lock(const char *resource, const int ttl,
if (!val) {
return xret;
}
mtx->m_resource = sdsnew(resource);
mtx->m_resource = sdsnew(key);
mtx->m_val = val;
retryCount = redlock->m_retryCount;
@@ -161,7 +160,7 @@ int rd_mutex_lock(const char *resource, const int ttl,
goto finish;
}
if (rd_lock_instance(c, resource, val, ttl)) {
if (rd_lock_instance(c, key, val, ttl)) {
n++;
}
@@ -201,3 +200,10 @@ void rd_lock_init()
return;
}
void rd_lock_fini()
{
struct rd_RedLock *rdlock = mutx_redlock();
sdsfree(rdlock->m_unlockScript);
}

View File

@@ -18,6 +18,8 @@ struct rd_lock_scb{
void rd_lock_init();
void rd_lock_fini();
int rd_mutex_lock(const char *resource, const int ttl,
struct rd_lock_scb *mtx, struct redisContext *c);

View File

@@ -13,6 +13,7 @@ using namespace std;
extern "C" screen_stat_handle_t FS_internal_create_handle(void);
extern "C" int FS_internal_set_para(screen_stat_handle_t handle, enum FS_option type,const void* value,int size);
extern "C" void FS_internal_start(screen_stat_handle_t handle);
extern "C" void FS_internal_stop(screen_stat_handle_t* handle);
extern "C" int FS_internal_register(screen_stat_handle_t handle,enum field_dsp_style_t style,
enum field_calc_algo calc_type,const char* name);
extern "C" int FS_internal_operate(screen_stat_handle_t handle,int id,int column_id,enum field_op op,long long value);
@@ -35,6 +36,11 @@ void FS_internal_start(screen_stat_handle_t handle)
FS_start(handle);
}
void FS_internal_stop(screen_stat_handle_t* handle)
{
FS_stop(handle);
}
int FS_internal_register(screen_stat_handle_t handle,enum field_dsp_style_t style,
enum field_calc_algo calc_type,const char* name)
{

View File

@@ -50,6 +50,8 @@ int FS_internal_set_para(screen_stat_handle_t handle, enum FS_option type,const
void FS_internal_start(screen_stat_handle_t handle);
void FS_internal_stop(screen_stat_handle_t* handle);
int FS_internal_register(screen_stat_handle_t handle,enum field_dsp_style_t style,
enum field_calc_algo calc_type,const char* name);

Binary file not shown.