fix scheduler statistic take too much cpu at idle

This commit is contained in:
lxsang 2020-09-13 02:46:24 +02:00
parent b35cd61da4
commit 69ad08b1a1
2 changed files with 100 additions and 64 deletions

Binary file not shown.

View File

@ -4,7 +4,6 @@
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <poll.h>
#include <unistd.h>
#include "scheduler.h"
#include "utils.h"
@ -13,12 +12,13 @@ static void set_nonblock(int fd)
{
int flags;
flags = fcntl(fd, F_GETFL, 0);
if(flags == -1)
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;
@ -50,7 +50,7 @@ static void stop(antd_scheduler_t *scheduler)
pthread_join(scheduler->workers[i].tid, NULL);
if (scheduler->workers)
free(scheduler->workers);
(void)pthread_join(scheduler->stat_tid, NULL);
(void)pthread_cancel(scheduler->stat_tid);
// destroy all the mutex
pthread_mutex_destroy(&scheduler->scheduler_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)
{
struct pollfd fdp;
fd_set fd_out;
int ret;
char buffer[MAX_FIFO_NAME_SZ];
antd_task_item_t it;
@ -181,19 +181,34 @@ static void *statistic(antd_scheduler_t *scheduler)
{
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)
{
ERROR("Unable to open FIFO %s: %s", scheduler->stat_fifo, strerror(errno));
return NULL;
}
else
{
set_nonblock(scheduler->stat_fd);
}
fdp.fd = scheduler->stat_fd;
fdp.events = POLLOUT;
// poll the fd in blocking mode
ret = poll(&fdp, 1, -1);
}
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;
if (ret > 0 && (fdp.revents & POLLOUT) && scheduler->pending_task > 0)
case 0:
break;
// we have data
default:
if (FD_ISSET(scheduler->stat_fd, &fd_out))
{
if (scheduler->pending_task > 0)
{
pthread_mutex_lock(&scheduler->scheduler_lock);
// write statistic data
@ -247,6 +262,37 @@ static void *statistic(antd_scheduler_t *scheduler)
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);
}
}
}
else
{
ret = close(scheduler->stat_fd);
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;
}
@ -322,22 +368,12 @@ int antd_scheduler_init(antd_scheduler_t *scheduler, int n)
}
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;
}