mirror of
https://github.com/lxsang/ant-http
synced 2024-12-25 16:28:21 +01:00
fix scheduler statistic take too much cpu at idle
This commit is contained in:
parent
b35cd61da4
commit
69ad08b1a1
BIN
dist/antd-1.0.5b.tar.gz
vendored
BIN
dist/antd-1.0.5b.tar.gz
vendored
Binary file not shown.
164
lib/scheduler.c
164
lib/scheduler.c
@ -4,7 +4,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <poll.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "scheduler.h"
|
#include "scheduler.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
@ -13,12 +12,13 @@ static void set_nonblock(int fd)
|
|||||||
{
|
{
|
||||||
int flags;
|
int flags;
|
||||||
flags = fcntl(fd, F_GETFL, 0);
|
flags = fcntl(fd, F_GETFL, 0);
|
||||||
if(flags == -1)
|
if (flags == -1)
|
||||||
{
|
{
|
||||||
ERROR("Unable to set flag");
|
ERROR("Unable to set flag");
|
||||||
}
|
}
|
||||||
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void enqueue(antd_task_queue_t *q, antd_task_t *task)
|
static void enqueue(antd_task_queue_t *q, antd_task_t *task)
|
||||||
{
|
{
|
||||||
antd_task_item_t it = *q;
|
antd_task_item_t it = *q;
|
||||||
@ -50,7 +50,7 @@ static void stop(antd_scheduler_t *scheduler)
|
|||||||
pthread_join(scheduler->workers[i].tid, NULL);
|
pthread_join(scheduler->workers[i].tid, NULL);
|
||||||
if (scheduler->workers)
|
if (scheduler->workers)
|
||||||
free(scheduler->workers);
|
free(scheduler->workers);
|
||||||
(void)pthread_join(scheduler->stat_tid, NULL);
|
(void)pthread_cancel(scheduler->stat_tid);
|
||||||
// destroy all the mutex
|
// destroy all the mutex
|
||||||
pthread_mutex_destroy(&scheduler->scheduler_lock);
|
pthread_mutex_destroy(&scheduler->scheduler_lock);
|
||||||
pthread_mutex_destroy(&scheduler->worker_lock);
|
pthread_mutex_destroy(&scheduler->worker_lock);
|
||||||
@ -173,7 +173,7 @@ static void *work(antd_worker_t *worker)
|
|||||||
|
|
||||||
static void *statistic(antd_scheduler_t *scheduler)
|
static void *statistic(antd_scheduler_t *scheduler)
|
||||||
{
|
{
|
||||||
struct pollfd fdp;
|
fd_set fd_out;
|
||||||
int ret;
|
int ret;
|
||||||
char buffer[MAX_FIFO_NAME_SZ];
|
char buffer[MAX_FIFO_NAME_SZ];
|
||||||
antd_task_item_t it;
|
antd_task_item_t it;
|
||||||
@ -181,72 +181,118 @@ static void *statistic(antd_scheduler_t *scheduler)
|
|||||||
{
|
{
|
||||||
if (scheduler->stat_fd == -1)
|
if (scheduler->stat_fd == -1)
|
||||||
{
|
{
|
||||||
scheduler->stat_fd = open(scheduler->stat_fifo, O_RDWR);
|
scheduler->stat_fd = open(scheduler->stat_fifo, O_WRONLY);
|
||||||
if (scheduler->stat_fd == -1)
|
if (scheduler->stat_fd == -1)
|
||||||
{
|
{
|
||||||
ERROR("Unable to open FIFO %s: %s", scheduler->stat_fifo, strerror(errno));
|
ERROR("Unable to open FIFO %s: %s", scheduler->stat_fifo, strerror(errno));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
else
|
||||||
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);
|
set_nonblock(scheduler->stat_fd);
|
||||||
ret = write(scheduler->stat_fd, buffer, strlen(buffer));
|
}
|
||||||
|
}
|
||||||
|
FD_ZERO(&fd_out);
|
||||||
|
FD_SET(scheduler->stat_fd, &fd_out);
|
||||||
|
ret = select(scheduler->stat_fd + 1, NULL, &fd_out, NULL, NULL);
|
||||||
|
switch (ret)
|
||||||
|
{
|
||||||
|
case -1:
|
||||||
|
ERROR("Error on select(): %s\n", strerror(errno));
|
||||||
|
close(scheduler->stat_fd);
|
||||||
|
return NULL;
|
||||||
|
|
||||||
it = scheduler->task_queue[i];
|
case 0:
|
||||||
while (it)
|
break;
|
||||||
|
// we have data
|
||||||
|
default:
|
||||||
|
if (FD_ISSET(scheduler->stat_fd, &fd_out))
|
||||||
|
{
|
||||||
|
if (scheduler->pending_task > 0)
|
||||||
{
|
{
|
||||||
// send statistic on task data
|
pthread_mutex_lock(&scheduler->scheduler_lock);
|
||||||
snprintf(buffer, MAX_FIFO_NAME_SZ, "---- Task created at: %lu ----\n", it->task->stamp);
|
// 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));
|
ret = write(scheduler->stat_fd, buffer, strlen(buffer));
|
||||||
|
|
||||||
// send statistic on task data
|
for (int i = 0; i < N_PRIORITY; i++)
|
||||||
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");
|
snprintf(buffer, MAX_FIFO_NAME_SZ, "#### PRIORITY: %d\n", i);
|
||||||
ret = write(scheduler->stat_fd, buffer, strlen(buffer));
|
ret = write(scheduler->stat_fd, buffer, strlen(buffer));
|
||||||
}
|
|
||||||
|
|
||||||
if (it->task->callback)
|
it = scheduler->task_queue[i];
|
||||||
{
|
while (it)
|
||||||
snprintf(buffer, MAX_FIFO_NAME_SZ, "Has callback: yes\n");
|
{
|
||||||
ret = write(scheduler->stat_fd, buffer, strlen(buffer));
|
// 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));
|
||||||
|
|
||||||
// now print all task data statistic
|
// send statistic on task data
|
||||||
if (scheduler->stat_data_cb)
|
snprintf(buffer, MAX_FIFO_NAME_SZ, "Access time: %lu\nn", (unsigned long)it->task->access_time);
|
||||||
{
|
ret = write(scheduler->stat_fd, buffer, strlen(buffer));
|
||||||
scheduler->stat_data_cb(scheduler->stat_fd, it->task->data);
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ret = write(scheduler->stat_fd, ".", 1);
|
||||||
|
if(ret == -1)
|
||||||
|
{
|
||||||
|
ret = close(scheduler->stat_fd);
|
||||||
|
scheduler->stat_fd = -1;
|
||||||
|
usleep(5000);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ret = write(scheduler->stat_fd, "\b", 1);
|
||||||
}
|
}
|
||||||
it = it->next;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&scheduler->scheduler_lock);
|
else
|
||||||
ret = close(scheduler->stat_fd);
|
{
|
||||||
scheduler->stat_fd = -1;
|
ret = close(scheduler->stat_fd);
|
||||||
usleep(5000);
|
scheduler->stat_fd = -1;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
/* else
|
||||||
|
{
|
||||||
|
ret = write(scheduler->stat_fd, ".", 1);
|
||||||
|
if(ret == -1)
|
||||||
|
{
|
||||||
|
ret = close(scheduler->stat_fd);
|
||||||
|
scheduler->stat_fd = -1;
|
||||||
|
}
|
||||||
|
} */
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -322,19 +368,9 @@ int antd_scheduler_init(antd_scheduler_t *scheduler, int n)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// open the fifo in write mode
|
if (pthread_create(&scheduler->stat_tid, NULL, (void *(*)(void *))statistic, scheduler) != 0)
|
||||||
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));
|
ERROR("pthread_create: cannot create statistic thread: %s", 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));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user