Blame test/proc_stat.h

Packit Service 724aca
/*
Packit Service 724aca
 * Copyright (C) 2017 - 2020 Intel Corporation.
Packit Service 724aca
 * All rights reserved.
Packit Service 724aca
 *
Packit Service 724aca
 * Redistribution and use in source and binary forms, with or without
Packit Service 724aca
 * modification, are permitted provided that the following conditions are met:
Packit Service 724aca
 * 1. Redistributions of source code must retain the above copyright notice(s),
Packit Service 724aca
 *    this list of conditions and the following disclaimer.
Packit Service 724aca
 * 2. Redistributions in binary form must reproduce the above copyright notice(s),
Packit Service 724aca
 *    this list of conditions and the following disclaimer in the documentation
Packit Service 724aca
 *    and/or other materials provided with the distribution.
Packit Service 724aca
 *
Packit Service 724aca
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS
Packit Service 724aca
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
Packit Service 724aca
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
Packit Service 724aca
 * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit Service 724aca
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit Service 724aca
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Packit Service 724aca
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Packit Service 724aca
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
Packit Service 724aca
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Packit Service 724aca
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 724aca
 */
Packit Service 724aca
Packit Service 724aca
#pragma once
Packit Service 724aca
#include <cstring>
Packit Service 724aca
#include <fstream>
Packit Service 724aca
Packit Service 724aca
class ProcStat
Packit Service 724aca
{
Packit Service 724aca
public:
Packit Service 724aca
    size_t get_virtual_memory_size_bytes()
Packit Service 724aca
    {
Packit Service 724aca
        get_stat("VmSize", str_value);
Packit Service 724aca
        return strtol(str_value, NULL, 10) * 1024;
Packit Service 724aca
    }
Packit Service 724aca
Packit Service 724aca
    size_t get_physical_memory_size_bytes()
Packit Service 724aca
    {
Packit Service 724aca
        get_stat("VmRSS", str_value);
Packit Service 724aca
        return strtol(str_value, NULL, 10) * 1024;
Packit Service 724aca
    }
Packit Service 724aca
Packit Service 724aca
    size_t get_used_swap_space_size_bytes(void)
Packit Service 724aca
    {
Packit Service 724aca
        get_stat("VmSwap", str_value);
Packit Service 724aca
        return strtol(str_value, NULL, 10) * 1024;
Packit Service 724aca
    }
Packit Service 724aca
private:
Packit Service 724aca
    /* We are avoiding to allocate local buffers,
Packit Service 724aca
     * since it can produce noise in memory footprint tests.
Packit Service 724aca
     */
Packit Service 724aca
    char line[1024];
Packit Service 724aca
    char current_entry_name[1024];
Packit Service 724aca
    char str_value[1024];
Packit Service 724aca
Packit Service 724aca
    // Note: this function is not thread-safe.
Packit Service 724aca
    void get_stat(const char *field_name, char *value)
Packit Service 724aca
    {
Packit Service 724aca
        char *pos = nullptr;
Packit Service 724aca
        std::ifstream file("/proc/self/status", std::ifstream::in);
Packit Service 724aca
        if (file.is_open()) {
Packit Service 724aca
            while (file.getline(line, sizeof(line))) {
Packit Service 724aca
                pos = strstr(line, field_name);
Packit Service 724aca
                if (pos) {
Packit Service 724aca
                    sscanf(pos, "%64[a-zA-Z_0-9()]: %s", current_entry_name, value);
Packit Service 724aca
                    break;
Packit Service 724aca
                }
Packit Service 724aca
            }
Packit Service 724aca
            file.close();
Packit Service 724aca
        }
Packit Service 724aca
    }
Packit Service 724aca
};
Packit Service 724aca