<commit_before>/*
 * Copyright (c) 2014-2018 Red Hat.
 * Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program 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 General Public License
 * for more details.
 */

#include "pmapi.h"
#include "libpcp.h"
#include <ctype.h>
#include <limits.h>
#include <float.h>
#include <sys/stat.h>

static struct pmTimeval	pmtv;
static char		timebuf[32];	/* for pmCtime result + .xxx */
static int		numpmid;
static pmID		*pmid;
static pmID		pmid_flags;
static pmID		pmid_missed;
static int		sflag;
static int		xflag;		/* for -x (long timestamps) */
static pmLogLabel	label;

static pmLongOptions longopts[] = {
    PMAPI_OPTIONS_HEADER("Options"),
    PMOPT_DEBUG,
    { "all", 0, 'a', 0, "dump everything" },
    { "descs", 0, 'd', 0, "dump metric descriptions" },
    { "labelsets", 0, 'e', 0, "dump all metric label metadata" },
    { "helptext", 0, 'h', 0, "dump all metric help text" },
    { "insts", 0, 'i', 0, "dump instance domain descriptions" },
    { "", 0, 'L', 0, "more verbose form of archive label dump" },
    { "label", 0, 'l', 0, "dump the archive label" },
    { "metrics", 0, 'm', 0, "dump values of the metrics (default)" },
    PMOPT_NAMESPACE,
    { "reverse", 0, 'r', 0, "process archive in reverse chronological order" },
    PMOPT_START,
    { "sizes", 0, 's', 0, "report size of data records in archive" },
    PMOPT_FINISH,
    { "", 0, 't', 0, "dump the temporal index" },
    { "", 1, 'v', "FILE", "verbose hex dump of a physical file in raw format" },
    { "", 0, 'x', 0, "include date in reported timestamps" },
    PMOPT_TIMEZONE,
    PMOPT_HOSTZONE,
    PMOPT_VERSION,
    PMOPT_HELP,
    PMAPI_OPTIONS_END
};

static int overrides(int, pmOptions *);
static pmOptions opts = {
    .flags = PM_OPTFLAG_DONE | PM_OPTFLAG_STDOUT_TZ | PM_OPTFLAG_BOUNDARIES,
    .short_options = "aD:dehilLmMn:rS:sT:tv:xZ:z?",
    .long_options = longopts,
    .short_usage = "[options] [archive [metricname ...]]",
    .override = overrides,
};

/*
 * return -1, 0 or 1 as the struct timeval's compare
 * a < b, a == b or a > b
 */
static int
tvcmp(struct timeval a, struct timeval b)
{
    if (a.tv_sec < b.tv_sec)
	return -1;
    if (a.tv_sec > b.tv_sec)
	return 1;
    if (a.tv_usec < b.tv_usec)
	return -1;
    if (a.tv_usec > b.tv_usec)
	return 1;
    return 0;
}

static int
do_size(pmResult *rp)
{
    int		nbyte = 0;
    int		i;
    int		j;
    /*
     *  Externally the log record looks like this ...
     *  :----------:-----------:..........:---------:
     *  | int len  | timestamp | pmResult | int len |
     *  :----------:-----------:..........:---------:
     *
     * start with sizes of the header len, timestamp, numpmid, and
     * trailer len
     */
    nbyte = sizeof(int) + sizeof(pmTimeval) + sizeof(int);
    						/* len + timestamp + len */
    nbyte += sizeof(int);
    							/* numpmid */
    for (i = 0; i < rp->numpmid; i++) {
	pmValueSet	*vsp = rp->vset[i];
	nbyte += sizeof(pmID) + sizeof(int);		/* + pmid[i], numval */
	if (vsp->numval > 0) {
	    nbyte += sizeof(int);			/* + valfmt */
	    for (j = 0; j < vsp->numval; j++) {
		nbyte += sizeof(__pmValue_PDU);		/* + pmValue[j] */
		if (vsp->valfmt != PM_VAL_INSITU)
							/* + pmValueBlock */
							/* rounded up */
		    nbyte += PM_PDU_SIZE_BYTES(vsp->vlist[j].value.pval->vlen);
	    }
	}
    }

    return nbyte;
}

static void
setup_event_derived_metrics(void)
{
    int sts;

    if (pmid_flags == 0) {
	/*
	 * get PMID for event.flags and event.missed
	 * note that pmUnpackEventRecords() will have called
	 * __pmRegisterAnon(), so the anon metrics
	 * should now be in the PMNS
	 */
	char	*name_flags = "event.flags";
	char	*name_missed = "event.missed";

	if ((sts = pmLookupName(1, &name_flags, &pmid_flags)) < 0) {
	    /* should not happen! */
	    fprintf(stderr, "Warning: cannot get PMID for %s: %s\n",
		    name_flags, pmErrStr(sts));
	    /* avoid subsequent warnings ... */
	    pmid_flags = pmID_build(pmID_domain(pmid_flags), pmID_cluster(pmid_flags), 1);
	}
	sts = pmLookupName(1, &name_missed, &pmid_missed);
	if (sts < 0) {
	    /* should not happen! */
	    fprintf(stderr, "Warning: cannot get PMID for %s: %s\n",
		    name_missed, pmErrStr(sts));
	    /* avoid subsequent warnings ... */
	    pmid_missed = pmID_build(pmID_domain(pmid_missed), pmID_cluster(pmid_missed), 1);
	}
    }
}

static int
dump_nrecords(int nrecords, int nmissed)
{
    printf("%d", nrecords);
    if (nmissed > 0)
	printf(" (and %d missed)", nmissed);
    if (nrecords + nmissed == 1)
	printf(" event record\n");
    else
	printf(" event records\n");
    return 0;
}

static int
dump_nparams(int numpmid)
{
    if (numpmid == 0) {
	printf(" ---\n");
	printf("	          No parameters\n");
	return -1;
    }
    if (numpmid < 0) {
	printf(" ---\n");
	printf("	          Error: illegal number of parameters (%d)\n",
		numpmid);
	return -1;
    }
    return 0;
}

