Commit b604c974 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Begin a major round of polishing of the VarnishAPI to make it sensible

general-ish and explainable.



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@4925 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 59c7da17
......@@ -245,7 +245,7 @@ main(int argc, char * const *argv)
if (n_arg != NULL) {
vsd = VSL_New();
assert(VSL_Log_Arg(vsd, 'n', n_arg));
if (!VSL_Open(vsd)) {
if (!VSL_Open(vsd, 1)) {
if (T_arg == NULL) {
p = VSL_Find_Alloc(vsd, "Arg", "-T", "", NULL);
if (p != NULL) {
......
......@@ -207,7 +207,7 @@ main(int argc, char * const *argv)
(void)VSL_Stat_Arg(vd, c, optarg);
break;
case 'l':
if (VSL_Open(vd))
if (VSL_Open(vd, 1))
exit(1);
list_fields(vd);
exit(0);
......@@ -227,7 +227,7 @@ main(int argc, char * const *argv)
}
}
if (VSL_Open(vd))
if (VSL_Open(vd, 1))
exit(1);
if ((VSL_stats = VSL_OpenStats(vd)) == NULL)
......
......@@ -164,7 +164,7 @@ do_curses(struct VSL_data *vd, const struct varnish_stats *VSL_stats,
* Only check if it looks like nothing is happening.
*/
act = VSL_stats->cache_hit + VSL_stats->cache_miss + 1;
if (act == lact && VSL_ReOpen(vd))
if (act == lact && VSL_ReOpen(vd, 1))
break;
lact = act;
......
......@@ -34,9 +34,86 @@
#include "shmlog.h"
/* base64.c */
void base64_init(void);
int base64_decode(char *d, unsigned dlen, const char *s);
/*
* Various notes:
* All malloc failures will result in assert tripping.
* API use failures will trip assert.
*/
/*---------------------------------------------------------------------
* Level 0: Create and destroy the VSL_data handle structure
*/
struct VSL_data *VSL_New(void);
/*
* Allocate and initialize a VSL_data handle structure.
* This is the first thing you will have to do, always.
* You can have multiple active VSL_data handles at the same time
* referencing the same or different shared memory files.
* Returns:
* Pointer to usable VSL_data handle.
*/
typedef void vsl_diag_f(void *priv, const char *fmt, ...);
void VSL_Diag(struct VSL_data *vd, vsl_diag_f *func, void *priv);
/*
* Set the diagnostics reporting function.
* Default is fprintf(stderr, ...)
* If func is NULL, diagnostics are disabled.
*/
int VSL_n_Arg(struct VSL_data *vd, const char *n_arg);
/*
* Configure which varnishd instance to access.
* Can also be, and normally is done through the VSL_Log_arg()
* and VSL_Stat_Arg() functions.
* Returns:
* 1 on success
* -1 on failure, with diagnostic on stderr.
*/
const char *VSL_Name(const struct VSL_data *vd);
/*
* Return the instance name.
*/
void VSL_Delete(struct VSL_data *vd);
/*
* Close and deallocate all storage and mappings.
*/
/* XXX: extension: Patience argument for sleeps */
/*---------------------------------------------------------------------
* Level 1: Open/Close and find allocation in shared memory segment
*/
int VSL_Open(struct VSL_data *vd, int diag);
/*
* Attempt to open and map the shared memory file.
* If diag is non-zero, diagnostics are emitted.
* Returns:
* 0 on success
* != 0 on failure
*/
int VSL_ReOpen(struct VSL_data *vd, int diag);
/*
* Check if shared memory segment needs to be reopened/remapped
* typically when the varnishd master process restarts.
* diag is passed to VSL_Open()
* Returns:
* 0 No reopen needed.
* 1 shared memory reopened/remapped.
* -1 failure to reopen.
*/
void *VSL_Find_Alloc(struct VSL_data *vd, const char *class, const char *type,
const char *ident, unsigned *lenp);
void VSL_Close(struct VSL_data *vd);
/* shmlog.c */
typedef int vsl_handler(void *priv, enum shmlogtag tag, unsigned fd,
......@@ -49,7 +126,6 @@ typedef int vsl_handler(void *priv, enum shmlogtag tag, unsigned fd,
" [-r file] [-s skip] [-X regexp] [-x tag]"
vsl_handler VSL_H_Print;
struct VSL_data;
struct VSL_data *VSL_New(void);
void VSL_Select(const struct VSL_data *vd, unsigned tag);
int VSL_OpenLog(struct VSL_data *vd);
void VSL_NonBlocking(struct VSL_data *vd, int nb);
......@@ -57,16 +133,9 @@ int VSL_Dispatch(struct VSL_data *vd, vsl_handler *func, void *priv);
int VSL_NextLog(struct VSL_data *lh, uint32_t **pp);
int VSL_Log_Arg(struct VSL_data *vd, int arg, const char *opt);
int VSL_Stat_Arg(struct VSL_data *vd, int arg, const char *opt);
void VSL_Close(struct VSL_data *vd);
int VSL_Open(struct VSL_data *vd);
void VSL_Delete(struct VSL_data *vd);
struct varnish_stats *VSL_OpenStats(struct VSL_data *vd);
const char *VSL_Name(const struct VSL_data *vd);
extern const char *VSL_tags[256];
void *VSL_Find_Alloc(struct VSL_data *vd, const char *class, const char *type,
const char *ident, unsigned *lenp);
int VSL_ReOpen(struct VSL_data *vd);
struct shmalloc *vsl_iter0(const struct VSL_data *vd);
void vsl_itern(const struct VSL_data *vd, struct shmalloc **pp);
......@@ -88,4 +157,8 @@ typedef int vsl_stat_f(void *priv, const struct vsl_statpt *const pt);
int VSL_IterStat(const struct VSL_data *vd, vsl_stat_f *func, void *priv);
/* base64.c */
void base64_init(void);
int base64_decode(char *d, unsigned dlen, const char *s);
#endif
......@@ -64,12 +64,16 @@ VSL_New(void)
{
struct VSL_data *vd;
vd = calloc(sizeof *vd, 1);
assert(vd != NULL);
vd->regflags = 0;
vd->magic = VSL_MAGIC;
ALLOC_OBJ(vd, VSL_MAGIC);
AN(vd);
vd->diag = (vsl_diag_f*)fprintf;
vd->priv = stderr;
vd->vsl_fd = -1;
vd->regflags = 0;
/* XXX: Allocate only if log access */
vd->vbm_client = vbit_init(4096);
vd->vbm_backend = vbit_init(4096);
......@@ -84,16 +88,59 @@ VSL_New(void)
VTAILQ_INIT(&vd->sf_list);
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
return (vd);
}
/*--------------------------------------------------------------------*/
void
VSL_Diag(struct VSL_data *vd, vsl_diag_f *func, void *priv)
{
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
if (func == NULL)
vd->diag = (vsl_diag_f*)getpid;
else
vd->diag = func;
vd->priv = priv;
}
/*--------------------------------------------------------------------*/
int
VSL_n_Arg(struct VSL_data *vd, const char *opt)
{
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
REPLACE(vd->n_opt, opt);
AN(vd->n_opt);
if (vin_n_arg(vd->n_opt, NULL, NULL, &vd->fname)) {
vd->diag(vd->priv, "Invalid instance name: %s\n",
strerror(errno));
return (-1);
}
return (1);
}
/*--------------------------------------------------------------------*/
const char *
VSL_Name(const struct VSL_data *vd)
{
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
return (vd->n_opt);
}
/*--------------------------------------------------------------------*/
void
VSL_Delete(struct VSL_data *vd)
{
struct vsl_sf *sf;
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
VSL_Close(vd);
vbit_destroy(vd->vbm_client);
vbit_destroy(vd->vbm_backend);
......@@ -118,7 +165,7 @@ VSL_Delete(struct VSL_data *vd)
/*--------------------------------------------------------------------*/
static int
vsl_open(struct VSL_data *vd, int rep)
vsl_open(struct VSL_data *vd, int diag)
{
int i;
struct shmloghead slh;
......@@ -128,30 +175,30 @@ vsl_open(struct VSL_data *vd, int rep)
vd->vsl_fd = open(vd->fname, O_RDONLY);
if (vd->vsl_fd < 0) {
if (rep)
fprintf(stderr, "Cannot open %s: %s\n",
if (diag)
vd->diag(vd->priv, "Cannot open %s: %s\n",
vd->fname, strerror(errno));
return (1);
}
assert(fstat(vd->vsl_fd, &vd->fstat) == 0);
if (!S_ISREG(vd->fstat.st_mode)) {
if (rep)
fprintf(stderr, "%s is not a regular file\n",
if (diag)
vd->diag(vd->priv, "%s is not a regular file\n",
vd->fname);
return (1);
}
i = read(vd->vsl_fd, &slh, sizeof slh);
if (i != sizeof slh) {
if (rep)
fprintf(stderr, "Cannot read %s: %s\n",
if (diag)
vd->diag(vd->priv, "Cannot read %s: %s\n",
vd->fname, strerror(errno));
return (1);
}
if (slh.magic != SHMLOGHEAD_MAGIC) {
if (rep)
fprintf(stderr, "Wrong magic number in file %s\n",
if (diag)
vd->diag(vd->priv, "Wrong magic number in file %s\n",
vd->fname);
return (1);
}
......@@ -159,24 +206,27 @@ vsl_open(struct VSL_data *vd, int rep)
vd->vsl_lh = (void *)mmap(NULL, slh.shm_size,
PROT_READ, MAP_SHARED|MAP_HASSEMAPHORE, vd->vsl_fd, 0);
if (vd->vsl_lh == MAP_FAILED) {
if (rep)
fprintf(stderr, "Cannot mmap %s: %s\n",
if (diag)
vd->diag(vd->priv, "Cannot mmap %s: %s\n",
vd->fname, strerror(errno));
return (1);
}
vd->vsl_end = (uint8_t *)vd->vsl_lh + slh.shm_size;
while(slh.alloc_seq == 0)
usleep(50000);
usleep(50000); /* XXX limit total sleep */
vd->alloc_seq = slh.alloc_seq;
return (0);
}
/*--------------------------------------------------------------------*/
int
VSL_Open(struct VSL_data *vd)
VSL_Open(struct VSL_data *vd, int diag)
{
return (vsl_open(vd, 1));
return (vsl_open(vd, diag));
}
/*--------------------------------------------------------------------*/
......@@ -196,13 +246,12 @@ VSL_Close(struct VSL_data *vd)
/*--------------------------------------------------------------------*/
int
VSL_ReOpen(struct VSL_data *vd)
VSL_ReOpen(struct VSL_data *vd, int diag)
{
struct stat st;
int i;
if (vd->vsl_lh == NULL)
return (-1);
AN(vd->vsl_lh);
if (stat(vd->fname, &st))
return (0);
......@@ -211,11 +260,11 @@ VSL_ReOpen(struct VSL_data *vd)
return (0);
VSL_Close(vd);
for (i = 0; i < 5; i++) {
for (i = 0; i < 5; i++) { /* XXX param */
if (!vsl_open(vd, 0))
return (1);
}
if (vsl_open(vd, 1))
if (vsl_open(vd, diag))
return (-1);
return (1);
}
......@@ -281,8 +330,6 @@ VSL_Find_Alloc(struct VSL_data *vd, const char *class, const char *type, const c
struct shmalloc *sha;
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
if (VSL_Open(vd))
return (NULL);
sha = vsl_find_alloc(vd, class, type, ident);
if (sha == NULL)
return (NULL);
......@@ -290,12 +337,3 @@ VSL_Find_Alloc(struct VSL_data *vd, const char *class, const char *type, const c
*lenp = sha->len - sizeof *sha;
return (SHA_PTR(sha));
}
/*--------------------------------------------------------------------*/
const char *
VSL_Name(const struct VSL_data *vd)
{
return (vd->n_opt);
}
......@@ -51,7 +51,13 @@ struct VSL_data {
unsigned magic;
#define VSL_MAGIC 0x6e3bd69b
vsl_diag_f *diag;
void *priv;
char *n_opt;
char *fname;
struct stat fstat;
int vsl_fd;
......@@ -78,7 +84,6 @@ struct VSL_data {
uint32_t *rbuf;
unsigned L_opt;
char *n_opt;
int b_opt;
int c_opt;
int d_opt;
......
......@@ -192,22 +192,6 @@ vsl_k_arg(struct VSL_data *vd, const char *opt)
/*--------------------------------------------------------------------*/
static int
vsl_n_arg(struct VSL_data *vd, const char *opt)
{
REPLACE(vd->n_opt, opt);
AN(vd->n_opt);
if (vin_n_arg(vd->n_opt, NULL, NULL, &vd->fname)) {
fprintf(stderr, "Invalid instance name: %s\n",
strerror(errno));
return (-1);
}
return (1);
}
/*--------------------------------------------------------------------*/
int
VSL_Log_Arg(struct VSL_data *vd, int arg, const char *opt)
{
......@@ -222,7 +206,7 @@ VSL_Log_Arg(struct VSL_data *vd, int arg, const char *opt)
return (1);
case 'i': case 'x': return (vsl_ix_arg(vd, opt, arg));
case 'k': return (vsl_k_arg(vd, opt));
case 'n': return (vsl_n_arg(vd, opt));
case 'n': return (VSL_n_Arg(vd, opt));
case 'r': return (vsl_r_arg(vd, opt));
case 's': return (vsl_s_arg(vd, opt));
case 'I': case 'X': return (vsl_IX_arg(vd, opt, arg));
......@@ -331,7 +315,7 @@ VSL_Stat_Arg(struct VSL_data *vd, int arg, const char *opt)
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
switch (arg) {
case 'f': return (vsl_sf_arg(vd, opt));
case 'n': return (vsl_n_arg(vd, opt));
case 'n': return (VSL_n_Arg(vd, opt));
default:
return (0);
}
......
......@@ -264,8 +264,6 @@ VSL_OpenLog(struct VSL_data *vd)
struct shmalloc *sha;
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
if (VSL_Open(vd))
return (-1);
sha = vsl_find_alloc(vd, VSL_CLASS_LOG, "", "");
assert(sha != NULL);
......
......@@ -54,8 +54,6 @@ VSL_OpenStats(struct VSL_data *vd)
CHECK_OBJ_NOTNULL(vd, VSL_MAGIC);
if (VSL_Open(vd))
return (NULL);
sha = vsl_find_alloc(vd, VSL_CLASS_STAT, "", "");
assert(sha != NULL);
return (SHA_PTR(sha));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment