mirror of
https://github.com/lxsang/ant-http
synced 2025-07-13 13:04:23 +02:00
add statistic log to scheduler, good for debug
This commit is contained in:
11
lib/handle.c
11
lib/handle.c
@ -6,6 +6,7 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
//open ssl
|
||||
#ifdef USE_OPENSSL
|
||||
@ -295,7 +296,7 @@ void antd_send_header(void *cl, antd_response_header_t *res)
|
||||
}
|
||||
else
|
||||
{
|
||||
client->status = Z_NO_FLUSH;
|
||||
client->z_status = Z_NO_FLUSH;
|
||||
dput(res->header, "Content-Encoding", strdup("gzip"));
|
||||
}
|
||||
}
|
||||
@ -309,7 +310,7 @@ void antd_send_header(void *cl, antd_response_header_t *res)
|
||||
}
|
||||
else
|
||||
{
|
||||
client->status = Z_NO_FLUSH;
|
||||
client->z_status = Z_NO_FLUSH;
|
||||
dput(res->header, "Content-Encoding", strdup("deflate"));
|
||||
}
|
||||
}
|
||||
@ -384,7 +385,7 @@ int antd_send(void *src, const void *data_in, int len_in)
|
||||
{
|
||||
zstream->avail_out = BUFFLEN;
|
||||
zstream->next_out = buf;
|
||||
if (deflate(zstream, source->status) == Z_STREAM_ERROR)
|
||||
if (deflate(zstream, source->z_status) == Z_STREAM_ERROR)
|
||||
{
|
||||
source->z_level = current_zlevel;
|
||||
data = NULL;
|
||||
@ -729,9 +730,9 @@ int antd_close(void *src)
|
||||
//TODO: send finish data to the socket before quit
|
||||
if (source->zstream)
|
||||
{
|
||||
if (source->status == Z_NO_FLUSH && source->z_level != ANTD_CNONE)
|
||||
if (source->z_status == Z_NO_FLUSH && source->z_level != ANTD_CNONE)
|
||||
{
|
||||
source->status = Z_FINISH;
|
||||
source->z_status = Z_FINISH;
|
||||
antd_send(source, "", 0);
|
||||
}
|
||||
deflateEnd(source->zstream);
|
||||
|
113
lib/handle.h
113
lib/handle.h
@ -6,44 +6,59 @@
|
||||
#include "list.h"
|
||||
#include "dictionary.h"
|
||||
|
||||
|
||||
#define SERVER_NAME "Antd"
|
||||
#define IS_POST(method) (strcmp(method,"POST")== 0)
|
||||
#define IS_GET(method) (strcmp(method,"GET")== 0)
|
||||
#define R_STR(d,k) ((char*)dvalue(d,k))
|
||||
#define R_INT(d,k) (atoi(dvalue(d,k)))
|
||||
#define R_FLOAT(d,k) ((double)atof(dvalue(d,k)))
|
||||
#define R_PTR(d,k) (dvalue(d,k))
|
||||
#define IS_POST(method) (strcmp(method, "POST") == 0)
|
||||
#define IS_GET(method) (strcmp(method, "GET") == 0)
|
||||
#define R_STR(d, k) ((char *)dvalue(d, k))
|
||||
#define R_INT(d, k) (atoi(dvalue(d, k)))
|
||||
#define R_FLOAT(d, k) ((double)atof(dvalue(d, k)))
|
||||
#define R_PTR(d, k) (dvalue(d, k))
|
||||
#define __RESULT__ "{\"result\":%d,\"msg\":\"%s\"}"
|
||||
#define FORM_URL_ENCODE "application/x-www-form-urlencoded"
|
||||
#define FORM_MULTI_PART "multipart/form-data"
|
||||
#define FORM_URL_ENCODE "application/x-www-form-urlencoded"
|
||||
#define FORM_MULTI_PART "multipart/form-data"
|
||||
#define MAX_IO_WAIT_TIME 5 // second
|
||||
|
||||
#define ANTD_CLIENT_ACCEPT 0x0
|
||||
#define ANTD_CLIENT_HANDSHAKE 0x1
|
||||
#define ANTD_CLIENT_HEADER_DECODE 0x2
|
||||
#define ANTD_CLIENT_PLUGIN_EXEC 0x3
|
||||
#define ANTD_CLIENT_PROTO_CHECK 0x4
|
||||
#define ANTD_CLIENT_RESOLVE_REQUEST 0x5
|
||||
#define ANTD_CLIENT_SERVE_FILE 0x6
|
||||
|
||||
typedef enum {ANTD_CGZ, ANTD_CDEFL, ANTD_CNONE} antd_compress_t;
|
||||
typedef enum
|
||||
{
|
||||
ANTD_CGZ,
|
||||
ANTD_CDEFL,
|
||||
ANTD_CNONE
|
||||
} antd_compress_t;
|
||||
|
||||
//extern config_t server_config;
|
||||
|
||||
typedef struct {
|
||||
typedef struct
|
||||
{
|
||||
unsigned int port;
|
||||
int usessl;
|
||||
char* htdocs;
|
||||
char *htdocs;
|
||||
int sock;
|
||||
dictionary_t rules;
|
||||
} port_config_t;
|
||||
|
||||
typedef struct{
|
||||
typedef struct
|
||||
{
|
||||
int sock;
|
||||
void* ssl;
|
||||
int status;
|
||||
void *ssl;
|
||||
int state;
|
||||
time_t last_io;
|
||||
// compress
|
||||
antd_compress_t z_level;
|
||||
void* zstream;
|
||||
void *zstream;
|
||||
int z_status;
|
||||
} antd_client_t;
|
||||
|
||||
typedef struct {
|
||||
antd_client_t* client;
|
||||
typedef struct
|
||||
{
|
||||
antd_client_t *client;
|
||||
dictionary_t request;
|
||||
} antd_request_t;
|
||||
|
||||
@ -55,15 +70,15 @@ typedef struct
|
||||
|
||||
} antd_response_header_t;
|
||||
|
||||
|
||||
|
||||
typedef struct {
|
||||
//int port;
|
||||
char *plugins_dir;
|
||||
typedef struct
|
||||
{
|
||||
//int port;
|
||||
char *plugins_dir;
|
||||
char *plugins_ext;
|
||||
char *db_path;
|
||||
//char* htdocs;
|
||||
char* tmpdir;
|
||||
char *tmpdir;
|
||||
char *stat_fifo_path;
|
||||
dictionary_t handlers;
|
||||
int backlog;
|
||||
int maxcon;
|
||||
@ -72,51 +87,51 @@ typedef struct {
|
||||
int max_upload_size;
|
||||
// ssl
|
||||
int enable_ssl;
|
||||
char* sslcert;
|
||||
char* sslkey;
|
||||
char* ssl_cipher;
|
||||
char *sslcert;
|
||||
char *sslkey;
|
||||
char *ssl_cipher;
|
||||
int gzip_enable;
|
||||
list_t gzip_types;
|
||||
dictionary_t mimes;
|
||||
dictionary_t ports;
|
||||
// #endif
|
||||
}config_t;
|
||||
// #endif
|
||||
} config_t;
|
||||
|
||||
typedef struct {
|
||||
char name[128];
|
||||
typedef struct
|
||||
{
|
||||
char name[128];
|
||||
char dbpath[512];
|
||||
char tmpdir[512];
|
||||
char pdir[512];
|
||||
int raw_body;
|
||||
} plugin_header_t;
|
||||
|
||||
int __attribute__((weak)) require_plugin(const char *);
|
||||
void __attribute__((weak)) htdocs(antd_request_t *rq, char *dest);
|
||||
void __attribute__((weak)) dbdir(char *dest);
|
||||
void __attribute__((weak)) tmpdir(char *dest);
|
||||
void __attribute__((weak)) plugindir(char *dest);
|
||||
|
||||
int __attribute__((weak)) require_plugin(const char*);
|
||||
void __attribute__((weak)) htdocs(antd_request_t* rq, char* dest);
|
||||
void __attribute__((weak)) dbdir(char* dest);
|
||||
void __attribute__((weak)) tmpdir(char* dest);
|
||||
void __attribute__((weak)) plugindir(char* dest);
|
||||
|
||||
int __attribute__((weak)) compressable(char* ctype);
|
||||
int __attribute__((weak)) compressable(char *ctype);
|
||||
|
||||
void set_nonblock(int socket);
|
||||
//void set_block(int socket);
|
||||
|
||||
void antd_send_header(void*,antd_response_header_t*);
|
||||
const char* get_status_str(int stat);
|
||||
int __t(void*, const char*,...);
|
||||
int __b(void*, const unsigned char*, int);
|
||||
int __f(void*, const char*);
|
||||
void antd_send_header(void *, antd_response_header_t *);
|
||||
const char *get_status_str(int stat);
|
||||
int __t(void *, const char *, ...);
|
||||
int __b(void *, const unsigned char *, int);
|
||||
int __f(void *, const char *);
|
||||
|
||||
int upload(const char*, const char*);
|
||||
int upload(const char *, const char *);
|
||||
|
||||
/*Default function for plugin*/
|
||||
void antd_error(void* client, int status, const char* msg);
|
||||
void antd_error(void *client, int status, const char *msg);
|
||||
|
||||
int ws_enable(dictionary_t);
|
||||
int read_buf(void* sock,char* buf,int i);
|
||||
int antd_send( void *source, const void* data, int len);
|
||||
int antd_recv( void *source, void* data, int len);
|
||||
int antd_close(void* source);
|
||||
int read_buf(void *sock, char *buf, int i);
|
||||
int antd_send(void *source, const void *data, int len);
|
||||
int antd_recv(void *source, void *data, int len);
|
||||
int antd_close(void *source);
|
||||
void destroy_request(void *data);
|
||||
#endif
|
||||
|
288
lib/scheduler.c
288
lib/scheduler.c
@ -1,18 +1,33 @@
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <poll.h>
|
||||
#include <unistd.h>
|
||||
#include "scheduler.h"
|
||||
#include "utils.h"
|
||||
|
||||
static void enqueue(antd_task_queue_t* q, antd_task_t* task)
|
||||
static void set_nonblock(int fd)
|
||||
{
|
||||
int flags;
|
||||
flags = fcntl(fd, F_GETFL, 0);
|
||||
if(flags == -1)
|
||||
{
|
||||
ERROR("Unable to set flag");
|
||||
}
|
||||
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||
}
|
||||
static void enqueue(antd_task_queue_t *q, antd_task_t *task)
|
||||
{
|
||||
antd_task_item_t it = *q;
|
||||
while(it && it->next != NULL)
|
||||
while (it && it->next != NULL)
|
||||
it = it->next;
|
||||
antd_task_item_t taski = (antd_task_item_t)malloc(sizeof *taski);
|
||||
taski->task = task;
|
||||
taski->next = NULL;
|
||||
if(!it) // first task
|
||||
if (!it) // first task
|
||||
{
|
||||
*q = taski;
|
||||
}
|
||||
@ -22,19 +37,20 @@ static void enqueue(antd_task_queue_t* q, antd_task_t* task)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void stop(antd_scheduler_t* scheduler)
|
||||
static void stop(antd_scheduler_t *scheduler)
|
||||
{
|
||||
scheduler->status = 0;
|
||||
// unlock all idle workers if any
|
||||
for (int i = 0; i < scheduler->n_workers; i++)
|
||||
sem_post(scheduler->worker_sem);
|
||||
if(scheduler->scheduler_sem)
|
||||
if (scheduler->scheduler_sem)
|
||||
sem_post(scheduler->scheduler_sem);
|
||||
for (int i = 0; i < scheduler->n_workers; i++)
|
||||
if(scheduler->workers[i].id != -1)
|
||||
if (scheduler->workers[i].id != -1)
|
||||
pthread_join(scheduler->workers[i].tid, NULL);
|
||||
if(scheduler->workers) free(scheduler->workers);
|
||||
if (scheduler->workers)
|
||||
free(scheduler->workers);
|
||||
(void)pthread_join(scheduler->stat_tid, NULL);
|
||||
// destroy all the mutex
|
||||
pthread_mutex_destroy(&scheduler->scheduler_lock);
|
||||
pthread_mutex_destroy(&scheduler->worker_lock);
|
||||
@ -45,10 +61,10 @@ static void stop(antd_scheduler_t* scheduler)
|
||||
sem_close(scheduler->worker_sem);
|
||||
}
|
||||
|
||||
static antd_task_item_t dequeue(antd_task_queue_t* q)
|
||||
static antd_task_item_t dequeue(antd_task_queue_t *q)
|
||||
{
|
||||
antd_task_item_t it = *q;
|
||||
if(it)
|
||||
if (it)
|
||||
{
|
||||
*q = it->next;
|
||||
it->next = NULL;
|
||||
@ -56,24 +72,23 @@ static antd_task_item_t dequeue(antd_task_queue_t* q)
|
||||
return it;
|
||||
}
|
||||
|
||||
|
||||
antd_callback_t* callback_of( void* (*callback)(void*) )
|
||||
antd_callback_t *callback_of(void *(*callback)(void *))
|
||||
{
|
||||
antd_callback_t* cb = NULL;
|
||||
if(callback)
|
||||
antd_callback_t *cb = NULL;
|
||||
if (callback)
|
||||
{
|
||||
cb = (antd_callback_t*)malloc(sizeof *cb);
|
||||
cb = (antd_callback_t *)malloc(sizeof *cb);
|
||||
cb->handle = callback;
|
||||
cb->next = NULL;
|
||||
}
|
||||
return cb;
|
||||
}
|
||||
|
||||
static void free_callback(antd_callback_t* cb)
|
||||
static void free_callback(antd_callback_t *cb)
|
||||
{
|
||||
antd_callback_t* it = cb;
|
||||
antd_callback_t* curr;
|
||||
while(it)
|
||||
antd_callback_t *it = cb;
|
||||
antd_callback_t *curr;
|
||||
while (it)
|
||||
{
|
||||
curr = it;
|
||||
it = it->next;
|
||||
@ -81,25 +96,26 @@ static void free_callback(antd_callback_t* cb)
|
||||
}
|
||||
}
|
||||
|
||||
static void enqueue_callback(antd_callback_t* cb, antd_callback_t* el)
|
||||
static void enqueue_callback(antd_callback_t *cb, antd_callback_t *el)
|
||||
{
|
||||
antd_callback_t* it = cb;
|
||||
while(it && it->next != NULL)
|
||||
antd_callback_t *it = cb;
|
||||
while (it && it->next != NULL)
|
||||
it = it->next;
|
||||
if(!it) return; // this should not happend
|
||||
if (!it)
|
||||
return; // this should not happend
|
||||
it->next = el;
|
||||
}
|
||||
|
||||
static void execute_callback(antd_scheduler_t* scheduler, antd_task_t* task)
|
||||
static void execute_callback(antd_scheduler_t *scheduler, antd_task_t *task)
|
||||
{
|
||||
antd_callback_t* cb = task->callback;
|
||||
if(cb)
|
||||
antd_callback_t *cb = task->callback;
|
||||
if (cb)
|
||||
{
|
||||
// call the first come call back
|
||||
task->handle = cb->handle;
|
||||
task->callback = task->callback->next;
|
||||
task->priority = task->priority + 1;
|
||||
if(task->priority > N_PRIORITY - 1)
|
||||
if (task->priority > N_PRIORITY - 1)
|
||||
{
|
||||
task->priority = N_PRIORITY - 1;
|
||||
}
|
||||
@ -116,21 +132,23 @@ static void destroy_queue(antd_task_queue_t q)
|
||||
{
|
||||
antd_task_item_t it, curr;
|
||||
it = q;
|
||||
while(it)
|
||||
while (it)
|
||||
{
|
||||
// first free the task
|
||||
if(it->task && it->task->callback) free_callback(it->task->callback);
|
||||
if(it->task) free(it->task);
|
||||
if (it->task && it->task->callback)
|
||||
free_callback(it->task->callback);
|
||||
if (it->task)
|
||||
free(it->task);
|
||||
// then free the placeholder
|
||||
curr = it;
|
||||
it = it->next;
|
||||
free(curr);
|
||||
}
|
||||
}
|
||||
static void* work(antd_worker_t* worker)
|
||||
static void *work(antd_worker_t *worker)
|
||||
{
|
||||
antd_scheduler_t* scheduler = (antd_scheduler_t*) worker->manager;
|
||||
while(scheduler->status)
|
||||
antd_scheduler_t *scheduler = (antd_scheduler_t *)worker->manager;
|
||||
while (scheduler->status)
|
||||
{
|
||||
antd_task_item_t it;
|
||||
pthread_mutex_lock(&scheduler->worker_lock);
|
||||
@ -139,7 +157,7 @@ static void* work(antd_worker_t* worker)
|
||||
// execute the task
|
||||
//LOG("task executed by worker %d\n", worker->pid);
|
||||
// no task to execute, just sleep wait
|
||||
if(!it)
|
||||
if (!it)
|
||||
{
|
||||
//LOG("Worker %d goes to idle state\n", worker->id);
|
||||
sem_wait(scheduler->worker_sem);
|
||||
@ -149,7 +167,86 @@ static void* work(antd_worker_t* worker)
|
||||
//LOG("task executed by worker %d\n", worker->id);
|
||||
antd_execute_task(scheduler, it);
|
||||
}
|
||||
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void *statistic(antd_scheduler_t *scheduler)
|
||||
{
|
||||
struct pollfd fdp;
|
||||
int ret;
|
||||
char buffer[MAX_FIFO_NAME_SZ];
|
||||
antd_task_item_t it;
|
||||
while (scheduler->status)
|
||||
{
|
||||
if (scheduler->stat_fd == -1)
|
||||
{
|
||||
scheduler->stat_fd = open(scheduler->stat_fifo, O_RDWR);
|
||||
if (scheduler->stat_fd == -1)
|
||||
{
|
||||
ERROR("Unable to open FIFO %s: %s", scheduler->stat_fifo, strerror(errno));
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
fdp.fd = scheduler->stat_fd;
|
||||
fdp.events = POLLOUT;
|
||||
// poll the fd in blocking mode
|
||||
ret = poll(&fdp, 1, -1);
|
||||
|
||||
if (ret > 0 && (fdp.revents & POLLOUT) && scheduler->pending_task > 0)
|
||||
{
|
||||
pthread_mutex_lock(&scheduler->scheduler_lock);
|
||||
// write statistic data
|
||||
snprintf(buffer, MAX_FIFO_NAME_SZ, "Pending task: %d. Detail:\n", scheduler->pending_task);
|
||||
ret = write(scheduler->stat_fd, buffer, strlen(buffer));
|
||||
|
||||
for (int i = 0; i < N_PRIORITY; i++)
|
||||
{
|
||||
snprintf(buffer, MAX_FIFO_NAME_SZ, "#### PRIORITY: %d\n", i);
|
||||
ret = write(scheduler->stat_fd, buffer, strlen(buffer));
|
||||
|
||||
it = scheduler->task_queue[i];
|
||||
while (it)
|
||||
{
|
||||
// send statistic on task data
|
||||
snprintf(buffer, MAX_FIFO_NAME_SZ, "---- Task created at: %lu ----\n", it->task->stamp);
|
||||
ret = write(scheduler->stat_fd, buffer, strlen(buffer));
|
||||
|
||||
// send statistic on task data
|
||||
snprintf(buffer, MAX_FIFO_NAME_SZ, "Access time: %lu\nn", (unsigned long)it->task->access_time);
|
||||
ret = write(scheduler->stat_fd, buffer, strlen(buffer));
|
||||
|
||||
snprintf(buffer, MAX_FIFO_NAME_SZ, "Current time: %lu\n", (unsigned long)time(NULL));
|
||||
ret = write(scheduler->stat_fd, buffer, strlen(buffer));
|
||||
|
||||
snprintf(buffer, MAX_FIFO_NAME_SZ, "Task type: %d\n", it->task->type);
|
||||
ret = write(scheduler->stat_fd, buffer, strlen(buffer));
|
||||
|
||||
if (it->task->handle)
|
||||
{
|
||||
snprintf(buffer, MAX_FIFO_NAME_SZ, "Has handle: yes\n");
|
||||
ret = write(scheduler->stat_fd, buffer, strlen(buffer));
|
||||
}
|
||||
|
||||
if (it->task->callback)
|
||||
{
|
||||
snprintf(buffer, MAX_FIFO_NAME_SZ, "Has callback: yes\n");
|
||||
ret = write(scheduler->stat_fd, buffer, strlen(buffer));
|
||||
}
|
||||
|
||||
// now print all task data statistic
|
||||
if (scheduler->stat_data_cb)
|
||||
{
|
||||
scheduler->stat_data_cb(scheduler->stat_fd, it->task->data);
|
||||
}
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&scheduler->scheduler_lock);
|
||||
ret = close(scheduler->stat_fd);
|
||||
scheduler->stat_fd = -1;
|
||||
usleep(5000);
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@ -159,14 +256,17 @@ static void* work(antd_worker_t* worker)
|
||||
init the main scheduler
|
||||
*/
|
||||
|
||||
int antd_scheduler_init(antd_scheduler_t* scheduler, int n)
|
||||
int antd_scheduler_init(antd_scheduler_t *scheduler, int n)
|
||||
{
|
||||
scheduler->n_workers = n;
|
||||
scheduler->status = 1;
|
||||
scheduler->workers_queue = NULL;
|
||||
scheduler->pending_task = 0 ;
|
||||
scheduler->pending_task = 0;
|
||||
scheduler->validate_data = 0;
|
||||
scheduler->destroy_data = NULL;
|
||||
scheduler->stat_fd = -1;
|
||||
//scheduler->stat_data_cb = NULL;
|
||||
//memset(scheduler->stat_fifo, 0, MAX_FIFO_NAME_SZ);
|
||||
// init semaphore
|
||||
scheduler->scheduler_sem = sem_open("scheduler", O_CREAT, 0600, 0);
|
||||
if (scheduler->scheduler_sem == SEM_FAILED)
|
||||
@ -180,25 +280,26 @@ int antd_scheduler_init(antd_scheduler_t* scheduler, int n)
|
||||
ERROR("Cannot open semaphore for workers");
|
||||
return -1;
|
||||
}
|
||||
// init lock
|
||||
pthread_mutex_init(&scheduler->scheduler_lock,NULL);
|
||||
// init lock
|
||||
pthread_mutex_init(&scheduler->scheduler_lock, NULL);
|
||||
pthread_mutex_init(&scheduler->worker_lock, NULL);
|
||||
pthread_mutex_init(&scheduler->pending_lock, NULL);
|
||||
for(int i = 0; i < N_PRIORITY; i++) scheduler->task_queue[i] = NULL;
|
||||
for (int i = 0; i < N_PRIORITY; i++)
|
||||
scheduler->task_queue[i] = NULL;
|
||||
// create scheduler.workers
|
||||
if(n > 0)
|
||||
if (n > 0)
|
||||
{
|
||||
scheduler->workers = (antd_worker_t*)malloc(n*(sizeof(antd_worker_t)));
|
||||
if(!scheduler->workers)
|
||||
scheduler->workers = (antd_worker_t *)malloc(n * (sizeof(antd_worker_t)));
|
||||
if (!scheduler->workers)
|
||||
{
|
||||
ERROR("Cannot allocate memory for worker");
|
||||
return -1;
|
||||
}
|
||||
for(int i = 0; i < scheduler->n_workers;i++)
|
||||
for (int i = 0; i < scheduler->n_workers; i++)
|
||||
{
|
||||
scheduler->workers[i].id = -1;
|
||||
scheduler->workers[i].manager = (void*)scheduler;
|
||||
if (pthread_create(&scheduler->workers[i].tid, NULL,(void *(*)(void *))work, (void*)&scheduler->workers[i]) != 0)
|
||||
scheduler->workers[i].manager = (void *)scheduler;
|
||||
if (pthread_create(&scheduler->workers[i].tid, NULL, (void *(*)(void *))work, (void *)&scheduler->workers[i]) != 0)
|
||||
{
|
||||
ERROR("pthread_create: cannot create worker: %s", strerror(errno));
|
||||
return -1;
|
||||
@ -209,6 +310,34 @@ int antd_scheduler_init(antd_scheduler_t* scheduler, int n)
|
||||
}
|
||||
}
|
||||
}
|
||||
// delete the fifo if any
|
||||
if (scheduler->stat_fifo[0] != '\0')
|
||||
{
|
||||
LOG("Statistic fifo at: %s", scheduler->stat_fifo);
|
||||
(void)remove(scheduler->stat_fifo);
|
||||
// create the fifo file
|
||||
if (mkfifo(scheduler->stat_fifo, 0666) == -1)
|
||||
{
|
||||
ERROR("Unable to create statictis FIFO %s: %s", scheduler->stat_fifo, strerror(errno));
|
||||
}
|
||||
else
|
||||
{
|
||||
// open the fifo in write mode
|
||||
scheduler->stat_fd = open(scheduler->stat_fifo, O_RDWR);
|
||||
if (scheduler->stat_fd == -1)
|
||||
{
|
||||
ERROR("Unable to open FIFO %s: %s", scheduler->stat_fifo, strerror(errno));
|
||||
}
|
||||
else
|
||||
{
|
||||
set_nonblock(scheduler->stat_fd);
|
||||
if (pthread_create(&scheduler->stat_tid, NULL, (void *(*)(void *))statistic, scheduler) != 0)
|
||||
{
|
||||
ERROR("pthread_create: cannot create statistic thread: %s", strerror(errno));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
LOG("Antd scheduler initialized with %d worker", scheduler->n_workers);
|
||||
return 0;
|
||||
}
|
||||
@ -216,12 +345,12 @@ int antd_scheduler_init(antd_scheduler_t* scheduler, int n)
|
||||
destroy all pending task
|
||||
pthread_mutex_lock(&scheduler.queue_lock);
|
||||
*/
|
||||
void antd_scheduler_destroy(antd_scheduler_t* scheduler)
|
||||
void antd_scheduler_destroy(antd_scheduler_t *scheduler)
|
||||
{
|
||||
// free all the chains
|
||||
stop(scheduler);
|
||||
LOG("Destroy remaining queue");
|
||||
for(int i=0; i < N_PRIORITY; i++)
|
||||
for (int i = 0; i < N_PRIORITY; i++)
|
||||
{
|
||||
destroy_queue(scheduler->task_queue[i]);
|
||||
}
|
||||
@ -231,9 +360,9 @@ void antd_scheduler_destroy(antd_scheduler_t* scheduler)
|
||||
/*
|
||||
create a task
|
||||
*/
|
||||
antd_task_t* antd_create_task(void* (*handle)(void*), void *data, void* (*callback)(void*), time_t atime)
|
||||
antd_task_t *antd_create_task(void *(*handle)(void *), void *data, void *(*callback)(void *), time_t atime)
|
||||
{
|
||||
antd_task_t* task = (antd_task_t*)malloc(sizeof *task);
|
||||
antd_task_t *task = (antd_task_t *)malloc(sizeof *task);
|
||||
task->stamp = (unsigned long)time(NULL);
|
||||
task->data = data;
|
||||
task->handle = handle;
|
||||
@ -248,10 +377,10 @@ antd_task_t* antd_create_task(void* (*handle)(void*), void *data, void* (*callba
|
||||
/*
|
||||
scheduling a task
|
||||
*/
|
||||
void antd_add_task(antd_scheduler_t* scheduler, antd_task_t* task)
|
||||
void antd_add_task(antd_scheduler_t *scheduler, antd_task_t *task)
|
||||
{
|
||||
// check if task is exist
|
||||
int prio = task->priority>N_PRIORITY-1?N_PRIORITY-1:task->priority;
|
||||
int prio = task->priority > N_PRIORITY - 1 ? N_PRIORITY - 1 : task->priority;
|
||||
//LOG("Prio is %d\n", prio);
|
||||
pthread_mutex_lock(&scheduler->scheduler_lock);
|
||||
enqueue(&scheduler->task_queue[prio], task);
|
||||
@ -263,15 +392,14 @@ void antd_add_task(antd_scheduler_t* scheduler, antd_task_t* task)
|
||||
sem_post(scheduler->scheduler_sem);
|
||||
}
|
||||
|
||||
|
||||
void antd_execute_task(antd_scheduler_t* scheduler, antd_task_item_t taski)
|
||||
void antd_execute_task(antd_scheduler_t *scheduler, antd_task_item_t taski)
|
||||
{
|
||||
if(!taski)
|
||||
if (!taski)
|
||||
return;
|
||||
// execute the task
|
||||
void *ret = (*(taski->task->handle))(taski->task->data);
|
||||
// check the return data if it is a new task
|
||||
if(!ret)
|
||||
if (!ret)
|
||||
{
|
||||
// call the first callback
|
||||
execute_callback(scheduler, taski->task);
|
||||
@ -279,10 +407,10 @@ void antd_execute_task(antd_scheduler_t* scheduler, antd_task_item_t taski)
|
||||
}
|
||||
else
|
||||
{
|
||||
antd_task_t* rtask = (antd_task_t*) ret;
|
||||
if(taski->task->callback)
|
||||
{
|
||||
if(rtask->callback)
|
||||
antd_task_t *rtask = (antd_task_t *)ret;
|
||||
if (taski->task->callback)
|
||||
{
|
||||
if (rtask->callback)
|
||||
{
|
||||
enqueue_callback(rtask->callback, taski->task->callback);
|
||||
}
|
||||
@ -291,7 +419,7 @@ void antd_execute_task(antd_scheduler_t* scheduler, antd_task_item_t taski)
|
||||
rtask->callback = taski->task->callback;
|
||||
}
|
||||
}
|
||||
if(!rtask->handle)
|
||||
if (!rtask->handle)
|
||||
{
|
||||
// call the first callback
|
||||
execute_callback(scheduler, rtask);
|
||||
@ -301,7 +429,7 @@ void antd_execute_task(antd_scheduler_t* scheduler, antd_task_item_t taski)
|
||||
else
|
||||
{
|
||||
rtask->priority = taski->task->priority + 1;
|
||||
if(rtask->priority > N_PRIORITY - 1)
|
||||
if (rtask->priority > N_PRIORITY - 1)
|
||||
{
|
||||
rtask->priority = N_PRIORITY - 1;
|
||||
}
|
||||
@ -310,43 +438,43 @@ void antd_execute_task(antd_scheduler_t* scheduler, antd_task_item_t taski)
|
||||
free(taski);
|
||||
}
|
||||
}
|
||||
pthread_mutex_lock(&scheduler->pending_lock);
|
||||
scheduler->pending_task--;
|
||||
pthread_mutex_unlock(&scheduler->pending_lock);
|
||||
}
|
||||
|
||||
int antd_scheduler_busy(antd_scheduler_t* scheduler)
|
||||
int antd_scheduler_busy(antd_scheduler_t *scheduler)
|
||||
{
|
||||
return scheduler->pending_task != 0;
|
||||
}
|
||||
|
||||
int antd_task_schedule(antd_scheduler_t* scheduler)
|
||||
int antd_task_schedule(antd_scheduler_t *scheduler)
|
||||
{
|
||||
// fetch next task from the task_queue
|
||||
antd_task_item_t it = NULL;
|
||||
pthread_mutex_lock(&scheduler->scheduler_lock);
|
||||
for(int i = 0; i< N_PRIORITY; i++)
|
||||
for (int i = 0; i < N_PRIORITY; i++)
|
||||
{
|
||||
|
||||
|
||||
it = dequeue(&scheduler->task_queue[i]);
|
||||
if(it)
|
||||
if (it)
|
||||
break;
|
||||
}
|
||||
pthread_mutex_unlock(&scheduler->scheduler_lock);
|
||||
// no task
|
||||
if(!it)
|
||||
if (!it)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
pthread_mutex_lock(&scheduler->pending_lock);
|
||||
scheduler->pending_task--;
|
||||
pthread_mutex_unlock(&scheduler->pending_lock);
|
||||
// has the task now
|
||||
// validate the task
|
||||
if(scheduler->validate_data && difftime( time(NULL), it->task->access_time) > MAX_VALIDITY_INTERVAL && it->task->priority == N_PRIORITY - 1)
|
||||
if (scheduler->validate_data && difftime(time(NULL), it->task->access_time) > MAX_VALIDITY_INTERVAL && it->task->priority == N_PRIORITY - 1)
|
||||
{
|
||||
// data task is not valid
|
||||
// LOG("Task is no longer valid and will be killed");
|
||||
if(scheduler->destroy_data)
|
||||
LOG("Task is no longer valid and will be killed");
|
||||
if (scheduler->destroy_data)
|
||||
scheduler->destroy_data(it->task->data);
|
||||
if(it->task->callback)
|
||||
if (it->task->callback)
|
||||
free_callback(it->task->callback);
|
||||
free(it->task);
|
||||
free(it);
|
||||
@ -354,10 +482,10 @@ int antd_task_schedule(antd_scheduler_t* scheduler)
|
||||
}
|
||||
|
||||
// check the type of task
|
||||
if(it->task->type == LIGHT || scheduler->n_workers <= 0)
|
||||
if (it->task->type == LIGHT || scheduler->n_workers <= 0)
|
||||
{
|
||||
// do it by myself
|
||||
antd_execute_task( scheduler, it);
|
||||
antd_execute_task(scheduler, it);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -372,13 +500,13 @@ int antd_task_schedule(antd_scheduler_t* scheduler)
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
void antd_wait(antd_scheduler_t* scheduler)
|
||||
void antd_wait(antd_scheduler_t *scheduler)
|
||||
{
|
||||
int stat;
|
||||
while(scheduler->status)
|
||||
while (scheduler->status)
|
||||
{
|
||||
stat = antd_task_schedule(scheduler);
|
||||
if(!stat)
|
||||
if (!stat)
|
||||
{
|
||||
// no task found, go to idle state
|
||||
sem_wait(scheduler->scheduler_sem);
|
||||
|
@ -9,6 +9,7 @@
|
||||
#define LOW_PRIORITY (N_PRIORITY - 1)
|
||||
#define HIGH_PRIORITY 0
|
||||
#define MAX_VALIDITY_INTERVAL 20 // 10 s for task validity
|
||||
#define MAX_FIFO_NAME_SZ 255
|
||||
typedef enum
|
||||
{
|
||||
LIGHT,
|
||||
@ -92,6 +93,13 @@ typedef struct
|
||||
*/
|
||||
void* (*destroy_data)(void*);
|
||||
int validate_data;
|
||||
/**
|
||||
* statistic infomation
|
||||
*/
|
||||
char stat_fifo[MAX_FIFO_NAME_SZ];
|
||||
int stat_fd;
|
||||
pthread_t stat_tid;
|
||||
void (*stat_data_cb)(int, void *);
|
||||
} antd_scheduler_t;
|
||||
|
||||
/*
|
||||
|
2
lib/ws.c
2
lib/ws.c
@ -415,7 +415,7 @@ int ws_client_connect(ws_client_t* wsclient, port_config_t pcnf)
|
||||
}
|
||||
// will be free
|
||||
wsclient->antdsock->sock = sock;
|
||||
wsclient->antdsock->status = 0;
|
||||
wsclient->antdsock->z_status = 0;
|
||||
wsclient->antdsock->last_io = time(NULL);
|
||||
wsclient->antdsock->zstream = NULL;
|
||||
#ifdef USE_OPENSSL
|
||||
|
Reference in New Issue
Block a user