static void
dump_parameter(pmValueSet *xvsp, int index, int *flagsp)
{
    int		sts, flags = *flagsp;
    pmDesc	desc;
    char	**names;

    if ((sts = pmNameAll(xvsp->pmid, &names)) >= 0) {
	if (index == 0) {
	    if (xvsp->pmid == pmid_flags) {
		flags = *flagsp = xvsp->vlist[0].value.lval;
		printf(" flags 0x%x", flags);
		printf(" (%s) ---\n", pmEventFlagsStr(flags));
		free(names);
		return;
	    }
	    printf(" ---\n");
	}
	if ((flags & PM_EVENT_FLAG_MISSED) && index == 1 &&
	    (xvsp->pmid == pmid_missed)) {
	    printf("        ==> %d missed event records\n",
		    xvsp->vlist[0].value.lval);
	    free(names);
	    return;
	}
	printf("        %s (", pmIDStr(xvsp->pmid));
	__pmPrintMetricNames(stdout, sts, names, " or ");
	printf("):");
	free(names);
    }
    else
	printf("        PMID: %s:", pmIDStr(xvsp->pmid));
    if ((sts = pmLookupDesc(xvsp->pmid, &desc)) < 0) {
	printf(" pmLookupDesc: %s\n", pmErrStr(sts));
	return;
    }
    printf(" value ");
    pmPrintValue(stdout, xvsp->valfmt, desc.type, &xvsp->vlist[0], 1);
    putchar('\n');
}

static void
dump_event(int numnames, char **names, pmValueSet *vsp, int index, int indom, int type)
{
    int		r;		/* event records */
    int		p;		/* event parameters */
    int		flags;
    int		nrecords;
    int		nmissed = 0;
    int		highres = (type == PM_TYPE_HIGHRES_EVENT);
    char	*iname;
    pmValue	*vp = &vsp->vlist[index];

    printf("    %s (", pmIDStr(vsp->pmid));
    __pmPrintMetricNames(stdout, numnames, names, " or ");
    if (indom != PM_INDOM_NULL) {
	putchar('[');
	if (pmNameInDom(indom, vp->inst, &iname) < 0)
	    printf("%d or ???])", vp->inst);
	else {
	    printf("%d or \"%s\"])", vp->inst, iname);
	    free(iname);
	}
    }
    else {
	printf(")");
    }
    printf(": ");

    if (highres) {
	pmHighResResult	**hr;

	if ((nrecords = pmUnpackHighResEventRecords(vsp, index, &hr)) < 0)
	    return;
	if (nrecords == 0) {
	    printf("No event records\n");
	    pmFreeHighResEventResult(hr);
	    return;
	}
	setup_event_derived_metrics();

	for (r = 0; r < nrecords; r++) {
	    if (hr[r]->numpmid == 2 && hr[r]->vset[0]->pmid == pmid_flags &&
		(hr[r]->vset[0]->vlist[0].value.lval & PM_EVENT_FLAG_MISSED) &&
		hr[r]->vset[1]->pmid == pmid_missed) {
		nmissed += hr[r]->vset[1]->vlist[0].value.lval;
	    }
	}
	dump_nrecords(nrecords, nmissed);

	for (r = 0; r < nrecords; r++) {
	    printf("        --- event record [%d] timestamp ", r);
	    pmPrintHighResStamp(stdout, &hr[r]->timestamp);
	    if (dump_nparams(hr[r]->numpmid) < 0)
		continue;
	    flags = 0;
	    for (p = 0; p < hr[r]->numpmid; p++)
		dump_parameter(hr[r]->vset[p], p, &flags);
	}
	pmFreeHighResEventResult(hr);
    }
    else {
	pmResult	**res;

	if ((nrecords = pmUnpackEventRecords(vsp, index, &res)) < 0)
	    return;
	if (nrecords == 0) {
	    printf("No event records\n");
	    pmFreeEventResult(res);
	    return;
	}
	setup_event_derived_metrics();

	for (r = 0; r < nrecords; r++) {
	    if (res[r]->numpmid == 2 && res[r]->vset[0]->pmid == pmid_flags &&
		(res[r]->vset[0]->vlist[0].value.lval & PM_EVENT_FLAG_MISSED) &&
		res[r]->vset[1]->pmid == pmid_missed) {
		nmissed += res[r]->vset[1]->vlist[0].value.lval;
	    }
	}
	dump_nrecords(nrecords, nmissed);

	for (r = 0; r < nrecords; r++) {
	    printf("        --- event record [%d] timestamp ", r);
	    pmtv.tv_sec = res[r]->timestamp.tv_sec;
	    pmtv.tv_usec = res[r]->timestamp.tv_usec;
	    __pmPrintTimeval(stdout, &pmtv);
	    if (dump_nparams(res[r]->numpmid) < 0)
		continue;
	    flags = 0;
	    for (p = 0; p < res[r]->numpmid; p++)
		dump_parameter(res[r]->vset[p], p, &flags);
	}
	pmFreeEventResult(res);
    }
}

static void
dump_metric(int numnames, char **names, pmValueSet *vsp, int index, int indom, int type)
{
    pmValue	*vp = &vsp->vlist[index];
    char	*iname;

    if (index == 0) {
	printf("    %s (", pmIDStr(vsp->pmid));
	__pmPrintMetricNames(stdout, numnames, names, " or ");
	printf("):");
	if (vsp->numval > 1) {
	    putchar('\n');
	    printf("       ");
	}
    }
    else
	printf("       ");

    if (indom != PM_INDOM_NULL) {
	printf(" inst [");
	if (pmNameInDom(indom, vp->inst, &iname) < 0)
	    printf("%d or ???]", vp->inst);
	else {
	    printf("%d or \"%s\"]", vp->inst, iname);
	    free(iname);
	}
    }
    printf(" value ");
    pmPrintValue(stdout, vsp->valfmt, type, vp, 1);
    putchar('\n');
}

