powerd++  0.4.4
Classes | Namespaces | Typedefs | Functions | Variables
libloadplay.cpp File Reference

Implements a library intended to be injected into a clock frequency deamon via LD_PRELOAD. More...

#include "utility.hpp"
#include "constants.hpp"
#include "version.hpp"
#include "sys/env.hpp"
#include "sys/io.hpp"
#include <unordered_map>
#include <map>
#include <string>
#include <regex>
#include <memory>
#include <thread>
#include <exception>
#include <mutex>
#include <chrono>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <csignal>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/resource.h>
#include <libutil.h>
#include <dlfcn.h>
#include <unistd.h>
Include dependency graph for libloadplay.cpp:

Classes

struct  anonymous_namespace{libloadplay.cpp}::mib_t
 Represents MIB, but wraps it to provide the necessary operators to use it as an std::map key. More...
 
class  anonymous_namespace{libloadplay.cpp}::Callback< FunctionArgs >
 Implements a recursion safe std::function wrapper. More...
 
class  anonymous_namespace{libloadplay.cpp}::SysctlValue
 Instances of this class represents a specific sysctl value. More...
 
class  anonymous_namespace{libloadplay.cpp}::Sysctls
 Singleton class representing the sysctl table for this library. More...
 
struct  anonymous_namespace{libloadplay.cpp}::CoreReport
 The reported state of a single CPU pipeline. More...
 
struct  anonymous_namespace{libloadplay.cpp}::CoreFrameReport
 The report frame information for a single CPU pipeline. More...
 
class  anonymous_namespace{libloadplay.cpp}::Report
 Provides a mechanism to provide frame wise per core load information. More...
 
class  anonymous_namespace{libloadplay.cpp}::Report::Frame
 Represents a frame of the report. More...
 
class  anonymous_namespace{libloadplay.cpp}::Emulator
 Instances of this class represent an emulator session. More...
 
struct  anonymous_namespace{libloadplay.cpp}::Emulator::Core
 Per core information. More...
 
class  anonymous_namespace{libloadplay.cpp}::Main
 Singleton class representing the main execution environment. More...
 
class  anonymous_namespace{libloadplay.cpp}::Hold< T >
 Sets a referenced variable to a given value and restores it when going out of context. More...
 

Namespaces

 anonymous_namespace{libloadplay.cpp}
 File local scope.
 

Typedefs

using anonymous_namespace{libloadplay.cpp}::cycles_t = uint64_t
 Clock cycle counting type.
 
template<auto Ownership>
using anonymous_namespace{libloadplay.cpp}::ofile = io::file< Ownership, io::write >
 Output file type alias. More...
 
template<auto Ownership>
using anonymous_namespace{libloadplay.cpp}::ifile = io::file< Ownership, io::read >
 Input file type alias. More...
 

Functions

template<size_t Size>
int anonymous_namespace{libloadplay.cpp}::strcmp (char const *const s1, char const (&s2)[Size])
 Safe wrapper around strncmp, which automatically determines the buffer size of s2. More...
 
std::regex anonymous_namespace{libloadplay.cpp}::operator""_r (char const *const str, size_t const len)
 User defined literal for regular expressions. More...
 
template<typename ... ArgTs>
constexpr void anonymous_namespace{libloadplay.cpp}::dprintf (ArgTs &&... args)
 Calls io::ferr.printf(...) if built with -DEBUG. More...
 
template<>
std::string anonymous_namespace{libloadplay.cpp}::SysctlValue::get< std::string > () const
 Returns a copy of the value string. More...
 
template<typename... MsgTs>
ofile< io::link > anonymous_namespace{libloadplay.cpp}::debug (MsgTs &&... msg)
 Print a debugging message if built with -DEBUG. More...
 
template<typename... MsgTs>
ofile< io::link > anonymous_namespace{libloadplay.cpp}::warn (MsgTs &&... msg)
 Print a warning. More...
 
int anonymous_namespace{libloadplay.cpp}::sys_result (int const result)
 Combine sys_results with a computed result. More...
 
template<typename... MsgTs>
ofile< io::link > anonymous_namespace{libloadplay.cpp}::fail (MsgTs &&... msg)
 This prints an error message and sets sys_results to make the hijacked process fail. More...
 
ofile< io::link > anonymous_namespace{libloadplay.cpp}::operator<< (ofile< io::link > fout, CoreFrameReport const &frame)
 Print recorded and running clock frequency and load for a frame. More...
 
int sysctl (const int *name, u_int namelen, void *oldp, size_t *oldlenp, const void *newp, size_t newlen)
 Functions to intercept. More...
 
int sysctlnametomib (const char *name, int *mibp, size_t *sizep)
 Intercept calls to sysctlnametomib(). More...
 
int daemon (int, int)
 Intercept calls to daemon(). More...
 
uid_t geteuid (void)
 Intercept calls to geteuid(). More...
 
pidfh * pidfile_open (const char *, mode_t, pid_t *)
 Intercept calls to pidfile_open(). More...
 
int pidfile_write (pidfh *)
 Intercept calls to pidfile_write(). More...
 
int pidfile_close (pidfh *)
 Intercept calls to pidfile_close(). More...
 
