<commit_before>/*
 * Copyright (C) 2013-2015,2017 Red Hat.
 *
 * This file is part of the "pcp" module, the python interfaces for the
 * Performance Co-Pilot toolkit.
 *
 * 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.
 */

/**************************************************************************\
**                                                                        **
** This C extension module mainly serves the purpose of loading functions **
** and macros needed to implement PMDAs in python.  These are exported to **
** python PMDAs via the pmda.py module, using ctypes.                     **
**                                                                        **
\**************************************************************************/

#include <Python.h>
#include "pmapi.h"
#include "pmda.h"
#include "libpcp.h"

#if PY_MAJOR_VERSION >= 3
#define MOD_ERROR_VAL NULL
#define MOD_SUCCESS_VAL(val) val
#define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
#define MOD_DEF(ob, name, doc, methods) \
        static struct PyModuleDef moduledef = { \
          PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
        ob = PyModule_Create(&moduledef);
#else
#define MOD_ERROR_VAL
#define MOD_SUCCESS_VAL(val)
#define MOD_INIT(name) void init##name(void)
#define MOD_DEF(ob, name, doc, methods) \
        ob = Py_InitModule3(name, methods, doc);
#endif

static pmdaInterface dispatch;
static __pmnsTree *pmns;
static int need_refresh;
static PyObject *indom_list;	  	/* indom list */
static PyObject *metric_list;	  	/* metric list */
static PyObject *pmns_dict;		/* metric pmid:names dictionary */
static PyObject *pmid_oneline_dict;	/* metric pmid:short text */
static PyObject *pmid_longtext_dict;	/* metric pmid:long help */
static PyObject *indom_oneline_dict;	/* indom pmid:short text */
static PyObject *indom_longtext_dict;	/* indom pmid:long help */

static PyObject *fetch_func;
static PyObject *label_func;
static PyObject *refresh_func;
static PyObject *instance_func;
static PyObject *store_cb_func;
static PyObject *fetch_cb_func;
static PyObject *label_cb_func;
static PyObject *refresh_all_func;
static PyObject *refresh_metrics_func;

static Py_ssize_t nindoms;
static pmdaIndom *indom_buffer;
static Py_ssize_t nmetrics;
static pmdaMetric *metric_buffer;

#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION <= 5
typedef int Py_ssize_t;
#endif

static void pmns_refresh(void);
static void pmda_refresh_metrics(void);

static void
maybe_refresh_all(void)
{
    // Call the refresh metrics hook (if it exists).
    if (refresh_metrics_func) {
	PyObject *arglist, *result;

	arglist = Py_BuildValue("()");
	if (arglist == NULL)
	    return;
	result = PyEval_CallObject(refresh_metrics_func, arglist);
	Py_DECREF(arglist);
	// Just ignore the result.
	Py_DECREF(result);
    }

    if (need_refresh) {
	pmns_refresh();
	pmda_refresh_metrics();
	need_refresh = 0;
    }
}

static void
pmns_refresh(void)
{
    int sts, count = 0;
    Py_ssize_t pos = 0;
    PyObject *key, *value;

    if (pmDebugOptions.libpmda)
        fprintf(stderr, "pmns_refresh: rebuilding namespace\n");

    // If there is nothing to do, just exit.<commit_msg>python pmda: tolerate exceptions thrown from refresh_all upcall<commit_after>/*
 * Copyright (C) 2013-2015,2017 Red Hat.
 *
 * This file is part of the "pcp" module, the python interfaces for the
 * Performance Co-Pilot toolkit.
 *
 * 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.
 */

/**************************************************************************\
**                                                                        **
** This C extension module mainly serves the purpose of loading functions **
** and macros needed to implement PMDAs in python.  These are exported to **
** python PMDAs via the pmda.py module, using ctypes.                     **
**                                                                        **
\**************************************************************************/

#include <Python.h>
#include "pmapi.h"
#include "pmda.h"
#include "libpcp.h"

#if PY_MAJOR_VERSION >= 3
#define MOD_ERROR_VAL NULL
#define MOD_SUCCESS_VAL(val) val
#define MOD_INIT(name) PyMODINIT_FUNC PyInit_##name(void)
#define MOD_DEF(ob, name, doc, methods) \
        static struct PyModuleDef moduledef = { \
          PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
        ob = PyModule_Create(&moduledef);
#else
#define MOD_ERROR_VAL
#define MOD_SUCCESS_VAL(val)
#define MOD_INIT(name) void init##name(void)
#define MOD_DEF(ob, name, doc, methods) \
        ob = Py_InitModule3(name, methods, doc);
#endif

static pmdaInterface dispatch;
static __pmnsTree *pmns;
static int need_refresh;
static PyObject *indom_list;	  	/* indom list */
static PyObject *metric_list;	  	/* metric list */
static PyObject *pmns_dict;		/* metric pmid:names dictionary */
static PyObject *pmid_oneline_dict;	/* metric pmid:short text */
static PyObject *pmid_longtext_dict;	/* metric pmid:long help */
static PyObject *indom_oneline_dict;	/* indom pmid:short text */
static PyObject *indom_longtext_dict;	/* indom pmid:long help */

static PyObject *fetch_func;
static PyObject *label_func;
static PyObject *refresh_func;
static PyObject *instance_func;
static PyObject *store_cb_func;
static PyObject *fetch_cb_func;
static PyObject *label_cb_func;
static PyObject *refresh_all_func;
static PyObject *refresh_metrics_func;

static Py_ssize_t nindoms;
static pmdaIndom *indom_buffer;
static Py_ssize_t nmetrics;
static pmdaMetric *metric_buffer;

#if PY_MAJOR_VERSION == 2 && PY_MINOR_VERSION <= 5
typedef int Py_ssize_t;
#endif

static void pmns_refresh(void);
static void pmda_refresh_metrics(void);

static void
maybe_refresh_all(void)
{
    // Call the refresh metrics hook (if it exists).
    if (refresh_metrics_func) {
	PyObject *arglist, *result;

	arglist = Py_BuildValue("()");
	if (arglist == NULL)
	    return;
	result = PyEval_CallObject(refresh_metrics_func, arglist);
	Py_DECREF(arglist);
        if (result == NULL)
            PyErr_Print();
        else
            // Just ignore the result.
            Py_DECREF(result);
    }

    if (need_refresh) {
	pmns_refresh();
	pmda_refresh_metrics();
	need_refresh = 0;
    }
}

static void
pmns_refresh(void)
{
    int sts, count = 0;
    Py_ssize_t pos = 0;
    PyObject *key, *value;

    if (pmDebugOptions.libpmda)
        fprintf(stderr, "pmns_refresh: rebuilding namespace\n");

    // If there is nothing to do, just exit.
{'fraction_non_alphanumeric': 0.12858689767190037, 'fraction_numerical': 0.00568489442338928, 'mean_word_length': 3.595149253731343, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5855374', 'n_tokens_mistral': 2247, 'n_tokens_neox': 2203, 'n_words': 721}