Blame rdtset/common.h

Packit Service 5befb9
/*
Packit Service 5befb9
 *   BSD LICENSE
Packit Service 5befb9
 *
Packit Service 8a4b7a
 *   Copyright(c) 2016-2020 Intel Corporation. All rights reserved.
Packit Service 5befb9
 *   All rights reserved.
Packit Service 5befb9
 *
Packit Service 5befb9
 *   Redistribution and use in source and binary forms, with or without
Packit Service 5befb9
 *   modification, are permitted provided that the following conditions
Packit Service 5befb9
 *   are met:
Packit Service 5befb9
 *
Packit Service 5befb9
 *     * Redistributions of source code must retain the above copyright
Packit Service 5befb9
 *       notice, this list of conditions and the following disclaimer.
Packit Service 5befb9
 *     * Redistributions in binary form must reproduce the above copyright
Packit Service 5befb9
 *       notice, this list of conditions and the following disclaimer in
Packit Service 5befb9
 *       the documentation and/or other materials provided with the
Packit Service 5befb9
 *       distribution.
Packit Service 5befb9
 *     * Neither the name of Intel Corporation nor the names of its
Packit Service 5befb9
 *       contributors may be used to endorse or promote products derived
Packit Service 5befb9
 *       from this software without specific prior written permission.
Packit Service 5befb9
 *
Packit Service 5befb9
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit Service 5befb9
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit Service 5befb9
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit Service 5befb9
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit Service 5befb9
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit Service 5befb9
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit Service 5befb9
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 5befb9
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 5befb9
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 5befb9
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit Service 5befb9
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 5befb9
 */