static void
dump_result(pmResult *resp)
{
    int		i;
    int		j;
    int		n;
    char	**names;
    pmDesc	desc;

    if (sflag) {
	int		nbyte;
	nbyte = do_size(resp);
	printf("[%d bytes]\n", nbyte);
    }

    if (xflag) {
	char	       *ddmm;
	char	       *yr;

	ddmm = pmCtime((const time_t *)&resp->timestamp.tv_sec, timebuf);
	ddmm[10] = '\0';
	yr = &ddmm[20];
	printf("%s ", ddmm);
	pmtv.tv_sec = resp->timestamp.tv_sec;
	pmtv.tv_usec = resp->timestamp.tv_usec;
	__pmPrintTimeval(stdout, &pmtv);
	printf(" %4.4s", yr);
	if (xflag >= 2)
	    printf(" (%.6f)", pmtimevalSub(&resp->timestamp, &label.ll_start));
    }
    else {
	pmtv.tv_sec = resp->timestamp.tv_sec;
	pmtv.tv_usec = resp->timestamp.tv_usec;
	__pmPrintTimeval(stdout, &pmtv);
    }

    if (resp->numpmid == 0) {
	printf("  <mark>\n");
	return;
    }
    printf(" %d metric", resp->numpmid);
    if (resp->numpmid == 0 || resp->numpmid > 1)
	putchar('s');
    putchar('\n');

    for (i = 0; i < resp->numpmid; i++) {
	pmValueSet	*vsp = resp->vset[i];

	names = NULL; /* silence coverity */
	n = pmNameAll(vsp->pmid, &names);
	if (vsp->numval == 0) {
	    printf("    %s (", pmIDStr(vsp->pmid));
	    __pmPrintMetricNames(stdout, n, names, " or ");
	    printf("): No values returned!\n");
	    goto next;
	}
	else if (vsp->numval < 0) {
	    printf("    %s (", pmIDStr(vsp->pmid));
	    __pmPrintMetricNames(stdout, n, names, " or ");
	    printf("): %s\n", pmErrStr(vsp->numval));
	    goto next;
	}

	if (pmLookupDesc(vsp->pmid, &desc) < 0) {
	    /* don't know, so punt on the most common cases */
	    desc.indom = PM_INDOM_NULL;
	    if (vsp->valfmt == PM_VAL_INSITU)
		desc.type = PM_TYPE_32;
	    else
		desc.type = PM_TYPE_AGGREGATE;
	}

	for (j = 0; j < vsp->numval; j++) {
	    if (desc.type == PM_TYPE_EVENT ||
		desc.type == PM_TYPE_HIGHRES_EVENT)
		dump_event(n, names, vsp, j, desc.indom, desc.type);
	    else
		dump_metric(n, names, vsp, j, desc.indom, desc.type);
	}
next:
	if (n > 0)
	    free(names);
    }
}

static void
dumpDesc(__pmContext *ctxp)
{
    int			i;
    int			sts;
    char		**names;
    __pmHashNode	*hp;
    pmDesc		*dp;

    printf("\nDescriptions for Metrics in the Log ...\n");
    for (i = 0; i < ctxp->c_archctl->ac_log->l_hashpmid.hsize; i++) {
	for (hp = ctxp->c_archctl->ac_log->l_hashpmid.hash[i]; hp != NULL; hp = hp->next) {
	    dp = (pmDesc *)hp->data;
	    names = NULL; /* silence coverity */
	    sts = pmNameAll(dp->pmid, &names);
	    if (sts < 0)
		printf("PMID: %s (%s)\n", pmIDStr(dp->pmid), "<noname>");
	    else {
		printf("PMID: %s (", pmIDStr(dp->pmid));
		__pmPrintMetricNames(stdout, sts, names, " or ");
		printf(")\n");
		free(names);
	    }
	    pmPrintDesc(stdout, dp);
	}
    }
}

static void
dumpInDom(__pmContext *ctxp)
{
    int		i;
    int		j;
    __pmHashNode	*hp;
    __pmLogInDom	*idp;
    __pmLogInDom	*ldp;
    printf("\nInstance Domains in the Log ...\n");
    for (i = 0; i < ctxp->c_archctl->ac_log->l_hashindom.hsize; i++) {
	for (hp = ctxp->c_archctl->ac_log->l_hashindom.hash[i]; hp != NULL; hp = hp->next) {
	    printf("InDom: %s\n", pmInDomStr((pmInDom)hp->key));
	    /*
	     * in reverse chronological order, so iteration is a bit funny
	     */
	    ldp = NULL;
	    for ( ; ; ) {
		for (idp = (__pmLogInDom *)hp->data; idp->next != ldp; idp =idp->next)
			;
		__pmPrintTimeval(stdout, &idp->stamp);
		printf(" %d instances\n", idp->numinst);
		for (j = 0; j < idp->numinst; j++) {
		    printf("   %d or \"%s\"\n",
			idp->instlist[j], idp->namelist[j]);
		}
		if (idp == (__pmLogInDom *)hp->data)
		    break;
		ldp = idp;
	    }
	}
    }
}

