1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
/*
* Copyright (C) 2011 Red Hat, Inc.
*
* All rights reserved.
*
* Author: Angus Salkeld <asalkeld@redhat.com>
*
* libqb is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* libqb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libqb. If not, see <http://www.gnu.org/licenses/>.
*/
#include "os_base.h"
#include <pthread.h>
#include <semaphore.h>
#include <qb/qbdefs.h>
#include <qb/qblist.h>
#include <qb/qbutil.h>
#include "log_int.h"
static int wthread_active = QB_FALSE;
static int wthread_should_exit = QB_FALSE;
static qb_thread_lock_t *logt_wthread_lock = NULL;
static QB_LIST_DECLARE(logt_print_finished_records);
static int logt_memory_used = 0;
static int logt_dropped_messages = 0;
static sem_t logt_thread_start;
static sem_t logt_print_finished;
static int logt_sched_param_queued = QB_FALSE;
static int logt_sched_policy;
#if defined(HAVE_PTHREAD_SETSCHEDPARAM)
static struct sched_param logt_sched_param;
#endif /* HAVE_PTHREAD_SETSCHEDPARAM */
static pthread_t logt_thread_id = 0;
static void *qb_logt_worker_thread(void *data) __attribute__ ((noreturn));
static void *
qb_logt_worker_thread(void *data)
{
struct qb_log_record *rec;
int dropped = 0;
int res;
/* Signal qb_log_thread_start that the initialization may continue */
sem_post(&logt_thread_start);
for (;;) {
retry_sem_wait:
res = sem_wait(&logt_print_finished);
if (res == -1 && errno == EINTR) {
goto retry_sem_wait;
} else if (res == -1) {
/* This case shouldn't happen */
pthread_exit(NULL);
}
(void)qb_thread_lock(logt_wthread_lock);
if (wthread_should_exit) {
int value = -1;
(void)sem_getvalue(&logt_print_finished, &value);
if (value == 0) {
(void)qb_thread_unlock(logt_wthread_lock);
pthread_exit(NULL);
}
}
rec =
qb_list_first_entry(&logt_print_finished_records,
struct qb_log_record, list);
qb_list_del(&rec->list);
logt_memory_used = logt_memory_used - strlen(rec->buffer) -
sizeof(struct qb_log_record) - 1;
dropped = logt_dropped_messages;
logt_dropped_messages = 0;
if (dropped) {
printf("%d messages lost\n", dropped);
}
qb_log_thread_log_write(rec->cs, &rec->timestamp, rec->buffer);
(void)qb_thread_unlock(logt_wthread_lock);
free(rec->buffer);
free(rec);
}
}
int32_t
qb_log_thread_priority_set(int32_t policy, int32_t priority)
{
int res = 0;
#if defined(HAVE_PTHREAD_SETSCHEDPARAM)
logt_sched_policy = policy;
if (policy == SCHED_OTHER
#ifdef SCHED_IDLE
|| policy == SCHED_IDLE
#endif
#if defined(SCHED_BATCH) && !defined(QB_DARWIN)
|| policy == SCHED_BATCH
#endif
) {
logt_sched_param.sched_priority = 0;
} else {
logt_sched_param.sched_priority = priority;
}
if (wthread_active == QB_FALSE) {
logt_sched_param_queued = QB_TRUE;
} else {
res = pthread_setschedparam(logt_thread_id, policy,
&logt_sched_param);
if (res != 0) {
res = -res;
}
}
#endif
return res;
}
int32_t
qb_log_thread_start(void)
{
int res;
qb_thread_lock_t *wthread_lock;
if (wthread_active) {
return 0;
}
wthread_active = QB_TRUE;
sem_init(&logt_thread_start, 0, 0);
sem_init(&logt_print_finished, 0, 0);
errno = 0;
logt_wthread_lock = qb_thread_lock_create(QB_THREAD_LOCK_SHORT);
if (logt_wthread_lock == NULL) {
return errno ? -errno : -1;
}
res = pthread_create(&logt_thread_id, NULL,
qb_logt_worker_thread, NULL);
if (res != 0) {
wthread_active = QB_FALSE;
(void)qb_thread_lock_destroy(logt_wthread_lock);
return -res;
}
sem_wait(&logt_thread_start);
if (logt_sched_param_queued) {
res = qb_log_thread_priority_set(logt_sched_policy,
#if defined(HAVE_PTHREAD_SETSCHEDPARAM)
logt_sched_param.sched_priority);
#else
0);
#endif
if (res != 0) {
goto cleanup_pthread;
}
logt_sched_param_queued = QB_FALSE;
}
return 0;
cleanup_pthread:
wthread_should_exit = QB_TRUE;
sem_post(&logt_print_finished);
pthread_join(logt_thread_id, NULL);
wthread_active = QB_FALSE;
wthread_lock = logt_wthread_lock;
logt_wthread_lock = NULL;
(void)qb_thread_lock_destroy(wthread_lock);
sem_destroy(&logt_print_finished);
sem_destroy(&logt_thread_start);
return res;
}
void
qb_log_thread_pause(struct qb_log_target *t)
{
if (t->threaded) {
(void)qb_thread_lock(logt_wthread_lock);
}
}
void
qb_log_thread_resume(struct qb_log_target *t)
{
if (t->threaded) {
(void)qb_thread_unlock(logt_wthread_lock);
}
}
void
qb_log_thread_log_post(struct qb_log_callsite *cs,
struct timespec *timestamp, const char *buffer)
{
struct qb_log_record *rec;
size_t buf_size;
size_t total_size;
rec = malloc(sizeof(struct qb_log_record));
if (rec == NULL) {
return;
}
buf_size = strlen(buffer) + 1;
total_size = sizeof(struct qb_log_record) + buf_size;
rec->cs = cs;
rec->buffer = malloc(buf_size);
if (rec->buffer == NULL) {
goto free_record;
}
memcpy(rec->buffer, buffer, buf_size);
memcpy(&rec->timestamp, timestamp, sizeof(struct timespec));
qb_list_init(&rec->list);
(void)qb_thread_lock(logt_wthread_lock);
logt_memory_used += total_size;
if (logt_memory_used > 512000) {
free(rec->buffer);
free(rec);
logt_memory_used = logt_memory_used - total_size;
logt_dropped_messages += 1;
(void)qb_thread_unlock(logt_wthread_lock);
return;
} else {
qb_list_add_tail(&rec->list, &logt_print_finished_records);
}
(void)qb_thread_unlock(logt_wthread_lock);
sem_post(&logt_print_finished);
return;
free_record:
free(rec);
}
void
qb_log_thread_stop(void)
{
int res;
int value;
struct qb_log_record *rec;
if (wthread_active == QB_FALSE && logt_wthread_lock == NULL) {
return;
}
if (wthread_active == QB_FALSE) {
for (;;) {
res = sem_getvalue(&logt_print_finished, &value);
if (res != 0 || value == 0) {
break;
}
sem_wait(&logt_print_finished);
(void)qb_thread_lock(logt_wthread_lock);
rec = qb_list_first_entry(&logt_print_finished_records,
struct qb_log_record, list);
qb_list_del(&rec->list);
logt_memory_used = logt_memory_used -
strlen(rec->buffer) -
sizeof(struct qb_log_record) - 1;
qb_log_thread_log_write(rec->cs, &rec->timestamp,
rec->buffer);
(void)qb_thread_unlock(logt_wthread_lock);
free(rec->buffer);
free(rec);
}
} else {
(void)qb_thread_lock(logt_wthread_lock);
wthread_should_exit = QB_TRUE;
(void)qb_thread_unlock(logt_wthread_lock);
sem_post(&logt_print_finished);
pthread_join(logt_thread_id, NULL);
}
(void)qb_thread_lock_destroy(logt_wthread_lock);
sem_destroy(&logt_print_finished);
sem_destroy(&logt_thread_start);
}
|