<commit_before>#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include "stats.h"
#include "collect.h"
#include "trace.h"

const char *perfquery = "/opt/ofed/bin/perfquery";

/* TODO *_data values count 4 octets. */

/* Fields for mlx4, mthca: */
#define IB_KEYS \
  X(excessive_buffer_overrun_errors), \
  X(link_downed), \
  X(link_error_recovery), \
  X(local_link_integrity_errors), \
  X(port_rcv_constraint_errors), \
  X(port_rcv_data), \
  X(port_rcv_errors), \
  X(port_rcv_packets), \
  X(port_rcv_remote_physical_errors), \
  X(port_rcv_switch_relay_errors), \
  X(port_xmit_constraint_errors), \
  X(port_xmit_data), \
  X(port_xmit_discards), \
  X(port_xmit_packets), \
  X(port_xmit_wait), \
  X(symbol_error), \
  X(VL15_dropped)

static void collect_ib_dev(struct stats_type *type, const char *dev)
{
  int port;
  for (port = 1; port <= 2; port++) {
    char path[80], id[80], cmd[160];
    FILE *file = NULL;
    unsigned int lid;
    struct stats *stats = NULL;

    snprintf(path, sizeof(path), "/sys/class/infiniband/%s/ports/%i/state", dev, port);
    file = fopen(path, "r");
    if (file == NULL)
      goto next; /* ERROR("cannot open `%s': %m\n", path); */

    char buf[80] = { 0 };
    if (fgets(buf, sizeof(buf), file) == NULL)
      goto next;

    fclose(file);
    file = NULL;

    if (strstr(buf, "ACTIVE") == NULL)
      goto next;

    TRACE("dev %s, port %i\n", dev, port);

    snprintf(id, sizeof(id), "%s.%i", dev, port); /* XXX */
    stats = get_current_stats(type, id);
    if (stats == NULL)
      goto next;

    snprintf(path, sizeof(path), "/sys/class/infiniband/%s/ports/%i/counters", dev, port);
    collect_key_value_dir(stats, path);

    /* Get the LID for perfquery. */
    snprintf(path, sizeof(path), "/sys/class/infiniband/%s/ports/%i/lid", dev, port);
    file = fopen(path, "r");
    if (fscanf(file, "%x", &lid) != 1)
      goto next;

    fclose(file);
    file = NULL;

    /* Call perfquery to clear stats.  Blech! */
    snprintf(cmd, sizeof(cmd), "%s -R %#x %d", perfquery, lid, port);
    int rc = system(cmd);
    TRACE("system `%s' returned %d\n", cmd, rc);

  next:
    if (file != NULL)
      fclose(file);
    file = NULL;
  }
}<commit_msg>Fix unused variable when DEBUG is not defined.<commit_after>#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include "stats.h"
#include "collect.h"
#include "trace.h"

const char *perfquery = "/opt/ofed/bin/perfquery";

/* TODO *_data values count 4 octets. */

/* Fields for mlx4, mthca: */
#define IB_KEYS \
  X(excessive_buffer_overrun_errors), \
  X(link_downed), \
  X(link_error_recovery), \
  X(local_link_integrity_errors), \
  X(port_rcv_constraint_errors), \
  X(port_rcv_data), \
  X(port_rcv_errors), \
  X(port_rcv_packets), \
  X(port_rcv_remote_physical_errors), \
  X(port_rcv_switch_relay_errors), \
  X(port_xmit_constraint_errors), \
  X(port_xmit_data), \
  X(port_xmit_discards), \
  X(port_xmit_packets), \
  X(port_xmit_wait), \
  X(symbol_error), \
  X(VL15_dropped)

static void collect_ib_dev(struct stats_type *type, const char *dev)
{
  int port;
  for (port = 1; port <= 2; port++) {
    char path[80], id[80], cmd[160];
    FILE *file = NULL;
    unsigned int lid;
    struct stats *stats = NULL;

    snprintf(path, sizeof(path), "/sys/class/infiniband/%s/ports/%i/state", dev, port);
    file = fopen(path, "r");
    if (file == NULL)
      goto next; /* ERROR("cannot open `%s': %m\n", path); */

    char buf[80] = { 0 };
    if (fgets(buf, sizeof(buf), file) == NULL)
      goto next;

    fclose(file);
    file = NULL;

    if (strstr(buf, "ACTIVE") == NULL)
      goto next;

    TRACE("dev %s, port %i\n", dev, port);

    snprintf(id, sizeof(id), "%s.%i", dev, port); /* XXX */
    stats = get_current_stats(type, id);
    if (stats == NULL)
      goto next;

    snprintf(path, sizeof(path), "/sys/class/infiniband/%s/ports/%i/counters", dev, port);
    collect_key_value_dir(stats, path);

    /* Get the LID for perfquery. */
    snprintf(path, sizeof(path), "/sys/class/infiniband/%s/ports/%i/lid", dev, port);
    file = fopen(path, "r");
    if (fscanf(file, "%x", &lid) != 1)
      goto next;

    fclose(file);
    file = NULL;

    /* Call perfquery to clear stats.  Blech! */
    snprintf(cmd, sizeof(cmd), "%s -R %#x %d", perfquery, lid, port);
    int cmd_rc = system(cmd);
    if (cmd_rc != 0)
      ERROR("`%s' exited with status %d\n", cmd, cmd_rc);

  next:
    if (file != NULL)
      fclose(file);
    file = NULL;
  }
}
{'fraction_non_alphanumeric': 0.17447179263776955, 'fraction_numerical': 0.007623611413635374, 'mean_word_length': 3.2283609576427255, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '4947541', 'n_tokens_mistral': 1854, 'n_tokens_neox': 1779, 'n_words': 405}