static void
dumpHelpText(__pmContext *ctxp)
{
    int			tix, cix, hix;
    unsigned int	type;
    unsigned int	class;
    unsigned int	ident;
    __pmHashCtl		*l_hashtext;
    const __pmHashCtl	*l_hashtype;
    const __pmHashNode	*hp, *tp;
    const __pmHashNode	*this_item[2], *prev_item[2];
    const char		*text;
    int textTypes[2]   = { PM_TEXT_PMID, PM_TEXT_INDOM };
    int textClasses[2] = { PM_TEXT_ONELINE, PM_TEXT_HELP };

    printf("\nMetric Help Text in the Log ...\n");

    /*
     * In order to make the output more deterministic for testing,
     * output the help text sorted by
     *   type (PM_TEXT_PMID, PM_TEXT_INDOM), then by
     *   identifier, then by
     *   class (PM_TEXT_ONELINE, PM_TEXT_HELP).
     */
    l_hashtext = &ctxp->c_archctl->ac_log->l_hashtext;
    for (tix = 0; tix < 2; ++tix) {
	type = textTypes[tix];

	/*
	 * Search the two hash tables representing the classes
	 * for this type to find the next lowest identifier.
	 */
	prev_item[0] = NULL;
	prev_item[1] = NULL;
	for (;;) {
	    for (cix = 0; cix < 2; ++cix) {
		this_item[cix] = NULL;

		class = textClasses[cix];
		hp = __pmHashSearch(type | class, l_hashtext);
		if (hp == NULL)
		    continue;

		l_hashtype = (__pmHashCtl *)hp->data;
		for (hix = 0; hix < l_hashtype->hsize; hix++) {
		    for (tp = l_hashtype->hash[hix]; tp != NULL; tp = tp->next) {
			ident = (unsigned int)tp->key;
			if (prev_item[cix] && ident <= (unsigned int)prev_item[cix]->key)
			    continue;
			if (!this_item[cix] || ident < (unsigned int)this_item[cix]->key)
			    this_item[cix] = tp;
		    }
		}
	    }

	    /* Are there any items left to print? */
	    if (this_item[0] == NULL && this_item[1] == NULL)
		break;

	    /*
	     * Print the item(s) with the lowest identifier.
	     * Careful. There will be either one or two items queued.
	     */
	    for (cix = 0; cix < 2; ++cix) {
		if (this_item[cix] == NULL)
		    continue;
		if (this_item[cix ^ 1] == NULL ||
		    ((unsigned int)this_item[cix]->key <=
		     (unsigned int)this_item[cix ^ 1]->key)) {
		    ident = (unsigned int)this_item[cix]->key;
		    text = (const char *)this_item[cix]->data;
		    if (type == PM_TEXT_PMID)
			printf("PMID: %s", pmIDStr((pmID)ident));
		    else if (type == PM_TEXT_INDOM)
			printf("InDom: %s", pmInDomStr((pmInDom)ident));
		    class = textClasses[cix];
		    if (class == PM_TEXT_ONELINE)
			printf(" [%s]", text);
		    else if (class == PM_TEXT_HELP)
			printf("\n%s", text);
		    putchar('\n');
		    
		    prev_item[cix] = this_item[cix];
		}
	    }
	}
    }
}