Packit Service 5befb9
Packit Service 5befb9
#ifndef _COMMON_H
Packit Service 5befb9
#define _COMMON_H
Packit Service 5befb9
Packit Service 5befb9
#include <stdint.h>
Packit Service 5befb9
Packit Service 5befb9
#include <pqos.h>
Packit Service 5befb9
Packit Service 5befb9
#include "cpu.h"
Packit Service 5befb9
Packit Service 5befb9
#ifdef __cplusplus
Packit Service 5befb9
extern "C" {
Packit Service 5befb9
#endif
Packit Service 5befb9
Packit Service 8a4b7a
#define RDT_MAX_PIDS   128
Packit Service 8a4b7a
#define MAX_OPTARG_LEN 64
Packit Service 8a4b7a
Packit Service 8a4b7a
/**
Packit Service 8a4b7a
 * MBA linear max value.
Packit Service 8a4b7a
 */
Packit Service 8a4b7a
#define RDT_MAX_MBA 100
Packit Service 8a4b7a
Packit Service 8a4b7a
/**
Packit Service 8a4b7a
 * MBA max value for AMD.
Packit Service 8a4b7a
 */
Packit Service 8a4b7a
#define RDT_MAX_MBA_AMD 0x800
Packit Service 5befb9
Packit Service 5befb9
#ifndef MIN
Packit Service 5befb9
/**
Packit Service 5befb9
 * Macro to return the minimum of two numbers
Packit Service 5befb9
 */
Packit Service 8a4b7a
#define MIN(a, b)                                                              \
Packit Service 8a4b7a
        ({                                                                     \
Packit Service 8a4b7a
                typeof(a) _a = (a);                                            \
Packit Service 8a4b7a
                typeof(b) _b = (b);                                            \
Packit Service 8a4b7a
                _a < _b ? _a : _b;                                             \
Packit Service 8a4b7a
        })
Packit Service 5befb9
#endif /* !MIN */
Packit Service 5befb9
Packit Service 5befb9
#ifndef MAX
Packit Service 5befb9
/**
Packit Service 5befb9
 * Macro to return the maximum of two numbers
Packit Service 5befb9
 */
Packit Service 8a4b7a
#define MAX(a, b)                                                              \
Packit Service 8a4b7a
        ({                                                                     \
Packit Service 8a4b7a
                typeof(a) _a = (a);                                            \
Packit Service 8a4b7a
                typeof(b) _b = (b);                                            \
Packit Service 8a4b7a
                _a > _b ? _a : _b;                                             \
Packit Service 8a4b7a
        })
Packit Service 5befb9
#endif /* !MAX */
Packit Service 5befb9
Packit Service 5befb9
#ifndef DIM
Packit Service 8a4b7a
#define DIM(x) (sizeof(x) / sizeof(x[0]))
Packit Service 5befb9
#endif /* !DIM */
Packit Service 5befb9
Packit Service 5befb9
#ifdef __FreeBSD__
Packit Service 5befb9
/* Fix for "undefined reference to '__bitcountl'" */
Packit Service 5befb9
#ifndef __bitcountl
Packit Service 5befb9
#define __bitcountl(x) __builtin_popcountl((unsigned long)(x))
Packit Service 5befb9
#endif /* !__bitcountl(x) */
Packit Service 5befb9
#endif /* __FreeBSD__ */
Packit Service 5befb9
Packit Service 5befb9
struct rdt_cfg {
Packit Service 8a4b7a
        enum pqos_cap_type type;
Packit Service 8a4b7a
        union {
Packit Service 8a4b7a
                struct pqos_l2ca *l2;
Packit Service 8a4b7a
                struct pqos_l3ca *l3;
Packit Service 8a4b7a
                struct pqos_mba *mba;
Packit Service 8a4b7a
                void *generic_ptr;
Packit Service 8a4b7a
        } u;
Packit Service 5befb9
};
Packit Service 5befb9
Packit Service 5befb9
/**
Packit Service 5befb9
 * @brief Creates \a rdt_cfg struct from \a pqos_l2ca struct
Packit Service 5befb9
 *
Packit Service 5befb9
 * @param [in] l2 L2 CAT class configuration
Packit Service 5befb9
 *
Packit Service 5befb9
 * @return rdt_cfg struct
Packit Service 5befb9
 */
Packit Service 8a4b7a
static inline struct rdt_cfg
Packit Service 8a4b7a
wrap_l2ca(struct pqos_l2ca *l2)
Packit Service 5befb9
{
Packit Service 8a4b7a
        struct rdt_cfg result;
Packit Service 5befb9
Packit Service 8a4b7a
        result.type = PQOS_CAP_TYPE_L2CA;
Packit Service 8a4b7a
        result.u.l2 = l2;
Packit Service 8a4b7a
        return result;
Packit Service 5befb9
}
Packit Service 5befb9
Packit Service 5befb9
/**
Packit Service 5befb9
 * @brief Creates \a rdt_cfg struct from \a pqos_l3ca struct
Packit Service 5befb9
 *
Packit Service 5befb9
 * @param [in] l3 L3 CAT class configuration
Packit Service 5befb9
 *
Packit Service 5befb9
 * @return rdt_cfg struct
Packit Service 5befb9
 */
Packit Service 8a4b7a
static inline struct rdt_cfg
Packit Service 8a4b7a
wrap_l3ca(struct pqos_l3ca *l3)
Packit Service 5befb9
{
Packit Service 8a4b7a
        struct rdt_cfg result;
Packit Service 5befb9
Packit Service 8a4b7a
        result.type = PQOS_CAP_TYPE_L3CA;
Packit Service 8a4b7a
        result.u.l3 = l3;
Packit Service 8a4b7a
        return result;
Packit Service 5befb9
}
Packit Service 5befb9
Packit Service 5befb9
/**
Packit Service 5befb9
 * @brief Creates \a rdt_cfg struct from \a pqos_mba struct
Packit Service 5befb9
 *
Packit Service 5befb9
 * @param [in] mba MBA class configuration
Packit Service 5befb9
 *
Packit Service 5befb9
 * @return rdt_cfg struct
Packit Service 5befb9
 */
Packit Service 8a4b7a
static inline struct rdt_cfg
Packit Service 8a4b7a
wrap_mba(struct pqos_mba *mba)
Packit Service 5befb9
{
Packit Service 8a4b7a
        struct rdt_cfg result;
Packit Service 5befb9
Packit Service 8a4b7a
        result.type = PQOS_CAP_TYPE_MBA;
Packit Service 8a4b7a
        result.u.mba = mba;
Packit Service 8a4b7a
        return result;
Packit Service 5befb9
}
Packit Service 5befb9
Packit Service 5befb9
struct rdt_config {
Packit Service 8a4b7a
        cpu_set_t cpumask;   /**< CPUs bitmask */
Packit Service 8a4b7a
        struct pqos_l3ca l3; /**< L3 configuration */
Packit Service 8a4b7a
        struct pqos_l2ca l2; /**< L2 configuration */
Packit Service 8a4b7a
        struct pqos_mba mba; /**< MBA configuretion */
Packit Service 8a4b7a
        int pid_cfg;         /**< associate PIDs to this cfg */
Packit Service 5befb9
};
Packit Service 5befb9
Packit Service 5befb9
/* rdtset command line configuration structure */
Packit Service 5befb9
struct rdtset {
Packit Service 8a4b7a
        pid_t pids[RDT_MAX_PIDS];              /**< process ID table */
Packit Service 8a4b7a
        unsigned pid_count;                    /**< Num of PIDs selected */
Packit Service 8a4b7a
        struct rdt_config config[CPU_SETSIZE]; /**< RDT configuration */
Packit Service 8a4b7a
        unsigned config_count;                 /**< Num of RDT config entries */
Packit Service 8a4b7a
        cpu_set_t cpu_aff_cpuset;      /**< CPU affinity configuration */
Packit Service 8a4b7a
        cpu_set_t reset_cpuset;        /**< List of CPUs to reset COS assoc */
Packit Service 8a4b7a
        unsigned sudo_keep : 1,        /**< don't drop elevated privileges */
Packit Service 8a4b7a
            verbose : 1,               /**< be verbose */
Packit Service 8a4b7a
            command : 1,               /**< command to be executed detected */
Packit Service 8a4b7a
            show_version : 1;          /**< print library version */
Packit Service 8a4b7a
        enum pqos_interface interface; /**< pqos interface to use */
Packit Service 5befb9
};
Packit Service 5befb9
Packit Service 8a4b7a
extern struct rdtset g_cfg;
Packit Service 8a4b7a
Packit Service 8a4b7a
#define DBG(...)                                                               \
Packit Service 8a4b7a
        do {                                                                   \
Packit Service 8a4b7a
                if (g_cfg.verbose)                                             \
Packit Service 8a4b7a
                        fprintf(stderr, __VA_ARGS__);                          \
Packit Service 8a4b7a
        } while (0)
Packit Service 5befb9
Packit Service 5befb9
/**
Packit Service 5befb9
 * @brief Parse CPU set string
Packit Service 5befb9
 *
Packit Service 5befb9
 * @note Parse elem, the elem could be single number/range or group
Packit Service 5befb9
 *       1) A single number elem, it's just a simple digit. e.g. 9
Packit Service 5befb9
 *       2) A single range elem, two digits with a '-' between. e.g. 2-6
Packit Service 5befb9
 *       3) A group elem, combines multiple 1) or 2) with e.g 0,2-4,6
Packit Service 5befb9
 *       Within group elem, '-' used for a range separator;
Packit Service 5befb9
 *       ',' used for a single number.
Packit Service 5befb9
 *
Packit Service 5befb9
 * @param [in] cpustr string representation of a cpu set
Packit Service 5befb9
 * @param [in] cpustr_len len of \a cpustr
Packit Service 5befb9
 * @param [out] cpuset parsed cpuset
Packit Service 5befb9
 *
Packit Service 5befb9
 * @return number of parsed characters on success
Packit Service 5befb9
 * @retval -ERRNO on error
Packit Service 5befb9
 */
Packit Service 8a4b7a
int
Packit Service 8a4b7a
str_to_cpuset(const char *cpustr, const unsigned cpustr_len, cpu_set_t *cpuset);
Packit Service 5befb9
Packit Service 5befb9
/**
Packit Service 5befb9
 * @brief Converts CPU set (cpu_set_t) to string
Packit Service 5befb9
 *
Packit Service 5befb9
 * @param [out] cpustr output string
Packit Service 5befb9
 * @param [in] cpustr_len max output string len
Packit Service 5befb9
 * @param [in] cpumask input cpuset
Packit Service 5befb9
 */
Packit Service 8a4b7a
void cpuset_to_str(char *cpustr,
Packit Service 8a4b7a
                   const unsigned cpustr_len,
Packit Service 8a4b7a
                   const cpu_set_t *cpumask);
Packit Service 5befb9
Packit Service 5befb9
/**
Packit Service 5befb9
 * @brief Converts string of characters representing list of
Packit Service 5befb9
 *        numbers into table of numbers.
Packit Service 5befb9
 *
Packit Service 5befb9
 * Allowed formats are:
Packit Service 5befb9
 *     0,1,2,3
Packit Service 5befb9
 *     0-10,20-18
Packit Service 5befb9
 *     1,3,5-8,10,0x10-12
Packit Service 5befb9
 *
Packit Service 5befb9
 * Numbers can be in decimal or hexadecimal format.
Packit Service 5befb9
 *
Packit Service 5befb9
 * On error, this functions causes process to exit with FAILURE code.
Packit Service 5befb9
 *
Packit Service 5befb9
 * @param s string representing list of unsigned numbers.
Packit Service 5befb9
 * @param tab table to put converted numeric values into
Packit Service 5befb9
 * @param max maximum number of elements that \a tab can accommodate
Packit Service 5befb9
 *
Packit Service 5befb9
 * @return Number of elements placed into \a tab
Packit Service 5befb9
 */
Packit Service 8a4b7a
unsigned strlisttotab(char *s, uint64_t *tab, const unsigned max);
Packit Service 8a4b7a
Packit Service 8a4b7a
/**
Packit Service 8a4b7a
 * @brief Get time in microseconds
Packit Service 8a4b7a
 *
Packit Service 8a4b7a
 * @param tv pointer to timeval structure to be converted
Packit Service 8a4b7a
 *
Packit Service 8a4b7a
 * @return Time un mocroseconds
Packit Service 8a4b7a
 */
Packit Service 8a4b7a
uint64_t get_time_usec(void);
Packit Service 8a4b7a
Packit Service 8a4b7a
/**
Packit Service 8a4b7a
 * @brief Scale MB value to bytes
Packit Service 8a4b7a
 *
Packit Service 8a4b7a
 * @param [in] MB value to be scaled
Packit Service 8a4b7a
 * @return scaled up value in bytes
Packit Service 8a4b7a
 */
Packit Service 8a4b7a
static inline uint64_t
Packit Service 8a4b7a
mb_to_bytes(const uint64_t mb)
Packit Service 8a4b7a
{
Packit Service 8a4b7a
        return mb * 1024 * 1024;
Packit Service 8a4b7a
}
Packit Service 8a4b7a
Packit Service 8a4b7a
/**
Packit Service 8a4b7a
 * @brief Scale bytes value to MB
Packit Service 8a4b7a
 *
Packit Service 8a4b7a
 * @param [in] bytes value to be scaled up
Packit Service 8a4b7a
 * @return scaled value in MB
Packit Service 8a4b7a
 */
Packit Service 8a4b7a
static inline uint64_t
Packit Service 8a4b7a
bytes_to_mb(const uint64_t bytes)
Packit Service 8a4b7a
{
Packit Service 8a4b7a
        return bytes / (1024 * 1024);
Packit Service 8a4b7a
}
Packit Service 5befb9
Packit Service 5befb9
#ifdef __cplusplus
Packit Service 5befb9
}
Packit Service 5befb9
#endif
Packit Service 5befb9
Packit Service 5befb9
#endif /* _COMMON_H */