1
0
mirror of https://github.com/lxsang/ant-http synced 2024-07-05 22:19:47 +02:00
ant-http/lib/scheduler.c

516 lines
15 KiB
C
Raw Normal View History

2020-08-25 16:40:24 +02:00
#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>
2018-09-25 17:51:56 +02:00
#include "scheduler.h"
2020-08-25 16:40:24 +02:00
#include "utils.h"
2018-09-25 17:51:56 +02:00
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)
2018-09-25 17:51:56 +02:00
{
2018-09-27 15:00:19 +02:00
antd_task_item_t it = *q;
while (it && it->next != NULL)
2018-09-25 17:51:56 +02:00
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
2018-09-25 17:51:56 +02:00
{
2018-09-27 15:00:19 +02:00
*q = taski;
2018-09-25 17:51:56 +02:00
}
else
{
it->next = taski;
}
}
static void stop(antd_scheduler_t *scheduler)
2018-09-25 17:51:56 +02:00
{
2018-10-01 22:49:20 +02:00
scheduler->status = 0;
2018-10-10 12:42:47 +02:00
// unlock all idle workers if any
2018-10-01 22:49:20 +02:00
for (int i = 0; i < scheduler->n_workers; i++)
2018-10-14 10:57:09 +02:00
sem_post(scheduler->worker_sem);
if (scheduler->scheduler_sem)
2019-12-20 16:49:41 +01:00
sem_post(scheduler->scheduler_sem);
2018-10-10 12:42:47 +02:00
for (int i = 0; i < scheduler->n_workers; i++)
if (scheduler->workers[i].id != -1)
2018-10-10 12:42:47 +02:00
pthread_join(scheduler->workers[i].tid, NULL);
if (scheduler->workers)
free(scheduler->workers);
(void)pthread_join(scheduler->stat_tid, NULL);
2018-09-27 15:00:19 +02:00
// destroy all the mutex
2018-10-01 22:49:20 +02:00
pthread_mutex_destroy(&scheduler->scheduler_lock);
pthread_mutex_destroy(&scheduler->worker_lock);
pthread_mutex_destroy(&scheduler->pending_lock);
2018-10-14 11:25:13 +02:00
sem_unlink("scheduler");
sem_unlink("worker");
2018-10-14 10:57:09 +02:00
sem_close(scheduler->scheduler_sem);
sem_close(scheduler->worker_sem);
2018-09-25 17:51:56 +02:00
}
static antd_task_item_t dequeue(antd_task_queue_t *q)
2018-09-25 17:51:56 +02:00
{
2018-09-27 15:00:19 +02:00
antd_task_item_t it = *q;
if (it)
2018-09-25 17:51:56 +02:00
{
2018-09-27 15:00:19 +02:00
*q = it->next;
it->next = NULL;
2018-09-25 17:51:56 +02:00
}
return it;
}
antd_callback_t *callback_of(void *(*callback)(void *))
2018-09-25 17:51:56 +02:00
{
antd_callback_t *cb = NULL;
if (callback)
2018-09-25 17:51:56 +02:00
{
cb = (antd_callback_t *)malloc(sizeof *cb);
2018-09-25 17:51:56 +02:00
cb->handle = callback;
cb->next = NULL;
}
return cb;
}
static void free_callback(antd_callback_t *cb)
2018-09-25 17:51:56 +02:00
{
antd_callback_t *it = cb;
antd_callback_t *curr;
while (it)
2018-09-25 17:51:56 +02:00
{
curr = it;
it = it->next;
free(curr);
}
}
static void enqueue_callback(antd_callback_t *cb, antd_callback_t *el)
2018-09-25 17:51:56 +02:00
{
antd_callback_t *it = cb;
while (it && it->next != NULL)
2018-09-25 17:51:56 +02:00
it = it->next;
if (!it)
return; // this should not happend
2018-09-25 17:51:56 +02:00
it->next = el;
}
static void execute_callback(antd_scheduler_t *scheduler, antd_task_t *task)
2018-09-25 17:51:56 +02:00
{
antd_callback_t *cb = task->callback;
if (cb)
2018-09-25 17:51:56 +02:00
{
// call the first come call back
task->handle = cb->handle;
task->callback = task->callback->next;
2020-08-25 16:40:24 +02:00
task->priority = task->priority + 1;
if (task->priority > N_PRIORITY - 1)
2020-08-25 16:40:24 +02:00
{
task->priority = N_PRIORITY - 1;
}
2018-09-25 17:51:56 +02:00
free(cb);
2018-10-01 22:49:20 +02:00
antd_add_task(scheduler, task);
2018-09-25 17:51:56 +02:00
}
else
{
free(task);
}
}
2018-09-27 15:00:19 +02:00
static void destroy_queue(antd_task_queue_t q)
{
antd_task_item_t it, curr;
it = q;
while (it)
2018-09-27 15:00:19 +02:00
{
// first free the task
if (it->task && it->task->callback)
free_callback(it->task->callback);
if (it->task)
free(it->task);
2018-09-27 15:00:19 +02:00
// then free the placeholder
curr = it;
it = it->next;
free(curr);
}
}
static void *work(antd_worker_t *worker)
2018-09-27 17:18:31 +02:00
{
antd_scheduler_t *scheduler = (antd_scheduler_t *)worker->manager;
while (scheduler->status)
2018-09-27 17:18:31 +02:00
{
2018-10-01 22:49:20 +02:00
antd_task_item_t it;
pthread_mutex_lock(&scheduler->worker_lock);
it = dequeue(&scheduler->workers_queue);
pthread_mutex_unlock(&scheduler->worker_lock);
// execute the task
//LOG("task executed by worker %d\n", worker->pid);
2018-10-10 12:42:47 +02:00
// no task to execute, just sleep wait
if (!it)
2018-10-09 17:24:00 +02:00
{
2018-10-10 12:42:47 +02:00
//LOG("Worker %d goes to idle state\n", worker->id);
2018-10-14 10:57:09 +02:00
sem_wait(scheduler->worker_sem);
2018-10-09 17:24:00 +02:00
}
2018-10-10 12:42:47 +02:00
else
{
//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);
}
2018-09-27 17:18:31 +02:00
}
2020-08-19 12:26:17 +02:00
return NULL;
2018-09-27 15:00:19 +02:00
}
2018-10-01 22:49:20 +02:00
2018-09-27 17:18:31 +02:00
/*
Main API methods
init the main scheduler
*/
2018-09-26 10:30:04 +02:00
int antd_scheduler_init(antd_scheduler_t *scheduler, int n)
2018-09-25 17:51:56 +02:00
{
2018-10-01 22:49:20 +02:00
scheduler->n_workers = n;
scheduler->status = 1;
scheduler->workers_queue = NULL;
scheduler->pending_task = 0;
2019-07-31 15:11:59 +02:00
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);
2018-10-10 12:42:47 +02:00
// init semaphore
2018-10-14 11:19:06 +02:00
scheduler->scheduler_sem = sem_open("scheduler", O_CREAT, 0600, 0);
if (scheduler->scheduler_sem == SEM_FAILED)
2018-10-10 12:42:47 +02:00
{
2019-12-11 23:17:42 +01:00
ERROR("Cannot open semaphore for scheduler");
2020-08-25 16:40:24 +02:00
return -1;
2018-10-10 12:42:47 +02:00
}
2018-10-14 11:19:06 +02:00
scheduler->worker_sem = sem_open("worker", O_CREAT, 0600, 0);
2018-10-14 10:57:09 +02:00
if (!scheduler->worker_sem)
2018-10-10 12:42:47 +02:00
{
2019-12-11 23:17:42 +01:00
ERROR("Cannot open semaphore for workers");
2020-08-25 16:40:24 +02:00
return -1;
2018-10-10 12:42:47 +02:00
}
// init lock
pthread_mutex_init(&scheduler->scheduler_lock, NULL);
2018-10-01 22:49:20 +02:00
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;
2018-09-25 17:51:56 +02:00
// create scheduler.workers
if (n > 0)
2018-09-25 17:51:56 +02:00
{
scheduler->workers = (antd_worker_t *)malloc(n * (sizeof(antd_worker_t)));
if (!scheduler->workers)
2018-09-25 17:51:56 +02:00
{
2019-12-11 23:17:42 +01:00
ERROR("Cannot allocate memory for worker");
2020-08-25 16:40:24 +02:00
return -1;
2018-09-26 10:30:04 +02:00
}
for (int i = 0; i < scheduler->n_workers; i++)
2018-09-26 10:30:04 +02:00
{
2018-10-10 12:42:47 +02:00
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)
2018-09-26 10:30:04 +02:00
{
2020-08-25 16:40:24 +02:00
ERROR("pthread_create: cannot create worker: %s", strerror(errno));
return -1;
2018-09-26 10:30:04 +02:00
}
2018-10-10 12:42:47 +02:00
else
{
scheduler->workers[i].id = i;
}
2018-09-25 17:51:56 +02:00
}
}
// 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));
}
}
}
}
2019-12-11 23:17:42 +01:00
LOG("Antd scheduler initialized with %d worker", scheduler->n_workers);
2020-08-25 16:40:24 +02:00
return 0;
2018-09-25 17:51:56 +02:00
}
/*
destroy all pending task
2018-09-27 15:00:19 +02:00
pthread_mutex_lock(&scheduler.queue_lock);
2018-09-25 17:51:56 +02:00
*/
void antd_scheduler_destroy(antd_scheduler_t *scheduler)
2018-09-25 17:51:56 +02:00
{
// free all the chains
2018-10-01 22:49:20 +02:00
stop(scheduler);
2019-12-11 23:17:42 +01:00
LOG("Destroy remaining queue");
for (int i = 0; i < N_PRIORITY; i++)
2018-09-25 17:51:56 +02:00
{
2018-10-01 22:49:20 +02:00
destroy_queue(scheduler->task_queue[i]);
2018-09-25 17:51:56 +02:00
}
2018-10-01 22:49:20 +02:00
destroy_queue(scheduler->workers_queue);
2018-09-25 17:51:56 +02:00
}
/*
create a task
*/
antd_task_t *antd_create_task(void *(*handle)(void *), void *data, void *(*callback)(void *), time_t atime)
2018-09-25 17:51:56 +02:00
{
antd_task_t *task = (antd_task_t *)malloc(sizeof *task);
2018-09-25 17:51:56 +02:00
task->stamp = (unsigned long)time(NULL);
task->data = data;
task->handle = handle;
task->callback = callback_of(callback);
2020-08-25 16:40:24 +02:00
task->priority = HIGH_PRIORITY;
2019-12-19 09:52:56 +01:00
task->type = HEAVY;
//task->type = LIGHT;
2019-07-31 15:11:59 +02:00
task->access_time = atime;
2018-09-25 17:51:56 +02:00
return task;
}
/*
scheduling a task
*/
void antd_add_task(antd_scheduler_t *scheduler, antd_task_t *task)
2018-09-25 17:51:56 +02:00
{
2018-09-27 15:00:19 +02:00
// check if task is exist
int prio = task->priority > N_PRIORITY - 1 ? N_PRIORITY - 1 : task->priority;
2018-10-07 01:03:05 +02:00
//LOG("Prio is %d\n", prio);
2018-10-01 22:49:20 +02:00
pthread_mutex_lock(&scheduler->scheduler_lock);
enqueue(&scheduler->task_queue[prio], task);
pthread_mutex_unlock(&scheduler->scheduler_lock);
pthread_mutex_lock(&scheduler->pending_lock);
scheduler->pending_task++;
pthread_mutex_unlock(&scheduler->pending_lock);
2018-10-10 12:42:47 +02:00
// wake up the scheduler if idle
2018-10-14 10:57:09 +02:00
sem_post(scheduler->scheduler_sem);
2018-09-25 17:51:56 +02:00
}
void antd_execute_task(antd_scheduler_t *scheduler, antd_task_item_t taski)
2018-09-25 17:51:56 +02:00
{
if (!taski)
2018-10-01 22:49:20 +02:00
return;
2018-09-25 17:51:56 +02:00
// execute the task
void *ret = (*(taski->task->handle))(taski->task->data);
// check the return data if it is a new task
if (!ret)
2018-09-25 17:51:56 +02:00
{
// call the first callback
2018-10-01 22:49:20 +02:00
execute_callback(scheduler, taski->task);
2018-09-25 17:51:56 +02:00
free(taski);
}
else
{
antd_task_t *rtask = (antd_task_t *)ret;
if (taski->task->callback)
{
if (rtask->callback)
2018-09-25 17:51:56 +02:00
{
enqueue_callback(rtask->callback, taski->task->callback);
}
else
{
rtask->callback = taski->task->callback;
}
}
if (!rtask->handle)
2018-09-25 17:51:56 +02:00
{
// call the first callback
2018-10-01 22:49:20 +02:00
execute_callback(scheduler, rtask);
2018-09-25 17:51:56 +02:00
free(taski->task);
free(taski);
}
else
{
2020-08-25 16:40:24 +02:00
rtask->priority = taski->task->priority + 1;
if (rtask->priority > N_PRIORITY - 1)
2020-08-25 16:40:24 +02:00
{
rtask->priority = N_PRIORITY - 1;
}
2018-10-01 22:49:20 +02:00
antd_add_task(scheduler, rtask);
2018-09-25 17:51:56 +02:00
free(taski->task);
free(taski);
}
}
}
2018-09-27 17:18:31 +02:00
int antd_scheduler_busy(antd_scheduler_t *scheduler)
2018-09-25 17:51:56 +02:00
{
2018-10-01 22:49:20 +02:00
return scheduler->pending_task != 0;
2018-09-25 17:51:56 +02:00
}
2018-10-01 22:49:20 +02:00
int antd_task_schedule(antd_scheduler_t *scheduler)
2018-09-27 15:00:19 +02:00
{
// fetch next task from the task_queue
antd_task_item_t it = NULL;
2018-10-01 22:49:20 +02:00
pthread_mutex_lock(&scheduler->scheduler_lock);
for (int i = 0; i < N_PRIORITY; i++)
2018-09-27 15:00:19 +02:00
{
2018-10-01 22:49:20 +02:00
it = dequeue(&scheduler->task_queue[i]);
if (it)
2018-09-27 15:00:19 +02:00
break;
}
2018-10-01 22:49:20 +02:00
pthread_mutex_unlock(&scheduler->scheduler_lock);
2019-07-31 15:11:59 +02:00
// no task
if (!it)
2018-09-27 15:00:19 +02:00
{
2018-10-09 17:24:00 +02:00
return 0;
2018-09-27 15:00:19 +02:00
}
pthread_mutex_lock(&scheduler->pending_lock);
scheduler->pending_task--;
pthread_mutex_unlock(&scheduler->pending_lock);
2018-09-27 15:00:19 +02:00
// has the task now
2019-07-31 15:11:59 +02:00
// validate the task
if (scheduler->validate_data && difftime(time(NULL), it->task->access_time) > MAX_VALIDITY_INTERVAL && it->task->priority == N_PRIORITY - 1)
2019-07-31 15:11:59 +02:00
{
// data task is not valid
LOG("Task is no longer valid and will be killed");
if (scheduler->destroy_data)
2019-07-31 15:11:59 +02:00
scheduler->destroy_data(it->task->data);
if (it->task->callback)
2019-07-31 15:11:59 +02:00
free_callback(it->task->callback);
free(it->task);
free(it);
return 0;
}
2018-10-10 12:42:47 +02:00
// check the type of task
if (it->task->type == LIGHT || scheduler->n_workers <= 0)
2018-09-27 15:00:19 +02:00
{
// do it by myself
antd_execute_task(scheduler, it);
2018-09-27 15:00:19 +02:00
}
else
{
// delegate to other workers by
//pushing to the worker queue
2018-10-01 22:49:20 +02:00
pthread_mutex_lock(&scheduler->worker_lock);
enqueue(&scheduler->workers_queue, it->task);
pthread_mutex_unlock(&scheduler->worker_lock);
2018-10-10 12:42:47 +02:00
// wake up idle worker
2018-10-14 10:57:09 +02:00
sem_post(scheduler->worker_sem);
2018-09-27 15:00:19 +02:00
free(it);
}
2018-10-09 17:24:00 +02:00
return 1;
2018-10-10 12:42:47 +02:00
}
void antd_wait(antd_scheduler_t *scheduler)
2018-10-10 12:42:47 +02:00
{
int stat;
while (scheduler->status)
2018-10-10 12:42:47 +02:00
{
stat = antd_task_schedule(scheduler);
if (!stat)
2018-10-10 12:42:47 +02:00
{
// no task found, go to idle state
2018-10-14 10:57:09 +02:00
sem_wait(scheduler->scheduler_sem);
2018-10-10 12:42:47 +02:00
}
}
}