<commit_before>/*
 * Copyright (c) 2004 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.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include "common.h"
#include <sys/utsname.h>

typedef struct {
    int		fetched;
    int		err;
    kstat_t	*ksp;
} ctl_t;

static int		ncpu;
static int		hz;
static long		pagesize;
static cpu_stat_t	*cpustat;
static ctl_t		*ctl;
static char		uname_full[SYS_NMLN * 5];

void
sysinfo_init(int first)
{
    kstat_t	*ksp;
    int		i;
    char	buf[10];	/* cpuXXXXX */

    if (!first)
	/* TODO ... not sure if/when we'll use this re-init hook */
	return;

    for (ncpu = 0; ; ncpu++) {
	ksp = kstat_lookup(kc, "cpu_stat", ncpu, NULL);
	if (ksp == NULL) break;
	if ((ctl = (ctl_t *)realloc(ctl, (ncpu+1) * sizeof(ctl_t))) == NULL) {
	    fprintf(stderr, "sysinfo_init: ctl realloc[%d] @ cpu=%d failed: %s\n",
		(ncpu+1) * sizeof(ctl_t), ncpu, strerror(errno));
	    exit(1);
	}
	if ((cpustat = (cpu_stat_t *)realloc(cpustat, (ncpu+1) * sizeof(cpu_stat_t))) == NULL) {
	    fprintf(stderr, "sysinfo_init: cpustat realloc[%d] @ cpu=%d failed: %s\n",
		(ncpu+1) * sizeof(cpu_stat_t), ncpu, strerror(errno));
	    exit(1);
	}
	ctl[ncpu].ksp = ksp;
	ctl[ncpu].err = 0;
    }

    indomtab[CPU_INDOM].it_numinst = ncpu;
    indomtab[CPU_INDOM].it_set = (pmdaInstid *)malloc(ncpu * sizeof(pmdaInstid));
    /* TODO check? */

    for (i = 0; i < ncpu; i++) {
	indomtab[CPU_INDOM].it_set[i].i_inst = i;
	snprintf(buf, sizeof(buf), "cpu%d", i);
	indomtab[CPU_INDOM].it_set[i].i_name = strdup(buf);
	/* TODO check? */
    }

    hz = (int)sysconf(_SC_CLK_TCK);
    pagesize = sysconf(_SC_PAGESIZE);

#ifdef PCP_DEBUG
    if ((pmDebug & (DBG_TRACE_APPL0|DBG_TRACE_APPL2)) == (DBG_TRACE_APPL0|DBG_TRACE_APPL2)) {
	/* desperate */
	fprintf(stderr, "sysinfo: ncpu=%d hz=%d\n", ncpu, hz);
    }
#endif
}

static __uint32_t
sysinfo_derived(pmdaMetric *mdesc, int inst)
{
    pmID	pmid;
    __pmID_int	*ip = (__pmID_int *)&pmid;
    __uint32_t	val;

    pmid = mdesc->m_desc.pmid;
    ip->domain = 0;

    switch (pmid) {

	case PMDA_PMID(0,56):	/* hinv.ncpu */
	    if (inst == 0)
		val = ncpu;
	    else
		val = 0;
	    break;

	default:
	    fprintf(stderr, "cpu_derived: Botch: no method for pmid %s\n",
		pmIDStr(mdesc->m_desc.pmid));
	    val = 0;
	    break;
    }

#ifdef PCP_DEBUG
    if ((pmDebug & (DBG_TRACE_APPL0|DBG_TRACE_APPL2)) == (DBG_TRACE_APPL0|DBG_TRACE_APPL2)) {
	/* desperate */
	fprintf(stderr, "cpu_derived: pmid %s inst %d val %d\n",
	    pmIDStr(mdesc->m_desc.pmid), inst, val);
    }
#endif
    
    return val;
}

void
sysinfo_prefetch(void)
{
    int		i;

    for (i = 0; i < ncpu; i++)
	ctl[i].fetched = 0;
}