int pidfile_remove (pidfh *)
 Intercept calls to pidfile_remove(). More...
 
int pidfile_fileno (pidfh const *)
 Intercept calls to pidfile_fileno(). More...
 

Variables

constexpr const flag_t anonymous_namespace{libloadplay.cpp}::FEATURES
 The set of supported features. More...
 
int anonymous_namespace{libloadplay.cpp}::sys_results = 0
 The success return value of intercepted functions.
 
class anonymous_namespace{libloadplay.cpp}::Sysctls anonymous_namespace{libloadplay.cpp}::sysctls
 Sole instance of Sysctls.
 
bool anonymous_namespace{libloadplay.cpp}::sysctl_startup = true
 Set to activate fallback to the original sysctl functions. More...
 
class anonymous_namespace{libloadplay.cpp}::Main anonymous_namespace{libloadplay.cpp}::main
 Sole instance of Main.
 

Detailed Description

Implements a library intended to be injected into a clock frequency deamon via LD_PRELOAD.

This library reads instructions from io::fin (stdin) and outputs statistics about the hijacked process on io::fout (stdout).

The following environment variables affect the operation of loadplay:

Variable Description
LOADPLAY_IN Alternative input file
LOADPLAY_OUT Alternative output file

Class Documentation

◆ anonymous_namespace{libloadplay.cpp}::CoreReport

struct anonymous_namespace{libloadplay.cpp}::CoreReport

The reported state of a single CPU pipeline.

Collaboration diagram for anonymous_namespace{libloadplay.cpp}::CoreReport:
[legend]
Class Members
mhz_t freq The core clock frequency in [MHz].
double load The core load as a fraction.

◆ anonymous_namespace{libloadplay.cpp}::CoreFrameReport

struct anonymous_namespace{libloadplay.cpp}::CoreFrameReport

The report frame information for a single CPU pipeline.

Collaboration diagram for anonymous_namespace{libloadplay.cpp}::CoreFrameReport:
[legend]
Class Members
CoreReport rec The recorded core state.
CoreReport run The running core state.

◆ anonymous_namespace{libloadplay.cpp}::Emulator::Core

struct anonymous_namespace{libloadplay.cpp}::Emulator::Core

Per core information.

Collaboration diagram for anonymous_namespace{libloadplay.cpp}::Emulator::Core:
[legend]
Class Members
cycles_t carryCycles[CPUSTATES] The cycles carried over to the next frame in [cycles].

This is determined at the beginning of frame and used to calculated the simulation load at the beginning of the next frame.

SysctlValue * freqCtl The sysctl handler.

The constructor ensures this points to a valid handler.

mhz_t recFreq The recorded clock frequency.

If FREQ_TRACKING is enabled this is updated at during the preliminary stage and used at the beginning of frame stage.

mhz_t runFreq The clock frequency the simulation is running at.

Updated at the end of frame and used in the next frame.

cycles_t runLoadCycles The load cycles simulated for this frame in [cycles].

This is determined at the beginning of frame and used to calculate the reported load at the end of frame.

Function Documentation

◆ daemon()

int daemon ( int  ,
int   
)

Intercept calls to daemon().

Prevents process from separating from the controlling terminal.

Returns
The value of sys_results

◆ geteuid()

uid_t geteuid ( void  )

Intercept calls to geteuid().

Tells the asking process that it is running as root.

Returns
Always returns 0

◆ pidfile_close()

int pidfile_close ( pidfh *  )

Intercept calls to pidfile_close().

Returns
The value of sys_results

◆ pidfile_fileno()

int pidfile_fileno ( pidfh const *  )

Intercept calls to pidfile_fileno().

Returns
The value of sys_results

◆ pidfile_open()

pidfh* pidfile_open ( const char *  ,
mode_t  ,
pid_t *   
)

Intercept calls to pidfile_open().

Prevents pidfile locking and creation by the hijacked process.

Returns
A dummy pointer

◆ pidfile_remove()

int pidfile_remove ( pidfh *  )

Intercept calls to pidfile_remove().

Returns
The value of sys_results

◆ pidfile_write()

int pidfile_write ( pidfh *  )

Intercept calls to pidfile_write().

Returns
The value of sys_results

◆ sysctl()

int sysctl ( const int *  name,
u_int  namelen,
void *  oldp,
size_t *  oldlenp,
const void *  newp,
size_t  newlen 
)

Functions to intercept.

Intercept calls to sysctl().

Uses the local anonymous_namespace{libloadplay::cpp}::sysctls store.

Falls back to the original under the following conditions:

  • sysctl_startup is set
  • The mib is not known to the simulation

The call may fail for 3 reasons:

  1. The fail() function was called and sys_results was assigned -1
  2. A target buffer was too small (errno == ENOMEM)
  3. The given sysctl is not in the sysctls store (errno == ENOENT)
Parameters
name,namelen,oldp,oldlenp,newp,newlenPlease refer to sysctl(3)
Return values
0The call succeeded
-1The call failed

◆ sysctlnametomib()

int sysctlnametomib ( const char *  name,
int *  mibp,
size_t *  sizep 
)

Intercept calls to sysctlnametomib().

Parameters
name,mibp,sizepPlease refer to sysctl(3)
Return values
0The call succeeded
-1The call failed