<commit_before>/*
 * $Id$
 *
 * Log tailer for Varnish
 */

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <regex.h>
#include <math.h>
#include <curses.h>

#include "compat/vis.h"

#include "libvarnish.h"
#include "shmlog.h"
#include "varnishapi.h"

#define HIST_LOW -50
#define HIST_HIGH 25
#define HIST_W (1 + (HIST_HIGH - HIST_LOW))
#define HIST_N 2000

static char rr_hist[HIST_N];
static unsigned next_hist;
static unsigned bucket_miss[HIST_W];
static unsigned bucket_hit[HIST_W];
static unsigned char hh[65536];
static double scale = 10;
static double c_hist;

static void
r_hist(void)
{
	int x, y;
	double m, r;
	time_t t;
	static time_t tl;

	t = time(NULL);
	if (t == tl)
		return;
	tl = t;
	m = 0;
	r = 0;
	for (x = 1; x < HIST_W; x++) {
		if (bucket_hit[x] + bucket_miss[x] > m)
			m = bucket_hit[x] + bucket_miss[x];
		r += bucket_hit[x];
		r += bucket_miss[x];
	}

	while (m > HIST_N / scale)
		scale--;

	mvprintw(0, 0, "Max %.0f Scale %.0f Tot: %.0f", m, HIST_N / scale, r);
	m = (HIST_N / scale) / (LINES - 3);
	move(1,0);
	for (y = LINES - 3; y > 0; y--) {
		if (y == 1)
			r = 0;
		else
			r = y * m;
		for (x = 0; x < HIST_W; x++) {
			if (bucket_miss[x] > r)
				addch('#');
			else if (bucket_hit[x] + bucket_miss[x] > r)
				addch('|');
			else
				addch(' ');
		}
		addch('\n');
	}
	refresh();
}

static int 
h_hist(void *priv, unsigned tag, unsigned fd, unsigned len, unsigned spec, const char *ptr)
{
	double b;
	int i, j;
	
	(void)priv;
	(void)fd;
	(void)len;
	(void)spec;
	if (tag == SLT_Hit) {
		hh[fd] = 1;
		return (0);
	}
	if (tag != SLT_ReqEnd)
		return (0);
#if 1
	i = sscanf(ptr, "%*d %*f %*f %*f %lf", &b);
#else
	i = sscanf(ptr, "%*d %*f %*f %lf", &b);
#endif
	assert(i == 1);
	i = log(b) * c_hist;
	if (i < HIST_LOW)
		i = HIST_LOW;
	if (i > HIST_HIGH)
		i = HIST_HIGH;
	i -= HIST_LOW;
	assert(i < HIST_W);

	j = rr_hist[next_hist];
	if (j < 0)  {
		assert(bucket_miss[-j] > 0);
		bucket_miss[-j]--;
	} else {
		assert(bucket_hit[j] > 0);
		bucket_hit[j]--;
	}

	if (hh[fd] || i == 0) {
		bucket_hit[i]++;
		rr_hist[next_hist] = i;
	} else {
		bucket_miss[i]++;
		rr_hist[next_hist] = -i;
	}
	if (++next_hist == HIST_N) {
		next_hist = 0;
	}
	if (!(next_hist % 100))
		r_hist();
	hh[fd] = 0;
	return (0);
}

/*--------------------------------------------------------------------*/

static void
usage(void)
{
	fprintf(stderr,
	    "usage: varnishhist");
	exit(1);
}

int
main(int argc, char **argv)
{
	int i, c, x;
	struct VSL_data *vd;

	vd = VSL_New();
	
	while ((c = getopt(argc, argv, VSL_ARGS)) != -1) {
		switch (c) {
		default:
			if (VSL_Arg(vd, c, optarg) > 0)
				break;
			usage();
		}
	}

	if (VSL_OpenLog(vd))
		exit (1);

	c_hist = 10.0 / log(10.0);
	initscr();
	erase();

	bucket_hit[0] = HIST_N;
	move(LINES - 2, 0);
	for (x = 0; x < HIST_W; x++)
		addch('-');

	for (x = 0; x < HIST_W; x++) {
		if ((x + HIST_LOW) % 10 != 0)
			continue;
		mvprintw(LINES - 2, x, "+");
		mvprintw(LINES - 1, x, "|1e%d", (x + HIST_LOW) / 10);
	}

	while (1) {
		i = VSL_Dispatch(vd, h_hist, NULL);
		if (i < 0)
			break;
		if (i == 0)
			r_hist();
	} 

	return (0);
}<commit_msg>Allow the delay between screen updates to be specified with -w.<commit_after>/*
 * $Id$
 *
 * Log tailer for Varnish
 */

#include <curses.h>
#include <errno.h>
#include <math.h>
#include <regex.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "compat/vis.h"

