<commit_before>#include <stdio.h>
#include <unistd.h>
#include "rapl.h"

#define MAX_PACKAGES 32

static int fd_msr[MAX_PACKAGES];
static struct rapl_units r_units;
static int units_ok;
static int num_packages;

static int get_kernel_nr_cpus(void)
{
    int nr_cpus = 1;
    FILE *fff = fopen("/sys/devices/system/cpu/kernel_max","r");
    if (fff == NULL)
        return nr_cpus;

    int num_read = fscanf(fff, "%d", &nr_cpus);
    fclose(fff);
    return (num_read == 1) ? (nr_cpus + 1) : 1;
}

void criterion_initrapl(void)
{
    int nr_cpus = get_kernel_nr_cpus();
    int cpu_to_use[nr_cpus];

    int package;
    char filename[256];
    num_packages = 0;

    // Fill with placeholders
    for (int i = 0; i < nr_cpus; i++)
        cpu_to_use[i] = -1;

    int j = 0;
    // Detect the amount of packages
    while (1) {
       sprintf(filename, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", j);
       FILE *fff = fopen(filename, "r");
       if (fff == NULL)
           break;

       int num_read = fscanf(fff, "%d", &package);
       fclose(fff);
       if (num_read != 1)
           break;

       // Check if a new package
       if ((package >= 0) && (package < nr_cpus)) {
           if (cpu_to_use[package] == -1) {
               cpu_to_use[package] = j;
               num_packages++;
           }
       } else {
           perror("Package outside of allowed range\n");
           break;
       }

       j++;
    }

    printf("Total packages found: %d\n", num_packages);

    for (int i = 0; i < num_packages; i++) {
        fd_msr[i] = rapl_open_msr(cpu_to_use[i]);
        if (fd_msr[i] <= 0) {
            char msg[256];
            sprintf(msg, "Could not open msr %d\n", cpu_to_use[i]);
            perror(msg);
        }
    }

    units_ok = rapl_get_units(fd_msr[0], &r_units);
}

double criterion_getenergypacket(void)
{
    if (!units_ok) {
        perror("Invalid units!");
        return -1;
    }

    struct rapl_raw_power_counters rpc;
    double total_package_energy = 0.0;

    for (int i = 0; i < num_packages; i++) {
        rapl_get_raw_power_counters(fd_msr[i], &r_units, &rpc);
        total_package_energy += rpc.pkg;
    }

    return total_package_energy;
}

void criterion_finishrapl(void)
{
    for (int i = 0; i < num_packages; i++)
        close(fd_msr[i]);
}<commit_msg>Fix warnings for cbits/energy_rapl.c<commit_after>#include <stdio.h>
#include <unistd.h>
#include "rapl.h"

#define MAX_PACKAGES 32

static int fd_msr[MAX_PACKAGES];
static struct rapl_units r_units;
static int units_ok;
static int num_packages;

static int get_kernel_nr_cpus(void)
{
    int nr_cpus = 1;
    FILE *fff = fopen("/sys/devices/system/cpu/kernel_max","r");
    if (fff == NULL)
        return nr_cpus;

    int num_read = fscanf(fff, "%d", &nr_cpus);
    fclose(fff);
    return (num_read == 1) ? (nr_cpus + 1) : 1;
}

void criterion_initrapl(void)
{
    int nr_cpus = get_kernel_nr_cpus();
    int cpu_to_use[nr_cpus];

    int package;
    char filename[256];
    num_packages = 0;

    // Fill with placeholders
    int i;
    for (i = 0; i < nr_cpus; i++)
        cpu_to_use[i] = -1;

    int j = 0;
    // Detect the amount of packages
    while (1) {
       sprintf(filename, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", j);
       FILE *fff = fopen(filename, "r");
       if (fff == NULL)
           break;

       int num_read = fscanf(fff, "%d", &package);
       fclose(fff);
       if (num_read != 1)
           break;

       // Check if a new package
       if ((package >= 0) && (package < nr_cpus)) {
           if (cpu_to_use[package] == -1) {
               cpu_to_use[package] = j;
               num_packages++;
           }
       } else {
           perror("Package outside of allowed range\n");
           break;
       }

       j++;
    }

    printf("Total packages found: %d\n", num_packages);

    for (i = 0; i < num_packages; i++) {
        fd_msr[i] = rapl_open_msr(cpu_to_use[i]);
        if (fd_msr[i] <= 0) {
            char msg[256];
            sprintf(msg, "Could not open msr %d\n", cpu_to_use[i]);
            perror(msg);
        }
    }

    units_ok = rapl_get_units(fd_msr[0], &r_units);
}

double criterion_getenergypacket(void)
{
    if (!units_ok) {
        perror("Invalid units!");
        return -1;
    }

    struct rapl_raw_power_counters rpc;
    double total_package_energy = 0.0;

    int i;
    for (i = 0; i < num_packages; i++) {
        rapl_get_raw_power_counters(fd_msr[i], &r_units, &rpc);
        total_package_energy += rpc.pkg;
    }

    return total_package_energy;
}

void criterion_finishrapl(void)
{
    int i;
    for (i = 0; i < num_packages; i++)
        close(fd_msr[i]);
}
{'fraction_non_alphanumeric': 0.13761662425784563, 'fraction_numerical': 0.011874469889737066, 'mean_word_length': 2.280250347705146, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 9, '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': '2927701', 'n_tokens_mistral': 1815, 'n_tokens_neox': 1727, 'n_words': 397}