<commit_before>/*
 *
 * honggfuzz - utilities
 * -----------------------------------------
 *
 * Author: <NAME> <<EMAIL>>
 *
 * Copyright 2010-2018 by Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 */

#include "libhfcommon/util.h"

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#if !defined(_HF_ARCH_DARWIN) && !defined(__CYGWIN__)
#include <link.h>
#endif /* !defined(_HF_ARCH_DARWIN) && !defined(__CYGWIN__) */
#include <math.h>
#include <pthread.h>
#if defined(_HF_ARCH_LINUX)
#include <sched.h>
#endif /* defined(_HF_ARCH_LINUX) */
#if defined(__FreeBSD__)
#include <sys/param.h>
#include <sys/cpuset.h>
#include <pthread_np.h>
#define cpu_set_t cpuset_t
#endif
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#if defined(_HF_ARCH_LINUX)
#include <sys/prctl.h>
#endif /* defined(_HF_ARCH_LINUX) */
#if defined(__FreeBSD__)
#include <sys/procctl.h>
#endif /* defined(__FreeBSD__) */
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#include "libhfcommon/common.h"
#include "libhfcommon/files.h"
#include "libhfcommon/log.h"

void util_ParentDeathSigIfAvail(int signo HF_ATTR_UNUSED) {
#if defined(__FreeBSD__)
    if (procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &signo) == -1) {
        PLOG_W("procctl(P_PID, PROC_PDEATHSIG_CTL, signo=%d (%s))", signo, util_sigName(signo));
    }
#endif /* defined(__FreeBSD__) */
#if defined(_HF_ARCH_LINUX)
    if (prctl(PR_SET_PDEATHSIG, (unsigned long)signo, 0UL, 0UL, 0UL) == -1) {
        PLOG_W("prctl(PR_SET_PDEATHSIG, signo=%d (%s))", signo, util_sigName(signo));
    }
#endif /* defined(_HF_ARCH_LINUX) */
}

bool util_PinThreadToCPUs(uint32_t threadno, uint32_t cpucnt) {
    if (cpucnt == 0) {
        return true;
    }

    long num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
    if (num_cpus == -1) {
        PLOG_W("sysconf(_SC_NPROCESSORS_ONLN) failed");
        return false;
    }

    uint32_t start_cpu = (threadno * cpucnt) % (uint32_t)num_cpus;
    uint32_t end_cpu   = (threadno * cpucnt + cpucnt - 1U) % (uint32_t)num_cpus;

    LOG_D("Setting CPU affinity for the current thread #%" PRIu32 " to %" PRId32
          " consecutive CPUs, (start:%" PRIu32 "-end:%" PRIu32 ") total_cpus:%ld",
        threadno, cpucnt, start_cpu, end_cpu, num_cpus);

    if (cpucnt > (uint32_t)num_cpus) {
        LOG_W("Number of requested CPUs (%" PRId32
              ") is bigger than number of available CPUs (%ld)",
            cpucnt, num_cpus);
        return false;
    }

#if defined(_HF_ARCH_LINUX) || defined(__FreeBSD__)
    cpu_set_t set;
    CPU_ZERO(&set);

    for (uint32_t i = 0; i < cpucnt; i++) {
        CPU_SET((start_cpu + i) % num_cpus, &set);
    }

#if defined(_HF_ARCH_LINUX)
    if (sched_setaffinity(gettid(), sizeof(set), &set) == -1) {
        PLOG_W("sched_setaffinity(tid=%d, sizeof(set)) failed", (int)gettid());
#elif defined(__FreeBSD__)
    if (cpuset_setaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(set), &set) == -1) {
        PLOG_W("cpuset_setaffinity(tid=%d, sizeof(set)) failed", (int)pthread_getthreadid_np());
#endif
        return false;
    }
#endif /* defined(_HF_ARCH_LINUX) */
    return true;
}

void* util_Malloc(size_t sz) {
    void* p = malloc(sz);
    if (p == NULL) {
        LOG_F("malloc(size='%zu')", sz);
    }
    return p;
}

void* util_Calloc(size_t sz) {
    void* p = util_Malloc(sz);
    memset(p, '\0', sz);
    return p;
}

void* util_AllocCopy(const uint8_t* ptr, size_t sz) {
    void* p = util_Malloc(sz);
    memcpy(p, ptr, sz);
    return p;
}<commit_msg>libhfcommon/util: unify util_PinThreadToCPUs across Linux/FreeBSD/NetBSD<commit_after>/*
 *
 * honggfuzz - utilities
 * -----------------------------------------
 *
 * Author: <NAME> <<EMAIL>>
 *
 * Copyright 2010-2018 by Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 *
 */