#include "libvarnish.h"
#include "shmlog.h"
#include "varnishapi.h"

#define HIST_LOW -50
#define HIST_HIGH 25
#define HIST_W (1 + (HIST_HIGH - HIST_LOW))
#define HIST_N 2000

static int delay = 1;
static volatile sig_atomic_t redraw;
static char rr_hist[HIST_N];
static unsigned next_hist;
static unsigned bucket_miss[HIST_W];
static unsigned bucket_hit[HIST_W];
static unsigned char hh[65536];
static double scale = 10;
static double c_hist;

static void
sigalrm(int sig)
{

	(void)sig;
	redraw = 1;
}

static void
r_hist(void)
{
	int x, y;
	double m, r;

	m = 0;
	r = 0;
	for (x = 1; x < HIST_W; x++) {
		if (bucket_hit[x] + bucket_miss[x] > m)
			m = bucket_hit[x] + bucket_miss[x];
		r += bucket_hit[x];
		r += bucket_miss[x];
	}

	while (m > HIST_N / scale)
		scale--;

	mvprintw(0, 0, "Max %.0f Scale %.0f Tot: %.0f", m, HIST_N / scale, r);
	m = (HIST_N / scale) / (LINES - 3);
	move(1,0);
	for (y = LINES - 3; y > 0; y--) {
		if (y == 1)
			r = 0;
		else
			r = y * m;
		for (x = 0; x < HIST_W; x++) {
			if (bucket_miss[x] > r)
				addch('#');
			else if (bucket_hit[x] + bucket_miss[x] > r)
				addch('|');
			else
				addch(' ');
		}
		addch('\n');
	}
	refresh();
	redraw = 0;
	alarm(delay);
}

static int 
h_hist(void *priv, unsigned tag, unsigned fd, unsigned len, unsigned spec, const char *ptr)
{
	double b;
	int i, j;
	
	(void)priv;
	(void)fd;
	(void)len;
	(void)spec;
	if (tag == SLT_Hit) {
		hh[fd] = 1;
		return (0);
	}
	if (tag != SLT_ReqEnd)
		return (0);
#if 1
	i = sscanf(ptr, "%*d %*f %*f %*f %lf", &b);
#else
	i = sscanf(ptr, "%*d %*f %*f %lf", &b);
#endif
	assert(i == 1);
	i = log(b) * c_hist;
	if (i < HIST_LOW)
		i = HIST_LOW;
	if (i > HIST_HIGH)
		i = HIST_HIGH;
	i -= HIST_LOW;
	assert(i < HIST_W);

	j = rr_hist[next_hist];
	if (j < 0)  {
		assert(bucket_miss[-j] > 0);
		bucket_miss[-j]--;
	} else {
		assert(bucket_hit[j] > 0);
		bucket_hit[j]--;
	}

	if (hh[fd] || i == 0) {
		bucket_hit[i]++;
		rr_hist[next_hist] = i;
	} else {
		bucket_miss[i]++;
		rr_hist[next_hist] = -i;
	}
	if (++next_hist == HIST_N) {
		next_hist = 0;
	}
	hh[fd] = 0;
	return (0);
}

/*--------------------------------------------------------------------*/

static void
usage(void)
{
	fprintf(stderr,
	    "usage: varnishhist");
	exit(1);
}

int
main(int argc, char **argv)
{
	int i, c, x;
	struct VSL_data *vd;

	vd = VSL_New();
	
	while ((c = getopt(argc, argv, VSL_ARGS "w:")) != -1) {
		switch (c) {
		case 'w':
			delay = atoi(optarg);
			break;
		default:
			if (VSL_Arg(vd, c, optarg) > 0)
				break;
			usage();
		}
	}

	if (VSL_OpenLog(vd))
		exit (1);

	c_hist = 10.0 / log(10.0);
	initscr();
	erase();

	bucket_hit[0] = HIST_N;
	move(LINES - 2, 0);
	for (x = 0; x < HIST_W; x++)
		addch('-');

	for (x = 0; x < HIST_W; x++) {
		if ((x + HIST_LOW) % 10 != 0)
			continue;
		mvprintw(LINES - 2, x, "+");
		mvprintw(LINES - 1, x, "|1e%d", (x + HIST_LOW) / 10);
	}

	signal(SIGALRM, sigalrm);
	redraw = 1;
	while (1) {
		i = VSL_Dispatch(vd, h_hist, NULL);
		if (i < 0)
			break;
		if (redraw)
			r_hist();
	} 

	return (0);
}
{'fraction_non_alphanumeric': 0.21620795107033638, 'fraction_numerical': 0.022629969418960245, 'mean_word_length': 2.811771561771562, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3686147', 'n_tokens_mistral': 3334, 'n_tokens_neox': 3054, 'n_words': 772}