-
Notifications
You must be signed in to change notification settings - Fork 55
/
cmdlog.c
444 lines (395 loc) · 15 KB
/
cmdlog.c
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* arcus-memcached - Arcus memory cache server
* Copyright 2015 JaM2in Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdint.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <memcached/util.h>
#include "cmdlog.h"
#define CMDLOG_INPUT_SIZE 400
#define CMDLOG_BUFFER_SIZE (10 * 1024 * 1024) /* 10MB */
#define CMDLOG_WRITE_SIZE (4 * 1024) /* 4KB */
#define CMDLOG_FILE_MAXSIZE (10 * 1024 * 1024) /* 10MB : log at most CMDLOG_INPUT_SIZE * N commands in one file */
#define CMDLOG_FILE_MAXNUM 10 /* # of cmdlog files */
#define CMDLOG_DIRPATH_LENGTH 128 /* directory path's length */
#define CMDLOG_FILENAME_LENGTH CMDLOG_DIRPATH_LENGTH + 128
#define CMDLOG_FILENAME_FORMAT "%s/command_%d_%d_%d_%d.log"
/* cmdlog state */
#define CMDLOG_NOT_STARTED 0 /* not started */
#define CMDLOG_OVERFLOW_STOP 1 /* stop by command log overflow */
#define CMDLOG_FLUSHERR_STOP 2 /* stop by flush operation error */
#define CMDLOG_RUNNING 3 /* running */
bool cmdlog_in_use = false; /* true or false : logging start condition */
static int mc_port;
static EXTENSION_LOGGER_DESCRIPTOR *mc_logger;
/* command log buffer structure */
struct cmd_log_buffer {
pthread_mutex_t lock;
char *data;
uint32_t size;
uint32_t head;
uint32_t tail;
uint32_t last;
};
/*command log flush structure */
struct cmd_log_flush {
pthread_t tid; /* flush thread id */
pthread_attr_t attr; /* flush thread mode */
pthread_mutex_t lock; /* flush thread sleep and wakeup */
pthread_cond_t cond; /* flush thread sleep and wakeup */
bool sleep;
};
/* command log stats structure */
struct cmd_log_stats {
int bgndate, bgntime;
int enddate, endtime;
int file_count;
volatile int state; /* command log module state */
uint32_t entered_commands; /* number of entered command */
uint32_t skipped_commands; /* number of skipped command */
char dirpath[CMDLOG_DIRPATH_LENGTH];
};
/* command log global structure */
struct cmd_log_global {
pthread_mutex_t lock;
struct cmd_log_buffer buffer;
struct cmd_log_flush flush;
struct cmd_log_stats stats;
volatile bool reqstop;
};
struct cmd_log_global cmdlog;
static void do_cmdlog_flush_sleep(void)
{
struct timeval tv;
struct timespec to;
/* 50 milli seconds sleep */
pthread_mutex_lock(&cmdlog.flush.lock);
gettimeofday(&tv, NULL);
tv.tv_usec += 50000;
if (tv.tv_usec >= 1000000) {
tv.tv_sec += 1;
tv.tv_usec -= 1000000;
}
to.tv_sec = tv.tv_sec;
to.tv_nsec = tv.tv_usec * 1000;
cmdlog.flush.sleep = true;
pthread_cond_timedwait(&cmdlog.flush.cond, &cmdlog.flush.lock, &to);
cmdlog.flush.sleep = false;
pthread_mutex_unlock(&cmdlog.flush.lock);
}
static void do_cmdlog_flush_wakeup(void)
{
/* wake up flush thread */
pthread_mutex_lock(&cmdlog.flush.lock);
if (cmdlog.flush.sleep == true) {
pthread_cond_signal(&cmdlog.flush.cond);
}
pthread_mutex_unlock(&cmdlog.flush.lock);
}
static void do_cmdlog_stop(void)
{
/* cmdlog lock has already been held */
cmdlog.stats.enddate = getnowdate_int();
cmdlog.stats.endtime = getnowtime_int();
cmdlog_in_use = false;
}
static void *cmdlog_flush_thread(void *arg)
{
struct cmd_log_buffer *buffer = &cmdlog.buffer;
char fname[CMDLOG_FILENAME_LENGTH];
uint32_t cur_tail;
uint32_t cur_last;
int fd = -1;
int stop_state = CMDLOG_NOT_STARTED;
int writelen;
int nwritten;
int nwtotal = 0;
while (!cmdlog.reqstop)
{
if (fd < 0) { /* open command log file */
sprintf(fname, CMDLOG_FILENAME_FORMAT, cmdlog.stats.dirpath,
mc_port, cmdlog.stats.bgndate, cmdlog.stats.bgntime,
cmdlog.stats.file_count);
if ((fd = open(fname, O_WRONLY | O_CREAT | O_APPEND, 0644)) < 0) {
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
"Can't open command log file: %s, error: %s\n", fname, strerror(errno));
stop_state = CMDLOG_FLUSHERR_STOP; break;
}
cmdlog.stats.file_count++;
}
pthread_mutex_lock(&buffer->lock);
cur_last = buffer->last;
cur_tail = buffer->tail;
pthread_mutex_unlock(&buffer->lock);
if (buffer->head <= cur_tail) {
assert(cur_last == 0);
if (cmdlog_in_use) {
if ((cur_tail - buffer->head) < CMDLOG_WRITE_SIZE) {
do_cmdlog_flush_sleep(); /* flush thread sleeps 50ms. */
continue;
}
} else {
if (buffer->head == cur_tail) {
break; /* stop flushing */
}
}
writelen = (cur_tail - buffer->head) < CMDLOG_WRITE_SIZE
? (cur_tail - buffer->head) : CMDLOG_WRITE_SIZE;
if (writelen > 0) {
nwritten = write(fd, buffer->data + buffer->head, writelen);
if (nwritten != writelen) {
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
"write command log error: nwritten(%d) != writelen(%d)\n",
nwritten, writelen);
stop_state = CMDLOG_FLUSHERR_STOP; break;
}
nwtotal += nwritten;
pthread_mutex_lock(&buffer->lock);
buffer->head += writelen;
pthread_mutex_unlock(&buffer->lock);
}
} else { /* buffer->head > cur_tail */
assert(cur_last > 0);
writelen = cur_last - buffer->head;
if (writelen > 0) {
nwritten = write(fd, buffer->data + buffer->head, writelen);
if (nwritten != writelen) {
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
"write command log error: nwritten(%d) != writelen(%d)\n",
nwritten, writelen);
stop_state = CMDLOG_FLUSHERR_STOP; break;
}
nwtotal += nwritten;
pthread_mutex_lock(&buffer->lock);
buffer->last = 0;
buffer->head = 0;
pthread_mutex_unlock(&buffer->lock);
}
}
if (nwtotal >= CMDLOG_FILE_MAXSIZE) { /* rotate file */
close(fd); fd = -1;
nwtotal = 0;
if (cmdlog.stats.file_count >= CMDLOG_FILE_MAXNUM) {
stop_state = CMDLOG_OVERFLOW_STOP; break; /* do internal stop: overflow stop */
}
}
}
if (fd > 0) close(fd);
pthread_mutex_lock(&cmdlog.lock);
if (cmdlog_in_use) {
do_cmdlog_stop();
}
cmdlog.stats.state = stop_state;
pthread_mutex_unlock(&cmdlog.lock);
return NULL;
}
void cmdlog_init(int port, EXTENSION_LOGGER_DESCRIPTOR *logger)
{
mc_port = port;
mc_logger = logger;
cmdlog_in_use = false;
pthread_mutex_init(&cmdlog.lock, NULL);
pthread_mutex_init(&cmdlog.buffer.lock, NULL);
cmdlog.buffer.size = CMDLOG_BUFFER_SIZE;
cmdlog.buffer.data = NULL;
pthread_mutex_init(&cmdlog.flush.lock, NULL);
pthread_cond_init(&cmdlog.flush.cond, NULL);
cmdlog.flush.sleep = false;
memset(&cmdlog.stats, 0, sizeof(struct cmd_log_stats));
}
void cmdlog_final(void)
{
while (cmdlog.stats.state == CMDLOG_RUNNING) {
cmdlog.reqstop = true;
do_cmdlog_flush_wakeup(); /* wake up flush thread */
usleep(5000); /* sleep 5ms */
}
pthread_mutex_destroy(&cmdlog.buffer.lock);
pthread_mutex_destroy(&cmdlog.lock);
pthread_mutex_destroy(&cmdlog.flush.lock);
pthread_cond_destroy(&cmdlog.flush.cond);
if (cmdlog.buffer.data != NULL) {
free(cmdlog.buffer.data);
}
}
int cmdlog_start(char *file_path, bool *already_started)
{
char fname[CMDLOG_FILENAME_LENGTH];
int ret = 0;
int fd = -1;
*already_started = false;
pthread_mutex_lock(&cmdlog.lock);
do {
if (cmdlog_in_use) {
*already_started = true;
break;
}
/* check the previous cmdlog flush thread state */
if (cmdlog.stats.state == CMDLOG_RUNNING) {
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
"The previous cmdlog flush thread has not been finished yet.\n");
ret = -1; break;
}
/* check the length of file_path */
if (file_path != NULL && strlen(file_path) > CMDLOG_DIRPATH_LENGTH) {
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
"Too long cmdlog file path.\n");
ret = -1; break;
}
/* prepare command logging buffer */
if (cmdlog.buffer.data == NULL) {
if ((cmdlog.buffer.data = malloc(CMDLOG_BUFFER_SIZE)) == NULL) {
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
"Can't allocate command log buffer\n");
ret = -1; break;
}
}
cmdlog.buffer.head = 0;
cmdlog.buffer.tail = 0;
cmdlog.buffer.last = 0;
/* prepare command logging stats */
memset(&cmdlog.stats, 0, sizeof(struct cmd_log_stats));
cmdlog.stats.bgndate = getnowdate_int();
cmdlog.stats.bgntime = getnowtime_int();
sprintf(cmdlog.stats.dirpath, "%s",
(file_path != NULL ? file_path : "command_log"));
/* open log file */
sprintf(fname, CMDLOG_FILENAME_FORMAT, cmdlog.stats.dirpath,
mc_port, cmdlog.stats.bgndate, cmdlog.stats.bgntime,
cmdlog.stats.file_count);
if ((fd = open(fname, O_WRONLY | O_CREAT | O_APPEND, 0644)) < 0) {
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
"Can't open command log file: %s, error: %s\n", fname, strerror(errno));
ret = -1; break;
} else {
close(fd);
}
/* enable command logging */
cmdlog_in_use = true;
cmdlog.stats.state = CMDLOG_RUNNING;
/* start the flush thread to write command log to disk */
if (pthread_attr_init(&cmdlog.flush.attr) != 0 ||
pthread_attr_setdetachstate(&cmdlog.flush.attr, PTHREAD_CREATE_DETACHED) != 0 ||
(ret = pthread_create(&cmdlog.flush.tid, &cmdlog.flush.attr, cmdlog_flush_thread, NULL)) != 0)
{
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
"Can't create command log flush thread: %s\n", strerror(ret));
cmdlog_in_use = false; // disable it */
if (remove(fname) != 0 && errno != ENOENT) {
mc_logger->log(EXTENSION_LOG_WARNING, NULL,
"Can't remove command log file: %s, error: %s\n", fname, strerror(errno));
}
cmdlog.stats.state = CMDLOG_NOT_STARTED;
ret = -1; break;
}
} while(0);
pthread_mutex_unlock(&cmdlog.lock);
return ret;
}
void cmdlog_stop(bool *already_stopped)
{
*already_stopped = false;
pthread_mutex_lock(&cmdlog.lock);
if (cmdlog_in_use == true) {
do_cmdlog_stop();
} else {
*already_stopped = true;
}
pthread_mutex_unlock(&cmdlog.lock);
}
char *cmdlog_stats(void)
{
char *str = (char*)malloc(CMDLOG_INPUT_SIZE);
if (str) {
char *state_str[5] = { "Not started", // CMDLOG_NOT_STARTED
"stopped by command log overflow", // CMDLOG_OVERFLOW_STOP
"stopped by disk flush error", // CMDLOG_FLUSHERR_STOP
"running" }; // CMDLOG_RUNNING
struct cmd_log_stats *stats = &cmdlog.stats;
if (cmdlog_in_use) {
stats->enddate = getnowdate_int();
stats->endtime = getnowtime_int();
}
snprintf(str, CMDLOG_INPUT_SIZE,
"\t" "Command logging state : %s" "\n"
"\t" "The last running time : %d_%d ~ %d_%d" "\n"
"\t" "The number of entered commands : %u" "\n"
"\t" "The number of skipped commands : %u" "\n"
"\t" "The number of log files : %d" "\n"
"\t" "The log file name: %s/command_%d_%d_%d_{n}.log" "\n",
(stats->state >= 0 && stats->state <= 4 ?
state_str[stats->state] : "unknown"),
stats->bgndate, stats->bgntime, stats->enddate, stats->endtime,
stats->entered_commands, stats->skipped_commands,
stats->file_count,
stats->dirpath, mc_port, stats->bgndate, stats->bgntime);
}
return str;
}
void cmdlog_write(char client_ip[], char* command)
{
struct tm *ptm;
struct timeval val;
struct cmd_log_buffer *buffer = &cmdlog.buffer;
char inputstr[CMDLOG_INPUT_SIZE];
int inputlen;
int nwritten;
gettimeofday(&val, NULL);
ptm = localtime(&val.tv_sec);
nwritten = snprintf(inputstr, CMDLOG_INPUT_SIZE, "%02d:%02d:%02d.%06ld %s %s\n",
ptm->tm_hour, ptm->tm_min, ptm->tm_sec, (long)val.tv_usec, client_ip, command);
/* truncated ? */
if (nwritten > CMDLOG_INPUT_SIZE) {
inputstr[CMDLOG_INPUT_SIZE-4] = '.';
inputstr[CMDLOG_INPUT_SIZE-3] = '.';
inputstr[CMDLOG_INPUT_SIZE-2] = '\n';
}
inputlen = strlen(inputstr);
pthread_mutex_lock(&buffer->lock);
cmdlog.stats.entered_commands += 1;
if (buffer->head <= buffer->tail && inputlen >= (buffer->size - buffer->tail)) {
buffer->last = buffer->tail;
buffer->tail = 0;
}
if (buffer->head <= buffer->tail) {
assert(buffer->last == 0);
assert(inputlen < (buffer->size - buffer->tail));
memcpy(buffer->data + buffer->tail, inputstr, inputlen);
buffer->tail += inputlen;
if ((buffer->tail - buffer->head) >= CMDLOG_WRITE_SIZE) {
do_cmdlog_flush_wakeup(); /* wake up flush thread */
}
} else { /* buffer->head > buffer->tail */
assert(buffer->last > 0);
if (inputlen < (buffer->head - buffer->tail)) {
memcpy(buffer->data + buffer->tail, inputstr, inputlen);
buffer->tail += inputlen;
} else {
cmdlog.stats.skipped_commands += 1;
}
do_cmdlog_flush_wakeup(); /* wake up flush thread */
}
pthread_mutex_unlock(&buffer->lock);
}