Dobby 3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Loading...
Searching...
No Matches
Classes | Public Member Functions | Private Types | Static Private Member Functions | Private Attributes | List of all members
DobbyStats Class Reference

Small utility class used to get the stats of a container. More...

#include <DobbyStats.h>

Classes

struct  Process
 

Public Member Functions

 DobbyStats (const ContainerId &id, const std::shared_ptr< IDobbyEnv > &env, const std::shared_ptr< IDobbyUtils > &utils)
 
const Json::Value & stats () const
 

Private Types

typedef struct DobbyStats::Process Process
 

Static Private Member Functions

static ssize_t readCgroupFile (const ContainerId &id, const std::string &cgroupMntPath, const std::string &cgroupfileName, char *buf, size_t bufLen)
 Reads a maximum of 4096 bytes from the given cgroup file.
 
static Json::Value readSingleCgroupValue (const ContainerId &id, const std::string &cgroupMntPath, const std::string &cgroupfileName)
 Reads a single value from the given cgroup file.
 
static std::vector< int64_t > readMultipleCgroupValues (const ContainerId &id, const std::string &cgroupMntPath, const std::string &cgroupfileName)
 Reads multiple values from the given cgroup file.
 
static Json::Value readMultipleCgroupValuesJson (const ContainerId &id, const std::string &cgroupMntPath, const std::string &cgroupfileName)
 
static Json::Value getStats (const ContainerId &id, const std::shared_ptr< IDobbyEnv > &env, const std::shared_ptr< IDobbyUtils > &utils)
 Gets the stats for the container.
 
static Json::Value getProcessTree (const ContainerId &id, const std::string &cpuCgroupMntPath, const std::shared_ptr< IDobbyUtils > &utils)
 Builds a json array of the processes running in the container.
 
static Process getProcessInfo (pid_t pid, const std::shared_ptr< IDobbyUtils > &utils)
 Returns information about a given PID.
 
static pid_t readNsPidFromProc (pid_t pid)
 Given a pid (in global namespace) tries to find what it's namespace pid is.
 

Private Attributes

const Json::Value mStats
 

Detailed Description

Small utility class used to get the stats of a container.

There is not much to this class, it's just a collection of utility functions for gathering statistics on a container with a given id.

Note that the code in this class tends not to print errors if it can't gather some stats, that's by design as it's meant to be a best effort.

Member Function Documentation

◆ getProcessInfo()

DobbyStats::Process DobbyStats::getProcessInfo ( pid_t  pid,
const std::shared_ptr< IDobbyUtils > &  utils 
)
staticprivate

Returns information about a given PID.

Parameters
[in]pidThe PID to get information about
Returns
Process struct containing information about the specified process

◆ getProcessTree()

Json::Value DobbyStats::getProcessTree ( const ContainerId id,
const std::string &  cpuCgroupMntPath,
const std::shared_ptr< IDobbyUtils > &  utils 
)
staticprivate

Builds a json array of the processes running in the container.

Will return a json array with all the processes inside the container with their filename, cmdline and PID (both in the host and container namespace)

"processes": [ { "pid": "2345", "nsPid": "1" "file": "/usr/libexec/DobbyInit", "cmdline": "/usr/libexec/DobbyInit sleep 30", } ]

Parameters
[in]idThe string id of the container.
Returns
Json array with all container processes

◆ getStats()

Json::Value DobbyStats::getStats ( const ContainerId id,
const std::shared_ptr< IDobbyEnv > &  env,
const std::shared_ptr< IDobbyUtils > &  utils 
)
staticprivate

Gets the stats for the container.

This is primarily a debugging method, used to get statistics on the container and roughly correlates to the 'runc events –stats <id>' call.

The reply is a json formatted string containing some info, it's form may change over time.

{
    "timestamp": 348134887768,
    "pids": [ 2046, 2064 ],
    "cpu": {
        "usage": {
            "total":734236982,
            "percpu":[348134887,386102095]
        }
    },
    "memory":{
        "user": {
            "limit":41943040,
            "usage":356352,
            "max":524288,
            "failcnt":0
        }
    }
    "gpu":{
        "memory": {
            "limit":41943040,
            "usage":356352,
            "max":524288,
            "failcnt":0
        }
    }
    ...
}
Parameters
[in]idThe container id, assumed to also be the name of the cgroups.
[in]envThe environment setup, used to get the mount point(s) of the various cgroups.
Returns
The populated json stats, may be an empty object if no cgroups could be found.

◆ readCgroupFile()

ssize_t DobbyStats::readCgroupFile ( const ContainerId id,
const std::string &  cgroupMntPath,
const std::string &  cgroupfileName,
char *  buf,
size_t  bufLen 
)
staticprivate

Reads a maximum of 4096 bytes from the given cgroup file.

The path to read is made up like: <cgroupMntPath>/<id>/<cgroupfileName>

Parameters
[in]idThe string id of the container.
[in]cgroupMntPathThe path to the cgroup mount point.
[in]cgroupfileNameThe name of the cgroup file.
[out]bufBuffer to store the file contents in
[in]bufLenThe size of the buffer.
Returns
The number of characters copied, or

◆ readMultipleCgroupValues()

std::vector< int64_t > DobbyStats::readMultipleCgroupValues ( const ContainerId id,
const std::string &  cgroupMntPath,
const std::string &  cgroupfileName 
)
staticprivate

Reads multiple values from the given cgroup file.

The path to read is made up like:

<cgroupMntPath>/<id>/<cgroupfileName>

Each value is expected to be delimited with either a space, tab or newline.

Parameters
[in]idThe string id of the container.
[in]cgroupMntPathThe path to the cgroup mount point.
[in]cgroupfileNameThe name of the cgroup file.
Returns
The value read as a json object, typically an integer value.

◆ readNsPidFromProc()

pid_t DobbyStats::readNsPidFromProc ( pid_t  pid)
staticprivate

Given a pid (in global namespace) tries to find what it's namespace pid is.

This reads the /proc/<pid>/status file, line NStgid.

Parameters
[in]pidThe real pid of the process to lookup.
Returns
Set of all the real pids within the container.

◆ readSingleCgroupValue()

Json::Value DobbyStats::readSingleCgroupValue ( const ContainerId id,
const std::string &  cgroupMntPath,
const std::string &  cgroupfileName 
)
staticprivate

Reads a single value from the given cgroup file.

The path to read is made up like:

<cgroupMntPath>/<id>/<cgroupfileName>
Parameters
[in]idThe string id of the container.
[in]cgroupMntPathThe path to the cgroup mount point.
[in]cgroupfileNameThe name of the cgroup file.
Returns
The value read as a json object, typically an integer value.

The documentation for this class was generated from the following files: