<commit_before>#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/param.h>
#include <sys/stat.h>

#include "sigar.h"
#include "sigar_private.h"
#include "sigar_os.h"
#include "sigar_util.h"

#include <asm/page.h> /* for PAGE_SHIFT */

#define pageshift(x) ((x) << PAGE_SHIFT)

#define PROC_MEMINFO PROC_FS_ROOT "meminfo"
#define PROC_STAT    PROC_FS_ROOT "stat"
#define PROC_UPTIME  PROC_FS_ROOT "uptime"
#define PROC_LOADAVG PROC_FS_ROOT "loadavg"

#define PROC_PSTAT   "/stat"
#define PROC_PSTATUS "/status"

/*
 * /proc/self/stat fields:
 * 1 - pid
 * 2 - comm
 * 3 - state
 * 4 - ppid
 * 5 - pgrp
 * 6 - session
 * 7 - tty_nr
 * 8 - tpgid
 * 9 - flags
 * 10 - minflt
 * 11 - cminflt
 * 12 - majflt
 * 13 - cmajflt
 * 14 - utime
 * 14 - stime
 * 15 - cutime
 * 16 - cstime
 * 17 - priority
 * 18 - nice
 * 19 - 0 (removed field)
 * 20 - itrealvalue
 * 21 - starttime
 * 22 - vsize
 * 23 - rss
 * 24 - rlim
 * 25 - startcode
 * 26 - endcode
 * 27 - startstack
 * 28 - kstkesp
 * 29 - kstkeip
 * 30 - signal
 * 31 - blocked
 * 32 - sigignore
 * 33 - sigcache
 * 34 - wchan
 * 35 - nswap
 * 36 - cnswap
 * 37 - exit_signal <-- looking for this.
 * 38 - processor
 * ... more for newer RH
 */

#define PROC_SIGNAL_IX 38

static int get_proc_signal_offset(void)
{
    char buffer[BUFSIZ], *ptr=buffer;
    int fields = 0;
    int status = sigar_file2str("/proc/self/stat",
                                buffer, sizeof(buffer));

    if (status != SIGAR_OK) {
        return 1;
    }

    while (*ptr) {
        if (*ptr++ == ' ') {
            fields++;
        }
    }

    return (fields - PROC_SIGNAL_IX) + 1;
}

sigar_pid_t sigar_pid_get(sigar_t *sigar)
{
    /* XXX cannot safely cache getpid unless using nptl */
    /* we can however, cache it for optimizations in the
     * case of proc_env_get for example.
     */
    sigar->pid = getpid();
    return sigar->pid;
}

int sigar_os_open(sigar_t **sigar)
{
    char buffer[BUFSIZ], *ptr;
    int status = sigar_file2str(PROC_STAT, buffer, sizeof(buffer));

    *sigar = malloc(sizeof(**sigar));

    if (status != SIGAR_OK) {
        return status;
    }

    ptr = strstr(buffer, "\nbtime");
    ptr = sigar_skip_token(ptr);
    (*sigar)->boot_time = sigar_strtoul(ptr);

    (*sigar)->ticks = 100; /* HZ */

    (*sigar)->ram = -1;

    (*sigar)->proc_signal_offset = -1;

    (*sigar)->last_proc_stat.pid = -1;

    (*sigar)->ht_enabled = -1;

    return SIGAR_OK;
}<commit_msg>use sysconf to get ticks<commit_after>#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/param.h>
#include <sys/stat.h>

#include "sigar.h"
#include "sigar_private.h"
#include "sigar_os.h"
#include "sigar_util.h"

#include <asm/page.h> /* for PAGE_SHIFT */

#define pageshift(x) ((x) << PAGE_SHIFT)

#define PROC_MEMINFO PROC_FS_ROOT "meminfo"
#define PROC_STAT    PROC_FS_ROOT "stat"
#define PROC_UPTIME  PROC_FS_ROOT "uptime"
#define PROC_LOADAVG PROC_FS_ROOT "loadavg"

#define PROC_PSTAT   "/stat"
#define PROC_PSTATUS "/status"

/*
 * /proc/self/stat fields:
 * 1 - pid
 * 2 - comm
 * 3 - state
 * 4 - ppid
 * 5 - pgrp
 * 6 - session
 * 7 - tty_nr
 * 8 - tpgid
 * 9 - flags
 * 10 - minflt
 * 11 - cminflt
 * 12 - majflt
 * 13 - cmajflt
 * 14 - utime
 * 14 - stime
 * 15 - cutime
 * 16 - cstime
 * 17 - priority
 * 18 - nice
 * 19 - 0 (removed field)
 * 20 - itrealvalue
 * 21 - starttime
 * 22 - vsize
 * 23 - rss
 * 24 - rlim
 * 25 - startcode
 * 26 - endcode
 * 27 - startstack
 * 28 - kstkesp
 * 29 - kstkeip
 * 30 - signal
 * 31 - blocked
 * 32 - sigignore
 * 33 - sigcache
 * 34 - wchan
 * 35 - nswap
 * 36 - cnswap
 * 37 - exit_signal <-- looking for this.
 * 38 - processor
 * ... more for newer RH
 */

#define PROC_SIGNAL_IX 38

static int get_proc_signal_offset(void)
{
    char buffer[BUFSIZ], *ptr=buffer;
    int fields = 0;
    int status = sigar_file2str("/proc/self/stat",
                                buffer, sizeof(buffer));

    if (status != SIGAR_OK) {
        return 1;
    }

    while (*ptr) {
        if (*ptr++ == ' ') {
            fields++;
        }
    }

    return (fields - PROC_SIGNAL_IX) + 1;
}

sigar_pid_t sigar_pid_get(sigar_t *sigar)
{
    /* XXX cannot safely cache getpid unless using nptl */
    /* we can however, cache it for optimizations in the
     * case of proc_env_get for example.
     */
    sigar->pid = getpid();
    return sigar->pid;
}

int sigar_os_open(sigar_t **sigar)
{
    char buffer[BUFSIZ], *ptr;
    int status = sigar_file2str(PROC_STAT, buffer, sizeof(buffer));

    *sigar = malloc(sizeof(**sigar));

    if (status != SIGAR_OK) {
        return status;
    }

    ptr = strstr(buffer, "\nbtime");
    ptr = sigar_skip_token(ptr);
    (*sigar)->boot_time = sigar_strtoul(ptr);

    (*sigar)->ticks = sysconf(_SC_CLK_TCK);

    (*sigar)->ram = -1;

    (*sigar)->proc_signal_offset = -1;

    (*sigar)->last_proc_stat.pid = -1;

    (*sigar)->ht_enabled = -1;

    return SIGAR_OK;
}
{'fraction_non_alphanumeric': 0.1454107250451898, 'fraction_numerical': 0.033139184575215905, 'mean_word_length': 2.4728033472803346, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 33, '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': '1523091', 'n_tokens_mistral': 2108, 'n_tokens_neox': 1896, 'n_words': 479}