int
sysinfo_fetch(pmdaMetric *mdesc, int inst, pmAtomValue *atom)
{
    __uint64_t		ull;
    int			i;
    int			ok;
    int			offset;
    kstat_t		*ks;
    struct utsname	u;

    /* Special processing of metrics which notionally belong
     * to sysinfo category */
    switch (pmid_item(mdesc->m_desc.pmid)) {
    case 109: /* hinv.physmem */
	if ((ks = kstat_lookup(kc, "unix", -1, "system_pages")) != NULL) {
	    kstat_named_t *kn;

	    kstat_read(kc, ks, NULL);

	    if ((kn = kstat_data_lookup(ks, "physmem")) != NULL) {
		atom->ull = (((uint64_t)kn->value.ui32) * pagesize) >> 20;
		return 1;
	    }
	}
	return 0;

    case 108: /* hinv.pagesize */
	atom->ul = pagesize;
	return 1;

    case 107: /* pmda.uname */
	if (uname(&u) < 0) 
	    return 0;
<commit_msg>Don't assume size of values in unix/system_pages.<commit_after>/*
 * Copyright (c) 2004 <NAME>, 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.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include "common.h"
#include <sys/utsname.h>

typedef struct {
    int		fetched;
    int		err;
    kstat_t	*ksp;
} ctl_t;

static int		ncpu;
static int		hz;
static long		pagesize;
static cpu_stat_t	*cpustat;
static ctl_t		*ctl;
static char		uname_full[SYS_NMLN * 5];

void
sysinfo_init(int first)
{
    kstat_t	*ksp;
    int		i;
    char	buf[10];	/* cpuXXXXX */

    if (!first)
	/* TODO ... not sure if/when we'll use this re-init hook */
	return;

    for (ncpu = 0; ; ncpu++) {
	ksp = kstat_lookup(kc, "cpu_stat", ncpu, NULL);
	if (ksp == NULL) break;
	if ((ctl = (ctl_t *)realloc(ctl, (ncpu+1) * sizeof(ctl_t))) == NULL) {
	    fprintf(stderr, "sysinfo_init: ctl realloc[%d] @ cpu=%d failed: %s\n",
		(ncpu+1) * sizeof(ctl_t), ncpu, strerror(errno));
	    exit(1);
	}
	if ((cpustat = (cpu_stat_t *)realloc(cpustat, (ncpu+1) * sizeof(cpu_stat_t))) == NULL) {
	    fprintf(stderr, "sysinfo_init: cpustat realloc[%d] @ cpu=%d failed: %s\n",
		(ncpu+1) * sizeof(cpu_stat_t), ncpu, strerror(errno));
	    exit(1);
	}
	ctl[ncpu].ksp = ksp;
	ctl[ncpu].err = 0;
    }

    indomtab[CPU_INDOM].it_numinst = ncpu;
    indomtab[CPU_INDOM].it_set = (pmdaInstid *)malloc(ncpu * sizeof(pmdaInstid));
    /* TODO check? */

    for (i = 0; i < ncpu; i++) {
	indomtab[CPU_INDOM].it_set[i].i_inst = i;
	snprintf(buf, sizeof(buf), "cpu%d", i);
	indomtab[CPU_INDOM].it_set[i].i_name = strdup(buf);
	/* TODO check? */
    }

    hz = (int)sysconf(_SC_CLK_TCK);
    pagesize = sysconf(_SC_PAGESIZE);

#ifdef PCP_DEBUG
    if ((pmDebug & (DBG_TRACE_APPL0|DBG_TRACE_APPL2)) == (DBG_TRACE_APPL0|DBG_TRACE_APPL2)) {
	/* desperate */
	fprintf(stderr, "sysinfo: ncpu=%d hz=%d\n", ncpu, hz);
    }
#endif
}

static __uint32_t
sysinfo_derived(pmdaMetric *mdesc, int inst)
{
    pmID	pmid;
    __pmID_int	*ip = (__pmID_int *)&pmid;
    __uint32_t	val;

    pmid = mdesc->m_desc.pmid;
    ip->domain = 0;

    switch (pmid) {

	case PMDA_PMID(0,56):	/* hinv.ncpu */
	    if (inst == 0)
		val = ncpu;
	    else
		val = 0;
	    break;

	default:
	    fprintf(stderr, "cpu_derived: Botch: no method for pmid %s\n",
		pmIDStr(mdesc->m_desc.pmid));
	    val = 0;
	    break;
    }

#ifdef PCP_DEBUG
    if ((pmDebug & (DBG_TRACE_APPL0|DBG_TRACE_APPL2)) == (DBG_TRACE_APPL0|DBG_TRACE_APPL2)) {
	/* desperate */
	fprintf(stderr, "cpu_derived: pmid %s inst %d val %d\n",
	    pmIDStr(mdesc->m_desc.pmid), inst, val);
    }
#endif
    
    return val;
}

void
sysinfo_prefetch(void)
{
    int		i;

    for (i = 0; i < ncpu; i++)
	ctl[i].fetched = 0;
}

int
sysinfo_fetch(pmdaMetric *mdesc, int inst, pmAtomValue *atom)
{
    __uint64_t		ull;
    int			i;
    int			ok;
    int			offset;
    kstat_t		*ks;
    struct utsname	u;

    /* Special processing of metrics which notionally belong
     * to sysinfo category */
    switch (pmid_item(mdesc->m_desc.pmid)) {
    case 109: /* hinv.physmem */
	if ((ks = kstat_lookup(kc, "unix", -1, "system_pages")) != NULL) {
	    kstat_named_t *kn;

	    kstat_read(kc, ks, NULL);

	    if ((kn = kstat_data_lookup(ks, "physmem")) != NULL) {
		switch (kn->data_type) {
		case KSTAT_DATA_UINT64:
		    atom->ull = kn->value.ui64;
		    break;
		case KSTAT_DATA_INT64:
		    atom->ull = kn->value.i64;
		    break;
		case KSTAT_DATA_UINT32:
		    atom->ull = kn->value.ui32;
		    break;
		case KSTAT_DATA_INT32:
		    atom->ull = kn->value.i32;
		    break;
		default:
		    return 0;
		}

		atom->ull = (atom->ull * pagesize) >> 20;
		return 1;
	    }
	}
	return 0;

    case 108: /* hinv.pagesize */
	atom->ul = pagesize;
	return 1;

    case 107: /* pmda.uname */
	if (uname(&u) < 0) 
	    return 0;

{'fraction_non_alphanumeric': 0.142957416939635, 'fraction_numerical': 0.019068788020589613, 'mean_word_length': 3.129951690821256, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 35, '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': '1851967', 'n_tokens_mistral': 3617, 'n_tokens_neox': 3386, 'n_words': 884}