static void
dumpLabelSets(__pmContext *ctxp)
{
    int				lix;
    int				tix;
    unsigned int		type;
    unsigned int		ident;
    __pmHashCtl			*l_hashlabels;
    const __pmHashCtl		*l_hashtype;
    const __pmHashNode		*hp, *tp;
    const __pmHashNode		*this_item, *prev_item;
    const __pmLogLabelSet	*p;
    double			tdiff, min_diff;
    pmTimeval			this_stamp, prev_stamp;
    /* Print the label types in this order. */
    unsigned int labelTypes[] = {
	PM_LABEL_CONTEXT,
	PM_LABEL_DOMAIN,
	PM_LABEL_CLUSTER,
	PM_LABEL_ITEM,
	PM_LABEL_INDOM,
	PM_LABEL_INSTANCES
    };

    printf("\nMetric Labels in the Log ...\n");

    /*
     * In order to make the output more deterministic for testing,
     * output the help text sorted by
     *   time, then by
     *   type, then by
     *   identifier
     */
    l_hashlabels = &ctxp->c_archctl->ac_log->l_hashlabels;
    prev_stamp.tv_sec = 0;
    prev_stamp.tv_usec = 0;
    for (;;) {
	/* find the next earliest time stamp. */
	min_diff = DBL_MAX;
	for (lix = 0; lix < l_hashlabels->hsize; ++lix) {
	    for (hp = l_hashlabels->hash[lix]; hp != NULL; hp = hp->next) {
		l_hashtype = (__pmHashCtl *)hp->data;
		for (tix = 0; tix < l_hashtype->hsize; tix++) {
		    for (tp = l_hashtype->hash[tix]; tp != NULL; tp = tp->next) {
			for (p = (__pmLogLabelSet *)tp->data; p != NULL; p = p->next) {
			    tdiff = __pmTimevalSub(&p->stamp, &prev_stamp);
			    /*
			     * The chains are sorted in reverse chronological
			     * order so, if this time stamp is less than or
			     * equal to the previously printed one, we can stop
			     * looking.
			     */
			    if (tdiff <= 0.0)
				break;
			    /* Do we have a new candidate? */
			    if (tdiff < min_diff) {
				min_diff = tdiff;
				this_stamp = p->stamp;
			    }
			}
		    }
		}
	    }
	}
	if (min_diff == DBL_MAX)
	    break; /* done */

	/*
	 * Now print all the label sets at this time stamp.
	 * Sort by type and then identifier.
	 */
	__pmPrintTimeval(stdout, &this_stamp);
	putchar('\n');
	for (lix = 0; lix < sizeof(labelTypes) / sizeof(*labelTypes); ++lix) {
	    /* Are there labels of this type? */
	    type = labelTypes[lix];
	    hp = __pmHashSearch(type, l_hashlabels);
	    if (hp == NULL)
		continue;

	    /* Iterate over the label sets by increasing identifier. */
	    l_hashtype = (__pmHashCtl *)hp->data;
	    prev_item = NULL;
	    for (;;) {
		/*
		 * Search the hash of identifiers looking for the next lowest
		 * one.
		 */
		this_item = NULL;
		for (tix = 0; tix < l_hashtype->hsize; tix++) {
		    for (tp = l_hashtype->hash[tix]; tp != NULL; tp = tp->next) {
			ident = (unsigned int)tp->key;
			if (prev_item && ident <= (unsigned int)prev_item->key)
			    continue;
			if (!this_item || ident < (unsigned int)this_item->key)
			    this_item = tp;
		    }
		}
		if (this_item == NULL)
		    break; /* done */

		/*
		 * We've found the next lowest identifier.
		 * Print the labels for this type and identifier at the current
		 * time stamp.
		 */
		; 
		for (p = (__pmLogLabelSet *)this_item->data; p != NULL; p = p->next){
		    if (__pmTimevalSub(&p->stamp, &this_stamp) != 0.0)
			continue;
		    ident = (unsigned int)this_item->key;
		    pmPrintLabelSets(stdout, ident, type, p->labelsets, p->nsets);
		}

		prev_item = this_item;
	    }
	}<commit_msg>pmdumplog(1): Fix breakage caused by previous commit.<commit_after>/*
 * Copyright (c) 2014-2018 Red Hat.
 * Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program 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 General Public License
 * for more details.
 */

#include "pmapi.h"
#include "libpcp.h"
#include <ctype.h>
#include <limits.h>
#include <float.h>
#include <sys/stat.h>

static struct pmTimeval	pmtv;
static char		timebuf[32];	/* for pmCtime result + .xxx */
static int		numpmid;
static pmID		*pmid;
static pmID		pmid_flags;
static pmID		pmid_missed;
static int		sflag;
static int		xflag;		/* for -x (long timestamps) */
static pmLogLabel	label;

static pmLongOptions longopts[] = {
    PMAPI_OPTIONS_HEADER("Options"),
    PMOPT_DEBUG,
    { "all", 0, 'a', 0, "dump everything" },
    { "descs", 0, 'd', 0, "dump metric descriptions" },
    { "labelsets", 0, 'e', 0, "dump all metric label metadata" },
    { "helptext", 0, 'h', 0, "dump all metric help text" },
    { "insts", 0, 'i', 0, "dump instance domain descriptions" },
    { "", 0, 'L', 0, "more verbose form of archive label dump" },
    { "label", 0, 'l', 0, "dump the archive label" },
    { "metrics", 0, 'm', 0, "dump values of the metrics (default)" },
    PMOPT_NAMESPACE,
    { "reverse", 0, 'r', 0, "process archive in reverse chronological order" },
    PMOPT_START,
    { "sizes", 0, 's', 0, "report size of data records in archive" },
    PMOPT_FINISH,
    { "", 0, 't', 0, "dump the temporal index" },
    { "", 1, 'v', "FILE", "verbose hex dump of a physical file in raw format" },
    { "", 0, 'x', 0, "include date in reported timestamps" },
    PMOPT_TIMEZONE,
    PMOPT_HOSTZONE,
    PMOPT_VERSION,
    PMOPT_HELP,
    PMAPI_OPTIONS_END
};

static int overrides(int, pmOptions *);
static pmOptions opts = {
    .flags = PM_OPTFLAG_DONE | PM_OPTFLAG_STDOUT_TZ | PM_OPTFLAG_BOUNDARIES,
    .short_options = "aD:dehilLmMn:rS:sT:tv:xZ:z?",
    .long_options = longopts,
    .short_usage = "[options] [archive [metricname ...]]",
    .override = overrides,
};

/*
 * return -1, 0 or 1 as the struct timeval's compare
 * a < b, a == b or a > b
 */
static int
tvcmp(struct timeval a, struct timeval b)
{
    if (a.tv_sec < b.tv_sec)
	return -1;
    if (a.tv_sec > b.tv_sec)
	return 1;
    if (a.tv_usec < b.tv_usec)
	return -1;
    if (a.tv_usec > b.tv_usec)
	return 1;
    return 0;
}

static int
do_size(pmResult *rp)
{
    int		nbyte = 0;
    int		i;
    int		j;
    /*
     *  Externally the log record looks like this ...
     *  :----------:-----------:..........:---------:
     *  | int len  | timestamp | pmResult | int len |
     *  :----------:-----------:..........:---------:
     *
     * start with sizes of the header len, timestamp, numpmid, and
     * trailer len
     */
    nbyte = sizeof(int) + sizeof(pmTimeval) + sizeof(int);
    						/* len + timestamp + len */
    nbyte += sizeof(int);
    							/* numpmid */
    for (i = 0; i < rp->numpmid; i++) {
	pmValueSet	*vsp = rp->vset[i];
	nbyte += sizeof(pmID) + sizeof(int);		/* + pmid[i], numval */
	if (vsp->numval > 0) {
	    nbyte += sizeof(int);			/* + valfmt */
	    for (j = 0; j < vsp->numval; j++) {
		nbyte += sizeof(__pmValue_PDU);		/* + pmValue[j] */
		if (vsp->valfmt != PM_VAL_INSITU)
							/* + pmValueBlock */
							/* rounded up */
		    nbyte += PM_PDU_SIZE_BYTES(vsp->vlist[j].value.pval->vlen);
	    }
	}
    }

    return nbyte;
}

static void
setup_event_derived_metrics(void)
{
    int sts;

    if (pmid_flags == 0) {
	/*
	 * get PMID for event.flags and event.missed
	 * note that pmUnpackEventRecords() will have called
	 * __pmRegisterAnon(), so the anon metrics
	 * should now be in the PMNS
	 */
	char	*name_flags = "event.flags";
	char	*name_missed = "event.missed";

	if ((sts = pmLookupName(1, &name_flags, &pmid_flags)) < 0) {
	    /* should not happen! */
	    fprintf(stderr, "Warning: cannot get PMID for %s: %s\n",
		    name_flags, pmErrStr(sts));
	    /* avoid subsequent warnings ... */
	    pmid_flags = pmID_build(pmID_domain(pmid_flags), pmID_cluster(pmid_flags), 1);
	}
	sts = pmLookupName(1, &name_missed, &pmid_missed);
	if (sts < 0) {
	    /* should not happen! */
	    fprintf(stderr, "Warning: cannot get PMID for %s: %s\n",
		    name_missed, pmErrStr(sts));
	    /* avoid subsequent warnings ... */
	    pmid_missed = pmID_build(pmID_domain(pmid_missed), pmID_cluster(pmid_missed), 1);
	}
    }
}

static int
dump_nrecords(int nrecords, int nmissed)
{
    printf("%d", nrecords);
    if (nmissed > 0)
	printf(" (and %d missed)", nmissed);
    if (nrecords + nmissed == 1)
	printf(" event record\n");
    else
	printf(" event records\n");
    return 0;
}

static int
dump_nparams(int numpmid)
{
    if (numpmid == 0) {
	printf(" ---\n");
	printf("	          No parameters\n");
	return -1;
    }
    if (numpmid < 0) {
	printf(" ---\n");
	printf("	          Error: illegal number of parameters (%d)\n",
		numpmid);
	return -1;
    }
    return 0;
}

static void
dump_parameter(pmValueSet *xvsp, int index, int *flagsp)
{
    int		sts, flags = *flagsp;
    pmDesc	desc;
    char	**names;

    if ((sts = pmNameAll(xvsp->pmid, &names)) >= 0) {
	if (index == 0) {
	    if (xvsp->pmid == pmid_flags) {
		flags = *flagsp = xvsp->vlist[0].value.lval;
		printf(" flags 0x%x", flags);
		printf(" (%s) ---\n", pmEventFlagsStr(flags));
		free(names);
		return;
	    }
	    printf(" ---\n");
	}
	if ((flags & PM_EVENT_FLAG_MISSED) && index == 1 &&
	    (xvsp->pmid == pmid_missed)) {
	    printf("        ==> %d missed event records\n",
		    xvsp->vlist[0].value.lval);
	    free(names);
	    return;
	}
	printf("        %s (", pmIDStr(xvsp->pmid));
	__pmPrintMetricNames(stdout, sts, names, " or ");
	printf("):");
	free(names);
    }
    else
	printf("        PMID: %s:", pmIDStr(xvsp->pmid));
    if ((sts = pmLookupDesc(xvsp->pmid, &desc)) < 0) {
	printf(" pmLookupDesc: %s\n", pmErrStr(sts));
	return;
    }
    printf(" value ");
    pmPrintValue(stdout, xvsp->valfmt, desc.type, &xvsp->vlist[0], 1);
    putchar('\n');
}

static void
dump_event(int numnames, char **names, pmValueSet *vsp, int index, int indom, int type)
{
    int		r;		/* event records */
    int		p;		/* event parameters */
    int		flags;
    int		nrecords;
    int		nmissed = 0;
    int		highres = (type == PM_TYPE_HIGHRES_EVENT);
    char	*iname;
    pmValue	*vp = &vsp->vlist[index];

    printf("    %s (", pmIDStr(vsp->pmid));
    __pmPrintMetricNames(stdout, numnames, names, " or ");
    if (indom != PM_INDOM_NULL) {
	putchar('[');
	if (pmNameInDom(indom, vp->inst, &iname) < 0)
	    printf("%d or ???])", vp->inst);
	else {
	    printf("%d or \"%s\"])", vp->inst, iname);
	    free(iname);
	}
    }
    else {
	printf(")");
    }
    printf(": ");

    if (highres) {
	pmHighResResult	**hr;

	if ((nrecords = pmUnpackHighResEventRecords(vsp, index, &hr)) < 0)
	    return;
	if (nrecords == 0) {
	    printf("No event records\n");
	    pmFreeHighResEventResult(hr);
	    return;
	}
	setup_event_derived_metrics();

	for (r = 0; r < nrecords; r++) {
	    if (hr[r]->numpmid == 2 && hr[r]->vset[0]->pmid == pmid_flags &&
		(hr[r]->vset[0]->vlist[0].value.lval & PM_EVENT_FLAG_MISSED) &&
		hr[r]->vset[1]->pmid == pmid_missed) {
		nmissed += hr[r]->vset[1]->vlist[0].value.lval;
	    }
	}
	dump_nrecords(nrecords, nmissed);

	for (r = 0; r < nrecords; r++) {
	    printf("        --- event record [%d] timestamp ", r);
	    pmPrintHighResStamp(stdout, &hr[r]->timestamp);
	    if (dump_nparams(hr[r]->numpmid) < 0)
		continue;
	    flags = 0;
	    for (p = 0; p < hr[r]->numpmid; p++)
		dump_parameter(hr[r]->vset[p], p, &flags);
	}
	pmFreeHighResEventResult(hr);
    }
    else {
	pmResult	**res;

	if ((nrecords = pmUnpackEventRecords(vsp, index, &res)) < 0)
	    return;
	if (nrecords == 0) {
	    printf("No event records\n");
	    pmFreeEventResult(res);
	    return;
	}
	setup_event_derived_metrics();

	for (r = 0; r < nrecords; r++) {
	    if (res[r]->numpmid == 2 && res[r]->vset[0]->pmid == pmid_flags &&
		(res[r]->vset[0]->vlist[0].value.lval & PM_EVENT_FLAG_MISSED) &&
		res[r]->vset[1]->pmid == pmid_missed) {
		nmissed += res[r]->vset[1]->vlist[0].value.lval;
	    }
	}
	dump_nrecords(nrecords, nmissed);

	for (r = 0; r < nrecords; r++) {
	    printf("        --- event record [%d] timestamp ", r);
	    pmtv.tv_sec = res[r]->timestamp.tv_sec;
	    pmtv.tv_usec = res[r]->timestamp.tv_usec;
	    __pmPrintTimeval(stdout, &pmtv);
	    if (dump_nparams(res[r]->numpmid) < 0)
		continue;
	    flags = 0;
	    for (p = 0; p < res[r]->numpmid; p++)
		dump_parameter(res[r]->vset[p], p, &flags);
	}
	pmFreeEventResult(res);
    }
}

static void
dump_metric(int numnames, char **names, pmValueSet *vsp, int index, int indom, int type)
{
    pmValue	*vp = &vsp->vlist[index];
    char	*iname;

    if (index == 0) {
	printf("    %s (", pmIDStr(vsp->pmid));
	__pmPrintMetricNames(stdout, numnames, names, " or ");
	printf("):");
	if (vsp->numval > 1) {
	    putchar('\n');
	    printf("       ");
	}
    }
    else
	printf("       ");

    if (indom != PM_INDOM_NULL) {
	printf(" inst [");
	if (pmNameInDom(indom, vp->inst, &iname) < 0)
	    printf("%d or ???]", vp->inst);
	else {
	    printf("%d or \"%s\"]", vp->inst, iname);
	    free(iname);
	}
    }
    printf(" value ");
    pmPrintValue(stdout, vsp->valfmt, type, vp, 1);
    putchar('\n');
}

static void
dump_result(pmResult *resp)
{
    int		i;
    int		j;
    int		n;
    char	**names;
    pmDesc	desc;

    if (sflag) {
	int		nbyte;
	nbyte = do_size(resp);
	printf("[%d bytes]\n", nbyte);
    }

    if (xflag) {
	char	       *ddmm;
	char	       *yr;

	ddmm = pmCtime((const time_t *)&resp->timestamp.tv_sec, timebuf);
	ddmm[10] = '\0';
	yr = &ddmm[20];
	printf("%s ", ddmm);
	pmtv.tv_sec = resp->timestamp.tv_sec;
	pmtv.tv_usec = resp->timestamp.tv_usec;
	__pmPrintTimeval(stdout, &pmtv);
	printf(" %4.4s", yr);
	if (xflag >= 2)
	    printf(" (%.6f)", pmtimevalSub(&resp->timestamp, &label.ll_start));
    }
    else {
	pmtv.tv_sec = resp->timestamp.tv_sec;
	pmtv.tv_usec = resp->timestamp.tv_usec;
	__pmPrintTimeval(stdout, &pmtv);
    }

    if (resp->numpmid == 0) {
	printf("  <mark>\n");
	return;
    }
    printf(" %d metric", resp->numpmid);
    if (resp->numpmid == 0 || resp->numpmid > 1)
	putchar('s');
    putchar('\n');

    for (i = 0; i < resp->numpmid; i++) {
	pmValueSet	*vsp = resp->vset[i];

	names = NULL; /* silence coverity */
	n = pmNameAll(vsp->pmid, &names);
	if (vsp->numval == 0) {
	    printf("    %s (", pmIDStr(vsp->pmid));
	    __pmPrintMetricNames(stdout, n, names, " or ");
	    printf("): No values returned!\n");
	    goto next;
	}
	else if (vsp->numval < 0) {
	    printf("    %s (", pmIDStr(vsp->pmid));
	    __pmPrintMetricNames(stdout, n, names, " or ");
	    printf("): %s\n", pmErrStr(vsp->numval));
	    goto next;
	}

	if (pmLookupDesc(vsp->pmid, &desc) < 0) {
	    /* don't know, so punt on the most common cases */
	    desc.indom = PM_INDOM_NULL;
	    if (vsp->valfmt == PM_VAL_INSITU)
		desc.type = PM_TYPE_32;
	    else
		desc.type = PM_TYPE_AGGREGATE;
	}

	for (j = 0; j < vsp->numval; j++) {
	    if (desc.type == PM_TYPE_EVENT ||
		desc.type == PM_TYPE_HIGHRES_EVENT)
		dump_event(n, names, vsp, j, desc.indom, desc.type);
	    else
		dump_metric(n, names, vsp, j, desc.indom, desc.type);
	}
next:
	if (n > 0)
	    free(names);
    }
}

static void
dumpDesc(__pmContext *ctxp)
{
    int			i;
    int			sts;
    char		**names;
    __pmHashNode	*hp;
    pmDesc		*dp;

    printf("\nDescriptions for Metrics in the Log ...\n");
    for (i = 0; i < ctxp->c_archctl->ac_log->l_hashpmid.hsize; i++) {
	for (hp = ctxp->c_archctl->ac_log->l_hashpmid.hash[i]; hp != NULL; hp = hp->next) {
	    dp = (pmDesc *)hp->data;
	    names = NULL; /* silence coverity */
	    sts = pmNameAll(dp->pmid, &names);
	    if (sts < 0)
		printf("PMID: %s (%s)\n", pmIDStr(dp->pmid), "<noname>");
	    else {
		printf("PMID: %s (", pmIDStr(dp->pmid));
		__pmPrintMetricNames(stdout, sts, names, " or ");
		printf(")\n");
		free(names);
	    }
	    pmPrintDesc(stdout, dp);
	}
    }
}

static void
dumpInDom(__pmContext *ctxp)
{
    int		i;
    int		j;
    __pmHashNode	*hp;
    __pmLogInDom	*idp;
    __pmLogInDom	*ldp;
    printf("\nInstance Domains in the Log ...\n");
    for (i = 0; i < ctxp->c_archctl->ac_log->l_hashindom.hsize; i++) {
	for (hp = ctxp->c_archctl->ac_log->l_hashindom.hash[i]; hp != NULL; hp = hp->next) {
	    printf("InDom: %s\n", pmInDomStr((pmInDom)hp->key));
	    /*
	     * in reverse chronological order, so iteration is a bit funny
	     */
	    ldp = NULL;
	    for ( ; ; ) {
		for (idp = (__pmLogInDom *)hp->data; idp->next != ldp; idp =idp->next)
			;
		__pmPrintTimeval(stdout, &idp->stamp);
		printf(" %d instances\n", idp->numinst);
		for (j = 0; j < idp->numinst; j++) {
		    printf("   %d or \"%s\"\n",
			idp->instlist[j], idp->namelist[j]);
		}
		if (idp == (__pmLogInDom *)hp->data)
		    break;
		ldp = idp;
	    }
	}
    }
}

static void
dumpHelpText(__pmContext *ctxp)
{
    int			tix, cix, hix;
    unsigned int	type;
    unsigned int	class;
    unsigned int	ident;
    __pmHashCtl		*l_hashtext;
    const __pmHashCtl	*l_hashtype;
    const __pmHashNode	*hp, *tp;
    const __pmHashNode	*this_item[2], *prev_item[2];
    const char		*text;
    int textTypes[2]   = { PM_TEXT_PMID, PM_TEXT_INDOM };
    int textClasses[2] = { PM_TEXT_ONELINE, PM_TEXT_HELP };

    printf("\nMetric Help Text in the Log ...\n");

    /*
     * In order to make the output more deterministic for testing,
     * output the help text sorted by
     *   type (PM_TEXT_PMID, PM_TEXT_INDOM), then by
     *   identifier, then by
     *   class (PM_TEXT_ONELINE, PM_TEXT_HELP).
     */
    l_hashtext = &ctxp->c_archctl->ac_log->l_hashtext;
    for (tix = 0; tix < 2; ++tix) {
	type = textTypes[tix];

	/*
	 * Search the two hash tables representing the classes
	 * for this type to find the next lowest identifier.
	 */
	prev_item[0] = NULL;
	prev_item[1] = NULL;
	for (;;) {
	    for (cix = 0; cix < 2; ++cix) {
		this_item[cix] = NULL;

		class = textClasses[cix];
		hp = __pmHashSearch(type | class, l_hashtext);
		if (hp == NULL)
		    continue;

		l_hashtype = (__pmHashCtl *)hp->data;
		for (hix = 0; hix < l_hashtype->hsize; hix++) {
		    for (tp = l_hashtype->hash[hix]; tp != NULL; tp = tp->next) {
			ident = (unsigned int)tp->key;
			if (prev_item[cix] && ident <= (unsigned int)prev_item[cix]->key)
			    continue;
			if (!this_item[cix] || ident < (unsigned int)this_item[cix]->key)
			    this_item[cix] = tp;
		    }
		}
	    }

	    /* Are there any items left to print? */
	    if (this_item[0] == NULL && this_item[1] == NULL)
		break;

	    /*
	     * Print the item(s) with the lowest identifier.
	     * Careful. There will be either one or two items queued.
	     */
	    for (cix = 0; cix < 2; ++cix) {
		if (this_item[cix] == NULL)
		    continue;
		if (this_item[cix ^ 1] == NULL ||
		    ((unsigned int)this_item[cix]->key <=
		     (unsigned int)this_item[cix ^ 1]->key)) {
		    ident = (unsigned int)this_item[cix]->key;
		    text = (const char *)this_item[cix]->data;
		    if (type == PM_TEXT_PMID)
			printf("PMID: %s", pmIDStr((pmID)ident));
		    else if (type == PM_TEXT_INDOM)
			printf("InDom: %s", pmInDomStr((pmInDom)ident));
		    class = textClasses[cix];
		    if (class == PM_TEXT_ONELINE)
			printf(" [%s]", text);
		    else if (class == PM_TEXT_HELP)
			printf("\n%s", text);
		    putchar('\n');
		    
		    prev_item[cix] = this_item[cix];
		}
	    }
	}
    }
}

static void
dumpLabelSets(__pmContext *ctxp)
{
    int				lix;
    int				tix;
    unsigned int		type;
    unsigned int		ident;
    __pmHashCtl			*l_hashlabels;
    const __pmHashCtl		*l_hashtype;
    const __pmHashNode		*hp, *tp;
    const __pmHashNode		*this_item, *prev_item;
    const __pmLogLabelSet	*p;
    double			tdiff, min_diff;
    pmTimeval			this_stamp, prev_stamp;
    /* Print the label types in this order. */
    unsigned int labelTypes[] = {
	PM_LABEL_CONTEXT,
	PM_LABEL_DOMAIN,
	PM_LABEL_CLUSTER,
	PM_LABEL_ITEM,
	PM_LABEL_INDOM,
	PM_LABEL_INSTANCES
    };

    printf("\nMetric Labels in the Log ...\n");

    /*
     * In order to make the output more deterministic for testing,
     * output the help text sorted by
     *   time, then by
     *   type, then by
     *   identifier
     */
    l_hashlabels = &ctxp->c_archctl->ac_log->l_hashlabels;
    prev_stamp.tv_sec = 0;
    prev_stamp.tv_usec = 0;
    for (;;) {
	/* find the next earliest time stamp. */
	min_diff = DBL_MAX;
	for (lix = 0; lix < l_hashlabels->hsize; ++lix) {
	    for (hp = l_hashlabels->hash[lix]; hp != NULL; hp = hp->next) {
		l_hashtype = (__pmHashCtl *)hp->data;
		for (tix = 0; tix < l_hashtype->hsize; tix++) {
		    for (tp = l_hashtype->hash[tix]; tp != NULL; tp = tp->next) {
			for (p = (__pmLogLabelSet *)tp->data; p != NULL; p = p->next) {
			    tdiff = __pmTimevalSub(&p->stamp, &prev_stamp);
			    /*
			     * The chains are sorted in reverse chronological
			     * order so, if this time stamp is less than or
			     * equal to the previously printed one, we can stop
			     * looking.
			     */
			    if (tdiff <= 0.0)
				break;
			    /* Do we have a new candidate? */
			    if (tdiff < min_diff) {
				min_diff = tdiff;
				this_stamp = p->stamp;
			    }
			}
		    }
		}
	    }
	}
	if (min_diff == DBL_MAX)
	    break; /* done */

	/*
	 * Now print all the label sets at this time stamp.
	 * Sort by type and then identifier.
	 */
	__pmPrintTimeval(stdout, &this_stamp);
	putchar('\n');
	for (lix = 0; lix < sizeof(labelTypes) / sizeof(*labelTypes); ++lix) {
	    /* Are there labels of this type? */
	    type = labelTypes[lix];
	    hp = __pmHashSearch(type, l_hashlabels);
	    if (hp == NULL)
		continue;

	    /* Iterate over the label sets by increasing identifier. */
	    l_hashtype = (__pmHashCtl *)hp->data;
	    prev_item = NULL;
	    for (;;) {
		if (type == PM_LABEL_CONTEXT) {
		    /*
		     * All context labels have the same identifier within a
		     * single hash chain. Find it and Traverse it linearly.
		     */
		    if (prev_item == NULL) {
			for (tix = 0; tix < l_hashtype->hsize; tix++) {
			    this_item = l_hashtype->hash[tix];
			    if (this_item != NULL)
				break;
			}
		    }
		    else
			this_item = this_item->next;
		}
		else {
		    /*
		     * Search the hash of identifiers looking for the next lowest
		     * one.
		     */
		    this_item = NULL;
		    for (tix = 0; tix < l_hashtype->hsize; tix++) {
			for (tp = l_hashtype->hash[tix]; tp != NULL; tp = tp->next) {
			    ident = (unsigned int)tp->key;
			    if (prev_item && ident <= (unsigned int)prev_item->key)
				continue;
			    if (!this_item || ident < (unsigned int)this_item->key)
				this_item = tp;
			}
		    }
		}
		if (this_item == NULL)
		    break; /* done */

		/*
		 * We've found the next lowest identifier.
		 * Print the labels for this type and identifier at the current
		 * time stamp.
		 */
		; 
		for (p = (__pmLogLabelSet *)this_item->data; p != NULL; p = p->next){
		    if (__pmTimevalSub(&p->stamp, &this_stamp) != 0.0)
			continue;
		    ident = (unsigned int)this_item->key;
		    pmPrintLabelSets(stdout, ident, type, p->labelsets, p->nsets);
		}

		prev_item = this_item;
	    }
	}
{'fraction_non_alphanumeric': 0.16944051446945338, 'fraction_numerical': 0.008540192926045015, 'mean_word_length': 2.6954372623574145, 'pattern_counts': {'":': 2, '<': 104, '<?xml version=': 0, '>': 332, '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': '3276590', 'n_tokens_mistral': 16166, 'n_tokens_neox': 15062, 'n_words': 3718}