#include "libhfcommon/util.h"

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#if !defined(_HF_ARCH_DARWIN) && !defined(__CYGWIN__)
#include <link.h>
#endif /* !defined(_HF_ARCH_DARWIN) && !defined(__CYGWIN__) */
#include <math.h>
#include <pthread.h>
#if defined(_HF_ARCH_LINUX)
#include <sched.h>
#endif /* defined(_HF_ARCH_LINUX) */
#if defined(__FreeBSD__)
#include <pthread_np.h>
#include <sys/cpuset.h>
#include <sys/param.h>
#endif
#if defined(HF_ARCH_NETBSD)
#include <sched.h>
#endif /* defined(HF_ARCH_NETBSD) */
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#if defined(_HF_ARCH_LINUX)
#include <sys/prctl.h>
#endif /* defined(_HF_ARCH_LINUX) */
#if defined(__FreeBSD__)
#include <sys/procctl.h>
#endif /* defined(__FreeBSD__) */
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>

#include "libhfcommon/common.h"
#include "libhfcommon/files.h"
#include "libhfcommon/log.h"

void util_ParentDeathSigIfAvail(int signo HF_ATTR_UNUSED) {
#if defined(__FreeBSD__)
    if (procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &signo) == -1) {
        PLOG_W("procctl(P_PID, PROC_PDEATHSIG_CTL, signo=%d (%s))", signo, util_sigName(signo));
    }
#endif /* defined(__FreeBSD__) */
#if defined(_HF_ARCH_LINUX)
    if (prctl(PR_SET_PDEATHSIG, (unsigned long)signo, 0UL, 0UL, 0UL) == -1) {
        PLOG_W("prctl(PR_SET_PDEATHSIG, signo=%d (%s))", signo, util_sigName(signo));
    }
#endif /* defined(_HF_ARCH_LINUX) */
}

bool util_PinThreadToCPUs(uint32_t threadno, uint32_t cpucnt) {
    if (cpucnt == 0) {
        return true;
    }

    long num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
    if (num_cpus == -1) {
        PLOG_W("sysconf(_SC_NPROCESSORS_ONLN) failed");
        return false;
    }

    uint32_t start_cpu = (threadno * cpucnt) % (uint32_t)num_cpus;
    uint32_t end_cpu   = (start_cpu + cpucnt - 1U) % (uint32_t)num_cpus;

    LOG_D("Setting CPU affinity for the current thread #%" PRIu32 " to %" PRId32
          " consecutive CPUs, (start:%" PRIu32 "-end:%" PRIu32 ") total_cpus:%ld",
        threadno, cpucnt, start_cpu, end_cpu, num_cpus);

    if (cpucnt > (uint32_t)num_cpus) {
        LOG_W("Requested CPUs (%" PRId32 ") > available CPUs (%ld)", cpucnt, num_cpus);
        return false;
    }

#if defined(_HF_ARCH_LINUX) || defined(__FreeBSD__) || defined(HF_ARCH_NETBSD)
#if defined(_HF_ARCH_LINUX)
    cpu_set_t set;
#endif /* defined(_HF_ARCH_LINUX) */
#if defined(__FreeBSD__) || defined(HF_ARCH_NETBSD)
    cpuset_t set;
#endif /* defined(__FreeBSD__) || defined(HF_ARCH_NETBSD) */
    CPU_ZERO(&set);

    for (uint32_t i = 0; i < cpucnt; i++) {
        CPU_SET((start_cpu + i) % num_cpus, &set);
    }
    if (pthread_setaffinity_np(pthread_self(), sizeof(set), &set) != 0) {
        PLOG_W("sched_setaffinity(thread=#%" PRIu32 "), failed", threadno);
        return false;
    }
#endif /* defined(_HF_ARCH_LINUX) || defined(__FreeBSD__) || defined(HF_ARCH_NETBSD) */
    return true;
}

void* util_Malloc(size_t sz) {
    void* p = malloc(sz);
    if (p == NULL) {
        LOG_F("malloc(size='%zu')", sz);
    }
    return p;
}

void* util_Calloc(size_t sz) {
    void* p = util_Malloc(sz);
    memset(p, '\0', sz);
    return p;
}

void* util_AllocCopy(const uint8_t* ptr, size_t sz) {
    void* p = util_Malloc(sz);
    memcpy(p, ptr, sz);
    return p;
}
{'fraction_non_alphanumeric': 0.15675165250236073, 'fraction_numerical': 0.01251180358829084, 'mean_word_length': 3.7735211267605635, 'pattern_counts': {'":': 0, '<': 62, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3348476', 'n_tokens_mistral': 3347, 'n_tokens_neox': 3202, 'n_words': 740}